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