annotate Lib/test/test_set.py @ 58925:eecf1bef052d 2.6

Merged revisions 78207 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r78207 | ezio.melotti | 2010-02-17 01:26:09 +0200 (Wed, 17 Feb 2010) | 1 line #7930: fix stripid ........
author Ezio Melotti <ezio.melotti@gmail.com>
date Tue, 16 Feb 2010 23:29:44 +0000
parents 7212e3e0cd17
children cf5756ec6ed7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1 import unittest
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2 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
3 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
4 import weakref
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
5 import operator
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
6 import copy
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
7 import pickle
32555
d909a0ddd193 * fix the print test
Raymond Hettinger <python@rcn.com>
parents: 32158
diff changeset
8 import os
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
40864
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40732
diff changeset
31 class HashCountingInt(int):
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40732
diff changeset
32 'int-like object that counts the number of times __hash__ is called'
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40732
diff changeset
33 def __init__(self, *args):
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40732
diff changeset
34 self.hash_count = 0
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40732
diff changeset
35 def __hash__(self):
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40732
diff changeset
36 self.hash_count += 1
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40732
diff changeset
37 return int.__hash__(self)
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40732
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)
b3408a85c89c Use Py_CLEAR(). Add unrelated test.
Raymond Hettinger <python@rcn.com>
parents: 33597
diff changeset
51
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
52 def test_uniquification(self):
31210
c06b570adf12 Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents: 31008
diff changeset
53 actual = sorted(self.s)
c06b570adf12 Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents: 31008
diff changeset
54 expected = sorted(self.d)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
55 self.assertEqual(actual, expected)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
56 self.assertRaises(PassThru, self.thetype, check_pass_thru())
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
57 self.assertRaises(TypeError, self.thetype, [[]])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
58
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
59 def test_len(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
60 self.assertEqual(len(self.s), len(self.d))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
61
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
62 def test_contains(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
63 for c in self.letters:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
64 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
65 self.assertRaises(TypeError, self.s.__contains__, [[]])
30995
d4826e097a3f Allow temporary hashability for the __contains__ test.
Raymond Hettinger <python@rcn.com>
parents: 30994
diff changeset
66 s = self.thetype([frozenset(self.letters)])
d4826e097a3f Allow temporary hashability for the __contains__ test.
Raymond Hettinger <python@rcn.com>
parents: 30994
diff changeset
67 self.assert_(self.thetype(self.letters) in s)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
68
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
69 def test_union(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
70 u = self.s.union(self.otherword)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
71 for c in self.letters:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
72 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
73 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
74 self.assertEqual(type(u), self.thetype)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
75 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
76 self.assertRaises(TypeError, self.s.union, [[]])
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
77 for C in set, frozenset, dict.fromkeys, str, unicode, list, tuple:
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
78 self.assertEqual(self.thetype('abcba').union(C('cdc')), set('abcd'))
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
79 self.assertEqual(self.thetype('abcba').union(C('efgfe')), set('abcefg'))
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
80 self.assertEqual(self.thetype('abcba').union(C('ccb')), set('abc'))
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
81 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
82 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
83
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
84 # 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
85 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
86 self.assertEqual(x.union(set([1]), x, set([2])), self.thetype([1, 2]))
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
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
88 def test_or(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
89 i = self.s.union(self.otherword)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
90 self.assertEqual(self.s | set(self.otherword), i)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
91 self.assertEqual(self.s | frozenset(self.otherword), i)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
92 try:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
93 self.s | self.otherword
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
94 except TypeError:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
95 pass
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
96 else:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
97 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
98
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
99 def test_intersection(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
100 i = self.s.intersection(self.otherword)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
101 for c in self.letters:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
102 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
103 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
104 self.assertEqual(type(i), self.thetype)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
105 self.assertRaises(PassThru, self.s.intersection, check_pass_thru())
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
106 for C in set, frozenset, dict.fromkeys, str, unicode, list, tuple:
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
107 self.assertEqual(self.thetype('abcba').intersection(C('cdc')), set('cc'))
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
108 self.assertEqual(self.thetype('abcba').intersection(C('efgfe')), set(''))
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
109 self.assertEqual(self.thetype('abcba').intersection(C('ccb')), set('bc'))
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
110 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
111 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
112 s = self.thetype('abcba')
7d70b59eeaf2 Handle the case with zero arguments.
Raymond Hettinger <python@rcn.com>
parents: 47533
diff changeset
113 z = s.intersection()
7d70b59eeaf2 Handle the case with zero arguments.
Raymond Hettinger <python@rcn.com>
parents: 47533
diff changeset
114 if self.thetype == frozenset():
7d70b59eeaf2 Handle the case with zero arguments.
Raymond Hettinger <python@rcn.com>
parents: 47533
diff changeset
115 self.assertEqual(id(s), id(z))
7d70b59eeaf2 Handle the case with zero arguments.
Raymond Hettinger <python@rcn.com>
parents: 47533
diff changeset
116 else:
7d70b59eeaf2 Handle the case with zero arguments.
Raymond Hettinger <python@rcn.com>
parents: 47533
diff changeset
117 self.assertNotEqual(id(s), id(z))
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
118
43794
2b3abe3d3ae6 Add set.isdisjoint()
Raymond Hettinger <python@rcn.com>
parents: 41532
diff changeset
119 def test_isdisjoint(self):
2b3abe3d3ae6 Add set.isdisjoint()
Raymond Hettinger <python@rcn.com>
parents: 41532
diff changeset
120 def f(s1, s2):
2b3abe3d3ae6 Add set.isdisjoint()
Raymond Hettinger <python@rcn.com>
parents: 41532
diff changeset
121 'Pure python equivalent of isdisjoint()'
2b3abe3d3ae6 Add set.isdisjoint()
Raymond Hettinger <python@rcn.com>
parents: 41532
diff changeset
122 return not set(s1).intersection(s2)
2b3abe3d3ae6 Add set.isdisjoint()
Raymond Hettinger <python@rcn.com>
parents: 41532
diff changeset
123 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
124 s1 = self.thetype(larg)
2b3abe3d3ae6 Add set.isdisjoint()
Raymond Hettinger <python@rcn.com>
parents: 41532
diff changeset
125 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
126 for C in set, frozenset, dict.fromkeys, str, unicode, list, tuple:
2b3abe3d3ae6 Add set.isdisjoint()
Raymond Hettinger <python@rcn.com>
parents: 41532
diff changeset
127 s2 = C(rarg)
2b3abe3d3ae6 Add set.isdisjoint()
Raymond Hettinger <python@rcn.com>
parents: 41532
diff changeset
128 actual = s1.isdisjoint(s2)
2b3abe3d3ae6 Add set.isdisjoint()
Raymond Hettinger <python@rcn.com>
parents: 41532
diff changeset
129 expected = f(s1, s2)
2b3abe3d3ae6 Add set.isdisjoint()
Raymond Hettinger <python@rcn.com>
parents: 41532
diff changeset
130 self.assertEqual(actual, expected)
2b3abe3d3ae6 Add set.isdisjoint()
Raymond Hettinger <python@rcn.com>
parents: 41532
diff changeset
131 self.assert_(actual is True or actual is False)
2b3abe3d3ae6 Add set.isdisjoint()
Raymond Hettinger <python@rcn.com>
parents: 41532
diff changeset
132
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
133 def test_and(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
134 i = self.s.intersection(self.otherword)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
135 self.assertEqual(self.s & set(self.otherword), i)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
136 self.assertEqual(self.s & frozenset(self.otherword), i)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
137 try:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
138 self.s & self.otherword
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
139 except TypeError:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
140 pass
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
141 else:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
142 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
143
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
144 def test_difference(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
145 i = self.s.difference(self.otherword)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
146 for c in self.letters:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
147 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
148 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
149 self.assertEqual(type(i), self.thetype)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
150 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
151 self.assertRaises(TypeError, self.s.difference, [[]])
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
152 for C in set, frozenset, dict.fromkeys, str, unicode, list, tuple:
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
153 self.assertEqual(self.thetype('abcba').difference(C('cdc')), set('ab'))
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
154 self.assertEqual(self.thetype('abcba').difference(C('efgfe')), set('abc'))
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
155 self.assertEqual(self.thetype('abcba').difference(C('ccb')), set('a'))
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
156 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
157 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
158 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
159
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
160 def test_sub(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
161 i = self.s.difference(self.otherword)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
162 self.assertEqual(self.s - set(self.otherword), i)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
163 self.assertEqual(self.s - frozenset(self.otherword), i)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
164 try:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
165 self.s - self.otherword
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
166 except TypeError:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
167 pass
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
168 else:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
169 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
170
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
171 def test_symmetric_difference(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
172 i = self.s.symmetric_difference(self.otherword)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
173 for c in self.letters:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
174 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
175 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
176 self.assertEqual(type(i), self.thetype)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
177 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
178 self.assertRaises(TypeError, self.s.symmetric_difference, [[]])
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
179 for C in set, frozenset, dict.fromkeys, str, unicode, list, tuple:
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
180 self.assertEqual(self.thetype('abcba').symmetric_difference(C('cdc')), set('abd'))
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
181 self.assertEqual(self.thetype('abcba').symmetric_difference(C('efgfe')), set('abcefg'))
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
182 self.assertEqual(self.thetype('abcba').symmetric_difference(C('ccb')), set('a'))
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
183 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
184
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
185 def test_xor(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
186 i = self.s.symmetric_difference(self.otherword)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
187 self.assertEqual(self.s ^ set(self.otherword), i)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
188 self.assertEqual(self.s ^ frozenset(self.otherword), i)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
189 try:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
190 self.s ^ self.otherword
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
191 except TypeError:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
192 pass
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
193 else:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
194 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
195
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
196 def test_equality(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
197 self.assertEqual(self.s, set(self.word))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
198 self.assertEqual(self.s, frozenset(self.word))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
199 self.assertEqual(self.s == self.word, False)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
200 self.assertNotEqual(self.s, set(self.otherword))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
201 self.assertNotEqual(self.s, frozenset(self.otherword))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
202 self.assertEqual(self.s != self.word, True)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
203
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
204 def test_setOfFrozensets(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
205 t = map(frozenset, ['abcdef', 'bcd', 'bdcb', 'fed', 'fedccba'])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
206 s = self.thetype(t)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
207 self.assertEqual(len(s), 3)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
208
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
209 def test_compare(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
210 self.assertRaises(TypeError, self.s.__cmp__, self.s)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
211
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
212 def test_sub_and_super(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
213 p, q, r = map(self.thetype, ['ab', 'abcde', 'def'])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
214 self.assert_(p < q)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
215 self.assert_(p <= q)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
216 self.assert_(q <= q)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
217 self.assert_(q > p)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
218 self.assert_(q >= p)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
219 self.failIf(q < r)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
220 self.failIf(q <= r)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
221 self.failIf(q > r)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
222 self.failIf(q >= r)
30994
632b223cb8e8 issubset() and issuperset() to work with general iterables
Raymond Hettinger <python@rcn.com>
parents: 30931
diff changeset
223 self.assert_(set('a').issubset('abc'))
632b223cb8e8 issubset() and issuperset() to work with general iterables
Raymond Hettinger <python@rcn.com>
parents: 30931
diff changeset
224 self.assert_(set('abc').issuperset('a'))
632b223cb8e8 issubset() and issuperset() to work with general iterables
Raymond Hettinger <python@rcn.com>
parents: 30931
diff changeset
225 self.failIf(set('a').issubset('cbs'))
632b223cb8e8 issubset() and issuperset() to work with general iterables
Raymond Hettinger <python@rcn.com>
parents: 30931
diff changeset
226 self.failIf(set('cbs').issuperset('a'))
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
227
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
228 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
229 for i in range(pickle.HIGHEST_PROTOCOL + 1):
33948
f2ce15b02ba3 SF 1062353: set pickling problems
Raymond Hettinger <python@rcn.com>
parents: 33605
diff changeset
230 p = pickle.dumps(self.s, i)
f2ce15b02ba3 SF 1062353: set pickling problems
Raymond Hettinger <python@rcn.com>
parents: 33605
diff changeset
231 dup = pickle.loads(p)
f2ce15b02ba3 SF 1062353: set pickling problems
Raymond Hettinger <python@rcn.com>
parents: 33605
diff changeset
232 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
233 if type(self.s) not in (set, frozenset):
f2ce15b02ba3 SF 1062353: set pickling problems
Raymond Hettinger <python@rcn.com>
parents: 33605
diff changeset
234 self.s.x = 10
f2ce15b02ba3 SF 1062353: set pickling problems
Raymond Hettinger <python@rcn.com>
parents: 33605
diff changeset
235 p = pickle.dumps(self.s)
f2ce15b02ba3 SF 1062353: set pickling problems
Raymond Hettinger <python@rcn.com>
parents: 33605
diff changeset
236 dup = pickle.loads(p)
f2ce15b02ba3 SF 1062353: set pickling problems
Raymond Hettinger <python@rcn.com>
parents: 33605
diff changeset
237 self.assertEqual(self.s.x, dup.x)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
238
31004
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
239 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
240 class Tracer:
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
241 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
242 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
243 def __hash__(self):
31389
aca1ce1b4b61 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 31291
diff changeset
244 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
245 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
246 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
247 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
248 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
249 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
250 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
251 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
252 newt = elem
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
253 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
254 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
255
35110
f3d3151f8417 SF patch #1200018: Restore GC support to set objects
Raymond Hettinger <python@rcn.com>
parents: 33948
diff changeset
256 def test_gc(self):
f3d3151f8417 SF patch #1200018: Restore GC support to set objects
Raymond Hettinger <python@rcn.com>
parents: 33948
diff changeset
257 # 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
258 class A:
f3d3151f8417 SF patch #1200018: Restore GC support to set objects
Raymond Hettinger <python@rcn.com>
parents: 33948
diff changeset
259 pass
f3d3151f8417 SF patch #1200018: Restore GC support to set objects
Raymond Hettinger <python@rcn.com>
parents: 33948
diff changeset
260 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
261 for elem in s:
f3d3151f8417 SF patch #1200018: Restore GC support to set objects
Raymond Hettinger <python@rcn.com>
parents: 33948
diff changeset
262 elem.cycle = s
f3d3151f8417 SF patch #1200018: Restore GC support to set objects
Raymond Hettinger <python@rcn.com>
parents: 33948
diff changeset
263 elem.sub = elem
f3d3151f8417 SF patch #1200018: Restore GC support to set objects
Raymond Hettinger <python@rcn.com>
parents: 33948
diff changeset
264 elem.set = set([elem])
f3d3151f8417 SF patch #1200018: Restore GC support to set objects
Raymond Hettinger <python@rcn.com>
parents: 33948
diff changeset
265
35334
725671db779a * Fix SF #1257731. Make __contains__(), remove(), and discard() only do
Raymond Hettinger <python@rcn.com>
parents: 35328
diff changeset
266 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
267 # Bug #1257731
725671db779a * Fix SF #1257731. Make __contains__(), remove(), and discard() only do
Raymond Hettinger <python@rcn.com>
parents: 35328
diff changeset
268 class H(self.thetype):
725671db779a * Fix SF #1257731. Make __contains__(), remove(), and discard() only do
Raymond Hettinger <python@rcn.com>
parents: 35328
diff changeset
269 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
270 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
271 s=H()
725671db779a * Fix SF #1257731. Make __contains__(), remove(), and discard() only do
Raymond Hettinger <python@rcn.com>
parents: 35328
diff changeset
272 f=set()
725671db779a * Fix SF #1257731. Make __contains__(), remove(), and discard() only do
Raymond Hettinger <python@rcn.com>
parents: 35328
diff changeset
273 f.add(s)
725671db779a * Fix SF #1257731. Make __contains__(), remove(), and discard() only do
Raymond Hettinger <python@rcn.com>
parents: 35328
diff changeset
274 self.assert_(s in f)
725671db779a * Fix SF #1257731. Make __contains__(), remove(), and discard() only do
Raymond Hettinger <python@rcn.com>
parents: 35328
diff changeset
275 f.remove(s)
725671db779a * Fix SF #1257731. Make __contains__(), remove(), and discard() only do
Raymond Hettinger <python@rcn.com>
parents: 35328
diff changeset
276 f.add(s)
725671db779a * Fix SF #1257731. Make __contains__(), remove(), and discard() only do
Raymond Hettinger <python@rcn.com>
parents: 35328
diff changeset
277 f.discard(s)
725671db779a * Fix SF #1257731. Make __contains__(), remove(), and discard() only do
Raymond Hettinger <python@rcn.com>
parents: 35328
diff changeset
278
35581
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
279 def test_badcmp(self):
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
280 s = self.thetype([BadCmp()])
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
281 # 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
282 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
283 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
284 # 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
285 if hasattr(s, 'add'):
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
286 self.assertRaises(RuntimeError, s.add, BadCmp())
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.discard, 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.remove, BadCmp())
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
289
40536
c1a4dc71cdd3 For sets with cyclical reprs, emit an ellipsis instead of infinitely recursing.
Raymond Hettinger <python@rcn.com>
parents: 40432
diff changeset
290 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
291 w = ReprWrapper()
c1a4dc71cdd3 For sets with cyclical reprs, emit an ellipsis instead of infinitely recursing.
Raymond Hettinger <python@rcn.com>
parents: 40432
diff changeset
292 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
293 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
294 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
295 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
296
c1a4dc71cdd3 For sets with cyclical reprs, emit an ellipsis instead of infinitely recursing.
Raymond Hettinger <python@rcn.com>
parents: 40432
diff changeset
297 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
298 w = ReprWrapper()
c1a4dc71cdd3 For sets with cyclical reprs, emit an ellipsis instead of infinitely recursing.
Raymond Hettinger <python@rcn.com>
parents: 40432
diff changeset
299 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
300 w.value = s
46065
de54495a414a Don't try to close a non-open file.
Neal Norwitz <nnorwitz@gmail.com>
parents: 44926
diff changeset
301 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
302 try:
c1a4dc71cdd3 For sets with cyclical reprs, emit an ellipsis instead of infinitely recursing.
Raymond Hettinger <python@rcn.com>
parents: 40432
diff changeset
303 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
304 fo.close()
c1a4dc71cdd3 For sets with cyclical reprs, emit an ellipsis instead of infinitely recursing.
Raymond Hettinger <python@rcn.com>
parents: 40432
diff changeset
305 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
306 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
307 finally:
c1a4dc71cdd3 For sets with cyclical reprs, emit an ellipsis instead of infinitely recursing.
Raymond Hettinger <python@rcn.com>
parents: 40432
diff changeset
308 fo.close()
46065
de54495a414a Don't try to close a non-open file.
Neal Norwitz <nnorwitz@gmail.com>
parents: 44926
diff changeset
309 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
310
40864
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40732
diff changeset
311 def test_do_not_rehash_dict_keys(self):
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40732
diff changeset
312 n = 10
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40732
diff changeset
313 d = dict.fromkeys(map(HashCountingInt, xrange(n)))
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40732
diff changeset
314 self.assertEqual(sum(elem.hash_count for elem in d), n)
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40732
diff changeset
315 s = self.thetype(d)
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40732
diff changeset
316 self.assertEqual(sum(elem.hash_count for elem in d), n)
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40732
diff changeset
317 s.difference(d)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 40864
diff changeset
318 self.assertEqual(sum(elem.hash_count for elem in d), n)
40864
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40732
diff changeset
319 if hasattr(s, 'symmetric_difference_update'):
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40732
diff changeset
320 s.symmetric_difference_update(d)
41532
4b9fa8092e82 Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents: 41270
diff changeset
321 self.assertEqual(sum(elem.hash_count for elem in d), n)
41248
ec6fb091039b Extend work on rev 52962 and 53829 eliminating redundant PyObject_Hash() calls and fixing set/dict interoperability.
Raymond Hettinger <python@rcn.com>
parents: 41133
diff changeset
322 d2 = dict.fromkeys(set(d))
ec6fb091039b Extend work on rev 52962 and 53829 eliminating redundant PyObject_Hash() calls and fixing set/dict interoperability.
Raymond Hettinger <python@rcn.com>
parents: 41133
diff changeset
323 self.assertEqual(sum(elem.hash_count for elem in d), n)
ec6fb091039b Extend work on rev 52962 and 53829 eliminating redundant PyObject_Hash() calls and fixing set/dict interoperability.
Raymond Hettinger <python@rcn.com>
parents: 41133
diff changeset
324 d3 = dict.fromkeys(frozenset(d))
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 40864
diff changeset
325 self.assertEqual(sum(elem.hash_count for elem in d), n)
41270
15a7e0b7e425 Add test and fix for fromkeys() optional argument.
Raymond Hettinger <python@rcn.com>
parents: 41248
diff changeset
326 d3 = dict.fromkeys(frozenset(d), 123)
15a7e0b7e425 Add test and fix for fromkeys() optional argument.
Raymond Hettinger <python@rcn.com>
parents: 41248
diff changeset
327 self.assertEqual(sum(elem.hash_count for elem in d), n)
15a7e0b7e425 Add test and fix for fromkeys() optional argument.
Raymond Hettinger <python@rcn.com>
parents: 41248
diff changeset
328 self.assertEqual(d3, dict.fromkeys(d, 123))
40864
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40732
diff changeset
329
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
330 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
331 # 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
332 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
333 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
334 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
335 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
336 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
337 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
338 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
339 gc.collect()
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 self.assert_(ref() is None, "Cycle was not collected")
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
341
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
342 class TestSet(TestJointOps):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
343 thetype = set
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
344
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30929
diff changeset
345 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
346 s = self.thetype()
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30929
diff changeset
347 s.__init__(self.word)
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30929
diff changeset
348 self.assertEqual(s, set(self.word))
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30929
diff changeset
349 s.__init__(self.otherword)
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30929
diff changeset
350 self.assertEqual(s, set(self.otherword))
32555
d909a0ddd193 * fix the print test
Raymond Hettinger <python@rcn.com>
parents: 32158
diff changeset
351 self.assertRaises(TypeError, s.__init__, s, 2);
d909a0ddd193 * fix the print test
Raymond Hettinger <python@rcn.com>
parents: 32158
diff changeset
352 self.assertRaises(TypeError, s.__init__, 1);
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30929
diff changeset
353
31004
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
354 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
355 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
356 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
357 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
358
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
359 def test_hash(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
360 self.assertRaises(TypeError, hash, self.s)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
361
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
362 def test_clear(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
363 self.s.clear()
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
364 self.assertEqual(self.s, set())
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
365 self.assertEqual(len(self.s), 0)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
366
31004
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
367 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
368 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
369 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
370 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
371
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
372 def test_add(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
373 self.s.add('Q')
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
374 self.assert_('Q' in self.s)
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
375 dup = self.s.copy()
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
376 self.s.add('Q')
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
377 self.assertEqual(self.s, dup)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
378 self.assertRaises(TypeError, self.s.add, [])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
379
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
380 def test_remove(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
381 self.s.remove('a')
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
382 self.assert_('a' not in self.s)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
383 self.assertRaises(KeyError, self.s.remove, 'Q')
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
384 self.assertRaises(TypeError, self.s.remove, [])
31002
622a76bc0237 Extend temporary hashability to remove() and discard().
Raymond Hettinger <python@rcn.com>
parents: 30995
diff changeset
385 s = self.thetype([frozenset(self.word)])
622a76bc0237 Extend temporary hashability to remove() and discard().
Raymond Hettinger <python@rcn.com>
parents: 30995
diff changeset
386 self.assert_(self.thetype(self.word) in s)
622a76bc0237 Extend temporary hashability to remove() and discard().
Raymond Hettinger <python@rcn.com>
parents: 30995
diff changeset
387 s.remove(self.thetype(self.word))
622a76bc0237 Extend temporary hashability to remove() and discard().
Raymond Hettinger <python@rcn.com>
parents: 30995
diff changeset
388 self.assert_(self.thetype(self.word) not in s)
622a76bc0237 Extend temporary hashability to remove() and discard().
Raymond Hettinger <python@rcn.com>
parents: 30995
diff changeset
389 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
390
40432
a15bd81efdb6 Add test for SF bug 1576657
Raymond Hettinger <python@rcn.com>
parents: 37574
diff changeset
391 def test_remove_keyerror_unpacking(self):
a15bd81efdb6 Add test for SF bug 1576657
Raymond Hettinger <python@rcn.com>
parents: 37574
diff changeset
392 # bug: www.python.org/sf/1576657
a15bd81efdb6 Add test for SF bug 1576657
Raymond Hettinger <python@rcn.com>
parents: 37574
diff changeset
393 for v1 in ['Q', (1,)]:
a15bd81efdb6 Add test for SF bug 1576657
Raymond Hettinger <python@rcn.com>
parents: 37574
diff changeset
394 try:
a15bd81efdb6 Add test for SF bug 1576657
Raymond Hettinger <python@rcn.com>
parents: 37574
diff changeset
395 self.s.remove(v1)
a15bd81efdb6 Add test for SF bug 1576657
Raymond Hettinger <python@rcn.com>
parents: 37574
diff changeset
396 except KeyError, e:
a15bd81efdb6 Add test for SF bug 1576657
Raymond Hettinger <python@rcn.com>
parents: 37574
diff changeset
397 v2 = e.args[0]
a15bd81efdb6 Add test for SF bug 1576657
Raymond Hettinger <python@rcn.com>
parents: 37574
diff changeset
398 self.assertEqual(v1, v2)
a15bd81efdb6 Add test for SF bug 1576657
Raymond Hettinger <python@rcn.com>
parents: 37574
diff changeset
399 else:
a15bd81efdb6 Add test for SF bug 1576657
Raymond Hettinger <python@rcn.com>
parents: 37574
diff changeset
400 self.fail()
a15bd81efdb6 Add test for SF bug 1576657
Raymond Hettinger <python@rcn.com>
parents: 37574
diff changeset
401
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
402 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
403 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
404 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
405 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
406 except KeyError as e:
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 self.assert_(e.args[0] is 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
408 "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
409 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
410 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
411 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
412
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
413 def test_discard(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
414 self.s.discard('a')
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
415 self.assert_('a' not in self.s)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
416 self.s.discard('Q')
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
417 self.assertRaises(TypeError, self.s.discard, [])
31002
622a76bc0237 Extend temporary hashability to remove() and discard().
Raymond Hettinger <python@rcn.com>
parents: 30995
diff changeset
418 s = self.thetype([frozenset(self.word)])
622a76bc0237 Extend temporary hashability to remove() and discard().
Raymond Hettinger <python@rcn.com>
parents: 30995
diff changeset
419 self.assert_(self.thetype(self.word) in s)
622a76bc0237 Extend temporary hashability to remove() and discard().
Raymond Hettinger <python@rcn.com>
parents: 30995
diff changeset
420 s.discard(self.thetype(self.word))
622a76bc0237 Extend temporary hashability to remove() and discard().
Raymond Hettinger <python@rcn.com>
parents: 30995
diff changeset
421 self.assert_(self.thetype(self.word) not in s)
622a76bc0237 Extend temporary hashability to remove() and discard().
Raymond Hettinger <python@rcn.com>
parents: 30995
diff changeset
422 s.discard(self.thetype(self.word))
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
423
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
424 def test_pop(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
425 for i in xrange(len(self.s)):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
426 elem = self.s.pop()
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
427 self.assert_(elem not in self.s)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
428 self.assertRaises(KeyError, self.s.pop)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
429
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
430 def test_update(self):
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
431 retval = self.s.update(self.otherword)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
432 self.assertEqual(retval, None)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
433 for c in (self.word + self.otherword):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
434 self.assert_(c in self.s)
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
435 self.assertRaises(PassThru, self.s.update, check_pass_thru())
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
436 self.assertRaises(TypeError, self.s.update, [[]])
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
437 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
438 for C in set, frozenset, dict.fromkeys, str, unicode, list, tuple:
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
439 s = self.thetype('abcba')
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
440 self.assertEqual(s.update(C(p)), None)
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
441 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
442 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
443 q = 'ahi'
d4cd7223af3b Let set.union() and set.update() accept multiple inputs.
Raymond Hettinger <python@rcn.com>
parents: 46065
diff changeset
444 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
445 s = self.thetype('abcba')
d4cd7223af3b Let set.union() and set.update() accept multiple inputs.
Raymond Hettinger <python@rcn.com>
parents: 46065
diff changeset
446 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
447 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
448
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
449 def test_ior(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
450 self.s |= set(self.otherword)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
451 for c in (self.word + self.otherword):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
452 self.assert_(c in self.s)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
453
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
454 def test_intersection_update(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
455 retval = self.s.intersection_update(self.otherword)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
456 self.assertEqual(retval, None)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
457 for c in (self.word + self.otherword):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
458 if c in self.otherword and c in self.word:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
459 self.assert_(c in self.s)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
460 else:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
461 self.assert_(c not in self.s)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
462 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
463 self.assertRaises(TypeError, self.s.intersection_update, [[]])
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
464 for p, q in (('cdc', 'c'), ('efgfe', ''), ('ccb', 'bc'), ('ef', '')):
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
465 for C in set, frozenset, dict.fromkeys, str, unicode, list, tuple:
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
466 s = self.thetype('abcba')
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
467 self.assertEqual(s.intersection_update(C(p)), None)
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
468 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
469 ss = 'abcba'
10577e25f78f Let set.intersection() and set.intersection_update() take multiple input arguments.
Raymond Hettinger <python@rcn.com>
parents: 47530
diff changeset
470 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
471 t = 'cbc'
10577e25f78f Let set.intersection() and set.intersection_update() take multiple input arguments.
Raymond Hettinger <python@rcn.com>
parents: 47530
diff changeset
472 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
473 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
474
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
475 def test_iand(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
476 self.s &= set(self.otherword)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
477 for c in (self.word + self.otherword):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
478 if c in self.otherword and c in self.word:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
479 self.assert_(c in self.s)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
480 else:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
481 self.assert_(c not in self.s)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
482
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
483 def test_difference_update(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
484 retval = self.s.difference_update(self.otherword)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
485 self.assertEqual(retval, None)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
486 for c in (self.word + self.otherword):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
487 if c in self.word and c not in self.otherword:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
488 self.assert_(c in self.s)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
489 else:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
490 self.assert_(c not in self.s)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
491 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
492 self.assertRaises(TypeError, self.s.difference_update, [[]])
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
493 self.assertRaises(TypeError, self.s.symmetric_difference_update, [[]])
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
494 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
495 for C in set, frozenset, dict.fromkeys, str, unicode, list, tuple:
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
496 s = self.thetype('abcba')
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
497 self.assertEqual(s.difference_update(C(p)), None)
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
498 self.assertEqual(s, set(q))
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
499
47588
8fe47b2c8bfe Multi-arg form for set.difference() and set.difference_update().
Raymond Hettinger <python@rcn.com>
parents: 47574
diff changeset
500 s = self.thetype('abcdefghih')
8fe47b2c8bfe Multi-arg form for set.difference() and set.difference_update().
Raymond Hettinger <python@rcn.com>
parents: 47574
diff changeset
501 s.difference_update()
8fe47b2c8bfe Multi-arg form for set.difference() and set.difference_update().
Raymond Hettinger <python@rcn.com>
parents: 47574
diff changeset
502 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
503
8fe47b2c8bfe Multi-arg form for set.difference() and set.difference_update().
Raymond Hettinger <python@rcn.com>
parents: 47574
diff changeset
504 s = self.thetype('abcdefghih')
8fe47b2c8bfe Multi-arg form for set.difference() and set.difference_update().
Raymond Hettinger <python@rcn.com>
parents: 47574
diff changeset
505 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
506 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
507
8fe47b2c8bfe Multi-arg form for set.difference() and set.difference_update().
Raymond Hettinger <python@rcn.com>
parents: 47574
diff changeset
508 s = self.thetype('abcdefghih')
8fe47b2c8bfe Multi-arg form for set.difference() and set.difference_update().
Raymond Hettinger <python@rcn.com>
parents: 47574
diff changeset
509 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
510 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
511
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
512 def test_isub(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
513 self.s -= set(self.otherword)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
514 for c in (self.word + self.otherword):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
515 if c in self.word and c not in self.otherword:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
516 self.assert_(c in self.s)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
517 else:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
518 self.assert_(c not in self.s)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
519
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
520 def test_symmetric_difference_update(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
521 retval = self.s.symmetric_difference_update(self.otherword)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
522 self.assertEqual(retval, None)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
523 for c in (self.word + self.otherword):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
524 if (c in self.word) ^ (c in self.otherword):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
525 self.assert_(c in self.s)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
526 else:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
527 self.assert_(c not in self.s)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
528 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
529 self.assertRaises(TypeError, self.s.symmetric_difference_update, [[]])
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
530 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
531 for C in set, frozenset, dict.fromkeys, str, unicode, list, tuple:
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
532 s = self.thetype('abcba')
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
533 self.assertEqual(s.symmetric_difference_update(C(p)), None)
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
534 self.assertEqual(s, set(q))
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
535
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
536 def test_ixor(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
537 self.s ^= set(self.otherword)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
538 for c in (self.word + self.otherword):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
539 if (c in self.word) ^ (c in self.otherword):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
540 self.assert_(c in self.s)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
541 else:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
542 self.assert_(c not in self.s)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
543
35328
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35297
diff changeset
544 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
545 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
546 t |= t
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35297
diff changeset
547 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
548 t &= t
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35297
diff changeset
549 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
550 t -= t
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35297
diff changeset
551 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
552 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
553 t ^= t
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35297
diff changeset
554 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
555
32158
679e5c8effae Make sets and deques weak referencable.
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
556 def test_weakref(self):
679e5c8effae Make sets and deques weak referencable.
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
557 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
558 p = weakref.proxy(s)
32158
679e5c8effae Make sets and deques weak referencable.
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
559 self.assertEqual(str(p), str(s))
679e5c8effae Make sets and deques weak referencable.
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
560 s = None
679e5c8effae Make sets and deques weak referencable.
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
561 self.assertRaises(ReferenceError, str, p)
679e5c8effae Make sets and deques weak referencable.
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
562
35348
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35334
diff changeset
563 # 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
564 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
565 def test_c_api(self):
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35334
diff changeset
566 self.assertEqual(set('abc').test_c_api(), True)
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35334
diff changeset
567
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30929
diff changeset
568 class SetSubclass(set):
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30929
diff changeset
569 pass
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30929
diff changeset
570
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30929
diff changeset
571 class TestSetSubclass(TestSet):
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30929
diff changeset
572 thetype = SetSubclass
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
573
40601
7da3dbc8145b SF #1486663 -- Allow keyword args in subclasses of set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 40536
diff changeset
574 class SetSubclassWithKeywordArgs(set):
7da3dbc8145b SF #1486663 -- Allow keyword args in subclasses of set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 40536
diff changeset
575 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
576 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
577
7da3dbc8145b SF #1486663 -- Allow keyword args in subclasses of set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 40536
diff changeset
578 class TestSetSubclassWithKeywordArgs(TestSet):
40732
5dae0d7f683a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 40601
diff changeset
579
40601
7da3dbc8145b SF #1486663 -- Allow keyword args in subclasses of set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 40536
diff changeset
580 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
581 '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
582 SetSubclassWithKeywordArgs(newarg=1)
7da3dbc8145b SF #1486663 -- Allow keyword args in subclasses of set() and frozenset().
Raymond Hettinger <python@rcn.com>
parents: 40536
diff changeset
583
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
584 class TestFrozenSet(TestJointOps):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
585 thetype = frozenset
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
586
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30929
diff changeset
587 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
588 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
589 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
590 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
591
35297
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35206
diff changeset
592 def test_singleton_empty_frozenset(self):
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35206
diff changeset
593 f = frozenset()
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35206
diff changeset
594 efs = [frozenset(), frozenset([]), frozenset(()), frozenset(''),
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35206
diff changeset
595 frozenset(), frozenset([]), frozenset(()), frozenset(''),
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35206
diff changeset
596 frozenset(xrange(0)), frozenset(frozenset()),
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35206
diff changeset
597 frozenset(f), f]
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35206
diff changeset
598 # 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
599 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
600
31004
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
601 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
602 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
603 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
604 self.assertEqual(id(s), id(t))
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30929
diff changeset
605
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
606 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
607 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
608 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
609
35206
89710805bc3d Add test for hash commutativity.
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
610 # 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
611 n = 100
89710805bc3d Add test for hash commutativity.
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
612 seq = [randrange(n) for i in xrange(n)]
89710805bc3d Add test for hash commutativity.
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
613 results = set()
89710805bc3d Add test for hash commutativity.
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
614 for i in xrange(200):
89710805bc3d Add test for hash commutativity.
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
615 shuffle(seq)
89710805bc3d Add test for hash commutativity.
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
616 results.add(hash(self.thetype(seq)))
89710805bc3d Add test for hash commutativity.
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
617 self.assertEqual(len(results), 1)
89710805bc3d Add test for hash commutativity.
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
618
31004
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
619 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
620 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
621 self.assertEqual(id(self.s), id(dup))
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
622
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
623 def test_frozen_as_dictkey(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
624 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
625 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
626 key2 = self.thetype(reversed(seq))
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
627 self.assertEqual(key1, key2)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
628 self.assertNotEqual(id(key1), id(key2))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
629 d = {}
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
630 d[key1] = 42
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
631 self.assertEqual(d[key2], 42)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
632
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
633 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
634 f = self.thetype('abcdcda')
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
635 self.assertEqual(hash(f), hash(f))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
636
31004
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
637 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
638 n = 13
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
639 hashvalues = set()
31291
868cace2219f Strengthen the test for hash effectiveness
Raymond Hettinger <python@rcn.com>
parents: 31210
diff changeset
640 addhashvalue = hashvalues.add
868cace2219f Strengthen the test for hash effectiveness
Raymond Hettinger <python@rcn.com>
parents: 31210
diff changeset
641 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
642 for i in xrange(2**n):
31291
868cace2219f Strengthen the test for hash effectiveness
Raymond Hettinger <python@rcn.com>
parents: 31210
diff changeset
643 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
644 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
645
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30929
diff changeset
646 class FrozenSetSubclass(frozenset):
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30929
diff changeset
647 pass
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30929
diff changeset
648
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30929
diff changeset
649 class TestFrozenSetSubclass(TestFrozenSet):
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30929
diff changeset
650 thetype = FrozenSetSubclass
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30929
diff changeset
651
31004
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
652 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
653 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
654 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
655 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
656
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
657 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
658 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
659 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
660
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
661 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
662 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
663 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
664 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
665
35297
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35206
diff changeset
666 def test_singleton_empty_frozenset(self):
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35206
diff changeset
667 Frozenset = self.thetype
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35206
diff changeset
668 f = frozenset()
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 efs = [Frozenset(), Frozenset([]), Frozenset(()), Frozenset(''),
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35206
diff changeset
671 Frozenset(), Frozenset([]), Frozenset(()), Frozenset(''),
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35206
diff changeset
672 Frozenset(xrange(0)), Frozenset(Frozenset()),
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35206
diff changeset
673 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
674 # 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
675 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
676
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
677 # Tests taken from test_sets.py =============================================
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
678
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
679 empty_set = set()
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
680
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 class TestBasicOps(unittest.TestCase):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
684
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
685 def test_repr(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
686 if self.repr is not None:
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 31389
diff changeset
687 self.assertEqual(repr(self.set), self.repr)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
688
32555
d909a0ddd193 * fix the print test
Raymond Hettinger <python@rcn.com>
parents: 32158
diff changeset
689 def test_print(self):
46065
de54495a414a Don't try to close a non-open file.
Neal Norwitz <nnorwitz@gmail.com>
parents: 44926
diff changeset
690 fo = open(test_support.TESTFN, "wb")
32555
d909a0ddd193 * fix the print test
Raymond Hettinger <python@rcn.com>
parents: 32158
diff changeset
691 try:
d909a0ddd193 * fix the print test
Raymond Hettinger <python@rcn.com>
parents: 32158
diff changeset
692 print >> fo, self.set,
d909a0ddd193 * fix the print test
Raymond Hettinger <python@rcn.com>
parents: 32158
diff changeset
693 fo.close()
d909a0ddd193 * fix the print test
Raymond Hettinger <python@rcn.com>
parents: 32158
diff changeset
694 fo = open(test_support.TESTFN, "rb")
d909a0ddd193 * fix the print test
Raymond Hettinger <python@rcn.com>
parents: 32158
diff changeset
695 self.assertEqual(fo.read(), repr(self.set))
d909a0ddd193 * fix the print test
Raymond Hettinger <python@rcn.com>
parents: 32158
diff changeset
696 finally:
d909a0ddd193 * fix the print test
Raymond Hettinger <python@rcn.com>
parents: 32158
diff changeset
697 fo.close()
46065
de54495a414a Don't try to close a non-open file.
Neal Norwitz <nnorwitz@gmail.com>
parents: 44926
diff changeset
698 test_support.unlink(test_support.TESTFN)
32555
d909a0ddd193 * fix the print test
Raymond Hettinger <python@rcn.com>
parents: 32158
diff changeset
699
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
700 def test_length(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
701 self.assertEqual(len(self.set), self.length)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
702
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
703 def test_self_equality(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
704 self.assertEqual(self.set, self.set)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
705
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
706 def test_equivalent_equality(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
707 self.assertEqual(self.set, self.dup)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
708
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
709 def test_copy(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
710 self.assertEqual(self.set.copy(), self.dup)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
711
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
712 def test_self_union(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
713 result = self.set | self.set
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
714 self.assertEqual(result, self.dup)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
715
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
716 def test_empty_union(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
717 result = self.set | empty_set
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
718 self.assertEqual(result, self.dup)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
719
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
720 def test_union_empty(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
721 result = empty_set | self.set
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
722 self.assertEqual(result, 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_intersection(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_intersection(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, empty_set)
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_intersection_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, empty_set)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
735
43794
2b3abe3d3ae6 Add set.isdisjoint()
Raymond Hettinger <python@rcn.com>
parents: 41532
diff changeset
736 def test_self_isdisjoint(self):
2b3abe3d3ae6 Add set.isdisjoint()
Raymond Hettinger <python@rcn.com>
parents: 41532
diff changeset
737 result = self.set.isdisjoint(self.set)
2b3abe3d3ae6 Add set.isdisjoint()
Raymond Hettinger <python@rcn.com>
parents: 41532
diff changeset
738 self.assertEqual(result, not self.set)
2b3abe3d3ae6 Add set.isdisjoint()
Raymond Hettinger <python@rcn.com>
parents: 41532
diff changeset
739
2b3abe3d3ae6 Add set.isdisjoint()
Raymond Hettinger <python@rcn.com>
parents: 41532
diff changeset
740 def test_empty_isdisjoint(self):
2b3abe3d3ae6 Add set.isdisjoint()
Raymond Hettinger <python@rcn.com>
parents: 41532
diff changeset
741 result = self.set.isdisjoint(empty_set)
2b3abe3d3ae6 Add set.isdisjoint()
Raymond Hettinger <python@rcn.com>
parents: 41532
diff changeset
742 self.assertEqual(result, True)
2b3abe3d3ae6 Add set.isdisjoint()
Raymond Hettinger <python@rcn.com>
parents: 41532
diff changeset
743
2b3abe3d3ae6 Add set.isdisjoint()
Raymond Hettinger <python@rcn.com>
parents: 41532
diff changeset
744 def test_isdisjoint_empty(self):
2b3abe3d3ae6 Add set.isdisjoint()
Raymond Hettinger <python@rcn.com>
parents: 41532
diff changeset
745 result = empty_set.isdisjoint(self.set)
2b3abe3d3ae6 Add set.isdisjoint()
Raymond Hettinger <python@rcn.com>
parents: 41532
diff changeset
746 self.assertEqual(result, True)
2b3abe3d3ae6 Add set.isdisjoint()
Raymond Hettinger <python@rcn.com>
parents: 41532
diff changeset
747
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
748 def test_self_symmetric_difference(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
749 result = self.set ^ self.set
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
750 self.assertEqual(result, empty_set)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
751
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
752 def checkempty_symmetric_difference(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
753 result = self.set ^ empty_set
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
754 self.assertEqual(result, self.set)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
755
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
756 def test_self_difference(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
757 result = self.set - self.set
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
758 self.assertEqual(result, empty_set)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
759
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
760 def test_empty_difference(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
761 result = self.set - empty_set
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
762 self.assertEqual(result, self.dup)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
763
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
764 def test_empty_difference_rev(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
765 result = empty_set - self.set
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
766 self.assertEqual(result, empty_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_iteration(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
769 for v in self.set:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
770 self.assert_(v in 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
771 setiter = iter(self.set)
36484
20f75d5e79e9 Renamed _length_cue() to __length_hint__(). See:
Armin Rigo <arigo@tunes.org>
parents: 35959
diff changeset
772 # 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
773 # 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
774 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
775
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
776 def test_pickling(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
777 p = pickle.dumps(self.set)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
778 copy = pickle.loads(p)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
779 self.assertEqual(self.set, copy,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
780 "%s != %s" % (self.set, copy))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
781
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
782 #------------------------------------------------------------------------------
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
783
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
784 class TestBasicOpsEmpty(TestBasicOps):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
785 def setUp(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
786 self.case = "empty set"
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
787 self.values = []
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
788 self.set = set(self.values)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
789 self.dup = set(self.values)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
790 self.length = 0
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
791 self.repr = "set([])"
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
792
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 class TestBasicOpsSingleton(TestBasicOps):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
796 def setUp(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
797 self.case = "unit set (number)"
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
798 self.values = [3]
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
799 self.set = set(self.values)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
800 self.dup = set(self.values)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
801 self.length = 1
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
802 self.repr = "set([3])"
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
803
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
804 def test_in(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
805 self.failUnless(3 in self.set)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
806
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
807 def test_not_in(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
808 self.failUnless(2 not in self.set)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
809
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
810 #------------------------------------------------------------------------------
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
811
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
812 class TestBasicOpsTuple(TestBasicOps):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
813 def setUp(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
814 self.case = "unit set (tuple)"
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
815 self.values = [(0, "zero")]
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
816 self.set = set(self.values)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
817 self.dup = set(self.values)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
818 self.length = 1
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
819 self.repr = "set([(0, 'zero')])"
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
820
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
821 def test_in(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
822 self.failUnless((0, "zero") in self.set)
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 def test_not_in(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
825 self.failUnless(9 not in self.set)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
826
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
827 #------------------------------------------------------------------------------
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
828
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
829 class TestBasicOpsTriple(TestBasicOps):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
830 def setUp(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
831 self.case = "triple set"
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
832 self.values = [0, "zero", operator.add]
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
833 self.set = set(self.values)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
834 self.dup = set(self.values)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
835 self.length = 3
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
836 self.repr = None
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
837
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 def baditer():
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
841 raise TypeError
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
842 yield True
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
843
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
844 def gooditer():
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
845 yield True
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
846
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
847 class TestExceptionPropagation(unittest.TestCase):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
848 """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
849
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
850 def test_instanceWithException(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
851 self.assertRaises(TypeError, set, baditer())
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
852
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
853 def test_instancesWithoutException(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
854 # All of these iterables should load without exception.
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
855 set([1,2,3])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
856 set((1,2,3))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
857 set({'one':1, 'two':2, 'three':3})
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
858 set(xrange(3))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
859 set('abc')
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
860 set(gooditer())
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
861
35959
fca7337710fd Improve test coverage. Hope the test_file changes work the same on windows.
Neal Norwitz <nnorwitz@gmail.com>
parents: 35581
diff changeset
862 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
863 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
864 try:
fca7337710fd Improve test coverage. Hope the test_file changes work the same on windows.
Neal Norwitz <nnorwitz@gmail.com>
parents: 35581
diff changeset
865 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
866 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
867 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
868 pass
fca7337710fd Improve test coverage. Hope the test_file changes work the same on windows.
Neal Norwitz <nnorwitz@gmail.com>
parents: 35581
diff changeset
869 else:
fca7337710fd Improve test coverage. Hope the test_file changes work the same on windows.
Neal Norwitz <nnorwitz@gmail.com>
parents: 35581
diff changeset
870 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
871
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
872 #==============================================================================
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
873
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
874 class TestSetOfSets(unittest.TestCase):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
875 def test_constructor(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
876 inner = frozenset([1])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
877 outer = set([inner])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
878 element = outer.pop()
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
879 self.assertEqual(type(element), frozenset)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
880 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
881 outer.remove(inner)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
882 self.assertEqual(outer, set()) # Verify that remove worked
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
883 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
884
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
885 #==============================================================================
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
886
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
887 class TestBinaryOps(unittest.TestCase):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
888 def setUp(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
889 self.set = set((2, 4, 6))
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 def test_eq(self): # SF bug 643115
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
892 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
893
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
894 def test_union_subset(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
895 result = self.set | set([2])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
896 self.assertEqual(result, set((2, 4, 6)))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
897
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
898 def test_union_superset(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
899 result = self.set | set([2, 4, 6, 8])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
900 self.assertEqual(result, set([2, 4, 6, 8]))
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_union_overlap(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
903 result = self.set | set([3, 4, 5])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
904 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
905
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
906 def test_union_non_overlap(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
907 result = self.set | set([8])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
908 self.assertEqual(result, set([2, 4, 6, 8]))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
909
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
910 def test_intersection_subset(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
911 result = self.set & set((2, 4))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
912 self.assertEqual(result, set((2, 4)))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
913
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
914 def test_intersection_superset(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
915 result = self.set & set([2, 4, 6, 8])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
916 self.assertEqual(result, set([2, 4, 6]))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
917
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
918 def test_intersection_overlap(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
919 result = self.set & set([3, 4, 5])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
920 self.assertEqual(result, set([4]))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
921
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
922 def test_intersection_non_overlap(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
923 result = self.set & set([8])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
924 self.assertEqual(result, empty_set)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
925
43794
2b3abe3d3ae6 Add set.isdisjoint()
Raymond Hettinger <python@rcn.com>
parents: 41532
diff changeset
926 def test_isdisjoint_subset(self):
2b3abe3d3ae6 Add set.isdisjoint()
Raymond Hettinger <python@rcn.com>
parents: 41532
diff changeset
927 result = self.set.isdisjoint(set((2, 4)))
2b3abe3d3ae6 Add set.isdisjoint()
Raymond Hettinger <python@rcn.com>
parents: 41532
diff changeset
928 self.assertEqual(result, False)
2b3abe3d3ae6 Add set.isdisjoint()
Raymond Hettinger <python@rcn.com>
parents: 41532
diff changeset
929
2b3abe3d3ae6 Add set.isdisjoint()
Raymond Hettinger <python@rcn.com>
parents: 41532
diff changeset
930 def test_isdisjoint_superset(self):
2b3abe3d3ae6 Add set.isdisjoint()
Raymond Hettinger <python@rcn.com>
parents: 41532
diff changeset
931 result = self.set.isdisjoint(set([2, 4, 6, 8]))
2b3abe3d3ae6 Add set.isdisjoint()
Raymond Hettinger <python@rcn.com>
parents: 41532
diff changeset
932 self.assertEqual(result, False)
2b3abe3d3ae6 Add set.isdisjoint()
Raymond Hettinger <python@rcn.com>
parents: 41532
diff changeset
933
2b3abe3d3ae6 Add set.isdisjoint()
Raymond Hettinger <python@rcn.com>
parents: 41532
diff changeset
934 def test_isdisjoint_overlap(self):
2b3abe3d3ae6 Add set.isdisjoint()
Raymond Hettinger <python@rcn.com>
parents: 41532
diff changeset
935 result = self.set.isdisjoint(set([3, 4, 5]))
2b3abe3d3ae6 Add set.isdisjoint()
Raymond Hettinger <python@rcn.com>
parents: 41532
diff changeset
936 self.assertEqual(result, False)
2b3abe3d3ae6 Add set.isdisjoint()
Raymond Hettinger <python@rcn.com>
parents: 41532
diff changeset
937
2b3abe3d3ae6 Add set.isdisjoint()
Raymond Hettinger <python@rcn.com>
parents: 41532
diff changeset
938 def test_isdisjoint_non_overlap(self):
2b3abe3d3ae6 Add set.isdisjoint()
Raymond Hettinger <python@rcn.com>
parents: 41532
diff changeset
939 result = self.set.isdisjoint(set([8]))
2b3abe3d3ae6 Add set.isdisjoint()
Raymond Hettinger <python@rcn.com>
parents: 41532
diff changeset
940 self.assertEqual(result, True)
2b3abe3d3ae6 Add set.isdisjoint()
Raymond Hettinger <python@rcn.com>
parents: 41532
diff changeset
941
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
942 def test_sym_difference_subset(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
943 result = self.set ^ set((2, 4))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
944 self.assertEqual(result, set([6]))
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_sym_difference_superset(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
947 result = self.set ^ set((2, 4, 6, 8))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
948 self.assertEqual(result, set([8]))
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_sym_difference_overlap(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
951 result = self.set ^ set((3, 4, 5))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
952 self.assertEqual(result, set([2, 3, 5, 6]))
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_sym_difference_non_overlap(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
955 result = self.set ^ set([8])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
956 self.assertEqual(result, set([2, 4, 6, 8]))
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_cmp(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
959 a, b = set('a'), set('b')
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
960 self.assertRaises(TypeError, cmp, a, b)
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 # 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
963 # 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
964 # 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
965 # without calling __cmp__.
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
966 self.assertEqual(cmp(a, a), 0)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
967
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
968 self.assertRaises(TypeError, cmp, a, 12)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
969 self.assertRaises(TypeError, cmp, "abc", a)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
970
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
971 #==============================================================================
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
972
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
973 class TestUpdateOps(unittest.TestCase):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
974 def setUp(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
975 self.set = set((2, 4, 6))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
976
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
977 def test_union_subset(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
978 self.set |= set([2])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
979 self.assertEqual(self.set, set((2, 4, 6)))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
980
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
981 def test_union_superset(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
982 self.set |= set([2, 4, 6, 8])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
983 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
984
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
985 def test_union_overlap(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
986 self.set |= set([3, 4, 5])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
987 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
988
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
989 def test_union_non_overlap(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
990 self.set |= set([8])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
991 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
992
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
993 def test_union_method_call(self):
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
994 self.set.update(set([3, 4, 5]))
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
995 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
996
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
997 def test_intersection_subset(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
998 self.set &= set((2, 4))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
999 self.assertEqual(self.set, set((2, 4)))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1000
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1001 def test_intersection_superset(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1002 self.set &= set([2, 4, 6, 8])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1003 self.assertEqual(self.set, set([2, 4, 6]))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1004
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1005 def test_intersection_overlap(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1006 self.set &= set([3, 4, 5])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1007 self.assertEqual(self.set, set([4]))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1008
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1009 def test_intersection_non_overlap(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1010 self.set &= set([8])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1011 self.assertEqual(self.set, empty_set)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1012
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1013 def test_intersection_method_call(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1014 self.set.intersection_update(set([3, 4, 5]))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1015 self.assertEqual(self.set, set([4]))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1016
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1017 def test_sym_difference_subset(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1018 self.set ^= set((2, 4))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1019 self.assertEqual(self.set, set([6]))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1020
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1021 def test_sym_difference_superset(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1022 self.set ^= set((2, 4, 6, 8))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1023 self.assertEqual(self.set, set([8]))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1024
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1025 def test_sym_difference_overlap(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1026 self.set ^= set((3, 4, 5))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1027 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
1028
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1029 def test_sym_difference_non_overlap(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1030 self.set ^= set([8])
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, 8]))
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_sym_difference_method_call(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1034 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
1035 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
1036
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1037 def test_difference_subset(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1038 self.set -= set((2, 4))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1039 self.assertEqual(self.set, set([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_difference_superset(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1042 self.set -= set((2, 4, 6, 8))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1043 self.assertEqual(self.set, set([]))
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_difference_overlap(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1046 self.set -= set((3, 4, 5))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1047 self.assertEqual(self.set, set([2, 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_difference_non_overlap(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1050 self.set -= set([8])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1051 self.assertEqual(self.set, set([2, 4, 6]))
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_difference_method_call(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1054 self.set.difference_update(set([3, 4, 5]))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1055 self.assertEqual(self.set, set([2, 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 #==============================================================================
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1058
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1059 class TestMutate(unittest.TestCase):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1060 def setUp(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1061 self.values = ["a", "b", "c"]
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1062 self.set = set(self.values)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1063
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1064 def test_add_present(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1065 self.set.add("c")
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1066 self.assertEqual(self.set, set("abc"))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1067
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1068 def test_add_absent(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1069 self.set.add("d")
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1070 self.assertEqual(self.set, set("abcd"))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1071
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1072 def test_add_until_full(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1073 tmp = set()
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1074 expected_len = 0
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1075 for v in self.values:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1076 tmp.add(v)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1077 expected_len += 1
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1078 self.assertEqual(len(tmp), expected_len)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1079 self.assertEqual(tmp, self.set)
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_remove_present(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1082 self.set.remove("b")
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1083 self.assertEqual(self.set, set("ac"))
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_remove_absent(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1086 try:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1087 self.set.remove("d")
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1088 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
1089 except LookupError:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1090 pass
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1091
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1092 def test_remove_until_empty(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1093 expected_len = len(self.set)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1094 for v in self.values:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1095 self.set.remove(v)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1096 expected_len -= 1
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1097 self.assertEqual(len(self.set), expected_len)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1098
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1099 def test_discard_present(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1100 self.set.discard("c")
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1101 self.assertEqual(self.set, set("ab"))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1102
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1103 def test_discard_absent(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1104 self.set.discard("d")
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1105 self.assertEqual(self.set, set("abc"))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1106
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1107 def test_clear(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1108 self.set.clear()
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1109 self.assertEqual(len(self.set), 0)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1110
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1111 def test_pop(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1112 popped = {}
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1113 while self.set:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1114 popped[self.set.pop()] = None
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1115 self.assertEqual(len(popped), len(self.values))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1116 for v in self.values:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1117 self.failUnless(v in popped)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1118
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1119 def test_update_empty_tuple(self):
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
1120 self.set.update(())
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1121 self.assertEqual(self.set, set(self.values))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1122
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1123 def test_update_unit_tuple_overlap(self):
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
1124 self.set.update(("a",))
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1125 self.assertEqual(self.set, set(self.values))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1126
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1127 def test_update_unit_tuple_non_overlap(self):
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
1128 self.set.update(("a", "z"))
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1129 self.assertEqual(self.set, set(self.values + ["z"]))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1130
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1131 #==============================================================================
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 class TestSubsets(unittest.TestCase):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1134
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1135 case2method = {"<=": "issubset",
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1136 ">=": "issuperset",
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1137 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1138
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1139 reverse = {"==": "==",
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1140 "!=": "!=",
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1141 "<": ">",
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1142 ">": "<",
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 ">=": "<=",
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1145 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1146
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1147 def test_issubset(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1148 x = self.left
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1149 y = self.right
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1150 for case in "!=", "==", "<", "<=", ">", ">=":
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1151 expected = case in self.cases
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1152 # Test the binary infix spelling.
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1153 result = eval("x" + case + "y", locals())
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1154 self.assertEqual(result, expected)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1155 # 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
1156 if case in TestSubsets.case2method:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1157 method = getattr(x, TestSubsets.case2method[case])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1158 result = method(y)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1159 self.assertEqual(result, expected)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1160
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1161 # Now do the same for the operands reversed.
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1162 rcase = TestSubsets.reverse[case]
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1163 result = eval("y" + rcase + "x", locals())
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1164 self.assertEqual(result, expected)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1165 if rcase in TestSubsets.case2method:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1166 method = getattr(y, TestSubsets.case2method[rcase])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1167 result = method(x)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1168 self.assertEqual(result, expected)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1169 #------------------------------------------------------------------------------
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 class TestSubsetEqualEmpty(TestSubsets):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1172 left = set()
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1173 right = set()
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1174 name = "both empty"
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1175 cases = "==", "<=", ">="
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1176
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1177 #------------------------------------------------------------------------------
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 class TestSubsetEqualNonEmpty(TestSubsets):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1180 left = set([1, 2])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1181 right = set([1, 2])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1182 name = "equal pair"
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1183 cases = "==", "<=", ">="
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 #------------------------------------------------------------------------------
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 class TestSubsetEmptyNonEmpty(TestSubsets):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1188 left = set()
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1189 right = set([1, 2])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1190 name = "one empty, one non-empty"
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1191 cases = "!=", "<", "<="
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 class TestSubsetPartial(TestSubsets):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1196 left = set([1])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1197 right = set([1, 2])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1198 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
1199 cases = "!=", "<", "<="
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1200
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1201 #------------------------------------------------------------------------------
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1202
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1203 class TestSubsetNonOverlap(TestSubsets):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1204 left = set([1])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1205 right = set([2])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1206 name = "neither empty, neither contains"
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1207 cases = "!="
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1208
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1209 #==============================================================================
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1210
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1211 class TestOnlySetsInBinaryOps(unittest.TestCase):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1212
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1213 def test_eq_ne(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1214 # 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
1215 self.assertEqual(self.other == self.set, False)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1216 self.assertEqual(self.set == self.other, False)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1217 self.assertEqual(self.other != self.set, True)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1218 self.assertEqual(self.set != self.other, True)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1219
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1220 def test_ge_gt_le_lt(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1221 self.assertRaises(TypeError, lambda: self.set < self.other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1222 self.assertRaises(TypeError, lambda: self.set <= self.other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1223 self.assertRaises(TypeError, lambda: self.set > self.other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1224 self.assertRaises(TypeError, lambda: self.set >= self.other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1225
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1226 self.assertRaises(TypeError, lambda: self.other < self.set)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1227 self.assertRaises(TypeError, lambda: self.other <= self.set)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1228 self.assertRaises(TypeError, lambda: self.other > self.set)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1229 self.assertRaises(TypeError, lambda: self.other >= self.set)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1230
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
1231 def test_update_operator(self):
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1232 try:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1233 self.set |= self.other
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1234 except TypeError:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1235 pass
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1236 else:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1237 self.fail("expected TypeError")
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1238
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
1239 def test_update(self):
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1240 if self.otherIsIterable:
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
1241 self.set.update(self.other)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1242 else:
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
1243 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
1244
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1245 def test_union(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1246 self.assertRaises(TypeError, lambda: self.set | self.other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1247 self.assertRaises(TypeError, lambda: self.other | self.set)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1248 if self.otherIsIterable:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1249 self.set.union(self.other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1250 else:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1251 self.assertRaises(TypeError, self.set.union, self.other)
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 def test_intersection_update_operator(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1254 try:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1255 self.set &= self.other
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1256 except TypeError:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1257 pass
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1258 else:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1259 self.fail("expected TypeError")
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 def test_intersection_update(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1262 if self.otherIsIterable:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1263 self.set.intersection_update(self.other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1264 else:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1265 self.assertRaises(TypeError,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1266 self.set.intersection_update,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1267 self.other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1268
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1269 def test_intersection(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1270 self.assertRaises(TypeError, lambda: self.set & self.other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1271 self.assertRaises(TypeError, lambda: self.other & self.set)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1272 if self.otherIsIterable:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1273 self.set.intersection(self.other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1274 else:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1275 self.assertRaises(TypeError, self.set.intersection, self.other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1276
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1277 def test_sym_difference_update_operator(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1278 try:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1279 self.set ^= self.other
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1280 except TypeError:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1281 pass
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1282 else:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1283 self.fail("expected TypeError")
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1284
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1285 def test_sym_difference_update(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1286 if self.otherIsIterable:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1287 self.set.symmetric_difference_update(self.other)
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.assertRaises(TypeError,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1290 self.set.symmetric_difference_update,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1291 self.other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1292
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1293 def test_sym_difference(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1294 self.assertRaises(TypeError, lambda: self.set ^ self.other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1295 self.assertRaises(TypeError, lambda: self.other ^ self.set)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1296 if self.otherIsIterable:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1297 self.set.symmetric_difference(self.other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1298 else:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1299 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
1300
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1301 def test_difference_update_operator(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1302 try:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1303 self.set -= self.other
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1304 except TypeError:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1305 pass
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1306 else:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1307 self.fail("expected TypeError")
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1308
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1309 def test_difference_update(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1310 if self.otherIsIterable:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1311 self.set.difference_update(self.other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1312 else:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1313 self.assertRaises(TypeError,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1314 self.set.difference_update,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1315 self.other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1316
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1317 def test_difference(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1318 self.assertRaises(TypeError, lambda: self.set - self.other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1319 self.assertRaises(TypeError, lambda: self.other - self.set)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1320 if self.otherIsIterable:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1321 self.set.difference(self.other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1322 else:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1323 self.assertRaises(TypeError, self.set.difference, self.other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1324
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1325 #------------------------------------------------------------------------------
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1326
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1327 class TestOnlySetsNumeric(TestOnlySetsInBinaryOps):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1328 def setUp(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1329 self.set = set((1, 2, 3))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1330 self.other = 19
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1331 self.otherIsIterable = False
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1332
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1333 #------------------------------------------------------------------------------
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1334
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1335 class TestOnlySetsDict(TestOnlySetsInBinaryOps):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1336 def setUp(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1337 self.set = set((1, 2, 3))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1338 self.other = {1:2, 3:4}
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1339 self.otherIsIterable = True
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1340
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1341 #------------------------------------------------------------------------------
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1342
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1343 class TestOnlySetsOperator(TestOnlySetsInBinaryOps):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1344 def setUp(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1345 self.set = set((1, 2, 3))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1346 self.other = operator.add
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1347 self.otherIsIterable = False
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1348
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1349 #------------------------------------------------------------------------------
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1350
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1351 class TestOnlySetsTuple(TestOnlySetsInBinaryOps):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1352 def setUp(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1353 self.set = set((1, 2, 3))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1354 self.other = (2, 4, 6)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1355 self.otherIsIterable = True
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1356
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1357 #------------------------------------------------------------------------------
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1358
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1359 class TestOnlySetsString(TestOnlySetsInBinaryOps):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1360 def setUp(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1361 self.set = set((1, 2, 3))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1362 self.other = 'abc'
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1363 self.otherIsIterable = True
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1364
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1365 #------------------------------------------------------------------------------
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1366
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1367 class TestOnlySetsGenerator(TestOnlySetsInBinaryOps):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1368 def setUp(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1369 def gen():
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1370 for i in xrange(0, 10, 2):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1371 yield i
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1372 self.set = set((1, 2, 3))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1373 self.other = gen()
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1374 self.otherIsIterable = True
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1375
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 class TestCopying(unittest.TestCase):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1379
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1380 def test_copy(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1381 dup = self.set.copy()
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1382 dup_list = list(dup); dup_list.sort()
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1383 set_list = list(self.set); set_list.sort()
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1384 self.assertEqual(len(dup_list), len(set_list))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1385 for i in range(len(dup_list)):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1386 self.failUnless(dup_list[i] is set_list[i])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1387
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1388 def test_deep_copy(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1389 dup = copy.deepcopy(self.set)
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 31389
diff changeset
1390 ##print type(dup), repr(dup)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1391 dup_list = list(dup); dup_list.sort()
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1392 set_list = list(self.set); set_list.sort()
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1393 self.assertEqual(len(dup_list), len(set_list))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1394 for i in range(len(dup_list)):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1395 self.assertEqual(dup_list[i], set_list[i])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1396
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1397 #------------------------------------------------------------------------------
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1398
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1399 class TestCopyingEmpty(TestCopying):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1400 def setUp(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1401 self.set = set()
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1402
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1403 #------------------------------------------------------------------------------
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1404
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1405 class TestCopyingSingleton(TestCopying):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1406 def setUp(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1407 self.set = set(["hello"])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1408
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1409 #------------------------------------------------------------------------------
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1410
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1411 class TestCopyingTriple(TestCopying):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1412 def setUp(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1413 self.set = set(["zero", 0, None])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1414
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1415 #------------------------------------------------------------------------------
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1416
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1417 class TestCopyingTuple(TestCopying):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1418 def setUp(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1419 self.set = set([(1, 2)])
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 TestCopyingNested(TestCopying):
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 self.set = set([((1, 2), (3, 4))])
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1426
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1427 #==============================================================================
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1428
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1429 class TestIdentities(unittest.TestCase):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1430 def setUp(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1431 self.a = set('abracadabra')
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1432 self.b = set('alacazam')
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 def test_binopsVsSubsets(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1435 a, b = self.a, self.b
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1436 self.assert_(a - b < a)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1437 self.assert_(b - a < b)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1438 self.assert_(a & b < a)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1439 self.assert_(a & b < b)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1440 self.assert_(a | b > a)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1441 self.assert_(a | b > b)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1442 self.assert_(a ^ b < a | b)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1443
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1444 def test_commutativity(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1445 a, b = self.a, self.b
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1446 self.assertEqual(a&b, b&a)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1447 self.assertEqual(a|b, b|a)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1448 self.assertEqual(a^b, b^a)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1449 if a != b:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1450 self.assertNotEqual(a-b, b-a)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1451
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1452 def test_summations(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1453 # check that sums of parts equal the whole
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1454 a, b = self.a, self.b
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1455 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
1456 self.assertEqual((a&b)|(a^b), a|b)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1457 self.assertEqual(a|(b-a), a|b)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1458 self.assertEqual((a-b)|b, a|b)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1459 self.assertEqual((a-b)|(a&b), a)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1460 self.assertEqual((b-a)|(a&b), b)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1461 self.assertEqual((a-b)|(b-a), a^b)
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 def test_exclusion(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1464 # check that inverse operations show non-overlap
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1465 a, b, zero = self.a, self.b, set()
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1466 self.assertEqual((a-b)&b, zero)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1467 self.assertEqual((b-a)&a, zero)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1468 self.assertEqual((a&b)&(a^b), zero)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1469
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1470 # Tests derived from test_itertools.py =======================================
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1471
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1472 def R(seqn):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1473 'Regular generator'
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1474 for i in seqn:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1475 yield i
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1476
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1477 class G:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1478 'Sequence using __getitem__'
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1479 def __init__(self, seqn):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1480 self.seqn = seqn
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1481 def __getitem__(self, i):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1482 return self.seqn[i]
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1483
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1484 class I:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1485 'Sequence using iterator protocol'
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1486 def __init__(self, seqn):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1487 self.seqn = seqn
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1488 self.i = 0
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1489 def __iter__(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1490 return self
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1491 def next(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1492 if self.i >= len(self.seqn): raise StopIteration
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1493 v = self.seqn[self.i]
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1494 self.i += 1
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1495 return v
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1496
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1497 class Ig:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1498 'Sequence using iterator protocol defined with a generator'
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1499 def __init__(self, seqn):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1500 self.seqn = seqn
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1501 self.i = 0
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1502 def __iter__(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1503 for val in self.seqn:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1504 yield val
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1505
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1506 class X:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1507 'Missing __getitem__ and __iter__'
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1508 def __init__(self, seqn):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1509 self.seqn = seqn
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1510 self.i = 0
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1511 def next(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1512 if self.i >= len(self.seqn): raise StopIteration
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1513 v = self.seqn[self.i]
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1514 self.i += 1
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1515 return v
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1516
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1517 class N:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1518 'Iterator missing next()'
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1519 def __init__(self, seqn):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1520 self.seqn = seqn
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1521 self.i = 0
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1522 def __iter__(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1523 return self
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1524
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1525 class E:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1526 'Test propagation of exceptions'
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1527 def __init__(self, seqn):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1528 self.seqn = seqn
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1529 self.i = 0
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1530 def __iter__(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1531 return self
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1532 def next(self):
33597
355b15e4a4da Use floor division operator.
Raymond Hettinger <python@rcn.com>
parents: 32555
diff changeset
1533 3 // 0
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1534
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1535 class S:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1536 'Test immediate stop'
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1537 def __init__(self, seqn):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1538 pass
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1539 def __iter__(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1540 return self
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1541 def next(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1542 raise StopIteration
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1543
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1544 from itertools import chain, imap
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1545 def L(seqn):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1546 'Test multiple tiers of iterators'
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1547 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
1548
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1549 class TestVariousIteratorArgs(unittest.TestCase):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1550
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1551 def test_constructor(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1552 for cons in (set, frozenset):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1553 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
1554 for g in (G, I, Ig, S, L, R):
31210
c06b570adf12 Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents: 31008
diff changeset
1555 self.assertEqual(sorted(cons(g(s))), sorted(g(s)))
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1556 self.assertRaises(TypeError, cons , X(s))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1557 self.assertRaises(TypeError, cons , N(s))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1558 self.assertRaises(ZeroDivisionError, cons , E(s))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1559
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1560 def test_inline_methods(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1561 s = set('november')
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1562 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
1563 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
1564 for g in (G, I, Ig, L, R):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1565 expected = meth(data)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1566 actual = meth(G(data))
43794
2b3abe3d3ae6 Add set.isdisjoint()
Raymond Hettinger <python@rcn.com>
parents: 41532
diff changeset
1567 if isinstance(expected, bool):
2b3abe3d3ae6 Add set.isdisjoint()
Raymond Hettinger <python@rcn.com>
parents: 41532
diff changeset
1568 self.assertEqual(actual, expected)
2b3abe3d3ae6 Add set.isdisjoint()
Raymond Hettinger <python@rcn.com>
parents: 41532
diff changeset
1569 else:
2b3abe3d3ae6 Add set.isdisjoint()
Raymond Hettinger <python@rcn.com>
parents: 41532
diff changeset
1570 self.assertEqual(sorted(actual), sorted(expected))
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1571 self.assertRaises(TypeError, meth, X(s))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1572 self.assertRaises(TypeError, meth, N(s))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1573 self.assertRaises(ZeroDivisionError, meth, E(s))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1574
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1575 def test_inplace_methods(self):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1576 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
1577 for methname in ('update', 'intersection_update',
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1578 'difference_update', 'symmetric_difference_update'):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1579 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
1580 s = set('january')
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1581 t = s.copy()
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1582 getattr(s, methname)(list(g(data)))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1583 getattr(t, methname)(g(data))
31210
c06b570adf12 Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents: 31008
diff changeset
1584 self.assertEqual(sorted(s), sorted(t))
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1585
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1586 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
1587 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
1588 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
1589
44926
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1590 # 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
1591
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1592 def powerset(U):
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1593 """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
1594 U = iter(U)
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1595 try:
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1596 x = frozenset([U.next()])
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1597 for S in powerset(U):
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1598 yield S
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1599 yield S | x
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1600 except StopIteration:
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1601 yield frozenset()
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1602
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1603 def cube(n):
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1604 """Graph of n-dimensional hypercube."""
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1605 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
1606 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
1607 for x in powerset(range(n))])
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1608
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1609 def linegraph(G):
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1610 """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
1611 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
1612 edges share a vertex."""
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1613 L = {}
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1614 for x in G:
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1615 for y in G[x]:
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1616 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
1617 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
1618 L[frozenset([x,y])] = frozenset(nx+ny)
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1619 return L
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1620
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1621 def faces(G):
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1622 '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
1623 # currently limited to triangles,squares, and pentagons
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1624 f = set()
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1625 for v1, edges in G.items():
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1626 for v2 in edges:
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1627 for v3 in G[v2]:
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1628 if v1 == v3:
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1629 continue
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1630 if v1 in G[v3]:
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1631 f.add(frozenset([v1, v2, v3]))
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1632 else:
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1633 for v4 in G[v3]:
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1634 if v4 == v2:
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1635 continue
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1636 if v1 in G[v4]:
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1637 f.add(frozenset([v1, v2, v3, v4]))
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1638 else:
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1639 for v5 in G[v4]:
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1640 if v5 == v3 or v5 == v2:
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1641 continue
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1642 if v1 in G[v5]:
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1643 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
1644 return f
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1645
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1646
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1647 class TestGraphs(unittest.TestCase):
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1648
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1649 def test_cube(self):
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1650
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1651 g = cube(3) # vert --> {v1, v2, v3}
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1652 vertices1 = set(g)
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1653 self.assertEqual(len(vertices1), 8) # eight vertices
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1654 for edge in g.values():
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1655 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
1656 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
1657 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
1658
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1659 cubefaces = faces(g)
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1660 self.assertEqual(len(cubefaces), 6) # six faces
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1661 for face in cubefaces:
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1662 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
1663
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1664 def test_cuboctahedron(self):
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1665
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1666 # http://en.wikipedia.org/wiki/Cuboctahedron
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1667 # 8 triangular faces and 6 square faces
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1668 # 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
1669
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1670 g = cube(3)
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1671 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
1672 self.assertEqual(len(cuboctahedron), 12)# twelve vertices
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1673
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1674 vertices = set(cuboctahedron)
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1675 for edges in cuboctahedron.values():
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1676 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
1677 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
1678 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
1679
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1680 cubofaces = faces(cuboctahedron)
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1681 facesizes = collections.defaultdict(int)
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1682 for face in cubofaces:
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1683 facesizes[len(face)] += 1
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1684 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
1685 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
1686
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1687 for vertex in cuboctahedron:
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1688 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
1689 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
1690 for cubevert in edge:
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1691 self.assert_(cubevert in g)
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1692
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1693
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1694 #==============================================================================
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1695
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1696 def test_main(verbose=None):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1697 from test import test_sets
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1698 test_classes = (
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1699 TestSet,
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30929
diff changeset
1700 TestSetSubclass,
40732
5dae0d7f683a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 40601
diff changeset
1701 TestSetSubclassWithKeywordArgs,
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1702 TestFrozenSet,
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30929
diff changeset
1703 TestFrozenSetSubclass,
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1704 TestSetOfSets,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1705 TestExceptionPropagation,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1706 TestBasicOpsEmpty,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1707 TestBasicOpsSingleton,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1708 TestBasicOpsTuple,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1709 TestBasicOpsTriple,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1710 TestBinaryOps,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1711 TestUpdateOps,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1712 TestMutate,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1713 TestSubsetEqualEmpty,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1714 TestSubsetEqualNonEmpty,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1715 TestSubsetEmptyNonEmpty,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1716 TestSubsetPartial,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1717 TestSubsetNonOverlap,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1718 TestOnlySetsNumeric,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1719 TestOnlySetsDict,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1720 TestOnlySetsOperator,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1721 TestOnlySetsTuple,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1722 TestOnlySetsString,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1723 TestOnlySetsGenerator,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1724 TestCopyingEmpty,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1725 TestCopyingSingleton,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1726 TestCopyingTriple,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1727 TestCopyingTuple,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1728 TestCopyingNested,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1729 TestIdentities,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1730 TestVariousIteratorArgs,
44926
3f61b6643b7e Expand tests to include nested graph structures.
Raymond Hettinger <python@rcn.com>
parents: 43794
diff changeset
1731 TestGraphs,
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1732 )
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1733
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1734 test_support.run_unittest(*test_classes)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1735
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1736 # verify reference counting
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1737 if verbose and hasattr(sys, "gettotalrefcount"):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1738 import gc
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1739 counts = [None] * 5
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1740 for i in xrange(len(counts)):
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1741 test_support.run_unittest(*test_classes)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1742 gc.collect()
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1743 counts[i] = sys.gettotalrefcount()
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1744 print counts
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1745
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1746 if __name__ == "__main__":
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1747 test_main(verbose=True)