| 1 | import py |
| 2 | from rpython.tool.identity_dict import identity_dict, IdentityDictPurePython |
| 3 | |
| 4 | class TestIdentityDictNative: |
| 5 | identity_dict = identity_dict |
| 6 | |
| 7 | def test_numbers(self): |
| 8 | d = self.identity_dict() |
| 9 | d[0] = 1 |
| 10 | d[0.0] = 2 |
| 11 | d[long(0)] = 3 |
| 12 | |
| 13 | assert d |
| 14 | assert len(d) == 3 |
| 15 | d.clear() |
| 16 | assert not d |
| 17 | |
| 18 | def test_get(self): |
| 19 | d = self.identity_dict() |
| 20 | d[None] = 1 |
| 21 | |
| 22 | assert d.get(None, 42) == 1 |
| 23 | assert d.get(None) == 1 |
| 24 | assert d.get(1) is None |
| 25 | assert d.get(1, 42) == 42 |
| 26 | |
| 27 | def test_unhashable(self): |
| 28 | d = self.identity_dict() |
| 29 | d[[]] = 1 |
| 30 | d[[]] = 2 |
| 31 | a = [] |
| 32 | d[a] = 3 |
| 33 | assert len(d) == 3 |
| 34 | d[a] = 4 |
| 35 | assert len(d) == 3 |
| 36 | assert d[a] == 4 |
| 37 | |
| 38 | py.test.raises(KeyError, d.__getitem__, []) |
| 39 | |
| 40 | def test_keys(self): |
| 41 | d = self.identity_dict() |
| 42 | d[[]] = 1 |
| 43 | d[[]] = 2 |
| 44 | d[[]] = 3 |
| 45 | |
| 46 | assert d.keys() == [[], [], []] |
| 47 | assert sorted(d.values()) == [1, 2, 3] |
| 48 | |
| 49 | def test_in(self): |
| 50 | d = self.identity_dict() |
| 51 | d[None] = 1 |
| 52 | |
| 53 | assert None in d |
| 54 | assert [] not in d |
| 55 | |
| 56 | |
| 57 | class TestIdentityDictPurePython(TestIdentityDictNative): |
| 58 | identity_dict = IdentityDictPurePython |