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 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600
|
# -*- coding: utf-8 -*-
import random
from UM.SortedList import SortedList
from itertools import chain
import pytest
def test_init():
slt = SortedList()
assert slt.key is None
slt._check()
slt = SortedList()
slt._reset(10000)
assert slt._load == 10000
slt._check()
slt = SortedList(range(10000))
assert all(tup[0] == tup[1] for tup in zip(slt, range(10000)))
slt.clear()
assert slt._len == 0
assert slt._maxes == []
assert slt._lists == []
slt._check()
def test_add():
random.seed(0)
slt = SortedList()
for val in range(1000):
slt.add(val)
slt._check()
slt = SortedList()
for val in range(1000, 0, -1):
slt.add(val)
slt._check()
slt = SortedList()
for val in range(1000):
slt.add(random.random())
slt._check()
def test_update():
slt = SortedList()
slt.update(range(1000))
assert len(slt) == 1000
slt._check()
slt.update(range(100))
assert len(slt) == 1100
slt._check()
slt.update(range(10000))
assert len(slt) == 11100
slt._check()
values = sorted(chain(range(1000), range(100), range(10000)))
assert all(tup[0] == tup[1] for tup in zip(slt, values))
def test_contains():
slt = SortedList()
assert 0 not in slt
slt.update(range(10000))
for val in range(10000):
assert val in slt
assert 10000 not in slt
slt._check()
def test_discard():
slt = SortedList()
assert slt.discard(0) == None
assert len(slt) == 0
slt._check()
slt = SortedList([1, 2, 2, 2, 3, 3, 5])
slt._reset(4)
slt.discard(6)
slt._check()
slt.discard(4)
slt._check()
slt.discard(2)
slt._check()
assert all(tup[0] == tup[1] for tup in zip(slt, [1, 2, 2, 3, 3, 5]))
def test_remove():
slt = SortedList()
assert slt.discard(0) == None
assert len(slt) == 0
slt._check()
slt = SortedList([1, 2, 2, 2, 3, 3, 5])
slt._reset(4)
slt.remove(2)
slt._check()
assert all(tup[0] == tup[1] for tup in zip(slt, [1, 2, 2, 3, 3, 5]))
def test_remove_valueerror1():
slt = SortedList()
with pytest.raises(ValueError):
slt.remove(0)
def test_remove_valueerror2():
slt = SortedList(range(100))
slt._reset(10)
with pytest.raises(ValueError):
slt.remove(100)
def test_remove_valueerror3():
slt = SortedList([1, 2, 2, 2, 3, 3, 5])
with pytest.raises(ValueError):
slt.remove(4)
def test_delete():
slt = SortedList(range(20))
slt._reset(4)
slt._check()
for val in range(20):
slt.remove(val)
slt._check()
assert len(slt) == 0
assert slt._maxes == []
assert slt._lists == []
def test_getitem():
random.seed(0)
slt = SortedList()
slt._reset(17)
lst = list()
for rpt in range(100):
val = random.random()
slt.add(val)
lst.append(val)
lst.sort()
assert all(slt[idx] == lst[idx] for idx in range(100))
assert all(slt[idx - 99] == lst[idx - 99] for idx in range(100))
def test_getitem_slice():
random.seed(0)
slt = SortedList()
slt._reset(17)
lst = list()
for rpt in range(100):
val = random.random()
slt.add(val)
lst.append(val)
lst.sort()
assert all(slt[start:] == lst[start:]
for start in [-75, -25, 0, 25, 75])
assert all(slt[:stop] == lst[:stop]
for stop in [-75, -25, 0, 25, 75])
assert all(slt[::step] == lst[::step]
for step in [-5, -1, 1, 5])
assert all(slt[start:stop] == lst[start:stop]
for start in [-75, -25, 0, 25, 75]
for stop in [-75, -25, 0, 25, 75])
assert all(slt[:stop:step] == lst[:stop:step]
for stop in [-75, -25, 0, 25, 75]
for step in [-5, -1, 1, 5])
assert all(slt[start::step] == lst[start::step]
for start in [-75, -25, 0, 25, 75]
for step in [-5, -1, 1, 5])
assert all(slt[start:stop:step] == lst[start:stop:step]
for start in [-75, -25, 0, 25, 75]
for stop in [-75, -25, 0, 25, 75]
for step in [-5, -1, 1, 5])
def test_getitem_slice_big():
slt = SortedList(range(4))
lst = list(range(4))
itr = ((start, stop, step)
for start in [-6, -4, -2, 0, 2, 4, 6]
for stop in [-6, -4, -2, 0, 2, 4, 6]
for step in [-3, -2, -1, 1, 2, 3])
for start, stop, step in itr:
assert slt[start:stop:step] == lst[start:stop:step]
def test_getitem_slicezero():
slt = SortedList(range(100))
slt._reset(17)
with pytest.raises(ValueError):
slt[::0]
def test_getitem_indexerror1():
slt = SortedList()
with pytest.raises(IndexError):
slt[5]
def test_getitem_indexerror2():
slt = SortedList(range(100))
with pytest.raises(IndexError):
slt[200]
def test_getitem_indexerror3():
slt = SortedList(range(100))
with pytest.raises(IndexError):
slt[-101]
def test_delitem():
random.seed(0)
slt = SortedList(range(100))
slt._reset(17)
while len(slt) > 0:
pos = random.randrange(len(slt))
del slt[pos]
slt._check()
slt = SortedList(range(100))
slt._reset(17)
del slt[:]
assert len(slt) == 0
slt._check()
def test_delitem_slice():
slt = SortedList(range(100))
slt._reset(17)
del slt[10:40:1]
del slt[10:40:-1]
del slt[10:40:2]
del slt[10:40:-2]
def test_iter():
slt = SortedList(range(10000))
itr = iter(slt)
assert all(tup[0] == tup[1] for tup in zip(range(10000), itr))
def test_reversed():
slt = SortedList(range(10000))
rev = reversed(slt)
assert all(tup[0] == tup[1] for tup in zip(range(9999, -1, -1), rev))
def test_reverse():
slt = SortedList(range(10000))
with pytest.raises(NotImplementedError):
slt.reverse()
def test_islice():
sl = SortedList()
sl._reset(7)
assert [] == list(sl.islice())
values = list(range(53))
sl.update(values)
for start in range(53):
for stop in range(53):
assert list(sl.islice(start, stop)) == values[start:stop]
for start in range(53):
for stop in range(53):
assert list(sl.islice(start, stop, reverse=True)) == values[start:stop][::-1]
for start in range(53):
assert list(sl.islice(start=start)) == values[start:]
assert list(sl.islice(start=start, reverse=True)) == values[start:][::-1]
for stop in range(53):
assert list(sl.islice(stop=stop)) == values[:stop]
assert list(sl.islice(stop=stop, reverse=True)) == values[:stop][::-1]
def test_irange():
sl = SortedList()
sl._reset(7)
assert [] == list(sl.irange())
values = list(range(53))
sl.update(values)
for start in range(53):
for end in range(start, 53):
assert list(sl.irange(start, end)) == values[start:(end + 1)]
assert list(sl.irange(start, end, reverse=True)) == values[start:(end + 1)][::-1]
for start in range(53):
for end in range(start, 53):
assert list(range(start, end)) == list(sl.irange(start, end, (True, False)))
for start in range(53):
for end in range(start, 53):
assert list(range(start + 1, end + 1)) == list(sl.irange(start, end, (False, True)))
for start in range(53):
for end in range(start, 53):
assert list(range(start + 1, end)) == list(sl.irange(start, end, (False, False)))
for start in range(53):
assert list(range(start, 53)) == list(sl.irange(start))
for end in range(53):
assert list(range(0, end)) == list(sl.irange(None, end, (True, False)))
assert values == list(sl.irange(inclusive=(False, False)))
assert [] == list(sl.irange(53))
assert values == list(sl.irange(None, 53, (True, False)))
def test_len():
slt = SortedList()
for val in range(10000):
slt.add(val)
assert len(slt) == (val + 1)
def test_bisect_left():
slt = SortedList()
assert slt.bisect_left(0) == 0
slt = SortedList(range(100))
slt._reset(17)
slt.update(range(100))
slt._check()
assert slt.bisect_left(50) == 100
assert slt.bisect_left(200) == 200
def test_bisect():
slt = SortedList()
assert slt.bisect(10) == 0
slt = SortedList(range(100))
slt._reset(17)
slt.update(range(100))
slt._check()
assert slt.bisect(10) == 22
assert slt.bisect(200) == 200
def test_bisect_right():
slt = SortedList()
assert slt.bisect_right(10) == 0
slt = SortedList(range(100))
slt._reset(17)
slt.update(range(100))
slt._check()
assert slt.bisect_right(10) == 22
assert slt.bisect_right(200) == 200
def test_copy():
alpha = SortedList(range(100))
alpha._reset(7)
beta = alpha.copy()
alpha.add(100)
assert len(alpha) == 101
assert len(beta) == 100
def test_copy_copy():
import copy
alpha = SortedList(range(100))
alpha._reset(7)
beta = copy.copy(alpha)
alpha.add(100)
assert len(alpha) == 101
assert len(beta) == 100
def test_count():
slt = SortedList()
slt._reset(7)
assert slt.count(0) == 0
for iii in range(100):
for jjj in range(iii):
slt.add(iii)
slt._check()
for iii in range(100):
assert slt.count(iii) == iii
assert slt.count(100) == 0
def test_pop():
slt = SortedList(range(10))
slt._reset(4)
slt._check()
assert slt.pop() == 9
slt._check()
assert slt.pop(0) == 0
slt._check()
assert slt.pop(-2) == 7
slt._check()
assert slt.pop(4) == 5
slt._check()
def test_pop_indexerror1():
slt = SortedList(range(10))
slt._reset(4)
with pytest.raises(IndexError):
slt.pop(-11)
def test_pop_indexerror2():
slt = SortedList(range(10))
slt._reset(4)
with pytest.raises(IndexError):
slt.pop(10)
def test_pop_indexerror3():
slt = SortedList()
with pytest.raises(IndexError):
slt.pop()
def test_index():
slt = SortedList(range(100))
slt._reset(17)
for val in range(100):
assert val == slt.index(val)
assert slt.index(99, 0, 1000) == 99
slt = SortedList((0 for rpt in range(100)))
slt._reset(17)
for start in range(100):
for stop in range(start, 100):
assert slt.index(0, start, stop + 1) == start
for start in range(100):
assert slt.index(0, -(100 - start)) == start
assert slt.index(0, -1000) == 0
def test_index_valueerror1():
slt = SortedList([0] * 10)
slt._reset(4)
with pytest.raises(ValueError):
slt.index(0, 10)
def test_index_valueerror2():
slt = SortedList([0] * 10)
slt._reset(4)
with pytest.raises(ValueError):
slt.index(0, 0, -10)
def test_index_valueerror3():
slt = SortedList([0] * 10)
slt._reset(4)
with pytest.raises(ValueError):
slt.index(0, 7, 3)
def test_index_valueerror4():
slt = SortedList([0] * 10)
slt._reset(4)
with pytest.raises(ValueError):
slt.index(1)
def test_index_valueerror5():
slt = SortedList()
with pytest.raises(ValueError):
slt.index(1)
def test_index_valueerror6():
slt = SortedList(range(10))
slt._reset(4)
with pytest.raises(ValueError):
slt.index(3, 5)
def test_index_valueerror7():
slt = SortedList([0] * 10 + [2] * 10)
slt._reset(4)
with pytest.raises(ValueError):
slt.index(1, 0, 10)
def test_mul():
this = SortedList(range(10))
this._reset(4)
that = this * 5
this._check()
that._check()
assert this == list(range(10))
assert that == sorted(list(range(10)) * 5)
assert this != that
def test_imul():
this = SortedList(range(10))
this._reset(4)
this *= 5
this._check()
assert this == sorted(list(range(10)) * 5)
def test_op_add():
this = SortedList(range(10))
this._reset(4)
assert (this + this + this) == (this * 3)
that = SortedList(range(10))
that._reset(4)
that += that
that += that
assert that == (this * 4)
def test_eq():
this = SortedList(range(10))
this._reset(4)
assert this == list(range(10))
assert this == tuple(range(10))
assert not (this == list(range(9)))
def test_ne():
this = SortedList(range(10))
this._reset(4)
assert this != list(range(9))
assert this != tuple(range(11))
assert this != [0, 1, 2, 3, 3, 5, 6, 7, 8, 9]
assert this != (val for val in range(10))
assert this != set()
def test_lt():
this = SortedList(range(10, 15))
this._reset(4)
assert this < [10, 11, 13, 13, 14]
assert this < [10, 11, 12, 13, 14, 15]
assert this < [11]
def test_le():
this = SortedList(range(10, 15))
this._reset(4)
assert this <= [10, 11, 12, 13, 14]
assert this <= [10, 11, 12, 13, 14, 15]
assert this <= [10, 11, 13, 13, 14]
assert this <= [11]
def test_gt():
this = SortedList(range(10, 15))
this._reset(4)
assert this > [10, 11, 11, 13, 14]
assert this > [10, 11, 12, 13]
assert this > [9]
def test_ge():
this = SortedList(range(10, 15))
this._reset(4)
assert this >= [10, 11, 12, 13, 14]
assert this >= [10, 11, 12, 13]
assert this >= [10, 11, 11, 13, 14]
assert this >= [9]
def test_repr():
this = SortedList(range(10))
this._reset(4)
assert repr(this) == 'SortedList([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])'
def test_repr_recursion():
this = SortedList([[1], [2], [3], [4]])
this._lists[-1].append(this)
assert repr(this) == 'SortedList([[1], [2], [3], [4], ...])'
def test_repr_subclass():
class CustomSortedList(SortedList):
pass
this = CustomSortedList([1, 2, 3, 4])
assert repr(this) == 'CustomSortedList([1, 2, 3, 4])'
def test_pickle():
import pickle
alpha = SortedList(range(10000))
alpha._reset(500)
beta = pickle.loads(pickle.dumps(alpha))
assert alpha == beta
assert alpha._load == beta._load
def test_build_index():
slt = SortedList([0])
slt._reset(4)
slt._build_index()
slt._check()
def test_check():
slt = SortedList(range(10))
slt._reset(4)
slt._len = 5
with pytest.raises(AssertionError):
slt._check()
|