-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_skipcollections.py
145 lines (101 loc) · 3.29 KB
/
test_skipcollections.py
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
from random import randint
from nose.tools import eq_, ok_
from six.moves import xrange
from skiplistcollections import SkipListDict, SkipListSet
def test_general_skiplistdict_behaviour():
stuff = SkipListDict(capacity=1024)
used_keys = set((0,))
for count in xrange(1000):
eq_(len(stuff), count)
eq_(len(tuple(stuff.keys())), count)
eq_(len(tuple(stuff.values())), count)
eq_(len(tuple(stuff.items())), count)
items = tuple(stuff.items())
if items:
previous_key, previous_value = items[0]
for k, v in items[1:]:
eq_(v, k % 100)
ok_(k > previous_key, '%r should be greater than %r' % (k, previous_key))
previous_key = k
number = 0
while number in used_keys:
number = randint(0, 1000000)
used_keys.add(number)
ok_(number not in stuff)
try:
stuff[number]
assert False, 'Should have raised an exception'
except KeyError:
pass
stuff[number] = number % 100
ok_(number in stuff)
eq_(stuff[number], number % 100)
used_keys.remove(0)
for key in set(used_keys):
used_keys.remove(key)
ok_(key in stuff)
del stuff[key]
ok_(key not in stuff)
eq_(len(stuff), len(used_keys))
def test_skiplistdict_boolean_value():
d = SkipListDict()
eq_(bool(d), False)
d[1] = 2
eq_(bool(d), True)
del d[1]
eq_(bool(d), False)
def test_level_drops_down_when_deleting_stuff():
d = SkipListDict()
counter = 0
while d.level < 4:
d[counter] = counter
counter += 1
for key in list(d.keys()):
del d[key]
eq_(d.level, 0)
def test_general_skiplistset_behaviour():
stuff = SkipListSet(capacity=1024)
used_keys = set((0,))
for count in xrange(1000):
eq_(len(stuff), count)
items = tuple(stuff)
if items:
previous_key = items[0]
for k in items[1:]:
ok_(k > previous_key, '%r should be greater than %r' % (k, previous_key))
previous_key = k
number = 0
while number in used_keys:
number = randint(0, 1000000)
used_keys.add(number)
ok_(number not in stuff)
stuff.add(number)
ok_(number in stuff)
used_keys.remove(0)
for key in set(used_keys):
used_keys.remove(key)
ok_(key in stuff)
stuff.remove(key)
ok_(key not in stuff)
try:
stuff.remove(key)
assert False, 'Should have raised an exception'
except KeyError:
pass
eq_(len(stuff), len(used_keys))
def test_skiplistset_boolean_value():
s = SkipListSet()
eq_(bool(s), False)
s.add(1)
eq_(bool(s), True)
s.remove(1)
eq_(bool(s), False)
def test_skiplistdict_iteration_regression():
# The regression would result in yielding more items than necessary
# if the search key was not found in the dictionary
sld = SkipListDict()
sld.update(dict(b='b', d='d'))
eq_(tuple(sld.keys(start_key='c')), ('d',))
eq_(tuple(sld.keys(start_key='c', reverse=True)), ('b',))
eq_(tuple(sld.keys(start_key='e')), ())
eq_(tuple(sld.keys(start_key='a', reverse=True)), ())