Debian Codegraph @

tests/test_coverage_sortedkeylist_modulo.py

raw
1 # -*- coding: utf-8 -*-
2
3 from sys import hexversion
4
5 import random
6 from .context import sortedcontainers
7 from sortedcontainers import SortedList, SortedKeyList
8 from itertools import chain, repeat
9 import pytest
10
11 if hexversion < 0x03000000:
12 from itertools import izip as zip
13 range = xrange
14
15 def modulo(val):
16 return val % 10
17
18 def test_init():
19 slt = SortedKeyList(key=modulo)
20 assert slt.key == modulo
21 slt._check()
22
23 slt = SortedKeyList(key=modulo)
24 slt._reset(10000)
25 assert slt._load == 10000
26 slt._check()
27
28 slt = SortedKeyList(range(10000), key=modulo)
29 assert all(tup[0] == tup[1] for tup in zip(slt, sorted(range(10000), key=modulo)))
30
31 slt.clear()
32 assert slt._len == 0
33 assert slt._maxes == []
34 assert slt._lists == []
35
36 assert isinstance(slt, SortedList)
37 assert isinstance(slt, SortedKeyList)
38
39 slt._check()
40
41 def test_new():
42 slt = SortedList(iter(range(1000)), key=modulo)
43 assert slt == sorted(range(1000), key=modulo)
44 slt._check()
45
46 assert isinstance(slt, SortedList)
47 assert isinstance(slt, SortedKeyList)
48 assert type(slt) == SortedKeyList
49
50 slt = SortedKeyList(iter(range(1000)), key=modulo)
51 assert slt == sorted(range(1000), key=modulo)
52 slt._check()
53
54 assert isinstance(slt, SortedList)
55 assert isinstance(slt, SortedKeyList)
56 assert type(slt) == SortedKeyList
57
58 def test_new_error():
59 class SortedListPlus(SortedList):
60 pass
61 with pytest.raises(TypeError):
62 SortedListPlus(key=modulo)
63
64 def test_key():
65 slt = SortedKeyList(range(10000), key=lambda val: val % 10)
66 slt._check()
67
68 values = sorted(range(10000), key=lambda val: (val % 10, val))
69 assert slt == values
70 assert all(val in slt for val in range(10000))
71
72 def test_key2():
73 class Incomparable:
74 pass
75 a = Incomparable()
76 b = Incomparable()
77 slt = SortedKeyList(key=lambda val: 1)
78 slt.add(a)
79 slt.add(b)
80 assert slt == [a, b]
81
82 def test_add():
83 random.seed(0)
84 slt = SortedKeyList(key=modulo)
85 for val in range(1000):
86 slt.add(val)
87 slt._check()
88
89 slt = SortedKeyList(key=modulo)
90 for val in range(1000, 0, -1):
91 slt.add(val)
92 slt._check()
93
94 slt = SortedKeyList(key=modulo)
95 for val in range(1000):
96 slt.add(random.random())
97 slt._check()
98
99 def test_update():
100 slt = SortedKeyList(key=modulo)
101
102 slt.update(range(1000))
103 assert all(tup[0] == tup[1] for tup in zip(slt, sorted(range(1000), key=modulo)))
104 assert len(slt) == 1000
105 slt._check()
106
107 slt.update(range(10000))
108 assert len(slt) == 11000
109 slt._check()
110
111 def test_update_order_consistency():
112 setup = [10, 20, 30]
113 slt1 = SortedKeyList(setup, key=modulo)
114 slt2 = SortedKeyList(setup, key=modulo)
115 addition = [40, 50, 60]
116 for value in addition:
117 slt1.add(value)
118 slt2.update(addition)
119 assert slt1 == slt2
120
121 def test_contains():
122 slt = SortedKeyList(key=modulo)
123 slt._reset(7)
124
125 assert 0 not in slt
126
127 slt.update(range(100))
128
129 for val in range(100):
130 assert val in slt
131
132 assert 100 not in slt
133
134 slt._check()
135
136 slt = SortedKeyList(range(100), key=modulo)
137 slt._reset(4)
138 assert all(val not in slt for val in range(100, 200))
139
140 def test_discard():
141 slt = SortedKeyList(key=modulo)
142
143 assert slt.discard(0) == None
144 assert len(slt) == 0
145 slt._check()
146
147 slt = SortedKeyList([1, 2, 2, 2, 3, 3, 5], key=modulo)
148 slt._reset(4)
149
150 slt.discard(6)
151 slt._check()
152 slt.discard(4)
153 slt._check()
154 slt.discard(2)
155 slt._check()
156 slt.discard(11)
157 slt.discard(12)
158 slt.discard(13)
159 slt.discard(15)
160
161 assert all(tup[0] == tup[1] for tup in zip(slt, [1, 2, 2, 3, 3, 5]))
162
163 def test_remove():
164 slt = SortedKeyList(key=modulo)
165
166 assert slt.discard(0) == None
167 assert len(slt) == 0
168 slt._check()
169
170 slt = SortedKeyList([1, 2, 2, 2, 3, 3, 5], key=modulo)
171 slt._reset(4)
172
173 slt.remove(2)
174 slt._check()
175
176 assert all(tup[0] == tup[1] for tup in zip(slt, [1, 2, 2, 3, 3, 5]))
177
178 def test_remove_valueerror1():
179 slt = SortedKeyList(key=modulo)
180 with pytest.raises(ValueError):
181 slt.remove(0)
182
183 def test_remove_valueerror2():
184 slt = SortedKeyList(range(100), key=modulo)
185 slt._reset(10)
186 with pytest.raises(ValueError):
187 slt.remove(100)
188
189 def test_remove_valueerror3():
190 slt = SortedKeyList([1, 2, 2, 2, 3, 3, 5], key=modulo)
191 with pytest.raises(ValueError):
192 slt.remove(4)
193
194 def test_remove_valueerror4():
195 slt = SortedKeyList([1, 1, 1, 2, 2, 2], key=modulo)
196 with pytest.raises(ValueError):
197 slt.remove(13)
198
199 def test_remove_valueerror5():
200 slt = SortedKeyList([1, 1, 1, 2, 2, 2], key=modulo)
201 with pytest.raises(ValueError):
202 slt.remove(12)
203
204 def test_delete():
205 slt = SortedKeyList(range(20), key=modulo)
206 slt._reset(4)
207 slt._check()
208 for val in range(20):
209 slt.remove(val)
210 slt._check()
211 assert len(slt) == 0
212 assert slt._maxes == []
213 assert slt._lists == []
214
215 def test_getitem():
216 random.seed(0)
217 slt = SortedKeyList(key=modulo)
218 slt._reset(17)
219
220 slt.add(5)
221 slt._build_index()
222 slt._check()
223 slt.clear()
224
225 lst = list(random.random() for rpt in range(100))
226 slt.update(lst)
227 lst.sort(key=modulo)
228
229 assert all(slt[idx] == lst[idx] for idx in range(100))
230 assert all(slt[idx - 99] == lst[idx - 99] for idx in range(100))
231
232 def test_getitem_slice():
233 random.seed(0)
234 slt = SortedKeyList(key=modulo)
235 slt._reset(17)
236
237 lst = list()
238
239 for rpt in range(100):
240 val = random.random()
241 slt.add(val)
242 lst.append(val)
243
244 lst.sort(key=modulo)
245
246 assert all(slt[start:] == lst[start:]
247 for start in [-75, -25, 0, 25, 75])
248
249 assert all(slt[:stop] == lst[:stop]
250 for stop in [-75, -25, 0, 25, 75])
251
252 assert all(slt[::step] == lst[::step]
253 for step in [-5, -1, 1, 5])
254
255 assert all(slt[start:stop] == lst[start:stop]
256 for start in [-75, -25, 0, 25, 75]
257 for stop in [-75, -25, 0, 25, 75])
258
259 assert all(slt[:stop:step] == lst[:stop:step]
260 for stop in [-75, -25, 0, 25, 75]
261 for step in [-5, -1, 1, 5])
262
263 assert all(slt[start::step] == lst[start::step]
264 for start in [-75, -25, 0, 25, 75]
265 for step in [-5, -1, 1, 5])
266
267 assert all(slt[start:stop:step] == lst[start:stop:step]
268 for start in [-75, -25, 0, 25, 75]
269 for stop in [-75, -25, 0, 25, 75]
270 for step in [-5, -1, 1, 5])
271
272 def test_getitem_slice_big():
273 slt = SortedKeyList(range(4), key=modulo)
274 lst = sorted(range(4), key=modulo)
275
276 itr = ((start, stop, step)
277 for start in [-6, -4, -2, 0, 2, 4, 6]
278 for stop in [-6, -4, -2, 0, 2, 4, 6]
279 for step in [-3, -2, -1, 1, 2, 3])
280
281 for start, stop, step in itr:
282 assert slt[start:stop:step] == lst[start:stop:step]
283
284 def test_getitem_slicezero():
285 slt = SortedKeyList(range(100), key=modulo)
286 slt._reset(17)
287 with pytest.raises(ValueError):
288 slt[::0]
289
290 def test_getitem_indexerror1():
291 slt = SortedKeyList(key=modulo)
292 with pytest.raises(IndexError):
293 slt[5]
294
295 def test_getitem_indexerror2():
296 slt = SortedKeyList(range(100), key=modulo)
297 with pytest.raises(IndexError):
298 slt[200]
299
300 def test_getitem_indexerror3():
301 slt = SortedKeyList(range(100), key=modulo)
302 with pytest.raises(IndexError):
303 slt[-101]
304
305 def test_delitem():
306 random.seed(0)
307
308 slt = SortedKeyList(range(100), key=modulo)
309 slt._reset(17)
310 while len(slt) > 0:
311 del slt[random.randrange(len(slt))]
312 slt._check()
313
314 slt = SortedKeyList(range(100), key=modulo)
315 slt._reset(17)
316 del slt[:]
317 assert len(slt) == 0
318 slt._check()
319
320 def test_delitem_slice():
321 slt = SortedKeyList(range(100), key=modulo)
322 slt._reset(17)
323 del slt[10:40:1]
324 del slt[10:40:-1]
325 del slt[10:40:2]
326 del slt[10:40:-2]
327
328 def test_iter():
329 slt = SortedKeyList(range(10000), key=modulo)
330 itr = iter(slt)
331 assert all(tup[0] == tup[1] for tup in zip(sorted(range(10000), key=modulo), itr))
332
333 def test_reversed():
334 slt = SortedKeyList(range(10000), key=modulo)
335 rev = reversed(slt)
336 assert all(tup[0] == tup[1] for tup in zip(reversed(sorted(range(10000), key=modulo)), rev))
337
338 def test_reverse():
339 slt = SortedKeyList(range(10000), key=modulo)
340 with pytest.raises(NotImplementedError):
341 slt.reverse()
342
343 def test_islice():
344 sl = SortedKeyList(key=modulo)
345 sl._reset(7)
346
347 assert [] == list(sl.islice())
348
349 values = sorted(range(100), key=modulo)
350 sl.update(values)
351
352 for start in range(53):
353 for stop in range(53):
354 assert list(sl.islice(start, stop)) == values[start:stop]
355
356 for start in range(53):
357 for stop in range(53):
358 assert list(sl.islice(start, stop, reverse=True)) == values[start:stop][::-1]
359
360 for start in range(53):
361 assert list(sl.islice(start=start)) == values[start:]
362 assert list(sl.islice(start=start, reverse=True)) == values[start:][::-1]
363
364 for stop in range(53):
365 assert list(sl.islice(stop=stop)) == values[:stop]
366 assert list(sl.islice(stop=stop, reverse=True)) == values[:stop][::-1]
367
368 def test_irange():
369 values = sorted(range(100), key=modulo)
370
371 for load in range(5, 16):
372 slt = SortedKeyList(range(100), key=modulo)
373 slt._reset(load)
374
375 for start in range(10):
376 for end in range(start, 10):
377 temp = list(slt.irange(start, end))
378 assert temp == values[(start * 10):((end + 1) * 10)]
379
380 temp = list(slt.irange(start, end, reverse=True))
381 assert temp == values[(start * 10):((end + 1) * 10)][::-1]
382
383 for start in range(10):
384 for end in range(start, 10):
385 temp = list(slt.irange(start, end, inclusive=(True, False)))
386 assert temp == values[(start * 10):(end * 10)]
387
388 for start in range(10):
389 for end in range(start, 10):
390 temp = list(slt.irange(start, end, (False, True)))
391 assert temp == values[((start + 1) * 10):((end + 1) * 10)]
392
393 for start in range(10):
394 for end in range(start, 10):
395 temp = list(slt.irange(start, end, inclusive=(False, False)))
396 assert temp == values[((start + 1) * 10):(end * 10)]
397
398 for start in range(10):
399 temp = list(slt.irange(minimum=start))
400 assert temp == values[(start * 10):]
401
402 for end in range(10):
403 temp = list(slt.irange(maximum=end))
404 assert temp == values[:(end + 1) * 10]
405
406 def test_irange_key():
407 values = sorted(range(100), key=modulo)
408
409 for load in range(5, 16):
410 slt = SortedKeyList(range(100), key=modulo)
411 slt._reset(load)
412
413 for start in range(10):
414 for end in range(start, 10):
415 temp = list(slt.irange_key(start, end))
416 assert temp == values[(start * 10):((end + 1) * 10)]
417
418 temp = list(slt.irange_key(start, end, reverse=True))
419 assert temp == values[(start * 10):((end + 1) * 10)][::-1]
420
421 for start in range(10):
422 for end in range(start, 10):
423 temp = list(slt.irange_key(start, end, inclusive=(True, False)))
424 assert temp == values[(start * 10):(end * 10)]
425
426 for start in range(10):
427 for end in range(start, 10):
428 temp = list(slt.irange_key(start, end, (False, True)))
429 assert temp == values[((start + 1) * 10):((end + 1) * 10)]
430
431 for start in range(10):
432 for end in range(start, 10):
433 temp = list(slt.irange_key(start, end, inclusive=(False, False)))
434 assert temp == values[((start + 1) * 10):(end * 10)]
435
436 for start in range(10):
437 temp = list(slt.irange_key(min_key=start))
438 assert temp == values[(start * 10):]
439
440 for end in range(10):
441 temp = list(slt.irange_key(max_key=end))
442 assert temp == values[:(end + 1) * 10]
443
444 def test_len():
445 slt = SortedKeyList(key=modulo)
446
447 for val in range(10000):
448 slt.add(val)
449 assert len(slt) == (val + 1)
450
451 def test_bisect_left():
452 slt = SortedKeyList(key=modulo)
453 assert slt.bisect_left(0) == 0
454 slt = SortedKeyList(range(100), key=modulo)
455 slt._reset(17)
456 slt.update(range(100))
457 slt._check()
458 assert slt.bisect_left(50) == 0
459 assert slt.bisect_left(0) == 0
460
461 def test_bisect():
462 slt = SortedKeyList(key=modulo)
463 assert slt.bisect(10) == 0
464 slt = SortedKeyList(range(100), key=modulo)
465 slt._reset(17)
466 slt.update(range(100))
467 slt._check()
468 assert slt.bisect(10) == 20
469 assert slt.bisect(0) == 20
470
471 def test_bisect_right():
472 slt = SortedKeyList(key=modulo)
473 assert slt.bisect_right(10) == 0
474 slt = SortedKeyList(range(100), key=modulo)
475 slt._reset(17)
476 slt.update(range(100))
477 slt._check()
478 assert slt.bisect_right(10) == 20
479 assert slt.bisect_right(0) == 20
480
481 def test_bisect_key_left():
482 slt = SortedKeyList(key=modulo)
483 assert slt.bisect_key_left(10) == 0
484 slt = SortedKeyList(range(100), key=modulo)
485 slt._reset(17)
486 slt.update(range(100))
487 slt._check()
488 assert slt.bisect_key_left(0) == 0
489 assert slt.bisect_key_left(5) == 100
490 assert slt.bisect_key_left(10) == 200
491
492 def test_bisect_key_right():
493 slt = SortedKeyList(key=modulo)
494 assert slt.bisect_key_right(0) == 0
495 slt = SortedKeyList(range(100), key=modulo)
496 slt._reset(17)
497 slt.update(range(100))
498 slt._check()
499 assert slt.bisect_key_right(0) == 20
500 assert slt.bisect_key_right(5) == 120
501 assert slt.bisect_key_right(10) == 200
502
503 def test_bisect_key():
504 slt = SortedKeyList(key=modulo)
505 assert slt.bisect_key(0) == 0
506 slt = SortedKeyList(range(100), key=modulo)
507 slt._reset(17)
508 slt.update(range(100))
509 slt._check()
510 assert slt.bisect_key(0) == 20
511 assert slt.bisect_key(5) == 120
512 assert slt.bisect_key(10) == 200
513
514 def test_copy():
515 slt = SortedKeyList(range(100), key=modulo)
516 slt._reset(7)
517 two = slt.copy()
518 slt.add(100)
519 assert len(slt) == 101
520 assert len(two) == 100
521
522 def test_copy_copy():
523 import copy
524 slt = SortedKeyList(range(100), key=modulo)
525 slt._reset(7)
526 two = copy.copy(slt)
527 slt.add(100)
528 assert len(slt) == 101
529 assert len(two) == 100
530
531 def test_count():
532 slt = SortedKeyList(key=modulo)
533 slt._reset(7)
534
535 assert slt.count(0) == 0
536
537 for iii in range(100):
538 for jjj in range(iii):
539 slt.add(iii)
540 slt._check()
541
542 for iii in range(100):
543 assert slt.count(iii) == iii
544
545 slt = SortedKeyList(range(8), key=modulo)
546 assert slt.count(9) == 0
547
548 def test_pop():
549 slt = SortedKeyList(range(10), key=modulo)
550 slt._reset(4)
551 slt._check()
552 assert slt.pop() == 9
553 slt._check()
554 assert slt.pop(0) == 0
555 slt._check()
556 assert slt.pop(-2) == 7
557 slt._check()
558 assert slt.pop(4) == 5
559 slt._check()
560
561 def test_pop_indexerror1():
562 slt = SortedKeyList(range(10), key=modulo)
563 slt._reset(4)
564 with pytest.raises(IndexError):
565 slt.pop(-11)
566
567 def test_pop_indexerror2():
568 slt = SortedKeyList(range(10), key=modulo)
569 slt._reset(4)
570 with pytest.raises(IndexError):
571 slt.pop(10)
572
573 def test_index():
574 slt = SortedKeyList(range(100), key=modulo)
575 slt._reset(7)
576
577 for pos, val in enumerate(sorted(range(100), key=modulo)):
578 assert val == slt.index(pos)
579
580 assert slt.index(9, 0, 1000) == 90
581
582 slt = SortedKeyList((0 for rpt in range(100)), key=modulo)
583 slt._reset(7)
584
585 for start in range(100):
586 for stop in range(start, 100):
587 assert slt.index(0, start, stop + 1) == start
588
589 for start in range(100):
590 assert slt.index(0, -(100 - start)) == start
591
592 assert slt.index(0, -1000) == 0
593
594 def test_index_valueerror1():
595 slt = SortedKeyList([0] * 10, key=modulo)
596 slt._reset(4)
597 with pytest.raises(ValueError):
598 slt.index(0, 10)
599
600 def test_index_valueerror2():
601 slt = SortedKeyList([0] * 10, key=modulo)
602 slt._reset(4)
603 with pytest.raises(ValueError):
604 slt.index(0, 0, -10)
605
606 def test_index_valueerror3():
607 slt = SortedKeyList([0] * 10, key=modulo)
608 slt._reset(4)
609 with pytest.raises(ValueError):
610 slt.index(0, 7, 3)
611
612 def test_index_valueerror4():
613 slt = SortedKeyList([0] * 10, key=modulo)
614 slt._reset(4)
615 with pytest.raises(ValueError):
616 slt.index(1)
617
618 def test_index_valueerror5():
619 slt = SortedKeyList(key=modulo)
620 with pytest.raises(ValueError):
621 slt.index(1)
622
623 def test_index_valueerror6():
624 slt = SortedKeyList(range(100), key=modulo)
625 slt._reset(4)
626 with pytest.raises(ValueError):
627 slt.index(91, 0, 15)
628
629 def test_index_valueerror7():
630 slt = SortedKeyList([0] * 10 + [1] * 10 + [2] * 10, key=modulo)
631 slt._reset(4)
632 with pytest.raises(ValueError):
633 slt.index(1, 0, 10)
634
635 def test_index_valueerror8():
636 slt = SortedKeyList(range(10), key=modulo)
637 slt._reset(4)
638 with pytest.raises(ValueError):
639 slt.index(4, 5)
640
641 def test_index_valueerror9():
642 slt = SortedKeyList(key=modulo)
643 slt._reset(4)
644 with pytest.raises(ValueError):
645 slt.index(5)
646
647 def test_index_valueerror10():
648 slt = SortedKeyList(range(10), key=modulo)
649 slt._reset(4)
650 with pytest.raises(ValueError):
651 slt.index(19)
652
653 def test_mul():
654 this = SortedKeyList(range(10), key=modulo)
655 this._reset(4)
656 that = this * 5
657 this._check()
658 that._check()
659 assert this == sorted(range(10), key=modulo)
660 assert that == sorted(list(range(10)) * 5, key=modulo)
661 assert this != that
662
663 def test_imul():
664 this = SortedKeyList(range(10), key=modulo)
665 this._reset(4)
666 this *= 5
667 this._check()
668 assert this == sorted(list(range(10)) * 5, key=modulo)
669
670 def test_op_add():
671 this = SortedKeyList(range(10), key=modulo)
672 this._reset(4)
673 assert (this + this + this) == (this * 3)
674
675 that = SortedKeyList(range(10), key=modulo)
676 that._reset(4)
677 that += that
678 that += that
679 assert that == (this * 4)
680
681 def test_eq():
682 this = SortedKeyList(range(10), key=modulo)
683 this._reset(4)
684 assert this == list(range(10))
685 assert this == tuple(range(10))
686 assert not (this == list(range(9)))
687
688 def test_ne():
689 this = SortedKeyList(range(10, 20), key=modulo)
690 this._reset(4)
691 assert this != list(range(11, 21))
692 assert this != tuple(range(10, 21))
693 assert this != [0, 1, 2, 3, 3, 5, 6, 7, 8, 9]
694 assert this != (val for val in range(10))
695 assert this != set()
696
697 def test_lt():
698 this = SortedKeyList(range(10, 15), key=modulo)
699 this._reset(4)
700 assert this < [10, 11, 13, 13, 14]
701 assert this < [10, 11, 12, 13, 14, 15]
702 assert this < [11]
703
704 def test_le():
705 this = SortedKeyList(range(10, 15), key=modulo)
706 this._reset(4)
707 assert this <= [10, 11, 12, 13, 14]
708 assert this <= [10, 11, 12, 13, 14, 15]
709 assert this <= [10, 11, 13, 13, 14]
710 assert this <= [11]
711
712 def test_gt():
713 this = SortedKeyList(range(10, 15), key=modulo)
714 this._reset(4)
715 assert this > [10, 11, 11, 13, 14]
716 assert this > [10, 11, 12, 13]
717 assert this > [9]
718
719 def test_ge():
720 this = SortedKeyList(range(10, 15), key=modulo)
721 this._reset(4)
722 assert this >= [10, 11, 12, 13, 14]
723 assert this >= [10, 11, 12, 13]
724 assert this >= [10, 11, 11, 13, 14]
725 assert this >= [9]
726
727 def test_repr():
728 this = SortedKeyList(range(10), key=modulo)
729 this._reset(4)
730 assert repr(this).startswith('SortedKeyList([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], key=<function modulo at ')
731
732 def test_repr_recursion():
733 this = SortedKeyList([[1], [2], [3], [4]], key=lambda val: val)
734 this._lists[-1].append(this)
735 assert repr(this).startswith('SortedKeyList([[1], [2], [3], [4], ...], key=<function ')
736
737 def test_repr_subclass():
738 class CustomSortedKeyList(SortedKeyList):
739 pass
740 this = CustomSortedKeyList(range(10), key=modulo)
741 this._reset(4)
742 assert repr(this).startswith('CustomSortedKeyList([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], key=<function modulo at ')
743
744 def test_check():
745 slt = SortedKeyList(range(10), key=modulo)
746 slt._reset(4)
747 slt._len = 5
748 with pytest.raises(AssertionError):
749 slt._check()