Debian Codegraph @

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