3 import unittest
, operator
, copy
, pickle
, random
4 from test
import test_support
6 test_support
.import_module("sets", deprecated
=True)
7 from sets
import Set
, ImmutableSet
11 #==============================================================================
13 class TestBasicOps(unittest
.TestCase
):
16 if self
.repr is not None:
17 self
.assertEqual(repr(self
.set), self
.repr)
19 def test_length(self
):
20 self
.assertEqual(len(self
.set), self
.length
)
22 def test_self_equality(self
):
23 self
.assertEqual(self
.set, self
.set)
25 def test_equivalent_equality(self
):
26 self
.assertEqual(self
.set, self
.dup
)
29 self
.assertEqual(self
.set.copy(), self
.dup
)
31 def test_self_union(self
):
32 result
= self
.set | self
.set
33 self
.assertEqual(result
, self
.dup
)
35 def test_empty_union(self
):
36 result
= self
.set | empty_set
37 self
.assertEqual(result
, self
.dup
)
39 def test_union_empty(self
):
40 result
= empty_set | self
.set
41 self
.assertEqual(result
, self
.dup
)
43 def test_self_intersection(self
):
44 result
= self
.set & self
.set
45 self
.assertEqual(result
, self
.dup
)
47 def test_empty_intersection(self
):
48 result
= self
.set & empty_set
49 self
.assertEqual(result
, empty_set
)
51 def test_intersection_empty(self
):
52 result
= empty_set
& self
.set
53 self
.assertEqual(result
, empty_set
)
55 def test_self_symmetric_difference(self
):
56 result
= self
.set ^ self
.set
57 self
.assertEqual(result
, empty_set
)
59 def checkempty_symmetric_difference(self
):
60 result
= self
.set ^ empty_set
61 self
.assertEqual(result
, self
.set)
63 def test_self_difference(self
):
64 result
= self
.set - self
.set
65 self
.assertEqual(result
, empty_set
)
67 def test_empty_difference(self
):
68 result
= self
.set - empty_set
69 self
.assertEqual(result
, self
.dup
)
71 def test_empty_difference_rev(self
):
72 result
= empty_set
- self
.set
73 self
.assertEqual(result
, empty_set
)
75 def test_iteration(self
):
77 self
.assertIn(v
, self
.values
)
79 def test_pickling(self
):
80 p
= pickle
.dumps(self
.set)
81 copy
= pickle
.loads(p
)
82 self
.assertEqual(self
.set, copy
,
83 "%s != %s" % (self
.set, copy
))
85 #------------------------------------------------------------------------------
87 class TestBasicOpsEmpty(TestBasicOps
):
89 self
.case
= "empty set"
91 self
.set = Set(self
.values
)
92 self
.dup
= Set(self
.values
)
96 #------------------------------------------------------------------------------
98 class TestBasicOpsSingleton(TestBasicOps
):
100 self
.case
= "unit set (number)"
102 self
.set = Set(self
.values
)
103 self
.dup
= Set(self
.values
)
105 self
.repr = "Set([3])"
108 self
.assertTrue(3 in self
.set)
110 def test_not_in(self
):
111 self
.assertTrue(2 not in self
.set)
113 #------------------------------------------------------------------------------
115 class TestBasicOpsTuple(TestBasicOps
):
117 self
.case
= "unit set (tuple)"
118 self
.values
= [(0, "zero")]
119 self
.set = Set(self
.values
)
120 self
.dup
= Set(self
.values
)
122 self
.repr = "Set([(0, 'zero')])"
125 self
.assertTrue((0, "zero") in self
.set)
127 def test_not_in(self
):
128 self
.assertTrue(9 not in self
.set)
130 #------------------------------------------------------------------------------
132 class TestBasicOpsTriple(TestBasicOps
):
134 self
.case
= "triple set"
135 self
.values
= [0, "zero", operator
.add
]
136 self
.set = Set(self
.values
)
137 self
.dup
= Set(self
.values
)
141 #==============================================================================
150 class TestExceptionPropagation(unittest
.TestCase
):
151 """SF 628246: Set constructor should not trap iterator TypeErrors"""
153 def test_instanceWithException(self
):
154 self
.assertRaises(TypeError, Set
, baditer())
156 def test_instancesWithoutException(self
):
157 # All of these iterables should load without exception.
160 Set({'one':1, 'two':2, 'three':3})
165 #==============================================================================
167 class TestSetOfSets(unittest
.TestCase
):
168 def test_constructor(self
):
171 element
= outer
.pop()
172 self
.assertEqual(type(element
), ImmutableSet
)
173 outer
.add(inner
) # Rebuild set of sets with .add method
175 self
.assertEqual(outer
, Set()) # Verify that remove worked
176 outer
.discard(inner
) # Absence of KeyError indicates working fine
178 #==============================================================================
180 class TestBinaryOps(unittest
.TestCase
):
182 self
.set = Set((2, 4, 6))
184 def test_eq(self
): # SF bug 643115
185 self
.assertEqual(self
.set, Set({2:1,4:3,6:5}))
187 def test_union_subset(self
):
188 result
= self
.set |
Set([2])
189 self
.assertEqual(result
, Set((2, 4, 6)))
191 def test_union_superset(self
):
192 result
= self
.set |
Set([2, 4, 6, 8])
193 self
.assertEqual(result
, Set([2, 4, 6, 8]))
195 def test_union_overlap(self
):
196 result
= self
.set |
Set([3, 4, 5])
197 self
.assertEqual(result
, Set([2, 3, 4, 5, 6]))
199 def test_union_non_overlap(self
):
200 result
= self
.set |
Set([8])
201 self
.assertEqual(result
, Set([2, 4, 6, 8]))
203 def test_intersection_subset(self
):
204 result
= self
.set & Set((2, 4))
205 self
.assertEqual(result
, Set((2, 4)))
207 def test_intersection_superset(self
):
208 result
= self
.set & Set([2, 4, 6, 8])
209 self
.assertEqual(result
, Set([2, 4, 6]))
211 def test_intersection_overlap(self
):
212 result
= self
.set & Set([3, 4, 5])
213 self
.assertEqual(result
, Set([4]))
215 def test_intersection_non_overlap(self
):
216 result
= self
.set & Set([8])
217 self
.assertEqual(result
, empty_set
)
219 def test_sym_difference_subset(self
):
220 result
= self
.set ^
Set((2, 4))
221 self
.assertEqual(result
, Set([6]))
223 def test_sym_difference_superset(self
):
224 result
= self
.set ^
Set((2, 4, 6, 8))
225 self
.assertEqual(result
, Set([8]))
227 def test_sym_difference_overlap(self
):
228 result
= self
.set ^
Set((3, 4, 5))
229 self
.assertEqual(result
, Set([2, 3, 5, 6]))
231 def test_sym_difference_non_overlap(self
):
232 result
= self
.set ^
Set([8])
233 self
.assertEqual(result
, Set([2, 4, 6, 8]))
236 a
, b
= Set('a'), Set('b')
237 self
.assertRaises(TypeError, cmp, a
, b
)
239 # You can view this as a buglet: cmp(a, a) does not raise TypeError,
240 # because __eq__ is tried before __cmp__, and a.__eq__(a) returns True,
241 # which Python thinks is good enough to synthesize a cmp() result
242 # without calling __cmp__.
243 self
.assertEqual(cmp(a
, a
), 0)
245 self
.assertRaises(TypeError, cmp, a
, 12)
246 self
.assertRaises(TypeError, cmp, "abc", a
)
248 def test_inplace_on_self(self
):
251 self
.assertEqual(t
, self
.set)
253 self
.assertEqual(t
, self
.set)
255 self
.assertEqual(len(t
), 0)
258 self
.assertEqual(len(t
), 0)
261 #==============================================================================
263 class TestUpdateOps(unittest
.TestCase
):
265 self
.set = Set((2, 4, 6))
267 def test_union_subset(self
):
269 self
.assertEqual(self
.set, Set((2, 4, 6)))
271 def test_union_superset(self
):
272 self
.set |
= Set([2, 4, 6, 8])
273 self
.assertEqual(self
.set, Set([2, 4, 6, 8]))
275 def test_union_overlap(self
):
276 self
.set |
= Set([3, 4, 5])
277 self
.assertEqual(self
.set, Set([2, 3, 4, 5, 6]))
279 def test_union_non_overlap(self
):
281 self
.assertEqual(self
.set, Set([2, 4, 6, 8]))
283 def test_union_method_call(self
):
284 self
.set.union_update(Set([3, 4, 5]))
285 self
.assertEqual(self
.set, Set([2, 3, 4, 5, 6]))
287 def test_intersection_subset(self
):
288 self
.set &= Set((2, 4))
289 self
.assertEqual(self
.set, Set((2, 4)))
291 def test_intersection_superset(self
):
292 self
.set &= Set([2, 4, 6, 8])
293 self
.assertEqual(self
.set, Set([2, 4, 6]))
295 def test_intersection_overlap(self
):
296 self
.set &= Set([3, 4, 5])
297 self
.assertEqual(self
.set, Set([4]))
299 def test_intersection_non_overlap(self
):
301 self
.assertEqual(self
.set, empty_set
)
303 def test_intersection_method_call(self
):
304 self
.set.intersection_update(Set([3, 4, 5]))
305 self
.assertEqual(self
.set, Set([4]))
307 def test_sym_difference_subset(self
):
308 self
.set ^
= Set((2, 4))
309 self
.assertEqual(self
.set, Set([6]))
311 def test_sym_difference_superset(self
):
312 self
.set ^
= Set((2, 4, 6, 8))
313 self
.assertEqual(self
.set, Set([8]))
315 def test_sym_difference_overlap(self
):
316 self
.set ^
= Set((3, 4, 5))
317 self
.assertEqual(self
.set, Set([2, 3, 5, 6]))
319 def test_sym_difference_non_overlap(self
):
321 self
.assertEqual(self
.set, Set([2, 4, 6, 8]))
323 def test_sym_difference_method_call(self
):
324 self
.set.symmetric_difference_update(Set([3, 4, 5]))
325 self
.assertEqual(self
.set, Set([2, 3, 5, 6]))
327 def test_difference_subset(self
):
328 self
.set -= Set((2, 4))
329 self
.assertEqual(self
.set, Set([6]))
331 def test_difference_superset(self
):
332 self
.set -= Set((2, 4, 6, 8))
333 self
.assertEqual(self
.set, Set([]))
335 def test_difference_overlap(self
):
336 self
.set -= Set((3, 4, 5))
337 self
.assertEqual(self
.set, Set([2, 6]))
339 def test_difference_non_overlap(self
):
341 self
.assertEqual(self
.set, Set([2, 4, 6]))
343 def test_difference_method_call(self
):
344 self
.set.difference_update(Set([3, 4, 5]))
345 self
.assertEqual(self
.set, Set([2, 6]))
347 #==============================================================================
349 class TestMutate(unittest
.TestCase
):
351 self
.values
= ["a", "b", "c"]
352 self
.set = Set(self
.values
)
354 def test_add_present(self
):
356 self
.assertEqual(self
.set, Set("abc"))
358 def test_add_absent(self
):
360 self
.assertEqual(self
.set, Set("abcd"))
362 def test_add_until_full(self
):
365 for v
in self
.values
:
368 self
.assertEqual(len(tmp
), expected_len
)
369 self
.assertEqual(tmp
, self
.set)
371 def test_remove_present(self
):
373 self
.assertEqual(self
.set, Set("ac"))
375 def test_remove_absent(self
):
378 self
.fail("Removing missing element should have raised LookupError")
382 def test_remove_until_empty(self
):
383 expected_len
= len(self
.set)
384 for v
in self
.values
:
387 self
.assertEqual(len(self
.set), expected_len
)
389 def test_discard_present(self
):
390 self
.set.discard("c")
391 self
.assertEqual(self
.set, Set("ab"))
393 def test_discard_absent(self
):
394 self
.set.discard("d")
395 self
.assertEqual(self
.set, Set("abc"))
397 def test_clear(self
):
399 self
.assertEqual(len(self
.set), 0)
404 popped
[self
.set.pop()] = None
405 self
.assertEqual(len(popped
), len(self
.values
))
406 for v
in self
.values
:
407 self
.assertIn(v
, popped
)
409 def test_update_empty_tuple(self
):
410 self
.set.union_update(())
411 self
.assertEqual(self
.set, Set(self
.values
))
413 def test_update_unit_tuple_overlap(self
):
414 self
.set.union_update(("a",))
415 self
.assertEqual(self
.set, Set(self
.values
))
417 def test_update_unit_tuple_non_overlap(self
):
418 self
.set.union_update(("a", "z"))
419 self
.assertEqual(self
.set, Set(self
.values
+ ["z"]))
421 #==============================================================================
423 class TestSubsets(unittest
.TestCase
):
425 case2method
= {"<=": "issubset",
429 reverse
= {"==": "==",
437 def test_issubset(self
):
440 for case
in "!=", "==", "<", "<=", ">", ">=":
441 expected
= case
in self
.cases
442 # Test the binary infix spelling.
443 result
= eval("x" + case
+ "y", locals())
444 self
.assertEqual(result
, expected
)
445 # Test the "friendly" method-name spelling, if one exists.
446 if case
in TestSubsets
.case2method
:
447 method
= getattr(x
, TestSubsets
.case2method
[case
])
449 self
.assertEqual(result
, expected
)
451 # Now do the same for the operands reversed.
452 rcase
= TestSubsets
.reverse
[case
]
453 result
= eval("y" + rcase
+ "x", locals())
454 self
.assertEqual(result
, expected
)
455 if rcase
in TestSubsets
.case2method
:
456 method
= getattr(y
, TestSubsets
.case2method
[rcase
])
458 self
.assertEqual(result
, expected
)
459 #------------------------------------------------------------------------------
461 class TestSubsetEqualEmpty(TestSubsets
):
465 cases
= "==", "<=", ">="
467 #------------------------------------------------------------------------------
469 class TestSubsetEqualNonEmpty(TestSubsets
):
473 cases
= "==", "<=", ">="
475 #------------------------------------------------------------------------------
477 class TestSubsetEmptyNonEmpty(TestSubsets
):
480 name
= "one empty, one non-empty"
481 cases
= "!=", "<", "<="
483 #------------------------------------------------------------------------------
485 class TestSubsetPartial(TestSubsets
):
488 name
= "one a non-empty proper subset of other"
489 cases
= "!=", "<", "<="
491 #------------------------------------------------------------------------------
493 class TestSubsetNonOverlap(TestSubsets
):
496 name
= "neither empty, neither contains"
499 #==============================================================================
501 class TestOnlySetsInBinaryOps(unittest
.TestCase
):
503 def test_eq_ne(self
):
504 # Unlike the others, this is testing that == and != *are* allowed.
505 self
.assertEqual(self
.other
== self
.set, False)
506 self
.assertEqual(self
.set == self
.other
, False)
507 self
.assertEqual(self
.other
!= self
.set, True)
508 self
.assertEqual(self
.set != self
.other
, True)
510 def test_ge_gt_le_lt(self
):
511 self
.assertRaises(TypeError, lambda: self
.set < self
.other
)
512 self
.assertRaises(TypeError, lambda: self
.set <= self
.other
)
513 self
.assertRaises(TypeError, lambda: self
.set > self
.other
)
514 self
.assertRaises(TypeError, lambda: self
.set >= self
.other
)
516 self
.assertRaises(TypeError, lambda: self
.other
< self
.set)
517 self
.assertRaises(TypeError, lambda: self
.other
<= self
.set)
518 self
.assertRaises(TypeError, lambda: self
.other
> self
.set)
519 self
.assertRaises(TypeError, lambda: self
.other
>= self
.set)
521 def test_union_update_operator(self
):
523 self
.set |
= self
.other
527 self
.fail("expected TypeError")
529 def test_union_update(self
):
530 if self
.otherIsIterable
:
531 self
.set.union_update(self
.other
)
533 self
.assertRaises(TypeError, self
.set.union_update
, self
.other
)
535 def test_union(self
):
536 self
.assertRaises(TypeError, lambda: self
.set | self
.other
)
537 self
.assertRaises(TypeError, lambda: self
.other | self
.set)
538 if self
.otherIsIterable
:
539 self
.set.union(self
.other
)
541 self
.assertRaises(TypeError, self
.set.union
, self
.other
)
543 def test_intersection_update_operator(self
):
545 self
.set &= self
.other
549 self
.fail("expected TypeError")
551 def test_intersection_update(self
):
552 if self
.otherIsIterable
:
553 self
.set.intersection_update(self
.other
)
555 self
.assertRaises(TypeError,
556 self
.set.intersection_update
,
559 def test_intersection(self
):
560 self
.assertRaises(TypeError, lambda: self
.set & self
.other
)
561 self
.assertRaises(TypeError, lambda: self
.other
& self
.set)
562 if self
.otherIsIterable
:
563 self
.set.intersection(self
.other
)
565 self
.assertRaises(TypeError, self
.set.intersection
, self
.other
)
567 def test_sym_difference_update_operator(self
):
569 self
.set ^
= self
.other
573 self
.fail("expected TypeError")
575 def test_sym_difference_update(self
):
576 if self
.otherIsIterable
:
577 self
.set.symmetric_difference_update(self
.other
)
579 self
.assertRaises(TypeError,
580 self
.set.symmetric_difference_update
,
583 def test_sym_difference(self
):
584 self
.assertRaises(TypeError, lambda: self
.set ^ self
.other
)
585 self
.assertRaises(TypeError, lambda: self
.other ^ self
.set)
586 if self
.otherIsIterable
:
587 self
.set.symmetric_difference(self
.other
)
589 self
.assertRaises(TypeError, self
.set.symmetric_difference
, self
.other
)
591 def test_difference_update_operator(self
):
593 self
.set -= self
.other
597 self
.fail("expected TypeError")
599 def test_difference_update(self
):
600 if self
.otherIsIterable
:
601 self
.set.difference_update(self
.other
)
603 self
.assertRaises(TypeError,
604 self
.set.difference_update
,
607 def test_difference(self
):
608 self
.assertRaises(TypeError, lambda: self
.set - self
.other
)
609 self
.assertRaises(TypeError, lambda: self
.other
- self
.set)
610 if self
.otherIsIterable
:
611 self
.set.difference(self
.other
)
613 self
.assertRaises(TypeError, self
.set.difference
, self
.other
)
615 #------------------------------------------------------------------------------
617 class TestOnlySetsNumeric(TestOnlySetsInBinaryOps
):
619 self
.set = Set((1, 2, 3))
621 self
.otherIsIterable
= False
623 #------------------------------------------------------------------------------
625 class TestOnlySetsDict(TestOnlySetsInBinaryOps
):
627 self
.set = Set((1, 2, 3))
628 self
.other
= {1:2, 3:4}
629 self
.otherIsIterable
= True
631 #------------------------------------------------------------------------------
633 class TestOnlySetsOperator(TestOnlySetsInBinaryOps
):
635 self
.set = Set((1, 2, 3))
636 self
.other
= operator
.add
637 self
.otherIsIterable
= False
639 def test_ge_gt_le_lt(self
):
640 with test_support
.check_py3k_warnings():
641 super(TestOnlySetsOperator
, self
).test_ge_gt_le_lt()
643 #------------------------------------------------------------------------------
645 class TestOnlySetsTuple(TestOnlySetsInBinaryOps
):
647 self
.set = Set((1, 2, 3))
648 self
.other
= (2, 4, 6)
649 self
.otherIsIterable
= True
651 #------------------------------------------------------------------------------
653 class TestOnlySetsString(TestOnlySetsInBinaryOps
):
655 self
.set = Set((1, 2, 3))
657 self
.otherIsIterable
= True
659 #------------------------------------------------------------------------------
661 class TestOnlySetsGenerator(TestOnlySetsInBinaryOps
):
664 for i
in xrange(0, 10, 2):
666 self
.set = Set((1, 2, 3))
668 self
.otherIsIterable
= True
670 #------------------------------------------------------------------------------
672 class TestOnlySetsofSets(TestOnlySetsInBinaryOps
):
674 self
.set = Set((1, 2, 3))
675 self
.other
= [Set('ab'), ImmutableSet('cd')]
676 self
.otherIsIterable
= True
678 #==============================================================================
680 class TestCopying(unittest
.TestCase
):
683 dup
= self
.set.copy()
684 self
.assertEqual(len(dup
), len(self
.set))
685 dup_list
= sorted(dup
)
686 set_list
= sorted(self
.set)
687 self
.assertEqual(len(dup_list
), len(set_list
))
688 for i
, el
in enumerate(dup_list
):
689 self
.assertIs(el
, set_list
[i
])
691 def test_deep_copy(self
):
692 dup
= copy
.deepcopy(self
.set)
693 self
.assertSetEqual(dup
, self
.set)
695 #------------------------------------------------------------------------------
697 class TestCopyingEmpty(TestCopying
):
701 #------------------------------------------------------------------------------
703 class TestCopyingSingleton(TestCopying
):
705 self
.set = Set(["hello"])
707 #------------------------------------------------------------------------------
709 class TestCopyingTriple(TestCopying
):
711 self
.set = Set(["zero", 0, None])
714 with test_support
.check_py3k_warnings():
715 super(TestCopyingTriple
, self
).test_copy()
717 #------------------------------------------------------------------------------
719 class TestCopyingTuple(TestCopying
):
721 self
.set = Set([(1, 2)])
723 #------------------------------------------------------------------------------
725 class TestCopyingNested(TestCopying
):
727 self
.set = Set([((1, 2), (3, 4))])
729 #==============================================================================
731 class TestIdentities(unittest
.TestCase
):
733 self
.a
= Set([random
.randrange(100) for i
in xrange(50)])
734 self
.b
= Set([random
.randrange(100) for i
in xrange(50)])
736 def test_binopsVsSubsets(self
):
737 a
, b
= self
.a
, self
.b
738 self
.assertTrue(a
- b
<= a
)
739 self
.assertTrue(b
- a
<= b
)
740 self
.assertTrue(a
& b
<= a
)
741 self
.assertTrue(a
& b
<= b
)
742 self
.assertTrue(a | b
>= a
)
743 self
.assertTrue(a | b
>= b
)
744 self
.assertTrue(a ^ b
<= a | b
)
746 def test_commutativity(self
):
747 a
, b
= self
.a
, self
.b
748 self
.assertEqual(a
&b
, b
&a
)
749 self
.assertEqual(a|b
, b|a
)
750 self
.assertEqual(a^b
, b^a
)
752 self
.assertNotEqual(a
-b
, b
-a
)
754 def test_reflexsive_relations(self
):
755 a
, zero
= self
.a
, Set()
756 self
.assertEqual(a ^ a
, zero
)
757 self
.assertEqual(a
- a
, zero
)
758 self
.assertEqual(a | a
, a
)
759 self
.assertEqual(a
& a
, a
)
760 self
.assertTrue(a
<= a
)
761 self
.assertTrue(a
>= a
)
762 self
.assertTrue(a
== a
)
764 def test_summations(self
):
765 # check that sums of parts equal the whole
766 a
, b
= self
.a
, self
.b
767 self
.assertEqual((a
-b
)|
(a
&b
)|
(b
-a
), a|b
)
768 self
.assertEqual((a
&b
)|
(a^b
), a|b
)
769 self
.assertEqual(a|
(b
-a
), a|b
)
770 self
.assertEqual((a
-b
)|b
, a|b
)
771 self
.assertEqual((a
-b
)|
(a
&b
), a
)
772 self
.assertEqual((b
-a
)|
(a
&b
), b
)
773 self
.assertEqual((a
-b
)|
(b
-a
), a^b
)
775 def test_exclusion(self
):
776 # check that inverse operations do not overlap
777 a
, b
, zero
= self
.a
, self
.b
, Set()
778 self
.assertEqual((a
-b
)&b
, zero
)
779 self
.assertEqual((b
-a
)&a
, zero
)
780 self
.assertEqual((a
&b
)&(a^b
), zero
)
782 def test_cardinality_relations(self
):
783 a
, b
= self
.a
, self
.b
784 self
.assertEqual(len(a
), len(a
-b
) + len(a
&b
))
785 self
.assertEqual(len(b
), len(b
-a
) + len(a
&b
))
786 self
.assertEqual(len(a^b
), len(a
-b
) + len(b
-a
))
787 self
.assertEqual(len(a|b
), len(a
-b
) + len(a
&b
) + len(b
-a
))
788 self
.assertEqual(len(a^b
) + len(a
&b
), len(a|b
))
790 #==============================================================================
793 Example from the Library Reference: Doc/lib/libsets.tex
795 >>> from sets import Set as Base # override _repr to get sorted output
798 ... return Base._repr(self, sorted=True)
799 >>> engineers = Set(['John', 'Jane', 'Jack', 'Janice'])
800 >>> programmers = Set(['Jack', 'Sam', 'Susan', 'Janice'])
801 >>> managers = Set(['Jane', 'Jack', 'Susan', 'Zack'])
802 >>> employees = engineers | programmers | managers # union
803 >>> engineering_management = engineers & managers # intersection
804 >>> fulltime_management = managers - engineers - programmers # difference
805 >>> engineers.add('Marvin')
807 Set(['Jack', 'Jane', 'Janice', 'John', 'Marvin'])
808 >>> employees.issuperset(engineers) # superset test
810 >>> employees.union_update(engineers) # update from another set
811 >>> employees.issuperset(engineers)
813 >>> for group in [engineers, programmers, managers, employees]:
814 ... group.discard('Susan') # unconditionally remove element
817 Set(['Jack', 'Jane', 'Janice', 'John', 'Marvin'])
818 Set(['Jack', 'Janice', 'Sam'])
819 Set(['Jack', 'Jane', 'Zack'])
820 Set(['Jack', 'Jane', 'Janice', 'John', 'Marvin', 'Sam', 'Zack'])
823 #==============================================================================
825 __test__
= {'libreftest' : libreftest
}
827 def test_main(verbose
=None):
829 from test
import test_sets
830 test_support
.run_unittest(
832 TestExceptionPropagation
,
834 TestBasicOpsSingleton
,
840 TestSubsetEqualEmpty
,
841 TestSubsetEqualNonEmpty
,
842 TestSubsetEmptyNonEmpty
,
844 TestSubsetNonOverlap
,
847 TestOnlySetsOperator
,
850 TestOnlySetsGenerator
,
853 TestCopyingSingleton
,
858 doctest
.DocTestSuite(test_sets
),
861 if __name__
== "__main__":
862 test_main(verbose
=True)