def test_delitem(): random.seed(0) slt = SortedListWithKey(range(100), key=negate) slt._reset(17) while len(slt) > 0: del slt[random.randrange(len(slt))] slt._check()
def test_islice(): sl = SortedListWithKey(key=modulo) sl._reset(7) assert [] == list(sl.islice()) values = sorted(range(100), key=modulo) 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_init(): slt = SortedListWithKey(key=modulo) assert slt.key == modulo slt._check() slt = SortedListWithKey(key=modulo) slt._reset(10000) assert slt._load == 10000 assert slt._half == 5000 assert slt._dual == 20000 slt._check() slt = SortedListWithKey(range(10000), key=modulo) assert all(tup[0] == tup[1] for tup in zip(slt, sorted(range(10000), key=modulo))) slt.clear() assert slt._len == 0 assert slt._maxes == [] assert slt._lists == [] assert isinstance(slt, SortedList) assert isinstance(slt, SortedListWithKey) slt._check()
def test_setitem_extended_slice(): slt = SortedListWithKey(range(1000, 0, -10), key=negate) slt._reset(17) lst = list(range(1000, 0, -10)) lst[10:90:10] = range(905, 105, -100) slt[10:90:10] = range(905, 105, -100) assert slt == lst
def test_islice(): return slt = SortedListWithKey(key=negate) slt._reset(7) assert [] == list(slt.islice()) values = sorted(range(53), key=negate) slt.update(values) for start in range(53): for stop in range(53): assert list(slt.islice(start, stop)) == values[start:stop] for start in range(53): for stop in range(53): assert list(slt.islice(start, stop, reverse=True)) == values[start:stop][::-1] for start in range(53): assert list(slt.islice(start=start)) == values[start:] assert list(slt.islice(start=start, reverse=True)) == values[start:][::-1] for stop in range(53): assert list(slt.islice(stop=stop)) == values[:stop] assert list(slt.islice(stop=stop, reverse=True)) == values[:stop][::-1]
def test_setitem(): random.seed(0) slt = SortedListWithKey(range(0, 100), key=modulo) slt._reset(17) slt[0] = 100 slt[99] = 99 slt[55] = 45
def test_copy(): slt = SortedListWithKey(range(100), key=negate) slt._reset(7) two = slt.copy() slt.add(100) assert len(slt) == 101 assert len(two) == 100
def test_getitem_slice(): random.seed(0) slt = SortedListWithKey(key=negate) slt._reset(17) lst = list() for rpt in range(100): val = random.random() slt.add(val) lst.append(val) lst.sort(reverse=True) 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_delitem_slice(): slt = SortedListWithKey(range(100), key=negate) 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_copy_copy(): import copy slt = SortedListWithKey(range(100), key=modulo) slt._reset(7) two = copy.copy(slt) slt.add(100) assert len(slt) == 101 assert len(two) == 100
def test_pickle(): import pickle alpha = SortedListWithKey(range(10000), key=negate) alpha._reset(500) beta = pickle.loads(pickle.dumps(alpha)) assert alpha == beta assert alpha._key == beta._key assert alpha._load == beta._load
def test_append(): slt = SortedListWithKey(key=negate) slt._reset(17) slt.append(1000) for val in range(999, -1, -1): slt.append(val) slt._check()
def test_setitem_slice(): slt = SortedListWithKey(range(100), key=modulo) slt._reset(17) slt[:10] = [90, 80, 70, 60, 50, 40, 30, 20, 10, 0] slt[:10:2] = [0, 10, 20, 30, 40] slt[:] = sorted(range(100), key=modulo) slt[90:] = [] slt[:10] = [] assert len(slt) == 80
def test_bisect_right(): slt = SortedListWithKey(key=negate) assert slt.bisect_right(10) == 0 slt = SortedListWithKey(range(100), key=negate) slt._reset(17) slt.update(range(100)) slt._check() assert slt.bisect_right(10) == 180 assert slt.bisect_right(0) == 200
def test_append(): slt = SortedListWithKey(key=modulo) slt._reset(4) slt.append(0) for val in range(1, 10): slt.append(val) slt._check()
def test_bisect(): slt = SortedListWithKey(key=modulo) assert slt.bisect(10) == 0 slt = SortedListWithKey(range(100), key=modulo) slt._reset(17) slt.update(range(100)) slt._check() assert slt.bisect(10) == 20 assert slt.bisect(0) == 20
def test_extend(): slt = SortedListWithKey(key=modulo) slt._reset(4) slt.extend(range(5)) slt._check() slt.extend(range(6, 10)) slt._check()
def test_eq(): this = SortedListWithKey(range(10), key=negate) this._reset(4) that = SortedListWithKey(range(20), key=negate) that._reset(4) assert not (this == that) that.clear() that.update(range(10)) assert this == that
def test_op_add(): this = SortedListWithKey(range(10), key=negate) this._reset(4) assert (this + this + this) == (this * 3) that = SortedListWithKey(range(10), key=negate) that._reset(4) that += that that += that assert that == (this * 4)
def test_gte(): this = SortedListWithKey(range(10), key=negate) this._reset(4) that = SortedListWithKey(range(10), key=negate) that._reset(5) assert this >= that assert that >= this del this[-1] assert that >= this assert not (this >= that)
def test_delete(): slt = SortedListWithKey(range(20), key=negate) 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_pop(): slt = SortedListWithKey(range(10), key=negate) slt._reset(4) slt._check() assert slt.pop() == 0 slt._check() assert slt.pop(0) == 9 slt._check() assert slt.pop(-2) == 2 slt._check() assert slt.pop(4) == 4 slt._check()
def test_setitem(): random.seed(0) slt = SortedListWithKey(range(0, 100, 10), key=negate) slt._reset(4) slt[-3] = 20 slt._check() values = list(enumerate(range(95, 5, -10))) random.shuffle(values) for pos, val in values: slt[pos] = val
def test_pop(): slt = SortedListWithKey(range(10), key=modulo) 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_count(): slt = SortedListWithKey(key=negate) 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
def test_insert(): slt = SortedListWithKey(range(10), key=negate) slt._reset(4) slt.insert(-1, 0) slt._check() slt.insert(-100, 9) slt._check() slt.insert(0, 10) slt._check() slt.insert(14, -1) slt._check() slt = SortedListWithKey(key=negate) slt._reset(4) slt.insert(0, 5) slt._check() slt = SortedListWithKey(range(5, 15), key=negate) slt._reset(4) for rpt in range(8): slt.insert(0, 15) slt._check() slt = SortedListWithKey(range(10), key=negate) slt._reset(4) slt.insert(8, 2) slt._check()
def test_insert(): slt = SortedListWithKey(range(10), key=modulo) slt._reset(4) slt.insert(-100, 0) slt._check() slt.insert(-1, 9) slt._check() slt.insert(0, 10) slt._check() slt = SortedListWithKey(key=modulo) slt._reset(4) slt.insert(0, 5) slt._check() slt = SortedListWithKey(range(5, 15), key=modulo) slt._reset(4) for rpt in range(8): slt.insert(0, 10) slt._check() slt = SortedListWithKey(range(10), key=modulo) slt._reset(4) slt.insert(8, 8) slt._check()
def test_remove(): slt = SortedListWithKey(key=modulo) assert slt.discard(0) == None assert len(slt) == 0 slt._check() slt = SortedListWithKey([1, 2, 2, 2, 3, 3, 5], key=modulo) 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_delitem(): random.seed(0) slt = SortedListWithKey(range(100), key=modulo) slt._reset(17) while len(slt) > 0: del slt[random.randrange(len(slt))] slt._check() slt = SortedListWithKey(range(100), key=modulo) slt._reset(17) del slt[:] assert len(slt) == 0 slt._check()
def test_extend(): slt = SortedListWithKey(key=negate) slt._reset(17) slt.extend(range(300, 200, -1)) slt._check() slt.extend(list(range(200, 100, -1))) slt._check() for val in range(100, 0, -1): del slt._index[:] slt._build_index() slt.extend([val] * (101 - val)) slt._check()