Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1 | import unittest |
| 2 | from test import test_support |
Raymond Hettinger | 691d805 | 2004-05-30 07:26:47 +0000 | [diff] [blame] | 3 | from weakref import proxy |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 4 | import operator |
| 5 | import copy |
| 6 | import pickle |
Raymond Hettinger | eae05de | 2004-07-09 04:51:24 +0000 | [diff] [blame] | 7 | import os |
Raymond Hettinger | 82cb9a2 | 2005-07-05 05:34:43 +0000 | [diff] [blame] | 8 | from random import randrange, shuffle |
Raymond Hettinger | c47e01d | 2005-08-16 10:44:15 +0000 | [diff] [blame] | 9 | import sys |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 10 | |
| 11 | class PassThru(Exception): |
| 12 | pass |
| 13 | |
| 14 | def check_pass_thru(): |
| 15 | raise PassThru |
| 16 | yield 1 |
| 17 | |
Raymond Hettinger | 9bda1d6 | 2005-09-16 07:14:21 +0000 | [diff] [blame] | 18 | class BadCmp: |
| 19 | def __hash__(self): |
| 20 | return 1 |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 21 | def __eq__(self, other): |
Raymond Hettinger | 9bda1d6 | 2005-09-16 07:14:21 +0000 | [diff] [blame] | 22 | raise RuntimeError |
| 23 | |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 24 | class ReprWrapper: |
| 25 | 'Used to test self-referential repr() calls' |
| 26 | def __repr__(self): |
| 27 | return repr(self.value) |
| 28 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 29 | class HashCountingInt(int): |
| 30 | 'int-like object that counts the number of times __hash__ is called' |
| 31 | def __init__(self, *args): |
| 32 | self.hash_count = 0 |
| 33 | def __hash__(self): |
| 34 | self.hash_count += 1 |
| 35 | return int.__hash__(self) |
| 36 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 37 | class TestJointOps(unittest.TestCase): |
| 38 | # Tests common to both set and frozenset |
| 39 | |
| 40 | def setUp(self): |
| 41 | self.word = word = 'simsalabim' |
| 42 | self.otherword = 'madagascar' |
| 43 | self.letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' |
| 44 | self.s = self.thetype(word) |
| 45 | self.d = dict.fromkeys(word) |
| 46 | |
Raymond Hettinger | 6429a47 | 2004-09-28 01:51:35 +0000 | [diff] [blame] | 47 | def test_new_or_init(self): |
| 48 | self.assertRaises(TypeError, self.thetype, [], 2) |
| 49 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 50 | def test_uniquification(self): |
Raymond Hettinger | 64958a1 | 2003-12-17 20:43:33 +0000 | [diff] [blame] | 51 | actual = sorted(self.s) |
| 52 | expected = sorted(self.d) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 53 | self.assertEqual(actual, expected) |
| 54 | self.assertRaises(PassThru, self.thetype, check_pass_thru()) |
| 55 | self.assertRaises(TypeError, self.thetype, [[]]) |
| 56 | |
| 57 | def test_len(self): |
| 58 | self.assertEqual(len(self.s), len(self.d)) |
| 59 | |
| 60 | def test_contains(self): |
| 61 | for c in self.letters: |
| 62 | self.assertEqual(c in self.s, c in self.d) |
| 63 | self.assertRaises(TypeError, self.s.__contains__, [[]]) |
Raymond Hettinger | 19c2d77 | 2003-11-21 18:36:54 +0000 | [diff] [blame] | 64 | s = self.thetype([frozenset(self.letters)]) |
| 65 | self.assert_(self.thetype(self.letters) in s) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 66 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 67 | def test_union(self): |
| 68 | u = self.s.union(self.otherword) |
| 69 | for c in self.letters: |
| 70 | self.assertEqual(c in u, c in self.d or c in self.otherword) |
Raymond Hettinger | 49ba4c3 | 2003-11-23 02:49:05 +0000 | [diff] [blame] | 71 | self.assertEqual(self.s, self.thetype(self.word)) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 72 | self.assertEqual(type(u), self.thetype) |
| 73 | self.assertRaises(PassThru, self.s.union, check_pass_thru()) |
| 74 | self.assertRaises(TypeError, self.s.union, [[]]) |
Guido van Rossum | 75a902d | 2007-10-19 22:06:24 +0000 | [diff] [blame] | 75 | for C in set, frozenset, dict.fromkeys, str, list, tuple: |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 76 | self.assertEqual(self.thetype('abcba').union(C('cdc')), set('abcd')) |
| 77 | self.assertEqual(self.thetype('abcba').union(C('efgfe')), set('abcefg')) |
| 78 | self.assertEqual(self.thetype('abcba').union(C('ccb')), set('abc')) |
| 79 | self.assertEqual(self.thetype('abcba').union(C('ef')), set('abcef')) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 80 | |
| 81 | def test_or(self): |
| 82 | i = self.s.union(self.otherword) |
| 83 | self.assertEqual(self.s | set(self.otherword), i) |
| 84 | self.assertEqual(self.s | frozenset(self.otherword), i) |
| 85 | try: |
| 86 | self.s | self.otherword |
| 87 | except TypeError: |
| 88 | pass |
| 89 | else: |
| 90 | self.fail("s|t did not screen-out general iterables") |
| 91 | |
| 92 | def test_intersection(self): |
| 93 | i = self.s.intersection(self.otherword) |
| 94 | for c in self.letters: |
| 95 | self.assertEqual(c in i, c in self.d and c in self.otherword) |
Raymond Hettinger | 49ba4c3 | 2003-11-23 02:49:05 +0000 | [diff] [blame] | 96 | self.assertEqual(self.s, self.thetype(self.word)) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 97 | self.assertEqual(type(i), self.thetype) |
| 98 | self.assertRaises(PassThru, self.s.intersection, check_pass_thru()) |
Guido van Rossum | 75a902d | 2007-10-19 22:06:24 +0000 | [diff] [blame] | 99 | for C in set, frozenset, dict.fromkeys, str, list, tuple: |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 100 | self.assertEqual(self.thetype('abcba').intersection(C('cdc')), set('cc')) |
| 101 | self.assertEqual(self.thetype('abcba').intersection(C('efgfe')), set('')) |
| 102 | self.assertEqual(self.thetype('abcba').intersection(C('ccb')), set('bc')) |
| 103 | self.assertEqual(self.thetype('abcba').intersection(C('ef')), set('')) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 104 | |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame^] | 105 | def test_isdisjoint(self): |
| 106 | def f(s1, s2): |
| 107 | 'Pure python equivalent of isdisjoint()' |
| 108 | return not set(s1).intersection(s2) |
| 109 | for larg in '', 'a', 'ab', 'abc', 'ababac', 'cdc', 'cc', 'efgfe', 'ccb', 'ef': |
| 110 | s1 = self.thetype(larg) |
| 111 | for rarg in '', 'a', 'ab', 'abc', 'ababac', 'cdc', 'cc', 'efgfe', 'ccb', 'ef': |
| 112 | for C in set, frozenset, dict.fromkeys, str, list, tuple: |
| 113 | s2 = C(rarg) |
| 114 | actual = s1.isdisjoint(s2) |
| 115 | expected = f(s1, s2) |
| 116 | self.assertEqual(actual, expected) |
| 117 | self.assert_(actual is True or actual is False) |
| 118 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 119 | def test_and(self): |
| 120 | i = self.s.intersection(self.otherword) |
| 121 | self.assertEqual(self.s & set(self.otherword), i) |
| 122 | self.assertEqual(self.s & frozenset(self.otherword), i) |
| 123 | try: |
| 124 | self.s & self.otherword |
| 125 | except TypeError: |
| 126 | pass |
| 127 | else: |
| 128 | self.fail("s&t did not screen-out general iterables") |
| 129 | |
| 130 | def test_difference(self): |
| 131 | i = self.s.difference(self.otherword) |
| 132 | for c in self.letters: |
| 133 | self.assertEqual(c in i, c in self.d and c not in self.otherword) |
Raymond Hettinger | 49ba4c3 | 2003-11-23 02:49:05 +0000 | [diff] [blame] | 134 | self.assertEqual(self.s, self.thetype(self.word)) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 135 | self.assertEqual(type(i), self.thetype) |
| 136 | self.assertRaises(PassThru, self.s.difference, check_pass_thru()) |
| 137 | self.assertRaises(TypeError, self.s.difference, [[]]) |
Guido van Rossum | 75a902d | 2007-10-19 22:06:24 +0000 | [diff] [blame] | 138 | for C in set, frozenset, dict.fromkeys, str, list, tuple: |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 139 | self.assertEqual(self.thetype('abcba').difference(C('cdc')), set('ab')) |
| 140 | self.assertEqual(self.thetype('abcba').difference(C('efgfe')), set('abc')) |
| 141 | self.assertEqual(self.thetype('abcba').difference(C('ccb')), set('a')) |
| 142 | self.assertEqual(self.thetype('abcba').difference(C('ef')), set('abc')) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 143 | |
| 144 | def test_sub(self): |
| 145 | i = self.s.difference(self.otherword) |
| 146 | self.assertEqual(self.s - set(self.otherword), i) |
| 147 | self.assertEqual(self.s - frozenset(self.otherword), i) |
| 148 | try: |
| 149 | self.s - self.otherword |
| 150 | except TypeError: |
| 151 | pass |
| 152 | else: |
| 153 | self.fail("s-t did not screen-out general iterables") |
| 154 | |
| 155 | def test_symmetric_difference(self): |
| 156 | i = self.s.symmetric_difference(self.otherword) |
| 157 | for c in self.letters: |
| 158 | self.assertEqual(c in i, (c in self.d) ^ (c in self.otherword)) |
Raymond Hettinger | 49ba4c3 | 2003-11-23 02:49:05 +0000 | [diff] [blame] | 159 | self.assertEqual(self.s, self.thetype(self.word)) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 160 | self.assertEqual(type(i), self.thetype) |
| 161 | self.assertRaises(PassThru, self.s.symmetric_difference, check_pass_thru()) |
| 162 | self.assertRaises(TypeError, self.s.symmetric_difference, [[]]) |
Guido van Rossum | 75a902d | 2007-10-19 22:06:24 +0000 | [diff] [blame] | 163 | for C in set, frozenset, dict.fromkeys, str, list, tuple: |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 164 | self.assertEqual(self.thetype('abcba').symmetric_difference(C('cdc')), set('abd')) |
| 165 | self.assertEqual(self.thetype('abcba').symmetric_difference(C('efgfe')), set('abcefg')) |
| 166 | self.assertEqual(self.thetype('abcba').symmetric_difference(C('ccb')), set('a')) |
| 167 | self.assertEqual(self.thetype('abcba').symmetric_difference(C('ef')), set('abcef')) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 168 | |
| 169 | def test_xor(self): |
| 170 | i = self.s.symmetric_difference(self.otherword) |
| 171 | self.assertEqual(self.s ^ set(self.otherword), i) |
| 172 | self.assertEqual(self.s ^ frozenset(self.otherword), i) |
| 173 | try: |
| 174 | self.s ^ self.otherword |
| 175 | except TypeError: |
| 176 | pass |
| 177 | else: |
| 178 | self.fail("s^t did not screen-out general iterables") |
| 179 | |
| 180 | def test_equality(self): |
| 181 | self.assertEqual(self.s, set(self.word)) |
| 182 | self.assertEqual(self.s, frozenset(self.word)) |
| 183 | self.assertEqual(self.s == self.word, False) |
| 184 | self.assertNotEqual(self.s, set(self.otherword)) |
| 185 | self.assertNotEqual(self.s, frozenset(self.otherword)) |
| 186 | self.assertEqual(self.s != self.word, True) |
| 187 | |
| 188 | def test_setOfFrozensets(self): |
| 189 | t = map(frozenset, ['abcdef', 'bcd', 'bdcb', 'fed', 'fedccba']) |
| 190 | s = self.thetype(t) |
| 191 | self.assertEqual(len(s), 3) |
| 192 | |
| 193 | def test_compare(self): |
| 194 | self.assertRaises(TypeError, self.s.__cmp__, self.s) |
| 195 | |
| 196 | def test_sub_and_super(self): |
| 197 | p, q, r = map(self.thetype, ['ab', 'abcde', 'def']) |
| 198 | self.assert_(p < q) |
| 199 | self.assert_(p <= q) |
| 200 | self.assert_(q <= q) |
| 201 | self.assert_(q > p) |
| 202 | self.assert_(q >= p) |
| 203 | self.failIf(q < r) |
| 204 | self.failIf(q <= r) |
| 205 | self.failIf(q > r) |
| 206 | self.failIf(q >= r) |
Raymond Hettinger | 3fbec70 | 2003-11-21 07:56:36 +0000 | [diff] [blame] | 207 | self.assert_(set('a').issubset('abc')) |
| 208 | self.assert_(set('abc').issuperset('a')) |
| 209 | self.failIf(set('a').issubset('cbs')) |
| 210 | self.failIf(set('cbs').issuperset('a')) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 211 | |
| 212 | def test_pickling(self): |
Raymond Hettinger | 15056a5 | 2004-11-09 07:25:31 +0000 | [diff] [blame] | 213 | for i in (0, 1, 2): |
| 214 | p = pickle.dumps(self.s, i) |
| 215 | dup = pickle.loads(p) |
| 216 | self.assertEqual(self.s, dup, "%s != %s" % (self.s, dup)) |
| 217 | if type(self.s) not in (set, frozenset): |
| 218 | self.s.x = 10 |
| 219 | p = pickle.dumps(self.s) |
| 220 | dup = pickle.loads(p) |
| 221 | self.assertEqual(self.s.x, dup.x) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 222 | |
Raymond Hettinger | 49ba4c3 | 2003-11-23 02:49:05 +0000 | [diff] [blame] | 223 | def test_deepcopy(self): |
| 224 | class Tracer: |
| 225 | def __init__(self, value): |
| 226 | self.value = value |
| 227 | def __hash__(self): |
Tim Peters | 58eb11c | 2004-01-18 20:29:55 +0000 | [diff] [blame] | 228 | return self.value |
Raymond Hettinger | 49ba4c3 | 2003-11-23 02:49:05 +0000 | [diff] [blame] | 229 | def __deepcopy__(self, memo=None): |
| 230 | return Tracer(self.value + 1) |
| 231 | t = Tracer(10) |
| 232 | s = self.thetype([t]) |
| 233 | dup = copy.deepcopy(s) |
| 234 | self.assertNotEqual(id(s), id(dup)) |
| 235 | for elem in dup: |
| 236 | newt = elem |
| 237 | self.assertNotEqual(id(t), id(newt)) |
| 238 | self.assertEqual(t.value + 1, newt.value) |
| 239 | |
Raymond Hettinger | bb999b5 | 2005-06-18 21:00:26 +0000 | [diff] [blame] | 240 | def test_gc(self): |
| 241 | # Create a nest of cycles to exercise overall ref count check |
| 242 | class A: |
| 243 | pass |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 244 | s = set(A() for i in range(1000)) |
Raymond Hettinger | bb999b5 | 2005-06-18 21:00:26 +0000 | [diff] [blame] | 245 | for elem in s: |
| 246 | elem.cycle = s |
| 247 | elem.sub = elem |
| 248 | elem.set = set([elem]) |
| 249 | |
Raymond Hettinger | 97979dd | 2005-08-12 23:58:22 +0000 | [diff] [blame] | 250 | def test_subclass_with_custom_hash(self): |
| 251 | # Bug #1257731 |
| 252 | class H(self.thetype): |
| 253 | def __hash__(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 254 | return int(id(self) & 0x7fffffff) |
Raymond Hettinger | 97979dd | 2005-08-12 23:58:22 +0000 | [diff] [blame] | 255 | s=H() |
| 256 | f=set() |
| 257 | f.add(s) |
| 258 | self.assert_(s in f) |
| 259 | f.remove(s) |
| 260 | f.add(s) |
| 261 | f.discard(s) |
| 262 | |
Raymond Hettinger | 9bda1d6 | 2005-09-16 07:14:21 +0000 | [diff] [blame] | 263 | def test_badcmp(self): |
| 264 | s = self.thetype([BadCmp()]) |
| 265 | # Detect comparison errors during insertion and lookup |
| 266 | self.assertRaises(RuntimeError, self.thetype, [BadCmp(), BadCmp()]) |
| 267 | self.assertRaises(RuntimeError, s.__contains__, BadCmp()) |
| 268 | # Detect errors during mutating operations |
| 269 | if hasattr(s, 'add'): |
| 270 | self.assertRaises(RuntimeError, s.add, BadCmp()) |
| 271 | self.assertRaises(RuntimeError, s.discard, BadCmp()) |
| 272 | self.assertRaises(RuntimeError, s.remove, BadCmp()) |
| 273 | |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 274 | def test_cyclical_repr(self): |
| 275 | w = ReprWrapper() |
| 276 | s = self.thetype([w]) |
| 277 | w.value = s |
| 278 | if self.thetype == set: |
| 279 | self.assertEqual(repr(s), '{set(...)}') |
| 280 | else: |
| 281 | name = repr(s).partition('(')[0] # strip class name |
Guido van Rossum | bdba5cf | 2007-08-07 22:44:20 +0000 | [diff] [blame] | 282 | self.assertEqual(repr(s), '%s({%s(...)})' % (name, name)) |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 283 | |
| 284 | def test_cyclical_print(self): |
| 285 | w = ReprWrapper() |
| 286 | s = self.thetype([w]) |
| 287 | w.value = s |
| 288 | try: |
Guido van Rossum | 292aa0d | 2007-05-24 18:00:35 +0000 | [diff] [blame] | 289 | fo = open(test_support.TESTFN, "w") |
Guido van Rossum | d8c1967 | 2007-02-09 21:54:58 +0000 | [diff] [blame] | 290 | fo.write(str(s)) |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 291 | fo.close() |
Guido van Rossum | 292aa0d | 2007-05-24 18:00:35 +0000 | [diff] [blame] | 292 | fo = open(test_support.TESTFN, "r") |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 293 | self.assertEqual(fo.read(), repr(s)) |
| 294 | finally: |
| 295 | fo.close() |
| 296 | os.remove(test_support.TESTFN) |
| 297 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 298 | def test_do_not_rehash_dict_keys(self): |
| 299 | n = 10 |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 300 | d = dict.fromkeys(map(HashCountingInt, range(n))) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 301 | self.assertEqual(sum(elem.hash_count for elem in d), n) |
| 302 | s = self.thetype(d) |
| 303 | self.assertEqual(sum(elem.hash_count for elem in d), n) |
| 304 | s.difference(d) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 305 | self.assertEqual(sum(elem.hash_count for elem in d), n) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 306 | if hasattr(s, 'symmetric_difference_update'): |
| 307 | s.symmetric_difference_update(d) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 308 | self.assertEqual(sum(elem.hash_count for elem in d), n) |
| 309 | d2 = dict.fromkeys(set(d)) |
| 310 | self.assertEqual(sum(elem.hash_count for elem in d), n) |
| 311 | d3 = dict.fromkeys(frozenset(d)) |
| 312 | self.assertEqual(sum(elem.hash_count for elem in d), n) |
| 313 | d3 = dict.fromkeys(frozenset(d), 123) |
| 314 | self.assertEqual(sum(elem.hash_count for elem in d), n) |
| 315 | self.assertEqual(d3, dict.fromkeys(d, 123)) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 316 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 317 | class TestSet(TestJointOps): |
| 318 | thetype = set |
| 319 | |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 320 | def test_init(self): |
Raymond Hettinger | 49ba4c3 | 2003-11-23 02:49:05 +0000 | [diff] [blame] | 321 | s = self.thetype() |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 322 | s.__init__(self.word) |
| 323 | self.assertEqual(s, set(self.word)) |
| 324 | s.__init__(self.otherword) |
| 325 | self.assertEqual(s, set(self.otherword)) |
Raymond Hettinger | eae05de | 2004-07-09 04:51:24 +0000 | [diff] [blame] | 326 | self.assertRaises(TypeError, s.__init__, s, 2); |
| 327 | self.assertRaises(TypeError, s.__init__, 1); |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 328 | |
Raymond Hettinger | 49ba4c3 | 2003-11-23 02:49:05 +0000 | [diff] [blame] | 329 | def test_constructor_identity(self): |
| 330 | s = self.thetype(range(3)) |
| 331 | t = self.thetype(s) |
| 332 | self.assertNotEqual(id(s), id(t)) |
| 333 | |
Guido van Rossum | 86e58e2 | 2006-08-28 15:27:34 +0000 | [diff] [blame] | 334 | def test_set_literal(self): |
| 335 | s = set([1,2,3]) |
| 336 | t = {1,2,3} |
| 337 | self.assertEqual(s, t) |
| 338 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 339 | def test_hash(self): |
| 340 | self.assertRaises(TypeError, hash, self.s) |
| 341 | |
| 342 | def test_clear(self): |
| 343 | self.s.clear() |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 344 | self.assertEqual(self.s, set()) |
| 345 | self.assertEqual(len(self.s), 0) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 346 | |
Raymond Hettinger | 49ba4c3 | 2003-11-23 02:49:05 +0000 | [diff] [blame] | 347 | def test_copy(self): |
| 348 | dup = self.s.copy() |
| 349 | self.assertEqual(self.s, dup) |
| 350 | self.assertNotEqual(id(self.s), id(dup)) |
| 351 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 352 | def test_add(self): |
| 353 | self.s.add('Q') |
| 354 | self.assert_('Q' in self.s) |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 355 | dup = self.s.copy() |
| 356 | self.s.add('Q') |
| 357 | self.assertEqual(self.s, dup) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 358 | self.assertRaises(TypeError, self.s.add, []) |
| 359 | |
| 360 | def test_remove(self): |
| 361 | self.s.remove('a') |
| 362 | self.assert_('a' not in self.s) |
| 363 | self.assertRaises(KeyError, self.s.remove, 'Q') |
| 364 | self.assertRaises(TypeError, self.s.remove, []) |
Raymond Hettinger | bfd334a | 2003-11-22 03:55:23 +0000 | [diff] [blame] | 365 | s = self.thetype([frozenset(self.word)]) |
| 366 | self.assert_(self.thetype(self.word) in s) |
| 367 | s.remove(self.thetype(self.word)) |
| 368 | self.assert_(self.thetype(self.word) not in s) |
| 369 | self.assertRaises(KeyError, self.s.remove, self.thetype(self.word)) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 370 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 371 | def test_remove_keyerror_unpacking(self): |
| 372 | # bug: www.python.org/sf/1576657 |
| 373 | for v1 in ['Q', (1,)]: |
| 374 | try: |
| 375 | self.s.remove(v1) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 376 | except KeyError as e: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 377 | v2 = e.args[0] |
| 378 | self.assertEqual(v1, v2) |
| 379 | else: |
| 380 | self.fail() |
| 381 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 382 | def test_discard(self): |
| 383 | self.s.discard('a') |
| 384 | self.assert_('a' not in self.s) |
| 385 | self.s.discard('Q') |
| 386 | self.assertRaises(TypeError, self.s.discard, []) |
Raymond Hettinger | bfd334a | 2003-11-22 03:55:23 +0000 | [diff] [blame] | 387 | s = self.thetype([frozenset(self.word)]) |
| 388 | self.assert_(self.thetype(self.word) in s) |
| 389 | s.discard(self.thetype(self.word)) |
| 390 | self.assert_(self.thetype(self.word) not in s) |
| 391 | s.discard(self.thetype(self.word)) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 392 | |
| 393 | def test_pop(self): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 394 | for i in range(len(self.s)): |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 395 | elem = self.s.pop() |
| 396 | self.assert_(elem not in self.s) |
| 397 | self.assertRaises(KeyError, self.s.pop) |
| 398 | |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 399 | def test_update(self): |
| 400 | retval = self.s.update(self.otherword) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 401 | self.assertEqual(retval, None) |
| 402 | for c in (self.word + self.otherword): |
| 403 | self.assert_(c in self.s) |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 404 | self.assertRaises(PassThru, self.s.update, check_pass_thru()) |
| 405 | self.assertRaises(TypeError, self.s.update, [[]]) |
| 406 | for p, q in (('cdc', 'abcd'), ('efgfe', 'abcefg'), ('ccb', 'abc'), ('ef', 'abcef')): |
Guido van Rossum | 75a902d | 2007-10-19 22:06:24 +0000 | [diff] [blame] | 407 | for C in set, frozenset, dict.fromkeys, str, list, tuple: |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 408 | s = self.thetype('abcba') |
| 409 | self.assertEqual(s.update(C(p)), None) |
| 410 | self.assertEqual(s, set(q)) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 411 | |
| 412 | def test_ior(self): |
| 413 | self.s |= set(self.otherword) |
| 414 | for c in (self.word + self.otherword): |
| 415 | self.assert_(c in self.s) |
| 416 | |
| 417 | def test_intersection_update(self): |
| 418 | retval = self.s.intersection_update(self.otherword) |
| 419 | self.assertEqual(retval, None) |
| 420 | for c in (self.word + self.otherword): |
| 421 | if c in self.otherword and c in self.word: |
| 422 | self.assert_(c in self.s) |
| 423 | else: |
| 424 | self.assert_(c not in self.s) |
| 425 | self.assertRaises(PassThru, self.s.intersection_update, check_pass_thru()) |
| 426 | self.assertRaises(TypeError, self.s.intersection_update, [[]]) |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 427 | for p, q in (('cdc', 'c'), ('efgfe', ''), ('ccb', 'bc'), ('ef', '')): |
Guido van Rossum | 75a902d | 2007-10-19 22:06:24 +0000 | [diff] [blame] | 428 | for C in set, frozenset, dict.fromkeys, str, list, tuple: |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 429 | s = self.thetype('abcba') |
| 430 | self.assertEqual(s.intersection_update(C(p)), None) |
| 431 | self.assertEqual(s, set(q)) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 432 | |
| 433 | def test_iand(self): |
| 434 | self.s &= set(self.otherword) |
| 435 | for c in (self.word + self.otherword): |
| 436 | if c in self.otherword and c in self.word: |
| 437 | self.assert_(c in self.s) |
| 438 | else: |
| 439 | self.assert_(c not in self.s) |
| 440 | |
| 441 | def test_difference_update(self): |
| 442 | retval = self.s.difference_update(self.otherword) |
| 443 | self.assertEqual(retval, None) |
| 444 | for c in (self.word + self.otherword): |
| 445 | if c in self.word and c not in self.otherword: |
| 446 | self.assert_(c in self.s) |
| 447 | else: |
| 448 | self.assert_(c not in self.s) |
| 449 | self.assertRaises(PassThru, self.s.difference_update, check_pass_thru()) |
| 450 | self.assertRaises(TypeError, self.s.difference_update, [[]]) |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 451 | self.assertRaises(TypeError, self.s.symmetric_difference_update, [[]]) |
| 452 | for p, q in (('cdc', 'ab'), ('efgfe', 'abc'), ('ccb', 'a'), ('ef', 'abc')): |
Guido van Rossum | 75a902d | 2007-10-19 22:06:24 +0000 | [diff] [blame] | 453 | for C in set, frozenset, dict.fromkeys, str, list, tuple: |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 454 | s = self.thetype('abcba') |
| 455 | self.assertEqual(s.difference_update(C(p)), None) |
| 456 | self.assertEqual(s, set(q)) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 457 | |
| 458 | def test_isub(self): |
| 459 | self.s -= set(self.otherword) |
| 460 | for c in (self.word + self.otherword): |
| 461 | if c in self.word and c not in self.otherword: |
| 462 | self.assert_(c in self.s) |
| 463 | else: |
| 464 | self.assert_(c not in self.s) |
| 465 | |
| 466 | def test_symmetric_difference_update(self): |
| 467 | retval = self.s.symmetric_difference_update(self.otherword) |
| 468 | self.assertEqual(retval, None) |
| 469 | for c in (self.word + self.otherword): |
| 470 | if (c in self.word) ^ (c in self.otherword): |
| 471 | self.assert_(c in self.s) |
| 472 | else: |
| 473 | self.assert_(c not in self.s) |
| 474 | self.assertRaises(PassThru, self.s.symmetric_difference_update, check_pass_thru()) |
| 475 | self.assertRaises(TypeError, self.s.symmetric_difference_update, [[]]) |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 476 | for p, q in (('cdc', 'abd'), ('efgfe', 'abcefg'), ('ccb', 'a'), ('ef', 'abcef')): |
Guido van Rossum | 75a902d | 2007-10-19 22:06:24 +0000 | [diff] [blame] | 477 | for C in set, frozenset, dict.fromkeys, str, list, tuple: |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 478 | s = self.thetype('abcba') |
| 479 | self.assertEqual(s.symmetric_difference_update(C(p)), None) |
| 480 | self.assertEqual(s, set(q)) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 481 | |
| 482 | def test_ixor(self): |
| 483 | self.s ^= set(self.otherword) |
| 484 | for c in (self.word + self.otherword): |
| 485 | if (c in self.word) ^ (c in self.otherword): |
| 486 | self.assert_(c in self.s) |
| 487 | else: |
| 488 | self.assert_(c not in self.s) |
| 489 | |
Raymond Hettinger | c991db2 | 2005-08-11 07:58:45 +0000 | [diff] [blame] | 490 | def test_inplace_on_self(self): |
| 491 | t = self.s.copy() |
| 492 | t |= t |
| 493 | self.assertEqual(t, self.s) |
| 494 | t &= t |
| 495 | self.assertEqual(t, self.s) |
| 496 | t -= t |
| 497 | self.assertEqual(t, self.thetype()) |
| 498 | t = self.s.copy() |
| 499 | t ^= t |
| 500 | self.assertEqual(t, self.thetype()) |
| 501 | |
Raymond Hettinger | 691d805 | 2004-05-30 07:26:47 +0000 | [diff] [blame] | 502 | def test_weakref(self): |
| 503 | s = self.thetype('gallahad') |
| 504 | p = proxy(s) |
| 505 | self.assertEqual(str(p), str(s)) |
| 506 | s = None |
| 507 | self.assertRaises(ReferenceError, str, p) |
| 508 | |
Guido van Rossum | 10ab4ae | 2007-08-23 23:57:24 +0000 | [diff] [blame] | 509 | def test_rich_compare(self): |
| 510 | class TestRichSetCompare: |
| 511 | def __gt__(self, some_set): |
| 512 | self.gt_called = True |
| 513 | return False |
| 514 | def __lt__(self, some_set): |
| 515 | self.lt_called = True |
| 516 | return False |
| 517 | def __ge__(self, some_set): |
| 518 | self.ge_called = True |
| 519 | return False |
| 520 | def __le__(self, some_set): |
| 521 | self.le_called = True |
| 522 | return False |
| 523 | |
| 524 | # This first tries the bulitin rich set comparison, which doesn't know |
| 525 | # how to handle the custom object. Upon returning NotImplemented, the |
| 526 | # corresponding comparison on the right object is invoked. |
| 527 | myset = {1, 2, 3} |
| 528 | |
| 529 | myobj = TestRichSetCompare() |
| 530 | myset < myobj |
| 531 | self.assert_(myobj.gt_called) |
| 532 | |
| 533 | myobj = TestRichSetCompare() |
| 534 | myset > myobj |
| 535 | self.assert_(myobj.lt_called) |
| 536 | |
| 537 | myobj = TestRichSetCompare() |
| 538 | myset <= myobj |
| 539 | self.assert_(myobj.ge_called) |
| 540 | |
| 541 | myobj = TestRichSetCompare() |
| 542 | myset >= myobj |
| 543 | self.assert_(myobj.le_called) |
| 544 | |
Raymond Hettinger | c47e01d | 2005-08-16 10:44:15 +0000 | [diff] [blame] | 545 | # C API test only available in a debug build |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 546 | if hasattr(set, "test_c_api"): |
Raymond Hettinger | c47e01d | 2005-08-16 10:44:15 +0000 | [diff] [blame] | 547 | def test_c_api(self): |
| 548 | self.assertEqual(set('abc').test_c_api(), True) |
| 549 | |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 550 | class SetSubclass(set): |
| 551 | pass |
| 552 | |
| 553 | class TestSetSubclass(TestSet): |
| 554 | thetype = SetSubclass |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 555 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 556 | class SetSubclassWithKeywordArgs(set): |
| 557 | def __init__(self, iterable=[], newarg=None): |
| 558 | set.__init__(self, iterable) |
| 559 | |
| 560 | class TestSetSubclassWithKeywordArgs(TestSet): |
Thomas Wouters | 9fe394c | 2007-02-05 01:24:16 +0000 | [diff] [blame] | 561 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 562 | def test_keywords_in_subclass(self): |
| 563 | 'SF bug #1486663 -- this used to erroneously raise a TypeError' |
| 564 | SetSubclassWithKeywordArgs(newarg=1) |
| 565 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 566 | class TestFrozenSet(TestJointOps): |
| 567 | thetype = frozenset |
| 568 | |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 569 | def test_init(self): |
Raymond Hettinger | 49ba4c3 | 2003-11-23 02:49:05 +0000 | [diff] [blame] | 570 | s = self.thetype(self.word) |
| 571 | s.__init__(self.otherword) |
| 572 | self.assertEqual(s, set(self.word)) |
| 573 | |
Raymond Hettinger | d794666 | 2005-08-01 21:39:29 +0000 | [diff] [blame] | 574 | def test_singleton_empty_frozenset(self): |
| 575 | f = frozenset() |
| 576 | efs = [frozenset(), frozenset([]), frozenset(()), frozenset(''), |
| 577 | frozenset(), frozenset([]), frozenset(()), frozenset(''), |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 578 | frozenset(range(0)), frozenset(frozenset()), |
Raymond Hettinger | d794666 | 2005-08-01 21:39:29 +0000 | [diff] [blame] | 579 | frozenset(f), f] |
| 580 | # All of the empty frozensets should have just one id() |
| 581 | self.assertEqual(len(set(map(id, efs))), 1) |
| 582 | |
Raymond Hettinger | 49ba4c3 | 2003-11-23 02:49:05 +0000 | [diff] [blame] | 583 | def test_constructor_identity(self): |
| 584 | s = self.thetype(range(3)) |
| 585 | t = self.thetype(s) |
| 586 | self.assertEqual(id(s), id(t)) |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 587 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 588 | def test_hash(self): |
Raymond Hettinger | 49ba4c3 | 2003-11-23 02:49:05 +0000 | [diff] [blame] | 589 | self.assertEqual(hash(self.thetype('abcdeb')), |
| 590 | hash(self.thetype('ebecda'))) |
| 591 | |
Raymond Hettinger | 82cb9a2 | 2005-07-05 05:34:43 +0000 | [diff] [blame] | 592 | # make sure that all permutations give the same hash value |
| 593 | n = 100 |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 594 | seq = [randrange(n) for i in range(n)] |
Raymond Hettinger | 82cb9a2 | 2005-07-05 05:34:43 +0000 | [diff] [blame] | 595 | results = set() |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 596 | for i in range(200): |
Raymond Hettinger | 82cb9a2 | 2005-07-05 05:34:43 +0000 | [diff] [blame] | 597 | shuffle(seq) |
| 598 | results.add(hash(self.thetype(seq))) |
| 599 | self.assertEqual(len(results), 1) |
| 600 | |
Raymond Hettinger | 49ba4c3 | 2003-11-23 02:49:05 +0000 | [diff] [blame] | 601 | def test_copy(self): |
| 602 | dup = self.s.copy() |
| 603 | self.assertEqual(id(self.s), id(dup)) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 604 | |
| 605 | def test_frozen_as_dictkey(self): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 606 | seq = list(range(10)) + list('abcdefg') + ['apple'] |
Raymond Hettinger | 49ba4c3 | 2003-11-23 02:49:05 +0000 | [diff] [blame] | 607 | key1 = self.thetype(seq) |
| 608 | key2 = self.thetype(reversed(seq)) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 609 | self.assertEqual(key1, key2) |
| 610 | self.assertNotEqual(id(key1), id(key2)) |
| 611 | d = {} |
| 612 | d[key1] = 42 |
| 613 | self.assertEqual(d[key2], 42) |
| 614 | |
| 615 | def test_hash_caching(self): |
Raymond Hettinger | 49ba4c3 | 2003-11-23 02:49:05 +0000 | [diff] [blame] | 616 | f = self.thetype('abcdcda') |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 617 | self.assertEqual(hash(f), hash(f)) |
| 618 | |
Raymond Hettinger | 49ba4c3 | 2003-11-23 02:49:05 +0000 | [diff] [blame] | 619 | def test_hash_effectiveness(self): |
| 620 | n = 13 |
Raymond Hettinger | 49ba4c3 | 2003-11-23 02:49:05 +0000 | [diff] [blame] | 621 | hashvalues = set() |
Raymond Hettinger | 6e70acc | 2003-12-31 02:01:33 +0000 | [diff] [blame] | 622 | addhashvalue = hashvalues.add |
| 623 | elemmasks = [(i+1, 1<<i) for i in range(n)] |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 624 | for i in range(2**n): |
Raymond Hettinger | 6e70acc | 2003-12-31 02:01:33 +0000 | [diff] [blame] | 625 | addhashvalue(hash(frozenset([e for e, m in elemmasks if m&i]))) |
| 626 | self.assertEqual(len(hashvalues), 2**n) |
Raymond Hettinger | 49ba4c3 | 2003-11-23 02:49:05 +0000 | [diff] [blame] | 627 | |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 628 | class FrozenSetSubclass(frozenset): |
| 629 | pass |
| 630 | |
| 631 | class TestFrozenSetSubclass(TestFrozenSet): |
| 632 | thetype = FrozenSetSubclass |
| 633 | |
Raymond Hettinger | 49ba4c3 | 2003-11-23 02:49:05 +0000 | [diff] [blame] | 634 | def test_constructor_identity(self): |
| 635 | s = self.thetype(range(3)) |
| 636 | t = self.thetype(s) |
| 637 | self.assertNotEqual(id(s), id(t)) |
| 638 | |
| 639 | def test_copy(self): |
| 640 | dup = self.s.copy() |
| 641 | self.assertNotEqual(id(self.s), id(dup)) |
| 642 | |
| 643 | def test_nested_empty_constructor(self): |
| 644 | s = self.thetype() |
| 645 | t = self.thetype(s) |
| 646 | self.assertEqual(s, t) |
| 647 | |
Raymond Hettinger | d794666 | 2005-08-01 21:39:29 +0000 | [diff] [blame] | 648 | def test_singleton_empty_frozenset(self): |
| 649 | Frozenset = self.thetype |
| 650 | f = frozenset() |
| 651 | F = Frozenset() |
| 652 | efs = [Frozenset(), Frozenset([]), Frozenset(()), Frozenset(''), |
| 653 | Frozenset(), Frozenset([]), Frozenset(()), Frozenset(''), |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 654 | Frozenset(range(0)), Frozenset(Frozenset()), |
Raymond Hettinger | d794666 | 2005-08-01 21:39:29 +0000 | [diff] [blame] | 655 | Frozenset(frozenset()), f, F, Frozenset(f), Frozenset(F)] |
| 656 | # All empty frozenset subclass instances should have different ids |
| 657 | self.assertEqual(len(set(map(id, efs))), len(efs)) |
| 658 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 659 | # Tests taken from test_sets.py ============================================= |
| 660 | |
| 661 | empty_set = set() |
| 662 | |
| 663 | #============================================================================== |
| 664 | |
| 665 | class TestBasicOps(unittest.TestCase): |
| 666 | |
| 667 | def test_repr(self): |
| 668 | if self.repr is not None: |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 669 | self.assertEqual(repr(self.set), self.repr) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 670 | |
Raymond Hettinger | eae05de | 2004-07-09 04:51:24 +0000 | [diff] [blame] | 671 | def test_print(self): |
| 672 | try: |
Guido van Rossum | 292aa0d | 2007-05-24 18:00:35 +0000 | [diff] [blame] | 673 | fo = open(test_support.TESTFN, "w") |
Guido van Rossum | d8c1967 | 2007-02-09 21:54:58 +0000 | [diff] [blame] | 674 | fo.write(str(self.set)) |
Raymond Hettinger | eae05de | 2004-07-09 04:51:24 +0000 | [diff] [blame] | 675 | fo.close() |
Guido van Rossum | 292aa0d | 2007-05-24 18:00:35 +0000 | [diff] [blame] | 676 | fo = open(test_support.TESTFN, "r") |
Raymond Hettinger | eae05de | 2004-07-09 04:51:24 +0000 | [diff] [blame] | 677 | self.assertEqual(fo.read(), repr(self.set)) |
| 678 | finally: |
| 679 | fo.close() |
| 680 | os.remove(test_support.TESTFN) |
| 681 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 682 | def test_length(self): |
| 683 | self.assertEqual(len(self.set), self.length) |
| 684 | |
| 685 | def test_self_equality(self): |
| 686 | self.assertEqual(self.set, self.set) |
| 687 | |
| 688 | def test_equivalent_equality(self): |
| 689 | self.assertEqual(self.set, self.dup) |
| 690 | |
| 691 | def test_copy(self): |
| 692 | self.assertEqual(self.set.copy(), self.dup) |
| 693 | |
| 694 | def test_self_union(self): |
| 695 | result = self.set | self.set |
| 696 | self.assertEqual(result, self.dup) |
| 697 | |
| 698 | def test_empty_union(self): |
| 699 | result = self.set | empty_set |
| 700 | self.assertEqual(result, self.dup) |
| 701 | |
| 702 | def test_union_empty(self): |
| 703 | result = empty_set | self.set |
| 704 | self.assertEqual(result, self.dup) |
| 705 | |
| 706 | def test_self_intersection(self): |
| 707 | result = self.set & self.set |
| 708 | self.assertEqual(result, self.dup) |
| 709 | |
| 710 | def test_empty_intersection(self): |
| 711 | result = self.set & empty_set |
| 712 | self.assertEqual(result, empty_set) |
| 713 | |
| 714 | def test_intersection_empty(self): |
| 715 | result = empty_set & self.set |
| 716 | self.assertEqual(result, empty_set) |
| 717 | |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame^] | 718 | def test_self_isdisjoint(self): |
| 719 | result = self.set.isdisjoint(self.set) |
| 720 | self.assertEqual(result, not self.set) |
| 721 | |
| 722 | def test_empty_isdisjoint(self): |
| 723 | result = self.set.isdisjoint(empty_set) |
| 724 | self.assertEqual(result, True) |
| 725 | |
| 726 | def test_isdisjoint_empty(self): |
| 727 | result = empty_set.isdisjoint(self.set) |
| 728 | self.assertEqual(result, True) |
| 729 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 730 | def test_self_symmetric_difference(self): |
| 731 | result = self.set ^ self.set |
| 732 | self.assertEqual(result, empty_set) |
| 733 | |
| 734 | def checkempty_symmetric_difference(self): |
| 735 | result = self.set ^ empty_set |
| 736 | self.assertEqual(result, self.set) |
| 737 | |
| 738 | def test_self_difference(self): |
| 739 | result = self.set - self.set |
| 740 | self.assertEqual(result, empty_set) |
| 741 | |
| 742 | def test_empty_difference(self): |
| 743 | result = self.set - empty_set |
| 744 | self.assertEqual(result, self.dup) |
| 745 | |
| 746 | def test_empty_difference_rev(self): |
| 747 | result = empty_set - self.set |
| 748 | self.assertEqual(result, empty_set) |
| 749 | |
| 750 | def test_iteration(self): |
| 751 | for v in self.set: |
| 752 | self.assert_(v in self.values) |
Neal Norwitz | fcf4435 | 2005-11-27 20:37:43 +0000 | [diff] [blame] | 753 | setiter = iter(self.set) |
Armin Rigo | f5b3e36 | 2006-02-11 21:32:43 +0000 | [diff] [blame] | 754 | # note: __length_hint__ is an internal undocumented API, |
| 755 | # don't rely on it in your own programs |
| 756 | self.assertEqual(setiter.__length_hint__(), len(self.set)) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 757 | |
| 758 | def test_pickling(self): |
| 759 | p = pickle.dumps(self.set) |
| 760 | copy = pickle.loads(p) |
| 761 | self.assertEqual(self.set, copy, |
| 762 | "%s != %s" % (self.set, copy)) |
| 763 | |
| 764 | #------------------------------------------------------------------------------ |
| 765 | |
| 766 | class TestBasicOpsEmpty(TestBasicOps): |
| 767 | def setUp(self): |
| 768 | self.case = "empty set" |
| 769 | self.values = [] |
| 770 | self.set = set(self.values) |
| 771 | self.dup = set(self.values) |
| 772 | self.length = 0 |
Georg Brandl | c4996ba | 2006-08-28 19:37:11 +0000 | [diff] [blame] | 773 | self.repr = "set()" |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 774 | |
| 775 | #------------------------------------------------------------------------------ |
| 776 | |
| 777 | class TestBasicOpsSingleton(TestBasicOps): |
| 778 | def setUp(self): |
| 779 | self.case = "unit set (number)" |
| 780 | self.values = [3] |
| 781 | self.set = set(self.values) |
| 782 | self.dup = set(self.values) |
| 783 | self.length = 1 |
Guido van Rossum | 86e58e2 | 2006-08-28 15:27:34 +0000 | [diff] [blame] | 784 | self.repr = "{3}" |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 785 | |
| 786 | def test_in(self): |
| 787 | self.failUnless(3 in self.set) |
| 788 | |
| 789 | def test_not_in(self): |
| 790 | self.failUnless(2 not in self.set) |
| 791 | |
| 792 | #------------------------------------------------------------------------------ |
| 793 | |
| 794 | class TestBasicOpsTuple(TestBasicOps): |
| 795 | def setUp(self): |
| 796 | self.case = "unit set (tuple)" |
| 797 | self.values = [(0, "zero")] |
| 798 | self.set = set(self.values) |
| 799 | self.dup = set(self.values) |
| 800 | self.length = 1 |
Guido van Rossum | 86e58e2 | 2006-08-28 15:27:34 +0000 | [diff] [blame] | 801 | self.repr = "{(0, 'zero')}" |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 802 | |
| 803 | def test_in(self): |
| 804 | self.failUnless((0, "zero") in self.set) |
| 805 | |
| 806 | def test_not_in(self): |
| 807 | self.failUnless(9 not in self.set) |
| 808 | |
| 809 | #------------------------------------------------------------------------------ |
| 810 | |
| 811 | class TestBasicOpsTriple(TestBasicOps): |
| 812 | def setUp(self): |
| 813 | self.case = "triple set" |
| 814 | self.values = [0, "zero", operator.add] |
| 815 | self.set = set(self.values) |
| 816 | self.dup = set(self.values) |
| 817 | self.length = 3 |
| 818 | self.repr = None |
| 819 | |
| 820 | #============================================================================== |
| 821 | |
| 822 | def baditer(): |
| 823 | raise TypeError |
| 824 | yield True |
| 825 | |
| 826 | def gooditer(): |
| 827 | yield True |
| 828 | |
| 829 | class TestExceptionPropagation(unittest.TestCase): |
| 830 | """SF 628246: Set constructor should not trap iterator TypeErrors""" |
| 831 | |
| 832 | def test_instanceWithException(self): |
| 833 | self.assertRaises(TypeError, set, baditer()) |
| 834 | |
| 835 | def test_instancesWithoutException(self): |
| 836 | # All of these iterables should load without exception. |
| 837 | set([1,2,3]) |
| 838 | set((1,2,3)) |
| 839 | set({'one':1, 'two':2, 'three':3}) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 840 | set(range(3)) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 841 | set('abc') |
| 842 | set(gooditer()) |
| 843 | |
Neal Norwitz | fcf4435 | 2005-11-27 20:37:43 +0000 | [diff] [blame] | 844 | def test_changingSizeWhileIterating(self): |
| 845 | s = set([1,2,3]) |
| 846 | try: |
| 847 | for i in s: |
| 848 | s.update([4]) |
| 849 | except RuntimeError: |
| 850 | pass |
| 851 | else: |
| 852 | self.fail("no exception when changing size during iteration") |
| 853 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 854 | #============================================================================== |
| 855 | |
| 856 | class TestSetOfSets(unittest.TestCase): |
| 857 | def test_constructor(self): |
| 858 | inner = frozenset([1]) |
| 859 | outer = set([inner]) |
| 860 | element = outer.pop() |
| 861 | self.assertEqual(type(element), frozenset) |
| 862 | outer.add(inner) # Rebuild set of sets with .add method |
| 863 | outer.remove(inner) |
| 864 | self.assertEqual(outer, set()) # Verify that remove worked |
| 865 | outer.discard(inner) # Absence of KeyError indicates working fine |
| 866 | |
| 867 | #============================================================================== |
| 868 | |
| 869 | class TestBinaryOps(unittest.TestCase): |
| 870 | def setUp(self): |
| 871 | self.set = set((2, 4, 6)) |
| 872 | |
| 873 | def test_eq(self): # SF bug 643115 |
| 874 | self.assertEqual(self.set, set({2:1,4:3,6:5})) |
| 875 | |
| 876 | def test_union_subset(self): |
| 877 | result = self.set | set([2]) |
| 878 | self.assertEqual(result, set((2, 4, 6))) |
| 879 | |
| 880 | def test_union_superset(self): |
| 881 | result = self.set | set([2, 4, 6, 8]) |
| 882 | self.assertEqual(result, set([2, 4, 6, 8])) |
| 883 | |
| 884 | def test_union_overlap(self): |
| 885 | result = self.set | set([3, 4, 5]) |
| 886 | self.assertEqual(result, set([2, 3, 4, 5, 6])) |
| 887 | |
| 888 | def test_union_non_overlap(self): |
| 889 | result = self.set | set([8]) |
| 890 | self.assertEqual(result, set([2, 4, 6, 8])) |
| 891 | |
| 892 | def test_intersection_subset(self): |
| 893 | result = self.set & set((2, 4)) |
| 894 | self.assertEqual(result, set((2, 4))) |
| 895 | |
| 896 | def test_intersection_superset(self): |
| 897 | result = self.set & set([2, 4, 6, 8]) |
| 898 | self.assertEqual(result, set([2, 4, 6])) |
| 899 | |
| 900 | def test_intersection_overlap(self): |
| 901 | result = self.set & set([3, 4, 5]) |
| 902 | self.assertEqual(result, set([4])) |
| 903 | |
| 904 | def test_intersection_non_overlap(self): |
| 905 | result = self.set & set([8]) |
| 906 | self.assertEqual(result, empty_set) |
| 907 | |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame^] | 908 | def test_isdisjoint_subset(self): |
| 909 | result = self.set.isdisjoint(set((2, 4))) |
| 910 | self.assertEqual(result, False) |
| 911 | |
| 912 | def test_isdisjoint_superset(self): |
| 913 | result = self.set.isdisjoint(set([2, 4, 6, 8])) |
| 914 | self.assertEqual(result, False) |
| 915 | |
| 916 | def test_isdisjoint_overlap(self): |
| 917 | result = self.set.isdisjoint(set([3, 4, 5])) |
| 918 | self.assertEqual(result, False) |
| 919 | |
| 920 | def test_isdisjoint_non_overlap(self): |
| 921 | result = self.set.isdisjoint(set([8])) |
| 922 | self.assertEqual(result, True) |
| 923 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 924 | def test_sym_difference_subset(self): |
| 925 | result = self.set ^ set((2, 4)) |
| 926 | self.assertEqual(result, set([6])) |
| 927 | |
| 928 | def test_sym_difference_superset(self): |
| 929 | result = self.set ^ set((2, 4, 6, 8)) |
| 930 | self.assertEqual(result, set([8])) |
| 931 | |
| 932 | def test_sym_difference_overlap(self): |
| 933 | result = self.set ^ set((3, 4, 5)) |
| 934 | self.assertEqual(result, set([2, 3, 5, 6])) |
| 935 | |
| 936 | def test_sym_difference_non_overlap(self): |
| 937 | result = self.set ^ set([8]) |
| 938 | self.assertEqual(result, set([2, 4, 6, 8])) |
| 939 | |
| 940 | def test_cmp(self): |
| 941 | a, b = set('a'), set('b') |
| 942 | self.assertRaises(TypeError, cmp, a, b) |
| 943 | |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 944 | # In py3k, this works! |
| 945 | self.assertRaises(TypeError, cmp, a, a) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 946 | |
| 947 | self.assertRaises(TypeError, cmp, a, 12) |
| 948 | self.assertRaises(TypeError, cmp, "abc", a) |
| 949 | |
| 950 | #============================================================================== |
| 951 | |
| 952 | class TestUpdateOps(unittest.TestCase): |
| 953 | def setUp(self): |
| 954 | self.set = set((2, 4, 6)) |
| 955 | |
| 956 | def test_union_subset(self): |
| 957 | self.set |= set([2]) |
| 958 | self.assertEqual(self.set, set((2, 4, 6))) |
| 959 | |
| 960 | def test_union_superset(self): |
| 961 | self.set |= set([2, 4, 6, 8]) |
| 962 | self.assertEqual(self.set, set([2, 4, 6, 8])) |
| 963 | |
| 964 | def test_union_overlap(self): |
| 965 | self.set |= set([3, 4, 5]) |
| 966 | self.assertEqual(self.set, set([2, 3, 4, 5, 6])) |
| 967 | |
| 968 | def test_union_non_overlap(self): |
| 969 | self.set |= set([8]) |
| 970 | self.assertEqual(self.set, set([2, 4, 6, 8])) |
| 971 | |
| 972 | def test_union_method_call(self): |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 973 | self.set.update(set([3, 4, 5])) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 974 | self.assertEqual(self.set, set([2, 3, 4, 5, 6])) |
| 975 | |
| 976 | def test_intersection_subset(self): |
| 977 | self.set &= set((2, 4)) |
| 978 | self.assertEqual(self.set, set((2, 4))) |
| 979 | |
| 980 | def test_intersection_superset(self): |
| 981 | self.set &= set([2, 4, 6, 8]) |
| 982 | self.assertEqual(self.set, set([2, 4, 6])) |
| 983 | |
| 984 | def test_intersection_overlap(self): |
| 985 | self.set &= set([3, 4, 5]) |
| 986 | self.assertEqual(self.set, set([4])) |
| 987 | |
| 988 | def test_intersection_non_overlap(self): |
| 989 | self.set &= set([8]) |
| 990 | self.assertEqual(self.set, empty_set) |
| 991 | |
| 992 | def test_intersection_method_call(self): |
| 993 | self.set.intersection_update(set([3, 4, 5])) |
| 994 | self.assertEqual(self.set, set([4])) |
| 995 | |
| 996 | def test_sym_difference_subset(self): |
| 997 | self.set ^= set((2, 4)) |
| 998 | self.assertEqual(self.set, set([6])) |
| 999 | |
| 1000 | def test_sym_difference_superset(self): |
| 1001 | self.set ^= set((2, 4, 6, 8)) |
| 1002 | self.assertEqual(self.set, set([8])) |
| 1003 | |
| 1004 | def test_sym_difference_overlap(self): |
| 1005 | self.set ^= set((3, 4, 5)) |
| 1006 | self.assertEqual(self.set, set([2, 3, 5, 6])) |
| 1007 | |
| 1008 | def test_sym_difference_non_overlap(self): |
| 1009 | self.set ^= set([8]) |
| 1010 | self.assertEqual(self.set, set([2, 4, 6, 8])) |
| 1011 | |
| 1012 | def test_sym_difference_method_call(self): |
| 1013 | self.set.symmetric_difference_update(set([3, 4, 5])) |
| 1014 | self.assertEqual(self.set, set([2, 3, 5, 6])) |
| 1015 | |
| 1016 | def test_difference_subset(self): |
| 1017 | self.set -= set((2, 4)) |
| 1018 | self.assertEqual(self.set, set([6])) |
| 1019 | |
| 1020 | def test_difference_superset(self): |
| 1021 | self.set -= set((2, 4, 6, 8)) |
| 1022 | self.assertEqual(self.set, set([])) |
| 1023 | |
| 1024 | def test_difference_overlap(self): |
| 1025 | self.set -= set((3, 4, 5)) |
| 1026 | self.assertEqual(self.set, set([2, 6])) |
| 1027 | |
| 1028 | def test_difference_non_overlap(self): |
| 1029 | self.set -= set([8]) |
| 1030 | self.assertEqual(self.set, set([2, 4, 6])) |
| 1031 | |
| 1032 | def test_difference_method_call(self): |
| 1033 | self.set.difference_update(set([3, 4, 5])) |
| 1034 | self.assertEqual(self.set, set([2, 6])) |
| 1035 | |
| 1036 | #============================================================================== |
| 1037 | |
| 1038 | class TestMutate(unittest.TestCase): |
| 1039 | def setUp(self): |
| 1040 | self.values = ["a", "b", "c"] |
| 1041 | self.set = set(self.values) |
| 1042 | |
| 1043 | def test_add_present(self): |
| 1044 | self.set.add("c") |
| 1045 | self.assertEqual(self.set, set("abc")) |
| 1046 | |
| 1047 | def test_add_absent(self): |
| 1048 | self.set.add("d") |
| 1049 | self.assertEqual(self.set, set("abcd")) |
| 1050 | |
| 1051 | def test_add_until_full(self): |
| 1052 | tmp = set() |
| 1053 | expected_len = 0 |
| 1054 | for v in self.values: |
| 1055 | tmp.add(v) |
| 1056 | expected_len += 1 |
| 1057 | self.assertEqual(len(tmp), expected_len) |
| 1058 | self.assertEqual(tmp, self.set) |
| 1059 | |
| 1060 | def test_remove_present(self): |
| 1061 | self.set.remove("b") |
| 1062 | self.assertEqual(self.set, set("ac")) |
| 1063 | |
| 1064 | def test_remove_absent(self): |
| 1065 | try: |
| 1066 | self.set.remove("d") |
| 1067 | self.fail("Removing missing element should have raised LookupError") |
| 1068 | except LookupError: |
| 1069 | pass |
| 1070 | |
| 1071 | def test_remove_until_empty(self): |
| 1072 | expected_len = len(self.set) |
| 1073 | for v in self.values: |
| 1074 | self.set.remove(v) |
| 1075 | expected_len -= 1 |
| 1076 | self.assertEqual(len(self.set), expected_len) |
| 1077 | |
| 1078 | def test_discard_present(self): |
| 1079 | self.set.discard("c") |
| 1080 | self.assertEqual(self.set, set("ab")) |
| 1081 | |
| 1082 | def test_discard_absent(self): |
| 1083 | self.set.discard("d") |
| 1084 | self.assertEqual(self.set, set("abc")) |
| 1085 | |
| 1086 | def test_clear(self): |
| 1087 | self.set.clear() |
| 1088 | self.assertEqual(len(self.set), 0) |
| 1089 | |
| 1090 | def test_pop(self): |
| 1091 | popped = {} |
| 1092 | while self.set: |
| 1093 | popped[self.set.pop()] = None |
| 1094 | self.assertEqual(len(popped), len(self.values)) |
| 1095 | for v in self.values: |
| 1096 | self.failUnless(v in popped) |
| 1097 | |
| 1098 | def test_update_empty_tuple(self): |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 1099 | self.set.update(()) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1100 | self.assertEqual(self.set, set(self.values)) |
| 1101 | |
| 1102 | def test_update_unit_tuple_overlap(self): |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 1103 | self.set.update(("a",)) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1104 | self.assertEqual(self.set, set(self.values)) |
| 1105 | |
| 1106 | def test_update_unit_tuple_non_overlap(self): |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 1107 | self.set.update(("a", "z")) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1108 | self.assertEqual(self.set, set(self.values + ["z"])) |
| 1109 | |
| 1110 | #============================================================================== |
| 1111 | |
| 1112 | class TestSubsets(unittest.TestCase): |
| 1113 | |
| 1114 | case2method = {"<=": "issubset", |
| 1115 | ">=": "issuperset", |
| 1116 | } |
| 1117 | |
| 1118 | reverse = {"==": "==", |
| 1119 | "!=": "!=", |
| 1120 | "<": ">", |
| 1121 | ">": "<", |
| 1122 | "<=": ">=", |
| 1123 | ">=": "<=", |
| 1124 | } |
| 1125 | |
| 1126 | def test_issubset(self): |
| 1127 | x = self.left |
| 1128 | y = self.right |
| 1129 | for case in "!=", "==", "<", "<=", ">", ">=": |
| 1130 | expected = case in self.cases |
| 1131 | # Test the binary infix spelling. |
| 1132 | result = eval("x" + case + "y", locals()) |
| 1133 | self.assertEqual(result, expected) |
| 1134 | # Test the "friendly" method-name spelling, if one exists. |
| 1135 | if case in TestSubsets.case2method: |
| 1136 | method = getattr(x, TestSubsets.case2method[case]) |
| 1137 | result = method(y) |
| 1138 | self.assertEqual(result, expected) |
| 1139 | |
| 1140 | # Now do the same for the operands reversed. |
| 1141 | rcase = TestSubsets.reverse[case] |
| 1142 | result = eval("y" + rcase + "x", locals()) |
| 1143 | self.assertEqual(result, expected) |
| 1144 | if rcase in TestSubsets.case2method: |
| 1145 | method = getattr(y, TestSubsets.case2method[rcase]) |
| 1146 | result = method(x) |
| 1147 | self.assertEqual(result, expected) |
| 1148 | #------------------------------------------------------------------------------ |
| 1149 | |
| 1150 | class TestSubsetEqualEmpty(TestSubsets): |
| 1151 | left = set() |
| 1152 | right = set() |
| 1153 | name = "both empty" |
| 1154 | cases = "==", "<=", ">=" |
| 1155 | |
| 1156 | #------------------------------------------------------------------------------ |
| 1157 | |
| 1158 | class TestSubsetEqualNonEmpty(TestSubsets): |
| 1159 | left = set([1, 2]) |
| 1160 | right = set([1, 2]) |
| 1161 | name = "equal pair" |
| 1162 | cases = "==", "<=", ">=" |
| 1163 | |
| 1164 | #------------------------------------------------------------------------------ |
| 1165 | |
| 1166 | class TestSubsetEmptyNonEmpty(TestSubsets): |
| 1167 | left = set() |
| 1168 | right = set([1, 2]) |
| 1169 | name = "one empty, one non-empty" |
| 1170 | cases = "!=", "<", "<=" |
| 1171 | |
| 1172 | #------------------------------------------------------------------------------ |
| 1173 | |
| 1174 | class TestSubsetPartial(TestSubsets): |
| 1175 | left = set([1]) |
| 1176 | right = set([1, 2]) |
| 1177 | name = "one a non-empty proper subset of other" |
| 1178 | cases = "!=", "<", "<=" |
| 1179 | |
| 1180 | #------------------------------------------------------------------------------ |
| 1181 | |
| 1182 | class TestSubsetNonOverlap(TestSubsets): |
| 1183 | left = set([1]) |
| 1184 | right = set([2]) |
| 1185 | name = "neither empty, neither contains" |
| 1186 | cases = "!=" |
| 1187 | |
| 1188 | #============================================================================== |
| 1189 | |
| 1190 | class TestOnlySetsInBinaryOps(unittest.TestCase): |
| 1191 | |
| 1192 | def test_eq_ne(self): |
| 1193 | # Unlike the others, this is testing that == and != *are* allowed. |
| 1194 | self.assertEqual(self.other == self.set, False) |
| 1195 | self.assertEqual(self.set == self.other, False) |
| 1196 | self.assertEqual(self.other != self.set, True) |
| 1197 | self.assertEqual(self.set != self.other, True) |
| 1198 | |
| 1199 | def test_ge_gt_le_lt(self): |
| 1200 | self.assertRaises(TypeError, lambda: self.set < self.other) |
| 1201 | self.assertRaises(TypeError, lambda: self.set <= self.other) |
| 1202 | self.assertRaises(TypeError, lambda: self.set > self.other) |
| 1203 | self.assertRaises(TypeError, lambda: self.set >= self.other) |
| 1204 | |
| 1205 | self.assertRaises(TypeError, lambda: self.other < self.set) |
| 1206 | self.assertRaises(TypeError, lambda: self.other <= self.set) |
| 1207 | self.assertRaises(TypeError, lambda: self.other > self.set) |
| 1208 | self.assertRaises(TypeError, lambda: self.other >= self.set) |
| 1209 | |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 1210 | def test_update_operator(self): |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1211 | try: |
| 1212 | self.set |= self.other |
| 1213 | except TypeError: |
| 1214 | pass |
| 1215 | else: |
| 1216 | self.fail("expected TypeError") |
| 1217 | |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 1218 | def test_update(self): |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1219 | if self.otherIsIterable: |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 1220 | self.set.update(self.other) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1221 | else: |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 1222 | self.assertRaises(TypeError, self.set.update, self.other) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1223 | |
| 1224 | def test_union(self): |
| 1225 | self.assertRaises(TypeError, lambda: self.set | self.other) |
| 1226 | self.assertRaises(TypeError, lambda: self.other | self.set) |
| 1227 | if self.otherIsIterable: |
| 1228 | self.set.union(self.other) |
| 1229 | else: |
| 1230 | self.assertRaises(TypeError, self.set.union, self.other) |
| 1231 | |
| 1232 | def test_intersection_update_operator(self): |
| 1233 | try: |
| 1234 | self.set &= self.other |
| 1235 | except TypeError: |
| 1236 | pass |
| 1237 | else: |
| 1238 | self.fail("expected TypeError") |
| 1239 | |
| 1240 | def test_intersection_update(self): |
| 1241 | if self.otherIsIterable: |
| 1242 | self.set.intersection_update(self.other) |
| 1243 | else: |
| 1244 | self.assertRaises(TypeError, |
| 1245 | self.set.intersection_update, |
| 1246 | self.other) |
| 1247 | |
| 1248 | def test_intersection(self): |
| 1249 | self.assertRaises(TypeError, lambda: self.set & self.other) |
| 1250 | self.assertRaises(TypeError, lambda: self.other & self.set) |
| 1251 | if self.otherIsIterable: |
| 1252 | self.set.intersection(self.other) |
| 1253 | else: |
| 1254 | self.assertRaises(TypeError, self.set.intersection, self.other) |
| 1255 | |
| 1256 | def test_sym_difference_update_operator(self): |
| 1257 | try: |
| 1258 | self.set ^= self.other |
| 1259 | except TypeError: |
| 1260 | pass |
| 1261 | else: |
| 1262 | self.fail("expected TypeError") |
| 1263 | |
| 1264 | def test_sym_difference_update(self): |
| 1265 | if self.otherIsIterable: |
| 1266 | self.set.symmetric_difference_update(self.other) |
| 1267 | else: |
| 1268 | self.assertRaises(TypeError, |
| 1269 | self.set.symmetric_difference_update, |
| 1270 | self.other) |
| 1271 | |
| 1272 | def test_sym_difference(self): |
| 1273 | self.assertRaises(TypeError, lambda: self.set ^ self.other) |
| 1274 | self.assertRaises(TypeError, lambda: self.other ^ self.set) |
| 1275 | if self.otherIsIterable: |
| 1276 | self.set.symmetric_difference(self.other) |
| 1277 | else: |
| 1278 | self.assertRaises(TypeError, self.set.symmetric_difference, self.other) |
| 1279 | |
| 1280 | def test_difference_update_operator(self): |
| 1281 | try: |
| 1282 | self.set -= self.other |
| 1283 | except TypeError: |
| 1284 | pass |
| 1285 | else: |
| 1286 | self.fail("expected TypeError") |
| 1287 | |
| 1288 | def test_difference_update(self): |
| 1289 | if self.otherIsIterable: |
| 1290 | self.set.difference_update(self.other) |
| 1291 | else: |
| 1292 | self.assertRaises(TypeError, |
| 1293 | self.set.difference_update, |
| 1294 | self.other) |
| 1295 | |
| 1296 | def test_difference(self): |
| 1297 | self.assertRaises(TypeError, lambda: self.set - self.other) |
| 1298 | self.assertRaises(TypeError, lambda: self.other - self.set) |
| 1299 | if self.otherIsIterable: |
| 1300 | self.set.difference(self.other) |
| 1301 | else: |
| 1302 | self.assertRaises(TypeError, self.set.difference, self.other) |
| 1303 | |
| 1304 | #------------------------------------------------------------------------------ |
| 1305 | |
| 1306 | class TestOnlySetsNumeric(TestOnlySetsInBinaryOps): |
| 1307 | def setUp(self): |
| 1308 | self.set = set((1, 2, 3)) |
| 1309 | self.other = 19 |
| 1310 | self.otherIsIterable = False |
| 1311 | |
| 1312 | #------------------------------------------------------------------------------ |
| 1313 | |
| 1314 | class TestOnlySetsDict(TestOnlySetsInBinaryOps): |
| 1315 | def setUp(self): |
| 1316 | self.set = set((1, 2, 3)) |
| 1317 | self.other = {1:2, 3:4} |
| 1318 | self.otherIsIterable = True |
| 1319 | |
| 1320 | #------------------------------------------------------------------------------ |
| 1321 | |
| 1322 | class TestOnlySetsOperator(TestOnlySetsInBinaryOps): |
| 1323 | def setUp(self): |
| 1324 | self.set = set((1, 2, 3)) |
| 1325 | self.other = operator.add |
| 1326 | self.otherIsIterable = False |
| 1327 | |
| 1328 | #------------------------------------------------------------------------------ |
| 1329 | |
| 1330 | class TestOnlySetsTuple(TestOnlySetsInBinaryOps): |
| 1331 | def setUp(self): |
| 1332 | self.set = set((1, 2, 3)) |
| 1333 | self.other = (2, 4, 6) |
| 1334 | self.otherIsIterable = True |
| 1335 | |
| 1336 | #------------------------------------------------------------------------------ |
| 1337 | |
| 1338 | class TestOnlySetsString(TestOnlySetsInBinaryOps): |
| 1339 | def setUp(self): |
| 1340 | self.set = set((1, 2, 3)) |
| 1341 | self.other = 'abc' |
| 1342 | self.otherIsIterable = True |
| 1343 | |
| 1344 | #------------------------------------------------------------------------------ |
| 1345 | |
| 1346 | class TestOnlySetsGenerator(TestOnlySetsInBinaryOps): |
| 1347 | def setUp(self): |
| 1348 | def gen(): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1349 | for i in range(0, 10, 2): |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1350 | yield i |
| 1351 | self.set = set((1, 2, 3)) |
| 1352 | self.other = gen() |
| 1353 | self.otherIsIterable = True |
| 1354 | |
| 1355 | #============================================================================== |
| 1356 | |
| 1357 | class TestCopying(unittest.TestCase): |
| 1358 | |
| 1359 | def test_copy(self): |
| 1360 | dup = self.set.copy() |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 1361 | dup_list = sorted(dup, key=repr) |
| 1362 | set_list = sorted(self.set, key=repr) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1363 | self.assertEqual(len(dup_list), len(set_list)) |
| 1364 | for i in range(len(dup_list)): |
| 1365 | self.failUnless(dup_list[i] is set_list[i]) |
| 1366 | |
| 1367 | def test_deep_copy(self): |
| 1368 | dup = copy.deepcopy(self.set) |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 1369 | ##print type(dup), repr(dup) |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 1370 | dup_list = sorted(dup, key=repr) |
| 1371 | set_list = sorted(self.set, key=repr) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1372 | self.assertEqual(len(dup_list), len(set_list)) |
| 1373 | for i in range(len(dup_list)): |
| 1374 | self.assertEqual(dup_list[i], set_list[i]) |
| 1375 | |
| 1376 | #------------------------------------------------------------------------------ |
| 1377 | |
| 1378 | class TestCopyingEmpty(TestCopying): |
| 1379 | def setUp(self): |
| 1380 | self.set = set() |
| 1381 | |
| 1382 | #------------------------------------------------------------------------------ |
| 1383 | |
| 1384 | class TestCopyingSingleton(TestCopying): |
| 1385 | def setUp(self): |
| 1386 | self.set = set(["hello"]) |
| 1387 | |
| 1388 | #------------------------------------------------------------------------------ |
| 1389 | |
| 1390 | class TestCopyingTriple(TestCopying): |
| 1391 | def setUp(self): |
| 1392 | self.set = set(["zero", 0, None]) |
| 1393 | |
| 1394 | #------------------------------------------------------------------------------ |
| 1395 | |
| 1396 | class TestCopyingTuple(TestCopying): |
| 1397 | def setUp(self): |
| 1398 | self.set = set([(1, 2)]) |
| 1399 | |
| 1400 | #------------------------------------------------------------------------------ |
| 1401 | |
| 1402 | class TestCopyingNested(TestCopying): |
| 1403 | def setUp(self): |
| 1404 | self.set = set([((1, 2), (3, 4))]) |
| 1405 | |
| 1406 | #============================================================================== |
| 1407 | |
| 1408 | class TestIdentities(unittest.TestCase): |
| 1409 | def setUp(self): |
| 1410 | self.a = set('abracadabra') |
| 1411 | self.b = set('alacazam') |
| 1412 | |
| 1413 | def test_binopsVsSubsets(self): |
| 1414 | a, b = self.a, self.b |
| 1415 | self.assert_(a - b < a) |
| 1416 | self.assert_(b - a < b) |
| 1417 | self.assert_(a & b < a) |
| 1418 | self.assert_(a & b < b) |
| 1419 | self.assert_(a | b > a) |
| 1420 | self.assert_(a | b > b) |
| 1421 | self.assert_(a ^ b < a | b) |
| 1422 | |
| 1423 | def test_commutativity(self): |
| 1424 | a, b = self.a, self.b |
| 1425 | self.assertEqual(a&b, b&a) |
| 1426 | self.assertEqual(a|b, b|a) |
| 1427 | self.assertEqual(a^b, b^a) |
| 1428 | if a != b: |
| 1429 | self.assertNotEqual(a-b, b-a) |
| 1430 | |
| 1431 | def test_summations(self): |
| 1432 | # check that sums of parts equal the whole |
| 1433 | a, b = self.a, self.b |
| 1434 | self.assertEqual((a-b)|(a&b)|(b-a), a|b) |
| 1435 | self.assertEqual((a&b)|(a^b), a|b) |
| 1436 | self.assertEqual(a|(b-a), a|b) |
| 1437 | self.assertEqual((a-b)|b, a|b) |
| 1438 | self.assertEqual((a-b)|(a&b), a) |
| 1439 | self.assertEqual((b-a)|(a&b), b) |
| 1440 | self.assertEqual((a-b)|(b-a), a^b) |
| 1441 | |
| 1442 | def test_exclusion(self): |
| 1443 | # check that inverse operations show non-overlap |
| 1444 | a, b, zero = self.a, self.b, set() |
| 1445 | self.assertEqual((a-b)&b, zero) |
| 1446 | self.assertEqual((b-a)&a, zero) |
| 1447 | self.assertEqual((a&b)&(a^b), zero) |
| 1448 | |
| 1449 | # Tests derived from test_itertools.py ======================================= |
| 1450 | |
| 1451 | def R(seqn): |
| 1452 | 'Regular generator' |
| 1453 | for i in seqn: |
| 1454 | yield i |
| 1455 | |
| 1456 | class G: |
| 1457 | 'Sequence using __getitem__' |
| 1458 | def __init__(self, seqn): |
| 1459 | self.seqn = seqn |
| 1460 | def __getitem__(self, i): |
| 1461 | return self.seqn[i] |
| 1462 | |
| 1463 | class I: |
| 1464 | 'Sequence using iterator protocol' |
| 1465 | def __init__(self, seqn): |
| 1466 | self.seqn = seqn |
| 1467 | self.i = 0 |
| 1468 | def __iter__(self): |
| 1469 | return self |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 1470 | def __next__(self): |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1471 | if self.i >= len(self.seqn): raise StopIteration |
| 1472 | v = self.seqn[self.i] |
| 1473 | self.i += 1 |
| 1474 | return v |
| 1475 | |
| 1476 | class Ig: |
| 1477 | 'Sequence using iterator protocol defined with a generator' |
| 1478 | def __init__(self, seqn): |
| 1479 | self.seqn = seqn |
| 1480 | self.i = 0 |
| 1481 | def __iter__(self): |
| 1482 | for val in self.seqn: |
| 1483 | yield val |
| 1484 | |
| 1485 | class X: |
| 1486 | 'Missing __getitem__ and __iter__' |
| 1487 | def __init__(self, seqn): |
| 1488 | self.seqn = seqn |
| 1489 | self.i = 0 |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 1490 | def __next__(self): |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1491 | if self.i >= len(self.seqn): raise StopIteration |
| 1492 | v = self.seqn[self.i] |
| 1493 | self.i += 1 |
| 1494 | return v |
| 1495 | |
| 1496 | class N: |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 1497 | 'Iterator missing __next__()' |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1498 | def __init__(self, seqn): |
| 1499 | self.seqn = seqn |
| 1500 | self.i = 0 |
| 1501 | def __iter__(self): |
| 1502 | return self |
| 1503 | |
| 1504 | class E: |
| 1505 | 'Test propagation of exceptions' |
| 1506 | def __init__(self, seqn): |
| 1507 | self.seqn = seqn |
| 1508 | self.i = 0 |
| 1509 | def __iter__(self): |
| 1510 | return self |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 1511 | def __next__(self): |
Raymond Hettinger | ffdb8bb | 2004-09-27 15:29:05 +0000 | [diff] [blame] | 1512 | 3 // 0 |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1513 | |
| 1514 | class S: |
| 1515 | 'Test immediate stop' |
| 1516 | def __init__(self, seqn): |
| 1517 | pass |
| 1518 | def __iter__(self): |
| 1519 | return self |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 1520 | def __next__(self): |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1521 | raise StopIteration |
| 1522 | |
| 1523 | from itertools import chain, imap |
| 1524 | def L(seqn): |
| 1525 | 'Test multiple tiers of iterators' |
| 1526 | return chain(imap(lambda x:x, R(Ig(G(seqn))))) |
| 1527 | |
| 1528 | class TestVariousIteratorArgs(unittest.TestCase): |
| 1529 | |
| 1530 | def test_constructor(self): |
| 1531 | for cons in (set, frozenset): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1532 | for s in ("123", "", range(1000), ('do', 1.2), range(2000,2200,5)): |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1533 | for g in (G, I, Ig, S, L, R): |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 1534 | self.assertEqual(sorted(cons(g(s)), key=repr), sorted(g(s), key=repr)) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1535 | self.assertRaises(TypeError, cons , X(s)) |
| 1536 | self.assertRaises(TypeError, cons , N(s)) |
| 1537 | self.assertRaises(ZeroDivisionError, cons , E(s)) |
| 1538 | |
| 1539 | def test_inline_methods(self): |
| 1540 | s = set('november') |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1541 | for data in ("123", "", range(1000), ('do', 1.2), range(2000,2200,5), 'december'): |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame^] | 1542 | for meth in (s.union, s.intersection, s.difference, s.symmetric_difference, s.isdisjoint): |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1543 | for g in (G, I, Ig, L, R): |
| 1544 | expected = meth(data) |
| 1545 | actual = meth(G(data)) |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame^] | 1546 | if isinstance(expected, bool): |
| 1547 | self.assertEqual(actual, expected) |
| 1548 | else: |
| 1549 | self.assertEqual(sorted(actual, key=repr), sorted(expected, key=repr)) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1550 | self.assertRaises(TypeError, meth, X(s)) |
| 1551 | self.assertRaises(TypeError, meth, N(s)) |
| 1552 | self.assertRaises(ZeroDivisionError, meth, E(s)) |
| 1553 | |
| 1554 | def test_inplace_methods(self): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1555 | for data in ("123", "", range(1000), ('do', 1.2), range(2000,2200,5), 'december'): |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 1556 | for methname in ('update', 'intersection_update', |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1557 | 'difference_update', 'symmetric_difference_update'): |
| 1558 | for g in (G, I, Ig, S, L, R): |
| 1559 | s = set('january') |
| 1560 | t = s.copy() |
| 1561 | getattr(s, methname)(list(g(data))) |
| 1562 | getattr(t, methname)(g(data)) |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 1563 | self.assertEqual(sorted(s, key=repr), sorted(t, key=repr)) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1564 | |
| 1565 | self.assertRaises(TypeError, getattr(set('january'), methname), X(data)) |
| 1566 | self.assertRaises(TypeError, getattr(set('january'), methname), N(data)) |
| 1567 | self.assertRaises(ZeroDivisionError, getattr(set('january'), methname), E(data)) |
| 1568 | |
| 1569 | #============================================================================== |
| 1570 | |
| 1571 | def test_main(verbose=None): |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1572 | test_classes = ( |
| 1573 | TestSet, |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 1574 | TestSetSubclass, |
Thomas Wouters | 9fe394c | 2007-02-05 01:24:16 +0000 | [diff] [blame] | 1575 | TestSetSubclassWithKeywordArgs, |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1576 | TestFrozenSet, |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 1577 | TestFrozenSetSubclass, |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1578 | TestSetOfSets, |
| 1579 | TestExceptionPropagation, |
| 1580 | TestBasicOpsEmpty, |
| 1581 | TestBasicOpsSingleton, |
| 1582 | TestBasicOpsTuple, |
| 1583 | TestBasicOpsTriple, |
| 1584 | TestBinaryOps, |
| 1585 | TestUpdateOps, |
| 1586 | TestMutate, |
| 1587 | TestSubsetEqualEmpty, |
| 1588 | TestSubsetEqualNonEmpty, |
| 1589 | TestSubsetEmptyNonEmpty, |
| 1590 | TestSubsetPartial, |
| 1591 | TestSubsetNonOverlap, |
| 1592 | TestOnlySetsNumeric, |
| 1593 | TestOnlySetsDict, |
| 1594 | TestOnlySetsOperator, |
| 1595 | TestOnlySetsTuple, |
| 1596 | TestOnlySetsString, |
| 1597 | TestOnlySetsGenerator, |
| 1598 | TestCopyingEmpty, |
| 1599 | TestCopyingSingleton, |
| 1600 | TestCopyingTriple, |
| 1601 | TestCopyingTuple, |
| 1602 | TestCopyingNested, |
| 1603 | TestIdentities, |
| 1604 | TestVariousIteratorArgs, |
| 1605 | ) |
| 1606 | |
| 1607 | test_support.run_unittest(*test_classes) |
| 1608 | |
| 1609 | # verify reference counting |
| 1610 | if verbose and hasattr(sys, "gettotalrefcount"): |
| 1611 | import gc |
| 1612 | counts = [None] * 5 |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1613 | for i in range(len(counts)): |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1614 | test_support.run_unittest(*test_classes) |
| 1615 | gc.collect() |
| 1616 | counts[i] = sys.gettotalrefcount() |
Guido van Rossum | d8c1967 | 2007-02-09 21:54:58 +0000 | [diff] [blame] | 1617 | print(counts) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1618 | |
| 1619 | if __name__ == "__main__": |
| 1620 | test_main(verbose=True) |