0001 #!/usr/bin/env python 0002 0003 import unittest, operator, copy, pickle, random 0004 from sets import Set, ImmutableSet 0005 from test import test_support 0006 0007 empty_set = Set() 0008 0009 #============================================================================== 0010 0011 class TestBasicOps(unittest.TestCase): 0012 0013 def test_repr(self): 0014 if self.repr is not None: 0015 self.assertEqual(repr(self.set), self.repr) 0016 0017 def test_length(self): 0018 self.assertEqual(len(self.set), self.length) 0019 0020 def test_self_equality(self): 0021 self.assertEqual(self.set, self.set) 0022 0023 def test_equivalent_equality(self): 0024 self.assertEqual(self.set, self.dup) 0025 0026 def test_copy(self): 0027 self.assertEqual(self.set.copy(), self.dup) 0028 0029 def test_self_union(self): 0030 result = self.set | self.set 0031 self.assertEqual(result, self.dup) 0032 0033 def test_empty_union(self): 0034 result = self.set | empty_set 0035 self.assertEqual(result, self.dup) 0036 0037 def test_union_empty(self): 0038 result = empty_set | self.set 0039 self.assertEqual(result, self.dup) 0040 0041 def test_self_intersection(self): 0042 result = self.set & self.set 0043 self.assertEqual(result, self.dup) 0044 0045 def test_empty_intersection(self): 0046 result = self.set & empty_set 0047 self.assertEqual(result, empty_set) 0048 0049 def test_intersection_empty(self): 0050 result = empty_set & self.set 0051 self.assertEqual(result, empty_set) 0052 0053 def test_self_symmetric_difference(self): 0054 result = self.set ^ self.set 0055 self.assertEqual(result, empty_set) 0056 0057 def checkempty_symmetric_difference(self): 0058 result = self.set ^ empty_set 0059 self.assertEqual(result, self.set) 0060 0061 def test_self_difference(self): 0062 result = self.set - self.set 0063 self.assertEqual(result, empty_set) 0064 0065 def test_empty_difference(self): 0066 result = self.set - empty_set 0067 self.assertEqual(result, self.dup) 0068 0069 def test_empty_difference_rev(self): 0070 result = empty_set - self.set 0071 self.assertEqual(result, empty_set) 0072 0073 def test_iteration(self): 0074 for v in self.set: 0075 self.assert_(v in self.values) 0076 0077 def test_pickling(self): 0078 p = pickle.dumps(self.set) 0079 copy = pickle.loads(p) 0080 self.assertEqual(self.set, copy, 0081 "%s != %s" % (self.set, copy)) 0082 0083 #------------------------------------------------------------------------------ 0084 0085 class TestBasicOpsEmpty(TestBasicOps): 0086 def setUp(self): 0087 self.case = "empty set" 0088 self.values = [] 0089 self.set = Set(self.values) 0090 self.dup = Set(self.values) 0091 self.length = 0 0092 self.repr = "Set([])" 0093 0094 #------------------------------------------------------------------------------ 0095 0096 class TestBasicOpsSingleton(TestBasicOps): 0097 def setUp(self): 0098 self.case = "unit set (number)" 0099 self.values = [3] 0100 self.set = Set(self.values) 0101 self.dup = Set(self.values) 0102 self.length = 1 0103 self.repr = "Set([3])" 0104 0105 def test_in(self): 0106 self.failUnless(3 in self.set) 0107 0108 def test_not_in(self): 0109 self.failUnless(2 not in self.set) 0110 0111 #------------------------------------------------------------------------------ 0112 0113 class TestBasicOpsTuple(TestBasicOps): 0114 def setUp(self): 0115 self.case = "unit set (tuple)" 0116 self.values = [(0, "zero")] 0117 self.set = Set(self.values) 0118 self.dup = Set(self.values) 0119 self.length = 1 0120 self.repr = "Set([(0, 'zero')])" 0121 0122 def test_in(self): 0123 self.failUnless((0, "zero") in self.set) 0124 0125 def test_not_in(self): 0126 self.failUnless(9 not in self.set) 0127 0128 #------------------------------------------------------------------------------ 0129 0130 class TestBasicOpsTriple(TestBasicOps): 0131 def setUp(self): 0132 self.case = "triple set" 0133 self.values = [0, "zero", operator.add] 0134 self.set = Set(self.values) 0135 self.dup = Set(self.values) 0136 self.length = 3 0137 self.repr = None 0138 0139 #============================================================================== 0140 0141 def baditer(): 0142 raise TypeError 0143 yield True 0144 0145 def gooditer(): 0146 yield True 0147 0148 class TestExceptionPropagation(unittest.TestCase): 0149 """SF 628246: Set constructor should not trap iterator TypeErrors""" 0150 0151 def test_instanceWithException(self): 0152 self.assertRaises(TypeError, Set, baditer()) 0153 0154 def test_instancesWithoutException(self): 0155 # All of these iterables should load without exception. 0156 Set([1,2,3]) 0157 Set((1,2,3)) 0158 Set({'one':1, 'two':2, 'three':3}) 0159 Set(xrange(3)) 0160 Set('abc') 0161 Set(gooditer()) 0162 0163 #============================================================================== 0164 0165 class TestSetOfSets(unittest.TestCase): 0166 def test_constructor(self): 0167 inner = Set([1]) 0168 outer = Set([inner]) 0169 element = outer.pop() 0170 self.assertEqual(type(element), ImmutableSet) 0171 outer.add(inner) # Rebuild set of sets with .add method 0172 outer.remove(inner) 0173 self.assertEqual(outer, Set()) # Verify that remove worked 0174 outer.discard(inner) # Absence of KeyError indicates working fine 0175 0176 #============================================================================== 0177 0178 class TestBinaryOps(unittest.TestCase): 0179 def setUp(self): 0180 self.set = Set((2, 4, 6)) 0181 0182 def test_eq(self): # SF bug 643115 0183 self.assertEqual(self.set, Set({2:1,4:3,6:5})) 0184 0185 def test_union_subset(self): 0186 result = self.set | Set([2]) 0187 self.assertEqual(result, Set((2, 4, 6))) 0188 0189 def test_union_superset(self): 0190 result = self.set | Set([2, 4, 6, 8]) 0191 self.assertEqual(result, Set([2, 4, 6, 8])) 0192 0193 def test_union_overlap(self): 0194 result = self.set | Set([3, 4, 5]) 0195 self.assertEqual(result, Set([2, 3, 4, 5, 6])) 0196 0197 def test_union_non_overlap(self): 0198 result = self.set | Set([8]) 0199 self.assertEqual(result, Set([2, 4, 6, 8])) 0200 0201 def test_intersection_subset(self): 0202 result = self.set & Set((2, 4)) 0203 self.assertEqual(result, Set((2, 4))) 0204 0205 def test_intersection_superset(self): 0206 result = self.set & Set([2, 4, 6, 8]) 0207 self.assertEqual(result, Set([2, 4, 6])) 0208 0209 def test_intersection_overlap(self): 0210 result = self.set & Set([3, 4, 5]) 0211 self.assertEqual(result, Set([4])) 0212 0213 def test_intersection_non_overlap(self): 0214 result = self.set & Set([8]) 0215 self.assertEqual(result, empty_set) 0216 0217 def test_sym_difference_subset(self): 0218 result = self.set ^ Set((2, 4)) 0219 self.assertEqual(result, Set([6])) 0220 0221 def test_sym_difference_superset(self): 0222 result = self.set ^ Set((2, 4, 6, 8)) 0223 self.assertEqual(result, Set([8])) 0224 0225 def test_sym_difference_overlap(self): 0226 result = self.set ^ Set((3, 4, 5)) 0227 self.assertEqual(result, Set([2, 3, 5, 6])) 0228 0229 def test_sym_difference_non_overlap(self): 0230 result = self.set ^ Set([8]) 0231 self.assertEqual(result, Set([2, 4, 6, 8])) 0232 0233 def test_cmp(self): 0234 a, b = Set('a'), Set('b') 0235 self.assertRaises(TypeError, cmp, a, b) 0236 0237 # You can view this as a buglet: cmp(a, a) does not raise TypeError, 0238 # because __eq__ is tried before __cmp__, and a.__eq__(a) returns True, 0239 # which Python thinks is good enough to synthesize a cmp() result 0240 # without calling __cmp__. 0241 self.assertEqual(cmp(a, a), 0) 0242 0243 self.assertRaises(TypeError, cmp, a, 12) 0244 self.assertRaises(TypeError, cmp, "abc", a) 0245 0246 #============================================================================== 0247 0248 class TestUpdateOps(unittest.TestCase): 0249 def setUp(self): 0250 self.set = Set((2, 4, 6)) 0251 0252 def test_union_subset(self): 0253 self.set |= Set([2]) 0254 self.assertEqual(self.set, Set((2, 4, 6))) 0255 0256 def test_union_superset(self): 0257 self.set |= Set([2, 4, 6, 8]) 0258 self.assertEqual(self.set, Set([2, 4, 6, 8])) 0259 0260 def test_union_overlap(self): 0261 self.set |= Set([3, 4, 5]) 0262 self.assertEqual(self.set, Set([2, 3, 4, 5, 6])) 0263 0264 def test_union_non_overlap(self): 0265 self.set |= Set([8]) 0266 self.assertEqual(self.set, Set([2, 4, 6, 8])) 0267 0268 def test_union_method_call(self): 0269 self.set.union_update(Set([3, 4, 5])) 0270 self.assertEqual(self.set, Set([2, 3, 4, 5, 6])) 0271 0272 def test_intersection_subset(self): 0273 self.set &= Set((2, 4)) 0274 self.assertEqual(self.set, Set((2, 4))) 0275 0276 def test_intersection_superset(self): 0277 self.set &= Set([2, 4, 6, 8]) 0278 self.assertEqual(self.set, Set([2, 4, 6])) 0279 0280 def test_intersection_overlap(self): 0281 self.set &= Set([3, 4, 5]) 0282 self.assertEqual(self.set, Set([4])) 0283 0284 def test_intersection_non_overlap(self): 0285 self.set &= Set([8]) 0286 self.assertEqual(self.set, empty_set) 0287 0288 def test_intersection_method_call(self): 0289 self.set.intersection_update(Set([3, 4, 5])) 0290 self.assertEqual(self.set, Set([4])) 0291 0292 def test_sym_difference_subset(self): 0293 self.set ^= Set((2, 4)) 0294 self.assertEqual(self.set, Set([6])) 0295 0296 def test_sym_difference_superset(self): 0297 self.set ^= Set((2, 4, 6, 8)) 0298 self.assertEqual(self.set, Set([8])) 0299 0300 def test_sym_difference_overlap(self): 0301 self.set ^= Set((3, 4, 5)) 0302 self.assertEqual(self.set, Set([2, 3, 5, 6])) 0303 0304 def test_sym_difference_non_overlap(self): 0305 self.set ^= Set([8]) 0306 self.assertEqual(self.set, Set([2, 4, 6, 8])) 0307 0308 def test_sym_difference_method_call(self): 0309 self.set.symmetric_difference_update(Set([3, 4, 5])) 0310 self.assertEqual(self.set, Set([2, 3, 5, 6])) 0311 0312 def test_difference_subset(self): 0313 self.set -= Set((2, 4)) 0314 self.assertEqual(self.set, Set([6])) 0315 0316 def test_difference_superset(self): 0317 self.set -= Set((2, 4, 6, 8)) 0318 self.assertEqual(self.set, Set([])) 0319 0320 def test_difference_overlap(self): 0321 self.set -= Set((3, 4, 5)) 0322 self.assertEqual(self.set, Set([2, 6])) 0323 0324 def test_difference_non_overlap(self): 0325 self.set -= Set([8]) 0326 self.assertEqual(self.set, Set([2, 4, 6])) 0327 0328 def test_difference_method_call(self): 0329 self.set.difference_update(Set([3, 4, 5])) 0330 self.assertEqual(self.set, Set([2, 6])) 0331 0332 #============================================================================== 0333 0334 class TestMutate(unittest.TestCase): 0335 def setUp(self): 0336 self.values = ["a", "b", "c"] 0337 self.set = Set(self.values) 0338 0339 def test_add_present(self): 0340 self.set.add("c") 0341 self.assertEqual(self.set, Set("abc")) 0342 0343 def test_add_absent(self): 0344 self.set.add("d") 0345 self.assertEqual(self.set, Set("abcd")) 0346 0347 def test_add_until_full(self): 0348 tmp = Set() 0349 expected_len = 0 0350 for v in self.values: 0351 tmp.add(v) 0352 expected_len += 1 0353 self.assertEqual(len(tmp), expected_len) 0354 self.assertEqual(tmp, self.set) 0355 0356 def test_remove_present(self): 0357 self.set.remove("b") 0358 self.assertEqual(self.set, Set("ac")) 0359 0360 def test_remove_absent(self): 0361 try: 0362 self.set.remove("d") 0363 self.fail("Removing missing element should have raised LookupError") 0364 except LookupError: 0365 pass 0366 0367 def test_remove_until_empty(self): 0368 expected_len = len(self.set) 0369 for v in self.values: 0370 self.set.remove(v) 0371 expected_len -= 1 0372 self.assertEqual(len(self.set), expected_len) 0373 0374 def test_discard_present(self): 0375 self.set.discard("c") 0376 self.assertEqual(self.set, Set("ab")) 0377 0378 def test_discard_absent(self): 0379 self.set.discard("d") 0380 self.assertEqual(self.set, Set("abc")) 0381 0382 def test_clear(self): 0383 self.set.clear() 0384 self.assertEqual(len(self.set), 0) 0385 0386 def test_pop(self): 0387 popped = {} 0388 while self.set: 0389 popped[self.set.pop()] = None 0390 self.assertEqual(len(popped), len(self.values)) 0391 for v in self.values: 0392 self.failUnless(v in popped) 0393 0394 def test_update_empty_tuple(self): 0395 self.set.union_update(()) 0396 self.assertEqual(self.set, Set(self.values)) 0397 0398 def test_update_unit_tuple_overlap(self): 0399 self.set.union_update(("a",)) 0400 self.assertEqual(self.set, Set(self.values)) 0401 0402 def test_update_unit_tuple_non_overlap(self): 0403 self.set.union_update(("a", "z")) 0404 self.assertEqual(self.set, Set(self.values + ["z"])) 0405 0406 #============================================================================== 0407 0408 class TestSubsets(unittest.TestCase): 0409 0410 case2method = {"<=": "issubset", 0411 ">=": "issuperset", 0412 } 0413 0414 reverse = {"==": "==", 0415 "!=": "!=", 0416 "<": ">", 0417 ">": "<", 0418 "<=": ">=", 0419 ">=": "<=", 0420 } 0421 0422 def test_issubset(self): 0423 x = self.left 0424 y = self.right 0425 for case in "!=", "==", "<", "<=", ">", ">=": 0426 expected = case in self.cases 0427 # Test the binary infix spelling. 0428 result = eval("x" + case + "y", locals()) 0429 self.assertEqual(result, expected) 0430 # Test the "friendly" method-name spelling, if one exists. 0431 if case in TestSubsets.case2method: 0432 method = getattr(x, TestSubsets.case2method[case]) 0433 result = method(y) 0434 self.assertEqual(result, expected) 0435 0436 # Now do the same for the operands reversed. 0437 rcase = TestSubsets.reverse[case] 0438 result = eval("y" + rcase + "x", locals()) 0439 self.assertEqual(result, expected) 0440 if rcase in TestSubsets.case2method: 0441 method = getattr(y, TestSubsets.case2method[rcase]) 0442 result = method(x) 0443 self.assertEqual(result, expected) 0444 #------------------------------------------------------------------------------ 0445 0446 class TestSubsetEqualEmpty(TestSubsets): 0447 left = Set() 0448 right = Set() 0449 name = "both empty" 0450 cases = "==", "<=", ">=" 0451 0452 #------------------------------------------------------------------------------ 0453 0454 class TestSubsetEqualNonEmpty(TestSubsets): 0455 left = Set([1, 2]) 0456 right = Set([1, 2]) 0457 name = "equal pair" 0458 cases = "==", "<=", ">=" 0459 0460 #------------------------------------------------------------------------------ 0461 0462 class TestSubsetEmptyNonEmpty(TestSubsets): 0463 left = Set() 0464 right = Set([1, 2]) 0465 name = "one empty, one non-empty" 0466 cases = "!=", "<", "<=" 0467 0468 #------------------------------------------------------------------------------ 0469 0470 class TestSubsetPartial(TestSubsets): 0471 left = Set([1]) 0472 right = Set([1, 2]) 0473 name = "one a non-empty proper subset of other" 0474 cases = "!=", "<", "<=" 0475 0476 #------------------------------------------------------------------------------ 0477 0478 class TestSubsetNonOverlap(TestSubsets): 0479 left = Set([1]) 0480 right = Set([2]) 0481 name = "neither empty, neither contains" 0482 cases = "!=" 0483 0484 #============================================================================== 0485 0486 class TestOnlySetsInBinaryOps(unittest.TestCase): 0487 0488 def test_eq_ne(self): 0489 # Unlike the others, this is testing that == and != *are* allowed. 0490 self.assertEqual(self.other == self.set, False) 0491 self.assertEqual(self.set == self.other, False) 0492 self.assertEqual(self.other != self.set, True) 0493 self.assertEqual(self.set != self.other, True) 0494 0495 def test_ge_gt_le_lt(self): 0496 self.assertRaises(TypeError, lambda: self.set < self.other) 0497 self.assertRaises(TypeError, lambda: self.set <= self.other) 0498 self.assertRaises(TypeError, lambda: self.set > self.other) 0499 self.assertRaises(TypeError, lambda: self.set >= self.other) 0500 0501 self.assertRaises(TypeError, lambda: self.other < self.set) 0502 self.assertRaises(TypeError, lambda: self.other <= self.set) 0503 self.assertRaises(TypeError, lambda: self.other > self.set) 0504 self.assertRaises(TypeError, lambda: self.other >= self.set) 0505 0506 def test_union_update_operator(self): 0507 try: 0508 self.set |= self.other 0509 except TypeError: 0510 pass 0511 else: 0512 self.fail("expected TypeError") 0513 0514 def test_union_update(self): 0515 if self.otherIsIterable: 0516 self.set.union_update(self.other) 0517 else: 0518 self.assertRaises(TypeError, self.set.union_update, self.other) 0519 0520 def test_union(self): 0521 self.assertRaises(TypeError, lambda: self.set | self.other) 0522 self.assertRaises(TypeError, lambda: self.other | self.set) 0523 if self.otherIsIterable: 0524 self.set.union(self.other) 0525 else: 0526 self.assertRaises(TypeError, self.set.union, self.other) 0527 0528 def test_intersection_update_operator(self): 0529 try: 0530 self.set &= self.other 0531 except TypeError: 0532 pass 0533 else: 0534 self.fail("expected TypeError") 0535 0536 def test_intersection_update(self): 0537 if self.otherIsIterable: 0538 self.set.intersection_update(self.other) 0539 else: 0540 self.assertRaises(TypeError, 0541 self.set.intersection_update, 0542 self.other) 0543 0544 def test_intersection(self): 0545 self.assertRaises(TypeError, lambda: self.set & self.other) 0546 self.assertRaises(TypeError, lambda: self.other & self.set) 0547 if self.otherIsIterable: 0548 self.set.intersection(self.other) 0549 else: 0550 self.assertRaises(TypeError, self.set.intersection, self.other) 0551 0552 def test_sym_difference_update_operator(self): 0553 try: 0554 self.set ^= self.other 0555 except TypeError: 0556 pass 0557 else: 0558 self.fail("expected TypeError") 0559 0560 def test_sym_difference_update(self): 0561 if self.otherIsIterable: 0562 self.set.symmetric_difference_update(self.other) 0563 else: 0564 self.assertRaises(TypeError, 0565 self.set.symmetric_difference_update, 0566 self.other) 0567 0568 def test_sym_difference(self): 0569 self.assertRaises(TypeError, lambda: self.set ^ self.other) 0570 self.assertRaises(TypeError, lambda: self.other ^ self.set) 0571 if self.otherIsIterable: 0572 self.set.symmetric_difference(self.other) 0573 else: 0574 self.assertRaises(TypeError, self.set.symmetric_difference, self.other) 0575 0576 def test_difference_update_operator(self): 0577 try: 0578 self.set -= self.other 0579 except TypeError: 0580 pass 0581 else: 0582 self.fail("expected TypeError") 0583 0584 def test_difference_update(self): 0585 if self.otherIsIterable: 0586 self.set.difference_update(self.other) 0587 else: 0588 self.assertRaises(TypeError, 0589 self.set.difference_update, 0590 self.other) 0591 0592 def test_difference(self): 0593 self.assertRaises(TypeError, lambda: self.set - self.other) 0594 self.assertRaises(TypeError, lambda: self.other - self.set) 0595 if self.otherIsIterable: 0596 self.set.difference(self.other) 0597 else: 0598 self.assertRaises(TypeError, self.set.difference, self.other) 0599 0600 #------------------------------------------------------------------------------ 0601 0602 class TestOnlySetsNumeric(TestOnlySetsInBinaryOps): 0603 def setUp(self): 0604 self.set = Set((1, 2, 3)) 0605 self.other = 19 0606 self.otherIsIterable = False 0607 0608 #------------------------------------------------------------------------------ 0609 0610 class TestOnlySetsDict(TestOnlySetsInBinaryOps): 0611 def setUp(self): 0612 self.set = Set((1, 2, 3)) 0613 self.other = {1:2, 3:4} 0614 self.otherIsIterable = True 0615 0616 #------------------------------------------------------------------------------ 0617 0618 class TestOnlySetsOperator(TestOnlySetsInBinaryOps): 0619 def setUp(self): 0620 self.set = Set((1, 2, 3)) 0621 self.other = operator.add 0622 self.otherIsIterable = False 0623 0624 #------------------------------------------------------------------------------ 0625 0626 class TestOnlySetsTuple(TestOnlySetsInBinaryOps): 0627 def setUp(self): 0628 self.set = Set((1, 2, 3)) 0629 self.other = (2, 4, 6) 0630 self.otherIsIterable = True 0631 0632 #------------------------------------------------------------------------------ 0633 0634 class TestOnlySetsString(TestOnlySetsInBinaryOps): 0635 def setUp(self): 0636 self.set = Set((1, 2, 3)) 0637 self.other = 'abc' 0638 self.otherIsIterable = True 0639 0640 #------------------------------------------------------------------------------ 0641 0642 class TestOnlySetsGenerator(TestOnlySetsInBinaryOps): 0643 def setUp(self): 0644 def gen(): 0645 for i in xrange(0, 10, 2): 0646 yield i 0647 self.set = Set((1, 2, 3)) 0648 self.other = gen() 0649 self.otherIsIterable = True 0650 0651 #------------------------------------------------------------------------------ 0652 0653 class TestOnlySetsofSets(TestOnlySetsInBinaryOps): 0654 def setUp(self): 0655 self.set = Set((1, 2, 3)) 0656 self.other = [Set('ab'), ImmutableSet('cd')] 0657 self.otherIsIterable = True 0658 0659 #============================================================================== 0660 0661 class TestCopying(unittest.TestCase): 0662 0663 def test_copy(self): 0664 dup = self.set.copy() 0665 dup_list = list(dup); dup_list.sort() 0666 set_list = list(self.set); set_list.sort() 0667 self.assertEqual(len(dup_list), len(set_list)) 0668 for i in range(len(dup_list)): 0669 self.failUnless(dup_list[i] is set_list[i]) 0670 0671 def test_deep_copy(self): 0672 dup = copy.deepcopy(self.set) 0673 ##print type(dup), repr(dup) 0674 dup_list = list(dup); dup_list.sort() 0675 set_list = list(self.set); set_list.sort() 0676 self.assertEqual(len(dup_list), len(set_list)) 0677 for i in range(len(dup_list)): 0678 self.assertEqual(dup_list[i], set_list[i]) 0679 0680 #------------------------------------------------------------------------------ 0681 0682 class TestCopyingEmpty(TestCopying): 0683 def setUp(self): 0684 self.set = Set() 0685 0686 #------------------------------------------------------------------------------ 0687 0688 class TestCopyingSingleton(TestCopying): 0689 def setUp(self): 0690 self.set = Set(["hello"]) 0691 0692 #------------------------------------------------------------------------------ 0693 0694 class TestCopyingTriple(TestCopying): 0695 def setUp(self): 0696 self.set = Set(["zero", 0, None]) 0697 0698 #------------------------------------------------------------------------------ 0699 0700 class TestCopyingTuple(TestCopying): 0701 def setUp(self): 0702 self.set = Set([(1, 2)]) 0703 0704 #------------------------------------------------------------------------------ 0705 0706 class TestCopyingNested(TestCopying): 0707 def setUp(self): 0708 self.set = Set([((1, 2), (3, 4))]) 0709 0710 #============================================================================== 0711 0712 class TestIdentities(unittest.TestCase): 0713 def setUp(self): 0714 self.a = Set([random.randrange(100) for i in xrange(50)]) 0715 self.b = Set([random.randrange(100) for i in xrange(50)]) 0716 0717 def test_binopsVsSubsets(self): 0718 a, b = self.a, self.b 0719 self.assert_(a - b <= a) 0720 self.assert_(b - a <= b) 0721 self.assert_(a & b <= a) 0722 self.assert_(a & b <= b) 0723 self.assert_(a | b >= a) 0724 self.assert_(a | b >= b) 0725 self.assert_(a ^ b <= a | b) 0726 0727 def test_commutativity(self): 0728 a, b = self.a, self.b 0729 self.assertEqual(a&b, b&a) 0730 self.assertEqual(a|b, b|a) 0731 self.assertEqual(a^b, b^a) 0732 if a != b: 0733 self.assertNotEqual(a-b, b-a) 0734 0735 def test_reflexsive_relations(self): 0736 a, zero = self.a, Set() 0737 self.assertEqual(a ^ a, zero) 0738 self.assertEqual(a - a, zero) 0739 self.assertEqual(a | a, a) 0740 self.assertEqual(a & a, a) 0741 self.assert_(a <= a) 0742 self.assert_(a >= a) 0743 self.assert_(a == a) 0744 0745 def test_summations(self): 0746 # check that sums of parts equal the whole 0747 a, b = self.a, self.b 0748 self.assertEqual((a-b)|(a&b)|(b-a), a|b) 0749 self.assertEqual((a&b)|(a^b), a|b) 0750 self.assertEqual(a|(b-a), a|b) 0751 self.assertEqual((a-b)|b, a|b) 0752 self.assertEqual((a-b)|(a&b), a) 0753 self.assertEqual((b-a)|(a&b), b) 0754 self.assertEqual((a-b)|(b-a), a^b) 0755 0756 def test_exclusion(self): 0757 # check that inverse operations do not overlap 0758 a, b, zero = self.a, self.b, Set() 0759 self.assertEqual((a-b)&b, zero) 0760 self.assertEqual((b-a)&a, zero) 0761 self.assertEqual((a&b)&(a^b), zero) 0762 0763 def test_cardinality_relations(self): 0764 a, b = self.a, self.b 0765 self.assertEqual(len(a), len(a-b) + len(a&b)) 0766 self.assertEqual(len(b), len(b-a) + len(a&b)) 0767 self.assertEqual(len(a^b), len(a-b) + len(b-a)) 0768 self.assertEqual(len(a|b), len(a-b) + len(a&b) + len(b-a)) 0769 self.assertEqual(len(a^b) + len(a&b), len(a|b)) 0770 0771 #============================================================================== 0772 0773 libreftest = """ 0774 Example from the Library Reference: Doc/lib/libsets.tex 0775 0776 >>> from sets import Set as Base # override _repr to get sorted output 0777 >>> class Set(Base): 0778 ... def _repr(self): 0779 ... return Base._repr(self, sorted=True) 0780 >>> engineers = Set(['John', 'Jane', 'Jack', 'Janice']) 0781 >>> programmers = Set(['Jack', 'Sam', 'Susan', 'Janice']) 0782 >>> managers = Set(['Jane', 'Jack', 'Susan', 'Zack']) 0783 >>> employees = engineers | programmers | managers # union 0784 >>> engineering_management = engineers & managers # intersection 0785 >>> fulltime_management = managers - engineers - programmers # difference 0786 >>> engineers.add('Marvin') 0787 >>> print engineers 0788 Set(['Jack', 'Jane', 'Janice', 'John', 'Marvin']) 0789 >>> employees.issuperset(engineers) # superset test 0790 False 0791 >>> employees.union_update(engineers) # update from another set 0792 >>> employees.issuperset(engineers) 0793 True 0794 >>> for group in [engineers, programmers, managers, employees]: 0795 ... group.discard('Susan') # unconditionally remove element 0796 ... print group 0797 ... 0798 Set(['Jack', 'Jane', 'Janice', 'John', 'Marvin']) 0799 Set(['Jack', 'Janice', 'Sam']) 0800 Set(['Jack', 'Jane', 'Zack']) 0801 Set(['Jack', 'Jane', 'Janice', 'John', 'Marvin', 'Sam', 'Zack']) 0802 """ 0803 0804 #============================================================================== 0805 0806 __test__ = {'libreftest' : libreftest} 0807 0808 def test_main(verbose=None): 0809 import test_sets, doctest 0810 test_support.run_unittest( 0811 TestSetOfSets, 0812 TestExceptionPropagation, 0813 TestBasicOpsEmpty, 0814 TestBasicOpsSingleton, 0815 TestBasicOpsTuple, 0816 TestBasicOpsTriple, 0817 TestBinaryOps, 0818 TestUpdateOps, 0819 TestMutate, 0820 TestSubsetEqualEmpty, 0821 TestSubsetEqualNonEmpty, 0822 TestSubsetEmptyNonEmpty, 0823 TestSubsetPartial, 0824 TestSubsetNonOverlap, 0825 TestOnlySetsNumeric, 0826 TestOnlySetsDict, 0827 TestOnlySetsOperator, 0828 TestOnlySetsTuple, 0829 TestOnlySetsString, 0830 TestOnlySetsGenerator, 0831 TestOnlySetsofSets, 0832 TestCopyingEmpty, 0833 TestCopyingSingleton, 0834 TestCopyingTriple, 0835 TestCopyingTuple, 0836 TestCopyingNested, 0837 TestIdentities, 0838 doctest.DocTestSuite(test_sets), 0839 ) 0840 0841 if __name__ == "__main__": 0842 test_main(verbose=True) 0843
Generated by PyXR 0.9.4