annotate Lib/test/test_set.py @ 78069:a8aa77458e12 3.2

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