annotate Lib/test/test_set.py @ 87690:66c1c9f32567 default tip

Add use_weakref option to functools.lru_cache The use_weakref option makes lru_cache create weak references to the function arguments when creating the cache key, which prevents them from staying in the memory forever even after all other references to them are gone. This is a proof of concept for http://bugs.python.org/issue19859
author Radomir Dopieralski <devel@sheep.art.pl>
date Tue, 03 Dec 2013 16:02:48 +0100
parents a699550bc73b
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1 import unittest
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 46084
diff changeset
2 from test import support
50553
badb471dbaeb Merged revisions 68132 via svnmerge from
Georg Brandl <georg@python.org>
parents: 50540
diff changeset
3 import gc
badb471dbaeb Merged revisions 68132 via svnmerge from
Georg Brandl <georg@python.org>
parents: 50540
diff changeset
4 import weakref
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
5 import operator
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
6 import copy
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
7 import pickle
35206
89710805bc3d Add test for hash commutativity.
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
8 from random import randrange, shuffle
35348
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35334
diff changeset
9 import sys
44252
444da08f78a4 Fixed issue #1564: The set implementation should special-case PyUnicode instead of PyString
Christian Heimes <christian@cheimes.de>
parents: 43815
diff changeset
10 import warnings
44943
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
11 import collections
76091
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75073
diff changeset
12 import collections.abc
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
13
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
14 class PassThru(Exception):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
15 pass
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
16
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
17 def check_pass_thru():
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
18 raise PassThru
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
19 yield 1
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
20
35581
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
21 class BadCmp:
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
22 def __hash__(self):
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
23 return 1
39644
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
24 def __eq__(self, other):
35581
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
25 raise RuntimeError
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
26
40582
67287ca4e1e8 Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40450
diff changeset
27 class ReprWrapper:
67287ca4e1e8 Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40450
diff changeset
28 'Used to test self-referential repr() calls'
67287ca4e1e8 Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40450
diff changeset
29 def __repr__(self):
67287ca4e1e8 Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40450
diff changeset
30 return repr(self.value)
67287ca4e1e8 Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40450
diff changeset
31
40893
32c4897b5d46 Merged revisions 53623-53858 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40789
diff changeset
32 class HashCountingInt(int):
32c4897b5d46 Merged revisions 53623-53858 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40789
diff changeset
33 'int-like object that counts the number of times __hash__ is called'
32c4897b5d46 Merged revisions 53623-53858 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40789
diff changeset
34 def __init__(self, *args):
32c4897b5d46 Merged revisions 53623-53858 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40789
diff changeset
35 self.hash_count = 0
32c4897b5d46 Merged revisions 53623-53858 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40789
diff changeset
36 def __hash__(self):
32c4897b5d46 Merged revisions 53623-53858 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40789
diff changeset
37 self.hash_count += 1
32c4897b5d46 Merged revisions 53623-53858 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40789
diff changeset
38 return int.__hash__(self)
32c4897b5d46 Merged revisions 53623-53858 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40789
diff changeset
39
83449
ef100be2e8ba #17790: test_set now works with unittest test discovery. Patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 76091
diff changeset
40 class TestJointOps:
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
41 # Tests common to both set and frozenset
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
42
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
43 def setUp(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
44 self.word = word = 'simsalabim'
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
45 self.otherword = 'madagascar'
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
46 self.letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
47 self.s = self.thetype(word)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
48 self.d = dict.fromkeys(word)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
49
33605
b3408a85c89c Use Py_CLEAR(). Add unrelated test.
Raymond Hettinger <python@rcn.com>
parents: 33597
diff changeset
50 def test_new_or_init(self):
b3408a85c89c Use Py_CLEAR(). Add unrelated test.
Raymond Hettinger <python@rcn.com>
parents: 33597
diff changeset
51 self.assertRaises(TypeError, self.thetype, [], 2)
60711
aa707f13c402 Issue 8436: set.__init__ accepts keyword args
Raymond Hettinger <python@rcn.com>
parents: 55593
diff changeset
52 self.assertRaises(TypeError, set().__init__, a=1)
33605
b3408a85c89c Use Py_CLEAR(). Add unrelated test.
Raymond Hettinger <python@rcn.com>
parents: 33597
diff changeset
53
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
54 def test_uniquification(self):
31210
c06b570adf12 Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents: 31008
diff changeset
55 actual = sorted(self.s)
c06b570adf12 Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents: 31008
diff changeset
56 expected = sorted(self.d)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
57 self.assertEqual(actual, expected)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
58 self.assertRaises(PassThru, self.thetype, check_pass_thru())
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
59 self.assertRaises(TypeError, self.thetype, [[]])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
60
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
61 def test_len(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
62 self.assertEqual(len(self.s), len(self.d))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
63
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
64 def test_contains(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
65 for c in self.letters:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
66 self.assertEqual(c in self.s, c in self.d)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
67 self.assertRaises(TypeError, self.s.__contains__, [[]])
30995
d4826e097a3f Allow temporary hashability for the __contains__ test.
Raymond Hettinger <python@rcn.com>
parents: 30994
diff changeset
68 s = self.thetype([frozenset(self.letters)])
58415
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 55466
diff changeset
69 self.assertIn(self.thetype(self.letters), s)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
70
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
71 def test_union(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
72 u = self.s.union(self.otherword)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
73 for c in self.letters:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
74 self.assertEqual(c in u, c in self.d or c in self.otherword)
31004
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
75 self.assertEqual(self.s, self.thetype(self.word))
49730
c707da343557 Issue #1721812: Binary operations and copy operations on set/frozenset
Raymond Hettinger <python@rcn.com>
parents: 49518
diff changeset
76 self.assertEqual(type(u), self.basetype)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
77 self.assertRaises(PassThru, self.s.union, check_pass_thru())
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
78 self.assertRaises(TypeError, self.s.union, [[]])
43604
b18f6092f6e0 Patch 1280, by Alexandre Vassalotti.
Guido van Rossum <guido@python.org>
parents: 42756
diff changeset
79 for C in set, frozenset, dict.fromkeys, str, list, tuple:
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
80 self.assertEqual(self.thetype('abcba').union(C('cdc')), set('abcd'))
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
81 self.assertEqual(self.thetype('abcba').union(C('efgfe')), set('abcefg'))
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
82 self.assertEqual(self.thetype('abcba').union(C('ccb')), set('abc'))
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
83 self.assertEqual(self.thetype('abcba').union(C('ef')), set('abcef'))
47562
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47201
diff changeset
84 self.assertEqual(self.thetype('abcba').union(C('ef'), C('fg')), set('abcefg'))
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
85
55467
b34cae786cb0 Issue 6573: Fix set.union() for cases where self is in the argument chain.
Raymond Hettinger <python@rcn.com>
parents: 51122
diff changeset
86 # Issue #6573
b34cae786cb0 Issue 6573: Fix set.union() for cases where self is in the argument chain.
Raymond Hettinger <python@rcn.com>
parents: 51122
diff changeset
87 x = self.thetype()
b34cae786cb0 Issue 6573: Fix set.union() for cases where self is in the argument chain.
Raymond Hettinger <python@rcn.com>
parents: 51122
diff changeset
88 self.assertEqual(x.union(set([1]), x, set([2])), self.thetype([1, 2]))
b34cae786cb0 Issue 6573: Fix set.union() for cases where self is in the argument chain.
Raymond Hettinger <python@rcn.com>
parents: 51122
diff changeset
89
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
90 def test_or(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
91 i = self.s.union(self.otherword)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
92 self.assertEqual(self.s | set(self.otherword), i)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
93 self.assertEqual(self.s | frozenset(self.otherword), i)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
94 try:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
95 self.s | self.otherword
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
96 except TypeError:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
97 pass
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
98 else:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
99 self.fail("s|t did not screen-out general iterables")
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
100
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
101 def test_intersection(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
102 i = self.s.intersection(self.otherword)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
103 for c in self.letters:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
104 self.assertEqual(c in i, c in self.d and c in self.otherword)
31004
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
105 self.assertEqual(self.s, self.thetype(self.word))
49730
c707da343557 Issue #1721812: Binary operations and copy operations on set/frozenset
Raymond Hettinger <python@rcn.com>
parents: 49518
diff changeset
106 self.assertEqual(type(i), self.basetype)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
107 self.assertRaises(PassThru, self.s.intersection, check_pass_thru())
43604
b18f6092f6e0 Patch 1280, by Alexandre Vassalotti.
Guido van Rossum <guido@python.org>
parents: 42756
diff changeset
108 for C in set, frozenset, dict.fromkeys, str, list, tuple:
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
109 self.assertEqual(self.thetype('abcba').intersection(C('cdc')), set('cc'))
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
110 self.assertEqual(self.thetype('abcba').intersection(C('efgfe')), set(''))
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
111 self.assertEqual(self.thetype('abcba').intersection(C('ccb')), set('bc'))
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
112 self.assertEqual(self.thetype('abcba').intersection(C('ef')), set(''))
47562
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47201
diff changeset
113 self.assertEqual(self.thetype('abcba').intersection(C('cbcf'), C('bag')), set('b'))
47750
f64996760984 Merged revisions 64089,64098,64100-64102,64113,64115-64116,64118,64120,64132,64342 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47562
diff changeset
114 s = self.thetype('abcba')
f64996760984 Merged revisions 64089,64098,64100-64102,64113,64115-64116,64118,64120,64132,64342 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47562
diff changeset
115 z = s.intersection()
f64996760984 Merged revisions 64089,64098,64100-64102,64113,64115-64116,64118,64120,64132,64342 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47562
diff changeset
116 if self.thetype == frozenset():
f64996760984 Merged revisions 64089,64098,64100-64102,64113,64115-64116,64118,64120,64132,64342 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47562
diff changeset
117 self.assertEqual(id(s), id(z))
f64996760984 Merged revisions 64089,64098,64100-64102,64113,64115-64116,64118,64120,64132,64342 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47562
diff changeset
118 else:
f64996760984 Merged revisions 64089,64098,64100-64102,64113,64115-64116,64118,64120,64132,64342 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47562
diff changeset
119 self.assertNotEqual(id(s), id(z))
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
120
43815
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43604
diff changeset
121 def test_isdisjoint(self):
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43604
diff changeset
122 def f(s1, s2):
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43604
diff changeset
123 'Pure python equivalent of isdisjoint()'
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43604
diff changeset
124 return not set(s1).intersection(s2)
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43604
diff changeset
125 for larg in '', 'a', 'ab', 'abc', 'ababac', 'cdc', 'cc', 'efgfe', 'ccb', 'ef':
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43604
diff changeset
126 s1 = self.thetype(larg)
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43604
diff changeset
127 for rarg in '', 'a', 'ab', 'abc', 'ababac', 'cdc', 'cc', 'efgfe', 'ccb', 'ef':
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43604
diff changeset
128 for C in set, frozenset, dict.fromkeys, str, list, tuple:
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43604
diff changeset
129 s2 = C(rarg)
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43604
diff changeset
130 actual = s1.isdisjoint(s2)
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43604
diff changeset
131 expected = f(s1, s2)
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43604
diff changeset
132 self.assertEqual(actual, expected)
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55467
diff changeset
133 self.assertTrue(actual is True or actual is False)
43815
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43604
diff changeset
134
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
135 def test_and(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
136 i = self.s.intersection(self.otherword)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
137 self.assertEqual(self.s & set(self.otherword), i)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
138 self.assertEqual(self.s & frozenset(self.otherword), i)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
139 try:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
140 self.s & self.otherword
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
141 except TypeError:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
142 pass
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
143 else:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
144 self.fail("s&t did not screen-out general iterables")
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
145
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
146 def test_difference(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
147 i = self.s.difference(self.otherword)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
148 for c in self.letters:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
149 self.assertEqual(c in i, c in self.d and c not in self.otherword)
31004
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
150 self.assertEqual(self.s, self.thetype(self.word))
49730
c707da343557 Issue #1721812: Binary operations and copy operations on set/frozenset
Raymond Hettinger <python@rcn.com>
parents: 49518
diff changeset
151 self.assertEqual(type(i), self.basetype)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
152 self.assertRaises(PassThru, self.s.difference, check_pass_thru())
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
153 self.assertRaises(TypeError, self.s.difference, [[]])
43604
b18f6092f6e0 Patch 1280, by Alexandre Vassalotti.
Guido van Rossum <guido@python.org>
parents: 42756
diff changeset
154 for C in set, frozenset, dict.fromkeys, str, list, tuple:
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
155 self.assertEqual(self.thetype('abcba').difference(C('cdc')), set('ab'))
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
156 self.assertEqual(self.thetype('abcba').difference(C('efgfe')), set('abc'))
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
157 self.assertEqual(self.thetype('abcba').difference(C('ccb')), set('a'))
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
158 self.assertEqual(self.thetype('abcba').difference(C('ef')), set('abc'))
47750
f64996760984 Merged revisions 64089,64098,64100-64102,64113,64115-64116,64118,64120,64132,64342 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47562
diff changeset
159 self.assertEqual(self.thetype('abcba').difference(), set('abc'))
f64996760984 Merged revisions 64089,64098,64100-64102,64113,64115-64116,64118,64120,64132,64342 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47562
diff changeset
160 self.assertEqual(self.thetype('abcba').difference(C('a'), C('b')), set('c'))
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
161
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
162 def test_sub(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
163 i = self.s.difference(self.otherword)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
164 self.assertEqual(self.s - set(self.otherword), i)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
165 self.assertEqual(self.s - frozenset(self.otherword), i)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
166 try:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
167 self.s - self.otherword
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
168 except TypeError:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
169 pass
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
170 else:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
171 self.fail("s-t did not screen-out general iterables")
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
172
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
173 def test_symmetric_difference(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
174 i = self.s.symmetric_difference(self.otherword)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
175 for c in self.letters:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
176 self.assertEqual(c in i, (c in self.d) ^ (c in self.otherword))
31004
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
177 self.assertEqual(self.s, self.thetype(self.word))
49730
c707da343557 Issue #1721812: Binary operations and copy operations on set/frozenset
Raymond Hettinger <python@rcn.com>
parents: 49518
diff changeset
178 self.assertEqual(type(i), self.basetype)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
179 self.assertRaises(PassThru, self.s.symmetric_difference, check_pass_thru())
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
180 self.assertRaises(TypeError, self.s.symmetric_difference, [[]])
43604
b18f6092f6e0 Patch 1280, by Alexandre Vassalotti.
Guido van Rossum <guido@python.org>
parents: 42756
diff changeset
181 for C in set, frozenset, dict.fromkeys, str, list, tuple:
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
182 self.assertEqual(self.thetype('abcba').symmetric_difference(C('cdc')), set('abd'))
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
183 self.assertEqual(self.thetype('abcba').symmetric_difference(C('efgfe')), set('abcefg'))
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
184 self.assertEqual(self.thetype('abcba').symmetric_difference(C('ccb')), set('a'))
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
185 self.assertEqual(self.thetype('abcba').symmetric_difference(C('ef')), set('abcef'))
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
186
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
187 def test_xor(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
188 i = self.s.symmetric_difference(self.otherword)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
189 self.assertEqual(self.s ^ set(self.otherword), i)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
190 self.assertEqual(self.s ^ frozenset(self.otherword), i)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
191 try:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
192 self.s ^ self.otherword
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
193 except TypeError:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
194 pass
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
195 else:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
196 self.fail("s^t did not screen-out general iterables")
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
197
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
198 def test_equality(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
199 self.assertEqual(self.s, set(self.word))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
200 self.assertEqual(self.s, frozenset(self.word))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
201 self.assertEqual(self.s == self.word, False)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
202 self.assertNotEqual(self.s, set(self.otherword))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
203 self.assertNotEqual(self.s, frozenset(self.otherword))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
204 self.assertEqual(self.s != self.word, True)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
205
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
206 def test_setOfFrozensets(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
207 t = map(frozenset, ['abcdef', 'bcd', 'bdcb', 'fed', 'fedccba'])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
208 s = self.thetype(t)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
209 self.assertEqual(len(s), 3)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
210
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
211 def test_sub_and_super(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
212 p, q, r = map(self.thetype, ['ab', 'abcde', 'def'])
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55467
diff changeset
213 self.assertTrue(p < q)
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55467
diff changeset
214 self.assertTrue(p <= q)
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55467
diff changeset
215 self.assertTrue(q <= q)
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55467
diff changeset
216 self.assertTrue(q > p)
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55467
diff changeset
217 self.assertTrue(q >= p)
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55467
diff changeset
218 self.assertFalse(q < r)
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55467
diff changeset
219 self.assertFalse(q <= r)
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55467
diff changeset
220 self.assertFalse(q > r)
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55467
diff changeset
221 self.assertFalse(q >= r)
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55467
diff changeset
222 self.assertTrue(set('a').issubset('abc'))
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55467
diff changeset
223 self.assertTrue(set('abc').issuperset('a'))
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55467
diff changeset
224 self.assertFalse(set('a').issubset('cbs'))
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55467
diff changeset
225 self.assertFalse(set('cbs').issuperset('a'))
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
226
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
227 def test_pickling(self):
50540
4229a4b14f1d Merged revisions 67653,67655,67682-67683,67724,67755,67780-67783,67786,67789,67841,67843,67865,67881,67884,67925,67929,67931 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49730
diff changeset
228 for i in range(pickle.HIGHEST_PROTOCOL + 1):
33948
f2ce15b02ba3 SF 1062353: set pickling problems
Raymond Hettinger <python@rcn.com>
parents: 33605
diff changeset
229 p = pickle.dumps(self.s, i)
f2ce15b02ba3 SF 1062353: set pickling problems
Raymond Hettinger <python@rcn.com>
parents: 33605
diff changeset
230 dup = pickle.loads(p)
f2ce15b02ba3 SF 1062353: set pickling problems
Raymond Hettinger <python@rcn.com>
parents: 33605
diff changeset
231 self.assertEqual(self.s, dup, "%s != %s" % (self.s, dup))
f2ce15b02ba3 SF 1062353: set pickling problems
Raymond Hettinger <python@rcn.com>
parents: 33605
diff changeset
232 if type(self.s) not in (set, frozenset):
f2ce15b02ba3 SF 1062353: set pickling problems
Raymond Hettinger <python@rcn.com>
parents: 33605
diff changeset
233 self.s.x = 10
f2ce15b02ba3 SF 1062353: set pickling problems
Raymond Hettinger <python@rcn.com>
parents: 33605
diff changeset
234 p = pickle.dumps(self.s)
f2ce15b02ba3 SF 1062353: set pickling problems
Raymond Hettinger <python@rcn.com>
parents: 33605
diff changeset
235 dup = pickle.loads(p)
f2ce15b02ba3 SF 1062353: set pickling problems
Raymond Hettinger <python@rcn.com>
parents: 33605
diff changeset
236 self.assertEqual(self.s.x, dup.x)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
237
76091
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75073
diff changeset
238 def test_iterator_pickling(self):
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75073
diff changeset
239 itorg = iter(self.s)
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75073
diff changeset
240 data = self.thetype(self.s)
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75073
diff changeset
241 d = pickle.dumps(itorg)
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75073
diff changeset
242 it = pickle.loads(d)
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75073
diff changeset
243 # Set iterators unpickle as list iterators due to the
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75073
diff changeset
244 # undefined order of set items.
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75073
diff changeset
245 # self.assertEqual(type(itorg), type(it))
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75073
diff changeset
246 self.assertTrue(isinstance(it, collections.abc.Iterator))
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75073
diff changeset
247 self.assertEqual(self.thetype(it), data)
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75073
diff changeset
248
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75073
diff changeset
249 it = pickle.loads(d)
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75073
diff changeset
250 try:
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75073
diff changeset
251 drop = next(it)
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75073
diff changeset
252 except StopIteration:
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75073
diff changeset
253 return
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75073
diff changeset
254 d = pickle.dumps(it)
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75073
diff changeset
255 it = pickle.loads(d)
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75073
diff changeset
256 self.assertEqual(self.thetype(it), data - self.thetype((drop,)))
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75073
diff changeset
257
31004
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
258 def test_deepcopy(self):
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
259 class Tracer:
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
260 def __init__(self, value):
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
261 self.value = value
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
262 def __hash__(self):
31389
aca1ce1b4b61 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 31291
diff changeset
263 return self.value
31004
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
264 def __deepcopy__(self, memo=None):
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
265 return Tracer(self.value + 1)
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
266 t = Tracer(10)
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
267 s = self.thetype([t])
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
268 dup = copy.deepcopy(s)
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
269 self.assertNotEqual(id(s), id(dup))
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
270 for elem in dup:
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
271 newt = elem
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
272 self.assertNotEqual(id(t), id(newt))
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
273 self.assertEqual(t.value + 1, newt.value)
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
274
35110
f3d3151f8417 SF patch #1200018: Restore GC support to set objects
Raymond Hettinger <python@rcn.com>
parents: 33948
diff changeset
275 def test_gc(self):
f3d3151f8417 SF patch #1200018: Restore GC support to set objects
Raymond Hettinger <python@rcn.com>
parents: 33948
diff changeset
276 # Create a nest of cycles to exercise overall ref count check
f3d3151f8417 SF patch #1200018: Restore GC support to set objects
Raymond Hettinger <python@rcn.com>
parents: 33948
diff changeset
277 class A:
f3d3151f8417 SF patch #1200018: Restore GC support to set objects
Raymond Hettinger <python@rcn.com>
parents: 33948
diff changeset
278 pass
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41575
diff changeset
279 s = set(A() for i in range(1000))
35110
f3d3151f8417 SF patch #1200018: Restore GC support to set objects
Raymond Hettinger <python@rcn.com>
parents: 33948
diff changeset
280 for elem in s:
f3d3151f8417 SF patch #1200018: Restore GC support to set objects
Raymond Hettinger <python@rcn.com>
parents: 33948
diff changeset
281 elem.cycle = s
f3d3151f8417 SF patch #1200018: Restore GC support to set objects
Raymond Hettinger <python@rcn.com>
parents: 33948
diff changeset
282 elem.sub = elem
f3d3151f8417 SF patch #1200018: Restore GC support to set objects
Raymond Hettinger <python@rcn.com>
parents: 33948
diff changeset
283 elem.set = set([elem])
f3d3151f8417 SF patch #1200018: Restore GC support to set objects
Raymond Hettinger <python@rcn.com>
parents: 33948
diff changeset
284
35334
725671db779a * Fix SF #1257731. Make __contains__(), remove(), and discard() only do
Raymond Hettinger <python@rcn.com>
parents: 35328
diff changeset
285 def test_subclass_with_custom_hash(self):
725671db779a * Fix SF #1257731. Make __contains__(), remove(), and discard() only do
Raymond Hettinger <python@rcn.com>
parents: 35328
diff changeset
286 # Bug #1257731
725671db779a * Fix SF #1257731. Make __contains__(), remove(), and discard() only do
Raymond Hettinger <python@rcn.com>
parents: 35328
diff changeset
287 class H(self.thetype):
725671db779a * Fix SF #1257731. Make __contains__(), remove(), and discard() only do
Raymond Hettinger <python@rcn.com>
parents: 35328
diff changeset
288 def __hash__(self):
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36484
diff changeset
289 return int(id(self) & 0x7fffffff)
35334
725671db779a * Fix SF #1257731. Make __contains__(), remove(), and discard() only do
Raymond Hettinger <python@rcn.com>
parents: 35328
diff changeset
290 s=H()
725671db779a * Fix SF #1257731. Make __contains__(), remove(), and discard() only do
Raymond Hettinger <python@rcn.com>
parents: 35328
diff changeset
291 f=set()
725671db779a * Fix SF #1257731. Make __contains__(), remove(), and discard() only do
Raymond Hettinger <python@rcn.com>
parents: 35328
diff changeset
292 f.add(s)
58415
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 55466
diff changeset
293 self.assertIn(s, f)
35334
725671db779a * Fix SF #1257731. Make __contains__(), remove(), and discard() only do
Raymond Hettinger <python@rcn.com>
parents: 35328
diff changeset
294 f.remove(s)
725671db779a * Fix SF #1257731. Make __contains__(), remove(), and discard() only do
Raymond Hettinger <python@rcn.com>
parents: 35328
diff changeset
295 f.add(s)
725671db779a * Fix SF #1257731. Make __contains__(), remove(), and discard() only do
Raymond Hettinger <python@rcn.com>
parents: 35328
diff changeset
296 f.discard(s)
725671db779a * Fix SF #1257731. Make __contains__(), remove(), and discard() only do
Raymond Hettinger <python@rcn.com>
parents: 35328
diff changeset
297
35581
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
298 def test_badcmp(self):
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
299 s = self.thetype([BadCmp()])
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
300 # Detect comparison errors during insertion and lookup
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
301 self.assertRaises(RuntimeError, self.thetype, [BadCmp(), BadCmp()])
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
302 self.assertRaises(RuntimeError, s.__contains__, BadCmp())
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
303 # Detect errors during mutating operations
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
304 if hasattr(s, 'add'):
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
305 self.assertRaises(RuntimeError, s.add, BadCmp())
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
306 self.assertRaises(RuntimeError, s.discard, BadCmp())
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
307 self.assertRaises(RuntimeError, s.remove, BadCmp())
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
308
40582
67287ca4e1e8 Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40450
diff changeset
309 def test_cyclical_repr(self):
67287ca4e1e8 Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40450
diff changeset
310 w = ReprWrapper()
67287ca4e1e8 Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40450
diff changeset
311 s = self.thetype([w])
67287ca4e1e8 Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40450
diff changeset
312 w.value = s
67287ca4e1e8 Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40450
diff changeset
313 if self.thetype == set:
67287ca4e1e8 Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40450
diff changeset
314 self.assertEqual(repr(s), '{set(...)}')
67287ca4e1e8 Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40450
diff changeset
315 else:
67287ca4e1e8 Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40450
diff changeset
316 name = repr(s).partition('(')[0] # strip class name
42482
4dea3b68b013 Change the repr() of frozenset instances (and set subclasses)
Guido van Rossum <guido@python.org>
parents: 42050
diff changeset
317 self.assertEqual(repr(s), '%s({%s(...)})' % (name, name))
40582
67287ca4e1e8 Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40450
diff changeset
318
67287ca4e1e8 Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40450
diff changeset
319 def test_cyclical_print(self):
67287ca4e1e8 Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40450
diff changeset
320 w = ReprWrapper()
67287ca4e1e8 Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40450
diff changeset
321 s = self.thetype([w])
67287ca4e1e8 Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40450
diff changeset
322 w.value = s
48476
f8723eb105ac Fix a potential NameError.
Thomas Heller <theller@ctypes.org>
parents: 47750
diff changeset
323 fo = open(support.TESTFN, "w")
40582
67287ca4e1e8 Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40450
diff changeset
324 try:
40789
e8b2913f9297 Some more tests pass now. (Also test_compiler.py with -u all.)
Guido van Rossum <guido@python.org>
parents: 40742
diff changeset
325 fo.write(str(s))
40582
67287ca4e1e8 Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40450
diff changeset
326 fo.close()
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 46084
diff changeset
327 fo = open(support.TESTFN, "r")
40582
67287ca4e1e8 Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40450
diff changeset
328 self.assertEqual(fo.read(), repr(s))
67287ca4e1e8 Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40450
diff changeset
329 finally:
67287ca4e1e8 Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40450
diff changeset
330 fo.close()
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 46084
diff changeset
331 support.unlink(support.TESTFN)
40582
67287ca4e1e8 Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40450
diff changeset
332
40893
32c4897b5d46 Merged revisions 53623-53858 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40789
diff changeset
333 def test_do_not_rehash_dict_keys(self):
32c4897b5d46 Merged revisions 53623-53858 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40789
diff changeset
334 n = 10
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41575
diff changeset
335 d = dict.fromkeys(map(HashCountingInt, range(n)))
40893
32c4897b5d46 Merged revisions 53623-53858 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40789
diff changeset
336 self.assertEqual(sum(elem.hash_count for elem in d), n)
32c4897b5d46 Merged revisions 53623-53858 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40789
diff changeset
337 s = self.thetype(d)
32c4897b5d46 Merged revisions 53623-53858 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40789
diff changeset
338 self.assertEqual(sum(elem.hash_count for elem in d), n)
32c4897b5d46 Merged revisions 53623-53858 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40789
diff changeset
339 s.difference(d)
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
340 self.assertEqual(sum(elem.hash_count for elem in d), n)
40893
32c4897b5d46 Merged revisions 53623-53858 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40789
diff changeset
341 if hasattr(s, 'symmetric_difference_update'):
32c4897b5d46 Merged revisions 53623-53858 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40789
diff changeset
342 s.symmetric_difference_update(d)
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
343 self.assertEqual(sum(elem.hash_count for elem in d), n)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
344 d2 = dict.fromkeys(set(d))
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
345 self.assertEqual(sum(elem.hash_count for elem in d), n)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
346 d3 = dict.fromkeys(frozenset(d))
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
347 self.assertEqual(sum(elem.hash_count for elem in d), n)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
348 d3 = dict.fromkeys(frozenset(d), 123)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
349 self.assertEqual(sum(elem.hash_count for elem in d), n)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
350 self.assertEqual(d3, dict.fromkeys(d, 123))
40893
32c4897b5d46 Merged revisions 53623-53858 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40789
diff changeset
351
50553
badb471dbaeb Merged revisions 68132 via svnmerge from
Georg Brandl <georg@python.org>
parents: 50540
diff changeset
352 def test_container_iterator(self):
badb471dbaeb Merged revisions 68132 via svnmerge from
Georg Brandl <georg@python.org>
parents: 50540
diff changeset
353 # Bug #3680: tp_traverse was not implemented for set iterator object
badb471dbaeb Merged revisions 68132 via svnmerge from
Georg Brandl <georg@python.org>
parents: 50540
diff changeset
354 class C(object):
badb471dbaeb Merged revisions 68132 via svnmerge from
Georg Brandl <georg@python.org>
parents: 50540
diff changeset
355 pass
badb471dbaeb Merged revisions 68132 via svnmerge from
Georg Brandl <georg@python.org>
parents: 50540
diff changeset
356 obj = C()
badb471dbaeb Merged revisions 68132 via svnmerge from
Georg Brandl <georg@python.org>
parents: 50540
diff changeset
357 ref = weakref.ref(obj)
badb471dbaeb Merged revisions 68132 via svnmerge from
Georg Brandl <georg@python.org>
parents: 50540
diff changeset
358 container = set([obj, 1])
badb471dbaeb Merged revisions 68132 via svnmerge from
Georg Brandl <georg@python.org>
parents: 50540
diff changeset
359 obj.x = iter(container)
badb471dbaeb Merged revisions 68132 via svnmerge from
Georg Brandl <georg@python.org>
parents: 50540
diff changeset
360 del obj, container
badb471dbaeb Merged revisions 68132 via svnmerge from
Georg Brandl <georg@python.org>
parents: 50540
diff changeset
361 gc.collect()
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55467
diff changeset
362 self.assertTrue(ref() is None, "Cycle was not collected")
50553
badb471dbaeb Merged revisions 68132 via svnmerge from
Georg Brandl <georg@python.org>
parents: 50540
diff changeset
363
83449
ef100be2e8ba #17790: test_set now works with unittest test discovery. Patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 76091
diff changeset
364 class TestSet(TestJointOps, unittest.TestCase):
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
365 thetype = set
49730
c707da343557 Issue #1721812: Binary operations and copy operations on set/frozenset
Raymond Hettinger <python@rcn.com>
parents: 49518
diff changeset
366 basetype = set
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
367
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30929
diff changeset
368 def test_init(self):
31004
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
369 s = self.thetype()
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30929
diff changeset
370 s.__init__(self.word)
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30929
diff changeset
371 self.assertEqual(s, set(self.word))
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30929
diff changeset
372 s.__init__(self.otherword)
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30929
diff changeset
373 self.assertEqual(s, set(self.otherword))
32555
d909a0ddd193 * fix the print test
Raymond Hettinger <python@rcn.com>
parents: 32158
diff changeset
374 self.assertRaises(TypeError, s.__init__, s, 2);
d909a0ddd193 * fix the print test
Raymond Hettinger <python@rcn.com>
parents: 32158
diff changeset
375 self.assertRaises(TypeError, s.__init__, 1);
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30929
diff changeset
376
31004
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
377 def test_constructor_identity(self):
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
378 s = self.thetype(range(3))
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
379 t = self.thetype(s)
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
380 self.assertNotEqual(id(s), id(t))
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
381
39693
d0b83c4630c0 SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents: 39644
diff changeset
382 def test_set_literal(self):
d0b83c4630c0 SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents: 39644
diff changeset
383 s = set([1,2,3])
d0b83c4630c0 SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents: 39644
diff changeset
384 t = {1,2,3}
d0b83c4630c0 SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents: 39644
diff changeset
385 self.assertEqual(s, t)
d0b83c4630c0 SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents: 39644
diff changeset
386
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
387 def test_hash(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
388 self.assertRaises(TypeError, hash, self.s)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
389
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
390 def test_clear(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
391 self.s.clear()
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
392 self.assertEqual(self.s, set())
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
393 self.assertEqual(len(self.s), 0)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
394
31004
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
395 def test_copy(self):
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
396 dup = self.s.copy()
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
397 self.assertEqual(self.s, dup)
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
398 self.assertNotEqual(id(self.s), id(dup))
49730
c707da343557 Issue #1721812: Binary operations and copy operations on set/frozenset
Raymond Hettinger <python@rcn.com>
parents: 49518
diff changeset
399 self.assertEqual(type(dup), self.basetype)
31004
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
400
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
401 def test_add(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
402 self.s.add('Q')
58415
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 55466
diff changeset
403 self.assertIn('Q', self.s)
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
404 dup = self.s.copy()
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
405 self.s.add('Q')
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
406 self.assertEqual(self.s, dup)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
407 self.assertRaises(TypeError, self.s.add, [])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
408
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
409 def test_remove(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
410 self.s.remove('a')
58415
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 55466
diff changeset
411 self.assertNotIn('a', self.s)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
412 self.assertRaises(KeyError, self.s.remove, 'Q')
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
413 self.assertRaises(TypeError, self.s.remove, [])
31002
622a76bc0237 Extend temporary hashability to remove() and discard().
Raymond Hettinger <python@rcn.com>
parents: 30995
diff changeset
414 s = self.thetype([frozenset(self.word)])
58415
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 55466
diff changeset
415 self.assertIn(self.thetype(self.word), s)
31002
622a76bc0237 Extend temporary hashability to remove() and discard().
Raymond Hettinger <python@rcn.com>
parents: 30995
diff changeset
416 s.remove(self.thetype(self.word))
58415
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 55466
diff changeset
417 self.assertNotIn(self.thetype(self.word), s)
31002
622a76bc0237 Extend temporary hashability to remove() and discard().
Raymond Hettinger <python@rcn.com>
parents: 30995
diff changeset
418 self.assertRaises(KeyError, self.s.remove, self.thetype(self.word))
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
419
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39696
diff changeset
420 def test_remove_keyerror_unpacking(self):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39696
diff changeset
421 # bug: www.python.org/sf/1576657
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39696
diff changeset
422 for v1 in ['Q', (1,)]:
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39696
diff changeset
423 try:
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39696
diff changeset
424 self.s.remove(v1)
40587
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 40583
diff changeset
425 except KeyError as e:
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39696
diff changeset
426 v2 = e.args[0]
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39696
diff changeset
427 self.assertEqual(v1, v2)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39696
diff changeset
428 else:
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39696
diff changeset
429 self.fail()
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39696
diff changeset
430
49493
bdf4da5d04ad merge from trunk
Benjamin Peterson <benjamin@python.org>
parents: 48476
diff changeset
431 def test_remove_keyerror_set(self):
bdf4da5d04ad merge from trunk
Benjamin Peterson <benjamin@python.org>
parents: 48476
diff changeset
432 key = self.thetype([3, 4])
bdf4da5d04ad merge from trunk
Benjamin Peterson <benjamin@python.org>
parents: 48476
diff changeset
433 try:
bdf4da5d04ad merge from trunk
Benjamin Peterson <benjamin@python.org>
parents: 48476
diff changeset
434 self.s.remove(key)
bdf4da5d04ad merge from trunk
Benjamin Peterson <benjamin@python.org>
parents: 48476
diff changeset
435 except KeyError as e:
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55467
diff changeset
436 self.assertTrue(e.args[0] is key,
49493
bdf4da5d04ad merge from trunk
Benjamin Peterson <benjamin@python.org>
parents: 48476
diff changeset
437 "KeyError should be {0}, not {1}".format(key,
bdf4da5d04ad merge from trunk
Benjamin Peterson <benjamin@python.org>
parents: 48476
diff changeset
438 e.args[0]))
bdf4da5d04ad merge from trunk
Benjamin Peterson <benjamin@python.org>
parents: 48476
diff changeset
439 else:
bdf4da5d04ad merge from trunk
Benjamin Peterson <benjamin@python.org>
parents: 48476
diff changeset
440 self.fail()
bdf4da5d04ad merge from trunk
Benjamin Peterson <benjamin@python.org>
parents: 48476
diff changeset
441
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
442 def test_discard(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
443 self.s.discard('a')
58415
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 55466
diff changeset
444 self.assertNotIn('a', self.s)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
445 self.s.discard('Q')
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
446 self.assertRaises(TypeError, self.s.discard, [])
31002
622a76bc0237 Extend temporary hashability to remove() and discard().
Raymond Hettinger <python@rcn.com>
parents: 30995
diff changeset
447 s = self.thetype([frozenset(self.word)])
58415
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 55466
diff changeset
448 self.assertIn(self.thetype(self.word), s)
31002
622a76bc0237 Extend temporary hashability to remove() and discard().
Raymond Hettinger <python@rcn.com>
parents: 30995
diff changeset
449 s.discard(self.thetype(self.word))
58415
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 55466
diff changeset
450 self.assertNotIn(self.thetype(self.word), s)
31002
622a76bc0237 Extend temporary hashability to remove() and discard().
Raymond Hettinger <python@rcn.com>
parents: 30995
diff changeset
451 s.discard(self.thetype(self.word))
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
452
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
453 def test_pop(self):
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41575
diff changeset
454 for i in range(len(self.s)):
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
455 elem = self.s.pop()
58415
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 55466
diff changeset
456 self.assertNotIn(elem, self.s)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
457 self.assertRaises(KeyError, self.s.pop)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
458
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
459 def test_update(self):
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
460 retval = self.s.update(self.otherword)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
461 self.assertEqual(retval, None)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
462 for c in (self.word + self.otherword):
58415
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 55466
diff changeset
463 self.assertIn(c, self.s)
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
464 self.assertRaises(PassThru, self.s.update, check_pass_thru())
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
465 self.assertRaises(TypeError, self.s.update, [[]])
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
466 for p, q in (('cdc', 'abcd'), ('efgfe', 'abcefg'), ('ccb', 'abc'), ('ef', 'abcef')):
43604
b18f6092f6e0 Patch 1280, by Alexandre Vassalotti.
Guido van Rossum <guido@python.org>
parents: 42756
diff changeset
467 for C in set, frozenset, dict.fromkeys, str, list, tuple:
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
468 s = self.thetype('abcba')
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
469 self.assertEqual(s.update(C(p)), None)
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
470 self.assertEqual(s, set(q))
47562
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47201
diff changeset
471 for p in ('cdc', 'efgfe', 'ccb', 'ef', 'abcda'):
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47201
diff changeset
472 q = 'ahi'
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47201
diff changeset
473 for C in set, frozenset, dict.fromkeys, str, list, tuple:
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47201
diff changeset
474 s = self.thetype('abcba')
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47201
diff changeset
475 self.assertEqual(s.update(C(p), C(q)), None)
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47201
diff changeset
476 self.assertEqual(s, set(s) | set(p) | set(q))
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
477
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
478 def test_ior(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
479 self.s |= set(self.otherword)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
480 for c in (self.word + self.otherword):
58415
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 55466
diff changeset
481 self.assertIn(c, self.s)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
482
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
483 def test_intersection_update(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
484 retval = self.s.intersection_update(self.otherword)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
485 self.assertEqual(retval, None)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
486 for c in (self.word + self.otherword):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
487 if c in self.otherword and c in self.word:
58415
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 55466
diff changeset
488 self.assertIn(c, self.s)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
489 else:
58415
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 55466
diff changeset
490 self.assertNotIn(c, self.s)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
491 self.assertRaises(PassThru, self.s.intersection_update, check_pass_thru())
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
492 self.assertRaises(TypeError, self.s.intersection_update, [[]])
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
493 for p, q in (('cdc', 'c'), ('efgfe', ''), ('ccb', 'bc'), ('ef', '')):
43604
b18f6092f6e0 Patch 1280, by Alexandre Vassalotti.
Guido van Rossum <guido@python.org>
parents: 42756
diff changeset
494 for C in set, frozenset, dict.fromkeys, str, list, tuple:
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
495 s = self.thetype('abcba')
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
496 self.assertEqual(s.intersection_update(C(p)), None)
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
497 self.assertEqual(s, set(q))
47562
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47201
diff changeset
498 ss = 'abcba'
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47201
diff changeset
499 s = self.thetype(ss)
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47201
diff changeset
500 t = 'cbc'
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47201
diff changeset
501 self.assertEqual(s.intersection_update(C(p), C(t)), None)
bd584f121c4b Merged revisions 64002-64003,64012,64036-64037,64047,64050-64052,64054-64055,64066,64071 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47201
diff changeset
502 self.assertEqual(s, set('abcba')&set(p)&set(t))
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
503
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
504 def test_iand(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
505 self.s &= set(self.otherword)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
506 for c in (self.word + self.otherword):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
507 if c in self.otherword and c in self.word:
58415
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 55466
diff changeset
508 self.assertIn(c, self.s)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
509 else:
58415
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 55466
diff changeset
510 self.assertNotIn(c, self.s)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
511
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
512 def test_difference_update(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
513 retval = self.s.difference_update(self.otherword)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
514 self.assertEqual(retval, None)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
515 for c in (self.word + self.otherword):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
516 if c in self.word and c not in self.otherword:
58415
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 55466
diff changeset
517 self.assertIn(c, self.s)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
518 else:
58415
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 55466
diff changeset
519 self.assertNotIn(c, self.s)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
520 self.assertRaises(PassThru, self.s.difference_update, check_pass_thru())
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
521 self.assertRaises(TypeError, self.s.difference_update, [[]])
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
522 self.assertRaises(TypeError, self.s.symmetric_difference_update, [[]])
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
523 for p, q in (('cdc', 'ab'), ('efgfe', 'abc'), ('ccb', 'a'), ('ef', 'abc')):
43604
b18f6092f6e0 Patch 1280, by Alexandre Vassalotti.
Guido van Rossum <guido@python.org>
parents: 42756
diff changeset
524 for C in set, frozenset, dict.fromkeys, str, list, tuple:
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
525 s = self.thetype('abcba')
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
526 self.assertEqual(s.difference_update(C(p)), None)
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
527 self.assertEqual(s, set(q))
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
528
47750
f64996760984 Merged revisions 64089,64098,64100-64102,64113,64115-64116,64118,64120,64132,64342 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47562
diff changeset
529 s = self.thetype('abcdefghih')
f64996760984 Merged revisions 64089,64098,64100-64102,64113,64115-64116,64118,64120,64132,64342 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47562
diff changeset
530 s.difference_update()
f64996760984 Merged revisions 64089,64098,64100-64102,64113,64115-64116,64118,64120,64132,64342 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47562
diff changeset
531 self.assertEqual(s, self.thetype('abcdefghih'))
f64996760984 Merged revisions 64089,64098,64100-64102,64113,64115-64116,64118,64120,64132,64342 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47562
diff changeset
532
f64996760984 Merged revisions 64089,64098,64100-64102,64113,64115-64116,64118,64120,64132,64342 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47562
diff changeset
533 s = self.thetype('abcdefghih')
f64996760984 Merged revisions 64089,64098,64100-64102,64113,64115-64116,64118,64120,64132,64342 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47562
diff changeset
534 s.difference_update(C('aba'))
f64996760984 Merged revisions 64089,64098,64100-64102,64113,64115-64116,64118,64120,64132,64342 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47562
diff changeset
535 self.assertEqual(s, self.thetype('cdefghih'))
f64996760984 Merged revisions 64089,64098,64100-64102,64113,64115-64116,64118,64120,64132,64342 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47562
diff changeset
536
f64996760984 Merged revisions 64089,64098,64100-64102,64113,64115-64116,64118,64120,64132,64342 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47562
diff changeset
537 s = self.thetype('abcdefghih')
f64996760984 Merged revisions 64089,64098,64100-64102,64113,64115-64116,64118,64120,64132,64342 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47562
diff changeset
538 s.difference_update(C('cdc'), C('aba'))
f64996760984 Merged revisions 64089,64098,64100-64102,64113,64115-64116,64118,64120,64132,64342 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47562
diff changeset
539 self.assertEqual(s, self.thetype('efghih'))
f64996760984 Merged revisions 64089,64098,64100-64102,64113,64115-64116,64118,64120,64132,64342 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47562
diff changeset
540
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
541 def test_isub(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
542 self.s -= set(self.otherword)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
543 for c in (self.word + self.otherword):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
544 if c in self.word and c not in self.otherword:
58415
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 55466
diff changeset
545 self.assertIn(c, self.s)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
546 else:
58415
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 55466
diff changeset
547 self.assertNotIn(c, self.s)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
548
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
549 def test_symmetric_difference_update(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
550 retval = self.s.symmetric_difference_update(self.otherword)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
551 self.assertEqual(retval, None)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
552 for c in (self.word + self.otherword):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
553 if (c in self.word) ^ (c in self.otherword):
58415
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 55466
diff changeset
554 self.assertIn(c, self.s)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
555 else:
58415
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 55466
diff changeset
556 self.assertNotIn(c, self.s)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
557 self.assertRaises(PassThru, self.s.symmetric_difference_update, check_pass_thru())
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
558 self.assertRaises(TypeError, self.s.symmetric_difference_update, [[]])
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
559 for p, q in (('cdc', 'abd'), ('efgfe', 'abcefg'), ('ccb', 'a'), ('ef', 'abcef')):
43604
b18f6092f6e0 Patch 1280, by Alexandre Vassalotti.
Guido van Rossum <guido@python.org>
parents: 42756
diff changeset
560 for C in set, frozenset, dict.fromkeys, str, list, tuple:
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
561 s = self.thetype('abcba')
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
562 self.assertEqual(s.symmetric_difference_update(C(p)), None)
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
563 self.assertEqual(s, set(q))
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
564
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
565 def test_ixor(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
566 self.s ^= set(self.otherword)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
567 for c in (self.word + self.otherword):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
568 if (c in self.word) ^ (c in self.otherword):
58415
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 55466
diff changeset
569 self.assertIn(c, self.s)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
570 else:
58415
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 55466
diff changeset
571 self.assertNotIn(c, self.s)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
572
35328
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35297
diff changeset
573 def test_inplace_on_self(self):
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35297
diff changeset
574 t = self.s.copy()
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35297
diff changeset
575 t |= t
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35297
diff changeset
576 self.assertEqual(t, self.s)
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35297
diff changeset
577 t &= t
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35297
diff changeset
578 self.assertEqual(t, self.s)
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35297
diff changeset
579 t -= t
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35297
diff changeset
580 self.assertEqual(t, self.thetype())
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35297
diff changeset
581 t = self.s.copy()
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35297
diff changeset
582 t ^= t
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35297
diff changeset
583 self.assertEqual(t, self.thetype())
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35297
diff changeset
584
32158
679e5c8effae Make sets and deques weak referencable.
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
585 def test_weakref(self):
679e5c8effae Make sets and deques weak referencable.
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
586 s = self.thetype('gallahad')
50553
badb471dbaeb Merged revisions 68132 via svnmerge from
Georg Brandl <georg@python.org>
parents: 50540
diff changeset
587 p = weakref.proxy(s)
32158
679e5c8effae Make sets and deques weak referencable.
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
588 self.assertEqual(str(p), str(s))
679e5c8effae Make sets and deques weak referencable.
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
589 s = None
679e5c8effae Make sets and deques weak referencable.
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
590 self.assertRaises(ReferenceError, str, p)
679e5c8effae Make sets and deques weak referencable.
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
591
42756
0b62539acfb8 Patch by Keir Mierle so that sets can be compared to other objects that know
Guido van Rossum <guido@python.org>
parents: 42482
diff changeset
592 def test_rich_compare(self):
0b62539acfb8 Patch by Keir Mierle so that sets can be compared to other objects that know
Guido van Rossum <guido@python.org>
parents: 42482
diff changeset
593 class TestRichSetCompare:
0b62539acfb8 Patch by Keir Mierle so that sets can be compared to other objects that know
Guido van Rossum <guido@python.org>
parents: 42482
diff changeset
594 def __gt__(self, some_set):
0b62539acfb8 Patch by Keir Mierle so that sets can be compared to other objects that know
Guido van Rossum <guido@python.org>
parents: 42482
diff changeset
595 self.gt_called = True
0b62539acfb8 Patch by Keir Mierle so that sets can be compared to other objects that know
Guido van Rossum <guido@python.org>
parents: 42482
diff changeset
596 return False
0b62539acfb8 Patch by Keir Mierle so that sets can be compared to other objects that know
Guido van Rossum <guido@python.org>
parents: 42482
diff changeset
597 def __lt__(self, some_set):
0b62539acfb8 Patch by Keir Mierle so that sets can be compared to other objects that know
Guido van Rossum <guido@python.org>
parents: 42482
diff changeset
598 self.lt_called = True
0b62539acfb8 Patch by Keir Mierle so that sets can be compared to other objects that know
Guido van Rossum <guido@python.org>
parents: 42482
diff changeset
599 return False
0b62539acfb8 Patch by Keir Mierle so that sets can be compared to other objects that know
Guido van Rossum <guido@python.org>
parents: 42482
diff changeset
600 def __ge__(self, some_set):
0b62539acfb8 Patch by Keir Mierle so that sets can be compared to other objects that know
Guido van Rossum <guido@python.org>
parents: 42482
diff changeset
601 self.ge_called = True
0b62539acfb8 Patch by Keir Mierle so that sets can be compared to other objects that know
Guido van Rossum <guido@python.org>
parents: 42482
diff changeset
602 return False
0b62539acfb8 Patch by Keir Mierle so that sets can be compared to other objects that know
Guido van Rossum <guido@python.org>
parents: 42482
diff changeset
603 def __le__(self, some_set):
0b62539acfb8 Patch by Keir Mierle so that sets can be compared to other objects that know
Guido van Rossum <guido@python.org>
parents: 42482
diff changeset
604 self.le_called = True
0b62539acfb8 Patch by Keir Mierle so that sets can be compared to other objects that know
Guido van Rossum <guido@python.org>
parents: 42482
diff changeset
605 return False
0b62539acfb8 Patch by Keir Mierle so that sets can be compared to other objects that know
Guido van Rossum <guido@python.org>
parents: 42482
diff changeset
606
68488
0991b40e895d #11515: fix several typos. Patch by Piotr Kasprzyk.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 63441
diff changeset
607 # This first tries the builtin rich set comparison, which doesn't know
42756
0b62539acfb8 Patch by Keir Mierle so that sets can be compared to other objects that know
Guido van Rossum <guido@python.org>
parents: 42482
diff changeset
608 # how to handle the custom object. Upon returning NotImplemented, the
0b62539acfb8 Patch by Keir Mierle so that sets can be compared to other objects that know
Guido van Rossum <guido@python.org>
parents: 42482
diff changeset
609 # corresponding comparison on the right object is invoked.
0b62539acfb8 Patch by Keir Mierle so that sets can be compared to other objects that know
Guido van Rossum <guido@python.org>
parents: 42482
diff changeset
610 myset = {1, 2, 3}
0b62539acfb8 Patch by Keir Mierle so that sets can be compared to other objects that know
Guido van Rossum <guido@python.org>
parents: 42482
diff changeset
611
0b62539acfb8 Patch by Keir Mierle so that sets can be compared to other objects that know
Guido van Rossum <guido@python.org>
parents: 42482
diff changeset
612 myobj = TestRichSetCompare()
0b62539acfb8 Patch by Keir Mierle so that sets can be compared to other objects that know
Guido van Rossum <guido@python.org>
parents: 42482
diff changeset
613 myset < myobj
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55467
diff changeset
614 self.assertTrue(myobj.gt_called)
42756
0b62539acfb8 Patch by Keir Mierle so that sets can be compared to other objects that know
Guido van Rossum <guido@python.org>
parents: 42482
diff changeset
615
0b62539acfb8 Patch by Keir Mierle so that sets can be compared to other objects that know
Guido van Rossum <guido@python.org>
parents: 42482
diff changeset
616 myobj = TestRichSetCompare()
0b62539acfb8 Patch by Keir Mierle so that sets can be compared to other objects that know
Guido van Rossum <guido@python.org>
parents: 42482
diff changeset
617 myset > myobj
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55467
diff changeset
618 self.assertTrue(myobj.lt_called)
42756
0b62539acfb8 Patch by Keir Mierle so that sets can be compared to other objects that know
Guido van Rossum <guido@python.org>
parents: 42482
diff changeset
619
0b62539acfb8 Patch by Keir Mierle so that sets can be compared to other objects that know
Guido van Rossum <guido@python.org>
parents: 42482
diff changeset
620 myobj = TestRichSetCompare()
0b62539acfb8 Patch by Keir Mierle so that sets can be compared to other objects that know
Guido van Rossum <guido@python.org>
parents: 42482
diff changeset
621 myset <= myobj
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55467
diff changeset
622 self.assertTrue(myobj.ge_called)
42756
0b62539acfb8 Patch by Keir Mierle so that sets can be compared to other objects that know
Guido van Rossum <guido@python.org>
parents: 42482
diff changeset
623
0b62539acfb8 Patch by Keir Mierle so that sets can be compared to other objects that know
Guido van Rossum <guido@python.org>
parents: 42482
diff changeset
624 myobj = TestRichSetCompare()
0b62539acfb8 Patch by Keir Mierle so that sets can be compared to other objects that know
Guido van Rossum <guido@python.org>
parents: 42482
diff changeset
625 myset >= myobj
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55467
diff changeset
626 self.assertTrue(myobj.le_called)
42756
0b62539acfb8 Patch by Keir Mierle so that sets can be compared to other objects that know
Guido van Rossum <guido@python.org>
parents: 42482
diff changeset
627
86895
1feeeb8992f8 Issue #18702: All skipped tests now reported as skipped.
Serhiy Storchaka <storchaka@gmail.com>
parents: 85579
diff changeset
628 @unittest.skipUnless(hasattr(set, "test_c_api"),
1feeeb8992f8 Issue #18702: All skipped tests now reported as skipped.
Serhiy Storchaka <storchaka@gmail.com>
parents: 85579
diff changeset
629 'C API test only available in a debug build')
1feeeb8992f8 Issue #18702: All skipped tests now reported as skipped.
Serhiy Storchaka <storchaka@gmail.com>
parents: 85579
diff changeset
630 def test_c_api(self):
1feeeb8992f8 Issue #18702: All skipped tests now reported as skipped.
Serhiy Storchaka <storchaka@gmail.com>
parents: 85579
diff changeset
631 self.assertEqual(set().test_c_api(), True)
35348
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35334
diff changeset
632
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30929
diff changeset
633 class SetSubclass(set):
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30929
diff changeset
634 pass
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30929
diff changeset
635
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30929
diff changeset
636 class TestSetSubclass(TestSet):
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30929
diff changeset
637 thetype = SetSubclass
49730
c707da343557 Issue #1721812: Binary operations and copy operations on set/frozenset
Raymond Hettinger <python@rcn.com>
parents: 49518
diff changeset
638 basetype = set
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
639
40643
47f9efdc0855 Merged revisions 53304-53433,53435-53450 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40587
diff changeset
640 class SetSubclassWithKeywordArgs(set):
47f9efdc0855 Merged revisions 53304-53433,53435-53450 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40587
diff changeset
641 def __init__(self, iterable=[], newarg=None):
47f9efdc0855 Merged revisions 53304-53433,53435-53450 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40587
diff changeset
642 set.__init__(self, iterable)
47f9efdc0855 Merged revisions 53304-53433,53435-53450 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40587
diff changeset
643
47f9efdc0855 Merged revisions 53304-53433,53435-53450 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40587
diff changeset
644 class TestSetSubclassWithKeywordArgs(TestSet):
40742
f1701ba2452b Merged revisions 53538-53622 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40643
diff changeset
645
40643
47f9efdc0855 Merged revisions 53304-53433,53435-53450 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40587
diff changeset
646 def test_keywords_in_subclass(self):
47f9efdc0855 Merged revisions 53304-53433,53435-53450 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40587
diff changeset
647 'SF bug #1486663 -- this used to erroneously raise a TypeError'
47f9efdc0855 Merged revisions 53304-53433,53435-53450 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40587
diff changeset
648 SetSubclassWithKeywordArgs(newarg=1)
47f9efdc0855 Merged revisions 53304-53433,53435-53450 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40587
diff changeset
649
83449
ef100be2e8ba #17790: test_set now works with unittest test discovery. Patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 76091
diff changeset
650 class TestFrozenSet(TestJointOps, unittest.TestCase):
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
651 thetype = frozenset
49730
c707da343557 Issue #1721812: Binary operations and copy operations on set/frozenset
Raymond Hettinger <python@rcn.com>
parents: 49518
diff changeset
652 basetype = frozenset
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
653
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30929
diff changeset
654 def test_init(self):
31004
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
655 s = self.thetype(self.word)
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
656 s.__init__(self.otherword)
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
657 self.assertEqual(s, set(self.word))
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
658
35297
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35206
diff changeset
659 def test_singleton_empty_frozenset(self):
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35206
diff changeset
660 f = frozenset()
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35206
diff changeset
661 efs = [frozenset(), frozenset([]), frozenset(()), frozenset(''),
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35206
diff changeset
662 frozenset(), frozenset([]), frozenset(()), frozenset(''),
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41575
diff changeset
663 frozenset(range(0)), frozenset(frozenset()),
35297
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35206
diff changeset
664 frozenset(f), f]
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35206
diff changeset
665 # All of the empty frozensets should have just one id()
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35206
diff changeset
666 self.assertEqual(len(set(map(id, efs))), 1)
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35206
diff changeset
667
31004
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
668 def test_constructor_identity(self):
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
669 s = self.thetype(range(3))
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
670 t = self.thetype(s)
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
671 self.assertEqual(id(s), id(t))
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30929
diff changeset
672
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
673 def test_hash(self):
31004
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
674 self.assertEqual(hash(self.thetype('abcdeb')),
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
675 hash(self.thetype('ebecda')))
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
676
35206
89710805bc3d Add test for hash commutativity.
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
677 # make sure that all permutations give the same hash value
89710805bc3d Add test for hash commutativity.
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
678 n = 100
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41575
diff changeset
679 seq = [randrange(n) for i in range(n)]
35206
89710805bc3d Add test for hash commutativity.
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
680 results = set()
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41575
diff changeset
681 for i in range(200):
35206
89710805bc3d Add test for hash commutativity.
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
682 shuffle(seq)
89710805bc3d Add test for hash commutativity.
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
683 results.add(hash(self.thetype(seq)))
89710805bc3d Add test for hash commutativity.
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
684 self.assertEqual(len(results), 1)
89710805bc3d Add test for hash commutativity.
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
685
31004
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
686 def test_copy(self):
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
687 dup = self.s.copy()
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
688 self.assertEqual(id(self.s), id(dup))
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
689
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
690 def test_frozen_as_dictkey(self):
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41575
diff changeset
691 seq = list(range(10)) + list('abcdefg') + ['apple']
31004
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
692 key1 = self.thetype(seq)
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
693 key2 = self.thetype(reversed(seq))
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
694 self.assertEqual(key1, key2)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
695 self.assertNotEqual(id(key1), id(key2))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
696 d = {}
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
697 d[key1] = 42
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
698 self.assertEqual(d[key2], 42)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
699
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
700 def test_hash_caching(self):
31004
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
701 f = self.thetype('abcdcda')
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
702 self.assertEqual(hash(f), hash(f))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
703
31004
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
704 def test_hash_effectiveness(self):
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
705 n = 13
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
706 hashvalues = set()
31291
868cace2219f Strengthen the test for hash effectiveness
Raymond Hettinger <python@rcn.com>
parents: 31210
diff changeset
707 addhashvalue = hashvalues.add
868cace2219f Strengthen the test for hash effectiveness
Raymond Hettinger <python@rcn.com>
parents: 31210
diff changeset
708 elemmasks = [(i+1, 1<<i) for i in range(n)]
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41575
diff changeset
709 for i in range(2**n):
31291
868cace2219f Strengthen the test for hash effectiveness
Raymond Hettinger <python@rcn.com>
parents: 31210
diff changeset
710 addhashvalue(hash(frozenset([e for e, m in elemmasks if m&i])))
868cace2219f Strengthen the test for hash effectiveness
Raymond Hettinger <python@rcn.com>
parents: 31210
diff changeset
711 self.assertEqual(len(hashvalues), 2**n)
31004
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
712
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30929
diff changeset
713 class FrozenSetSubclass(frozenset):
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30929
diff changeset
714 pass
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30929
diff changeset
715
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30929
diff changeset
716 class TestFrozenSetSubclass(TestFrozenSet):
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30929
diff changeset
717 thetype = FrozenSetSubclass
49730
c707da343557 Issue #1721812: Binary operations and copy operations on set/frozenset
Raymond Hettinger <python@rcn.com>
parents: 49518
diff changeset
718 basetype = frozenset
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30929
diff changeset
719
31004
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
720 def test_constructor_identity(self):
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
721 s = self.thetype(range(3))
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
722 t = self.thetype(s)
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
723 self.assertNotEqual(id(s), id(t))
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
724
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
725 def test_copy(self):
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
726 dup = self.s.copy()
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
727 self.assertNotEqual(id(self.s), id(dup))
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
728
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
729 def test_nested_empty_constructor(self):
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
730 s = self.thetype()
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
731 t = self.thetype(s)
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
732 self.assertEqual(s, t)
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
733
35297
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35206
diff changeset
734 def test_singleton_empty_frozenset(self):
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35206
diff changeset
735 Frozenset = self.thetype
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35206
diff changeset
736 f = frozenset()
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35206
diff changeset
737 F = Frozenset()
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35206
diff changeset
738 efs = [Frozenset(), Frozenset([]), Frozenset(()), Frozenset(''),
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35206
diff changeset
739 Frozenset(), Frozenset([]), Frozenset(()), Frozenset(''),
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41575
diff changeset
740 Frozenset(range(0)), Frozenset(Frozenset()),
35297
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35206
diff changeset
741 Frozenset(frozenset()), f, F, Frozenset(f), Frozenset(F)]
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35206
diff changeset
742 # All empty frozenset subclass instances should have different ids
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35206
diff changeset
743 self.assertEqual(len(set(map(id, efs))), len(efs))
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35206
diff changeset
744
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
745 # Tests taken from test_sets.py =============================================
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
746
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
747 empty_set = set()
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
748
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
749 #==============================================================================
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
750
83449
ef100be2e8ba #17790: test_set now works with unittest test discovery. Patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 76091
diff changeset
751 class TestBasicOps:
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
752
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
753 def test_repr(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
754 if self.repr is not None:
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 31389
diff changeset
755 self.assertEqual(repr(self.set), self.repr)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
756
75066
f4b7ecf8a5f8 Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents: 68860
diff changeset
757 def check_repr_against_values(self):
f4b7ecf8a5f8 Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents: 68860
diff changeset
758 text = repr(self.set)
f4b7ecf8a5f8 Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents: 68860
diff changeset
759 self.assertTrue(text.startswith('{'))
f4b7ecf8a5f8 Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents: 68860
diff changeset
760 self.assertTrue(text.endswith('}'))
f4b7ecf8a5f8 Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents: 68860
diff changeset
761
f4b7ecf8a5f8 Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents: 68860
diff changeset
762 result = text[1:-1].split(', ')
f4b7ecf8a5f8 Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents: 68860
diff changeset
763 result.sort()
f4b7ecf8a5f8 Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents: 68860
diff changeset
764 sorted_repr_values = [repr(value) for value in self.values]
f4b7ecf8a5f8 Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents: 68860
diff changeset
765 sorted_repr_values.sort()
f4b7ecf8a5f8 Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents: 68860
diff changeset
766 self.assertEqual(result, sorted_repr_values)
f4b7ecf8a5f8 Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents: 68860
diff changeset
767
32555
d909a0ddd193 * fix the print test
Raymond Hettinger <python@rcn.com>
parents: 32158
diff changeset
768 def test_print(self):
d909a0ddd193 * fix the print test
Raymond Hettinger <python@rcn.com>
parents: 32158
diff changeset
769 try:
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 46084
diff changeset
770 fo = open(support.TESTFN, "w")
40789
e8b2913f9297 Some more tests pass now. (Also test_compiler.py with -u all.)
Guido van Rossum <guido@python.org>
parents: 40742
diff changeset
771 fo.write(str(self.set))
32555
d909a0ddd193 * fix the print test
Raymond Hettinger <python@rcn.com>
parents: 32158
diff changeset
772 fo.close()
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 46084
diff changeset
773 fo = open(support.TESTFN, "r")
32555
d909a0ddd193 * fix the print test
Raymond Hettinger <python@rcn.com>
parents: 32158
diff changeset
774 self.assertEqual(fo.read(), repr(self.set))
d909a0ddd193 * fix the print test
Raymond Hettinger <python@rcn.com>
parents: 32158
diff changeset
775 finally:
d909a0ddd193 * fix the print test
Raymond Hettinger <python@rcn.com>
parents: 32158
diff changeset
776 fo.close()
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 46084
diff changeset
777 support.unlink(support.TESTFN)
32555
d909a0ddd193 * fix the print test
Raymond Hettinger <python@rcn.com>
parents: 32158
diff changeset
778
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
779 def test_length(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
780 self.assertEqual(len(self.set), self.length)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
781
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
782 def test_self_equality(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
783 self.assertEqual(self.set, self.set)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
784
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
785 def test_equivalent_equality(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
786 self.assertEqual(self.set, self.dup)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
787
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
788 def test_copy(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
789 self.assertEqual(self.set.copy(), self.dup)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
790
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
791 def test_self_union(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
792 result = self.set | self.set
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
793 self.assertEqual(result, self.dup)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
794
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
795 def test_empty_union(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
796 result = self.set | empty_set
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
797 self.assertEqual(result, self.dup)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
798
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
799 def test_union_empty(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
800 result = empty_set | self.set
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
801 self.assertEqual(result, self.dup)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
802
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
803 def test_self_intersection(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
804 result = self.set & self.set
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
805 self.assertEqual(result, self.dup)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
806
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
807 def test_empty_intersection(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
808 result = self.set & empty_set
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
809 self.assertEqual(result, empty_set)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
810
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
811 def test_intersection_empty(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
812 result = empty_set & self.set
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
813 self.assertEqual(result, empty_set)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
814
43815
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43604
diff changeset
815 def test_self_isdisjoint(self):
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43604
diff changeset
816 result = self.set.isdisjoint(self.set)
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43604
diff changeset
817 self.assertEqual(result, not self.set)
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43604
diff changeset
818
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43604
diff changeset
819 def test_empty_isdisjoint(self):
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43604
diff changeset
820 result = self.set.isdisjoint(empty_set)
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43604
diff changeset
821 self.assertEqual(result, True)
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43604
diff changeset
822
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43604
diff changeset
823 def test_isdisjoint_empty(self):
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43604
diff changeset
824 result = empty_set.isdisjoint(self.set)
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43604
diff changeset
825 self.assertEqual(result, True)
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43604
diff changeset
826
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
827 def test_self_symmetric_difference(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
828 result = self.set ^ self.set
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
829 self.assertEqual(result, empty_set)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
830
63441
bc66e1566a19 Merged revisions 83385-83389,83391 via svnmerge from
Georg Brandl <georg@python.org>
parents: 60711
diff changeset
831 def test_empty_symmetric_difference(self):
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
832 result = self.set ^ empty_set
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
833 self.assertEqual(result, self.set)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
834
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
835 def test_self_difference(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
836 result = self.set - self.set
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
837 self.assertEqual(result, empty_set)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
838
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
839 def test_empty_difference(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
840 result = self.set - empty_set
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
841 self.assertEqual(result, self.dup)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
842
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
843 def test_empty_difference_rev(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
844 result = empty_set - self.set
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
845 self.assertEqual(result, empty_set)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
846
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
847 def test_iteration(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
848 for v in self.set:
58415
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 55466
diff changeset
849 self.assertIn(v, self.values)
35959
fca7337710fd Improve test coverage. Hope the test_file changes work the same on windows.
Neal Norwitz <nnorwitz@gmail.com>
parents: 35581
diff changeset
850 setiter = iter(self.set)
36484
20f75d5e79e9 Renamed _length_cue() to __length_hint__(). See:
Armin Rigo <arigo@tunes.org>
parents: 35959
diff changeset
851 self.assertEqual(setiter.__length_hint__(), len(self.set))
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
852
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
853 def test_pickling(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
854 p = pickle.dumps(self.set)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
855 copy = pickle.loads(p)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
856 self.assertEqual(self.set, copy,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
857 "%s != %s" % (self.set, copy))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
858
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
859 #------------------------------------------------------------------------------
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
860
83449
ef100be2e8ba #17790: test_set now works with unittest test discovery. Patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 76091
diff changeset
861 class TestBasicOpsEmpty(TestBasicOps, unittest.TestCase):
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
862 def setUp(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
863 self.case = "empty set"
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
864 self.values = []
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
865 self.set = set(self.values)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
866 self.dup = set(self.values)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
867 self.length = 0
39696
d14f10a4d9d4 Fix str() and repr() of empty sets.
Georg Brandl <georg@python.org>
parents: 39693
diff changeset
868 self.repr = "set()"
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
869
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
870 #------------------------------------------------------------------------------
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
871
83449
ef100be2e8ba #17790: test_set now works with unittest test discovery. Patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 76091
diff changeset
872 class TestBasicOpsSingleton(TestBasicOps, unittest.TestCase):
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
873 def setUp(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
874 self.case = "unit set (number)"
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
875 self.values = [3]
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
876 self.set = set(self.values)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
877 self.dup = set(self.values)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
878 self.length = 1
39693
d0b83c4630c0 SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents: 39644
diff changeset
879 self.repr = "{3}"
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
880
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
881 def test_in(self):
58415
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 55466
diff changeset
882 self.assertIn(3, self.set)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
883
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
884 def test_not_in(self):
58415
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 55466
diff changeset
885 self.assertNotIn(2, self.set)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
886
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
887 #------------------------------------------------------------------------------
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
888
83449
ef100be2e8ba #17790: test_set now works with unittest test discovery. Patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 76091
diff changeset
889 class TestBasicOpsTuple(TestBasicOps, unittest.TestCase):
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
890 def setUp(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
891 self.case = "unit set (tuple)"
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
892 self.values = [(0, "zero")]
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
893 self.set = set(self.values)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
894 self.dup = set(self.values)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
895 self.length = 1
39693
d0b83c4630c0 SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents: 39644
diff changeset
896 self.repr = "{(0, 'zero')}"
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
897
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
898 def test_in(self):
58415
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 55466
diff changeset
899 self.assertIn((0, "zero"), self.set)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
900
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
901 def test_not_in(self):
58415
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 55466
diff changeset
902 self.assertNotIn(9, self.set)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
903
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
904 #------------------------------------------------------------------------------
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
905
83449
ef100be2e8ba #17790: test_set now works with unittest test discovery. Patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 76091
diff changeset
906 class TestBasicOpsTriple(TestBasicOps, unittest.TestCase):
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
907 def setUp(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
908 self.case = "triple set"
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
909 self.values = [0, "zero", operator.add]
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
910 self.set = set(self.values)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
911 self.dup = set(self.values)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
912 self.length = 3
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
913 self.repr = None
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
914
44252
444da08f78a4 Fixed issue #1564: The set implementation should special-case PyUnicode instead of PyString
Christian Heimes <christian@cheimes.de>
parents: 43815
diff changeset
915 #------------------------------------------------------------------------------
444da08f78a4 Fixed issue #1564: The set implementation should special-case PyUnicode instead of PyString
Christian Heimes <christian@cheimes.de>
parents: 43815
diff changeset
916
83449
ef100be2e8ba #17790: test_set now works with unittest test discovery. Patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 76091
diff changeset
917 class TestBasicOpsString(TestBasicOps, unittest.TestCase):
44252
444da08f78a4 Fixed issue #1564: The set implementation should special-case PyUnicode instead of PyString
Christian Heimes <christian@cheimes.de>
parents: 43815
diff changeset
918 def setUp(self):
444da08f78a4 Fixed issue #1564: The set implementation should special-case PyUnicode instead of PyString
Christian Heimes <christian@cheimes.de>
parents: 43815
diff changeset
919 self.case = "string set"
444da08f78a4 Fixed issue #1564: The set implementation should special-case PyUnicode instead of PyString
Christian Heimes <christian@cheimes.de>
parents: 43815
diff changeset
920 self.values = ["a", "b", "c"]
444da08f78a4 Fixed issue #1564: The set implementation should special-case PyUnicode instead of PyString
Christian Heimes <christian@cheimes.de>
parents: 43815
diff changeset
921 self.set = set(self.values)
444da08f78a4 Fixed issue #1564: The set implementation should special-case PyUnicode instead of PyString
Christian Heimes <christian@cheimes.de>
parents: 43815
diff changeset
922 self.dup = set(self.values)
444da08f78a4 Fixed issue #1564: The set implementation should special-case PyUnicode instead of PyString
Christian Heimes <christian@cheimes.de>
parents: 43815
diff changeset
923 self.length = 3
75066
f4b7ecf8a5f8 Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents: 68860
diff changeset
924
f4b7ecf8a5f8 Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents: 68860
diff changeset
925 def test_repr(self):
f4b7ecf8a5f8 Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents: 68860
diff changeset
926 self.check_repr_against_values()
44252
444da08f78a4 Fixed issue #1564: The set implementation should special-case PyUnicode instead of PyString
Christian Heimes <christian@cheimes.de>
parents: 43815
diff changeset
927
444da08f78a4 Fixed issue #1564: The set implementation should special-case PyUnicode instead of PyString
Christian Heimes <christian@cheimes.de>
parents: 43815
diff changeset
928 #------------------------------------------------------------------------------
444da08f78a4 Fixed issue #1564: The set implementation should special-case PyUnicode instead of PyString
Christian Heimes <christian@cheimes.de>
parents: 43815
diff changeset
929
83449
ef100be2e8ba #17790: test_set now works with unittest test discovery. Patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 76091
diff changeset
930 class TestBasicOpsBytes(TestBasicOps, unittest.TestCase):
44252
444da08f78a4 Fixed issue #1564: The set implementation should special-case PyUnicode instead of PyString
Christian Heimes <christian@cheimes.de>
parents: 43815
diff changeset
931 def setUp(self):
444da08f78a4 Fixed issue #1564: The set implementation should special-case PyUnicode instead of PyString
Christian Heimes <christian@cheimes.de>
parents: 43815
diff changeset
932 self.case = "string set"
444da08f78a4 Fixed issue #1564: The set implementation should special-case PyUnicode instead of PyString
Christian Heimes <christian@cheimes.de>
parents: 43815
diff changeset
933 self.values = [b"a", b"b", b"c"]
444da08f78a4 Fixed issue #1564: The set implementation should special-case PyUnicode instead of PyString
Christian Heimes <christian@cheimes.de>
parents: 43815
diff changeset
934 self.set = set(self.values)
444da08f78a4 Fixed issue #1564: The set implementation should special-case PyUnicode instead of PyString
Christian Heimes <christian@cheimes.de>
parents: 43815
diff changeset
935 self.dup = set(self.values)
444da08f78a4 Fixed issue #1564: The set implementation should special-case PyUnicode instead of PyString
Christian Heimes <christian@cheimes.de>
parents: 43815
diff changeset
936 self.length = 3
75066
f4b7ecf8a5f8 Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents: 68860
diff changeset
937
f4b7ecf8a5f8 Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents: 68860
diff changeset
938 def test_repr(self):
f4b7ecf8a5f8 Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents: 68860
diff changeset
939 self.check_repr_against_values()
44252
444da08f78a4 Fixed issue #1564: The set implementation should special-case PyUnicode instead of PyString
Christian Heimes <christian@cheimes.de>
parents: 43815
diff changeset
940
444da08f78a4 Fixed issue #1564: The set implementation should special-case PyUnicode instead of PyString
Christian Heimes <christian@cheimes.de>
parents: 43815
diff changeset
941 #------------------------------------------------------------------------------
444da08f78a4 Fixed issue #1564: The set implementation should special-case PyUnicode instead of PyString
Christian Heimes <christian@cheimes.de>
parents: 43815
diff changeset
942
83449
ef100be2e8ba #17790: test_set now works with unittest test discovery. Patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 76091
diff changeset
943 class TestBasicOpsMixedStringBytes(TestBasicOps, unittest.TestCase):
44252
444da08f78a4 Fixed issue #1564: The set implementation should special-case PyUnicode instead of PyString
Christian Heimes <christian@cheimes.de>
parents: 43815
diff changeset
944 def setUp(self):
59772
ae9e137411f1 Clean up the warnings filter use in test_set.
Brett Cannon <bcannon@gmail.com>
parents: 59615
diff changeset
945 self._warning_filters = support.check_warnings()
ae9e137411f1 Clean up the warnings filter use in test_set.
Brett Cannon <bcannon@gmail.com>
parents: 59615
diff changeset
946 self._warning_filters.__enter__()
44252
444da08f78a4 Fixed issue #1564: The set implementation should special-case PyUnicode instead of PyString
Christian Heimes <christian@cheimes.de>
parents: 43815
diff changeset
947 warnings.simplefilter('ignore', BytesWarning)
444da08f78a4 Fixed issue #1564: The set implementation should special-case PyUnicode instead of PyString
Christian Heimes <christian@cheimes.de>
parents: 43815
diff changeset
948 self.case = "string and bytes set"
444da08f78a4 Fixed issue #1564: The set implementation should special-case PyUnicode instead of PyString
Christian Heimes <christian@cheimes.de>
parents: 43815
diff changeset
949 self.values = ["a", "b", b"a", b"b"]
444da08f78a4 Fixed issue #1564: The set implementation should special-case PyUnicode instead of PyString
Christian Heimes <christian@cheimes.de>
parents: 43815
diff changeset
950 self.set = set(self.values)
444da08f78a4 Fixed issue #1564: The set implementation should special-case PyUnicode instead of PyString
Christian Heimes <christian@cheimes.de>
parents: 43815
diff changeset
951 self.dup = set(self.values)
444da08f78a4 Fixed issue #1564: The set implementation should special-case PyUnicode instead of PyString
Christian Heimes <christian@cheimes.de>
parents: 43815
diff changeset
952 self.length = 4
444da08f78a4 Fixed issue #1564: The set implementation should special-case PyUnicode instead of PyString
Christian Heimes <christian@cheimes.de>
parents: 43815
diff changeset
953
444da08f78a4 Fixed issue #1564: The set implementation should special-case PyUnicode instead of PyString
Christian Heimes <christian@cheimes.de>
parents: 43815
diff changeset
954 def tearDown(self):
59772
ae9e137411f1 Clean up the warnings filter use in test_set.
Brett Cannon <bcannon@gmail.com>
parents: 59615
diff changeset
955 self._warning_filters.__exit__(None, None, None)
44252
444da08f78a4 Fixed issue #1564: The set implementation should special-case PyUnicode instead of PyString
Christian Heimes <christian@cheimes.de>
parents: 43815
diff changeset
956
75066
f4b7ecf8a5f8 Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents: 68860
diff changeset
957 def test_repr(self):
f4b7ecf8a5f8 Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents: 68860
diff changeset
958 self.check_repr_against_values()
f4b7ecf8a5f8 Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents: 68860
diff changeset
959
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
960 #==============================================================================
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
961
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
962 def baditer():
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
963 raise TypeError
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
964 yield True
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
965
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
966 def gooditer():
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
967 yield True
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
968
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
969 class TestExceptionPropagation(unittest.TestCase):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
970 """SF 628246: Set constructor should not trap iterator TypeErrors"""
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
971
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
972 def test_instanceWithException(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
973 self.assertRaises(TypeError, set, baditer())
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
974
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
975 def test_instancesWithoutException(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
976 # All of these iterables should load without exception.
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
977 set([1,2,3])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
978 set((1,2,3))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
979 set({'one':1, 'two':2, 'three':3})
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41575
diff changeset
980 set(range(3))
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
981 set('abc')
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
982 set(gooditer())
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
983
35959
fca7337710fd Improve test coverage. Hope the test_file changes work the same on windows.
Neal Norwitz <nnorwitz@gmail.com>
parents: 35581
diff changeset
984 def test_changingSizeWhileIterating(self):
fca7337710fd Improve test coverage. Hope the test_file changes work the same on windows.
Neal Norwitz <nnorwitz@gmail.com>
parents: 35581
diff changeset
985 s = set([1,2,3])
fca7337710fd Improve test coverage. Hope the test_file changes work the same on windows.
Neal Norwitz <nnorwitz@gmail.com>
parents: 35581
diff changeset
986 try:
fca7337710fd Improve test coverage. Hope the test_file changes work the same on windows.
Neal Norwitz <nnorwitz@gmail.com>
parents: 35581
diff changeset
987 for i in s:
fca7337710fd Improve test coverage. Hope the test_file changes work the same on windows.
Neal Norwitz <nnorwitz@gmail.com>
parents: 35581
diff changeset
988 s.update([4])
fca7337710fd Improve test coverage. Hope the test_file changes work the same on windows.
Neal Norwitz <nnorwitz@gmail.com>
parents: 35581
diff changeset
989 except RuntimeError:
fca7337710fd Improve test coverage. Hope the test_file changes work the same on windows.
Neal Norwitz <nnorwitz@gmail.com>
parents: 35581
diff changeset
990 pass
fca7337710fd Improve test coverage. Hope the test_file changes work the same on windows.
Neal Norwitz <nnorwitz@gmail.com>
parents: 35581
diff changeset
991 else:
fca7337710fd Improve test coverage. Hope the test_file changes work the same on windows.
Neal Norwitz <nnorwitz@gmail.com>
parents: 35581
diff changeset
992 self.fail("no exception when changing size during iteration")
fca7337710fd Improve test coverage. Hope the test_file changes work the same on windows.
Neal Norwitz <nnorwitz@gmail.com>
parents: 35581
diff changeset
993
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
994 #==============================================================================
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
995
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
996 class TestSetOfSets(unittest.TestCase):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
997 def test_constructor(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
998 inner = frozenset([1])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
999 outer = set([inner])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1000 element = outer.pop()
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1001 self.assertEqual(type(element), frozenset)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1002 outer.add(inner) # Rebuild set of sets with .add method
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1003 outer.remove(inner)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1004 self.assertEqual(outer, set()) # Verify that remove worked
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1005 outer.discard(inner) # Absence of KeyError indicates working fine
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1006
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1007 #==============================================================================
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1008
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1009 class TestBinaryOps(unittest.TestCase):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1010 def setUp(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1011 self.set = set((2, 4, 6))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1012
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1013 def test_eq(self): # SF bug 643115
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1014 self.assertEqual(self.set, set({2:1,4:3,6:5}))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1015
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1016 def test_union_subset(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1017 result = self.set | set([2])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1018 self.assertEqual(result, set((2, 4, 6)))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1019
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1020 def test_union_superset(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1021 result = self.set | set([2, 4, 6, 8])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1022 self.assertEqual(result, set([2, 4, 6, 8]))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1023
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1024 def test_union_overlap(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1025 result = self.set | set([3, 4, 5])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1026 self.assertEqual(result, set([2, 3, 4, 5, 6]))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1027
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1028 def test_union_non_overlap(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1029 result = self.set | set([8])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1030 self.assertEqual(result, set([2, 4, 6, 8]))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1031
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1032 def test_intersection_subset(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1033 result = self.set & set((2, 4))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1034 self.assertEqual(result, set((2, 4)))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1035
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1036 def test_intersection_superset(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1037 result = self.set & set([2, 4, 6, 8])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1038 self.assertEqual(result, set([2, 4, 6]))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1039
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1040 def test_intersection_overlap(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1041 result = self.set & set([3, 4, 5])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1042 self.assertEqual(result, set([4]))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1043
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1044 def test_intersection_non_overlap(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1045 result = self.set & set([8])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1046 self.assertEqual(result, empty_set)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1047
43815
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43604
diff changeset
1048 def test_isdisjoint_subset(self):
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43604
diff changeset
1049 result = self.set.isdisjoint(set((2, 4)))
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43604
diff changeset
1050 self.assertEqual(result, False)
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43604
diff changeset
1051
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43604
diff changeset
1052 def test_isdisjoint_superset(self):
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43604
diff changeset
1053 result = self.set.isdisjoint(set([2, 4, 6, 8]))
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43604
diff changeset
1054 self.assertEqual(result, False)
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43604
diff changeset
1055
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43604
diff changeset
1056 def test_isdisjoint_overlap(self):
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43604
diff changeset
1057 result = self.set.isdisjoint(set([3, 4, 5]))
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43604
diff changeset
1058 self.assertEqual(result, False)
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43604
diff changeset
1059
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43604
diff changeset
1060 def test_isdisjoint_non_overlap(self):
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43604
diff changeset
1061 result = self.set.isdisjoint(set([8]))
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43604
diff changeset
1062 self.assertEqual(result, True)
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43604
diff changeset
1063
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1064 def test_sym_difference_subset(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1065 result = self.set ^ set((2, 4))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1066 self.assertEqual(result, set([6]))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1067
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1068 def test_sym_difference_superset(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1069 result = self.set ^ set((2, 4, 6, 8))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1070 self.assertEqual(result, set([8]))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1071
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1072 def test_sym_difference_overlap(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1073 result = self.set ^ set((3, 4, 5))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1074 self.assertEqual(result, set([2, 3, 5, 6]))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1075
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1076 def test_sym_difference_non_overlap(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1077 result = self.set ^ set([8])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1078 self.assertEqual(result, set([2, 4, 6, 8]))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1079
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1080 #==============================================================================
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1081
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1082 class TestUpdateOps(unittest.TestCase):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1083 def setUp(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1084 self.set = set((2, 4, 6))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1085
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1086 def test_union_subset(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1087 self.set |= set([2])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1088 self.assertEqual(self.set, set((2, 4, 6)))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1089
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1090 def test_union_superset(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1091 self.set |= set([2, 4, 6, 8])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1092 self.assertEqual(self.set, set([2, 4, 6, 8]))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1093
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1094 def test_union_overlap(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1095 self.set |= set([3, 4, 5])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1096 self.assertEqual(self.set, set([2, 3, 4, 5, 6]))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1097
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1098 def test_union_non_overlap(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1099 self.set |= set([8])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1100 self.assertEqual(self.set, set([2, 4, 6, 8]))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1101
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1102 def test_union_method_call(self):
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
1103 self.set.update(set([3, 4, 5]))
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1104 self.assertEqual(self.set, set([2, 3, 4, 5, 6]))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1105
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1106 def test_intersection_subset(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1107 self.set &= set((2, 4))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1108 self.assertEqual(self.set, set((2, 4)))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1109
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1110 def test_intersection_superset(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1111 self.set &= set([2, 4, 6, 8])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1112 self.assertEqual(self.set, set([2, 4, 6]))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1113
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1114 def test_intersection_overlap(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1115 self.set &= set([3, 4, 5])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1116 self.assertEqual(self.set, set([4]))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1117
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1118 def test_intersection_non_overlap(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1119 self.set &= set([8])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1120 self.assertEqual(self.set, empty_set)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1121
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1122 def test_intersection_method_call(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1123 self.set.intersection_update(set([3, 4, 5]))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1124 self.assertEqual(self.set, set([4]))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1125
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1126 def test_sym_difference_subset(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1127 self.set ^= set((2, 4))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1128 self.assertEqual(self.set, set([6]))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1129
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1130 def test_sym_difference_superset(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1131 self.set ^= set((2, 4, 6, 8))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1132 self.assertEqual(self.set, set([8]))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1133
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1134 def test_sym_difference_overlap(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1135 self.set ^= set((3, 4, 5))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1136 self.assertEqual(self.set, set([2, 3, 5, 6]))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1137
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1138 def test_sym_difference_non_overlap(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1139 self.set ^= set([8])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1140 self.assertEqual(self.set, set([2, 4, 6, 8]))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1141
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1142 def test_sym_difference_method_call(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1143 self.set.symmetric_difference_update(set([3, 4, 5]))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1144 self.assertEqual(self.set, set([2, 3, 5, 6]))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1145
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1146 def test_difference_subset(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1147 self.set -= set((2, 4))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1148 self.assertEqual(self.set, set([6]))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1149
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1150 def test_difference_superset(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1151 self.set -= set((2, 4, 6, 8))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1152 self.assertEqual(self.set, set([]))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1153
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1154 def test_difference_overlap(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1155 self.set -= set((3, 4, 5))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1156 self.assertEqual(self.set, set([2, 6]))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1157
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1158 def test_difference_non_overlap(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1159 self.set -= set([8])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1160 self.assertEqual(self.set, set([2, 4, 6]))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1161
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1162 def test_difference_method_call(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1163 self.set.difference_update(set([3, 4, 5]))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1164 self.assertEqual(self.set, set([2, 6]))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1165
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1166 #==============================================================================
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1167
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1168 class TestMutate(unittest.TestCase):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1169 def setUp(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1170 self.values = ["a", "b", "c"]
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1171 self.set = set(self.values)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1172
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1173 def test_add_present(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1174 self.set.add("c")
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1175 self.assertEqual(self.set, set("abc"))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1176
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1177 def test_add_absent(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1178 self.set.add("d")
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1179 self.assertEqual(self.set, set("abcd"))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1180
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1181 def test_add_until_full(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1182 tmp = set()
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1183 expected_len = 0
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1184 for v in self.values:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1185 tmp.add(v)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1186 expected_len += 1
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1187 self.assertEqual(len(tmp), expected_len)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1188 self.assertEqual(tmp, self.set)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1189
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1190 def test_remove_present(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1191 self.set.remove("b")
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1192 self.assertEqual(self.set, set("ac"))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1193
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1194 def test_remove_absent(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1195 try:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1196 self.set.remove("d")
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1197 self.fail("Removing missing element should have raised LookupError")
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1198 except LookupError:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1199 pass
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1200
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1201 def test_remove_until_empty(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1202 expected_len = len(self.set)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1203 for v in self.values:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1204 self.set.remove(v)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1205 expected_len -= 1
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1206 self.assertEqual(len(self.set), expected_len)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1207
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1208 def test_discard_present(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1209 self.set.discard("c")
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1210 self.assertEqual(self.set, set("ab"))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1211
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1212 def test_discard_absent(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1213 self.set.discard("d")
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1214 self.assertEqual(self.set, set("abc"))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1215
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1216 def test_clear(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1217 self.set.clear()
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1218 self.assertEqual(len(self.set), 0)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1219
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1220 def test_pop(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1221 popped = {}
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1222 while self.set:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1223 popped[self.set.pop()] = None
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1224 self.assertEqual(len(popped), len(self.values))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1225 for v in self.values:
58415
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 55466
diff changeset
1226 self.assertIn(v, popped)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1227
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1228 def test_update_empty_tuple(self):
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
1229 self.set.update(())
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1230 self.assertEqual(self.set, set(self.values))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1231
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1232 def test_update_unit_tuple_overlap(self):
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
1233 self.set.update(("a",))
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1234 self.assertEqual(self.set, set(self.values))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1235
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1236 def test_update_unit_tuple_non_overlap(self):
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
1237 self.set.update(("a", "z"))
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1238 self.assertEqual(self.set, set(self.values + ["z"]))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1239
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1240 #==============================================================================
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1241
83449
ef100be2e8ba #17790: test_set now works with unittest test discovery. Patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 76091
diff changeset
1242 class TestSubsets:
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1243
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1244 case2method = {"<=": "issubset",
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1245 ">=": "issuperset",
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1246 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1247
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1248 reverse = {"==": "==",
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1249 "!=": "!=",
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1250 "<": ">",
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1251 ">": "<",
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1252 "<=": ">=",
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1253 ">=": "<=",
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1254 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1255
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1256 def test_issubset(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1257 x = self.left
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1258 y = self.right
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1259 for case in "!=", "==", "<", "<=", ">", ">=":
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1260 expected = case in self.cases
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1261 # Test the binary infix spelling.
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1262 result = eval("x" + case + "y", locals())
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1263 self.assertEqual(result, expected)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1264 # Test the "friendly" method-name spelling, if one exists.
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1265 if case in TestSubsets.case2method:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1266 method = getattr(x, TestSubsets.case2method[case])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1267 result = method(y)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1268 self.assertEqual(result, expected)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1269
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1270 # Now do the same for the operands reversed.
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1271 rcase = TestSubsets.reverse[case]
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1272 result = eval("y" + rcase + "x", locals())
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1273 self.assertEqual(result, expected)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1274 if rcase in TestSubsets.case2method:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1275 method = getattr(y, TestSubsets.case2method[rcase])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1276 result = method(x)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1277 self.assertEqual(result, expected)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1278 #------------------------------------------------------------------------------
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1279
83449
ef100be2e8ba #17790: test_set now works with unittest test discovery. Patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 76091
diff changeset
1280 class TestSubsetEqualEmpty(TestSubsets, unittest.TestCase):
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1281 left = set()
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1282 right = set()
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1283 name = "both empty"
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1284 cases = "==", "<=", ">="
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1285
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1286 #------------------------------------------------------------------------------
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1287
83449
ef100be2e8ba #17790: test_set now works with unittest test discovery. Patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 76091
diff changeset
1288 class TestSubsetEqualNonEmpty(TestSubsets, unittest.TestCase):
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1289 left = set([1, 2])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1290 right = set([1, 2])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1291 name = "equal pair"
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1292 cases = "==", "<=", ">="
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1293
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1294 #------------------------------------------------------------------------------
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1295
83449
ef100be2e8ba #17790: test_set now works with unittest test discovery. Patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 76091
diff changeset
1296 class TestSubsetEmptyNonEmpty(TestSubsets, unittest.TestCase):
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1297 left = set()
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1298 right = set([1, 2])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1299 name = "one empty, one non-empty"
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1300 cases = "!=", "<", "<="
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1301
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1302 #------------------------------------------------------------------------------
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1303
83449
ef100be2e8ba #17790: test_set now works with unittest test discovery. Patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 76091
diff changeset
1304 class TestSubsetPartial(TestSubsets, unittest.TestCase):
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1305 left = set([1])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1306 right = set([1, 2])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1307 name = "one a non-empty proper subset of other"
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1308 cases = "!=", "<", "<="
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1309
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1310 #------------------------------------------------------------------------------
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1311
83449
ef100be2e8ba #17790: test_set now works with unittest test discovery. Patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 76091
diff changeset
1312 class TestSubsetNonOverlap(TestSubsets, unittest.TestCase):
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1313 left = set([1])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1314 right = set([2])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1315 name = "neither empty, neither contains"
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1316 cases = "!="
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1317
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1318 #==============================================================================
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1319
83449
ef100be2e8ba #17790: test_set now works with unittest test discovery. Patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 76091
diff changeset
1320 class TestOnlySetsInBinaryOps:
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1321
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1322 def test_eq_ne(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1323 # Unlike the others, this is testing that == and != *are* allowed.
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1324 self.assertEqual(self.other == self.set, False)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1325 self.assertEqual(self.set == self.other, False)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1326 self.assertEqual(self.other != self.set, True)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1327 self.assertEqual(self.set != self.other, True)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1328
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1329 def test_ge_gt_le_lt(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1330 self.assertRaises(TypeError, lambda: self.set < self.other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1331 self.assertRaises(TypeError, lambda: self.set <= self.other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1332 self.assertRaises(TypeError, lambda: self.set > self.other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1333 self.assertRaises(TypeError, lambda: self.set >= self.other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1334
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1335 self.assertRaises(TypeError, lambda: self.other < self.set)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1336 self.assertRaises(TypeError, lambda: self.other <= self.set)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1337 self.assertRaises(TypeError, lambda: self.other > self.set)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1338 self.assertRaises(TypeError, lambda: self.other >= self.set)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1339
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
1340 def test_update_operator(self):
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1341 try:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1342 self.set |= self.other
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1343 except TypeError:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1344 pass
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1345 else:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1346 self.fail("expected TypeError")
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1347
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
1348 def test_update(self):
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1349 if self.otherIsIterable:
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
1350 self.set.update(self.other)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1351 else:
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
1352 self.assertRaises(TypeError, self.set.update, self.other)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1353
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1354 def test_union(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1355 self.assertRaises(TypeError, lambda: self.set | self.other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1356 self.assertRaises(TypeError, lambda: self.other | self.set)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1357 if self.otherIsIterable:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1358 self.set.union(self.other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1359 else:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1360 self.assertRaises(TypeError, self.set.union, self.other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1361
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1362 def test_intersection_update_operator(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1363 try:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1364 self.set &= self.other
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1365 except TypeError:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1366 pass
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1367 else:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1368 self.fail("expected TypeError")
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1369
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1370 def test_intersection_update(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1371 if self.otherIsIterable:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1372 self.set.intersection_update(self.other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1373 else:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1374 self.assertRaises(TypeError,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1375 self.set.intersection_update,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1376 self.other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1377
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1378 def test_intersection(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1379 self.assertRaises(TypeError, lambda: self.set & self.other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1380 self.assertRaises(TypeError, lambda: self.other & self.set)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1381 if self.otherIsIterable:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1382 self.set.intersection(self.other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1383 else:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1384 self.assertRaises(TypeError, self.set.intersection, self.other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1385
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1386 def test_sym_difference_update_operator(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1387 try:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1388 self.set ^= self.other
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1389 except TypeError:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1390 pass
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1391 else:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1392 self.fail("expected TypeError")
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1393
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1394 def test_sym_difference_update(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1395 if self.otherIsIterable:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1396 self.set.symmetric_difference_update(self.other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1397 else:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1398 self.assertRaises(TypeError,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1399 self.set.symmetric_difference_update,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1400 self.other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1401
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1402 def test_sym_difference(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1403 self.assertRaises(TypeError, lambda: self.set ^ self.other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1404 self.assertRaises(TypeError, lambda: self.other ^ self.set)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1405 if self.otherIsIterable:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1406 self.set.symmetric_difference(self.other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1407 else:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1408 self.assertRaises(TypeError, self.set.symmetric_difference, self.other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1409
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1410 def test_difference_update_operator(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1411 try:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1412 self.set -= self.other
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1413 except TypeError:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1414 pass
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1415 else:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1416 self.fail("expected TypeError")
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1417
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1418 def test_difference_update(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1419 if self.otherIsIterable:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1420 self.set.difference_update(self.other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1421 else:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1422 self.assertRaises(TypeError,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1423 self.set.difference_update,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1424 self.other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1425
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1426 def test_difference(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1427 self.assertRaises(TypeError, lambda: self.set - self.other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1428 self.assertRaises(TypeError, lambda: self.other - self.set)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1429 if self.otherIsIterable:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1430 self.set.difference(self.other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1431 else:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1432 self.assertRaises(TypeError, self.set.difference, self.other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1433
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1434 #------------------------------------------------------------------------------
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1435
83449
ef100be2e8ba #17790: test_set now works with unittest test discovery. Patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 76091
diff changeset
1436 class TestOnlySetsNumeric(TestOnlySetsInBinaryOps, unittest.TestCase):
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1437 def setUp(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1438 self.set = set((1, 2, 3))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1439 self.other = 19
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1440 self.otherIsIterable = False
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1441
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1442 #------------------------------------------------------------------------------
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1443
83449
ef100be2e8ba #17790: test_set now works with unittest test discovery. Patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 76091
diff changeset
1444 class TestOnlySetsDict(TestOnlySetsInBinaryOps, unittest.TestCase):
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1445 def setUp(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1446 self.set = set((1, 2, 3))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1447 self.other = {1:2, 3:4}
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1448 self.otherIsIterable = True
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1449
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1450 #------------------------------------------------------------------------------
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1451
83449
ef100be2e8ba #17790: test_set now works with unittest test discovery. Patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 76091
diff changeset
1452 class TestOnlySetsOperator(TestOnlySetsInBinaryOps, unittest.TestCase):
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1453 def setUp(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1454 self.set = set((1, 2, 3))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1455 self.other = operator.add
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1456 self.otherIsIterable = False
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1457
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1458 #------------------------------------------------------------------------------
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1459
83449
ef100be2e8ba #17790: test_set now works with unittest test discovery. Patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 76091
diff changeset
1460 class TestOnlySetsTuple(TestOnlySetsInBinaryOps, unittest.TestCase):
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1461 def setUp(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1462 self.set = set((1, 2, 3))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1463 self.other = (2, 4, 6)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1464 self.otherIsIterable = True
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1465
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1466 #------------------------------------------------------------------------------
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1467
83449
ef100be2e8ba #17790: test_set now works with unittest test discovery. Patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 76091
diff changeset
1468 class TestOnlySetsString(TestOnlySetsInBinaryOps, unittest.TestCase):
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1469 def setUp(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1470 self.set = set((1, 2, 3))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1471 self.other = 'abc'
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1472 self.otherIsIterable = True
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1473
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1474 #------------------------------------------------------------------------------
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1475
83449
ef100be2e8ba #17790: test_set now works with unittest test discovery. Patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 76091
diff changeset
1476 class TestOnlySetsGenerator(TestOnlySetsInBinaryOps, unittest.TestCase):
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1477 def setUp(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1478 def gen():
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41575
diff changeset
1479 for i in range(0, 10, 2):
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1480 yield i
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1481 self.set = set((1, 2, 3))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1482 self.other = gen()
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1483 self.otherIsIterable = True
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1484
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1485 #==============================================================================
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1486
83449
ef100be2e8ba #17790: test_set now works with unittest test discovery. Patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 76091
diff changeset
1487 class TestCopying:
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1488
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1489 def test_copy(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1490 dup = self.set.copy()
39644
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1491 dup_list = sorted(dup, key=repr)
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1492 set_list = sorted(self.set, key=repr)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1493 self.assertEqual(len(dup_list), len(set_list))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1494 for i in range(len(dup_list)):
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55467
diff changeset
1495 self.assertTrue(dup_list[i] is set_list[i])
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1496
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1497 def test_deep_copy(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1498 dup = copy.deepcopy(self.set)
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 31389
diff changeset
1499 ##print type(dup), repr(dup)
39644
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1500 dup_list = sorted(dup, key=repr)
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1501 set_list = sorted(self.set, key=repr)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1502 self.assertEqual(len(dup_list), len(set_list))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1503 for i in range(len(dup_list)):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1504 self.assertEqual(dup_list[i], set_list[i])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1505
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1506 #------------------------------------------------------------------------------
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1507
83449
ef100be2e8ba #17790: test_set now works with unittest test discovery. Patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 76091
diff changeset
1508 class TestCopyingEmpty(TestCopying, unittest.TestCase):
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1509 def setUp(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1510 self.set = set()
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1511
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1512 #------------------------------------------------------------------------------
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1513
83449
ef100be2e8ba #17790: test_set now works with unittest test discovery. Patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 76091
diff changeset
1514 class TestCopyingSingleton(TestCopying, unittest.TestCase):
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1515 def setUp(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1516 self.set = set(["hello"])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1517
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1518 #------------------------------------------------------------------------------
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1519
83449
ef100be2e8ba #17790: test_set now works with unittest test discovery. Patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 76091
diff changeset
1520 class TestCopyingTriple(TestCopying, unittest.TestCase):
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1521 def setUp(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1522 self.set = set(["zero", 0, None])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1523
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1524 #------------------------------------------------------------------------------
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1525
83449
ef100be2e8ba #17790: test_set now works with unittest test discovery. Patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 76091
diff changeset
1526 class TestCopyingTuple(TestCopying, unittest.TestCase):
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1527 def setUp(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1528 self.set = set([(1, 2)])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1529
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1530 #------------------------------------------------------------------------------
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1531
83449
ef100be2e8ba #17790: test_set now works with unittest test discovery. Patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 76091
diff changeset
1532 class TestCopyingNested(TestCopying, unittest.TestCase):
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1533 def setUp(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1534 self.set = set([((1, 2), (3, 4))])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1535
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1536 #==============================================================================
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1537
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1538 class TestIdentities(unittest.TestCase):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1539 def setUp(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1540 self.a = set('abracadabra')
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1541 self.b = set('alacazam')
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1542
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1543 def test_binopsVsSubsets(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1544 a, b = self.a, self.b
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55467
diff changeset
1545 self.assertTrue(a - b < a)
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55467
diff changeset
1546 self.assertTrue(b - a < b)
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55467
diff changeset
1547 self.assertTrue(a & b < a)
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55467
diff changeset
1548 self.assertTrue(a & b < b)
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55467
diff changeset
1549 self.assertTrue(a | b > a)
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55467
diff changeset
1550 self.assertTrue(a | b > b)
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55467
diff changeset
1551 self.assertTrue(a ^ b < a | b)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1552
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1553 def test_commutativity(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1554 a, b = self.a, self.b
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1555 self.assertEqual(a&b, b&a)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1556 self.assertEqual(a|b, b|a)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1557 self.assertEqual(a^b, b^a)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1558 if a != b:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1559 self.assertNotEqual(a-b, b-a)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1560
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1561 def test_summations(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1562 # check that sums of parts equal the whole
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1563 a, b = self.a, self.b
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1564 self.assertEqual((a-b)|(a&b)|(b-a), a|b)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1565 self.assertEqual((a&b)|(a^b), a|b)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1566 self.assertEqual(a|(b-a), a|b)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1567 self.assertEqual((a-b)|b, a|b)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1568 self.assertEqual((a-b)|(a&b), a)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1569 self.assertEqual((b-a)|(a&b), b)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1570 self.assertEqual((a-b)|(b-a), a^b)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1571
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1572 def test_exclusion(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1573 # check that inverse operations show non-overlap
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1574 a, b, zero = self.a, self.b, set()
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1575 self.assertEqual((a-b)&b, zero)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1576 self.assertEqual((b-a)&a, zero)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1577 self.assertEqual((a&b)&(a^b), zero)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1578
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1579 # Tests derived from test_itertools.py =======================================
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1580
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1581 def R(seqn):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1582 'Regular generator'
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1583 for i in seqn:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1584 yield i
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1585
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1586 class G:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1587 'Sequence using __getitem__'
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1588 def __init__(self, seqn):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1589 self.seqn = seqn
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1590 def __getitem__(self, i):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1591 return self.seqn[i]
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1592
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1593 class I:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1594 'Sequence using iterator protocol'
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1595 def __init__(self, seqn):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1596 self.seqn = seqn
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1597 self.i = 0
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1598 def __iter__(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1599 return self
41501
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 40893
diff changeset
1600 def __next__(self):
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1601 if self.i >= len(self.seqn): raise StopIteration
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1602 v = self.seqn[self.i]
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1603 self.i += 1
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1604 return v
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1605
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1606 class Ig:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1607 'Sequence using iterator protocol defined with a generator'
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1608 def __init__(self, seqn):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1609 self.seqn = seqn
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1610 self.i = 0
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1611 def __iter__(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1612 for val in self.seqn:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1613 yield val
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1614
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1615 class X:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1616 'Missing __getitem__ and __iter__'
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1617 def __init__(self, seqn):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1618 self.seqn = seqn
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1619 self.i = 0
41501
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 40893
diff changeset
1620 def __next__(self):
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1621 if self.i >= len(self.seqn): raise StopIteration
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1622 v = self.seqn[self.i]
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1623 self.i += 1
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1624 return v
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1625
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1626 class N:
41501
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 40893
diff changeset
1627 'Iterator missing __next__()'
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1628 def __init__(self, seqn):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1629 self.seqn = seqn
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1630 self.i = 0
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1631 def __iter__(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1632 return self
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1633
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1634 class E:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1635 'Test propagation of exceptions'
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1636 def __init__(self, seqn):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1637 self.seqn = seqn
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1638 self.i = 0
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1639 def __iter__(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1640 return self
41501
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 40893
diff changeset
1641 def __next__(self):
33597
355b15e4a4da Use floor division operator.
Raymond Hettinger <python@rcn.com>
parents: 32555
diff changeset
1642 3 // 0
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1643
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1644 class S:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1645 'Test immediate stop'
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1646 def __init__(self, seqn):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1647 pass
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1648 def __iter__(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1649 return self
41501
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 40893
diff changeset
1650 def __next__(self):
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1651 raise StopIteration
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1652
45735
7adaab64eb72 Issues 2186 and 2187. Move map() from itertools to builtins.
Raymond Hettinger <python@rcn.com>
parents: 44943
diff changeset
1653 from itertools import chain
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1654 def L(seqn):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1655 'Test multiple tiers of iterators'
45735
7adaab64eb72 Issues 2186 and 2187. Move map() from itertools to builtins.
Raymond Hettinger <python@rcn.com>
parents: 44943
diff changeset
1656 return chain(map(lambda x:x, R(Ig(G(seqn)))))
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1657
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1658 class TestVariousIteratorArgs(unittest.TestCase):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1659
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1660 def test_constructor(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1661 for cons in (set, frozenset):
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41575
diff changeset
1662 for s in ("123", "", range(1000), ('do', 1.2), range(2000,2200,5)):
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1663 for g in (G, I, Ig, S, L, R):
39644
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1664 self.assertEqual(sorted(cons(g(s)), key=repr), sorted(g(s), key=repr))
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1665 self.assertRaises(TypeError, cons , X(s))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1666 self.assertRaises(TypeError, cons , N(s))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1667 self.assertRaises(ZeroDivisionError, cons , E(s))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1668
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1669 def test_inline_methods(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1670 s = set('november')
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41575
diff changeset
1671 for data in ("123", "", range(1000), ('do', 1.2), range(2000,2200,5), 'december'):
43815
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43604
diff changeset
1672 for meth in (s.union, s.intersection, s.difference, s.symmetric_difference, s.isdisjoint):
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1673 for g in (G, I, Ig, L, R):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1674 expected = meth(data)
85579
b6059bac8a9c Issue 18944: fix a 1-character typo in test_set.py.
Tim Peters <tim@python.org>
parents: 83449
diff changeset
1675 actual = meth(g(data))
43815
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43604
diff changeset
1676 if isinstance(expected, bool):
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43604
diff changeset
1677 self.assertEqual(actual, expected)
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43604
diff changeset
1678 else:
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43604
diff changeset
1679 self.assertEqual(sorted(actual, key=repr), sorted(expected, key=repr))
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1680 self.assertRaises(TypeError, meth, X(s))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1681 self.assertRaises(TypeError, meth, N(s))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1682 self.assertRaises(ZeroDivisionError, meth, E(s))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1683
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1684 def test_inplace_methods(self):
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41575
diff changeset
1685 for data in ("123", "", range(1000), ('do', 1.2), range(2000,2200,5), 'december'):
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
1686 for methname in ('update', 'intersection_update',
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1687 'difference_update', 'symmetric_difference_update'):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1688 for g in (G, I, Ig, S, L, R):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1689 s = set('january')
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1690 t = s.copy()
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1691 getattr(s, methname)(list(g(data)))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1692 getattr(t, methname)(g(data))
39644
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1693 self.assertEqual(sorted(s, key=repr), sorted(t, key=repr))
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1694
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1695 self.assertRaises(TypeError, getattr(set('january'), methname), X(data))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1696 self.assertRaises(TypeError, getattr(set('january'), methname), N(data))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1697 self.assertRaises(ZeroDivisionError, getattr(set('january'), methname), E(data))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1698
68860
57657393ceaf Fix obscure set crashers (#8420). Backport of d56b3cafb1e6, reviewed by Raymond.
Éric Araujo <merwok@netwok.org>
parents: 68488
diff changeset
1699 class bad_eq:
57657393ceaf Fix obscure set crashers (#8420). Backport of d56b3cafb1e6, reviewed by Raymond.
Éric Araujo <merwok@netwok.org>
parents: 68488
diff changeset
1700 def __eq__(self, other):
57657393ceaf Fix obscure set crashers (#8420). Backport of d56b3cafb1e6, reviewed by Raymond.
Éric Araujo <merwok@netwok.org>
parents: 68488
diff changeset
1701 if be_bad:
57657393ceaf Fix obscure set crashers (#8420). Backport of d56b3cafb1e6, reviewed by Raymond.
Éric Araujo <merwok@netwok.org>
parents: 68488
diff changeset
1702 set2.clear()
57657393ceaf Fix obscure set crashers (#8420). Backport of d56b3cafb1e6, reviewed by Raymond.
Éric Araujo <merwok@netwok.org>
parents: 68488
diff changeset
1703 raise ZeroDivisionError
57657393ceaf Fix obscure set crashers (#8420). Backport of d56b3cafb1e6, reviewed by Raymond.
Éric Araujo <merwok@netwok.org>
parents: 68488
diff changeset
1704 return self is other
57657393ceaf Fix obscure set crashers (#8420). Backport of d56b3cafb1e6, reviewed by Raymond.
Éric Araujo <merwok@netwok.org>
parents: 68488
diff changeset
1705 def __hash__(self):
57657393ceaf Fix obscure set crashers (#8420). Backport of d56b3cafb1e6, reviewed by Raymond.
Éric Araujo <merwok@netwok.org>
parents: 68488
diff changeset
1706 return 0
57657393ceaf Fix obscure set crashers (#8420). Backport of d56b3cafb1e6, reviewed by Raymond.
Éric Araujo <merwok@netwok.org>
parents: 68488
diff changeset
1707
57657393ceaf Fix obscure set crashers (#8420). Backport of d56b3cafb1e6, reviewed by Raymond.
Éric Araujo <merwok@netwok.org>
parents: 68488
diff changeset
1708 class bad_dict_clear:
57657393ceaf Fix obscure set crashers (#8420). Backport of d56b3cafb1e6, reviewed by Raymond.
Éric Araujo <merwok@netwok.org>
parents: 68488
diff changeset
1709 def __eq__(self, other):
57657393ceaf Fix obscure set crashers (#8420). Backport of d56b3cafb1e6, reviewed by Raymond.
Éric Araujo <merwok@netwok.org>
parents: 68488
diff changeset
1710 if be_bad:
57657393ceaf Fix obscure set crashers (#8420). Backport of d56b3cafb1e6, reviewed by Raymond.
Éric Araujo <merwok@netwok.org>
parents: 68488
diff changeset
1711 dict2.clear()
57657393ceaf Fix obscure set crashers (#8420). Backport of d56b3cafb1e6, reviewed by Raymond.
Éric Araujo <merwok@netwok.org>
parents: 68488
diff changeset
1712 return self is other
57657393ceaf Fix obscure set crashers (#8420). Backport of d56b3cafb1e6, reviewed by Raymond.
Éric Araujo <merwok@netwok.org>
parents: 68488
diff changeset
1713 def __hash__(self):
57657393ceaf Fix obscure set crashers (#8420). Backport of d56b3cafb1e6, reviewed by Raymond.
Éric Araujo <merwok@netwok.org>
parents: 68488
diff changeset
1714 return 0
57657393ceaf Fix obscure set crashers (#8420). Backport of d56b3cafb1e6, reviewed by Raymond.
Éric Araujo <merwok@netwok.org>
parents: 68488
diff changeset
1715
57657393ceaf Fix obscure set crashers (#8420). Backport of d56b3cafb1e6, reviewed by Raymond.
Éric Araujo <merwok@netwok.org>
parents: 68488
diff changeset
1716 class TestWeirdBugs(unittest.TestCase):
57657393ceaf Fix obscure set crashers (#8420). Backport of d56b3cafb1e6, reviewed by Raymond.
Éric Araujo <merwok@netwok.org>
parents: 68488
diff changeset
1717 def test_8420_set_merge(self):
57657393ceaf Fix obscure set crashers (#8420). Backport of d56b3cafb1e6, reviewed by Raymond.
Éric Araujo <merwok@netwok.org>
parents: 68488
diff changeset
1718 # This used to segfault
57657393ceaf Fix obscure set crashers (#8420). Backport of d56b3cafb1e6, reviewed by Raymond.
Éric Araujo <merwok@netwok.org>
parents: 68488
diff changeset
1719 global be_bad, set2, dict2
57657393ceaf Fix obscure set crashers (#8420). Backport of d56b3cafb1e6, reviewed by Raymond.
Éric Araujo <merwok@netwok.org>
parents: 68488
diff changeset
1720 be_bad = False
57657393ceaf Fix obscure set crashers (#8420). Backport of d56b3cafb1e6, reviewed by Raymond.
Éric Araujo <merwok@netwok.org>
parents: 68488
diff changeset
1721 set1 = {bad_eq()}
57657393ceaf Fix obscure set crashers (#8420). Backport of d56b3cafb1e6, reviewed by Raymond.
Éric Araujo <merwok@netwok.org>
parents: 68488
diff changeset
1722 set2 = {bad_eq() for i in range(75)}
57657393ceaf Fix obscure set crashers (#8420). Backport of d56b3cafb1e6, reviewed by Raymond.
Éric Araujo <merwok@netwok.org>
parents: 68488
diff changeset
1723 be_bad = True
57657393ceaf Fix obscure set crashers (#8420). Backport of d56b3cafb1e6, reviewed by Raymond.
Éric Araujo <merwok@netwok.org>
parents: 68488
diff changeset
1724 self.assertRaises(ZeroDivisionError, set1.update, set2)
57657393ceaf Fix obscure set crashers (#8420). Backport of d56b3cafb1e6, reviewed by Raymond.
Éric Araujo <merwok@netwok.org>
parents: 68488
diff changeset
1725
57657393ceaf Fix obscure set crashers (#8420). Backport of d56b3cafb1e6, reviewed by Raymond.
Éric Araujo <merwok@netwok.org>
parents: 68488
diff changeset
1726 be_bad = False
57657393ceaf Fix obscure set crashers (#8420). Backport of d56b3cafb1e6, reviewed by Raymond.
Éric Araujo <merwok@netwok.org>
parents: 68488
diff changeset
1727 set1 = {bad_dict_clear()}
57657393ceaf Fix obscure set crashers (#8420). Backport of d56b3cafb1e6, reviewed by Raymond.
Éric Araujo <merwok@netwok.org>
parents: 68488
diff changeset
1728 dict2 = {bad_dict_clear(): None}
57657393ceaf Fix obscure set crashers (#8420). Backport of d56b3cafb1e6, reviewed by Raymond.
Éric Araujo <merwok@netwok.org>
parents: 68488
diff changeset
1729 be_bad = True
57657393ceaf Fix obscure set crashers (#8420). Backport of d56b3cafb1e6, reviewed by Raymond.
Éric Araujo <merwok@netwok.org>
parents: 68488
diff changeset
1730 set1.symmetric_difference_update(dict2)
57657393ceaf Fix obscure set crashers (#8420). Backport of d56b3cafb1e6, reviewed by Raymond.
Éric Araujo <merwok@netwok.org>
parents: 68488
diff changeset
1731
44943
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1732 # Application tests (based on David Eppstein's graph recipes ====================================
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1733
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1734 def powerset(U):
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1735 """Generates all subsets of a set or sequence U."""
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1736 U = iter(U)
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1737 try:
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1738 x = frozenset([next(U)])
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1739 for S in powerset(U):
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1740 yield S
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1741 yield S | x
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1742 except StopIteration:
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1743 yield frozenset()
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1744
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1745 def cube(n):
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1746 """Graph of n-dimensional hypercube."""
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1747 singletons = [frozenset([x]) for x in range(n)]
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1748 return dict([(x, frozenset([x^s for s in singletons]))
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1749 for x in powerset(range(n))])
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1750
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1751 def linegraph(G):
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1752 """Graph, the vertices of which are edges of G,
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1753 with two vertices being adjacent iff the corresponding
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1754 edges share a vertex."""
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1755 L = {}
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1756 for x in G:
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1757 for y in G[x]:
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1758 nx = [frozenset([x,z]) for z in G[x] if z != y]
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1759 ny = [frozenset([y,z]) for z in G[y] if z != x]
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1760 L[frozenset([x,y])] = frozenset(nx+ny)
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1761 return L
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1762
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1763 def faces(G):
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1764 'Return a set of faces in G. Where a face is a set of vertices on that face'
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1765 # currently limited to triangles,squares, and pentagons
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1766 f = set()
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1767 for v1, edges in G.items():
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1768 for v2 in edges:
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1769 for v3 in G[v2]:
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1770 if v1 == v3:
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1771 continue
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1772 if v1 in G[v3]:
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1773 f.add(frozenset([v1, v2, v3]))
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1774 else:
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1775 for v4 in G[v3]:
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1776 if v4 == v2:
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1777 continue
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1778 if v1 in G[v4]:
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1779 f.add(frozenset([v1, v2, v3, v4]))
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1780 else:
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1781 for v5 in G[v4]:
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1782 if v5 == v3 or v5 == v2:
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1783 continue
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1784 if v1 in G[v5]:
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1785 f.add(frozenset([v1, v2, v3, v4, v5]))
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1786 return f
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1787
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1788
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1789 class TestGraphs(unittest.TestCase):
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1790
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1791 def test_cube(self):
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1792
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1793 g = cube(3) # vert --> {v1, v2, v3}
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1794 vertices1 = set(g)
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1795 self.assertEqual(len(vertices1), 8) # eight vertices
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1796 for edge in g.values():
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1797 self.assertEqual(len(edge), 3) # each vertex connects to three edges
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1798 vertices2 = set(v for edges in g.values() for v in edges)
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1799 self.assertEqual(vertices1, vertices2) # edge vertices in original set
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1800
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1801 cubefaces = faces(g)
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1802 self.assertEqual(len(cubefaces), 6) # six faces
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1803 for face in cubefaces:
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1804 self.assertEqual(len(face), 4) # each face is a square
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1805
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1806 def test_cuboctahedron(self):
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1807
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1808 # http://en.wikipedia.org/wiki/Cuboctahedron
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1809 # 8 triangular faces and 6 square faces
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1810 # 12 indentical vertices each connecting a triangle and square
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1811
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1812 g = cube(3)
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1813 cuboctahedron = linegraph(g) # V( --> {V1, V2, V3, V4}
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1814 self.assertEqual(len(cuboctahedron), 12)# twelve vertices
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1815
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1816 vertices = set(cuboctahedron)
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1817 for edges in cuboctahedron.values():
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1818 self.assertEqual(len(edges), 4) # each vertex connects to four other vertices
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1819 othervertices = set(edge for edges in cuboctahedron.values() for edge in edges)
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1820 self.assertEqual(vertices, othervertices) # edge vertices in original set
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1821
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1822 cubofaces = faces(cuboctahedron)
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1823 facesizes = collections.defaultdict(int)
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1824 for face in cubofaces:
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1825 facesizes[len(face)] += 1
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1826 self.assertEqual(facesizes[3], 8) # eight triangular faces
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1827 self.assertEqual(facesizes[4], 6) # six square faces
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1828
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1829 for vertex in cuboctahedron:
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1830 edge = vertex # Cuboctahedron vertices are edges in Cube
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1831 self.assertEqual(len(edge), 2) # Two cube vertices define an edge
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1832 for cubevert in edge:
58415
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 55466
diff changeset
1833 self.assertIn(cubevert, g)
44943
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1834
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1835
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1836 #==============================================================================
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1837
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1838 if __name__ == "__main__":
83449
ef100be2e8ba #17790: test_set now works with unittest test discovery. Patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 76091
diff changeset
1839 unittest.main()