The silencing of DeprecationWarning was not taking -3 into consideration. Since
[python.git] / Lib / test / test_sets.py
blob9c598361a68831fc7804a18234499c83a768af12
1 #!/usr/bin/env python
3 import warnings
4 warnings.filterwarnings("ignore", "the sets module is deprecated",
5 DeprecationWarning, "test\.test_sets")
7 import unittest, operator, copy, pickle, random
8 from sets import Set, ImmutableSet
9 from test import test_support
11 empty_set = Set()
13 #==============================================================================
15 class TestBasicOps(unittest.TestCase):
17 def test_repr(self):
18 if self.repr is not None:
19 self.assertEqual(repr(self.set), self.repr)
21 def test_length(self):
22 self.assertEqual(len(self.set), self.length)
24 def test_self_equality(self):
25 self.assertEqual(self.set, self.set)
27 def test_equivalent_equality(self):
28 self.assertEqual(self.set, self.dup)
30 def test_copy(self):
31 self.assertEqual(self.set.copy(), self.dup)
33 def test_self_union(self):
34 result = self.set | self.set
35 self.assertEqual(result, self.dup)
37 def test_empty_union(self):
38 result = self.set | empty_set
39 self.assertEqual(result, self.dup)
41 def test_union_empty(self):
42 result = empty_set | self.set
43 self.assertEqual(result, self.dup)
45 def test_self_intersection(self):
46 result = self.set & self.set
47 self.assertEqual(result, self.dup)
49 def test_empty_intersection(self):
50 result = self.set & empty_set
51 self.assertEqual(result, empty_set)
53 def test_intersection_empty(self):
54 result = empty_set & self.set
55 self.assertEqual(result, empty_set)
57 def test_self_symmetric_difference(self):
58 result = self.set ^ self.set
59 self.assertEqual(result, empty_set)
61 def checkempty_symmetric_difference(self):
62 result = self.set ^ empty_set
63 self.assertEqual(result, self.set)
65 def test_self_difference(self):
66 result = self.set - self.set
67 self.assertEqual(result, empty_set)
69 def test_empty_difference(self):
70 result = self.set - empty_set
71 self.assertEqual(result, self.dup)
73 def test_empty_difference_rev(self):
74 result = empty_set - self.set
75 self.assertEqual(result, empty_set)
77 def test_iteration(self):
78 for v in self.set:
79 self.assertTrue(v in self.values)
81 def test_pickling(self):
82 p = pickle.dumps(self.set)
83 copy = pickle.loads(p)
84 self.assertEqual(self.set, copy,
85 "%s != %s" % (self.set, copy))
87 #------------------------------------------------------------------------------
89 class TestBasicOpsEmpty(TestBasicOps):
90 def setUp(self):
91 self.case = "empty set"
92 self.values = []
93 self.set = Set(self.values)
94 self.dup = Set(self.values)
95 self.length = 0
96 self.repr = "Set([])"
98 #------------------------------------------------------------------------------
100 class TestBasicOpsSingleton(TestBasicOps):
101 def setUp(self):
102 self.case = "unit set (number)"
103 self.values = [3]
104 self.set = Set(self.values)
105 self.dup = Set(self.values)
106 self.length = 1
107 self.repr = "Set([3])"
109 def test_in(self):
110 self.assertTrue(3 in self.set)
112 def test_not_in(self):
113 self.assertTrue(2 not in self.set)
115 #------------------------------------------------------------------------------
117 class TestBasicOpsTuple(TestBasicOps):
118 def setUp(self):
119 self.case = "unit set (tuple)"
120 self.values = [(0, "zero")]
121 self.set = Set(self.values)
122 self.dup = Set(self.values)
123 self.length = 1
124 self.repr = "Set([(0, 'zero')])"
126 def test_in(self):
127 self.assertTrue((0, "zero") in self.set)
129 def test_not_in(self):
130 self.assertTrue(9 not in self.set)
132 #------------------------------------------------------------------------------
134 class TestBasicOpsTriple(TestBasicOps):
135 def setUp(self):
136 self.case = "triple set"
137 self.values = [0, "zero", operator.add]
138 self.set = Set(self.values)
139 self.dup = Set(self.values)
140 self.length = 3
141 self.repr = None
143 #==============================================================================
145 def baditer():
146 raise TypeError
147 yield True
149 def gooditer():
150 yield True
152 class TestExceptionPropagation(unittest.TestCase):
153 """SF 628246: Set constructor should not trap iterator TypeErrors"""
155 def test_instanceWithException(self):
156 self.assertRaises(TypeError, Set, baditer())
158 def test_instancesWithoutException(self):
159 # All of these iterables should load without exception.
160 Set([1,2,3])
161 Set((1,2,3))
162 Set({'one':1, 'two':2, 'three':3})
163 Set(xrange(3))
164 Set('abc')
165 Set(gooditer())
167 #==============================================================================
169 class TestSetOfSets(unittest.TestCase):
170 def test_constructor(self):
171 inner = Set([1])
172 outer = Set([inner])
173 element = outer.pop()
174 self.assertEqual(type(element), ImmutableSet)
175 outer.add(inner) # Rebuild set of sets with .add method
176 outer.remove(inner)
177 self.assertEqual(outer, Set()) # Verify that remove worked
178 outer.discard(inner) # Absence of KeyError indicates working fine
180 #==============================================================================
182 class TestBinaryOps(unittest.TestCase):
183 def setUp(self):
184 self.set = Set((2, 4, 6))
186 def test_eq(self): # SF bug 643115
187 self.assertEqual(self.set, Set({2:1,4:3,6:5}))
189 def test_union_subset(self):
190 result = self.set | Set([2])
191 self.assertEqual(result, Set((2, 4, 6)))
193 def test_union_superset(self):
194 result = self.set | Set([2, 4, 6, 8])
195 self.assertEqual(result, Set([2, 4, 6, 8]))
197 def test_union_overlap(self):
198 result = self.set | Set([3, 4, 5])
199 self.assertEqual(result, Set([2, 3, 4, 5, 6]))
201 def test_union_non_overlap(self):
202 result = self.set | Set([8])
203 self.assertEqual(result, Set([2, 4, 6, 8]))
205 def test_intersection_subset(self):
206 result = self.set & Set((2, 4))
207 self.assertEqual(result, Set((2, 4)))
209 def test_intersection_superset(self):
210 result = self.set & Set([2, 4, 6, 8])
211 self.assertEqual(result, Set([2, 4, 6]))
213 def test_intersection_overlap(self):
214 result = self.set & Set([3, 4, 5])
215 self.assertEqual(result, Set([4]))
217 def test_intersection_non_overlap(self):
218 result = self.set & Set([8])
219 self.assertEqual(result, empty_set)
221 def test_sym_difference_subset(self):
222 result = self.set ^ Set((2, 4))
223 self.assertEqual(result, Set([6]))
225 def test_sym_difference_superset(self):
226 result = self.set ^ Set((2, 4, 6, 8))
227 self.assertEqual(result, Set([8]))
229 def test_sym_difference_overlap(self):
230 result = self.set ^ Set((3, 4, 5))
231 self.assertEqual(result, Set([2, 3, 5, 6]))
233 def test_sym_difference_non_overlap(self):
234 result = self.set ^ Set([8])
235 self.assertEqual(result, Set([2, 4, 6, 8]))
237 def test_cmp(self):
238 a, b = Set('a'), Set('b')
239 self.assertRaises(TypeError, cmp, a, b)
241 # You can view this as a buglet: cmp(a, a) does not raise TypeError,
242 # because __eq__ is tried before __cmp__, and a.__eq__(a) returns True,
243 # which Python thinks is good enough to synthesize a cmp() result
244 # without calling __cmp__.
245 self.assertEqual(cmp(a, a), 0)
247 self.assertRaises(TypeError, cmp, a, 12)
248 self.assertRaises(TypeError, cmp, "abc", a)
250 def test_inplace_on_self(self):
251 t = self.set.copy()
252 t |= t
253 self.assertEqual(t, self.set)
254 t &= t
255 self.assertEqual(t, self.set)
256 t -= t
257 self.assertEqual(len(t), 0)
258 t = self.set.copy()
259 t ^= t
260 self.assertEqual(len(t), 0)
263 #==============================================================================
265 class TestUpdateOps(unittest.TestCase):
266 def setUp(self):
267 self.set = Set((2, 4, 6))
269 def test_union_subset(self):
270 self.set |= Set([2])
271 self.assertEqual(self.set, Set((2, 4, 6)))
273 def test_union_superset(self):
274 self.set |= Set([2, 4, 6, 8])
275 self.assertEqual(self.set, Set([2, 4, 6, 8]))
277 def test_union_overlap(self):
278 self.set |= Set([3, 4, 5])
279 self.assertEqual(self.set, Set([2, 3, 4, 5, 6]))
281 def test_union_non_overlap(self):
282 self.set |= Set([8])
283 self.assertEqual(self.set, Set([2, 4, 6, 8]))
285 def test_union_method_call(self):
286 self.set.union_update(Set([3, 4, 5]))
287 self.assertEqual(self.set, Set([2, 3, 4, 5, 6]))
289 def test_intersection_subset(self):
290 self.set &= Set((2, 4))
291 self.assertEqual(self.set, Set((2, 4)))
293 def test_intersection_superset(self):
294 self.set &= Set([2, 4, 6, 8])
295 self.assertEqual(self.set, Set([2, 4, 6]))
297 def test_intersection_overlap(self):
298 self.set &= Set([3, 4, 5])
299 self.assertEqual(self.set, Set([4]))
301 def test_intersection_non_overlap(self):
302 self.set &= Set([8])
303 self.assertEqual(self.set, empty_set)
305 def test_intersection_method_call(self):
306 self.set.intersection_update(Set([3, 4, 5]))
307 self.assertEqual(self.set, Set([4]))
309 def test_sym_difference_subset(self):
310 self.set ^= Set((2, 4))
311 self.assertEqual(self.set, Set([6]))
313 def test_sym_difference_superset(self):
314 self.set ^= Set((2, 4, 6, 8))
315 self.assertEqual(self.set, Set([8]))
317 def test_sym_difference_overlap(self):
318 self.set ^= Set((3, 4, 5))
319 self.assertEqual(self.set, Set([2, 3, 5, 6]))
321 def test_sym_difference_non_overlap(self):
322 self.set ^= Set([8])
323 self.assertEqual(self.set, Set([2, 4, 6, 8]))
325 def test_sym_difference_method_call(self):
326 self.set.symmetric_difference_update(Set([3, 4, 5]))
327 self.assertEqual(self.set, Set([2, 3, 5, 6]))
329 def test_difference_subset(self):
330 self.set -= Set((2, 4))
331 self.assertEqual(self.set, Set([6]))
333 def test_difference_superset(self):
334 self.set -= Set((2, 4, 6, 8))
335 self.assertEqual(self.set, Set([]))
337 def test_difference_overlap(self):
338 self.set -= Set((3, 4, 5))
339 self.assertEqual(self.set, Set([2, 6]))
341 def test_difference_non_overlap(self):
342 self.set -= Set([8])
343 self.assertEqual(self.set, Set([2, 4, 6]))
345 def test_difference_method_call(self):
346 self.set.difference_update(Set([3, 4, 5]))
347 self.assertEqual(self.set, Set([2, 6]))
349 #==============================================================================
351 class TestMutate(unittest.TestCase):
352 def setUp(self):
353 self.values = ["a", "b", "c"]
354 self.set = Set(self.values)
356 def test_add_present(self):
357 self.set.add("c")
358 self.assertEqual(self.set, Set("abc"))
360 def test_add_absent(self):
361 self.set.add("d")
362 self.assertEqual(self.set, Set("abcd"))
364 def test_add_until_full(self):
365 tmp = Set()
366 expected_len = 0
367 for v in self.values:
368 tmp.add(v)
369 expected_len += 1
370 self.assertEqual(len(tmp), expected_len)
371 self.assertEqual(tmp, self.set)
373 def test_remove_present(self):
374 self.set.remove("b")
375 self.assertEqual(self.set, Set("ac"))
377 def test_remove_absent(self):
378 try:
379 self.set.remove("d")
380 self.fail("Removing missing element should have raised LookupError")
381 except LookupError:
382 pass
384 def test_remove_until_empty(self):
385 expected_len = len(self.set)
386 for v in self.values:
387 self.set.remove(v)
388 expected_len -= 1
389 self.assertEqual(len(self.set), expected_len)
391 def test_discard_present(self):
392 self.set.discard("c")
393 self.assertEqual(self.set, Set("ab"))
395 def test_discard_absent(self):
396 self.set.discard("d")
397 self.assertEqual(self.set, Set("abc"))
399 def test_clear(self):
400 self.set.clear()
401 self.assertEqual(len(self.set), 0)
403 def test_pop(self):
404 popped = {}
405 while self.set:
406 popped[self.set.pop()] = None
407 self.assertEqual(len(popped), len(self.values))
408 for v in self.values:
409 self.assertTrue(v in popped)
411 def test_update_empty_tuple(self):
412 self.set.union_update(())
413 self.assertEqual(self.set, Set(self.values))
415 def test_update_unit_tuple_overlap(self):
416 self.set.union_update(("a",))
417 self.assertEqual(self.set, Set(self.values))
419 def test_update_unit_tuple_non_overlap(self):
420 self.set.union_update(("a", "z"))
421 self.assertEqual(self.set, Set(self.values + ["z"]))
423 #==============================================================================
425 class TestSubsets(unittest.TestCase):
427 case2method = {"<=": "issubset",
428 ">=": "issuperset",
431 reverse = {"==": "==",
432 "!=": "!=",
433 "<": ">",
434 ">": "<",
435 "<=": ">=",
436 ">=": "<=",
439 def test_issubset(self):
440 x = self.left
441 y = self.right
442 for case in "!=", "==", "<", "<=", ">", ">=":
443 expected = case in self.cases
444 # Test the binary infix spelling.
445 result = eval("x" + case + "y", locals())
446 self.assertEqual(result, expected)
447 # Test the "friendly" method-name spelling, if one exists.
448 if case in TestSubsets.case2method:
449 method = getattr(x, TestSubsets.case2method[case])
450 result = method(y)
451 self.assertEqual(result, expected)
453 # Now do the same for the operands reversed.
454 rcase = TestSubsets.reverse[case]
455 result = eval("y" + rcase + "x", locals())
456 self.assertEqual(result, expected)
457 if rcase in TestSubsets.case2method:
458 method = getattr(y, TestSubsets.case2method[rcase])
459 result = method(x)
460 self.assertEqual(result, expected)
461 #------------------------------------------------------------------------------
463 class TestSubsetEqualEmpty(TestSubsets):
464 left = Set()
465 right = Set()
466 name = "both empty"
467 cases = "==", "<=", ">="
469 #------------------------------------------------------------------------------
471 class TestSubsetEqualNonEmpty(TestSubsets):
472 left = Set([1, 2])
473 right = Set([1, 2])
474 name = "equal pair"
475 cases = "==", "<=", ">="
477 #------------------------------------------------------------------------------
479 class TestSubsetEmptyNonEmpty(TestSubsets):
480 left = Set()
481 right = Set([1, 2])
482 name = "one empty, one non-empty"
483 cases = "!=", "<", "<="
485 #------------------------------------------------------------------------------
487 class TestSubsetPartial(TestSubsets):
488 left = Set([1])
489 right = Set([1, 2])
490 name = "one a non-empty proper subset of other"
491 cases = "!=", "<", "<="
493 #------------------------------------------------------------------------------
495 class TestSubsetNonOverlap(TestSubsets):
496 left = Set([1])
497 right = Set([2])
498 name = "neither empty, neither contains"
499 cases = "!="
501 #==============================================================================
503 class TestOnlySetsInBinaryOps(unittest.TestCase):
505 def test_eq_ne(self):
506 # Unlike the others, this is testing that == and != *are* allowed.
507 self.assertEqual(self.other == self.set, False)
508 self.assertEqual(self.set == self.other, False)
509 self.assertEqual(self.other != self.set, True)
510 self.assertEqual(self.set != self.other, True)
512 def test_ge_gt_le_lt(self):
513 self.assertRaises(TypeError, lambda: self.set < self.other)
514 self.assertRaises(TypeError, lambda: self.set <= self.other)
515 self.assertRaises(TypeError, lambda: self.set > self.other)
516 self.assertRaises(TypeError, lambda: self.set >= self.other)
518 self.assertRaises(TypeError, lambda: self.other < self.set)
519 self.assertRaises(TypeError, lambda: self.other <= self.set)
520 self.assertRaises(TypeError, lambda: self.other > self.set)
521 self.assertRaises(TypeError, lambda: self.other >= self.set)
523 def test_union_update_operator(self):
524 try:
525 self.set |= self.other
526 except TypeError:
527 pass
528 else:
529 self.fail("expected TypeError")
531 def test_union_update(self):
532 if self.otherIsIterable:
533 self.set.union_update(self.other)
534 else:
535 self.assertRaises(TypeError, self.set.union_update, self.other)
537 def test_union(self):
538 self.assertRaises(TypeError, lambda: self.set | self.other)
539 self.assertRaises(TypeError, lambda: self.other | self.set)
540 if self.otherIsIterable:
541 self.set.union(self.other)
542 else:
543 self.assertRaises(TypeError, self.set.union, self.other)
545 def test_intersection_update_operator(self):
546 try:
547 self.set &= self.other
548 except TypeError:
549 pass
550 else:
551 self.fail("expected TypeError")
553 def test_intersection_update(self):
554 if self.otherIsIterable:
555 self.set.intersection_update(self.other)
556 else:
557 self.assertRaises(TypeError,
558 self.set.intersection_update,
559 self.other)
561 def test_intersection(self):
562 self.assertRaises(TypeError, lambda: self.set & self.other)
563 self.assertRaises(TypeError, lambda: self.other & self.set)
564 if self.otherIsIterable:
565 self.set.intersection(self.other)
566 else:
567 self.assertRaises(TypeError, self.set.intersection, self.other)
569 def test_sym_difference_update_operator(self):
570 try:
571 self.set ^= self.other
572 except TypeError:
573 pass
574 else:
575 self.fail("expected TypeError")
577 def test_sym_difference_update(self):
578 if self.otherIsIterable:
579 self.set.symmetric_difference_update(self.other)
580 else:
581 self.assertRaises(TypeError,
582 self.set.symmetric_difference_update,
583 self.other)
585 def test_sym_difference(self):
586 self.assertRaises(TypeError, lambda: self.set ^ self.other)
587 self.assertRaises(TypeError, lambda: self.other ^ self.set)
588 if self.otherIsIterable:
589 self.set.symmetric_difference(self.other)
590 else:
591 self.assertRaises(TypeError, self.set.symmetric_difference, self.other)
593 def test_difference_update_operator(self):
594 try:
595 self.set -= self.other
596 except TypeError:
597 pass
598 else:
599 self.fail("expected TypeError")
601 def test_difference_update(self):
602 if self.otherIsIterable:
603 self.set.difference_update(self.other)
604 else:
605 self.assertRaises(TypeError,
606 self.set.difference_update,
607 self.other)
609 def test_difference(self):
610 self.assertRaises(TypeError, lambda: self.set - self.other)
611 self.assertRaises(TypeError, lambda: self.other - self.set)
612 if self.otherIsIterable:
613 self.set.difference(self.other)
614 else:
615 self.assertRaises(TypeError, self.set.difference, self.other)
617 #------------------------------------------------------------------------------
619 class TestOnlySetsNumeric(TestOnlySetsInBinaryOps):
620 def setUp(self):
621 self.set = Set((1, 2, 3))
622 self.other = 19
623 self.otherIsIterable = False
625 #------------------------------------------------------------------------------
627 class TestOnlySetsDict(TestOnlySetsInBinaryOps):
628 def setUp(self):
629 self.set = Set((1, 2, 3))
630 self.other = {1:2, 3:4}
631 self.otherIsIterable = True
633 #------------------------------------------------------------------------------
635 class TestOnlySetsOperator(TestOnlySetsInBinaryOps):
636 def setUp(self):
637 self.set = Set((1, 2, 3))
638 self.other = operator.add
639 self.otherIsIterable = False
641 #------------------------------------------------------------------------------
643 class TestOnlySetsTuple(TestOnlySetsInBinaryOps):
644 def setUp(self):
645 self.set = Set((1, 2, 3))
646 self.other = (2, 4, 6)
647 self.otherIsIterable = True
649 #------------------------------------------------------------------------------
651 class TestOnlySetsString(TestOnlySetsInBinaryOps):
652 def setUp(self):
653 self.set = Set((1, 2, 3))
654 self.other = 'abc'
655 self.otherIsIterable = True
657 #------------------------------------------------------------------------------
659 class TestOnlySetsGenerator(TestOnlySetsInBinaryOps):
660 def setUp(self):
661 def gen():
662 for i in xrange(0, 10, 2):
663 yield i
664 self.set = Set((1, 2, 3))
665 self.other = gen()
666 self.otherIsIterable = True
668 #------------------------------------------------------------------------------
670 class TestOnlySetsofSets(TestOnlySetsInBinaryOps):
671 def setUp(self):
672 self.set = Set((1, 2, 3))
673 self.other = [Set('ab'), ImmutableSet('cd')]
674 self.otherIsIterable = True
676 #==============================================================================
678 class TestCopying(unittest.TestCase):
680 def test_copy(self):
681 dup = self.set.copy()
682 dup_list = list(dup); dup_list.sort()
683 set_list = list(self.set); set_list.sort()
684 self.assertEqual(len(dup_list), len(set_list))
685 for i in range(len(dup_list)):
686 self.assertTrue(dup_list[i] is set_list[i])
688 def test_deep_copy(self):
689 dup = copy.deepcopy(self.set)
690 ##print type(dup), repr(dup)
691 dup_list = list(dup); dup_list.sort()
692 set_list = list(self.set); set_list.sort()
693 self.assertEqual(len(dup_list), len(set_list))
694 for i in range(len(dup_list)):
695 self.assertEqual(dup_list[i], set_list[i])
697 #------------------------------------------------------------------------------
699 class TestCopyingEmpty(TestCopying):
700 def setUp(self):
701 self.set = Set()
703 #------------------------------------------------------------------------------
705 class TestCopyingSingleton(TestCopying):
706 def setUp(self):
707 self.set = Set(["hello"])
709 #------------------------------------------------------------------------------
711 class TestCopyingTriple(TestCopying):
712 def setUp(self):
713 self.set = Set(["zero", 0, None])
715 #------------------------------------------------------------------------------
717 class TestCopyingTuple(TestCopying):
718 def setUp(self):
719 self.set = Set([(1, 2)])
721 #------------------------------------------------------------------------------
723 class TestCopyingNested(TestCopying):
724 def setUp(self):
725 self.set = Set([((1, 2), (3, 4))])
727 #==============================================================================
729 class TestIdentities(unittest.TestCase):
730 def setUp(self):
731 self.a = Set([random.randrange(100) for i in xrange(50)])
732 self.b = Set([random.randrange(100) for i in xrange(50)])
734 def test_binopsVsSubsets(self):
735 a, b = self.a, self.b
736 self.assertTrue(a - b <= a)
737 self.assertTrue(b - a <= b)
738 self.assertTrue(a & b <= a)
739 self.assertTrue(a & b <= b)
740 self.assertTrue(a | b >= a)
741 self.assertTrue(a | b >= b)
742 self.assertTrue(a ^ b <= a | b)
744 def test_commutativity(self):
745 a, b = self.a, self.b
746 self.assertEqual(a&b, b&a)
747 self.assertEqual(a|b, b|a)
748 self.assertEqual(a^b, b^a)
749 if a != b:
750 self.assertNotEqual(a-b, b-a)
752 def test_reflexsive_relations(self):
753 a, zero = self.a, Set()
754 self.assertEqual(a ^ a, zero)
755 self.assertEqual(a - a, zero)
756 self.assertEqual(a | a, a)
757 self.assertEqual(a & a, a)
758 self.assertTrue(a <= a)
759 self.assertTrue(a >= a)
760 self.assertTrue(a == a)
762 def test_summations(self):
763 # check that sums of parts equal the whole
764 a, b = self.a, self.b
765 self.assertEqual((a-b)|(a&b)|(b-a), a|b)
766 self.assertEqual((a&b)|(a^b), a|b)
767 self.assertEqual(a|(b-a), a|b)
768 self.assertEqual((a-b)|b, a|b)
769 self.assertEqual((a-b)|(a&b), a)
770 self.assertEqual((b-a)|(a&b), b)
771 self.assertEqual((a-b)|(b-a), a^b)
773 def test_exclusion(self):
774 # check that inverse operations do not overlap
775 a, b, zero = self.a, self.b, Set()
776 self.assertEqual((a-b)&b, zero)
777 self.assertEqual((b-a)&a, zero)
778 self.assertEqual((a&b)&(a^b), zero)
780 def test_cardinality_relations(self):
781 a, b = self.a, self.b
782 self.assertEqual(len(a), len(a-b) + len(a&b))
783 self.assertEqual(len(b), len(b-a) + len(a&b))
784 self.assertEqual(len(a^b), len(a-b) + len(b-a))
785 self.assertEqual(len(a|b), len(a-b) + len(a&b) + len(b-a))
786 self.assertEqual(len(a^b) + len(a&b), len(a|b))
788 #==============================================================================
790 libreftest = """
791 Example from the Library Reference: Doc/lib/libsets.tex
793 >>> from sets import Set as Base # override _repr to get sorted output
794 >>> class Set(Base):
795 ... def _repr(self):
796 ... return Base._repr(self, sorted=True)
797 >>> engineers = Set(['John', 'Jane', 'Jack', 'Janice'])
798 >>> programmers = Set(['Jack', 'Sam', 'Susan', 'Janice'])
799 >>> managers = Set(['Jane', 'Jack', 'Susan', 'Zack'])
800 >>> employees = engineers | programmers | managers # union
801 >>> engineering_management = engineers & managers # intersection
802 >>> fulltime_management = managers - engineers - programmers # difference
803 >>> engineers.add('Marvin')
804 >>> print engineers
805 Set(['Jack', 'Jane', 'Janice', 'John', 'Marvin'])
806 >>> employees.issuperset(engineers) # superset test
807 False
808 >>> employees.union_update(engineers) # update from another set
809 >>> employees.issuperset(engineers)
810 True
811 >>> for group in [engineers, programmers, managers, employees]:
812 ... group.discard('Susan') # unconditionally remove element
813 ... print group
815 Set(['Jack', 'Jane', 'Janice', 'John', 'Marvin'])
816 Set(['Jack', 'Janice', 'Sam'])
817 Set(['Jack', 'Jane', 'Zack'])
818 Set(['Jack', 'Jane', 'Janice', 'John', 'Marvin', 'Sam', 'Zack'])
821 #==============================================================================
823 __test__ = {'libreftest' : libreftest}
825 def test_main(verbose=None):
826 import doctest
827 from test import test_sets
828 test_support.run_unittest(
829 TestSetOfSets,
830 TestExceptionPropagation,
831 TestBasicOpsEmpty,
832 TestBasicOpsSingleton,
833 TestBasicOpsTuple,
834 TestBasicOpsTriple,
835 TestBinaryOps,
836 TestUpdateOps,
837 TestMutate,
838 TestSubsetEqualEmpty,
839 TestSubsetEqualNonEmpty,
840 TestSubsetEmptyNonEmpty,
841 TestSubsetPartial,
842 TestSubsetNonOverlap,
843 TestOnlySetsNumeric,
844 TestOnlySetsDict,
845 TestOnlySetsOperator,
846 TestOnlySetsTuple,
847 TestOnlySetsString,
848 TestOnlySetsGenerator,
849 TestOnlySetsofSets,
850 TestCopyingEmpty,
851 TestCopyingSingleton,
852 TestCopyingTriple,
853 TestCopyingTuple,
854 TestCopyingNested,
855 TestIdentities,
856 doctest.DocTestSuite(test_sets),
859 if __name__ == "__main__":
860 test_main(verbose=True)