3 from test
import test_support
9 from random
import randrange
, shuffle
13 class PassThru(Exception):
16 def check_pass_thru():
23 def __cmp__(self
, other
):
27 'Used to test self-referential repr() calls'
29 return repr(self
.value
)
31 class HashCountingInt(int):
32 'int-like object that counts the number of times __hash__ is called'
33 def __init__(self
, *args
):
37 return int.__hash
__(self
)
39 class TestJointOps(unittest
.TestCase
):
40 # Tests common to both set and frozenset
43 self
.word
= word
= 'simsalabim'
44 self
.otherword
= 'madagascar'
45 self
.letters
= 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
46 self
.s
= self
.thetype(word
)
47 self
.d
= dict.fromkeys(word
)
49 def test_new_or_init(self
):
50 self
.assertRaises(TypeError, self
.thetype
, [], 2)
51 self
.assertRaises(TypeError, set().__init
__, a
=1)
53 def test_uniquification(self
):
54 actual
= sorted(self
.s
)
55 expected
= sorted(self
.d
)
56 self
.assertEqual(actual
, expected
)
57 self
.assertRaises(PassThru
, self
.thetype
, check_pass_thru())
58 self
.assertRaises(TypeError, self
.thetype
, [[]])
61 self
.assertEqual(len(self
.s
), len(self
.d
))
63 def test_contains(self
):
64 for c
in self
.letters
:
65 self
.assertEqual(c
in self
.s
, c
in self
.d
)
66 self
.assertRaises(TypeError, self
.s
.__contains
__, [[]])
67 s
= self
.thetype([frozenset(self
.letters
)])
68 self
.assertIn(self
.thetype(self
.letters
), s
)
71 u
= self
.s
.union(self
.otherword
)
72 for c
in self
.letters
:
73 self
.assertEqual(c
in u
, c
in self
.d
or c
in self
.otherword
)
74 self
.assertEqual(self
.s
, self
.thetype(self
.word
))
75 self
.assertEqual(type(u
), self
.thetype
)
76 self
.assertRaises(PassThru
, self
.s
.union
, check_pass_thru())
77 self
.assertRaises(TypeError, self
.s
.union
, [[]])
78 for C
in set, frozenset, dict.fromkeys
, str, unicode, list, tuple:
79 self
.assertEqual(self
.thetype('abcba').union(C('cdc')), set('abcd'))
80 self
.assertEqual(self
.thetype('abcba').union(C('efgfe')), set('abcefg'))
81 self
.assertEqual(self
.thetype('abcba').union(C('ccb')), set('abc'))
82 self
.assertEqual(self
.thetype('abcba').union(C('ef')), set('abcef'))
83 self
.assertEqual(self
.thetype('abcba').union(C('ef'), C('fg')), set('abcefg'))
87 self
.assertEqual(x
.union(set([1]), x
, set([2])), self
.thetype([1, 2]))
90 i
= self
.s
.union(self
.otherword
)
91 self
.assertEqual(self
.s |
set(self
.otherword
), i
)
92 self
.assertEqual(self
.s |
frozenset(self
.otherword
), i
)
94 self
.s | self
.otherword
98 self
.fail("s|t did not screen-out general iterables")
100 def test_intersection(self
):
101 i
= self
.s
.intersection(self
.otherword
)
102 for c
in self
.letters
:
103 self
.assertEqual(c
in i
, c
in self
.d
and c
in self
.otherword
)
104 self
.assertEqual(self
.s
, self
.thetype(self
.word
))
105 self
.assertEqual(type(i
), self
.thetype
)
106 self
.assertRaises(PassThru
, self
.s
.intersection
, check_pass_thru())
107 for C
in set, frozenset, dict.fromkeys
, str, unicode, list, tuple:
108 self
.assertEqual(self
.thetype('abcba').intersection(C('cdc')), set('cc'))
109 self
.assertEqual(self
.thetype('abcba').intersection(C('efgfe')), set(''))
110 self
.assertEqual(self
.thetype('abcba').intersection(C('ccb')), set('bc'))
111 self
.assertEqual(self
.thetype('abcba').intersection(C('ef')), set(''))
112 self
.assertEqual(self
.thetype('abcba').intersection(C('cbcf'), C('bag')), set('b'))
113 s
= self
.thetype('abcba')
115 if self
.thetype
== frozenset():
116 self
.assertEqual(id(s
), id(z
))
118 self
.assertNotEqual(id(s
), id(z
))
120 def test_isdisjoint(self
):
122 'Pure python equivalent of isdisjoint()'
123 return not set(s1
).intersection(s2
)
124 for larg
in '', 'a', 'ab', 'abc', 'ababac', 'cdc', 'cc', 'efgfe', 'ccb', 'ef':
125 s1
= self
.thetype(larg
)
126 for rarg
in '', 'a', 'ab', 'abc', 'ababac', 'cdc', 'cc', 'efgfe', 'ccb', 'ef':
127 for C
in set, frozenset, dict.fromkeys
, str, unicode, list, tuple:
129 actual
= s1
.isdisjoint(s2
)
131 self
.assertEqual(actual
, expected
)
132 self
.assertTrue(actual
is True or actual
is False)
135 i
= self
.s
.intersection(self
.otherword
)
136 self
.assertEqual(self
.s
& set(self
.otherword
), i
)
137 self
.assertEqual(self
.s
& frozenset(self
.otherword
), i
)
139 self
.s
& self
.otherword
143 self
.fail("s&t did not screen-out general iterables")
145 def test_difference(self
):
146 i
= self
.s
.difference(self
.otherword
)
147 for c
in self
.letters
:
148 self
.assertEqual(c
in i
, c
in self
.d
and c
not in self
.otherword
)
149 self
.assertEqual(self
.s
, self
.thetype(self
.word
))
150 self
.assertEqual(type(i
), self
.thetype
)
151 self
.assertRaises(PassThru
, self
.s
.difference
, check_pass_thru())
152 self
.assertRaises(TypeError, self
.s
.difference
, [[]])
153 for C
in set, frozenset, dict.fromkeys
, str, unicode, list, tuple:
154 self
.assertEqual(self
.thetype('abcba').difference(C('cdc')), set('ab'))
155 self
.assertEqual(self
.thetype('abcba').difference(C('efgfe')), set('abc'))
156 self
.assertEqual(self
.thetype('abcba').difference(C('ccb')), set('a'))
157 self
.assertEqual(self
.thetype('abcba').difference(C('ef')), set('abc'))
158 self
.assertEqual(self
.thetype('abcba').difference(), set('abc'))
159 self
.assertEqual(self
.thetype('abcba').difference(C('a'), C('b')), set('c'))
162 i
= self
.s
.difference(self
.otherword
)
163 self
.assertEqual(self
.s
- set(self
.otherword
), i
)
164 self
.assertEqual(self
.s
- frozenset(self
.otherword
), i
)
166 self
.s
- self
.otherword
170 self
.fail("s-t did not screen-out general iterables")
172 def test_symmetric_difference(self
):
173 i
= self
.s
.symmetric_difference(self
.otherword
)
174 for c
in self
.letters
:
175 self
.assertEqual(c
in i
, (c
in self
.d
) ^
(c
in self
.otherword
))
176 self
.assertEqual(self
.s
, self
.thetype(self
.word
))
177 self
.assertEqual(type(i
), self
.thetype
)
178 self
.assertRaises(PassThru
, self
.s
.symmetric_difference
, check_pass_thru())
179 self
.assertRaises(TypeError, self
.s
.symmetric_difference
, [[]])
180 for C
in set, frozenset, dict.fromkeys
, str, unicode, list, tuple:
181 self
.assertEqual(self
.thetype('abcba').symmetric_difference(C('cdc')), set('abd'))
182 self
.assertEqual(self
.thetype('abcba').symmetric_difference(C('efgfe')), set('abcefg'))
183 self
.assertEqual(self
.thetype('abcba').symmetric_difference(C('ccb')), set('a'))
184 self
.assertEqual(self
.thetype('abcba').symmetric_difference(C('ef')), set('abcef'))
187 i
= self
.s
.symmetric_difference(self
.otherword
)
188 self
.assertEqual(self
.s ^
set(self
.otherword
), i
)
189 self
.assertEqual(self
.s ^
frozenset(self
.otherword
), i
)
191 self
.s ^ self
.otherword
195 self
.fail("s^t did not screen-out general iterables")
197 def test_equality(self
):
198 self
.assertEqual(self
.s
, set(self
.word
))
199 self
.assertEqual(self
.s
, frozenset(self
.word
))
200 self
.assertEqual(self
.s
== self
.word
, False)
201 self
.assertNotEqual(self
.s
, set(self
.otherword
))
202 self
.assertNotEqual(self
.s
, frozenset(self
.otherword
))
203 self
.assertEqual(self
.s
!= self
.word
, True)
205 def test_setOfFrozensets(self
):
206 t
= map(frozenset, ['abcdef', 'bcd', 'bdcb', 'fed', 'fedccba'])
208 self
.assertEqual(len(s
), 3)
210 def test_compare(self
):
211 self
.assertRaises(TypeError, self
.s
.__cmp
__, self
.s
)
213 def test_sub_and_super(self
):
214 p
, q
, r
= map(self
.thetype
, ['ab', 'abcde', 'def'])
215 self
.assertTrue(p
< q
)
216 self
.assertTrue(p
<= q
)
217 self
.assertTrue(q
<= q
)
218 self
.assertTrue(q
> p
)
219 self
.assertTrue(q
>= p
)
220 self
.assertFalse(q
< r
)
221 self
.assertFalse(q
<= r
)
222 self
.assertFalse(q
> r
)
223 self
.assertFalse(q
>= r
)
224 self
.assertTrue(set('a').issubset('abc'))
225 self
.assertTrue(set('abc').issuperset('a'))
226 self
.assertFalse(set('a').issubset('cbs'))
227 self
.assertFalse(set('cbs').issuperset('a'))
229 def test_pickling(self
):
230 for i
in range(pickle
.HIGHEST_PROTOCOL
+ 1):
231 p
= pickle
.dumps(self
.s
, i
)
232 dup
= pickle
.loads(p
)
233 self
.assertEqual(self
.s
, dup
, "%s != %s" % (self
.s
, dup
))
234 if type(self
.s
) not in (set, frozenset):
236 p
= pickle
.dumps(self
.s
)
237 dup
= pickle
.loads(p
)
238 self
.assertEqual(self
.s
.x
, dup
.x
)
240 def test_deepcopy(self
):
242 def __init__(self
, value
):
246 def __deepcopy__(self
, memo
=None):
247 return Tracer(self
.value
+ 1)
249 s
= self
.thetype([t
])
250 dup
= copy
.deepcopy(s
)
251 self
.assertNotEqual(id(s
), id(dup
))
254 self
.assertNotEqual(id(t
), id(newt
))
255 self
.assertEqual(t
.value
+ 1, newt
.value
)
258 # Create a nest of cycles to exercise overall ref count check
261 s
= set(A() for i
in xrange(1000))
265 elem
.set = set([elem
])
267 def test_subclass_with_custom_hash(self
):
269 class H(self
.thetype
):
271 return int(id(self
) & 0x7fffffff)
280 def test_badcmp(self
):
281 s
= self
.thetype([BadCmp()])
282 # Detect comparison errors during insertion and lookup
283 self
.assertRaises(RuntimeError, self
.thetype
, [BadCmp(), BadCmp()])
284 self
.assertRaises(RuntimeError, s
.__contains
__, BadCmp())
285 # Detect errors during mutating operations
286 if hasattr(s
, 'add'):
287 self
.assertRaises(RuntimeError, s
.add
, BadCmp())
288 self
.assertRaises(RuntimeError, s
.discard
, BadCmp())
289 self
.assertRaises(RuntimeError, s
.remove
, BadCmp())
291 def test_cyclical_repr(self
):
293 s
= self
.thetype([w
])
295 name
= repr(s
).partition('(')[0] # strip class name from repr string
296 self
.assertEqual(repr(s
), '%s([%s(...)])' % (name
, name
))
298 def test_cyclical_print(self
):
300 s
= self
.thetype([w
])
302 fo
= open(test_support
.TESTFN
, "wb")
306 fo
= open(test_support
.TESTFN
, "rb")
307 self
.assertEqual(fo
.read(), repr(s
))
310 test_support
.unlink(test_support
.TESTFN
)
312 def test_do_not_rehash_dict_keys(self
):
314 d
= dict.fromkeys(map(HashCountingInt
, xrange(n
)))
315 self
.assertEqual(sum(elem
.hash_count
for elem
in d
), n
)
317 self
.assertEqual(sum(elem
.hash_count
for elem
in d
), n
)
319 self
.assertEqual(sum(elem
.hash_count
for elem
in d
), n
)
320 if hasattr(s
, 'symmetric_difference_update'):
321 s
.symmetric_difference_update(d
)
322 self
.assertEqual(sum(elem
.hash_count
for elem
in d
), n
)
323 d2
= dict.fromkeys(set(d
))
324 self
.assertEqual(sum(elem
.hash_count
for elem
in d
), n
)
325 d3
= dict.fromkeys(frozenset(d
))
326 self
.assertEqual(sum(elem
.hash_count
for elem
in d
), n
)
327 d3
= dict.fromkeys(frozenset(d
), 123)
328 self
.assertEqual(sum(elem
.hash_count
for elem
in d
), n
)
329 self
.assertEqual(d3
, dict.fromkeys(d
, 123))
331 def test_container_iterator(self
):
332 # Bug #3680: tp_traverse was not implemented for set iterator object
336 ref
= weakref
.ref(obj
)
337 container
= set([obj
, 1])
338 obj
.x
= iter(container
)
341 self
.assertTrue(ref() is None, "Cycle was not collected")
343 class TestSet(TestJointOps
):
348 s
.__init
__(self
.word
)
349 self
.assertEqual(s
, set(self
.word
))
350 s
.__init
__(self
.otherword
)
351 self
.assertEqual(s
, set(self
.otherword
))
352 self
.assertRaises(TypeError, s
.__init
__, s
, 2);
353 self
.assertRaises(TypeError, s
.__init
__, 1);
355 def test_constructor_identity(self
):
356 s
= self
.thetype(range(3))
358 self
.assertNotEqual(id(s
), id(t
))
361 self
.assertRaises(TypeError, hash, self
.s
)
363 def test_clear(self
):
365 self
.assertEqual(self
.s
, set())
366 self
.assertEqual(len(self
.s
), 0)
370 self
.assertEqual(self
.s
, dup
)
371 self
.assertNotEqual(id(self
.s
), id(dup
))
375 self
.assertIn('Q', self
.s
)
378 self
.assertEqual(self
.s
, dup
)
379 self
.assertRaises(TypeError, self
.s
.add
, [])
381 def test_remove(self
):
383 self
.assertNotIn('a', self
.s
)
384 self
.assertRaises(KeyError, self
.s
.remove
, 'Q')
385 self
.assertRaises(TypeError, self
.s
.remove
, [])
386 s
= self
.thetype([frozenset(self
.word
)])
387 self
.assertIn(self
.thetype(self
.word
), s
)
388 s
.remove(self
.thetype(self
.word
))
389 self
.assertNotIn(self
.thetype(self
.word
), s
)
390 self
.assertRaises(KeyError, self
.s
.remove
, self
.thetype(self
.word
))
392 def test_remove_keyerror_unpacking(self
):
393 # bug: www.python.org/sf/1576657
394 for v1
in ['Q', (1,)]:
399 self
.assertEqual(v1
, v2
)
403 def test_remove_keyerror_set(self
):
404 key
= self
.thetype([3, 4])
407 except KeyError as e
:
408 self
.assertTrue(e
.args
[0] is key
,
409 "KeyError should be {0}, not {1}".format(key
,
414 def test_discard(self
):
416 self
.assertNotIn('a', self
.s
)
418 self
.assertRaises(TypeError, self
.s
.discard
, [])
419 s
= self
.thetype([frozenset(self
.word
)])
420 self
.assertIn(self
.thetype(self
.word
), s
)
421 s
.discard(self
.thetype(self
.word
))
422 self
.assertNotIn(self
.thetype(self
.word
), s
)
423 s
.discard(self
.thetype(self
.word
))
426 for i
in xrange(len(self
.s
)):
428 self
.assertNotIn(elem
, self
.s
)
429 self
.assertRaises(KeyError, self
.s
.pop
)
431 def test_update(self
):
432 retval
= self
.s
.update(self
.otherword
)
433 self
.assertEqual(retval
, None)
434 for c
in (self
.word
+ self
.otherword
):
435 self
.assertIn(c
, self
.s
)
436 self
.assertRaises(PassThru
, self
.s
.update
, check_pass_thru())
437 self
.assertRaises(TypeError, self
.s
.update
, [[]])
438 for p
, q
in (('cdc', 'abcd'), ('efgfe', 'abcefg'), ('ccb', 'abc'), ('ef', 'abcef')):
439 for C
in set, frozenset, dict.fromkeys
, str, unicode, list, tuple:
440 s
= self
.thetype('abcba')
441 self
.assertEqual(s
.update(C(p
)), None)
442 self
.assertEqual(s
, set(q
))
443 for p
in ('cdc', 'efgfe', 'ccb', 'ef', 'abcda'):
445 for C
in set, frozenset, dict.fromkeys
, str, unicode, list, tuple:
446 s
= self
.thetype('abcba')
447 self
.assertEqual(s
.update(C(p
), C(q
)), None)
448 self
.assertEqual(s
, set(s
) |
set(p
) |
set(q
))
451 self
.s |
= set(self
.otherword
)
452 for c
in (self
.word
+ self
.otherword
):
453 self
.assertIn(c
, self
.s
)
455 def test_intersection_update(self
):
456 retval
= self
.s
.intersection_update(self
.otherword
)
457 self
.assertEqual(retval
, None)
458 for c
in (self
.word
+ self
.otherword
):
459 if c
in self
.otherword
and c
in self
.word
:
460 self
.assertIn(c
, self
.s
)
462 self
.assertNotIn(c
, self
.s
)
463 self
.assertRaises(PassThru
, self
.s
.intersection_update
, check_pass_thru())
464 self
.assertRaises(TypeError, self
.s
.intersection_update
, [[]])
465 for p
, q
in (('cdc', 'c'), ('efgfe', ''), ('ccb', 'bc'), ('ef', '')):
466 for C
in set, frozenset, dict.fromkeys
, str, unicode, list, tuple:
467 s
= self
.thetype('abcba')
468 self
.assertEqual(s
.intersection_update(C(p
)), None)
469 self
.assertEqual(s
, set(q
))
473 self
.assertEqual(s
.intersection_update(C(p
), C(t
)), None)
474 self
.assertEqual(s
, set('abcba')&set(p
)&set(t
))
477 self
.s
&= set(self
.otherword
)
478 for c
in (self
.word
+ self
.otherword
):
479 if c
in self
.otherword
and c
in self
.word
:
480 self
.assertIn(c
, self
.s
)
482 self
.assertNotIn(c
, self
.s
)
484 def test_difference_update(self
):
485 retval
= self
.s
.difference_update(self
.otherword
)
486 self
.assertEqual(retval
, None)
487 for c
in (self
.word
+ self
.otherword
):
488 if c
in self
.word
and c
not in self
.otherword
:
489 self
.assertIn(c
, self
.s
)
491 self
.assertNotIn(c
, self
.s
)
492 self
.assertRaises(PassThru
, self
.s
.difference_update
, check_pass_thru())
493 self
.assertRaises(TypeError, self
.s
.difference_update
, [[]])
494 self
.assertRaises(TypeError, self
.s
.symmetric_difference_update
, [[]])
495 for p
, q
in (('cdc', 'ab'), ('efgfe', 'abc'), ('ccb', 'a'), ('ef', 'abc')):
496 for C
in set, frozenset, dict.fromkeys
, str, unicode, list, tuple:
497 s
= self
.thetype('abcba')
498 self
.assertEqual(s
.difference_update(C(p
)), None)
499 self
.assertEqual(s
, set(q
))
501 s
= self
.thetype('abcdefghih')
502 s
.difference_update()
503 self
.assertEqual(s
, self
.thetype('abcdefghih'))
505 s
= self
.thetype('abcdefghih')
506 s
.difference_update(C('aba'))
507 self
.assertEqual(s
, self
.thetype('cdefghih'))
509 s
= self
.thetype('abcdefghih')
510 s
.difference_update(C('cdc'), C('aba'))
511 self
.assertEqual(s
, self
.thetype('efghih'))
514 self
.s
-= set(self
.otherword
)
515 for c
in (self
.word
+ self
.otherword
):
516 if c
in self
.word
and c
not in self
.otherword
:
517 self
.assertIn(c
, self
.s
)
519 self
.assertNotIn(c
, self
.s
)
521 def test_symmetric_difference_update(self
):
522 retval
= self
.s
.symmetric_difference_update(self
.otherword
)
523 self
.assertEqual(retval
, None)
524 for c
in (self
.word
+ self
.otherword
):
525 if (c
in self
.word
) ^
(c
in self
.otherword
):
526 self
.assertIn(c
, self
.s
)
528 self
.assertNotIn(c
, self
.s
)
529 self
.assertRaises(PassThru
, self
.s
.symmetric_difference_update
, check_pass_thru())
530 self
.assertRaises(TypeError, self
.s
.symmetric_difference_update
, [[]])
531 for p
, q
in (('cdc', 'abd'), ('efgfe', 'abcefg'), ('ccb', 'a'), ('ef', 'abcef')):
532 for C
in set, frozenset, dict.fromkeys
, str, unicode, list, tuple:
533 s
= self
.thetype('abcba')
534 self
.assertEqual(s
.symmetric_difference_update(C(p
)), None)
535 self
.assertEqual(s
, set(q
))
538 self
.s ^
= set(self
.otherword
)
539 for c
in (self
.word
+ self
.otherword
):
540 if (c
in self
.word
) ^
(c
in self
.otherword
):
541 self
.assertIn(c
, self
.s
)
543 self
.assertNotIn(c
, self
.s
)
545 def test_inplace_on_self(self
):
548 self
.assertEqual(t
, self
.s
)
550 self
.assertEqual(t
, self
.s
)
552 self
.assertEqual(t
, self
.thetype())
555 self
.assertEqual(t
, self
.thetype())
557 def test_weakref(self
):
558 s
= self
.thetype('gallahad')
560 self
.assertEqual(str(p
), str(s
))
562 self
.assertRaises(ReferenceError, str, p
)
564 # C API test only available in a debug build
565 if hasattr(set, "test_c_api"):
566 def test_c_api(self
):
567 self
.assertEqual(set().test_c_api(), True)
569 class SetSubclass(set):
572 class TestSetSubclass(TestSet
):
573 thetype
= SetSubclass
575 class SetSubclassWithKeywordArgs(set):
576 def __init__(self
, iterable
=[], newarg
=None):
577 set.__init
__(self
, iterable
)
579 class TestSetSubclassWithKeywordArgs(TestSet
):
581 def test_keywords_in_subclass(self
):
582 'SF bug #1486663 -- this used to erroneously raise a TypeError'
583 SetSubclassWithKeywordArgs(newarg
=1)
585 class TestFrozenSet(TestJointOps
):
589 s
= self
.thetype(self
.word
)
590 s
.__init
__(self
.otherword
)
591 self
.assertEqual(s
, set(self
.word
))
593 def test_singleton_empty_frozenset(self
):
595 efs
= [frozenset(), frozenset([]), frozenset(()), frozenset(''),
596 frozenset(), frozenset([]), frozenset(()), frozenset(''),
597 frozenset(xrange(0)), frozenset(frozenset()),
599 # All of the empty frozensets should have just one id()
600 self
.assertEqual(len(set(map(id, efs
))), 1)
602 def test_constructor_identity(self
):
603 s
= self
.thetype(range(3))
605 self
.assertEqual(id(s
), id(t
))
608 self
.assertEqual(hash(self
.thetype('abcdeb')),
609 hash(self
.thetype('ebecda')))
611 # make sure that all permutations give the same hash value
613 seq
= [randrange(n
) for i
in xrange(n
)]
615 for i
in xrange(200):
617 results
.add(hash(self
.thetype(seq
)))
618 self
.assertEqual(len(results
), 1)
622 self
.assertEqual(id(self
.s
), id(dup
))
624 def test_frozen_as_dictkey(self
):
625 seq
= range(10) + list('abcdefg') + ['apple']
626 key1
= self
.thetype(seq
)
627 key2
= self
.thetype(reversed(seq
))
628 self
.assertEqual(key1
, key2
)
629 self
.assertNotEqual(id(key1
), id(key2
))
632 self
.assertEqual(d
[key2
], 42)
634 def test_hash_caching(self
):
635 f
= self
.thetype('abcdcda')
636 self
.assertEqual(hash(f
), hash(f
))
638 def test_hash_effectiveness(self
):
641 addhashvalue
= hashvalues
.add
642 elemmasks
= [(i
+1, 1<<i
) for i
in range(n
)]
643 for i
in xrange(2**n
):
644 addhashvalue(hash(frozenset([e
for e
, m
in elemmasks
if m
&i
])))
645 self
.assertEqual(len(hashvalues
), 2**n
)
647 class FrozenSetSubclass(frozenset):
650 class TestFrozenSetSubclass(TestFrozenSet
):
651 thetype
= FrozenSetSubclass
653 def test_constructor_identity(self
):
654 s
= self
.thetype(range(3))
656 self
.assertNotEqual(id(s
), id(t
))
660 self
.assertNotEqual(id(self
.s
), id(dup
))
662 def test_nested_empty_constructor(self
):
665 self
.assertEqual(s
, t
)
667 def test_singleton_empty_frozenset(self
):
668 Frozenset
= self
.thetype
671 efs
= [Frozenset(), Frozenset([]), Frozenset(()), Frozenset(''),
672 Frozenset(), Frozenset([]), Frozenset(()), Frozenset(''),
673 Frozenset(xrange(0)), Frozenset(Frozenset()),
674 Frozenset(frozenset()), f
, F
, Frozenset(f
), Frozenset(F
)]
675 # All empty frozenset subclass instances should have different ids
676 self
.assertEqual(len(set(map(id, efs
))), len(efs
))
678 # Tests taken from test_sets.py =============================================
682 #==============================================================================
684 class TestBasicOps(unittest
.TestCase
):
687 if self
.repr is not None:
688 self
.assertEqual(repr(self
.set), self
.repr)
690 def test_print(self
):
691 fo
= open(test_support
.TESTFN
, "wb")
693 print >> fo
, self
.set,
695 fo
= open(test_support
.TESTFN
, "rb")
696 self
.assertEqual(fo
.read(), repr(self
.set))
699 test_support
.unlink(test_support
.TESTFN
)
701 def test_length(self
):
702 self
.assertEqual(len(self
.set), self
.length
)
704 def test_self_equality(self
):
705 self
.assertEqual(self
.set, self
.set)
707 def test_equivalent_equality(self
):
708 self
.assertEqual(self
.set, self
.dup
)
711 self
.assertEqual(self
.set.copy(), self
.dup
)
713 def test_self_union(self
):
714 result
= self
.set | self
.set
715 self
.assertEqual(result
, self
.dup
)
717 def test_empty_union(self
):
718 result
= self
.set | empty_set
719 self
.assertEqual(result
, self
.dup
)
721 def test_union_empty(self
):
722 result
= empty_set | self
.set
723 self
.assertEqual(result
, self
.dup
)
725 def test_self_intersection(self
):
726 result
= self
.set & self
.set
727 self
.assertEqual(result
, self
.dup
)
729 def test_empty_intersection(self
):
730 result
= self
.set & empty_set
731 self
.assertEqual(result
, empty_set
)
733 def test_intersection_empty(self
):
734 result
= empty_set
& self
.set
735 self
.assertEqual(result
, empty_set
)
737 def test_self_isdisjoint(self
):
738 result
= self
.set.isdisjoint(self
.set)
739 self
.assertEqual(result
, not self
.set)
741 def test_empty_isdisjoint(self
):
742 result
= self
.set.isdisjoint(empty_set
)
743 self
.assertEqual(result
, True)
745 def test_isdisjoint_empty(self
):
746 result
= empty_set
.isdisjoint(self
.set)
747 self
.assertEqual(result
, True)
749 def test_self_symmetric_difference(self
):
750 result
= self
.set ^ self
.set
751 self
.assertEqual(result
, empty_set
)
753 def checkempty_symmetric_difference(self
):
754 result
= self
.set ^ empty_set
755 self
.assertEqual(result
, self
.set)
757 def test_self_difference(self
):
758 result
= self
.set - self
.set
759 self
.assertEqual(result
, empty_set
)
761 def test_empty_difference(self
):
762 result
= self
.set - empty_set
763 self
.assertEqual(result
, self
.dup
)
765 def test_empty_difference_rev(self
):
766 result
= empty_set
- self
.set
767 self
.assertEqual(result
, empty_set
)
769 def test_iteration(self
):
771 self
.assertIn(v
, self
.values
)
772 setiter
= iter(self
.set)
773 # note: __length_hint__ is an internal undocumented API,
774 # don't rely on it in your own programs
775 self
.assertEqual(setiter
.__length
_hint
__(), len(self
.set))
777 def test_pickling(self
):
778 p
= pickle
.dumps(self
.set)
779 copy
= pickle
.loads(p
)
780 self
.assertEqual(self
.set, copy
,
781 "%s != %s" % (self
.set, copy
))
783 #------------------------------------------------------------------------------
785 class TestBasicOpsEmpty(TestBasicOps
):
787 self
.case
= "empty set"
789 self
.set = set(self
.values
)
790 self
.dup
= set(self
.values
)
792 self
.repr = "set([])"
794 #------------------------------------------------------------------------------
796 class TestBasicOpsSingleton(TestBasicOps
):
798 self
.case
= "unit set (number)"
800 self
.set = set(self
.values
)
801 self
.dup
= set(self
.values
)
803 self
.repr = "set([3])"
806 self
.assertIn(3, self
.set)
808 def test_not_in(self
):
809 self
.assertNotIn(2, self
.set)
811 #------------------------------------------------------------------------------
813 class TestBasicOpsTuple(TestBasicOps
):
815 self
.case
= "unit set (tuple)"
816 self
.values
= [(0, "zero")]
817 self
.set = set(self
.values
)
818 self
.dup
= set(self
.values
)
820 self
.repr = "set([(0, 'zero')])"
823 self
.assertIn((0, "zero"), self
.set)
825 def test_not_in(self
):
826 self
.assertNotIn(9, self
.set)
828 #------------------------------------------------------------------------------
830 class TestBasicOpsTriple(TestBasicOps
):
832 self
.case
= "triple set"
833 self
.values
= [0, "zero", operator
.add
]
834 self
.set = set(self
.values
)
835 self
.dup
= set(self
.values
)
839 #==============================================================================
848 class TestExceptionPropagation(unittest
.TestCase
):
849 """SF 628246: Set constructor should not trap iterator TypeErrors"""
851 def test_instanceWithException(self
):
852 self
.assertRaises(TypeError, set, baditer())
854 def test_instancesWithoutException(self
):
855 # All of these iterables should load without exception.
858 set({'one':1, 'two':2, 'three':3})
863 def test_changingSizeWhileIterating(self
):
871 self
.fail("no exception when changing size during iteration")
873 #==============================================================================
875 class TestSetOfSets(unittest
.TestCase
):
876 def test_constructor(self
):
877 inner
= frozenset([1])
879 element
= outer
.pop()
880 self
.assertEqual(type(element
), frozenset)
881 outer
.add(inner
) # Rebuild set of sets with .add method
883 self
.assertEqual(outer
, set()) # Verify that remove worked
884 outer
.discard(inner
) # Absence of KeyError indicates working fine
886 #==============================================================================
888 class TestBinaryOps(unittest
.TestCase
):
890 self
.set = set((2, 4, 6))
892 def test_eq(self
): # SF bug 643115
893 self
.assertEqual(self
.set, set({2:1,4:3,6:5}))
895 def test_union_subset(self
):
896 result
= self
.set |
set([2])
897 self
.assertEqual(result
, set((2, 4, 6)))
899 def test_union_superset(self
):
900 result
= self
.set |
set([2, 4, 6, 8])
901 self
.assertEqual(result
, set([2, 4, 6, 8]))
903 def test_union_overlap(self
):
904 result
= self
.set |
set([3, 4, 5])
905 self
.assertEqual(result
, set([2, 3, 4, 5, 6]))
907 def test_union_non_overlap(self
):
908 result
= self
.set |
set([8])
909 self
.assertEqual(result
, set([2, 4, 6, 8]))
911 def test_intersection_subset(self
):
912 result
= self
.set & set((2, 4))
913 self
.assertEqual(result
, set((2, 4)))
915 def test_intersection_superset(self
):
916 result
= self
.set & set([2, 4, 6, 8])
917 self
.assertEqual(result
, set([2, 4, 6]))
919 def test_intersection_overlap(self
):
920 result
= self
.set & set([3, 4, 5])
921 self
.assertEqual(result
, set([4]))
923 def test_intersection_non_overlap(self
):
924 result
= self
.set & set([8])
925 self
.assertEqual(result
, empty_set
)
927 def test_isdisjoint_subset(self
):
928 result
= self
.set.isdisjoint(set((2, 4)))
929 self
.assertEqual(result
, False)
931 def test_isdisjoint_superset(self
):
932 result
= self
.set.isdisjoint(set([2, 4, 6, 8]))
933 self
.assertEqual(result
, False)
935 def test_isdisjoint_overlap(self
):
936 result
= self
.set.isdisjoint(set([3, 4, 5]))
937 self
.assertEqual(result
, False)
939 def test_isdisjoint_non_overlap(self
):
940 result
= self
.set.isdisjoint(set([8]))
941 self
.assertEqual(result
, True)
943 def test_sym_difference_subset(self
):
944 result
= self
.set ^
set((2, 4))
945 self
.assertEqual(result
, set([6]))
947 def test_sym_difference_superset(self
):
948 result
= self
.set ^
set((2, 4, 6, 8))
949 self
.assertEqual(result
, set([8]))
951 def test_sym_difference_overlap(self
):
952 result
= self
.set ^
set((3, 4, 5))
953 self
.assertEqual(result
, set([2, 3, 5, 6]))
955 def test_sym_difference_non_overlap(self
):
956 result
= self
.set ^
set([8])
957 self
.assertEqual(result
, set([2, 4, 6, 8]))
960 a
, b
= set('a'), set('b')
961 self
.assertRaises(TypeError, cmp, a
, b
)
963 # You can view this as a buglet: cmp(a, a) does not raise TypeError,
964 # because __eq__ is tried before __cmp__, and a.__eq__(a) returns True,
965 # which Python thinks is good enough to synthesize a cmp() result
966 # without calling __cmp__.
967 self
.assertEqual(cmp(a
, a
), 0)
969 self
.assertRaises(TypeError, cmp, a
, 12)
970 self
.assertRaises(TypeError, cmp, "abc", a
)
972 #==============================================================================
974 class TestUpdateOps(unittest
.TestCase
):
976 self
.set = set((2, 4, 6))
978 def test_union_subset(self
):
980 self
.assertEqual(self
.set, set((2, 4, 6)))
982 def test_union_superset(self
):
983 self
.set |
= set([2, 4, 6, 8])
984 self
.assertEqual(self
.set, set([2, 4, 6, 8]))
986 def test_union_overlap(self
):
987 self
.set |
= set([3, 4, 5])
988 self
.assertEqual(self
.set, set([2, 3, 4, 5, 6]))
990 def test_union_non_overlap(self
):
992 self
.assertEqual(self
.set, set([2, 4, 6, 8]))
994 def test_union_method_call(self
):
995 self
.set.update(set([3, 4, 5]))
996 self
.assertEqual(self
.set, set([2, 3, 4, 5, 6]))
998 def test_intersection_subset(self
):
999 self
.set &= set((2, 4))
1000 self
.assertEqual(self
.set, set((2, 4)))
1002 def test_intersection_superset(self
):
1003 self
.set &= set([2, 4, 6, 8])
1004 self
.assertEqual(self
.set, set([2, 4, 6]))
1006 def test_intersection_overlap(self
):
1007 self
.set &= set([3, 4, 5])
1008 self
.assertEqual(self
.set, set([4]))
1010 def test_intersection_non_overlap(self
):
1011 self
.set &= set([8])
1012 self
.assertEqual(self
.set, empty_set
)
1014 def test_intersection_method_call(self
):
1015 self
.set.intersection_update(set([3, 4, 5]))
1016 self
.assertEqual(self
.set, set([4]))
1018 def test_sym_difference_subset(self
):
1019 self
.set ^
= set((2, 4))
1020 self
.assertEqual(self
.set, set([6]))
1022 def test_sym_difference_superset(self
):
1023 self
.set ^
= set((2, 4, 6, 8))
1024 self
.assertEqual(self
.set, set([8]))
1026 def test_sym_difference_overlap(self
):
1027 self
.set ^
= set((3, 4, 5))
1028 self
.assertEqual(self
.set, set([2, 3, 5, 6]))
1030 def test_sym_difference_non_overlap(self
):
1031 self
.set ^
= set([8])
1032 self
.assertEqual(self
.set, set([2, 4, 6, 8]))
1034 def test_sym_difference_method_call(self
):
1035 self
.set.symmetric_difference_update(set([3, 4, 5]))
1036 self
.assertEqual(self
.set, set([2, 3, 5, 6]))
1038 def test_difference_subset(self
):
1039 self
.set -= set((2, 4))
1040 self
.assertEqual(self
.set, set([6]))
1042 def test_difference_superset(self
):
1043 self
.set -= set((2, 4, 6, 8))
1044 self
.assertEqual(self
.set, set([]))
1046 def test_difference_overlap(self
):
1047 self
.set -= set((3, 4, 5))
1048 self
.assertEqual(self
.set, set([2, 6]))
1050 def test_difference_non_overlap(self
):
1051 self
.set -= set([8])
1052 self
.assertEqual(self
.set, set([2, 4, 6]))
1054 def test_difference_method_call(self
):
1055 self
.set.difference_update(set([3, 4, 5]))
1056 self
.assertEqual(self
.set, set([2, 6]))
1058 #==============================================================================
1060 class TestMutate(unittest
.TestCase
):
1062 self
.values
= ["a", "b", "c"]
1063 self
.set = set(self
.values
)
1065 def test_add_present(self
):
1067 self
.assertEqual(self
.set, set("abc"))
1069 def test_add_absent(self
):
1071 self
.assertEqual(self
.set, set("abcd"))
1073 def test_add_until_full(self
):
1076 for v
in self
.values
:
1079 self
.assertEqual(len(tmp
), expected_len
)
1080 self
.assertEqual(tmp
, self
.set)
1082 def test_remove_present(self
):
1083 self
.set.remove("b")
1084 self
.assertEqual(self
.set, set("ac"))
1086 def test_remove_absent(self
):
1088 self
.set.remove("d")
1089 self
.fail("Removing missing element should have raised LookupError")
1093 def test_remove_until_empty(self
):
1094 expected_len
= len(self
.set)
1095 for v
in self
.values
:
1098 self
.assertEqual(len(self
.set), expected_len
)
1100 def test_discard_present(self
):
1101 self
.set.discard("c")
1102 self
.assertEqual(self
.set, set("ab"))
1104 def test_discard_absent(self
):
1105 self
.set.discard("d")
1106 self
.assertEqual(self
.set, set("abc"))
1108 def test_clear(self
):
1110 self
.assertEqual(len(self
.set), 0)
1115 popped
[self
.set.pop()] = None
1116 self
.assertEqual(len(popped
), len(self
.values
))
1117 for v
in self
.values
:
1118 self
.assertIn(v
, popped
)
1120 def test_update_empty_tuple(self
):
1122 self
.assertEqual(self
.set, set(self
.values
))
1124 def test_update_unit_tuple_overlap(self
):
1125 self
.set.update(("a",))
1126 self
.assertEqual(self
.set, set(self
.values
))
1128 def test_update_unit_tuple_non_overlap(self
):
1129 self
.set.update(("a", "z"))
1130 self
.assertEqual(self
.set, set(self
.values
+ ["z"]))
1132 #==============================================================================
1134 class TestSubsets(unittest
.TestCase
):
1136 case2method
= {"<=": "issubset",
1140 reverse
= {"==": "==",
1148 def test_issubset(self
):
1151 for case
in "!=", "==", "<", "<=", ">", ">=":
1152 expected
= case
in self
.cases
1153 # Test the binary infix spelling.
1154 result
= eval("x" + case
+ "y", locals())
1155 self
.assertEqual(result
, expected
)
1156 # Test the "friendly" method-name spelling, if one exists.
1157 if case
in TestSubsets
.case2method
:
1158 method
= getattr(x
, TestSubsets
.case2method
[case
])
1160 self
.assertEqual(result
, expected
)
1162 # Now do the same for the operands reversed.
1163 rcase
= TestSubsets
.reverse
[case
]
1164 result
= eval("y" + rcase
+ "x", locals())
1165 self
.assertEqual(result
, expected
)
1166 if rcase
in TestSubsets
.case2method
:
1167 method
= getattr(y
, TestSubsets
.case2method
[rcase
])
1169 self
.assertEqual(result
, expected
)
1170 #------------------------------------------------------------------------------
1172 class TestSubsetEqualEmpty(TestSubsets
):
1176 cases
= "==", "<=", ">="
1178 #------------------------------------------------------------------------------
1180 class TestSubsetEqualNonEmpty(TestSubsets
):
1184 cases
= "==", "<=", ">="
1186 #------------------------------------------------------------------------------
1188 class TestSubsetEmptyNonEmpty(TestSubsets
):
1191 name
= "one empty, one non-empty"
1192 cases
= "!=", "<", "<="
1194 #------------------------------------------------------------------------------
1196 class TestSubsetPartial(TestSubsets
):
1199 name
= "one a non-empty proper subset of other"
1200 cases
= "!=", "<", "<="
1202 #------------------------------------------------------------------------------
1204 class TestSubsetNonOverlap(TestSubsets
):
1207 name
= "neither empty, neither contains"
1210 #==============================================================================
1212 class TestOnlySetsInBinaryOps(unittest
.TestCase
):
1214 def test_eq_ne(self
):
1215 # Unlike the others, this is testing that == and != *are* allowed.
1216 self
.assertEqual(self
.other
== self
.set, False)
1217 self
.assertEqual(self
.set == self
.other
, False)
1218 self
.assertEqual(self
.other
!= self
.set, True)
1219 self
.assertEqual(self
.set != self
.other
, True)
1221 def test_ge_gt_le_lt(self
):
1222 self
.assertRaises(TypeError, lambda: self
.set < self
.other
)
1223 self
.assertRaises(TypeError, lambda: self
.set <= self
.other
)
1224 self
.assertRaises(TypeError, lambda: self
.set > self
.other
)
1225 self
.assertRaises(TypeError, lambda: self
.set >= self
.other
)
1227 self
.assertRaises(TypeError, lambda: self
.other
< self
.set)
1228 self
.assertRaises(TypeError, lambda: self
.other
<= self
.set)
1229 self
.assertRaises(TypeError, lambda: self
.other
> self
.set)
1230 self
.assertRaises(TypeError, lambda: self
.other
>= self
.set)
1232 def test_update_operator(self
):
1234 self
.set |
= self
.other
1238 self
.fail("expected TypeError")
1240 def test_update(self
):
1241 if self
.otherIsIterable
:
1242 self
.set.update(self
.other
)
1244 self
.assertRaises(TypeError, self
.set.update
, self
.other
)
1246 def test_union(self
):
1247 self
.assertRaises(TypeError, lambda: self
.set | self
.other
)
1248 self
.assertRaises(TypeError, lambda: self
.other | self
.set)
1249 if self
.otherIsIterable
:
1250 self
.set.union(self
.other
)
1252 self
.assertRaises(TypeError, self
.set.union
, self
.other
)
1254 def test_intersection_update_operator(self
):
1256 self
.set &= self
.other
1260 self
.fail("expected TypeError")
1262 def test_intersection_update(self
):
1263 if self
.otherIsIterable
:
1264 self
.set.intersection_update(self
.other
)
1266 self
.assertRaises(TypeError,
1267 self
.set.intersection_update
,
1270 def test_intersection(self
):
1271 self
.assertRaises(TypeError, lambda: self
.set & self
.other
)
1272 self
.assertRaises(TypeError, lambda: self
.other
& self
.set)
1273 if self
.otherIsIterable
:
1274 self
.set.intersection(self
.other
)
1276 self
.assertRaises(TypeError, self
.set.intersection
, self
.other
)
1278 def test_sym_difference_update_operator(self
):
1280 self
.set ^
= self
.other
1284 self
.fail("expected TypeError")
1286 def test_sym_difference_update(self
):
1287 if self
.otherIsIterable
:
1288 self
.set.symmetric_difference_update(self
.other
)
1290 self
.assertRaises(TypeError,
1291 self
.set.symmetric_difference_update
,
1294 def test_sym_difference(self
):
1295 self
.assertRaises(TypeError, lambda: self
.set ^ self
.other
)
1296 self
.assertRaises(TypeError, lambda: self
.other ^ self
.set)
1297 if self
.otherIsIterable
:
1298 self
.set.symmetric_difference(self
.other
)
1300 self
.assertRaises(TypeError, self
.set.symmetric_difference
, self
.other
)
1302 def test_difference_update_operator(self
):
1304 self
.set -= self
.other
1308 self
.fail("expected TypeError")
1310 def test_difference_update(self
):
1311 if self
.otherIsIterable
:
1312 self
.set.difference_update(self
.other
)
1314 self
.assertRaises(TypeError,
1315 self
.set.difference_update
,
1318 def test_difference(self
):
1319 self
.assertRaises(TypeError, lambda: self
.set - self
.other
)
1320 self
.assertRaises(TypeError, lambda: self
.other
- self
.set)
1321 if self
.otherIsIterable
:
1322 self
.set.difference(self
.other
)
1324 self
.assertRaises(TypeError, self
.set.difference
, self
.other
)
1326 #------------------------------------------------------------------------------
1328 class TestOnlySetsNumeric(TestOnlySetsInBinaryOps
):
1330 self
.set = set((1, 2, 3))
1332 self
.otherIsIterable
= False
1334 #------------------------------------------------------------------------------
1336 class TestOnlySetsDict(TestOnlySetsInBinaryOps
):
1338 self
.set = set((1, 2, 3))
1339 self
.other
= {1:2, 3:4}
1340 self
.otherIsIterable
= True
1342 #------------------------------------------------------------------------------
1344 class TestOnlySetsOperator(TestOnlySetsInBinaryOps
):
1346 self
.set = set((1, 2, 3))
1347 self
.other
= operator
.add
1348 self
.otherIsIterable
= False
1350 def test_ge_gt_le_lt(self
):
1351 with test_support
.check_py3k_warnings():
1352 super(TestOnlySetsOperator
, self
).test_ge_gt_le_lt()
1354 #------------------------------------------------------------------------------
1356 class TestOnlySetsTuple(TestOnlySetsInBinaryOps
):
1358 self
.set = set((1, 2, 3))
1359 self
.other
= (2, 4, 6)
1360 self
.otherIsIterable
= True
1362 #------------------------------------------------------------------------------
1364 class TestOnlySetsString(TestOnlySetsInBinaryOps
):
1366 self
.set = set((1, 2, 3))
1368 self
.otherIsIterable
= True
1370 #------------------------------------------------------------------------------
1372 class TestOnlySetsGenerator(TestOnlySetsInBinaryOps
):
1375 for i
in xrange(0, 10, 2):
1377 self
.set = set((1, 2, 3))
1379 self
.otherIsIterable
= True
1381 #==============================================================================
1383 class TestCopying(unittest
.TestCase
):
1385 def test_copy(self
):
1386 dup
= list(self
.set.copy())
1387 self
.assertEqual(len(dup
), len(self
.set))
1389 self
.assertIn(el
, dup
)
1391 self
.assertIs(el
, dup
.pop(pos
))
1392 self
.assertFalse(dup
)
1394 def test_deep_copy(self
):
1395 dup
= copy
.deepcopy(self
.set)
1396 self
.assertSetEqual(dup
, self
.set)
1398 #------------------------------------------------------------------------------
1400 class TestCopyingEmpty(TestCopying
):
1404 #------------------------------------------------------------------------------
1406 class TestCopyingSingleton(TestCopying
):
1408 self
.set = set(["hello"])
1410 #------------------------------------------------------------------------------
1412 class TestCopyingTriple(TestCopying
):
1414 self
.set = set(["zero", 0, None])
1416 #------------------------------------------------------------------------------
1418 class TestCopyingTuple(TestCopying
):
1420 self
.set = set([(1, 2)])
1422 #------------------------------------------------------------------------------
1424 class TestCopyingNested(TestCopying
):
1426 self
.set = set([((1, 2), (3, 4))])
1428 #==============================================================================
1430 class TestIdentities(unittest
.TestCase
):
1432 self
.a
= set('abracadabra')
1433 self
.b
= set('alacazam')
1435 def test_binopsVsSubsets(self
):
1436 a
, b
= self
.a
, self
.b
1437 self
.assertTrue(a
- b
< a
)
1438 self
.assertTrue(b
- a
< b
)
1439 self
.assertTrue(a
& b
< a
)
1440 self
.assertTrue(a
& b
< b
)
1441 self
.assertTrue(a | b
> a
)
1442 self
.assertTrue(a | b
> b
)
1443 self
.assertTrue(a ^ b
< a | b
)
1445 def test_commutativity(self
):
1446 a
, b
= self
.a
, self
.b
1447 self
.assertEqual(a
&b
, b
&a
)
1448 self
.assertEqual(a|b
, b|a
)
1449 self
.assertEqual(a^b
, b^a
)
1451 self
.assertNotEqual(a
-b
, b
-a
)
1453 def test_summations(self
):
1454 # check that sums of parts equal the whole
1455 a
, b
= self
.a
, self
.b
1456 self
.assertEqual((a
-b
)|
(a
&b
)|
(b
-a
), a|b
)
1457 self
.assertEqual((a
&b
)|
(a^b
), a|b
)
1458 self
.assertEqual(a|
(b
-a
), a|b
)
1459 self
.assertEqual((a
-b
)|b
, a|b
)
1460 self
.assertEqual((a
-b
)|
(a
&b
), a
)
1461 self
.assertEqual((b
-a
)|
(a
&b
), b
)
1462 self
.assertEqual((a
-b
)|
(b
-a
), a^b
)
1464 def test_exclusion(self
):
1465 # check that inverse operations show non-overlap
1466 a
, b
, zero
= self
.a
, self
.b
, set()
1467 self
.assertEqual((a
-b
)&b
, zero
)
1468 self
.assertEqual((b
-a
)&a
, zero
)
1469 self
.assertEqual((a
&b
)&(a^b
), zero
)
1471 # Tests derived from test_itertools.py =======================================
1479 'Sequence using __getitem__'
1480 def __init__(self
, seqn
):
1482 def __getitem__(self
, i
):
1486 'Sequence using iterator protocol'
1487 def __init__(self
, seqn
):
1493 if self
.i
>= len(self
.seqn
): raise StopIteration
1494 v
= self
.seqn
[self
.i
]
1499 'Sequence using iterator protocol defined with a generator'
1500 def __init__(self
, seqn
):
1504 for val
in self
.seqn
:
1508 'Missing __getitem__ and __iter__'
1509 def __init__(self
, seqn
):
1513 if self
.i
>= len(self
.seqn
): raise StopIteration
1514 v
= self
.seqn
[self
.i
]
1519 'Iterator missing next()'
1520 def __init__(self
, seqn
):
1527 'Test propagation of exceptions'
1528 def __init__(self
, seqn
):
1537 'Test immediate stop'
1538 def __init__(self
, seqn
):
1545 from itertools
import chain
, imap
1547 'Test multiple tiers of iterators'
1548 return chain(imap(lambda x
:x
, R(Ig(G(seqn
)))))
1550 class TestVariousIteratorArgs(unittest
.TestCase
):
1552 def test_constructor(self
):
1553 for cons
in (set, frozenset):
1554 for s
in ("123", "", range(1000), ('do', 1.2), xrange(2000,2200,5)):
1555 for g
in (G
, I
, Ig
, S
, L
, R
):
1556 self
.assertSetEqual(cons(g(s
)), set(g(s
)))
1557 self
.assertRaises(TypeError, cons
, X(s
))
1558 self
.assertRaises(TypeError, cons
, N(s
))
1559 self
.assertRaises(ZeroDivisionError, cons
, E(s
))
1561 def test_inline_methods(self
):
1563 for data
in ("123", "", range(1000), ('do', 1.2), xrange(2000,2200,5), 'december'):
1564 for meth
in (s
.union
, s
.intersection
, s
.difference
, s
.symmetric_difference
, s
.isdisjoint
):
1565 for g
in (G
, I
, Ig
, L
, R
):
1566 expected
= meth(data
)
1567 actual
= meth(G(data
))
1568 if isinstance(expected
, bool):
1569 self
.assertEqual(actual
, expected
)
1571 self
.assertSetEqual(actual
, expected
)
1572 self
.assertRaises(TypeError, meth
, X(s
))
1573 self
.assertRaises(TypeError, meth
, N(s
))
1574 self
.assertRaises(ZeroDivisionError, meth
, E(s
))
1576 def test_inplace_methods(self
):
1577 for data
in ("123", "", range(1000), ('do', 1.2), xrange(2000,2200,5), 'december'):
1578 for methname
in ('update', 'intersection_update',
1579 'difference_update', 'symmetric_difference_update'):
1580 for g
in (G
, I
, Ig
, S
, L
, R
):
1583 getattr(s
, methname
)(list(g(data
)))
1584 getattr(t
, methname
)(g(data
))
1585 self
.assertSetEqual(s
, t
)
1587 self
.assertRaises(TypeError, getattr(set('january'), methname
), X(data
))
1588 self
.assertRaises(TypeError, getattr(set('january'), methname
), N(data
))
1589 self
.assertRaises(ZeroDivisionError, getattr(set('january'), methname
), E(data
))
1591 # Application tests (based on David Eppstein's graph recipes ====================================
1594 """Generates all subsets of a set or sequence U."""
1597 x
= frozenset([U
.next()])
1598 for S
in powerset(U
):
1601 except StopIteration:
1605 """Graph of n-dimensional hypercube."""
1606 singletons
= [frozenset([x
]) for x
in range(n
)]
1607 return dict([(x
, frozenset([x^s
for s
in singletons
]))
1608 for x
in powerset(range(n
))])
1611 """Graph, the vertices of which are edges of G,
1612 with two vertices being adjacent iff the corresponding
1613 edges share a vertex."""
1617 nx
= [frozenset([x
,z
]) for z
in G
[x
] if z
!= y
]
1618 ny
= [frozenset([y
,z
]) for z
in G
[y
] if z
!= x
]
1619 L
[frozenset([x
,y
])] = frozenset(nx
+ny
)
1623 'Return a set of faces in G. Where a face is a set of vertices on that face'
1624 # currently limited to triangles,squares, and pentagons
1626 for v1
, edges
in G
.items():
1632 f
.add(frozenset([v1
, v2
, v3
]))
1638 f
.add(frozenset([v1
, v2
, v3
, v4
]))
1641 if v5
== v3
or v5
== v2
:
1644 f
.add(frozenset([v1
, v2
, v3
, v4
, v5
]))
1648 class TestGraphs(unittest
.TestCase
):
1650 def test_cube(self
):
1652 g
= cube(3) # vert --> {v1, v2, v3}
1654 self
.assertEqual(len(vertices1
), 8) # eight vertices
1655 for edge
in g
.values():
1656 self
.assertEqual(len(edge
), 3) # each vertex connects to three edges
1657 vertices2
= set(v
for edges
in g
.values() for v
in edges
)
1658 self
.assertEqual(vertices1
, vertices2
) # edge vertices in original set
1660 cubefaces
= faces(g
)
1661 self
.assertEqual(len(cubefaces
), 6) # six faces
1662 for face
in cubefaces
:
1663 self
.assertEqual(len(face
), 4) # each face is a square
1665 def test_cuboctahedron(self
):
1667 # http://en.wikipedia.org/wiki/Cuboctahedron
1668 # 8 triangular faces and 6 square faces
1669 # 12 indentical vertices each connecting a triangle and square
1672 cuboctahedron
= linegraph(g
) # V( --> {V1, V2, V3, V4}
1673 self
.assertEqual(len(cuboctahedron
), 12)# twelve vertices
1675 vertices
= set(cuboctahedron
)
1676 for edges
in cuboctahedron
.values():
1677 self
.assertEqual(len(edges
), 4) # each vertex connects to four other vertices
1678 othervertices
= set(edge
for edges
in cuboctahedron
.values() for edge
in edges
)
1679 self
.assertEqual(vertices
, othervertices
) # edge vertices in original set
1681 cubofaces
= faces(cuboctahedron
)
1682 facesizes
= collections
.defaultdict(int)
1683 for face
in cubofaces
:
1684 facesizes
[len(face
)] += 1
1685 self
.assertEqual(facesizes
[3], 8) # eight triangular faces
1686 self
.assertEqual(facesizes
[4], 6) # six square faces
1688 for vertex
in cuboctahedron
:
1689 edge
= vertex
# Cuboctahedron vertices are edges in Cube
1690 self
.assertEqual(len(edge
), 2) # Two cube vertices define an edge
1691 for cubevert
in edge
:
1692 self
.assertIn(cubevert
, g
)
1695 #==============================================================================
1697 def test_main(verbose
=None):
1701 TestSetSubclassWithKeywordArgs
,
1703 TestFrozenSetSubclass
,
1705 TestExceptionPropagation
,
1707 TestBasicOpsSingleton
,
1713 TestSubsetEqualEmpty
,
1714 TestSubsetEqualNonEmpty
,
1715 TestSubsetEmptyNonEmpty
,
1717 TestSubsetNonOverlap
,
1718 TestOnlySetsNumeric
,
1720 TestOnlySetsOperator
,
1723 TestOnlySetsGenerator
,
1725 TestCopyingSingleton
,
1730 TestVariousIteratorArgs
,
1734 test_support
.run_unittest(*test_classes
)
1736 # verify reference counting
1737 if verbose
and hasattr(sys
, "gettotalrefcount"):
1740 for i
in xrange(len(counts
)):
1741 test_support
.run_unittest(*test_classes
)
1743 counts
[i
] = sys
.gettotalrefcount()
1746 if __name__
== "__main__":
1747 test_main(verbose
=True)