Debian Codegraph @

tests/test_coverage_sortedlist.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
8 from itertools import chain
9 import pytest
10
11 if hexversion < 0x03000000:
12 from itertools import izip as zip
13 range = xrange
14
15 def test_init():
16 slt = SortedList()
17 assert slt.key is None
18 slt._check()
19
20 slt = SortedList()
21 slt._reset(10000)
22 assert slt._load == 10000
23 slt._check()
24
25 slt = SortedList(range(10000))
26 assert all(tup[0] == tup[1] for tup in zip(slt, range(10000)))
27
28 slt.clear()
29 assert slt._len == 0
30 assert slt._maxes == []
31 assert slt._lists == []
32 slt._check()
33
34 def test_add():
35 random.seed(0)
36 slt = SortedList()
37 for val in range(1000):
38 slt.add(val)
39 slt._check()
40
41 slt = SortedList()
42 for val in range(1000, 0, -1):
43 slt.add(val)
44 slt._check()
45
46 slt = SortedList()
47 for val in range(1000):
48 slt.add(random.random())
49 slt._check()
50
51 def test_update():
52 slt = SortedList()
53
54 slt.update(range(1000))
55 assert len(slt) == 1000
56 slt._check()
57
58 slt.update(range(100))
59 assert len(slt) == 1100
60 slt._check()
61
62 slt.update(range(10000))
63 assert len(slt) == 11100
64 slt._check()
65
66 values = sorted(chain(range(1000), range(100), range(10000)))
67 assert all(tup[0] == tup[1] for tup in zip(slt, values))
68
69 def test_contains():
70 slt = SortedList()
71 assert 0 not in slt
72
73 slt.update(range(10000))
74
75 for val in range(10000):
76 assert val in slt
77
78 assert 10000 not in slt
79
80 slt._check()
81
82 def test_discard():
83 slt = SortedList()
84
85 assert slt.discard(0) == None
86 assert len(slt) == 0
87 slt._check()
88
89 slt = SortedList([1, 2, 2, 2, 3, 3, 5])
90 slt._reset(4)
91
92 slt.discard(6)
93 slt._check()
94 slt.discard(4)
95 slt._check()
96 slt.discard(2)
97 slt._check()
98
99 assert all(tup[0] == tup[1] for tup in zip(slt, [1, 2, 2, 3, 3, 5]))
100
101 def test_remove():
102 slt = SortedList()
103
104 assert slt.discard(0) == None
105 assert len(slt) == 0
106 slt._check()
107
108 slt = SortedList([1, 2, 2, 2, 3, 3, 5])
109 slt._reset(4)
110
111 slt.remove(2)
112 slt._check()
113
114 assert all(tup[0] == tup[1] for tup in zip(slt, [1, 2, 2, 3, 3, 5]))
115
116 def test_remove_valueerror1():
117 slt = SortedList()
118 with pytest.raises(ValueError):
119 slt.remove(0)
120
121 def test_remove_valueerror2():
122 slt = SortedList(range(100))
123 slt._reset(10)
124 with pytest.raises(ValueError):
125 slt.remove(100)
126
127 def test_remove_valueerror3():
128 slt = SortedList([1, 2, 2, 2, 3, 3, 5])
129 with pytest.raises(ValueError):
130 slt.remove(4)
131
132 def test_delete():
133 slt = SortedList(range(20))
134 slt._reset(4)
135 slt._check()
136 for val in range(20):
137 slt.remove(val)
138 slt._check()
139 assert len(slt) == 0
140 assert slt._maxes == []
141 assert slt._lists == []
142
143 def test_getitem():
144 random.seed(0)
145 slt = SortedList()
146 slt._reset(17)
147
148 lst = list()
149
150 for rpt in range(100):
151 val = random.random()
152 slt.add(val)
153 lst.append(val)
154
155 lst.sort()
156
157 assert all(slt[idx] == lst[idx] for idx in range(100))
158 assert all(slt[idx - 99] == lst[idx - 99] for idx in range(100))
159
160 def test_getitem_slice():
161 random.seed(0)
162 slt = SortedList()
163 slt._reset(17)
164
165 lst = list()
166
167 for rpt in range(100):
168 val = random.random()
169 slt.add(val)
170 lst.append(val)
171
172 lst.sort()
173
174 assert all(slt[start:] == lst[start:]
175 for start in [-75, -25, 0, 25, 75])
176
177 assert all(slt[:stop] == lst[:stop]
178 for stop in [-75, -25, 0, 25, 75])
179
180 assert all(slt[::step] == lst[::step]
181 for step in [-5, -1, 1, 5])
182
183 assert all(slt[start:stop] == lst[start:stop]
184 for start in [-75, -25, 0, 25, 75]
185 for stop in [-75, -25, 0, 25, 75])
186
187 assert all(slt[:stop:step] == lst[:stop:step]
188 for stop in [-75, -25, 0, 25, 75]
189 for step in [-5, -1, 1, 5])
190
191 assert all(slt[start::step] == lst[start::step]
192 for start in [-75, -25, 0, 25, 75]
193 for step in [-5, -1, 1, 5])
194
195 assert all(slt[start:stop:step] == lst[start:stop:step]
196 for start in [-75, -25, 0, 25, 75]
197 for stop in [-75, -25, 0, 25, 75]
198 for step in [-5, -1, 1, 5])
199
200 def test_getitem_slice_big():
201 slt = SortedList(range(4))
202 lst = list(range(4))
203
204 itr = ((start, stop, step)
205 for start in [-6, -4, -2, 0, 2, 4, 6]
206 for stop in [-6, -4, -2, 0, 2, 4, 6]
207 for step in [-3, -2, -1, 1, 2, 3])
208
209 for start, stop, step in itr:
210 assert slt[start:stop:step] == lst[start:stop:step]
211
212 def test_getitem_slicezero():
213 slt = SortedList(range(100))
214 slt._reset(17)
215 with pytest.raises(ValueError):
216 slt[::0]
217
218 def test_getitem_indexerror1():
219 slt = SortedList()
220 with pytest.raises(IndexError):
221 slt[5]
222
223 def test_getitem_indexerror2():
224 slt = SortedList(range(100))
225 with pytest.raises(IndexError):
226 slt[200]
227
228 def test_getitem_indexerror3():
229 slt = SortedList(range(100))
230 with pytest.raises(IndexError):
231 slt[-101]
232
233 def test_delitem():
234 random.seed(0)
235
236 slt = SortedList(range(100))
237 slt._reset(17)
238 while len(slt) > 0:
239 pos = random.randrange(len(slt))
240 del slt[pos]
241 slt._check()
242
243 slt = SortedList(range(100))
244 slt._reset(17)
245 del slt[:]
246 assert len(slt) == 0
247 slt._check()
248
249 def test_delitem_slice():
250 slt = SortedList(range(100))
251 slt._reset(17)
252 del slt[10:40:1]
253 del slt[10:40:-1]
254 del slt[10:40:2]
255 del slt[10:40:-2]
256
257 def test_iter():
258 slt = SortedList(range(10000))
259 itr = iter(slt)
260 assert all(tup[0] == tup[1] for tup in zip(range(10000), itr))
261
262 def test_reversed():
263 slt = SortedList(range(10000))
264 rev = reversed(slt)
265 assert all(tup[0] == tup[1] for tup in zip(range(9999, -1, -1), rev))
266
267 def test_reverse():
268 slt = SortedList(range(10000))
269 with pytest.raises(NotImplementedError):
270 slt.reverse()
271
272 def test_islice():
273 sl = SortedList()
274 sl._reset(7)
275
276 assert [] == list(sl.islice())
277
278 values = list(range(53))
279 sl.update(values)
280
281 for start in range(53):
282 for stop in range(53):
283 assert list(sl.islice(start, stop)) == values[start:stop]
284
285 for start in range(53):
286 for stop in range(53):
287 assert list(sl.islice(start, stop, reverse=True)) == values[start:stop][::-1]
288
289 for start in range(53):
290 assert list(sl.islice(start=start)) == values[start:]
291 assert list(sl.islice(start=start, reverse=True)) == values[start:][::-1]
292
293 for stop in range(53):
294 assert list(sl.islice(stop=stop)) == values[:stop]
295 assert list(sl.islice(stop=stop, reverse=True)) == values[:stop][::-1]
296
297 def test_irange():
298 sl = SortedList()
299 sl._reset(7)
300
301 assert [] == list(sl.irange())
302
303 values = list(range(53))
304 sl.update(values)
305
306 for start in range(53):
307 for end in range(start, 53):
308 assert list(sl.irange(start, end)) == values[start:(end + 1)]
309 assert list(sl.irange(start, end, reverse=True)) == values[start:(end + 1)][::-1]
310
311 for start in range(53):
312 for end in range(start, 53):
313 assert list(range(start, end)) == list(sl.irange(start, end, (True, False)))
314
315 for start in range(53):
316 for end in range(start, 53):
317 assert list(range(start + 1, end + 1)) == list(sl.irange(start, end, (False, True)))
318
319 for start in range(53):
320 for end in range(start, 53):
321 assert list(range(start + 1, end)) == list(sl.irange(start, end, (False, False)))
322
323 for start in range(53):
324 assert list(range(start, 53)) == list(sl.irange(start))
325
326 for end in range(53):
327 assert list(range(0, end)) == list(sl.irange(None, end, (True, False)))
328
329 assert values == list(sl.irange(inclusive=(False, False)))
330
331 assert [] == list(sl.irange(53))
332 assert values == list(sl.irange(None, 53, (True, False)))
333
334 def test_len():
335 slt = SortedList()
336
337 for val in range(10000):
338 slt.add(val)
339 assert len(slt) == (val + 1)
340
341 def test_bisect_left():
342 slt = SortedList()
343 assert slt.bisect_left(0) == 0
344 slt = SortedList(range(100))
345 slt._reset(17)
346 slt.update(range(100))
347 slt._check()
348 assert slt.bisect_left(50) == 100
349 assert slt.bisect_left(200) == 200
350
351 def test_bisect():
352 slt = SortedList()
353 assert slt.bisect(10) == 0
354 slt = SortedList(range(100))
355 slt._reset(17)
356 slt.update(range(100))
357 slt._check()
358 assert slt.bisect(10) == 22
359 assert slt.bisect(200) == 200
360
361 def test_bisect_right():
362 slt = SortedList()
363 assert slt.bisect_right(10) == 0
364 slt = SortedList(range(100))
365 slt._reset(17)
366 slt.update(range(100))
367 slt._check()
368 assert slt.bisect_right(10) == 22
369 assert slt.bisect_right(200) == 200
370
371 def test_copy():
372 alpha = SortedList(range(100))
373 alpha._reset(7)
374 beta = alpha.copy()
375 alpha.add(100)
376 assert len(alpha) == 101
377 assert len(beta) == 100
378
379 def test_copy_copy():
380 import copy
381 alpha = SortedList(range(100))
382 alpha._reset(7)
383 beta = copy.copy(alpha)
384 alpha.add(100)
385 assert len(alpha) == 101
386 assert len(beta) == 100
387
388 def test_count():
389 slt = SortedList()
390 slt._reset(7)
391
392 assert slt.count(0) == 0
393
394 for iii in range(100):
395 for jjj in range(iii):
396 slt.add(iii)
397 slt._check()
398
399 for iii in range(100):
400 assert slt.count(iii) == iii
401
402 assert slt.count(100) == 0
403
404 def test_pop():
405 slt = SortedList(range(10))
406 slt._reset(4)
407 slt._check()
408 assert slt.pop() == 9
409 slt._check()
410 assert slt.pop(0) == 0
411 slt._check()
412 assert slt.pop(-2) == 7
413 slt._check()
414 assert slt.pop(4) == 5
415 slt._check()
416
417 def test_pop_indexerror1():
418 slt = SortedList(range(10))
419 slt._reset(4)
420 with pytest.raises(IndexError):
421 slt.pop(-11)
422
423 def test_pop_indexerror2():
424 slt = SortedList(range(10))
425 slt._reset(4)
426 with pytest.raises(IndexError):
427 slt.pop(10)
428
429 def test_pop_indexerror3():
430 slt = SortedList()
431 with pytest.raises(IndexError):
432 slt.pop()
433
434 def test_index():
435 slt = SortedList(range(100))
436 slt._reset(17)
437
438 for val in range(100):
439 assert val == slt.index(val)
440
441 assert slt.index(99, 0, 1000) == 99
442
443 slt = SortedList((0 for rpt in range(100)))
444 slt._reset(17)
445
446 for start in range(100):
447 for stop in range(start, 100):
448 assert slt.index(0, start, stop + 1) == start
449
450 for start in range(100):
451 assert slt.index(0, -(100 - start)) == start
452
453 assert slt.index(0, -1000) == 0
454
455 def test_index_valueerror1():
456 slt = SortedList([0] * 10)
457 slt._reset(4)
458 with pytest.raises(ValueError):
459 slt.index(0, 10)
460
461 def test_index_valueerror2():
462 slt = SortedList([0] * 10)
463 slt._reset(4)
464 with pytest.raises(ValueError):
465 slt.index(0, 0, -10)
466
467 def test_index_valueerror3():
468 slt = SortedList([0] * 10)
469 slt._reset(4)
470 with pytest.raises(ValueError):
471 slt.index(0, 7, 3)
472
473 def test_index_valueerror4():
474 slt = SortedList([0] * 10)
475 slt._reset(4)
476 with pytest.raises(ValueError):
477 slt.index(1)
478
479 def test_index_valueerror5():
480 slt = SortedList()
481 with pytest.raises(ValueError):
482 slt.index(1)
483
484 def test_index_valueerror6():
485 slt = SortedList(range(10))
486 slt._reset(4)
487 with pytest.raises(ValueError):
488 slt.index(3, 5)
489
490 def test_index_valueerror7():
491 slt = SortedList([0] * 10 + [2] * 10)
492 slt._reset(4)
493 with pytest.raises(ValueError):
494 slt.index(1, 0, 10)
495
496 def test_mul():
497 this = SortedList(range(10))
498 this._reset(4)
499 that = this * 5
500 this._check()
501 that._check()
502 assert this == list(range(10))
503 assert that == sorted(list(range(10)) * 5)
504 assert this != that
505
506 def test_imul():
507 this = SortedList(range(10))
508 this._reset(4)
509 this *= 5
510 this._check()
511 assert this == sorted(list(range(10)) * 5)
512
513 def test_op_add():
514 this = SortedList(range(10))
515 this._reset(4)
516 assert (this + this + this) == (this * 3)
517
518 that = SortedList(range(10))
519 that._reset(4)
520 that += that
521 that += that
522 assert that == (this * 4)
523
524 def test_eq():
525 this = SortedList(range(10))
526 this._reset(4)
527 assert this == list(range(10))
528 assert this == tuple(range(10))
529 assert not (this == list(range(9)))
530
531 def test_ne():
532 this = SortedList(range(10))
533 this._reset(4)
534 assert this != list(range(9))
535 assert this != tuple(range(11))
536 assert this != [0, 1, 2, 3, 3, 5, 6, 7, 8, 9]
537 assert this != (val for val in range(10))
538 assert this != set()
539
540 def test_lt():
541 this = SortedList(range(10, 15))
542 this._reset(4)
543 assert this < [10, 11, 13, 13, 14]
544 assert this < [10, 11, 12, 13, 14, 15]
545 assert this < [11]
546
547 def test_le():
548 this = SortedList(range(10, 15))
549 this._reset(4)
550 assert this <= [10, 11, 12, 13, 14]
551 assert this <= [10, 11, 12, 13, 14, 15]
552 assert this <= [10, 11, 13, 13, 14]
553 assert this <= [11]
554
555 def test_gt():
556 this = SortedList(range(10, 15))
557 this._reset(4)
558 assert this > [10, 11, 11, 13, 14]
559 assert this > [10, 11, 12, 13]
560 assert this > [9]
561
562 def test_ge():
563 this = SortedList(range(10, 15))
564 this._reset(4)
565 assert this >= [10, 11, 12, 13, 14]
566 assert this >= [10, 11, 12, 13]
567 assert this >= [10, 11, 11, 13, 14]
568 assert this >= [9]
569
570 def test_repr():
571 this = SortedList(range(10))
572 this._reset(4)
573 assert repr(this) == 'SortedList([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])'
574
575 def test_repr_recursion():
576 this = SortedList([[1], [2], [3], [4]])
577 this._lists[-1].append(this)
578 assert repr(this) == 'SortedList([[1], [2], [3], [4], ...])'
579
580 def test_repr_subclass():
581 class CustomSortedList(SortedList):
582 pass
583 this = CustomSortedList([1, 2, 3, 4])
584 assert repr(this) == 'CustomSortedList([1, 2, 3, 4])'
585
586 def test_pickle():
587 import pickle
588 alpha = SortedList(range(10000))
589 alpha._reset(500)
590 beta = pickle.loads(pickle.dumps(alpha))
591 assert alpha == beta
592 assert alpha._load == 500
593 assert beta._load == 1000
594
595 def test_build_index():
596 slt = SortedList([0])
597 slt._reset(4)
598 slt._build_index()
599 slt._check()
600
601 def test_check():
602 slt = SortedList(range(10))
603 slt._reset(4)
604 slt._len = 5
605 with pytest.raises(AssertionError):
606 slt._check()