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