def test_bisect_key(): temp = SortedDict(modulo, ((val, val) for val in range(100))) temp._reset(7) assert all(temp.bisect(val) == ((val % 10) + 1) * 10 for val in range(100)) assert all( temp.bisect_right(val) == ((val % 10) + 1) * 10 for val in range(100)) assert all(temp.bisect_left(val) == (val % 10) * 10 for val in range(100))
def test_pickle(): import pickle alpha = SortedDict(negate, zip(range(10000), range(10000))) alpha._reset(500) beta = pickle.loads(pickle.dumps(alpha)) assert alpha == beta assert alpha._key == beta._key
def test_irange(): mapping = [(val, pos) for pos, val in enumerate(string.ascii_lowercase)] temp = SortedDict(mapping) temp._reset(7) for start in range(26): for stop in range(start + 1, 26): result = list(string.ascii_lowercase[start:stop]) assert list(temp.irange(result[0], result[-1])) == result
def test_islice(): mapping = [(val, pos) for pos, val in enumerate(string.ascii_lowercase)] temp = SortedDict(mapping) temp._reset(7) for start in range(30): for stop in range(30): assert list(temp.islice(start, stop)) == list(string.ascii_lowercase[start:stop])
def test_irange_key(): temp = SortedDict(modulo, ((val, val) for val in range(100))) temp._reset(7) values = sorted(range(100), key=modulo) for start in range(10): for stop in range(start, 10): result = list(temp.irange_key(start, stop)) assert result == values[(start * 10):((stop + 1) * 10)]
def test_init(): sdict = SortedDict() sdict._check() sdict = SortedDict() sdict._reset(17) sdict._check() sdict = SortedDict((val, -val) for val in range(10000)) sdict._check() assert all(key == -val for key, val in sdict.items()) sdict.clear() sdict._check() assert len(sdict) == 0 sdict = SortedDict.fromkeys(range(1000), None) assert all(sdict[key] == None for key in range(1000))
def test_reversed_key(): temp = SortedDict(modulo, ((val, val) for val in range(100))) temp._reset(7) values = sorted(range(100), key=modulo) assert all(lhs == rhs for lhs, rhs in zip(reversed(temp), reversed(values)))
def test_index_key(): temp = SortedDict(negate, ((val, val) for val in range(100))) temp._reset(7) assert all(temp.index(val) == (99 - val) for val in range(100))
def test_iter_key(): temp = SortedDict(negate, ((val, val) for val in range(100))) temp._reset(7) assert all(lhs == rhs for lhs, rhs in zip(temp, reversed(range(100))))
def test_bisect_key2(): temp = SortedDict(modulo, ((val, val) for val in range(100))) temp._reset(7) assert all(temp.bisect_key(val) == ((val % 10) + 1) * 10 for val in range(10)) assert all(temp.bisect_key_right(val) == ((val % 10) + 1) * 10 for val in range(10)) assert all(temp.bisect_key_left(val) == (val % 10) * 10 for val in range(10))