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