aboutsummaryrefslogtreecommitdiff
path: root/tests/test_none_keys.py
blob: 8c0bb379f419e8c05b3fa1c617369f8b939d511d (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
import unittest

from immutables.map import map_hash, map_mask, Map as PyMap
from immutables._testutils import HashKey


none_hash = map_hash(None)
assert(none_hash != 1)
assert((none_hash >> 32) == 0)

not_collision = 0xffffffff & (~none_hash)

mask = 0x7ffffffff
none_collisions = [none_hash & (mask >> shift)
                   for shift in reversed(range(0, 32, 5))]
assert(len(none_collisions) == 7)
none_collisions = [h | (not_collision & (mask << shift))
                   for shift, h in zip(range(5, 37, 5), none_collisions)]


class NoneCollision(HashKey):
    def __init__(self, name, level):
        if name is None:
            raise ValueError("Can't have a NoneCollision with a None value")
        super().__init__(none_collisions[level], name)

    def __eq__(self, other):
        if other is None:
            return False
        return super().__eq__(other)

    __hash__ = HashKey.__hash__


class BaseNoneTest:
    Map = None

    def test_none_collisions(self):
        collisions = [NoneCollision('a', level) for level in range(7)]
        indices = [map_mask(none_hash, shift) for shift in range(0, 32, 5)]

        for i, c in enumerate(collisions[:-1], 1):
            self.assertNotEqual(c, None)
            c_hash = map_hash(c)
            self.assertNotEqual(c_hash, none_hash)
            for j, idx in enumerate(indices[:i]):
                self.assertEqual(map_mask(c_hash, j*5), idx)
            for j, idx in enumerate(indices[i:], i):
                self.assertNotEqual(map_mask(c_hash, j*5), idx)

        c = collisions[-1]
        self.assertNotEqual(c, None)
        c_hash = map_hash(c)
        self.assertEqual(c_hash, none_hash)
        for i, idx in enumerate(indices):
            self.assertEqual(map_mask(c_hash, i*5), idx)

    def test_none_as_key(self):
        m = self.Map({None: 1})

        self.assertEqual(len(m), 1)
        self.assertTrue(None in m)
        self.assertEqual(m[None], 1)
        self.assertEqual(repr(m), 'immutables.Map({None: 1})')

        for level in range(7):
            key = NoneCollision('a', level)
            self.assertFalse(key in m)
            with self.assertRaises(KeyError):
                m.delete(key)

        m = m.delete(None)
        self.assertEqual(len(m), 0)
        self.assertFalse(None in m)
        self.assertEqual(repr(m), 'immutables.Map({})')

        self.assertEqual(m, self.Map())

        with self.assertRaises(KeyError):
            m.delete(None)

    def test_none_set(self):
        m = self.Map().set(None, 2)

        self.assertEqual(len(m), 1)
        self.assertTrue(None in m)
        self.assertEqual(m[None], 2)

        m = m.set(None, 1)

        self.assertEqual(len(m), 1)
        self.assertTrue(None in m)
        self.assertEqual(m[None], 1)

        m = m.delete(None)

        self.assertEqual(len(m), 0)
        self.assertEqual(m, self.Map())
        self.assertFalse(None in m)

        with self.assertRaises(KeyError):
            m.delete(None)

    def test_none_collision_1(self):
        for level in range(7):
            key = NoneCollision('a', level)
            m = self.Map({None: 1, key: 2})

            self.assertEqual(len(m), 2)
            self.assertTrue(None in m)
            self.assertEqual(m[None], 1)
            self.assertTrue(key in m)
            self.assertEqual(m[key], 2)

            m2 = m.delete(None)
            self.assertEqual(len(m2), 1)
            self.assertTrue(key in m2)
            self.assertEqual(m2[key], 2)
            self.assertFalse(None in m2)
            with self.assertRaises(KeyError):
                m2.delete(None)

            m3 = m2.delete(key)
            self.assertEqual(len(m3), 0)
            self.assertFalse(None in m3)
            self.assertFalse(key in m3)
            self.assertEqual(m3, self.Map())
            self.assertEqual(repr(m3), 'immutables.Map({})')
            with self.assertRaises(KeyError):
                m3.delete(None)
            with self.assertRaises(KeyError):
                m3.delete(key)

            m2 = m.delete(key)
            self.assertEqual(len(m2), 1)
            self.assertTrue(None in m2)
            self.assertEqual(m2[None], 1)
            self.assertFalse(key in m2)
            with self.assertRaises(KeyError):
                m2.delete(key)

            m4 = m2.delete(None)
            self.assertEqual(len(m4), 0)
            self.assertFalse(None in m4)
            self.assertFalse(key in m4)
            self.assertEqual(m4, self.Map())
            self.assertEqual(repr(m4), 'immutables.Map({})')
            with self.assertRaises(KeyError):
                m4.delete(None)
            with self.assertRaises(KeyError):
                m4.delete(key)

            self.assertEqual(m3, m4)

    def test_none_collision_2(self):
        key = HashKey(not_collision, 'a')
        m = self.Map().set(None, 1).set(key, 2)

        self.assertEqual(len(m), 2)
        self.assertTrue(key in m)
        self.assertTrue(None in m)
        self.assertEqual(m[key], 2)
        self.assertEqual

        m = m.set(None, 0)
        self.assertEqual(len(m), 2)
        self.assertTrue(key in m)
        self.assertTrue(None in m)

        for level in range(7):
            key2 = NoneCollision('b', level)
            self.assertFalse(key2 in m)
            m2 = m.set(key2, 1)

            self.assertEqual(len(m2), 3)
            self.assertTrue(key in m2)
            self.assertTrue(None in m2)
            self.assertTrue(key2 in m2)
            self.assertEqual(m2[key], 2)
            self.assertEqual(m2[None], 0)
            self.assertEqual(m2[key2], 1)

            m2 = m2.set(None, 1)
            self.assertEqual(len(m2), 3)
            self.assertTrue(key in m2)
            self.assertTrue(None in m2)
            self.assertTrue(key2 in m2)
            self.assertEqual(m2[key], 2)
            self.assertEqual(m2[None], 1)
            self.assertEqual(m2[key2], 1)

            m2 = m2.set(None, 2)
            self.assertEqual(len(m2), 3)
            self.assertTrue(key in m2)
            self.assertTrue(None in m2)
            self.assertTrue(key2 in m2)
            self.assertEqual(m2[key], 2)
            self.assertEqual(m2[None], 2)
            self.assertEqual(m2[key2], 1)

            m3 = m2.delete(key)
            self.assertEqual(len(m3), 2)
            self.assertTrue(None in m3)
            self.assertTrue(key2 in m3)
            self.assertFalse(key in m3)
            self.assertEqual(m3[None], 2)
            self.assertEqual(m3[key2], 1)
            with self.assertRaises(KeyError):
                m3.delete(key)

            m3 = m2.delete(key2)
            self.assertEqual(len(m3), 2)
            self.assertTrue(None in m3)
            self.assertTrue(key in m3)
            self.assertFalse(key2 in m3)
            self.assertEqual(m3[None], 2)
            self.assertEqual(m3[key], 2)
            with self.assertRaises(KeyError):
                m3.delete(key2)

            m3 = m2.delete(None)
            self.assertEqual(len(m3), 2)
            self.assertTrue(key in m3)
            self.assertTrue(key2 in m3)
            self.assertFalse(None in m3)
            self.assertEqual(m3[key], 2)
            self.assertEqual(m3[key2], 1)
            with self.assertRaises(KeyError):
                m3.delete(None)

        m2 = m.delete(None)
        self.assertEqual(len(m2), 1)
        self.assertFalse(None in m2)
        self.assertTrue(key in m2)
        self.assertEqual(m2[key], 2)
        with self.assertRaises(KeyError):
            m2.delete(None)

        m2 = m.delete(key)
        self.assertEqual(len(m2), 1)
        self.assertFalse(key in m2)
        self.assertTrue(None in m2)
        self.assertEqual(m2[None], 0)
        with self.assertRaises(KeyError):
            m2.delete(key)

    def test_none_collision_3(self):
        for level in range(7):
            key = NoneCollision('a', level)
            m = self.Map({key: 2})

            self.assertEqual(len(m), 1)
            self.assertFalse(None in m)
            self.assertTrue(key in m)
            self.assertEqual(m[key], 2)
            with self.assertRaises(KeyError):
                m.delete(None)

            m = m.set(None, 1)
            self.assertEqual(len(m), 2)
            self.assertTrue(key in m)
            self.assertEqual(m[key], 2)
            self.assertTrue(None in m)
            self.assertEqual(m[None], 1)

            m = m.set(None, 0)
            self.assertEqual(len(m), 2)
            self.assertTrue(key in m)
            self.assertEqual(m[key], 2)
            self.assertTrue(None in m)
            self.assertEqual(m[None], 0)

            m2 = m.delete(key)
            self.assertEqual(len(m2), 1)
            self.assertTrue(None in m2)
            self.assertEqual(m2[None], 0)
            self.assertFalse(key in m2)
            with self.assertRaises(KeyError):
                m2.delete(key)

            m2 = m.delete(None)
            self.assertEqual(len(m2), 1)
            self.assertTrue(key in m2)
            self.assertEqual(m2[key], 2)
            self.assertFalse(None in m2)
            with self.assertRaises(KeyError):
                m2.delete(None)

    def test_collision_4(self):
        key2 = NoneCollision('a', 2)
        key4 = NoneCollision('b', 4)
        m = self.Map({key2: 2, key4: 4})

        self.assertEqual(len(m), 2)
        self.assertTrue(key2 in m)
        self.assertTrue(key4 in m)
        self.assertEqual(m[key2], 2)
        self.assertEqual(m[key4], 4)
        self.assertFalse(None in m)

        m2 = m.set(None, 9)

        self.assertEqual(len(m2), 3)
        self.assertTrue(key2 in m2)
        self.assertTrue(key4 in m2)
        self.assertTrue(None in m2)
        self.assertEqual(m2[key2], 2)
        self.assertEqual(m2[key4], 4)
        self.assertEqual(m2[None], 9)

        m3 = m2.set(None, 0)
        self.assertEqual(len(m3), 3)
        self.assertTrue(key2 in m3)
        self.assertTrue(key4 in m3)
        self.assertTrue(None in m3)
        self.assertEqual(m3[key2], 2)
        self.assertEqual(m3[key4], 4)
        self.assertEqual(m3[None], 0)

        m3 = m2.set(key2, 0)
        self.assertEqual(len(m3), 3)
        self.assertTrue(key2 in m3)
        self.assertTrue(key4 in m3)
        self.assertTrue(None in m3)
        self.assertEqual(m3[key2], 0)
        self.assertEqual(m3[key4], 4)
        self.assertEqual(m3[None], 9)

        m3 = m2.set(key4, 0)
        self.assertEqual(len(m3), 3)
        self.assertTrue(key2 in m3)
        self.assertTrue(key4 in m3)
        self.assertTrue(None in m3)
        self.assertEqual(m3[key2], 2)
        self.assertEqual(m3[key4], 0)
        self.assertEqual(m3[None], 9)

        m3 = m2.delete(None)
        self.assertEqual(m3, m)
        self.assertEqual(len(m3), 2)
        self.assertTrue(key2 in m3)
        self.assertTrue(key4 in m3)
        self.assertEqual(m3[key2], 2)
        self.assertEqual(m3[key4], 4)
        self.assertFalse(None in m3)
        with self.assertRaises(KeyError):
            m3.delete(None)

        m3 = m2.delete(key2)
        self.assertEqual(len(m3), 2)
        self.assertTrue(None in m3)
        self.assertTrue(key4 in m3)
        self.assertEqual(m3[None], 9)
        self.assertEqual(m3[key4], 4)
        self.assertFalse(key2 in m3)
        with self.assertRaises(KeyError):
            m3.delete(key2)

        m3 = m2.delete(key4)
        self.assertEqual(len(m3), 2)
        self.assertTrue(None in m3)
        self.assertTrue(key2 in m3)
        self.assertEqual(m3[None], 9)
        self.assertEqual(m3[key2], 2)
        self.assertFalse(key4 in m3)
        with self.assertRaises(KeyError):
            m3.delete(key4)

    def test_none_mutation(self):
        key2 = NoneCollision('a', 2)
        key4 = NoneCollision('b', 4)
        key = NoneCollision('c', -1)
        m = self.Map({key: -1, key2: 2, key4: 4, None: 9})

        with m.mutate() as mm:
            self.assertEqual(len(mm), 4)
            self.assertTrue(key in mm)
            self.assertTrue(key2 in mm)
            self.assertTrue(key4 in mm)
            self.assertTrue(None in mm)
            self.assertEqual(mm[key2], 2)
            self.assertEqual(mm[key4], 4)
            self.assertEqual(mm[key], -1)
            self.assertEqual(mm[None], 9)

            for k in m:
                mm[k] = -mm[k]

            self.assertEqual(len(mm), 4)
            self.assertTrue(key in mm)
            self.assertTrue(key2 in mm)
            self.assertTrue(key4 in mm)
            self.assertTrue(None in mm)
            self.assertEqual(mm[key2], -2)
            self.assertEqual(mm[key4], -4)
            self.assertEqual(mm[key], 1)
            self.assertEqual(mm[None], -9)

            for k in m:
                del mm[k]
                self.assertEqual(len(mm), 3)
                self.assertFalse(k in mm)
                for n in m:
                    if n != k:
                        self.assertTrue(n in mm)
                        self.assertEqual(mm[n], -m[n])
                with self.assertRaises(KeyError):
                    del mm[k]
                mm[k] = -m[k]
                self.assertEqual(len(mm), 4)
                self.assertTrue(k in mm)
                self.assertEqual(mm[k], -m[k])

            for k in m:
                mm[k] = -mm[k]

            self.assertEqual(len(mm), 4)
            self.assertTrue(key in mm)
            self.assertTrue(key2 in mm)
            self.assertTrue(key4 in mm)
            self.assertTrue(None in mm)
            self.assertEqual(mm[key2], 2)
            self.assertEqual(mm[key4], 4)
            self.assertEqual(mm[key], -1)
            self.assertEqual(mm[None], 9)

            for k in m:
                mm[k] = -mm[k]

            self.assertEqual(len(mm), 4)
            self.assertTrue(key in mm)
            self.assertTrue(key2 in mm)
            self.assertTrue(key4 in mm)
            self.assertTrue(None in mm)
            self.assertEqual(mm[key2], -2)
            self.assertEqual(mm[key4], -4)
            self.assertEqual(mm[key], 1)
            self.assertEqual(mm[None], -9)

            m2 = mm.finish()

        self.assertEqual(set(m), set(m2))
        self.assertEqual(len(m2), 4)
        self.assertTrue(key in m2)
        self.assertTrue(key2 in m2)
        self.assertTrue(key4 in m2)
        self.assertTrue(None in m2)
        self.assertEqual(m2[key2], -2)
        self.assertEqual(m2[key4], -4)
        self.assertEqual(m2[key], 1)
        self.assertEqual(m2[None], -9)

        for k, v in m.items():
            self.assertTrue(k in m2)
            self.assertEqual(m2[k], -v)

    def test_iterators(self):
        key2 = NoneCollision('a', 2)
        key4 = NoneCollision('b', 4)
        key = NoneCollision('c', -1)
        m = self.Map({key: -1, key2: 2, key4: 4, None: 9})

        self.assertEqual(len(m), 4)
        self.assertTrue(key in m)
        self.assertTrue(key2 in m)
        self.assertTrue(key4 in m)
        self.assertTrue(None in m)
        self.assertEqual(m[key2], 2)
        self.assertEqual(m[key4], 4)
        self.assertEqual(m[key], -1)
        self.assertEqual(m[None], 9)

        s = set(m)
        self.assertEqual(len(s), 4)
        self.assertEqual(s, set([None, key, key2, key4]))

        sk = set(m.keys())
        self.assertEqual(s, sk)

        sv = set(m.values())
        self.assertEqual(len(sv), 4)
        self.assertEqual(sv, set([-1, 2, 4, 9]))

        si = set(m.items())
        self.assertEqual(len(si), 4)
        self.assertEqual(si,
                         set([(key, -1), (key2, 2), (key4, 4), (None, 9)]))

        d = {key: -1, key2: 2, key4: 4, None: 9}
        self.assertEqual(dict(m.items()), d)


class PyMapNoneTest(BaseNoneTest, unittest.TestCase):

    Map = PyMap


try:
    from immutables._map import Map as CMap
except ImportError:
    CMap = None


@unittest.skipIf(CMap is None, 'C Map is not available')
class CMapNoneTest(BaseNoneTest, unittest.TestCase):

    Map = CMap


if __name__ == "__main__":
    unittest.main()