github.com/grumpyhome/grumpy@v0.3.1-0.20201208125205-7b775405bdf1/grumpy-runtime-src/third_party/stdlib/test/test_dict.py (about) 1 import unittest 2 from test import test_support 3 4 import UserDict, random, string 5 # import gc, weakref 6 7 8 class DictTest(unittest.TestCase): 9 def test_constructor(self): 10 # calling built-in types without argument must return empty 11 self.assertEqual(dict(), {}) 12 self.assertIsNot(dict(), {}) 13 14 @unittest.expectedFailure 15 def test_literal_constructor(self): 16 # check literal constructor for different sized dicts 17 # (to exercise the BUILD_MAP oparg). 18 for n in (0, 1, 6, 256, 400): 19 items = [(''.join(random.sample(string.letters, 8)), i) 20 for i in range(n)] 21 random.shuffle(items) 22 formatted_items = ('{!r}: {:d}'.format(k, v) for k, v in items) 23 dictliteral = '{' + ', '.join(formatted_items) + '}' 24 self.assertEqual(eval(dictliteral), dict(items)) 25 26 def test_bool(self): 27 self.assertIs(not {}, True) 28 self.assertTrue({1: 2}) 29 self.assertIs(bool({}), False) 30 self.assertIs(bool({1: 2}), True) 31 32 @unittest.expectedFailure 33 def test_keys(self): 34 d = {} 35 self.assertEqual(d.keys(), []) 36 d = {'a': 1, 'b': 2} 37 k = d.keys() 38 # self.assertEqual(set(k), {'a', 'b'}) 39 self.assertIn('a', k) 40 self.assertIn('b', k) 41 self.assertTrue(d.has_key('a')) 42 self.assertTrue(d.has_key('b')) 43 self.assertRaises(TypeError, d.keys, None) 44 45 def test_values(self): 46 d = {} 47 self.assertEqual(d.values(), []) 48 d = {1:2} 49 self.assertEqual(d.values(), [2]) 50 51 self.assertRaises(TypeError, d.values, None) 52 53 def test_items(self): 54 d = {} 55 self.assertEqual(d.items(), []) 56 57 d = {1:2} 58 self.assertEqual(d.items(), [(1, 2)]) 59 60 self.assertRaises(TypeError, d.items, None) 61 62 @unittest.expectedFailure 63 def test_has_key(self): 64 d = {} 65 self.assertFalse(d.has_key('a')) 66 d = {'a': 1, 'b': 2} 67 k = d.keys() 68 k.sort() 69 self.assertEqual(k, ['a', 'b']) 70 71 self.assertRaises(TypeError, d.has_key) 72 73 def test_contains(self): 74 d = {} 75 self.assertNotIn('a', d) 76 self.assertFalse('a' in d) 77 self.assertTrue('a' not in d) 78 d = {'a': 1, 'b': 2} 79 self.assertIn('a', d) 80 self.assertIn('b', d) 81 self.assertNotIn('c', d) 82 83 self.assertRaises(TypeError, d.__contains__) 84 85 def test_len(self): 86 d = {} 87 self.assertEqual(len(d), 0) 88 d = {'a': 1, 'b': 2} 89 self.assertEqual(len(d), 2) 90 91 def test_getitem(self): 92 d = {'a': 1, 'b': 2} 93 self.assertEqual(d['a'], 1) 94 self.assertEqual(d['b'], 2) 95 d['c'] = 3 96 d['a'] = 4 97 self.assertEqual(d['c'], 3) 98 self.assertEqual(d['a'], 4) 99 del d['b'] 100 self.assertEqual(d, {'a': 4, 'c': 3}) 101 102 self.assertRaises(TypeError, d.__getitem__) 103 104 class BadEq(object): 105 def __eq__(self, other): 106 raise Exc() 107 def __hash__(self): 108 return 24 109 110 d = {} 111 d[BadEq()] = 42 112 self.assertRaises(KeyError, d.__getitem__, 23) 113 114 class Exc(Exception): pass 115 116 class BadHash(object): 117 fail = False 118 def __hash__(self): 119 if self.fail: 120 raise Exc() 121 else: 122 return 42 123 124 x = BadHash() 125 d[x] = 42 126 x.fail = True 127 self.assertRaises(Exc, d.__getitem__, x) 128 129 def test_clear(self): 130 d = {1:1, 2:2, 3:3} 131 d.clear() 132 self.assertEqual(d, {}) 133 134 self.assertRaises(TypeError, d.clear, None) 135 136 @unittest.expectedFailure 137 def test_update(self): 138 d = {} 139 d.update({1:100}) 140 d.update({2:20}) 141 d.update({1:1, 2:2, 3:3}) 142 self.assertEqual(d, {1:1, 2:2, 3:3}) 143 144 d.update() 145 self.assertEqual(d, {1:1, 2:2, 3:3}) 146 147 self.assertRaises((TypeError, AttributeError), d.update, None) 148 149 class SimpleUserDict(object): 150 def __init__(self): 151 self.d = {1:1, 2:2, 3:3} 152 def keys(self): 153 return self.d.keys() 154 def __getitem__(self, i): 155 return self.d[i] 156 d.clear() 157 d.update(SimpleUserDict()) 158 self.assertEqual(d, {1:1, 2:2, 3:3}) 159 160 class Exc(Exception): pass 161 162 d.clear() 163 class FailingUserDict(object): 164 def keys(self): 165 raise Exc 166 self.assertRaises(Exc, d.update, FailingUserDict()) 167 168 class FailingUserDict(object): 169 def keys(self): 170 class BogonIter(object): 171 def __init__(self): 172 self.i = 1 173 def __iter__(self): 174 return self 175 def next(self): 176 if self.i: 177 self.i = 0 178 return 'a' 179 raise Exc 180 return BogonIter() 181 def __getitem__(self, key): 182 return key 183 self.assertRaises(Exc, d.update, FailingUserDict()) 184 185 class FailingUserDict(object): 186 def keys(self): 187 class BogonIter(object): 188 def __init__(self): 189 self.i = ord('a') 190 def __iter__(self): 191 return self 192 def next(self): 193 if self.i <= ord('z'): 194 rtn = chr(self.i) 195 self.i += 1 196 return rtn 197 raise StopIteration 198 return BogonIter() 199 def __getitem__(self, key): 200 raise Exc 201 self.assertRaises(Exc, d.update, FailingUserDict()) 202 203 class badseq(object): 204 def __iter__(self): 205 return self 206 def next(self): 207 raise Exc() 208 209 self.assertRaises(Exc, {}.update, badseq()) 210 211 self.assertRaises(ValueError, {}.update, [(1, 2, 3)]) 212 213 @unittest.expectedFailure 214 def test_fromkeys(self): 215 self.assertEqual(dict.fromkeys('abc'), {'a':None, 'b':None, 'c':None}) 216 d = {} 217 self.assertIsNot(d.fromkeys('abc'), d) 218 self.assertEqual(d.fromkeys('abc'), {'a':None, 'b':None, 'c':None}) 219 self.assertEqual(d.fromkeys((4,5),0), {4:0, 5:0}) 220 self.assertEqual(d.fromkeys([]), {}) 221 def g(): 222 yield 1 223 self.assertEqual(d.fromkeys(g()), {1:None}) 224 self.assertRaises(TypeError, {}.fromkeys, 3) 225 class dictlike(dict): pass 226 self.assertEqual(dictlike.fromkeys('a'), {'a':None}) 227 self.assertEqual(dictlike().fromkeys('a'), {'a':None}) 228 self.assertIsInstance(dictlike.fromkeys('a'), dictlike) 229 self.assertIsInstance(dictlike().fromkeys('a'), dictlike) 230 class mydict(dict): 231 def __new__(cls): 232 return UserDict.UserDict() 233 ud = mydict.fromkeys('ab') 234 self.assertEqual(ud, {'a':None, 'b':None}) 235 self.assertIsInstance(ud, UserDict.UserDict) 236 self.assertRaises(TypeError, dict.fromkeys) 237 238 class Exc(Exception): pass 239 240 class baddict1(dict): 241 def __init__(self): 242 raise Exc() 243 244 self.assertRaises(Exc, baddict1.fromkeys, [1]) 245 246 class BadSeq(object): 247 def __iter__(self): 248 return self 249 def next(self): 250 raise Exc() 251 252 self.assertRaises(Exc, dict.fromkeys, BadSeq()) 253 254 class baddict2(dict): 255 def __setitem__(self, key, value): 256 raise Exc() 257 258 self.assertRaises(Exc, baddict2.fromkeys, [1]) 259 260 # test fast path for dictionary inputs 261 d = dict(zip(range(6), range(6))) 262 self.assertEqual(dict.fromkeys(d, 0), dict(zip(range(6), [0]*6))) 263 264 class baddict3(dict): 265 def __new__(cls): 266 return d 267 d = {i : i for i in range(10)} 268 res = d.copy() 269 res.update(a=None, b=None, c=None) 270 # self.assertEqual(baddict3.fromkeys({"a", "b", "c"}), res) 271 272 @unittest.expectedFailure 273 def test_copy(self): 274 d = {1:1, 2:2, 3:3} 275 self.assertEqual(d.copy(), {1:1, 2:2, 3:3}) 276 self.assertEqual({}.copy(), {}) 277 self.assertRaises(TypeError, d.copy, None) 278 279 def test_get(self): 280 d = {} 281 self.assertIs(d.get('c'), None) 282 self.assertEqual(d.get('c', 3), 3) 283 d = {'a': 1, 'b': 2} 284 self.assertIs(d.get('c'), None) 285 self.assertEqual(d.get('c', 3), 3) 286 self.assertEqual(d.get('a'), 1) 287 self.assertEqual(d.get('a', 3), 1) 288 self.assertRaises(TypeError, d.get) 289 self.assertRaises(TypeError, d.get, None, None, None) 290 291 def test_setdefault(self): 292 # dict.setdefault() 293 d = {} 294 self.assertIs(d.setdefault('key0'), None) 295 d.setdefault('key0', []) 296 self.assertIs(d.setdefault('key0'), None) 297 d.setdefault('key', []).append(3) 298 self.assertEqual(d['key'][0], 3) 299 d.setdefault('key', []).append(4) 300 self.assertEqual(len(d['key']), 2) 301 self.assertRaises(TypeError, d.setdefault) 302 303 class Exc(Exception): pass 304 305 class BadHash(object): 306 fail = False 307 def __hash__(self): 308 if self.fail: 309 raise Exc() 310 else: 311 return 42 312 313 x = BadHash() 314 d[x] = 42 315 x.fail = True 316 self.assertRaises(Exc, d.setdefault, x, []) 317 318 def test_setdefault_atomic(self): 319 # Issue #13521: setdefault() calls __hash__ and __eq__ only once. 320 class Hashed(object): 321 def __init__(self): 322 self.hash_count = 0 323 self.eq_count = 0 324 def __hash__(self): 325 self.hash_count += 1 326 return 42 327 def __eq__(self, other): 328 self.eq_count += 1 329 return id(self) == id(other) 330 hashed1 = Hashed() 331 y = {hashed1: 5} 332 hashed2 = Hashed() 333 y.setdefault(hashed2, []) 334 self.assertEqual(hashed1.hash_count, 1) 335 self.assertEqual(hashed2.hash_count, 1) 336 self.assertEqual(hashed1.eq_count + hashed2.eq_count, 1) 337 338 @unittest.expectedFailure 339 def test_popitem(self): 340 # dict.popitem() 341 # for copymode in -1, +1: 342 for copymode in -1, 1: 343 # -1: b has same structure as a 344 # +1: b is a.copy() 345 for log2size in range(12): 346 size = 2**log2size 347 a = {} 348 b = {} 349 for i in range(size): 350 a[repr(i)] = i 351 if copymode < 0: 352 b[repr(i)] = i 353 if copymode > 0: 354 b = a.copy() 355 for i in range(size): 356 ka, va = ta = a.popitem() 357 self.assertEqual(va, int(ka)) 358 kb, vb = tb = b.popitem() 359 self.assertEqual(vb, int(kb)) 360 self.assertFalse(copymode < 0 and ta != tb) 361 self.assertFalse(a) 362 self.assertFalse(b) 363 364 d = {} 365 self.assertRaises(KeyError, d.popitem) 366 367 @unittest.expectedFailure 368 def test_pop(self): 369 # Tests for pop with specified key 370 d = {} 371 k, v = 'abc', 'def' 372 d[k] = v 373 self.assertRaises(KeyError, d.pop, 'ghi') 374 375 self.assertEqual(d.pop(k), v) 376 self.assertEqual(len(d), 0) 377 378 self.assertRaises(KeyError, d.pop, k) 379 380 # verify longs/ints get same value when key > 32 bits 381 # (for 64-bit archs). See SF bug #689659. 382 x = 4503599627370496L 383 y = 4503599627370496 384 h = {x: 'anything', y: 'something else'} 385 self.assertEqual(h[x], h[y]) 386 387 self.assertEqual(d.pop(k, v), v) 388 d[k] = v 389 self.assertEqual(d.pop(k, 1), v) 390 391 self.assertRaises(TypeError, d.pop) 392 393 class Exc(Exception): pass 394 395 class BadHash(object): 396 fail = False 397 def __hash__(self): 398 if self.fail: 399 raise Exc() 400 else: 401 return 42 402 403 x = BadHash() 404 d[x] = 42 405 x.fail = True 406 self.assertRaises(Exc, d.pop, x) 407 408 def test_mutatingiteration(self): 409 # changing dict size during iteration 410 d = {} 411 d[1] = 1 412 with self.assertRaises(RuntimeError): 413 for i in d: 414 d[i+1] = 1 415 416 def test_repr(self): 417 d = {} 418 self.assertEqual(repr(d), '{}') 419 d[1] = 2 420 self.assertEqual(repr(d), '{1: 2}') 421 d = {} 422 d[1] = d 423 self.assertEqual(repr(d), '{1: {...}}') 424 425 class Exc(Exception): pass 426 427 class BadRepr(object): 428 def __repr__(self): 429 raise Exc() 430 431 d = {1: BadRepr()} 432 self.assertRaises(Exc, repr, d) 433 434 @unittest.expectedFailure 435 def test_le(self): 436 self.assertFalse({} < {}) 437 self.assertFalse({1: 2} < {1L: 2L}) 438 439 class Exc(Exception): pass 440 441 class BadCmp(object): 442 def __eq__(self, other): 443 raise Exc() 444 def __hash__(self): 445 return 42 446 447 d1 = {BadCmp(): 1} 448 d2 = {1: 1} 449 450 with self.assertRaises(Exc): 451 d1 < d2 452 453 @unittest.expectedFailure 454 def test_missing(self): 455 # Make sure dict doesn't have a __missing__ method 456 self.assertFalse(hasattr(dict, "__missing__")) 457 self.assertFalse(hasattr({}, "__missing__")) 458 # Test several cases: 459 # (D) subclass defines __missing__ method returning a value 460 # (E) subclass defines __missing__ method raising RuntimeError 461 # (F) subclass sets __missing__ instance variable (no effect) 462 # (G) subclass doesn't define __missing__ at all 463 class D(dict): 464 def __missing__(self, key): 465 return 42 466 d = D({1: 2, 3: 4}) 467 self.assertEqual(d[1], 2) 468 self.assertEqual(d[3], 4) 469 self.assertNotIn(2, d) 470 self.assertNotIn(2, d.keys()) 471 self.assertEqual(d[2], 42) 472 473 class E(dict): 474 def __missing__(self, key): 475 raise RuntimeError(key) 476 e = E() 477 with self.assertRaises(RuntimeError) as c: 478 e[42] 479 self.assertEqual(c.exception.args, (42,)) 480 481 class F(dict): 482 def __init__(self): 483 # An instance variable __missing__ should have no effect 484 self.__missing__ = lambda key: None 485 f = F() 486 with self.assertRaises(KeyError) as c: 487 f[42] 488 self.assertEqual(c.exception.args, (42,)) 489 490 class G(dict): 491 pass 492 g = G() 493 with self.assertRaises(KeyError) as c: 494 g[42] 495 self.assertEqual(c.exception.args, (42,)) 496 497 @unittest.expectedFailure 498 def test_tuple_keyerror(self): 499 # SF #1576657 500 d = {} 501 with self.assertRaises(KeyError) as c: 502 d[(1,)] 503 self.assertEqual(c.exception.args, ((1,),)) 504 505 # def test_bad_key(self): 506 # # Dictionary lookups should fail if __cmp__() raises an exception. 507 # class CustomException(Exception): 508 # pass 509 510 # class BadDictKey(object): 511 # def __hash__(self): 512 # return hash(self.__class__) 513 514 # def __cmp__(self, other): 515 # if isinstance(other, self.__class__): 516 # raise CustomException 517 # return other 518 519 # d = {} 520 # x1 = BadDictKey() 521 # x2 = BadDictKey() 522 # d[x1] = 1 523 # for stmt in ['d[x2] = 2', 524 # 'z = d[x2]', 525 # 'x2 in d', 526 # 'd.has_key(x2)', 527 # 'd.get(x2)', 528 # 'd.setdefault(x2, 42)', 529 # 'd.pop(x2)', 530 # 'd.update({x2: 2})']: 531 # with self.assertRaises(CustomException): 532 # exec stmt in locals() 533 534 def test_resize1(self): 535 # Dict resizing bug, found by Jack Jansen in 2.2 CVS development. 536 # This version got an assert failure in debug build, infinite loop in 537 # release build. Unfortunately, provoking this kind of stuff requires 538 # a mix of inserts and deletes hitting exactly the right hash codes in 539 # exactly the right order, and I can't think of a randomized approach 540 # that would be *likely* to hit a failing case in reasonable time. 541 542 d = {} 543 for i in range(5): 544 d[i] = i 545 for i in range(5): 546 del d[i] 547 for i in range(5, 9): # i==8 was the problem 548 d[i] = i 549 550 def test_resize2(self): 551 # Another dict resizing bug (SF bug #1456209). 552 # This caused Segmentation faults or Illegal instructions. 553 554 class X(object): 555 def __hash__(self): 556 return 5 557 def __eq__(self, other): 558 if resizing: 559 d.clear() 560 return False 561 d = {} 562 resizing = False 563 d[X()] = 1 564 d[X()] = 2 565 d[X()] = 3 566 d[X()] = 4 567 d[X()] = 5 568 # now trigger a resize 569 resizing = True 570 d[9] = 6 571 572 def test_empty_presized_dict_in_freelist(self): 573 # Bug #3537: if an empty but presized dict with a size larger 574 # than 7 was in the freelist, it triggered an assertion failure 575 with self.assertRaises(ZeroDivisionError): 576 d = {'a': 1 // 0, 'b': None, 'c': None, 'd': None, 'e': None, 577 'f': None, 'g': None, 'h': None} 578 d = {} 579 580 # def test_container_iterator(self): 581 # # Bug #3680: tp_traverse was not implemented for dictiter objects 582 # class C(object): 583 # pass 584 # iterators = (dict.iteritems, dict.itervalues, dict.iterkeys) 585 # for i in iterators: 586 # obj = C() 587 # ref = weakref.ref(obj) 588 # container = {obj: 1} 589 # obj.x = i(container) 590 # del obj, container 591 # gc.collect() 592 # self.assertIs(ref(), None, "Cycle was not collected") 593 594 # def _not_tracked(self, t): 595 # # Nested containers can take several collections to untrack 596 # gc.collect() 597 # gc.collect() 598 # self.assertFalse(gc.is_tracked(t), t) 599 600 # def _tracked(self, t): 601 # self.assertTrue(gc.is_tracked(t), t) 602 # gc.collect() 603 # gc.collect() 604 # self.assertTrue(gc.is_tracked(t), t) 605 606 @test_support.cpython_only 607 def test_track_literals(self): 608 # Test GC-optimization of dict literals 609 x, y, z, w = 1.5, "a", (1, None), [] 610 611 self._not_tracked({}) 612 self._not_tracked({x:(), y:x, z:1}) 613 self._not_tracked({1: "a", "b": 2}) 614 self._not_tracked({1: 2, (None, True, False, ()): int}) 615 self._not_tracked({1: object()}) 616 617 # Dicts with mutable elements are always tracked, even if those 618 # elements are not tracked right now. 619 self._tracked({1: []}) 620 self._tracked({1: ([],)}) 621 self._tracked({1: {}}) 622 self._tracked({1: set()}) 623 624 @test_support.cpython_only 625 def test_track_dynamic(self): 626 # Test GC-optimization of dynamically-created dicts 627 class MyObject(object): 628 pass 629 x, y, z, w, o = 1.5, "a", (1, object()), [], MyObject() 630 631 d = dict() 632 self._not_tracked(d) 633 d[1] = "a" 634 self._not_tracked(d) 635 d[y] = 2 636 self._not_tracked(d) 637 d[z] = 3 638 self._not_tracked(d) 639 self._not_tracked(d.copy()) 640 d[4] = w 641 self._tracked(d) 642 self._tracked(d.copy()) 643 d[4] = None 644 self._not_tracked(d) 645 self._not_tracked(d.copy()) 646 647 # dd isn't tracked right now, but it may mutate and therefore d 648 # which contains it must be tracked. 649 d = dict() 650 dd = dict() 651 d[1] = dd 652 self._not_tracked(dd) 653 self._tracked(d) 654 dd[1] = d 655 self._tracked(dd) 656 657 d = dict.fromkeys([x, y, z]) 658 self._not_tracked(d) 659 dd = dict() 660 dd.update(d) 661 self._not_tracked(dd) 662 d = dict.fromkeys([x, y, z, o]) 663 self._tracked(d) 664 dd = dict() 665 dd.update(d) 666 self._tracked(dd) 667 668 d = dict(x=x, y=y, z=z) 669 self._not_tracked(d) 670 d = dict(x=x, y=y, z=z, w=w) 671 self._tracked(d) 672 d = dict() 673 d.update(x=x, y=y, z=z) 674 self._not_tracked(d) 675 d.update(w=w) 676 self._tracked(d) 677 678 d = dict([(x, y), (z, 1)]) 679 self._not_tracked(d) 680 d = dict([(x, y), (z, w)]) 681 self._tracked(d) 682 d = dict() 683 d.update([(x, y), (z, 1)]) 684 self._not_tracked(d) 685 d.update([(x, y), (z, w)]) 686 self._tracked(d) 687 688 @test_support.cpython_only 689 def test_track_subtypes(self): 690 # Dict subtypes are always tracked 691 class MyDict(dict): 692 pass 693 self._tracked(MyDict()) 694 695 696 # def test_free_after_iterating(self): 697 # test_support.check_free_after_iterating(self, iter, dict) 698 # test_support.check_free_after_iterating(self, lambda d: d.iterkeys(), dict) 699 # test_support.check_free_after_iterating(self, lambda d: d.itervalues(), dict) 700 # test_support.check_free_after_iterating(self, lambda d: d.iteritems(), dict) 701 # test_support.check_free_after_iterating(self, lambda d: iter(d.viewkeys()), dict) 702 # test_support.check_free_after_iterating(self, lambda d: iter(d.viewvalues()), dict) 703 # test_support.check_free_after_iterating(self, lambda d: iter(d.viewitems()), dict) 704 705 from test import mapping_tests 706 707 class GeneralMappingTests(mapping_tests.BasicTestMappingProtocol): 708 type2test = dict 709 710 class Dict(dict): 711 pass 712 713 class SubclassMappingTests(mapping_tests.BasicTestMappingProtocol): 714 type2test = Dict 715 716 def test_main(): 717 with test_support.check_py3k_warnings( 718 ('dict(.has_key..| inequality comparisons) not supported in 3.x', 719 DeprecationWarning)): 720 test_support.run_unittest( 721 DictTest, 722 GeneralMappingTests, 723 SubclassMappingTests, 724 ) 725 726 if __name__ == "__main__": 727 test_main()