annotate Lib/test/test_set.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents a0f43f4481e0
children 2d330f7908e7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
60710
cf5756ec6ed7 Issue 8436: set.__init__ accepts keyword args
Raymond Hettinger <python@rcn.com>
parents: 55469
diff changeset
1
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2 import unittest
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
3 from test import test_support
50424
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 50305
diff changeset
4 import gc
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 50305
diff changeset
5 import weakref
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
6 import operator
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
7 import copy
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
8 import pickle
35206
89710805bc3d Add test for hash commutativity.
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
9 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
10 import sys
44926
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
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
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
23 def __cmp__(self, other):
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
40536
c1a4dc71cdd3 For sets with cyclical reprs, emit an ellipsis instead of infinitely recursing.
Raymond Hettinger <python@rcn.com>
parents: 40432
diff changeset
26 class ReprWrapper:
c1a4dc71cdd3 For sets with cyclical reprs, emit an ellipsis instead of infinitely recursing.
Raymond Hettinger <python@rcn.com>
parents: 40432
diff changeset
27 'Used to test self-referential repr() calls'
c1a4dc71cdd3 For sets with cyclical reprs, emit an ellipsis instead of infinitely recursing.
Raymond Hettinger <python@rcn.com>
parents: 40432
diff changeset
28 def __repr__(self):
c1a4dc71cdd3 For sets with cyclical reprs, emit an ellipsis instead of infinitely recursing.
Raymond Hettinger <python@rcn.com>
parents: 40432
diff changeset
29 return repr(self.value)
c1a4dc71cdd3 For sets with cyclical reprs, emit an ellipsis instead of infinitely recursing.
Raymond Hettinger <python@rcn.com>
parents: 40432
diff changeset
30
40865
81b5df5798c2 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40601
diff changeset
31 class HashCountingInt(int):
81b5df5798c2 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40601
diff changeset
32 'int-like object that counts the number of times __hash__ is called'
81b5df5798c2 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40601
diff changeset
33 def __init__(self, *args):
81b5df5798c2 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40601
diff changeset
34 self.hash_count = 0
81b5df5798c2 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40601
diff changeset
35 def __hash__(self):
81b5df5798c2 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40601
diff changeset
36 self.hash_count += 1
81b5df5798c2 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40601
diff changeset
37 return int.__hash__(self)
81b5df5798c2 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40601
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)
60710
cf5756ec6ed7 Issue 8436: set.__init__ accepts keyword args
Raymond Hettinger <python@rcn.com>
parents: 55469
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)])
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
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))
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
75 self.assertEqual(type(u), self.thetype)
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, [[]])
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
78 for C in set, frozenset, dict.fromkeys, str, unicode, list, tuple:
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'))
47530
d4cd7223af3b Let set.union() and set.update() accept multiple inputs.
Raymond Hettinger <python@rcn.com>
parents: 46065
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
55469
7212e3e0cd17 Issue 6573: Fix set.union() for cases where self is in the argument chain.
Raymond Hettinger <python@rcn.com>
parents: 50522
diff changeset
85 # Issue #6573
7212e3e0cd17 Issue 6573: Fix set.union() for cases where self is in the argument chain.
Raymond Hettinger <python@rcn.com>
parents: 50522
diff changeset
86 x = self.thetype()
7212e3e0cd17 Issue 6573: Fix set.union() for cases where self is in the argument chain.
Raymond Hettinger <python@rcn.com>
parents: 50522
diff changeset
87 self.assertEqual(x.union(set([1]), x, set([2])), self.thetype([1, 2]))
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
88
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))
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
105 self.assertEqual(type(i), self.thetype)
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())
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
107 for C in set, frozenset, dict.fromkeys, str, unicode, list, tuple:
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(''))
47533
10577e25f78f Let set.intersection() and set.intersection_update() take multiple input arguments.
Raymond Hettinger <python@rcn.com>
parents: 47530
diff changeset
112 self.assertEqual(self.thetype('abcba').intersection(C('cbcf'), C('bag')), set('b'))
47574
7d70b59eeaf2 Handle the case with zero arguments.
Raymond Hettinger <python@rcn.com>
parents: 47533
diff changeset
113 s = self.thetype('abcba')
7d70b59eeaf2 Handle the case with zero arguments.
Raymond Hettinger <python@rcn.com>
parents: 47533
diff changeset
114 z = s.intersection()
7d70b59eeaf2 Handle the case with zero arguments.
Raymond Hettinger <python@rcn.com>
parents: 47533
diff changeset
115 if self.thetype == frozenset():
7d70b59eeaf2 Handle the case with zero arguments.
Raymond Hettinger <python@rcn.com>
parents: 47533
diff changeset
116 self.assertEqual(id(s), id(z))
7d70b59eeaf2 Handle the case with zero arguments.
Raymond Hettinger <python@rcn.com>
parents: 47533
diff changeset
117 else:
7d70b59eeaf2 Handle the case with zero arguments.
Raymond Hettinger <python@rcn.com>
parents: 47533
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
43794
2b3abe3d3ae6 Add set.isdisjoint()
Raymond Hettinger <python@rcn.com>
parents: 41532
diff changeset
120 def test_isdisjoint(self):
2b3abe3d3ae6 Add set.isdisjoint()
Raymond Hettinger <python@rcn.com>
parents: 41532
diff changeset
121 def f(s1, s2):
2b3abe3d3ae6 Add set.isdisjoint()
Raymond Hettinger <python@rcn.com>
parents: 41532
diff changeset
122 'Pure python equivalent of isdisjoint()'
2b3abe3d3ae6 Add set.isdisjoint()
Raymond Hettinger <python@rcn.com>
parents: 41532
diff changeset
123 return not set(s1).intersection(s2)
2b3abe3d3ae6 Add set.isdisjoint()
Raymond Hettinger <python@rcn.com>
parents: 41532
diff changeset
124 for larg in '', 'a', 'ab', 'abc', 'ababac', 'cdc', 'cc', 'efgfe', 'ccb', 'ef':
2b3abe3d3ae6 Add set.isdisjoint()
Raymond Hettinger <python@rcn.com>
parents: 41532
diff changeset
125 s1 = self.thetype(larg)
2b3abe3d3ae6 Add set.isdisjoint()
Raymond Hettinger <python@rcn.com>
parents: 41532
diff changeset
126 for rarg in '', 'a', 'ab', 'abc', 'ababac', 'cdc', 'cc', 'efgfe', 'ccb', 'ef':
2b3abe3d3ae6 Add set.isdisjoint()
Raymond Hettinger <python@rcn.com>
parents: 41532
diff changeset
127 for C in set, frozenset, dict.fromkeys, str, unicode, list, tuple:
2b3abe3d3ae6 Add set.isdisjoint()
Raymond Hettinger <python@rcn.com>
parents: 41532
diff changeset
128 s2 = C(rarg)
2b3abe3d3ae6 Add set.isdisjoint()
Raymond Hettinger <python@rcn.com>
parents: 41532
diff changeset
129 actual = s1.isdisjoint(s2)
2b3abe3d3ae6 Add set.isdisjoint()
Raymond Hettinger <python@rcn.com>
parents: 41532
diff changeset
130 expected = f(s1, s2)
2b3abe3d3ae6 Add set.isdisjoint()
Raymond Hettinger <python@rcn.com>
parents: 41532
diff changeset
131 self.assertEqual(actual, expected)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 50522
diff changeset
132 self.assertTrue(actual is True or actual is False)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
133
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))
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
150 self.assertEqual(type(i), self.thetype)
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, [[]])
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
153 for C in set, frozenset, dict.fromkeys, str, unicode, list, tuple:
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'))
47588
8fe47b2c8bfe Multi-arg form for set.difference() and set.difference_update().
Raymond Hettinger <python@rcn.com>
parents: 47574
diff changeset
158 self.assertEqual(self.thetype('abcba').difference(), set('abc'))
8fe47b2c8bfe Multi-arg form for set.difference() and set.difference_update().
Raymond Hettinger <python@rcn.com>
parents: 47574
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))
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
177 self.assertEqual(type(i), self.thetype)
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, [[]])
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
180 for C in set, frozenset, dict.fromkeys, str, unicode, list, tuple:
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_compare(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
211 self.assertRaises(TypeError, self.s.__cmp__, self.s)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
212
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
213 def test_sub_and_super(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
214 p, q, r = map(self.thetype, ['ab', 'abcde', 'def'])
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 50522
diff changeset
215 self.assertTrue(p < q)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 50522
diff changeset
216 self.assertTrue(p <= q)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 50522
diff changeset
217 self.assertTrue(q <= q)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 50522
diff changeset
218 self.assertTrue(q > p)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 50522
diff changeset
219 self.assertTrue(q >= p)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 50522
diff changeset
220 self.assertFalse(q < r)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 50522
diff changeset
221 self.assertFalse(q <= r)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 50522
diff changeset
222 self.assertFalse(q > r)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 50522
diff changeset
223 self.assertFalse(q >= r)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 50522
diff changeset
224 self.assertTrue(set('a').issubset('abc'))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 50522
diff changeset
225 self.assertTrue(set('abc').issuperset('a'))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 50522
diff changeset
226 self.assertFalse(set('a').issubset('cbs'))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 50522
diff changeset
227 self.assertFalse(set('cbs').issuperset('a'))
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
228
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
229 def test_pickling(self):
50305
e673632e1885 Merged revisions 67898,67904-67907,67912,67918,67920-67921,67923-67924,67927,67930,67932,67943 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49466
diff changeset
230 for i in range(pickle.HIGHEST_PROTOCOL + 1):
33948
f2ce15b02ba3 SF 1062353: set pickling problems
Raymond Hettinger <python@rcn.com>
parents: 33605
diff changeset
231 p = pickle.dumps(self.s, i)
f2ce15b02ba3 SF 1062353: set pickling problems
Raymond Hettinger <python@rcn.com>
parents: 33605
diff changeset
232 dup = pickle.loads(p)
f2ce15b02ba3 SF 1062353: set pickling problems
Raymond Hettinger <python@rcn.com>
parents: 33605
diff changeset
233 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
234 if type(self.s) not in (set, frozenset):
f2ce15b02ba3 SF 1062353: set pickling problems
Raymond Hettinger <python@rcn.com>
parents: 33605
diff changeset
235 self.s.x = 10
f2ce15b02ba3 SF 1062353: set pickling problems
Raymond Hettinger <python@rcn.com>
parents: 33605
diff changeset
236 p = pickle.dumps(self.s)
f2ce15b02ba3 SF 1062353: set pickling problems
Raymond Hettinger <python@rcn.com>
parents: 33605
diff changeset
237 dup = pickle.loads(p)
f2ce15b02ba3 SF 1062353: set pickling problems
Raymond Hettinger <python@rcn.com>
parents: 33605
diff changeset
238 self.assertEqual(self.s.x, dup.x)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
239
31004
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
240 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
241 class Tracer:
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
242 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
243 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
244 def __hash__(self):
31389
aca1ce1b4b61 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 31291
diff changeset
245 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
246 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
247 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
248 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
249 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
250 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
251 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
252 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
253 newt = elem
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
254 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
255 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
256
35110
f3d3151f8417 SF patch #1200018: Restore GC support to set objects
Raymond Hettinger <python@rcn.com>
parents: 33948
diff changeset
257 def test_gc(self):
f3d3151f8417 SF patch #1200018: Restore GC support to set objects
Raymond Hettinger <python@rcn.com>
parents: 33948
diff changeset
258 # 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
259 class A:
f3d3151f8417 SF patch #1200018: Restore GC support to set objects
Raymond Hettinger <python@rcn.com>
parents: 33948
diff changeset
260 pass
f3d3151f8417 SF patch #1200018: Restore GC support to set objects
Raymond Hettinger <python@rcn.com>
parents: 33948
diff changeset
261 s = set(A() for i in xrange(1000))
f3d3151f8417 SF patch #1200018: Restore GC support to set objects
Raymond Hettinger <python@rcn.com>
parents: 33948
diff changeset
262 for elem in s:
f3d3151f8417 SF patch #1200018: Restore GC support to set objects
Raymond Hettinger <python@rcn.com>
parents: 33948
diff changeset
263 elem.cycle = s
f3d3151f8417 SF patch #1200018: Restore GC support to set objects
Raymond Hettinger <python@rcn.com>
parents: 33948
diff changeset
264 elem.sub = elem
f3d3151f8417 SF patch #1200018: Restore GC support to set objects
Raymond Hettinger <python@rcn.com>
parents: 33948
diff changeset
265 elem.set = set([elem])
f3d3151f8417 SF patch #1200018: Restore GC support to set objects
Raymond Hettinger <python@rcn.com>
parents: 33948
diff changeset
266
35334
725671db779a * Fix SF #1257731. Make __contains__(), remove(), and discard() only do
Raymond Hettinger <python@rcn.com>
parents: 35328
diff changeset
267 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
268 # Bug #1257731
725671db779a * Fix SF #1257731. Make __contains__(), remove(), and discard() only do
Raymond Hettinger <python@rcn.com>
parents: 35328
diff changeset
269 class H(self.thetype):
725671db779a * Fix SF #1257731. Make __contains__(), remove(), and discard() only do
Raymond Hettinger <python@rcn.com>
parents: 35328
diff changeset
270 def __hash__(self):
37574
dd085c13c49a Try to repair more new buildbot failures in "x86 OpenBSD trunk", due
Tim Peters <tim.peters@gmail.com>
parents: 37315
diff changeset
271 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
272 s=H()
725671db779a * Fix SF #1257731. Make __contains__(), remove(), and discard() only do
Raymond Hettinger <python@rcn.com>
parents: 35328
diff changeset
273 f=set()
725671db779a * Fix SF #1257731. Make __contains__(), remove(), and discard() only do
Raymond Hettinger <python@rcn.com>
parents: 35328
diff changeset
274 f.add(s)
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
275 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
276 f.remove(s)
725671db779a * Fix SF #1257731. Make __contains__(), remove(), and discard() only do
Raymond Hettinger <python@rcn.com>
parents: 35328
diff changeset
277 f.add(s)
725671db779a * Fix SF #1257731. Make __contains__(), remove(), and discard() only do
Raymond Hettinger <python@rcn.com>
parents: 35328
diff changeset
278 f.discard(s)
725671db779a * Fix SF #1257731. Make __contains__(), remove(), and discard() only do
Raymond Hettinger <python@rcn.com>
parents: 35328
diff changeset
279
35581
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
280 def test_badcmp(self):
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
281 s = self.thetype([BadCmp()])
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
282 # 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
283 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
284 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
285 # 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
286 if hasattr(s, 'add'):
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
287 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
288 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
289 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
290
40536
c1a4dc71cdd3 For sets with cyclical reprs, emit an ellipsis instead of infinitely recursing.
Raymond Hettinger <python@rcn.com>
parents: 40432
diff changeset
291 def test_cyclical_repr(self):
c1a4dc71cdd3 For sets with cyclical reprs, emit an ellipsis instead of infinitely recursing.
Raymond Hettinger <python@rcn.com>
parents: 40432
diff changeset
292 w = ReprWrapper()
c1a4dc71cdd3 For sets with cyclical reprs, emit an ellipsis instead of infinitely recursing.
Raymond Hettinger <python@rcn.com>
parents: 40432
diff changeset
293 s = self.thetype([w])
c1a4dc71cdd3 For sets with cyclical reprs, emit an ellipsis instead of infinitely recursing.
Raymond Hettinger <python@rcn.com>
parents: 40432
diff changeset
294 w.value = s
c1a4dc71cdd3 For sets with cyclical reprs, emit an ellipsis instead of infinitely recursing.
Raymond Hettinger <python@rcn.com>
parents: 40432
diff changeset
295 name = repr(s).partition('(')[0] # strip class name from repr string
c1a4dc71cdd3 For sets with cyclical reprs, emit an ellipsis instead of infinitely recursing.
Raymond Hettinger <python@rcn.com>
parents: 40432
diff changeset
296 self.assertEqual(repr(s), '%s([%s(...)])' % (name, name))
c1a4dc71cdd3 For sets with cyclical reprs, emit an ellipsis instead of infinitely recursing.
Raymond Hettinger <python@rcn.com>
parents: 40432
diff changeset
297
c1a4dc71cdd3 For sets with cyclical reprs, emit an ellipsis instead of infinitely recursing.
Raymond Hettinger <python@rcn.com>
parents: 40432
diff changeset
298 def test_cyclical_print(self):
c1a4dc71cdd3 For sets with cyclical reprs, emit an ellipsis instead of infinitely recursing.
Raymond Hettinger <python@rcn.com>
parents: 40432
diff changeset
299 w = ReprWrapper()
c1a4dc71cdd3 For sets with cyclical reprs, emit an ellipsis instead of infinitely recursing.
Raymond Hettinger <python@rcn.com>
parents: 40432
diff changeset
300 s = self.thetype([w])
c1a4dc71cdd3 For sets with cyclical reprs, emit an ellipsis instead of infinitely recursing.
Raymond Hettinger <python@rcn.com>
parents: 40432
diff changeset
301 w.value = s
46065
de54495a414a Don't try to close a non-open file.
Neal Norwitz <nnorwitz@gmail.com>
parents: 44926
diff changeset
302 fo = open(test_support.TESTFN, "wb")
40536
c1a4dc71cdd3 For sets with cyclical reprs, emit an ellipsis instead of infinitely recursing.
Raymond Hettinger <python@rcn.com>
parents: 40432
diff changeset
303 try:
c1a4dc71cdd3 For sets with cyclical reprs, emit an ellipsis instead of infinitely recursing.
Raymond Hettinger <python@rcn.com>
parents: 40432
diff changeset
304 print >> fo, s,
c1a4dc71cdd3 For sets with cyclical reprs, emit an ellipsis instead of infinitely recursing.
Raymond Hettinger <python@rcn.com>
parents: 40432
diff changeset
305 fo.close()
c1a4dc71cdd3 For sets with cyclical reprs, emit an ellipsis instead of infinitely recursing.
Raymond Hettinger <python@rcn.com>
parents: 40432
diff changeset
306 fo = open(test_support.TESTFN, "rb")
c1a4dc71cdd3 For sets with cyclical reprs, emit an ellipsis instead of infinitely recursing.
Raymond Hettinger <python@rcn.com>
parents: 40432
diff changeset
307 self.assertEqual(fo.read(), repr(s))
c1a4dc71cdd3 For sets with cyclical reprs, emit an ellipsis instead of infinitely recursing.
Raymond Hettinger <python@rcn.com>
parents: 40432
diff changeset
308 finally:
c1a4dc71cdd3 For sets with cyclical reprs, emit an ellipsis instead of infinitely recursing.
Raymond Hettinger <python@rcn.com>
parents: 40432
diff changeset
309 fo.close()
46065
de54495a414a Don't try to close a non-open file.
Neal Norwitz <nnorwitz@gmail.com>
parents: 44926
diff changeset
310 test_support.unlink(test_support.TESTFN)
40536
c1a4dc71cdd3 For sets with cyclical reprs, emit an ellipsis instead of infinitely recursing.
Raymond Hettinger <python@rcn.com>
parents: 40432
diff changeset
311
40865
81b5df5798c2 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40601
diff changeset
312 def test_do_not_rehash_dict_keys(self):
81b5df5798c2 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40601
diff changeset
313 n = 10
81b5df5798c2 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40601
diff changeset
314 d = dict.fromkeys(map(HashCountingInt, xrange(n)))
81b5df5798c2 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40601
diff changeset
315 self.assertEqual(sum(elem.hash_count for elem in d), n)
81b5df5798c2 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40601
diff changeset
316 s = self.thetype(d)
81b5df5798c2 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40601
diff changeset
317 self.assertEqual(sum(elem.hash_count for elem in d), n)
81b5df5798c2 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40601
diff changeset
318 s.difference(d)
41249
50a6b33c9d2c Extend work on rev 52962 and 53830 eliminating redundant PyObject_Hash() calls and fixing set/dict interoperability.
Raymond Hettinger <python@rcn.com>
parents: 40865
diff changeset
319 self.assertEqual(sum(elem.hash_count for elem in d), n)
40865
81b5df5798c2 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40601
diff changeset
320 if hasattr(s, 'symmetric_difference_update'):
81b5df5798c2 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40601
diff changeset
321 s.symmetric_difference_update(d)
41533
7ded787e6311 Whitespace normalization
Neal Norwitz <nnorwitz@gmail.com>
parents: 41271
diff changeset
322 self.assertEqual(sum(elem.hash_count for elem in d), n)
41249
50a6b33c9d2c Extend work on rev 52962 and 53830 eliminating redundant PyObject_Hash() calls and fixing set/dict interoperability.
Raymond Hettinger <python@rcn.com>
parents: 40865
diff changeset
323 d2 = dict.fromkeys(set(d))
50a6b33c9d2c Extend work on rev 52962 and 53830 eliminating redundant PyObject_Hash() calls and fixing set/dict interoperability.
Raymond Hettinger <python@rcn.com>
parents: 40865
diff changeset
324 self.assertEqual(sum(elem.hash_count for elem in d), n)
50a6b33c9d2c Extend work on rev 52962 and 53830 eliminating redundant PyObject_Hash() calls and fixing set/dict interoperability.
Raymond Hettinger <python@rcn.com>
parents: 40865
diff changeset
325 d3 = dict.fromkeys(frozenset(d))
50a6b33c9d2c Extend work on rev 52962 and 53830 eliminating redundant PyObject_Hash() calls and fixing set/dict interoperability.
Raymond Hettinger <python@rcn.com>
parents: 40865
diff changeset
326 self.assertEqual(sum(elem.hash_count for elem in d), n)
41271
f889f65a4861 Test and fix fromkeys optional argument.
Raymond Hettinger <python@rcn.com>
parents: 41249
diff changeset
327 d3 = dict.fromkeys(frozenset(d), 123)
f889f65a4861 Test and fix fromkeys optional argument.
Raymond Hettinger <python@rcn.com>
parents: 41249
diff changeset
328 self.assertEqual(sum(elem.hash_count for elem in d), n)
f889f65a4861 Test and fix fromkeys optional argument.
Raymond Hettinger <python@rcn.com>
parents: 41249
diff changeset
329 self.assertEqual(d3, dict.fromkeys(d, 123))
40865
81b5df5798c2 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40601
diff changeset
330
50424
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 50305
diff changeset
331 def test_container_iterator(self):
50522
ee9ced92843a Merged revisions 68133-68134,68141-68142,68145-68146,68148-68149,68159-68162,68166,68171-68174,68179,68195-68196,68210,68214-68215,68217-68222 via svnmerge from
Georg Brandl <georg@python.org>
parents: 50424
diff changeset
332 # Bug #3680: tp_traverse was not implemented for set iterator object
50424
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 50305
diff changeset
333 class C(object):
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 50305
diff changeset
334 pass
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 50305
diff changeset
335 obj = C()
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 50305
diff changeset
336 ref = weakref.ref(obj)
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 50305
diff changeset
337 container = set([obj, 1])
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 50305
diff changeset
338 obj.x = iter(container)
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 50305
diff changeset
339 del obj, container
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 50305
diff changeset
340 gc.collect()
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 50522
diff changeset
341 self.assertTrue(ref() is None, "Cycle was not collected")
50424
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 50305
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
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
345
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30929
diff changeset
346 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
347 s = self.thetype()
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30929
diff changeset
348 s.__init__(self.word)
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30929
diff changeset
349 self.assertEqual(s, set(self.word))
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30929
diff changeset
350 s.__init__(self.otherword)
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30929
diff changeset
351 self.assertEqual(s, set(self.otherword))
32555
d909a0ddd193 * fix the print test
Raymond Hettinger <python@rcn.com>
parents: 32158
diff changeset
352 self.assertRaises(TypeError, s.__init__, s, 2);
d909a0ddd193 * fix the print test
Raymond Hettinger <python@rcn.com>
parents: 32158
diff changeset
353 self.assertRaises(TypeError, s.__init__, 1);
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30929
diff changeset
354
31004
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
355 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
356 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
357 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
358 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
359
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
360 def test_hash(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
361 self.assertRaises(TypeError, hash, self.s)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
362
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
363 def test_clear(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
364 self.s.clear()
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
365 self.assertEqual(self.s, set())
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
366 self.assertEqual(len(self.s), 0)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
367
31004
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
368 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
369 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
370 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
371 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
372
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
373 def test_add(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
374 self.s.add('Q')
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
375 self.assertIn('Q', self.s)
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
376 dup = self.s.copy()
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
377 self.s.add('Q')
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
378 self.assertEqual(self.s, dup)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
379 self.assertRaises(TypeError, self.s.add, [])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
380
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
381 def test_remove(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
382 self.s.remove('a')
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
383 self.assertNotIn('a', self.s)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
384 self.assertRaises(KeyError, self.s.remove, 'Q')
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
385 self.assertRaises(TypeError, self.s.remove, [])
31002
622a76bc0237 Extend temporary hashability to remove() and discard().
Raymond Hettinger <python@rcn.com>
parents: 30995
diff changeset
386 s = self.thetype([frozenset(self.word)])
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
387 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
388 s.remove(self.thetype(self.word))
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
389 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
390 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
391
40432
a15bd81efdb6 Add test for SF bug 1576657
Raymond Hettinger <python@rcn.com>
parents: 37574
diff changeset
392 def test_remove_keyerror_unpacking(self):
a15bd81efdb6 Add test for SF bug 1576657
Raymond Hettinger <python@rcn.com>
parents: 37574
diff changeset
393 # bug: www.python.org/sf/1576657
a15bd81efdb6 Add test for SF bug 1576657
Raymond Hettinger <python@rcn.com>
parents: 37574
diff changeset
394 for v1 in ['Q', (1,)]:
a15bd81efdb6 Add test for SF bug 1576657
Raymond Hettinger <python@rcn.com>
parents: 37574
diff changeset
395 try:
a15bd81efdb6 Add test for SF bug 1576657
Raymond Hettinger <python@rcn.com>
parents: 37574
diff changeset
396 self.s.remove(v1)
a15bd81efdb6 Add test for SF bug 1576657
Raymond Hettinger <python@rcn.com>
parents: 37574
diff changeset
397 except KeyError, e:
a15bd81efdb6 Add test for SF bug 1576657
Raymond Hettinger <python@rcn.com>
parents: 37574
diff changeset
398 v2 = e.args[0]
a15bd81efdb6 Add test for SF bug 1576657
Raymond Hettinger <python@rcn.com>
parents: 37574
diff changeset
399 self.assertEqual(v1, v2)
a15bd81efdb6 Add test for SF bug 1576657
Raymond Hettinger <python@rcn.com>
parents: 37574
diff changeset
400 else:
a15bd81efdb6 Add test for SF bug 1576657
Raymond Hettinger <python@rcn.com>
parents: 37574
diff changeset
401 self.fail()
a15bd81efdb6 Add test for SF bug 1576657
Raymond Hettinger <python@rcn.com>
parents: 37574
diff changeset
402
49466
6e7b853e8616 #4069: aSet.remove(otherSet) would always report the empty frozenset([]) as the missing key.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47588
diff changeset
403 def test_remove_keyerror_set(self):
6e7b853e8616 #4069: aSet.remove(otherSet) would always report the empty frozenset([]) as the missing key.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47588
diff changeset
404 key = self.thetype([3, 4])
6e7b853e8616 #4069: aSet.remove(otherSet) would always report the empty frozenset([]) as the missing key.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47588
diff changeset
405 try:
6e7b853e8616 #4069: aSet.remove(otherSet) would always report the empty frozenset([]) as the missing key.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47588
diff changeset
406 self.s.remove(key)
6e7b853e8616 #4069: aSet.remove(otherSet) would always report the empty frozenset([]) as the missing key.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47588
diff changeset
407 except KeyError as e:
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 50522
diff changeset
408 self.assertTrue(e.args[0] is key,
49466
6e7b853e8616 #4069: aSet.remove(otherSet) would always report the empty frozenset([]) as the missing key.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47588
diff changeset
409 "KeyError should be {0}, not {1}".format(key,
6e7b853e8616 #4069: aSet.remove(otherSet) would always report the empty frozenset([]) as the missing key.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47588
diff changeset
410 e.args[0]))
6e7b853e8616 #4069: aSet.remove(otherSet) would always report the empty frozenset([]) as the missing key.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47588
diff changeset
411 else:
6e7b853e8616 #4069: aSet.remove(otherSet) would always report the empty frozenset([]) as the missing key.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47588
diff changeset
412 self.fail()
6e7b853e8616 #4069: aSet.remove(otherSet) would always report the empty frozenset([]) as the missing key.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47588
diff changeset
413
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
414 def test_discard(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
415 self.s.discard('a')
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
416 self.assertNotIn('a', self.s)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
417 self.s.discard('Q')
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
418 self.assertRaises(TypeError, self.s.discard, [])
31002
622a76bc0237 Extend temporary hashability to remove() and discard().
Raymond Hettinger <python@rcn.com>
parents: 30995
diff changeset
419 s = self.thetype([frozenset(self.word)])
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
420 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
421 s.discard(self.thetype(self.word))
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
422 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
423 s.discard(self.thetype(self.word))
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
424
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
425 def test_pop(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
426 for i in xrange(len(self.s)):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
427 elem = self.s.pop()
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
428 self.assertNotIn(elem, self.s)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
429 self.assertRaises(KeyError, self.s.pop)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
430
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
431 def test_update(self):
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
432 retval = self.s.update(self.otherword)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
433 self.assertEqual(retval, None)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
434 for c in (self.word + self.otherword):
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
435 self.assertIn(c, self.s)
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
436 self.assertRaises(PassThru, self.s.update, check_pass_thru())
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
437 self.assertRaises(TypeError, self.s.update, [[]])
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
438 for p, q in (('cdc', 'abcd'), ('efgfe', 'abcefg'), ('ccb', 'abc'), ('ef', 'abcef')):
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
439 for C in set, frozenset, dict.fromkeys, str, unicode, list, tuple:
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
440 s = self.thetype('abcba')
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
441 self.assertEqual(s.update(C(p)), None)
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
442 self.assertEqual(s, set(q))
47530
d4cd7223af3b Let set.union() and set.update() accept multiple inputs.
Raymond Hettinger <python@rcn.com>
parents: 46065
diff changeset
443 for p in ('cdc', 'efgfe', 'ccb', 'ef', 'abcda'):
d4cd7223af3b Let set.union() and set.update() accept multiple inputs.
Raymond Hettinger <python@rcn.com>
parents: 46065
diff changeset
444 q = 'ahi'
d4cd7223af3b Let set.union() and set.update() accept multiple inputs.
Raymond Hettinger <python@rcn.com>
parents: 46065
diff changeset
445 for C in set, frozenset, dict.fromkeys, str, unicode, list, tuple:
d4cd7223af3b Let set.union() and set.update() accept multiple inputs.
Raymond Hettinger <python@rcn.com>
parents: 46065
diff changeset
446 s = self.thetype('abcba')
d4cd7223af3b Let set.union() and set.update() accept multiple inputs.
Raymond Hettinger <python@rcn.com>
parents: 46065
diff changeset
447 self.assertEqual(s.update(C(p), C(q)), None)
d4cd7223af3b Let set.union() and set.update() accept multiple inputs.
Raymond Hettinger <python@rcn.com>
parents: 46065
diff changeset
448 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
449
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
450 def test_ior(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
451 self.s |= set(self.otherword)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
452 for c in (self.word + self.otherword):
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
453 self.assertIn(c, self.s)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
454
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
455 def test_intersection_update(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
456 retval = self.s.intersection_update(self.otherword)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
457 self.assertEqual(retval, None)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
458 for c in (self.word + self.otherword):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
459 if c in self.otherword and c in self.word:
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
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 else:
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
462 self.assertNotIn(c, self.s)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
463 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
464 self.assertRaises(TypeError, self.s.intersection_update, [[]])
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
465 for p, q in (('cdc', 'c'), ('efgfe', ''), ('ccb', 'bc'), ('ef', '')):
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
466 for C in set, frozenset, dict.fromkeys, str, unicode, list, tuple:
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
467 s = self.thetype('abcba')
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
468 self.assertEqual(s.intersection_update(C(p)), None)
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
469 self.assertEqual(s, set(q))
47533
10577e25f78f Let set.intersection() and set.intersection_update() take multiple input arguments.
Raymond Hettinger <python@rcn.com>
parents: 47530
diff changeset
470 ss = 'abcba'
10577e25f78f Let set.intersection() and set.intersection_update() take multiple input arguments.
Raymond Hettinger <python@rcn.com>
parents: 47530
diff changeset
471 s = self.thetype(ss)
10577e25f78f Let set.intersection() and set.intersection_update() take multiple input arguments.
Raymond Hettinger <python@rcn.com>
parents: 47530
diff changeset
472 t = 'cbc'
10577e25f78f Let set.intersection() and set.intersection_update() take multiple input arguments.
Raymond Hettinger <python@rcn.com>
parents: 47530
diff changeset
473 self.assertEqual(s.intersection_update(C(p), C(t)), None)
10577e25f78f Let set.intersection() and set.intersection_update() take multiple input arguments.
Raymond Hettinger <python@rcn.com>
parents: 47530
diff changeset
474 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
475
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
476 def test_iand(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
477 self.s &= set(self.otherword)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
478 for c in (self.word + self.otherword):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
479 if c in self.otherword and c in self.word:
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
480 self.assertIn(c, self.s)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
481 else:
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
482 self.assertNotIn(c, self.s)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
483
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
484 def test_difference_update(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
485 retval = self.s.difference_update(self.otherword)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
486 self.assertEqual(retval, None)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
487 for c in (self.word + self.otherword):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
488 if c in self.word and c not in self.otherword:
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
489 self.assertIn(c, self.s)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
490 else:
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
491 self.assertNotIn(c, self.s)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
492 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
493 self.assertRaises(TypeError, self.s.difference_update, [[]])
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
494 self.assertRaises(TypeError, self.s.symmetric_difference_update, [[]])
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
495 for p, q in (('cdc', 'ab'), ('efgfe', 'abc'), ('ccb', 'a'), ('ef', 'abc')):
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
496 for C in set, frozenset, dict.fromkeys, str, unicode, list, tuple:
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
497 s = self.thetype('abcba')
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
498 self.assertEqual(s.difference_update(C(p)), None)
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
499 self.assertEqual(s, set(q))
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
500
47588
8fe47b2c8bfe Multi-arg form for set.difference() and set.difference_update().
Raymond Hettinger <python@rcn.com>
parents: 47574
diff changeset
501 s = self.thetype('abcdefghih')
8fe47b2c8bfe Multi-arg form for set.difference() and set.difference_update().
Raymond Hettinger <python@rcn.com>
parents: 47574
diff changeset
502 s.difference_update()
8fe47b2c8bfe Multi-arg form for set.difference() and set.difference_update().
Raymond Hettinger <python@rcn.com>
parents: 47574
diff changeset
503 self.assertEqual(s, self.thetype('abcdefghih'))
8fe47b2c8bfe Multi-arg form for set.difference() and set.difference_update().
Raymond Hettinger <python@rcn.com>
parents: 47574
diff changeset
504
8fe47b2c8bfe Multi-arg form for set.difference() and set.difference_update().
Raymond Hettinger <python@rcn.com>
parents: 47574
diff changeset
505 s = self.thetype('abcdefghih')
8fe47b2c8bfe Multi-arg form for set.difference() and set.difference_update().
Raymond Hettinger <python@rcn.com>
parents: 47574
diff changeset
506 s.difference_update(C('aba'))
8fe47b2c8bfe Multi-arg form for set.difference() and set.difference_update().
Raymond Hettinger <python@rcn.com>
parents: 47574
diff changeset
507 self.assertEqual(s, self.thetype('cdefghih'))
8fe47b2c8bfe Multi-arg form for set.difference() and set.difference_update().
Raymond Hettinger <python@rcn.com>
parents: 47574
diff changeset
508
8fe47b2c8bfe Multi-arg form for set.difference() and set.difference_update().
Raymond Hettinger <python@rcn.com>
parents: 47574
diff changeset
509 s = self.thetype('abcdefghih')
8fe47b2c8bfe Multi-arg form for set.difference() and set.difference_update().
Raymond Hettinger <python@rcn.com>
parents: 47574
diff changeset
510 s.difference_update(C('cdc'), C('aba'))
8fe47b2c8bfe Multi-arg form for set.difference() and set.difference_update().
Raymond Hettinger <python@rcn.com>
parents: 47574
diff changeset
511 self.assertEqual(s, self.thetype('efghih'))
8fe47b2c8bfe Multi-arg form for set.difference() and set.difference_update().
Raymond Hettinger <python@rcn.com>
parents: 47574
diff changeset
512
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
513 def test_isub(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
514 self.s -= set(self.otherword)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
515 for c in (self.word + self.otherword):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
516 if c in self.word and c not in self.otherword:
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
517 self.assertIn(c, self.s)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
518 else:
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
519 self.assertNotIn(c, self.s)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
520
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
521 def test_symmetric_difference_update(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
522 retval = self.s.symmetric_difference_update(self.otherword)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
523 self.assertEqual(retval, None)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
524 for c in (self.word + self.otherword):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
525 if (c in self.word) ^ (c in self.otherword):
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
526 self.assertIn(c, self.s)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
527 else:
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
528 self.assertNotIn(c, self.s)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
529 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
530 self.assertRaises(TypeError, self.s.symmetric_difference_update, [[]])
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
531 for p, q in (('cdc', 'abd'), ('efgfe', 'abcefg'), ('ccb', 'a'), ('ef', 'abcef')):
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
532 for C in set, frozenset, dict.fromkeys, str, unicode, list, tuple:
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
533 s = self.thetype('abcba')
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
534 self.assertEqual(s.symmetric_difference_update(C(p)), None)
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
535 self.assertEqual(s, set(q))
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
536
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
537 def test_ixor(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
538 self.s ^= set(self.otherword)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
539 for c in (self.word + self.otherword):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
540 if (c in self.word) ^ (c in self.otherword):
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
541 self.assertIn(c, self.s)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
542 else:
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
543 self.assertNotIn(c, self.s)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
544
35328
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35297
diff changeset
545 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
546 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
547 t |= t
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35297
diff changeset
548 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
549 t &= t
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35297
diff changeset
550 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
551 t -= t
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35297
diff changeset
552 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
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.thetype())
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35297
diff changeset
556
32158
679e5c8effae Make sets and deques weak referencable.
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
557 def test_weakref(self):
679e5c8effae Make sets and deques weak referencable.
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
558 s = self.thetype('gallahad')
50424
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 50305
diff changeset
559 p = weakref.proxy(s)
32158
679e5c8effae Make sets and deques weak referencable.
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
560 self.assertEqual(str(p), str(s))
679e5c8effae Make sets and deques weak referencable.
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
561 s = None
679e5c8effae Make sets and deques weak referencable.
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
562 self.assertRaises(ReferenceError, str, p)
679e5c8effae Make sets and deques weak referencable.
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
563
35348
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35334
diff changeset
564 # C API test only available in a debug build
37315
19059df45cfe SF patch #1458476 with modifications based on discussions in python-dev. This
Barry Warsaw <barry@python.org>
parents: 36484
diff changeset
565 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
566 def test_c_api(self):
59547
1e01ef975899 Issue #7818: set().test_c_api() doesn't expect a set('abc'), modify the set.
Victor Stinner <victor.stinner@haypocalc.com>
parents: 58817
diff changeset
567 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
568
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30929
diff changeset
569 class SetSubclass(set):
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30929
diff changeset
570 pass
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30929
diff changeset
571
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30929
diff changeset
572 class TestSetSubclass(TestSet):
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30929
diff changeset
573 thetype = SetSubclass
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
574
40601
7da3dbc8145b SF #1486663 -- Allow keyword args in subclasses of set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 40536
diff changeset
575 class SetSubclassWithKeywordArgs(set):
7da3dbc8145b SF #1486663 -- Allow keyword args in subclasses of set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 40536
diff changeset
576 def __init__(self, iterable=[], newarg=None):
7da3dbc8145b SF #1486663 -- Allow keyword args in subclasses of set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 40536
diff changeset
577 set.__init__(self, iterable)
7da3dbc8145b SF #1486663 -- Allow keyword args in subclasses of set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 40536
diff changeset
578
7da3dbc8145b SF #1486663 -- Allow keyword args in subclasses of set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 40536
diff changeset
579 class TestSetSubclassWithKeywordArgs(TestSet):
41533
7ded787e6311 Whitespace normalization
Neal Norwitz <nnorwitz@gmail.com>
parents: 41271
diff changeset
580
40601
7da3dbc8145b SF #1486663 -- Allow keyword args in subclasses of set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 40536
diff changeset
581 def test_keywords_in_subclass(self):
7da3dbc8145b SF #1486663 -- Allow keyword args in subclasses of set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 40536
diff changeset
582 'SF bug #1486663 -- this used to erroneously raise a TypeError'
7da3dbc8145b SF #1486663 -- Allow keyword args in subclasses of set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 40536
diff changeset
583 SetSubclassWithKeywordArgs(newarg=1)
7da3dbc8145b SF #1486663 -- Allow keyword args in subclasses of set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 40536
diff changeset
584
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
585 class TestFrozenSet(TestJointOps):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
586 thetype = frozenset
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
587
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30929
diff changeset
588 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
589 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
590 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
591 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
592
35297
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35206
diff changeset
593 def test_singleton_empty_frozenset(self):
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35206
diff changeset
594 f = frozenset()
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35206
diff changeset
595 efs = [frozenset(), frozenset([]), frozenset(()), frozenset(''),
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35206
diff changeset
596 frozenset(), frozenset([]), frozenset(()), frozenset(''),
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35206
diff changeset
597 frozenset(xrange(0)), frozenset(frozenset()),
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35206
diff changeset
598 frozenset(f), f]
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35206
diff changeset
599 # 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
600 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
601
31004
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
602 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
603 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
604 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
605 self.assertEqual(id(s), id(t))
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30929
diff changeset
606
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
607 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
608 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
609 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
610
35206
89710805bc3d Add test for hash commutativity.
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
611 # 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
612 n = 100
89710805bc3d Add test for hash commutativity.
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
613 seq = [randrange(n) for i in xrange(n)]
89710805bc3d Add test for hash commutativity.
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
614 results = set()
89710805bc3d Add test for hash commutativity.
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
615 for i in xrange(200):
89710805bc3d Add test for hash commutativity.
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
616 shuffle(seq)
89710805bc3d Add test for hash commutativity.
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
617 results.add(hash(self.thetype(seq)))
89710805bc3d Add test for hash commutativity.
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
618 self.assertEqual(len(results), 1)
89710805bc3d Add test for hash commutativity.
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
619
31004
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
620 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
621 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
622 self.assertEqual(id(self.s), id(dup))
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
623
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
624 def test_frozen_as_dictkey(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
625 seq = 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
626 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
627 key2 = self.thetype(reversed(seq))
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
628 self.assertEqual(key1, key2)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
629 self.assertNotEqual(id(key1), id(key2))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
630 d = {}
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
631 d[key1] = 42
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
632 self.assertEqual(d[key2], 42)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
633
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
634 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
635 f = self.thetype('abcdcda')
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
636 self.assertEqual(hash(f), hash(f))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
637
31004
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
638 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
639 n = 13
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
640 hashvalues = set()
31291
868cace2219f Strengthen the test for hash effectiveness
Raymond Hettinger <python@rcn.com>
parents: 31210
diff changeset
641 addhashvalue = hashvalues.add
868cace2219f Strengthen the test for hash effectiveness
Raymond Hettinger <python@rcn.com>
parents: 31210
diff changeset
642 elemmasks = [(i+1, 1<<i) for i in range(n)]
31004
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
643 for i in xrange(2**n):
31291
868cace2219f Strengthen the test for hash effectiveness
Raymond Hettinger <python@rcn.com>
parents: 31210
diff changeset
644 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
645 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
646
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30929
diff changeset
647 class FrozenSetSubclass(frozenset):
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30929
diff changeset
648 pass
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30929
diff changeset
649
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30929
diff changeset
650 class TestFrozenSetSubclass(TestFrozenSet):
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30929
diff changeset
651 thetype = FrozenSetSubclass
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30929
diff changeset
652
31004
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
653 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
654 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
655 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
656 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
657
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
658 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
659 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
660 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
661
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
662 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
663 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
664 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
665 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
666
35297
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35206
diff changeset
667 def test_singleton_empty_frozenset(self):
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35206
diff changeset
668 Frozenset = self.thetype
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35206
diff changeset
669 f = frozenset()
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35206
diff changeset
670 F = Frozenset()
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35206
diff changeset
671 efs = [Frozenset(), Frozenset([]), Frozenset(()), Frozenset(''),
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35206
diff changeset
672 Frozenset(), Frozenset([]), Frozenset(()), Frozenset(''),
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35206
diff changeset
673 Frozenset(xrange(0)), Frozenset(Frozenset()),
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35206
diff changeset
674 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
675 # 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
676 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
677
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
678 # Tests taken from test_sets.py =============================================
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
679
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
680 empty_set = set()
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
681
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
682 #==============================================================================
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
683
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
684 class TestBasicOps(unittest.TestCase):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
685
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
686 def test_repr(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
687 if self.repr is not None:
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 31389
diff changeset
688 self.assertEqual(repr(self.set), self.repr)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
689
75100
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 68255
diff changeset
690 def check_repr_against_values(self):
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 68255
diff changeset
691 text = repr(self.set)
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 68255
diff changeset
692 self.assertTrue(text.startswith('{'))
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 68255
diff changeset
693 self.assertTrue(text.endswith('}'))
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 68255
diff changeset
694
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 68255
diff changeset
695 result = text[1:-1].split(', ')
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 68255
diff changeset
696 result.sort()
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 68255
diff changeset
697 sorted_repr_values = [repr(value) for value in self.values]
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 68255
diff changeset
698 sorted_repr_values.sort()
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 68255
diff changeset
699 self.assertEqual(result, sorted_repr_values)
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 68255
diff changeset
700
32555
d909a0ddd193 * fix the print test
Raymond Hettinger <python@rcn.com>
parents: 32158
diff changeset
701 def test_print(self):
46065
de54495a414a Don't try to close a non-open file.
Neal Norwitz <nnorwitz@gmail.com>
parents: 44926
diff changeset
702 fo = open(test_support.TESTFN, "wb")
32555
d909a0ddd193 * fix the print test
Raymond Hettinger <python@rcn.com>
parents: 32158
diff changeset
703 try:
d909a0ddd193 * fix the print test
Raymond Hettinger <python@rcn.com>
parents: 32158
diff changeset
704 print >> fo, self.set,
d909a0ddd193 * fix the print test
Raymond Hettinger <python@rcn.com>
parents: 32158
diff changeset
705 fo.close()
d909a0ddd193 * fix the print test
Raymond Hettinger <python@rcn.com>
parents: 32158
diff changeset
706 fo = open(test_support.TESTFN, "rb")
d909a0ddd193 * fix the print test
Raymond Hettinger <python@rcn.com>
parents: 32158
diff changeset
707 self.assertEqual(fo.read(), repr(self.set))
d909a0ddd193 * fix the print test
Raymond Hettinger <python@rcn.com>
parents: 32158
diff changeset
708 finally:
d909a0ddd193 * fix the print test
Raymond Hettinger <python@rcn.com>
parents: 32158
diff changeset
709 fo.close()
46065
de54495a414a Don't try to close a non-open file.
Neal Norwitz <nnorwitz@gmail.com>
parents: 44926
diff changeset
710 test_support.unlink(test_support.TESTFN)
32555
d909a0ddd193 * fix the print test
Raymond Hettinger <python@rcn.com>
parents: 32158
diff changeset
711
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
712 def test_length(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
713 self.assertEqual(len(self.set), self.length)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
714
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
715 def test_self_equality(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
716 self.assertEqual(self.set, self.set)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
717
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
718 def test_equivalent_equality(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
719 self.assertEqual(self.set, self.dup)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
720
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
721 def test_copy(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
722 self.assertEqual(self.set.copy(), self.dup)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
723
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
724 def test_self_union(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
725 result = self.set | self.set
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
726 self.assertEqual(result, self.dup)
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 def test_empty_union(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
729 result = self.set | empty_set
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
730 self.assertEqual(result, self.dup)
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_union_empty(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
733 result = empty_set | self.set
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
734 self.assertEqual(result, self.dup)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
735
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
736 def test_self_intersection(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
737 result = self.set & self.set
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
738 self.assertEqual(result, self.dup)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
739
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
740 def test_empty_intersection(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
741 result = self.set & empty_set
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
742 self.assertEqual(result, empty_set)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
743
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
744 def test_intersection_empty(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
745 result = empty_set & self.set
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
746 self.assertEqual(result, empty_set)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
747
43794
2b3abe3d3ae6 Add set.isdisjoint()
Raymond Hettinger <python@rcn.com>
parents: 41532
diff changeset
748 def test_self_isdisjoint(self):
2b3abe3d3ae6 Add set.isdisjoint()
Raymond Hettinger <python@rcn.com>
parents: 41532
diff changeset
749 result = self.set.isdisjoint(self.set)
2b3abe3d3ae6 Add set.isdisjoint()
Raymond Hettinger <python@rcn.com>
parents: 41532
diff changeset
750 self.assertEqual(result, not self.set)
2b3abe3d3ae6 Add set.isdisjoint()
Raymond Hettinger <python@rcn.com>
parents: 41532
diff changeset
751
2b3abe3d3ae6 Add set.isdisjoint()
Raymond Hettinger <python@rcn.com>
parents: 41532
diff changeset
752 def test_empty_isdisjoint(self):
2b3abe3d3ae6 Add set.isdisjoint()
Raymond Hettinger <python@rcn.com>
parents: 41532
diff changeset
753 result = self.set.isdisjoint(empty_set)
2b3abe3d3ae6 Add set.isdisjoint()
Raymond Hettinger <python@rcn.com>
parents: 41532
diff changeset
754 self.assertEqual(result, True)
2b3abe3d3ae6 Add set.isdisjoint()
Raymond Hettinger <python@rcn.com>
parents: 41532
diff changeset
755
2b3abe3d3ae6 Add set.isdisjoint()
Raymond Hettinger <python@rcn.com>
parents: 41532
diff changeset
756 def test_isdisjoint_empty(self):
2b3abe3d3ae6 Add set.isdisjoint()
Raymond Hettinger <python@rcn.com>
parents: 41532
diff changeset
757 result = empty_set.isdisjoint(self.set)
2b3abe3d3ae6 Add set.isdisjoint()
Raymond Hettinger <python@rcn.com>
parents: 41532
diff changeset
758 self.assertEqual(result, True)
2b3abe3d3ae6 Add set.isdisjoint()
Raymond Hettinger <python@rcn.com>
parents: 41532
diff changeset
759
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
760 def test_self_symmetric_difference(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
761 result = self.set ^ self.set
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
762 self.assertEqual(result, empty_set)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
763
63511
61ad81a774c5 Merged revisions 83421,83424 via svnmerge from
Georg Brandl <georg@python.org>
parents: 60710
diff changeset
764 def test_empty_symmetric_difference(self):
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
765 result = self.set ^ empty_set
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
766 self.assertEqual(result, self.set)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
767
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
768 def test_self_difference(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
769 result = self.set - self.set
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
770 self.assertEqual(result, empty_set)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
771
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
772 def test_empty_difference(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
773 result = self.set - empty_set
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
774 self.assertEqual(result, self.dup)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
775
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
776 def test_empty_difference_rev(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
777 result = empty_set - self.set
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
778 self.assertEqual(result, empty_set)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
779
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
780 def test_iteration(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
781 for v in self.set:
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
782 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
783 setiter = iter(self.set)
36484
20f75d5e79e9 Renamed _length_cue() to __length_hint__(). See:
Armin Rigo <arigo@tunes.org>
parents: 35959
diff changeset
784 # 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
785 # 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
786 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
787
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
788 def test_pickling(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
789 p = pickle.dumps(self.set)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
790 copy = pickle.loads(p)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
791 self.assertEqual(self.set, copy,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
792 "%s != %s" % (self.set, copy))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
793
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
794 #------------------------------------------------------------------------------
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
795
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
796 class TestBasicOpsEmpty(TestBasicOps):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
797 def setUp(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
798 self.case = "empty set"
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
799 self.values = []
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
800 self.set = set(self.values)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
801 self.dup = set(self.values)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
802 self.length = 0
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
803 self.repr = "set([])"
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
804
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
805 #------------------------------------------------------------------------------
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
806
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
807 class TestBasicOpsSingleton(TestBasicOps):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
808 def setUp(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
809 self.case = "unit set (number)"
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
810 self.values = [3]
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
811 self.set = set(self.values)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
812 self.dup = set(self.values)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
813 self.length = 1
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
814 self.repr = "set([3])"
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
815
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
816 def test_in(self):
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
817 self.assertIn(3, self.set)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
818
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
819 def test_not_in(self):
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
820 self.assertNotIn(2, self.set)
30929
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 #------------------------------------------------------------------------------
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
823
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
824 class TestBasicOpsTuple(TestBasicOps):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
825 def setUp(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
826 self.case = "unit set (tuple)"
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
827 self.values = [(0, "zero")]
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
828 self.set = set(self.values)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
829 self.dup = set(self.values)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
830 self.length = 1
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
831 self.repr = "set([(0, 'zero')])"
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
832
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
833 def test_in(self):
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
834 self.assertIn((0, "zero"), self.set)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
835
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
836 def test_not_in(self):
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
837 self.assertNotIn(9, self.set)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
838
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
839 #------------------------------------------------------------------------------
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 class TestBasicOpsTriple(TestBasicOps):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
842 def setUp(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
843 self.case = "triple set"
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
844 self.values = [0, "zero", operator.add]
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
845 self.set = set(self.values)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
846 self.dup = set(self.values)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
847 self.length = 3
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
848 self.repr = None
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
849
75100
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 68255
diff changeset
850 #------------------------------------------------------------------------------
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 68255
diff changeset
851
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 68255
diff changeset
852 class TestBasicOpsString(TestBasicOps):
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 68255
diff changeset
853 def setUp(self):
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 68255
diff changeset
854 self.case = "string set"
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 68255
diff changeset
855 self.values = ["a", "b", "c"]
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 68255
diff changeset
856 self.set = set(self.values)
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 68255
diff changeset
857 self.dup = set(self.values)
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 68255
diff changeset
858 self.length = 3
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 68255
diff changeset
859
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 68255
diff changeset
860 def test_repr(self):
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 68255
diff changeset
861 self.check_repr_against_values()
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 68255
diff changeset
862
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 68255
diff changeset
863 #------------------------------------------------------------------------------
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 68255
diff changeset
864
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 68255
diff changeset
865 class TestBasicOpsUnicode(TestBasicOps):
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 68255
diff changeset
866 def setUp(self):
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 68255
diff changeset
867 self.case = "unicode set"
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 68255
diff changeset
868 self.values = [u"a", u"b", u"c"]
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 68255
diff changeset
869 self.set = set(self.values)
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 68255
diff changeset
870 self.dup = set(self.values)
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 68255
diff changeset
871 self.length = 3
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 68255
diff changeset
872
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 68255
diff changeset
873 def test_repr(self):
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 68255
diff changeset
874 self.check_repr_against_values()
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 68255
diff changeset
875
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 68255
diff changeset
876 #------------------------------------------------------------------------------
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 68255
diff changeset
877
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 68255
diff changeset
878 class TestBasicOpsMixedStringUnicode(TestBasicOps):
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 68255
diff changeset
879 def setUp(self):
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 68255
diff changeset
880 self.case = "string and bytes set"
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 68255
diff changeset
881 self.values = ["a", "b", u"a", u"b"]
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 68255
diff changeset
882 self.set = set(self.values)
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 68255
diff changeset
883 self.dup = set(self.values)
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 68255
diff changeset
884 self.length = 4
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 68255
diff changeset
885
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 68255
diff changeset
886 def test_repr(self):
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 68255
diff changeset
887 with test_support.check_warnings():
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 68255
diff changeset
888 self.check_repr_against_values()
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 68255
diff changeset
889
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
890 #==============================================================================
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
891
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
892 def baditer():
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
893 raise TypeError
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
894 yield True
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
895
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
896 def gooditer():
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
897 yield True
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
898
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
899 class TestExceptionPropagation(unittest.TestCase):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
900 """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
901
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
902 def test_instanceWithException(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
903 self.assertRaises(TypeError, set, baditer())
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
904
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
905 def test_instancesWithoutException(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
906 # All of these iterables should load without exception.
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
907 set([1,2,3])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
908 set((1,2,3))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
909 set({'one':1, 'two':2, 'three':3})
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
910 set(xrange(3))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
911 set('abc')
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
912 set(gooditer())
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
913
35959
fca7337710fd Improve test coverage. Hope the test_file changes work the same on windows.
Neal Norwitz <nnorwitz@gmail.com>
parents: 35581
diff changeset
914 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
915 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
916 try:
fca7337710fd Improve test coverage. Hope the test_file changes work the same on windows.
Neal Norwitz <nnorwitz@gmail.com>
parents: 35581
diff changeset
917 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
918 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
919 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
920 pass
fca7337710fd Improve test coverage. Hope the test_file changes work the same on windows.
Neal Norwitz <nnorwitz@gmail.com>
parents: 35581
diff changeset
921 else:
fca7337710fd Improve test coverage. Hope the test_file changes work the same on windows.
Neal Norwitz <nnorwitz@gmail.com>
parents: 35581
diff changeset
922 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
923
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
924 #==============================================================================
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
925
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
926 class TestSetOfSets(unittest.TestCase):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
927 def test_constructor(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
928 inner = frozenset([1])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
929 outer = set([inner])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
930 element = outer.pop()
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
931 self.assertEqual(type(element), frozenset)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
932 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
933 outer.remove(inner)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
934 self.assertEqual(outer, set()) # Verify that remove worked
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
935 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
936
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
937 #==============================================================================
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
938
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
939 class TestBinaryOps(unittest.TestCase):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
940 def setUp(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
941 self.set = set((2, 4, 6))
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 test_eq(self): # SF bug 643115
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
944 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
945
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
946 def test_union_subset(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
947 result = self.set | set([2])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
948 self.assertEqual(result, set((2, 4, 6)))
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 def test_union_superset(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
951 result = self.set | set([2, 4, 6, 8])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
952 self.assertEqual(result, set([2, 4, 6, 8]))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
953
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
954 def test_union_overlap(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
955 result = self.set | set([3, 4, 5])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
956 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
957
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
958 def test_union_non_overlap(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
959 result = self.set | set([8])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
960 self.assertEqual(result, set([2, 4, 6, 8]))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
961
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
962 def test_intersection_subset(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
963 result = self.set & set((2, 4))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
964 self.assertEqual(result, set((2, 4)))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
965
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
966 def test_intersection_superset(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
967 result = self.set & set([2, 4, 6, 8])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
968 self.assertEqual(result, set([2, 4, 6]))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
969
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
970 def test_intersection_overlap(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
971 result = self.set & set([3, 4, 5])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
972 self.assertEqual(result, set([4]))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
973
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
974 def test_intersection_non_overlap(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
975 result = self.set & set([8])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
976 self.assertEqual(result, empty_set)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
977
43794
2b3abe3d3ae6 Add set.isdisjoint()
Raymond Hettinger <python@rcn.com>
parents: 41532
diff changeset
978 def test_isdisjoint_subset(self):
2b3abe3d3ae6 Add set.isdisjoint()
Raymond Hettinger <python@rcn.com>
parents: 41532
diff changeset
979 result = self.set.isdisjoint(set((2, 4)))
2b3abe3d3ae6 Add set.isdisjoint()
Raymond Hettinger <python@rcn.com>
parents: 41532
diff changeset
980 self.assertEqual(result, False)
2b3abe3d3ae6 Add set.isdisjoint()
Raymond Hettinger <python@rcn.com>
parents: 41532
diff changeset
981
2b3abe3d3ae6 Add set.isdisjoint()
Raymond Hettinger <python@rcn.com>
parents: 41532
diff changeset
982 def test_isdisjoint_superset(self):
2b3abe3d3ae6 Add set.isdisjoint()
Raymond Hettinger <python@rcn.com>
parents: 41532
diff changeset
983 result = self.set.isdisjoint(set([2, 4, 6, 8]))
2b3abe3d3ae6 Add set.isdisjoint()
Raymond Hettinger <python@rcn.com>
parents: 41532
diff changeset
984 self.assertEqual(result, False)
2b3abe3d3ae6 Add set.isdisjoint()
Raymond Hettinger <python@rcn.com>
parents: 41532
diff changeset
985
2b3abe3d3ae6 Add set.isdisjoint()
Raymond Hettinger <python@rcn.com>
parents: 41532
diff changeset
986 def test_isdisjoint_overlap(self):
2b3abe3d3ae6 Add set.isdisjoint()
Raymond Hettinger <python@rcn.com>
parents: 41532
diff changeset
987 result = self.set.isdisjoint(set([3, 4, 5]))
2b3abe3d3ae6 Add set.isdisjoint()
Raymond Hettinger <python@rcn.com>
parents: 41532
diff changeset
988 self.assertEqual(result, False)
2b3abe3d3ae6 Add set.isdisjoint()
Raymond Hettinger <python@rcn.com>
parents: 41532
diff changeset
989
2b3abe3d3ae6 Add set.isdisjoint()
Raymond Hettinger <python@rcn.com>
parents: 41532
diff changeset
990 def test_isdisjoint_non_overlap(self):
2b3abe3d3ae6 Add set.isdisjoint()
Raymond Hettinger <python@rcn.com>
parents: 41532
diff changeset
991 result = self.set.isdisjoint(set([8]))
2b3abe3d3ae6 Add set.isdisjoint()
Raymond Hettinger <python@rcn.com>
parents: 41532
diff changeset
992 self.assertEqual(result, True)
2b3abe3d3ae6 Add set.isdisjoint()
Raymond Hettinger <python@rcn.com>
parents: 41532
diff changeset
993
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
994 def test_sym_difference_subset(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
995 result = self.set ^ set((2, 4))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
996 self.assertEqual(result, set([6]))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
997
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
998 def test_sym_difference_superset(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
999 result = self.set ^ set((2, 4, 6, 8))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1000 self.assertEqual(result, set([8]))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1001
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1002 def test_sym_difference_overlap(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1003 result = self.set ^ set((3, 4, 5))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1004 self.assertEqual(result, set([2, 3, 5, 6]))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1005
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1006 def test_sym_difference_non_overlap(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1007 result = self.set ^ set([8])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1008 self.assertEqual(result, set([2, 4, 6, 8]))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1009
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1010 def test_cmp(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1011 a, b = set('a'), set('b')
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1012 self.assertRaises(TypeError, cmp, a, b)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1013
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1014 # You can view this as a buglet: cmp(a, a) does not raise TypeError,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1015 # because __eq__ is tried before __cmp__, and a.__eq__(a) returns True,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1016 # which Python thinks is good enough to synthesize a cmp() result
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1017 # without calling __cmp__.
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1018 self.assertEqual(cmp(a, a), 0)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1019
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1020 self.assertRaises(TypeError, cmp, a, 12)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1021 self.assertRaises(TypeError, cmp, "abc", a)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1022
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1023 #==============================================================================
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1024
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1025 class TestUpdateOps(unittest.TestCase):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1026 def setUp(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1027 self.set = set((2, 4, 6))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1028
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1029 def test_union_subset(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1030 self.set |= set([2])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1031 self.assertEqual(self.set, set((2, 4, 6)))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1032
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1033 def test_union_superset(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1034 self.set |= set([2, 4, 6, 8])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1035 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
1036
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1037 def test_union_overlap(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1038 self.set |= set([3, 4, 5])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1039 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
1040
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1041 def test_union_non_overlap(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1042 self.set |= set([8])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1043 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
1044
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1045 def test_union_method_call(self):
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
1046 self.set.update(set([3, 4, 5]))
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1047 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
1048
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1049 def test_intersection_subset(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1050 self.set &= set((2, 4))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1051 self.assertEqual(self.set, set((2, 4)))
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_intersection_superset(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1054 self.set &= set([2, 4, 6, 8])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1055 self.assertEqual(self.set, set([2, 4, 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_intersection_overlap(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1058 self.set &= set([3, 4, 5])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1059 self.assertEqual(self.set, set([4]))
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 def test_intersection_non_overlap(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1062 self.set &= set([8])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1063 self.assertEqual(self.set, empty_set)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1064
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1065 def test_intersection_method_call(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1066 self.set.intersection_update(set([3, 4, 5]))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1067 self.assertEqual(self.set, set([4]))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1068
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1069 def test_sym_difference_subset(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1070 self.set ^= set((2, 4))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1071 self.assertEqual(self.set, set([6]))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1072
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1073 def test_sym_difference_superset(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1074 self.set ^= set((2, 4, 6, 8))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1075 self.assertEqual(self.set, set([8]))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1076
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1077 def test_sym_difference_overlap(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1078 self.set ^= set((3, 4, 5))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1079 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
1080
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1081 def test_sym_difference_non_overlap(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1082 self.set ^= set([8])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1083 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
1084
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1085 def test_sym_difference_method_call(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1086 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
1087 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
1088
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1089 def test_difference_subset(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1090 self.set -= set((2, 4))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1091 self.assertEqual(self.set, set([6]))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1092
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1093 def test_difference_superset(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1094 self.set -= set((2, 4, 6, 8))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1095 self.assertEqual(self.set, set([]))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1096
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1097 def test_difference_overlap(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1098 self.set -= set((3, 4, 5))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1099 self.assertEqual(self.set, set([2, 6]))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1100
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1101 def test_difference_non_overlap(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1102 self.set -= set([8])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1103 self.assertEqual(self.set, set([2, 4, 6]))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1104
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1105 def test_difference_method_call(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1106 self.set.difference_update(set([3, 4, 5]))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1107 self.assertEqual(self.set, set([2, 6]))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1108
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1109 #==============================================================================
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1110
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1111 class TestMutate(unittest.TestCase):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1112 def setUp(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1113 self.values = ["a", "b", "c"]
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1114 self.set = set(self.values)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1115
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1116 def test_add_present(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1117 self.set.add("c")
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1118 self.assertEqual(self.set, set("abc"))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1119
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1120 def test_add_absent(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1121 self.set.add("d")
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1122 self.assertEqual(self.set, set("abcd"))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1123
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1124 def test_add_until_full(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1125 tmp = set()
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1126 expected_len = 0
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1127 for v in self.values:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1128 tmp.add(v)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1129 expected_len += 1
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1130 self.assertEqual(len(tmp), expected_len)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1131 self.assertEqual(tmp, self.set)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1132
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1133 def test_remove_present(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1134 self.set.remove("b")
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1135 self.assertEqual(self.set, set("ac"))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1136
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1137 def test_remove_absent(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1138 try:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1139 self.set.remove("d")
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1140 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
1141 except LookupError:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1142 pass
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1143
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1144 def test_remove_until_empty(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1145 expected_len = len(self.set)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1146 for v in self.values:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1147 self.set.remove(v)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1148 expected_len -= 1
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1149 self.assertEqual(len(self.set), expected_len)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1150
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1151 def test_discard_present(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1152 self.set.discard("c")
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1153 self.assertEqual(self.set, set("ab"))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1154
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1155 def test_discard_absent(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1156 self.set.discard("d")
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1157 self.assertEqual(self.set, set("abc"))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1158
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1159 def test_clear(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1160 self.set.clear()
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1161 self.assertEqual(len(self.set), 0)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1162
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1163 def test_pop(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1164 popped = {}
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1165 while self.set:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1166 popped[self.set.pop()] = None
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1167 self.assertEqual(len(popped), len(self.values))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1168 for v in self.values:
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
1169 self.assertIn(v, popped)
30929
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_update_empty_tuple(self):
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
1172 self.set.update(())
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1173 self.assertEqual(self.set, set(self.values))
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_update_unit_tuple_overlap(self):
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
1176 self.set.update(("a",))
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1177 self.assertEqual(self.set, set(self.values))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1178
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1179 def test_update_unit_tuple_non_overlap(self):
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
1180 self.set.update(("a", "z"))
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1181 self.assertEqual(self.set, set(self.values + ["z"]))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1182
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1183 #==============================================================================
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1184
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1185 class TestSubsets(unittest.TestCase):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1186
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1187 case2method = {"<=": "issubset",
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1188 ">=": "issuperset",
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1189 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1190
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1191 reverse = {"==": "==",
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 "<": ">",
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1194 ">": "<",
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1195 "<=": ">=",
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 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1198
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1199 def test_issubset(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1200 x = self.left
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1201 y = self.right
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1202 for case in "!=", "==", "<", "<=", ">", ">=":
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1203 expected = case in self.cases
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1204 # Test the binary infix spelling.
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1205 result = eval("x" + case + "y", locals())
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1206 self.assertEqual(result, expected)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1207 # 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
1208 if case in TestSubsets.case2method:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1209 method = getattr(x, TestSubsets.case2method[case])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1210 result = method(y)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1211 self.assertEqual(result, expected)
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 # Now do the same for the operands reversed.
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1214 rcase = TestSubsets.reverse[case]
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1215 result = eval("y" + rcase + "x", locals())
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1216 self.assertEqual(result, expected)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1217 if rcase in TestSubsets.case2method:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1218 method = getattr(y, TestSubsets.case2method[rcase])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1219 result = method(x)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1220 self.assertEqual(result, expected)
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 TestSubsetEqualEmpty(TestSubsets):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1224 left = set()
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1225 right = set()
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1226 name = "both empty"
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1227 cases = "==", "<=", ">="
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 #------------------------------------------------------------------------------
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 class TestSubsetEqualNonEmpty(TestSubsets):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1232 left = set([1, 2])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1233 right = set([1, 2])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1234 name = "equal pair"
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1235 cases = "==", "<=", ">="
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 #------------------------------------------------------------------------------
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1238
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1239 class TestSubsetEmptyNonEmpty(TestSubsets):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1240 left = set()
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1241 right = set([1, 2])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1242 name = "one empty, one non-empty"
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1243 cases = "!=", "<", "<="
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1244
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1245 #------------------------------------------------------------------------------
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1246
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1247 class TestSubsetPartial(TestSubsets):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1248 left = set([1])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1249 right = set([1, 2])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1250 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
1251 cases = "!=", "<", "<="
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1252
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1253 #------------------------------------------------------------------------------
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1254
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1255 class TestSubsetNonOverlap(TestSubsets):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1256 left = set([1])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1257 right = set([2])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1258 name = "neither empty, neither contains"
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1259 cases = "!="
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 #==============================================================================
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1262
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1263 class TestOnlySetsInBinaryOps(unittest.TestCase):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1264
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1265 def test_eq_ne(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1266 # 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
1267 self.assertEqual(self.other == self.set, False)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1268 self.assertEqual(self.set == self.other, False)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1269 self.assertEqual(self.other != self.set, True)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1270 self.assertEqual(self.set != self.other, True)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1271
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1272 def test_ge_gt_le_lt(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1273 self.assertRaises(TypeError, lambda: self.set < self.other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1274 self.assertRaises(TypeError, lambda: self.set <= self.other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1275 self.assertRaises(TypeError, lambda: self.set > self.other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1276 self.assertRaises(TypeError, lambda: self.set >= self.other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1277
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1278 self.assertRaises(TypeError, lambda: self.other < self.set)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1279 self.assertRaises(TypeError, lambda: self.other <= self.set)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1280 self.assertRaises(TypeError, lambda: self.other > self.set)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1281 self.assertRaises(TypeError, lambda: self.other >= self.set)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1282
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
1283 def test_update_operator(self):
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1284 try:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1285 self.set |= self.other
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1286 except TypeError:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1287 pass
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1288 else:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1289 self.fail("expected TypeError")
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1290
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
1291 def test_update(self):
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1292 if self.otherIsIterable:
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
1293 self.set.update(self.other)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1294 else:
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
1295 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
1296
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1297 def test_union(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1298 self.assertRaises(TypeError, lambda: self.set | self.other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1299 self.assertRaises(TypeError, lambda: self.other | self.set)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1300 if self.otherIsIterable:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1301 self.set.union(self.other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1302 else:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1303 self.assertRaises(TypeError, self.set.union, self.other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1304
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1305 def test_intersection_update_operator(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1306 try:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1307 self.set &= self.other
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1308 except TypeError:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1309 pass
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1310 else:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1311 self.fail("expected TypeError")
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1312
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1313 def test_intersection_update(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1314 if self.otherIsIterable:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1315 self.set.intersection_update(self.other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1316 else:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1317 self.assertRaises(TypeError,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1318 self.set.intersection_update,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1319 self.other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1320
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1321 def test_intersection(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1322 self.assertRaises(TypeError, lambda: self.set & self.other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1323 self.assertRaises(TypeError, lambda: self.other & self.set)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1324 if self.otherIsIterable:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1325 self.set.intersection(self.other)
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.assertRaises(TypeError, self.set.intersection, self.other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1328
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1329 def test_sym_difference_update_operator(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1330 try:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1331 self.set ^= self.other
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1332 except TypeError:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1333 pass
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1334 else:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1335 self.fail("expected TypeError")
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1336
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1337 def test_sym_difference_update(self):
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.symmetric_difference_update(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,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1342 self.set.symmetric_difference_update,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1343 self.other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1344
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1345 def test_sym_difference(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1346 self.assertRaises(TypeError, lambda: self.set ^ self.other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1347 self.assertRaises(TypeError, lambda: self.other ^ self.set)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1348 if self.otherIsIterable:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1349 self.set.symmetric_difference(self.other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1350 else:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1351 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
1352
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1353 def test_difference_update_operator(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1354 try:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1355 self.set -= self.other
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1356 except TypeError:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1357 pass
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1358 else:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1359 self.fail("expected TypeError")
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1360
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1361 def test_difference_update(self):
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.difference_update(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,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1366 self.set.difference_update,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1367 self.other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1368
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1369 def test_difference(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1370 self.assertRaises(TypeError, lambda: self.set - self.other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1371 self.assertRaises(TypeError, lambda: self.other - self.set)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1372 if self.otherIsIterable:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1373 self.set.difference(self.other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1374 else:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1375 self.assertRaises(TypeError, self.set.difference, self.other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1376
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1377 #------------------------------------------------------------------------------
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1378
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1379 class TestOnlySetsNumeric(TestOnlySetsInBinaryOps):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1380 def setUp(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1381 self.set = set((1, 2, 3))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1382 self.other = 19
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1383 self.otherIsIterable = False
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1384
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1385 #------------------------------------------------------------------------------
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1386
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1387 class TestOnlySetsDict(TestOnlySetsInBinaryOps):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1388 def setUp(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1389 self.set = set((1, 2, 3))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1390 self.other = {1:2, 3:4}
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1391 self.otherIsIterable = True
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1392
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1393 #------------------------------------------------------------------------------
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1394
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1395 class TestOnlySetsOperator(TestOnlySetsInBinaryOps):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1396 def setUp(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1397 self.set = set((1, 2, 3))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1398 self.other = operator.add
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1399 self.otherIsIterable = False
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1400
63572
51102abdec50 Merged revisions 79165 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 63511
diff changeset
1401 def test_ge_gt_le_lt(self):
59783
c8c840162804 #7092 - Silence more py3k deprecation warnings, using test_support.check_py3k_warnings() helper.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59663
diff changeset
1402 with test_support.check_py3k_warnings():
63572
51102abdec50 Merged revisions 79165 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 63511
diff changeset
1403 super(TestOnlySetsOperator, self).test_ge_gt_le_lt()
51102abdec50 Merged revisions 79165 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 63511
diff changeset
1404
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1405 #------------------------------------------------------------------------------
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 class TestOnlySetsTuple(TestOnlySetsInBinaryOps):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1408 def setUp(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1409 self.set = set((1, 2, 3))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1410 self.other = (2, 4, 6)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1411 self.otherIsIterable = True
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1412
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1413 #------------------------------------------------------------------------------
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 class TestOnlySetsString(TestOnlySetsInBinaryOps):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1416 def setUp(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1417 self.set = set((1, 2, 3))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1418 self.other = 'abc'
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1419 self.otherIsIterable = True
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1420
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1421 #------------------------------------------------------------------------------
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 class TestOnlySetsGenerator(TestOnlySetsInBinaryOps):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1424 def setUp(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1425 def gen():
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1426 for i in xrange(0, 10, 2):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1427 yield i
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1428 self.set = set((1, 2, 3))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1429 self.other = gen()
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1430 self.otherIsIterable = True
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
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1434 class TestCopying(unittest.TestCase):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1435
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1436 def test_copy(self):
59663
5b888f590fe2 #7092: silence some more py3k warnings.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 59547
diff changeset
1437 dup = list(self.set.copy())
5b888f590fe2 #7092: silence some more py3k warnings.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 59547
diff changeset
1438 self.assertEqual(len(dup), len(self.set))
5b888f590fe2 #7092: silence some more py3k warnings.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 59547
diff changeset
1439 for el in self.set:
5b888f590fe2 #7092: silence some more py3k warnings.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 59547
diff changeset
1440 self.assertIn(el, dup)
5b888f590fe2 #7092: silence some more py3k warnings.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 59547
diff changeset
1441 pos = dup.index(el)
5b888f590fe2 #7092: silence some more py3k warnings.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 59547
diff changeset
1442 self.assertIs(el, dup.pop(pos))
5b888f590fe2 #7092: silence some more py3k warnings.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 59547
diff changeset
1443 self.assertFalse(dup)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1444
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1445 def test_deep_copy(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1446 dup = copy.deepcopy(self.set)
59663
5b888f590fe2 #7092: silence some more py3k warnings.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 59547
diff changeset
1447 self.assertSetEqual(dup, self.set)
30929
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 #------------------------------------------------------------------------------
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1450
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1451 class TestCopyingEmpty(TestCopying):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1452 def setUp(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1453 self.set = set()
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 TestCopyingSingleton(TestCopying):
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 self.set = set(["hello"])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1460
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1461 #------------------------------------------------------------------------------
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1462
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1463 class TestCopyingTriple(TestCopying):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1464 def setUp(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1465 self.set = set(["zero", 0, None])
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
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1469 class TestCopyingTuple(TestCopying):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1470 def setUp(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1471 self.set = set([(1, 2)])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1472
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1473 #------------------------------------------------------------------------------
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1474
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1475 class TestCopyingNested(TestCopying):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1476 def setUp(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1477 self.set = set([((1, 2), (3, 4))])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1478
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1479 #==============================================================================
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1480
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1481 class TestIdentities(unittest.TestCase):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1482 def setUp(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1483 self.a = set('abracadabra')
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1484 self.b = set('alacazam')
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1485
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1486 def test_binopsVsSubsets(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1487 a, b = self.a, self.b
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 50522
diff changeset
1488 self.assertTrue(a - b < a)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 50522
diff changeset
1489 self.assertTrue(b - a < b)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 50522
diff changeset
1490 self.assertTrue(a & b < a)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 50522
diff changeset
1491 self.assertTrue(a & b < b)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 50522
diff changeset
1492 self.assertTrue(a | b > a)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 50522
diff changeset
1493 self.assertTrue(a | b > b)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 50522
diff changeset
1494 self.assertTrue(a ^ b < a | b)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1495
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1496 def test_commutativity(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1497 a, b = self.a, self.b
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1498 self.assertEqual(a&b, b&a)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1499 self.assertEqual(a|b, b|a)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1500 self.assertEqual(a^b, b^a)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1501 if a != b:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1502 self.assertNotEqual(a-b, b-a)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1503
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1504 def test_summations(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1505 # check that sums of parts equal the whole
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1506 a, b = self.a, self.b
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1507 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
1508 self.assertEqual((a&b)|(a^b), a|b)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1509 self.assertEqual(a|(b-a), a|b)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1510 self.assertEqual((a-b)|b, a|b)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1511 self.assertEqual((a-b)|(a&b), a)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1512 self.assertEqual((b-a)|(a&b), b)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1513 self.assertEqual((a-b)|(b-a), a^b)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1514
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1515 def test_exclusion(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1516 # check that inverse operations show non-overlap
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1517 a, b, zero = self.a, self.b, set()
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1518 self.assertEqual((a-b)&b, zero)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1519 self.assertEqual((b-a)&a, zero)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1520 self.assertEqual((a&b)&(a^b), zero)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1521
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1522 # Tests derived from test_itertools.py =======================================
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 R(seqn):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1525 'Regular generator'
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1526 for i in seqn:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1527 yield i
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1528
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1529 class G:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1530 'Sequence using __getitem__'
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1531 def __init__(self, seqn):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1532 self.seqn = seqn
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1533 def __getitem__(self, i):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1534 return self.seqn[i]
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1535
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1536 class I:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1537 'Sequence using iterator protocol'
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1538 def __init__(self, seqn):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1539 self.seqn = seqn
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1540 self.i = 0
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1541 def __iter__(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1542 return self
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1543 def next(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1544 if self.i >= len(self.seqn): raise StopIteration
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1545 v = self.seqn[self.i]
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1546 self.i += 1
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1547 return v
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1548
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1549 class Ig:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1550 'Sequence using iterator protocol defined with a generator'
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1551 def __init__(self, seqn):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1552 self.seqn = seqn
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1553 self.i = 0
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1554 def __iter__(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1555 for val in self.seqn:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1556 yield val
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1557
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1558 class X:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1559 'Missing __getitem__ and __iter__'
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1560 def __init__(self, seqn):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1561 self.seqn = seqn
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1562 self.i = 0
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1563 def next(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1564 if self.i >= len(self.seqn): raise StopIteration
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1565 v = self.seqn[self.i]
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1566 self.i += 1
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1567 return v
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1568
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1569 class N:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1570 'Iterator missing next()'
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1571 def __init__(self, seqn):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1572 self.seqn = seqn
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1573 self.i = 0
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1574 def __iter__(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1575 return self
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1576
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1577 class E:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1578 'Test propagation of exceptions'
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1579 def __init__(self, seqn):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1580 self.seqn = seqn
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1581 self.i = 0
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1582 def __iter__(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1583 return self
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1584 def next(self):
33597
355b15e4a4da Use floor division operator.
Raymond Hettinger <python@rcn.com>
parents: 32555
diff changeset
1585 3 // 0
30929
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 S:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1588 'Test immediate stop'
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 pass
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1591 def __iter__(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1592 return self
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1593 def next(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1594 raise StopIteration
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 from itertools import chain, imap
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1597 def L(seqn):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1598 'Test multiple tiers of iterators'
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1599 return chain(imap(lambda x:x, R(Ig(G(seqn)))))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1600
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1601 class TestVariousIteratorArgs(unittest.TestCase):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1602
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1603 def test_constructor(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1604 for cons in (set, frozenset):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1605 for s in ("123", "", range(1000), ('do', 1.2), xrange(2000,2200,5)):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1606 for g in (G, I, Ig, S, L, R):
59663
5b888f590fe2 #7092: silence some more py3k warnings.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 59547
diff changeset
1607 self.assertSetEqual(cons(g(s)), set(g(s)))
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1608 self.assertRaises(TypeError, cons , X(s))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1609 self.assertRaises(TypeError, cons , N(s))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1610 self.assertRaises(ZeroDivisionError, cons , E(s))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1611
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1612 def test_inline_methods(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1613 s = set('november')
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1614 for data in ("123", "", range(1000), ('do', 1.2), xrange(2000,2200,5), 'december'):
43794
2b3abe3d3ae6 Add set.isdisjoint()
Raymond Hettinger <python@rcn.com>
parents: 41532
diff changeset
1615 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
1616 for g in (G, I, Ig, L, R):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1617 expected = meth(data)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1618 actual = meth(G(data))
43794
2b3abe3d3ae6 Add set.isdisjoint()
Raymond Hettinger <python@rcn.com>
parents: 41532
diff changeset
1619 if isinstance(expected, bool):
2b3abe3d3ae6 Add set.isdisjoint()
Raymond Hettinger <python@rcn.com>
parents: 41532
diff changeset
1620 self.assertEqual(actual, expected)
2b3abe3d3ae6 Add set.isdisjoint()
Raymond Hettinger <python@rcn.com>
parents: 41532
diff changeset
1621 else:
59663
5b888f590fe2 #7092: silence some more py3k warnings.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 59547
diff changeset
1622 self.assertSetEqual(actual, expected)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1623 self.assertRaises(TypeError, meth, X(s))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1624 self.assertRaises(TypeError, meth, N(s))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1625 self.assertRaises(ZeroDivisionError, meth, E(s))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1626
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1627 def test_inplace_methods(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1628 for data in ("123", "", range(1000), ('do', 1.2), xrange(2000,2200,5), 'december'):
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
1629 for methname in ('update', 'intersection_update',
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1630 'difference_update', 'symmetric_difference_update'):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1631 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
1632 s = set('january')
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1633 t = s.copy()
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1634 getattr(s, methname)(list(g(data)))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1635 getattr(t, methname)(g(data))
59663
5b888f590fe2 #7092: silence some more py3k warnings.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 59547
diff changeset
1636 self.assertSetEqual(s, t)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1637
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1638 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
1639 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
1640 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
1641
68856
24179f82b7de Fix obscure set crashers (#4420). Backport of d56b3cafb1e6, reviewed by Raymond.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
1642 class bad_eq:
24179f82b7de Fix obscure set crashers (#4420). Backport of d56b3cafb1e6, reviewed by Raymond.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
1643 def __eq__(self, other):
24179f82b7de Fix obscure set crashers (#4420). Backport of d56b3cafb1e6, reviewed by Raymond.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
1644 if be_bad:
24179f82b7de Fix obscure set crashers (#4420). Backport of d56b3cafb1e6, reviewed by Raymond.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
1645 set2.clear()
24179f82b7de Fix obscure set crashers (#4420). Backport of d56b3cafb1e6, reviewed by Raymond.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
1646 raise ZeroDivisionError
24179f82b7de Fix obscure set crashers (#4420). Backport of d56b3cafb1e6, reviewed by Raymond.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
1647 return self is other
24179f82b7de Fix obscure set crashers (#4420). Backport of d56b3cafb1e6, reviewed by Raymond.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
1648 def __hash__(self):
24179f82b7de Fix obscure set crashers (#4420). Backport of d56b3cafb1e6, reviewed by Raymond.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
1649 return 0
24179f82b7de Fix obscure set crashers (#4420). Backport of d56b3cafb1e6, reviewed by Raymond.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
1650
24179f82b7de Fix obscure set crashers (#4420). Backport of d56b3cafb1e6, reviewed by Raymond.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
1651 class bad_dict_clear:
24179f82b7de Fix obscure set crashers (#4420). Backport of d56b3cafb1e6, reviewed by Raymond.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
1652 def __eq__(self, other):
24179f82b7de Fix obscure set crashers (#4420). Backport of d56b3cafb1e6, reviewed by Raymond.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
1653 if be_bad:
24179f82b7de Fix obscure set crashers (#4420). Backport of d56b3cafb1e6, reviewed by Raymond.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
1654 dict2.clear()
24179f82b7de Fix obscure set crashers (#4420). Backport of d56b3cafb1e6, reviewed by Raymond.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
1655 return self is other
24179f82b7de Fix obscure set crashers (#4420). Backport of d56b3cafb1e6, reviewed by Raymond.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
1656 def __hash__(self):
24179f82b7de Fix obscure set crashers (#4420). Backport of d56b3cafb1e6, reviewed by Raymond.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
1657 return 0
24179f82b7de Fix obscure set crashers (#4420). Backport of d56b3cafb1e6, reviewed by Raymond.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
1658
24179f82b7de Fix obscure set crashers (#4420). Backport of d56b3cafb1e6, reviewed by Raymond.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
1659 class TestWeirdBugs(unittest.TestCase):
24179f82b7de Fix obscure set crashers (#4420). Backport of d56b3cafb1e6, reviewed by Raymond.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
1660 def test_8420_set_merge(self):
24179f82b7de Fix obscure set crashers (#4420). Backport of d56b3cafb1e6, reviewed by Raymond.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
1661 # This used to segfault
24179f82b7de Fix obscure set crashers (#4420). Backport of d56b3cafb1e6, reviewed by Raymond.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
1662 global be_bad, set2, dict2
24179f82b7de Fix obscure set crashers (#4420). Backport of d56b3cafb1e6, reviewed by Raymond.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
1663 be_bad = False
24179f82b7de Fix obscure set crashers (#4420). Backport of d56b3cafb1e6, reviewed by Raymond.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
1664 set1 = {bad_eq()}
24179f82b7de Fix obscure set crashers (#4420). Backport of d56b3cafb1e6, reviewed by Raymond.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
1665 set2 = {bad_eq() for i in range(75)}
24179f82b7de Fix obscure set crashers (#4420). Backport of d56b3cafb1e6, reviewed by Raymond.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
1666 be_bad = True
24179f82b7de Fix obscure set crashers (#4420). Backport of d56b3cafb1e6, reviewed by Raymond.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
1667 self.assertRaises(ZeroDivisionError, set1.update, set2)
24179f82b7de Fix obscure set crashers (#4420). Backport of d56b3cafb1e6, reviewed by Raymond.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
1668
24179f82b7de Fix obscure set crashers (#4420). Backport of d56b3cafb1e6, reviewed by Raymond.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
1669 be_bad = False
24179f82b7de Fix obscure set crashers (#4420). Backport of d56b3cafb1e6, reviewed by Raymond.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
1670 set1 = {bad_dict_clear()}
24179f82b7de Fix obscure set crashers (#4420). Backport of d56b3cafb1e6, reviewed by Raymond.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
1671 dict2 = {bad_dict_clear(): None}
24179f82b7de Fix obscure set crashers (#4420). Backport of d56b3cafb1e6, reviewed by Raymond.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
1672 be_bad = True
24179f82b7de Fix obscure set crashers (#4420). Backport of d56b3cafb1e6, reviewed by Raymond.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
1673 set1.symmetric_difference_update(dict2)
24179f82b7de Fix obscure set crashers (#4420). Backport of d56b3cafb1e6, reviewed by Raymond.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
1674
44926
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1675 # Application tests (based on David Eppstein's graph recipes ====================================
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1676
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1677 def powerset(U):
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1678 """Generates all subsets of a set or sequence U."""
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1679 U = iter(U)
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1680 try:
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1681 x = frozenset([U.next()])
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1682 for S in powerset(U):
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1683 yield S
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1684 yield S | x
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1685 except StopIteration:
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1686 yield frozenset()
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1687
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1688 def cube(n):
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1689 """Graph of n-dimensional hypercube."""
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1690 singletons = [frozenset([x]) for x in range(n)]
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1691 return dict([(x, frozenset([x^s for s in singletons]))
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1692 for x in powerset(range(n))])
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1693
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1694 def linegraph(G):
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1695 """Graph, the vertices of which are edges of G,
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1696 with two vertices being adjacent iff the corresponding
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1697 edges share a vertex."""
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1698 L = {}
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1699 for x in G:
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1700 for y in G[x]:
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1701 nx = [frozenset([x,z]) for z in G[x] if z != y]
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1702 ny = [frozenset([y,z]) for z in G[y] if z != x]
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1703 L[frozenset([x,y])] = frozenset(nx+ny)
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1704 return L
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1705
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1706 def faces(G):
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1707 'Return a set of faces in G. Where a face is a set of vertices on that face'
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1708 # currently limited to triangles,squares, and pentagons
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1709 f = set()
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1710 for v1, edges in G.items():
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1711 for v2 in edges:
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1712 for v3 in G[v2]:
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1713 if v1 == v3:
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1714 continue
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1715 if v1 in G[v3]:
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1716 f.add(frozenset([v1, v2, v3]))
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1717 else:
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1718 for v4 in G[v3]:
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1719 if v4 == v2:
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1720 continue
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1721 if v1 in G[v4]:
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1722 f.add(frozenset([v1, v2, v3, v4]))
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1723 else:
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1724 for v5 in G[v4]:
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1725 if v5 == v3 or v5 == v2:
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1726 continue
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1727 if v1 in G[v5]:
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1728 f.add(frozenset([v1, v2, v3, v4, v5]))
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1729 return f
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1730
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1731
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1732 class TestGraphs(unittest.TestCase):
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1733
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1734 def test_cube(self):
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1735
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1736 g = cube(3) # vert --> {v1, v2, v3}
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1737 vertices1 = set(g)
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1738 self.assertEqual(len(vertices1), 8) # eight vertices
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1739 for edge in g.values():
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1740 self.assertEqual(len(edge), 3) # each vertex connects to three edges
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1741 vertices2 = set(v for edges in g.values() for v in edges)
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1742 self.assertEqual(vertices1, vertices2) # edge vertices in original set
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1743
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1744 cubefaces = faces(g)
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1745 self.assertEqual(len(cubefaces), 6) # six faces
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1746 for face in cubefaces:
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1747 self.assertEqual(len(face), 4) # each face is a square
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1748
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1749 def test_cuboctahedron(self):
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1750
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1751 # http://en.wikipedia.org/wiki/Cuboctahedron
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1752 # 8 triangular faces and 6 square faces
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1753 # 12 indentical vertices each connecting a triangle and square
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1754
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1755 g = cube(3)
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1756 cuboctahedron = linegraph(g) # V( --> {V1, V2, V3, V4}
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1757 self.assertEqual(len(cuboctahedron), 12)# twelve vertices
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1758
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1759 vertices = set(cuboctahedron)
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1760 for edges in cuboctahedron.values():
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1761 self.assertEqual(len(edges), 4) # each vertex connects to four other vertices
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1762 othervertices = set(edge for edges in cuboctahedron.values() for edge in edges)
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1763 self.assertEqual(vertices, othervertices) # edge vertices in original set
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1764
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1765 cubofaces = faces(cuboctahedron)
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1766 facesizes = collections.defaultdict(int)
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1767 for face in cubofaces:
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1768 facesizes[len(face)] += 1
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1769 self.assertEqual(facesizes[3], 8) # eight triangular faces
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1770 self.assertEqual(facesizes[4], 6) # six square faces
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1771
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1772 for vertex in cuboctahedron:
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1773 edge = vertex # Cuboctahedron vertices are edges in Cube
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1774 self.assertEqual(len(edge), 2) # Two cube vertices define an edge
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1775 for cubevert in edge:
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
1776 self.assertIn(cubevert, g)
44926
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1777
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1778
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1779 #==============================================================================
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1780
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1781 def test_main(verbose=None):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1782 test_classes = (
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1783 TestSet,
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30929
diff changeset
1784 TestSetSubclass,
41533
7ded787e6311 Whitespace normalization
Neal Norwitz <nnorwitz@gmail.com>
parents: 41271
diff changeset
1785 TestSetSubclassWithKeywordArgs,
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1786 TestFrozenSet,
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30929
diff changeset
1787 TestFrozenSetSubclass,
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1788 TestSetOfSets,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1789 TestExceptionPropagation,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1790 TestBasicOpsEmpty,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1791 TestBasicOpsSingleton,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1792 TestBasicOpsTuple,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1793 TestBasicOpsTriple,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1794 TestBinaryOps,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1795 TestUpdateOps,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1796 TestMutate,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1797 TestSubsetEqualEmpty,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1798 TestSubsetEqualNonEmpty,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1799 TestSubsetEmptyNonEmpty,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1800 TestSubsetPartial,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1801 TestSubsetNonOverlap,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1802 TestOnlySetsNumeric,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1803 TestOnlySetsDict,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1804 TestOnlySetsOperator,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1805 TestOnlySetsTuple,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1806 TestOnlySetsString,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1807 TestOnlySetsGenerator,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1808 TestCopyingEmpty,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1809 TestCopyingSingleton,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1810 TestCopyingTriple,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1811 TestCopyingTuple,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1812 TestCopyingNested,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1813 TestIdentities,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1814 TestVariousIteratorArgs,
44926
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1815 TestGraphs,
68856
24179f82b7de Fix obscure set crashers (#4420). Backport of d56b3cafb1e6, reviewed by Raymond.
Éric Araujo <merwok@netwok.org>
parents: 68256
diff changeset
1816 TestWeirdBugs,
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 test_support.run_unittest(*test_classes)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1820
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1821 # verify reference counting
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1822 if verbose and hasattr(sys, "gettotalrefcount"):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1823 import gc
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1824 counts = [None] * 5
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1825 for i in xrange(len(counts)):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1826 test_support.run_unittest(*test_classes)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1827 gc.collect()
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1828 counts[i] = sys.gettotalrefcount()
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1829 print counts
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1830
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1831 if __name__ == "__main__":
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1832 test_main(verbose=True)