mirror of https://github.com/mahmoud/boltons.git
setutils tests
This commit is contained in:
parent
4f48afda36
commit
6f49c4895d
|
@ -419,53 +419,3 @@ class IndexedSet(MutableSet):
|
||||||
except KeyError:
|
except KeyError:
|
||||||
cn = self.__class__.__name__
|
cn = self.__class__.__name__
|
||||||
raise ValueError('%r is not in %s' % (val, cn))
|
raise ValueError('%r is not in %s' % (val, cn))
|
||||||
|
|
||||||
|
|
||||||
# Tests of a manner
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
zero2nine = IndexedSet(range(10))
|
|
||||||
five2nine = zero2nine & IndexedSet(range(5, 15))
|
|
||||||
x = IndexedSet(five2nine)
|
|
||||||
x |= set([10])
|
|
||||||
print(zero2nine, five2nine, x, x[-1])
|
|
||||||
print(zero2nine ^ five2nine)
|
|
||||||
print(x[:3], x[2:4:-1])
|
|
||||||
|
|
||||||
try:
|
|
||||||
thou = IndexedSet(range(1000))
|
|
||||||
print(thou.pop(), thou.pop())
|
|
||||||
print(thou.pop(499), thou.pop(499), [thou[i] for i in range(495, 505)])
|
|
||||||
print('thou hath', len(thou), 'items')
|
|
||||||
while len(thou) > 600:
|
|
||||||
dead_idx_len = len(thou.dead_indices)
|
|
||||||
dead_idx_count = thou._dead_index_count
|
|
||||||
thou.pop(0)
|
|
||||||
new_dead_idx_len = len(thou.dead_indices)
|
|
||||||
if new_dead_idx_len < dead_idx_len:
|
|
||||||
print('thou hath culled', dead_idx_count, 'indices')
|
|
||||||
print('thou hath', len(thou), 'items')
|
|
||||||
print('thou hath', thou._dead_index_count, 'dead indices')
|
|
||||||
print('exposing _MISSING:', any([thou[i] is _MISSING
|
|
||||||
for i in range(len(thou))]))
|
|
||||||
thou &= IndexedSet(range(500, 503))
|
|
||||||
print(thou)
|
|
||||||
|
|
||||||
from os import urandom
|
|
||||||
import time
|
|
||||||
big_set = IndexedSet(range(100000))
|
|
||||||
rands = [ord(r) for r in urandom(len(big_set))]
|
|
||||||
start_time, start_size = time.time(), len(big_set)
|
|
||||||
while len(big_set) > 10000:
|
|
||||||
if len(big_set) % 10000 == 0:
|
|
||||||
print(len(big_set) / 10000)
|
|
||||||
rand = rands.pop()
|
|
||||||
big_set.pop(rand)
|
|
||||||
big_set.pop(-rand)
|
|
||||||
end_time, end_size = time.time(), len(big_set)
|
|
||||||
print()
|
|
||||||
print('popped %s items in %s seconds' % (start_size - end_size,
|
|
||||||
end_time - start_time))
|
|
||||||
except Exception as e:
|
|
||||||
import pdb;pdb.post_mortem()
|
|
||||||
raise
|
|
||||||
|
|
|
@ -0,0 +1,68 @@
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
from boltons.setutils import IndexedSet, _MISSING
|
||||||
|
|
||||||
|
|
||||||
|
def test_indexed_set_basic():
|
||||||
|
zero2nine = IndexedSet(range(10))
|
||||||
|
five2nine = zero2nine & IndexedSet(range(5, 15))
|
||||||
|
x = IndexedSet(five2nine)
|
||||||
|
x |= set([10])
|
||||||
|
|
||||||
|
assert list(zero2nine) == [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
|
||||||
|
assert set(zero2nine) == set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
|
||||||
|
assert list(five2nine) == [5, 6, 7, 8, 9]
|
||||||
|
assert x == IndexedSet([5, 6, 7, 8, 9, 10])
|
||||||
|
assert x[-1] == 10
|
||||||
|
|
||||||
|
assert zero2nine ^ five2nine == IndexedSet([0, 1, 2, 3, 4])
|
||||||
|
|
||||||
|
assert x[:3] == IndexedSet([5, 6, 7])
|
||||||
|
assert x[2:4:-1] == IndexedSet([8, 7])
|
||||||
|
|
||||||
|
|
||||||
|
def test_indexed_set_mutate():
|
||||||
|
thou = IndexedSet(range(1000))
|
||||||
|
assert (thou.pop(), thou.pop()) == (999, 998)
|
||||||
|
assert (thou.pop(499), thou.pop(499)) == (499, 500)
|
||||||
|
|
||||||
|
ref = [495, 496, 497, 498, 501, 502, 503, 504, 505, 506]
|
||||||
|
assert [thou[i] for i in range(495, 505)] == ref
|
||||||
|
|
||||||
|
assert len(thou) == 996
|
||||||
|
while len(thou) > 600:
|
||||||
|
dead_idx_len = len(thou.dead_indices)
|
||||||
|
dead_idx_count = thou._dead_index_count
|
||||||
|
thou.pop(0)
|
||||||
|
new_dead_idx_len = len(thou.dead_indices)
|
||||||
|
if new_dead_idx_len < dead_idx_len:
|
||||||
|
assert dead_idx_count > 0
|
||||||
|
# 124, 109, 95
|
||||||
|
assert len(thou) == 600
|
||||||
|
assert thou._dead_index_count == 67
|
||||||
|
|
||||||
|
assert not any([thou[i] is _MISSING for i in range(len(thou))])
|
||||||
|
|
||||||
|
thou &= IndexedSet(range(500, 503))
|
||||||
|
|
||||||
|
assert thou == IndexedSet([501, 502])
|
||||||
|
return
|
||||||
|
|
||||||
|
|
||||||
|
def big_popper():
|
||||||
|
# more of a benchmark than a test
|
||||||
|
from os import urandom
|
||||||
|
import time
|
||||||
|
big_set = IndexedSet(range(100000))
|
||||||
|
rands = [ord(r) for r in urandom(len(big_set))]
|
||||||
|
start_time, start_size = time.time(), len(big_set)
|
||||||
|
while len(big_set) > 10000:
|
||||||
|
if len(big_set) % 10000 == 0:
|
||||||
|
print(len(big_set) / 10000)
|
||||||
|
rand = rands.pop()
|
||||||
|
big_set.pop(rand)
|
||||||
|
big_set.pop(-rand)
|
||||||
|
end_time, end_size = time.time(), len(big_set)
|
||||||
|
print()
|
||||||
|
print('popped %s items in %s seconds' % (start_size - end_size,
|
||||||
|
end_time - start_time))
|
Loading…
Reference in New Issue