Ejemplo n.º 1
0
 def test_constructor(self):
     self.assertEqual(str(idset.IDset()), "")
     self.assertEqual(str(idset.IDset(idset.IDset())), "")
     self.assertEqual(str(idset.IDset(0)), "0")
     self.assertEqual(str(idset.IDset(42)), "42")
     self.assertEqual(str(idset.IDset([42])), "42")
     self.assertEqual(str(idset.IDset("40,41,42")), "40-42")
     self.assertEqual(str(idset.IDset([40, 41, 42])), "40-42")
     self.assertEqual(str(idset.IDset([42, 41, 40])), "40-42")
Ejemplo n.º 2
0
 def test_equal(self):
     ids1 = idset.IDset()
     ids2 = idset.IDset()
     self.assertEqual(ids1, ids2)
     self.assertTrue(ids1.equal(ids2))
     ids1.set(0, 9)
     ids2.set(0, 9)
     self.assertEqual(ids1, ids2)
     self.assertTrue(ids2.equal(ids1))
     ids1.clear(0)
     self.assertNotEqual(ids1, ids2)
     self.assertFalse(ids1.equal(ids2))
     with self.assertRaises(TypeError):
         ids1 == "foo"
     with self.assertRaises(TypeError):
         ids1.equal("foo")
Ejemplo n.º 3
0
    def test_set_and_clear_exceptions(self):
        ids = idset.IDset()
        self.assertRaises(ValueError, lambda x: x.set(-1), ids)
        self.assertRaises(ValueError, lambda x: x.set(1, -1), ids)
        self.assertRaises(TypeError, lambda x: x.set("a"), ids)
        self.assertRaises(ValueError, lambda x: x.set(5, 1), ids)

        self.assertRaises(ValueError, lambda x: x.clear(-1), ids)
        self.assertRaises(ValueError, lambda x: x.clear(1, -1), ids)
        self.assertRaises(TypeError, lambda x: x.clear("a"), ids)
        self.assertRaises(ValueError, lambda x: x.clear(5, 1), ids)
Ejemplo n.º 4
0
    def test_set_and_clear(self):
        ids = idset.IDset()
        ids.set(3)
        self.assertTrue(ids[3])
        self.assertEqual(str(ids), "3")
        ids.clear(3)
        self.assertFalse(ids[3])
        self.assertEqual(str(ids), "")

        ids.set(3, 7)
        self.assertEqual(str(ids), "3-7")

        ids.clear(4, 5)
        self.assertEqual(str(ids), "3,6-7")
Ejemplo n.º 5
0
    def test_add_subtract(self):
        ids = idset.decode("0-9")
        ids2 = ids.copy()

        self.assertEqual(str(ids.add("10-11")), "0-11")
        self.assertEqual(str(ids.add([20, 21])), "0-11,20-21")
        self.assertEqual(str(ids.add(idset.decode(""))), "0-11,20-21")

        ids2 += "10-11"
        self.assertEqual(str(ids2), "0-11")
        ids2 += [20, 21]
        self.assertEqual(str(ids2), "0-11,20-21")
        ids2 += idset.decode("")
        self.assertEqual(str(ids2), "0-11,20-21")

        self.assertEqual(str(ids.subtract([])), "0-11,20-21")
        self.assertEqual(str(ids.subtract("11-20")), "0-10,21")
        self.assertEqual(str(ids.subtract(idset.decode("0-10"))), "21")
        self.assertEqual(str(ids.subtract([21])), "")

        ids2 -= ""
        self.assertEqual(str(ids2), "0-11,20-21")
        ids2 -= idset.IDset()
        self.assertEqual(str(ids2), "0-11,20-21")
        ids2 -= "11-20"
        self.assertEqual(str(ids2), "0-10,21")
        ids2 -= idset.decode("0-10")
        self.assertEqual(str(ids2), "21")
        ids2 -= 21
        self.assertEqual(str(ids2), "")

        with self.assertRaises(ValueError):
            ids.subtract("foo")
        with self.assertRaises(TypeError):
            ids.subtract(42.0)
        with self.assertRaises(ValueError):
            ids.add("foo")
        with self.assertRaises(TypeError):
            ids.add(42.0)