예제 #1
0
 def setUp(self):
     self.case   = "unit set (number)"
     self.values = [3]
     self.set    = set(self.values)
     self.dup    = set(self.values)
     self.length = 1
     self.repr   = "%s([3])" % set.__name__
예제 #2
0
 def setUp(self):
     self.case   = "empty set"
     self.values = []
     self.set    = set(self.values)
     self.dup    = set(self.values)
     self.length = 0
     self.repr   = "%s([])" % set.__name__
예제 #3
0
 def setUp(self):
     self.case   = "unit set (tuple)"
     self.values = [(0, "zero")]
     self.set    = set(self.values)
     self.dup    = set(self.values)
     self.length = 1
     self.repr   = "%s([(0, 'zero')])" % set.__name__
예제 #4
0
 def test_equality(self):
     self.assertEqual(self.s, set(self.word))
     self.assertEqual(self.s, frozenset(self.word))
     self.assertEqual(self.s == self.word, False)
     self.assertNotEqual(self.s, set(self.otherword))
     self.assertNotEqual(self.s, frozenset(self.otherword))
     self.assertEqual(self.s != self.word, True)
예제 #5
0
 def setUp(self):
     self.case   = "triple set"
     self.values = [0, "zero", operator.add]
     self.set    = set(self.values)
     self.dup    = set(self.values)
     self.length = 3
     self.repr   = None
예제 #6
0
 def test_init(self):
     s = self.thetype()
     s.__init__(self.word)
     self.assertEqual(s, set(self.word))
     s.__init__(self.otherword)
     self.assertEqual(s, set(self.otherword))
     self.assertRaises(TypeError, s.__init__, s, 2);
     self.assertRaises(TypeError, s.__init__, 1);
예제 #7
0
 def test_constructor(self):
     inner = frozenset([1])
     outer = set([inner])
     element = outer.pop()
     self.assertEqual(type(element), frozenset)
     outer.add(inner)        # Rebuild set of sets with .add method
     outer.remove(inner)
     self.assertEqual(outer, set())   # Verify that remove worked
     outer.discard(inner)    # Absence of KeyError indicates working fine
예제 #8
0
    def test_cmp(self):
        a, b = set('a'), set('b')
        self.assertRaises(TypeError, cmp, a, b)

        # You can view this as a buglet:  cmp(a, a) does not raise TypeError,
        # because __eq__ is tried before __cmp__, and a.__eq__(a) returns True,
        # which Python thinks is good enough to synthesize a cmp() result
        # without calling __cmp__.
        self.assertEqual(cmp(a, a), 0)

        self.assertRaises(TypeError, cmp, a, 12)
        self.assertRaises(TypeError, cmp, "abc", a)
예제 #9
0
 def test_intersection(self):
     i = self.s.intersection(self.otherword)
     for c in self.letters:
         self.assertEqual(c in i, c in self.d and c in self.otherword)
     self.assertEqual(self.s, self.thetype(self.word))
     self.assertEqual(type(i), self.thetype)
     self.assertRaises(PassThru, self.s.intersection, check_pass_thru())
     for C in set, frozenset, dict.fromkeys, str, unicode, list, tuple:
         self.assertEqual(self.thetype('abcba').intersection(C('cdc')), set('cc'))
         self.assertEqual(self.thetype('abcba').intersection(C('efgfe')), set(''))
         self.assertEqual(self.thetype('abcba').intersection(C('ccb')), set('bc'))
         self.assertEqual(self.thetype('abcba').intersection(C('ef')), set(''))
예제 #10
0
 def test_union(self):
     u = self.s.union(self.otherword)
     for c in self.letters:
         self.assertEqual(c in u, c in self.d or c in self.otherword)
     self.assertEqual(self.s, self.thetype(self.word))
     self.assertEqual(type(u), self.thetype)
     self.assertRaises(PassThru, self.s.union, check_pass_thru())
     self.assertRaises(TypeError, self.s.union, [[]])
     for C in set, frozenset, dict.fromkeys, str, unicode, list, tuple:
         self.assertEqual(self.thetype('abcba').union(C('cdc')), set('abcd'))
         self.assertEqual(self.thetype('abcba').union(C('efgfe')), set('abcefg'))
         self.assertEqual(self.thetype('abcba').union(C('ccb')), set('abc'))
         self.assertEqual(self.thetype('abcba').union(C('ef')), set('abcef'))
예제 #11
0
 def test_symmetric_difference(self):
     i = self.s.symmetric_difference(self.otherword)
     for c in self.letters:
         self.assertEqual(c in i, (c in self.d) ^ (c in self.otherword))
     self.assertEqual(self.s, self.thetype(self.word))
     self.assertEqual(type(i), self.thetype)
     self.assertRaises(PassThru, self.s.symmetric_difference, check_pass_thru())
     self.assertRaises(TypeError, self.s.symmetric_difference, [[]])
     for C in set, frozenset, dict.fromkeys, str, unicode, list, tuple:
         self.assertEqual(self.thetype('abcba').symmetric_difference(C('cdc')), set('abd'))
         self.assertEqual(self.thetype('abcba').symmetric_difference(C('efgfe')), set('abcefg'))
         self.assertEqual(self.thetype('abcba').symmetric_difference(C('ccb')), set('a'))
         self.assertEqual(self.thetype('abcba').symmetric_difference(C('ef')), set('abcef'))
예제 #12
0
    def test_inplace_methods(self):
        for data in ("123", "", range(1000), ('do', 1.2), xrange(2000,2200,5), 'december'):
            for methname in ('update', 'intersection_update',
                             'difference_update', 'symmetric_difference_update'):
                for g in (G, I, Ig, S, L, R):
                    s = set('january')
                    t = s.copy()
                    getattr(s, methname)(list(g(data)))
                    getattr(t, methname)(g(data))
                    self.assertEqual(sorted(s), sorted(t))

                self.assertRaises(TypeError, getattr(set('january'), methname), X(data))
                self.assertRaises(TypeError, getattr(set('january'), methname), N(data))
                self.assertRaises(ZeroDivisionError, getattr(set('january'), methname), E(data))
예제 #13
0
 def test_sub_and_super(self):
     p, q, r = map(self.thetype, ['ab', 'abcde', 'def'])
     self.assert_(p < q)
     self.assert_(p <= q)
     self.assert_(q <= q)
     self.assert_(q > p)
     self.assert_(q >= p)
     self.failIf(q < r)
     self.failIf(q <= r)
     self.failIf(q > r)
     self.failIf(q >= r)
     self.assert_(set('a').issubset('abc'))
     self.assert_(set('abc').issuperset('a'))
     self.failIf(set('a').issubset('cbs'))
     self.failIf(set('cbs').issuperset('a'))
예제 #14
0
 def test_ixor(self):
     self.s ^= set(self.otherword)
     for c in (self.word + self.otherword):
         if (c in self.word) ^ (c in self.otherword):
             self.assert_(c in self.s)
         else:
             self.assert_(c not in self.s)
예제 #15
0
 def test_isub(self):
     self.s -= set(self.otherword)
     for c in (self.word + self.otherword):
         if c in self.word and c not in self.otherword:
             self.assert_(c in self.s)
         else:
             self.assert_(c not in self.s)
예제 #16
0
 def setUp(self):
     def gen():
         for i in xrange(0, 10, 2):
             yield i
     self.set   = set((1, 2, 3))
     self.other = gen()
     self.otherIsIterable = True
예제 #17
0
 def test_hash_effectiveness(self):
     n = 13
     hashvalues = set()
     addhashvalue = hashvalues.add
     elemmasks = [(i+1, 1<<i) for i in range(n)]
     for i in xrange(2**n):
         addhashvalue(hash(frozenset([e for e, m in elemmasks if m&i])))
     self.assertEqual(len(hashvalues), 2**n)
예제 #18
0
 def test_add_until_full(self):
     tmp = set()
     expected_len = 0
     for v in self.values:
         tmp.add(v)
         expected_len += 1
         self.assertEqual(len(tmp), expected_len)
     self.assertEqual(tmp, self.set)
예제 #19
0
 def test_and(self):
     i = self.s.intersection(self.otherword)
     self.assertEqual(self.s & set(self.otherword), i)
     self.assertEqual(self.s & frozenset(self.otherword), i)
     try:
         self.s & self.otherword
     except TypeError:
         pass
     else:
         self.fail("s&t did not screen-out general iterables")
예제 #20
0
 def test_sub(self):
     i = self.s.difference(self.otherword)
     self.assertEqual(self.s - set(self.otherword), i)
     self.assertEqual(self.s - frozenset(self.otherword), i)
     try:
         self.s - self.otherword
     except TypeError:
         pass
     else:
         self.fail("s-t did not screen-out general iterables")
예제 #21
0
 def test_xor(self):
     i = self.s.symmetric_difference(self.otherword)
     self.assertEqual(self.s ^ set(self.otherword), i)
     self.assertEqual(self.s ^ frozenset(self.otherword), i)
     try:
         self.s ^ self.otherword
     except TypeError:
         pass
     else:
         self.fail("s^t did not screen-out general iterables")
예제 #22
0
 def test_or(self):
     i = self.s.union(self.otherword)
     self.assertEqual(self.s | set(self.otherword), i)
     self.assertEqual(self.s | frozenset(self.otherword), i)
     try:
         self.s | self.otherword
     except TypeError:
         pass
     else:
         self.fail("s|t did not screen-out general iterables")
예제 #23
0
 def test_inline_methods(self):
     s = set('november')
     for data in ("123", "", range(1000), ('do', 1.2), xrange(2000,2200,5), 'december'):
         for meth in (s.union, s.intersection, s.difference, s.symmetric_difference):
             for g in (G, I, Ig, L, R):
                 expected = meth(data)
                 actual = meth(G(data))
                 self.assertEqual(sorted(actual), sorted(expected))
             self.assertRaises(TypeError, meth, X(s))
             self.assertRaises(TypeError, meth, N(s))
             self.assertRaises(ZeroDivisionError, meth, E(s))
예제 #24
0
 def test_update(self):
     retval = self.s.update(self.otherword)
     self.assertEqual(retval, None)
     for c in (self.word + self.otherword):
         self.assert_(c in self.s)
     self.assertRaises(PassThru, self.s.update, check_pass_thru())
     self.assertRaises(TypeError, self.s.update, [[]])
     for p, q in (('cdc', 'abcd'), ('efgfe', 'abcefg'), ('ccb', 'abc'), ('ef', 'abcef')):
         for C in set, frozenset, dict.fromkeys, str, unicode, list, tuple:
             s = self.thetype('abcba')
             self.assertEqual(s.update(C(p)), None)
             self.assertEqual(s, set(q))
예제 #25
0
class TestSubsetEqualEmpty(TestSubsets):
    left  = set()
    right = set()
    name  = "both empty"
    cases = "==", "<=", ">="
예제 #26
0
 def test_update_unit_tuple_non_overlap(self):
     self.set.update(("a", "z"))
     self.assertEqual(self.set, set(self.values + ["z"]))
예제 #27
0
 def test_update_unit_tuple_overlap(self):
     self.set.update(("a",))
     self.assertEqual(self.set, set(self.values))
예제 #28
0
 def test_update_empty_tuple(self):
     self.set.update(())
     self.assertEqual(self.set, set(self.values))
예제 #29
0
class TestSubsetPartial(TestSubsets):
    left  = set([1])
    right = set([1, 2])
    name  = "one a non-empty proper subset of other"
    cases = "!=", "<", "<="
예제 #30
0
 def test_remove_present(self):
     self.set.remove("b")
     self.assertEqual(self.set, set("ac"))
예제 #31
0
class TestSubsetEqualNonEmpty(TestSubsets):
    left  = set([1, 2])
    right = set([1, 2])
    name  = "equal pair"
    cases = "==", "<=", ">="
예제 #32
0
class TestSubsetEmptyNonEmpty(TestSubsets):
    left  = set()
    right = set([1, 2])
    name  = "one empty, one non-empty"
    cases = "!=", "<", "<="
예제 #33
0
 def test_discard_present(self):
     self.set.discard("c")
     self.assertEqual(self.set, set("ab"))
예제 #34
0
class TestSubsetNonOverlap(TestSubsets):
    left  = set([1])
    right = set([2])
    name  = "neither empty, neither contains"
    cases = "!="
예제 #35
0
 def test_discard_absent(self):
     self.set.discard("d")
     self.assertEqual(self.set, set("abc"))