Debian Codegraph @

tests/test_coverage_sorteddict.py

raw
1 # -*- coding: utf-8 -*-
2
3 import platform
4 import random
5 import string
6 import warnings
7
8 from .context import sortedcontainers
9 from sortedcontainers import SortedDict
10 import pytest
11 from sys import hexversion
12 import gc
13
14 if hexversion < 0x03000000:
15 range = xrange
16
17 def negate(value):
18 return -value
19
20 def modulo(value):
21 return value % 10
22
23 def get_keysview(dic):
24 if hexversion < 0x03000000:
25 return dic.viewkeys()
26 else:
27 return dic.keys()
28
29 def get_itemsview(dic):
30 if hexversion < 0x03000000:
31 return dic.viewitems()
32 else:
33 return dic.items()
34
35 def test_init():
36 temp = SortedDict()
37 assert temp.key is None
38 temp._check()
39
40 def test_init_key():
41 temp = SortedDict(negate)
42 assert temp.key == negate
43 temp._check()
44
45 def test_init_args():
46 temp = SortedDict([('a', 1), ('b', 2)])
47 assert len(temp) == 2
48 assert temp['a'] == 1
49 assert temp['b'] == 2
50 temp._check()
51
52 def test_init_kwargs():
53 temp = SortedDict(a=1, b=2)
54 assert len(temp) == 2
55 assert temp['a'] == 1
56 assert temp['b'] == 2
57 temp._check()
58
59 def test_clear():
60 mapping = [(val, pos) for pos, val in enumerate(string.ascii_lowercase)]
61 temp = SortedDict(mapping)
62 assert len(temp) == 26
63 assert list(temp.items()) == mapping
64 temp.clear()
65 assert len(temp) == 0
66
67 def test_contains():
68 mapping = [(val, pos) for pos, val in enumerate(string.ascii_lowercase)]
69 temp = SortedDict(mapping)
70 assert all((val in temp) for val in string.ascii_lowercase)
71
72 def test_delitem():
73 mapping = [(val, pos) for pos, val in enumerate(string.ascii_lowercase)]
74 temp = SortedDict(mapping)
75 del temp['a']
76 temp._check()
77
78 def test_getitem():
79 mapping = [(val, pos) for pos, val in enumerate(string.ascii_lowercase)]
80 temp = SortedDict(mapping)
81 assert all((temp[val] == pos) for pos, val in enumerate(string.ascii_lowercase))
82
83 def test_eq():
84 mapping = [(val, pos) for pos, val in enumerate(string.ascii_lowercase)]
85 temp1 = SortedDict(mapping)
86 temp2 = SortedDict(mapping)
87 assert temp1 == temp2
88 assert not (temp1 != temp2)
89 temp2['a'] = 100
90 assert temp1 != temp2
91 assert not (temp1 == temp2)
92 del temp2['a']
93 assert temp1 != temp2
94 assert not (temp1 == temp2)
95 temp2['zz'] = 0
96 assert temp1 != temp2
97 assert not (temp1 == temp2)
98
99 def test_iter():
100 mapping = [(val, pos) for pos, val in enumerate(string.ascii_lowercase)]
101 temp = SortedDict(mapping)
102 assert all(lhs == rhs for lhs, rhs in zip(temp, string.ascii_lowercase))
103
104 def test_iter_key():
105 temp = SortedDict(negate, ((val, val) for val in range(100)))
106 temp._reset(7)
107 assert all(lhs == rhs for lhs, rhs in zip(temp, reversed(range(100))))
108
109 def test_reversed():
110 mapping = [(val, pos) for pos, val in enumerate(string.ascii_lowercase)]
111 temp = SortedDict(mapping)
112 assert all(lhs == rhs for lhs, rhs in
113 zip(reversed(temp), reversed(string.ascii_lowercase)))
114
115 def test_reversed_key():
116 temp = SortedDict(modulo, ((val, val) for val in range(100)))
117 temp._reset(7)
118 values = sorted(range(100), key=modulo)
119 assert all(lhs == rhs for lhs, rhs in zip(reversed(temp), reversed(values)))
120
121 def test_islice():
122 mapping = [(val, pos) for pos, val in enumerate(string.ascii_lowercase)]
123 temp = SortedDict(mapping)
124 temp._reset(7)
125
126 for start in range(30):
127 for stop in range(30):
128 assert list(temp.islice(start, stop)) == list(string.ascii_lowercase[start:stop])
129
130 def test_irange():
131 mapping = [(val, pos) for pos, val in enumerate(string.ascii_lowercase)]
132 temp = SortedDict(mapping)
133 temp._reset(7)
134 for start in range(26):
135 for stop in range(start + 1, 26):
136 result = list(string.ascii_lowercase[start:stop])
137 assert list(temp.irange(result[0], result[-1])) == result
138
139 def test_irange_key():
140 temp = SortedDict(modulo, ((val, val) for val in range(100)))
141 temp._reset(7)
142 values = sorted(range(100), key=modulo)
143
144 for start in range(10):
145 for stop in range(start, 10):
146 result = list(temp.irange_key(start, stop))
147 assert result == values[(start * 10):((stop + 1) * 10)]
148
149 def test_len():
150 mapping = [(val, pos) for pos, val in enumerate(string.ascii_lowercase)]
151 temp = SortedDict(mapping)
152 assert len(temp) == 26
153
154 def test_setitem():
155 temp = SortedDict()
156
157 for pos, key in enumerate(string.ascii_lowercase):
158 temp[key] = pos
159 temp._check()
160
161 assert len(temp) == 26
162
163 for pos, key in enumerate(string.ascii_lowercase):
164 temp[key] = pos
165 temp._check()
166
167 assert len(temp) == 26
168
169 def test_copy():
170 mapping = [(val, pos) for pos, val in enumerate(string.ascii_lowercase)]
171 temp = SortedDict(mapping)
172 dup = temp.copy()
173 assert len(temp) == 26
174 assert len(dup) == 26
175 dup.clear()
176 assert len(temp) == 26
177 assert len(dup) == 0
178
179 def test_copy_copy():
180 import copy
181 mapping = [(val, pos) for pos, val in enumerate(string.ascii_lowercase)]
182 temp = SortedDict(mapping)
183 dup = copy.copy(temp)
184 assert len(temp) == 26
185 assert len(dup) == 26
186 dup.clear()
187 assert len(temp) == 26
188 assert len(dup) == 0
189
190 def test_fromkeys():
191 mapping = [(val, pos) for pos, val in enumerate(string.ascii_lowercase)]
192 temp = SortedDict.fromkeys(mapping, 1)
193 assert all(temp[key] == 1 for key in temp)
194
195 def test_get():
196 mapping = [(val, pos) for pos, val in enumerate(string.ascii_lowercase)]
197 temp = SortedDict(mapping)
198 assert temp.get('a') == 0
199 assert temp.get('A', -1) == -1
200
201 def test_has_key():
202 if hexversion > 0x03000000:
203 return
204 mapping = [(val, pos) for pos, val in enumerate(string.ascii_lowercase)]
205 temp = SortedDict(mapping)
206 assert temp.has_key('a')
207
208 def test_items():
209 mapping = [(val, pos) for pos, val in enumerate(string.ascii_lowercase)]
210 temp = SortedDict(mapping)
211 assert list(temp.items()) == mapping
212
213 def test_keys():
214 mapping = [(val, pos) for pos, val in enumerate(string.ascii_lowercase)]
215 temp = SortedDict(mapping)
216 assert list(temp.keys()) == [key for key, pos in mapping]
217
218 def test_values():
219 mapping = [(val, pos) for pos, val in enumerate(string.ascii_lowercase)]
220 temp = SortedDict(mapping)
221 assert list(temp.values()) == [pos for key, pos in mapping]
222
223 def test_iterkeys():
224 temp = SortedDict()
225 with pytest.raises(AttributeError):
226 temp.iterkeys
227
228 def test_notgiven():
229 assert repr(SortedDict._SortedDict__not_given) == '<not-given>'
230
231 def test_pop():
232 mapping = [(val, pos) for pos, val in enumerate(string.ascii_lowercase)]
233 temp = SortedDict(mapping)
234 assert temp.pop('a') == 0
235 assert temp.pop('a', -1) == -1
236
237 def test_pop2():
238 mapping = [(val, pos) for pos, val in enumerate(string.ascii_lowercase)]
239 temp = SortedDict(mapping)
240 with pytest.raises(KeyError):
241 temp.pop('A')
242
243 def test_popitem():
244 mapping = [(val, pos) for pos, val in enumerate(string.ascii_lowercase)]
245 temp = SortedDict(mapping)
246 assert temp.popitem() == ('z', 25)
247
248 def test_popitem2():
249 temp = SortedDict()
250 with pytest.raises(KeyError):
251 temp.popitem()
252
253 def test_popitem3():
254 mapping = [(val, pos) for pos, val in enumerate(string.ascii_lowercase)]
255 temp = SortedDict(mapping)
256 assert temp.popitem(index=0) == ('a', 0)
257
258 def test_peekitem():
259 mapping = [(val, pos) for pos, val in enumerate(string.ascii_lowercase)]
260 temp = SortedDict(mapping)
261 assert temp.peekitem() == ('z', 25)
262 assert temp.peekitem(0) == ('a', 0)
263 assert temp.peekitem(index=4) == ('e', 4)
264
265 def test_peekitem2():
266 mapping = [(val, pos) for pos, val in enumerate(string.ascii_lowercase)]
267 temp = SortedDict(mapping)
268 with pytest.raises(IndexError):
269 temp.peekitem(index=100)
270
271 def test_setdefault():
272 mapping = [(val, pos) for pos, val in enumerate(string.ascii_lowercase)]
273 temp = SortedDict(mapping)
274 assert temp.setdefault('a', -1) == 0
275 assert temp['a'] == 0
276 assert temp.setdefault('A', -1) == -1
277
278 def test_update():
279 mapping = [(val, pos) for pos, val in enumerate(string.ascii_lowercase)]
280 temp = SortedDict()
281 temp.update()
282 temp.update(mapping)
283 temp.update(dict(mapping))
284 temp.update(mapping[5:7])
285 assert list(temp.items()) == mapping
286
287 def test_update2():
288 mapping = [(val, pos) for pos, val in enumerate(string.ascii_lowercase)]
289 temp = SortedDict()
290 temp.update(**dict(mapping))
291 assert list(temp.items()) == mapping
292
293 def test_repr():
294 temp = SortedDict({'alice': 3, 'bob': 1, 'carol': 2, 'dave': 4})
295 assert repr(temp) == "SortedDict({'alice': 3, 'bob': 1, 'carol': 2, 'dave': 4})"
296
297 class Identity(object):
298 def __call__(self, value):
299 return value
300 def __repr__(self):
301 return 'identity'
302
303 def test_repr_recursion():
304 temp = SortedDict(Identity(), {'alice': 3, 'bob': 1, 'carol': 2, 'dave': 4})
305 temp['bob'] = temp
306 assert repr(temp) == "SortedDict(identity, {'alice': 3, 'bob': ..., 'carol': 2, 'dave': 4})"
307
308 def test_repr_subclass():
309 class CustomSortedDict(SortedDict):
310 pass
311 temp = CustomSortedDict({'alice': 3, 'bob': 1, 'carol': 2, 'dave': 4})
312 assert repr(temp) == "CustomSortedDict({'alice': 3, 'bob': 1, 'carol': 2, 'dave': 4})"
313
314 def test_index():
315 mapping = [(val, pos) for pos, val in enumerate(string.ascii_lowercase)]
316 temp = SortedDict(mapping)
317 assert temp.index('a') == 0
318 assert temp.index('f', 3, -3) == 5
319
320 def test_iloc():
321 with warnings.catch_warnings():
322 warnings.simplefilter('ignore', category=DeprecationWarning)
323 mapping = [(val, pos) for pos, val in enumerate(string.ascii_lowercase)]
324 temp = SortedDict(mapping)
325 assert len(temp.iloc) == 26
326 assert temp.iloc[0] == 'a'
327 assert temp.iloc[-1] == 'z'
328 assert temp.iloc[-3:] == ['x', 'y', 'z']
329 del temp.iloc[0]
330 assert temp.iloc[0] == 'b'
331 del temp.iloc[-3:]
332 assert temp.iloc[-1] == 'w'
333
334 def test_index_key():
335 temp = SortedDict(negate, ((val, val) for val in range(100)))
336 temp._reset(7)
337 assert all(temp.index(val) == (99 - val) for val in range(100))
338
339 def test_bisect():
340 mapping = [(val, pos) for pos, val in enumerate(string.ascii_lowercase)]
341 temp = SortedDict(mapping)
342 assert temp.bisect_left('a') == 0
343 assert temp.bisect_right('f') == 6
344 assert temp.bisect('f') == 6
345
346 def test_bisect_key():
347 temp = SortedDict(modulo, ((val, val) for val in range(100)))
348 temp._reset(7)
349 assert all(temp.bisect(val) == ((val % 10) + 1) * 10 for val in range(100))
350 assert all(temp.bisect_right(val) == ((val % 10) + 1) * 10 for val in range(100))
351 assert all(temp.bisect_left(val) == (val % 10) * 10 for val in range(100))
352
353 def test_bisect_key2():
354 temp = SortedDict(modulo, ((val, val) for val in range(100)))
355 temp._reset(7)
356 assert all(temp.bisect_key(val) == ((val % 10) + 1) * 10 for val in range(10))
357 assert all(temp.bisect_key_right(val) == ((val % 10) + 1) * 10 for val in range(10))
358 assert all(temp.bisect_key_left(val) == (val % 10) * 10 for val in range(10))
359
360 def test_keysview():
361 mapping = [(val, pos) for pos, val in enumerate(string.ascii_lowercase)]
362 temp = SortedDict(mapping[:13])
363 keys = temp.keys()
364
365 assert len(keys) == 13
366 assert 'a' in keys
367 assert list(keys) == [val for val, pos in mapping[:13]]
368 assert keys[0] == 'a'
369 assert list(reversed(keys)) == list(reversed(string.ascii_lowercase[:13]))
370 assert keys.index('f') == 5
371 assert keys.count('m') == 1
372 assert keys.count('0') == 0
373 assert keys.isdisjoint(['1', '2', '3'])
374
375 temp.update(mapping[13:])
376
377 assert len(keys) == 26
378 assert 'z' in keys
379 assert list(keys) == [val for val, pos in mapping]
380
381 that = dict(mapping)
382
383 that_keys = get_keysview(that)
384
385 assert keys == that_keys
386 assert not (keys != that_keys)
387 assert not (keys < that_keys)
388 assert not (keys > that_keys)
389 assert keys <= that_keys
390 assert keys >= that_keys
391
392 assert list(keys & that_keys) == [val for val, pos in mapping]
393 assert list(keys | that_keys) == [val for val, pos in mapping]
394 assert list(keys - that_keys) == []
395 assert list(keys ^ that_keys) == []
396
397 keys = SortedDict(mapping[:2]).keys()
398 assert repr(keys) == "SortedKeysView(SortedDict({'a': 0, 'b': 1}))"
399
400 def test_valuesview():
401 mapping = [(val, pos) for pos, val in enumerate(string.ascii_lowercase)]
402 temp = SortedDict(mapping[:13])
403 values = temp.values()
404
405 assert len(values) == 13
406 assert 0 in values
407 assert list(values) == [pos for val, pos in mapping[:13]]
408 assert values[0] == 0
409 assert values[-3:] == [10, 11, 12]
410 assert list(reversed(values)) == list(reversed(range(13)))
411 assert values.index(5) == 5
412 assert values.count(10) == 1
413
414 temp.update(mapping[13:])
415
416 assert len(values) == 26
417 assert 25 in values
418 assert list(values) == [pos for val, pos in mapping]
419
420 values = SortedDict(mapping[:2]).values()
421 assert repr(values) == "SortedValuesView(SortedDict({'a': 0, 'b': 1}))"
422
423 def test_values_view_index():
424 mapping = [(val, pos) for pos, val in enumerate(string.ascii_lowercase)]
425 temp = SortedDict(mapping[:13])
426 values = temp.values()
427 with pytest.raises(ValueError):
428 values.index(100)
429
430 def test_itemsview():
431 mapping = [(val, pos) for pos, val in enumerate(string.ascii_lowercase)]
432 temp = SortedDict(mapping[:13])
433 items = temp.items()
434
435 assert len(items) == 13
436 assert ('a', 0) in items
437 assert list(items) == mapping[:13]
438 assert items[0] == ('a', 0)
439 assert items[-3:] == [('k', 10), ('l', 11), ('m', 12)]
440 assert list(reversed(items)) == list(reversed(mapping[:13]))
441 assert items.index(('f', 5)) == 5
442 assert items.count(('m', 12)) == 1
443 assert items.isdisjoint([('0', 26), ('1', 27)])
444 assert not items.isdisjoint([('a', 0), ('b', 1)])
445
446 temp.update(mapping[13:])
447
448 assert len(items) == 26
449 assert ('z', 25) in items
450 assert list(items) == mapping
451
452 that = dict(mapping)
453 that_items = get_itemsview(that)
454
455 assert items == that_items
456 assert not (items != that_items)
457 assert not (items < that_items)
458 assert not (items > that_items)
459 assert items <= that_items
460 assert items >= that_items
461
462 assert list(items & that_items) == mapping
463 assert list(items | that_items) == mapping
464 assert list(items - that_items) == []
465 assert list(items ^ that_items) == []
466
467 items = SortedDict(mapping[:2]).items()
468 assert repr(items) == "SortedItemsView(SortedDict({'a': 0, 'b': 1}))"
469
470 def test_items_view_index():
471 mapping = [(val, pos) for pos, val in enumerate(string.ascii_lowercase)]
472 temp = SortedDict(mapping[:13])
473 items = temp.items()
474 with pytest.raises(ValueError):
475 items.index(('f', 100))
476
477 def test_pickle():
478 import pickle
479 alpha = SortedDict(negate, zip(range(10000), range(10000)))
480 alpha._reset(500)
481 beta = pickle.loads(pickle.dumps(alpha))
482 assert alpha == beta
483 assert alpha._key == beta._key
484
485 if platform.python_implementation() == 'CPython':
486 def test_ref_counts():
487 start_count = len(gc.get_objects())
488 temp = SortedDict()
489 init_count = len(gc.get_objects())
490 assert init_count > start_count
491 del temp
492 del_count = len(gc.get_objects())
493 assert start_count == del_count
494
495 class CustomOr:
496 def __or__(self, other):
497 return NotImplemented
498
499 def __ror__(self, other):
500 return self
501
502 def test_or():
503 mapping = [(val, pos) for pos, val in enumerate(string.ascii_lowercase)]
504 temp1 = SortedDict(mapping[:13])
505 temp2 = SortedDict(mapping[13:])
506 temp3 = temp1 | temp2
507 assert temp3 == dict(mapping)
508
509 def test_or_not_implemented():
510 SortedDict() | CustomOr()
511
512 def test_ror():
513 mapping = [(val, pos) for pos, val in enumerate(string.ascii_lowercase)]
514 temp1 = dict(mapping[:13])
515 temp2 = SortedDict(mapping[13:])
516 temp3 = temp1 | temp2
517 assert temp3 == dict(mapping)
518
519 def test_ror_not_implemented():
520 with pytest.raises(TypeError):
521 CustomOr() | SortedDict()
522
523 def test_ior():
524 mapping = [(val, pos) for pos, val in enumerate(string.ascii_lowercase)]
525 temp1 = SortedDict(mapping[:13])
526 temp2 = SortedDict(mapping[13:])
527 temp1 |= temp2
528 assert temp1 == dict(mapping)