Debian Codegraph @

tests/test_stress_sorteddict.py

raw
1 # -*- coding: utf-8 -*-
2
3 from __future__ import print_function
4 from sys import hexversion
5
6 import random
7 from .context import sortedcontainers
8 from sortedcontainers import SortedDict
9 from functools import wraps
10
11 if hexversion < 0x03000000:
12 from itertools import izip as zip
13 range = xrange
14
15 random.seed(0)
16 actions = []
17
18 def actor(func):
19 actions.append(func)
20 return func
21
22 def test_init():
23 sdict = SortedDict()
24 sdict._check()
25
26 sdict = SortedDict()
27 sdict._reset(17)
28 sdict._check()
29
30 sdict = SortedDict((val, -val) for val in range(10000))
31 sdict._check()
32 assert all(key == -val for key, val in sdict.items())
33
34 sdict.clear()
35 sdict._check()
36 assert len(sdict) == 0
37
38 sdict = SortedDict.fromkeys(range(1000), None)
39 assert all(sdict[key] == None for key in range(1000))
40
41 @actor
42 def stress_contains(sdict):
43 keys = list(sdict)
44 assert all((key in sdict for key in keys))
45
46 @actor
47 def stress_delitem(sdict):
48 keys = list(sdict)
49 for rpt in range(100):
50 pos = random.randrange(0, len(sdict))
51 del sdict[keys[pos]]
52 del keys[pos]
53
54 @actor
55 def stress_getitem(sdict):
56 items = list(sdict.items())
57 assert all(sdict[key] == value for key, value in items)
58
59 @actor
60 def stress_eq(sdict):
61 that = dict((key, value) for key, value in sdict.items())
62 assert sdict == that
63
64 @actor
65 def stress_setitem_len(sdict):
66 start_len = len(sdict)
67 keys = list(range(100))
68 missing = sum(1 for val in keys if val not in sdict)
69 for val in keys:
70 sdict[val] = -val
71 end_len = len(sdict)
72 assert (start_len + missing) == end_len
73
74 @actor
75 def stress_copy(sdict):
76 that = sdict.copy()
77
78 @actor
79 def stress_get(sdict):
80 keys = list(range(100))
81 for key in keys:
82 if key in sdict:
83 assert sdict.get(key, 1) == -key
84 else:
85 assert sdict.get(key, 1) == 1
86
87 @actor
88 def stress_has_key(sdict):
89 if hexversion > 0x03000000:
90 return
91 keys = list(range(100))
92 for key in keys:
93 assert all((key in sdict) == (sdict.has_key(key)) for key in sdict)
94
95 @actor
96 def stress_items_keys_values(sdict):
97 items = sdict.items()
98 keys = sdict.keys()
99 values = sdict.values()
100 assert list(items) == list(zip(keys, values))
101
102 @actor
103 def stress_iter_items_keys_values(sdict):
104 it = zip(sdict.items(), sdict.keys(), sdict.values())
105 assert all(tup[0] == (tup[1], tup[2]) for tup in it)
106
107 @actor
108 def stress_pop(sdict):
109 keys = list(range(200))
110 for key in keys:
111 if key in sdict:
112 val = sdict[key]
113 assert sdict.pop(key, 1) == val
114 else:
115 assert sdict.pop(key, 1) == 1
116
117 @actor
118 def stress_popitem(sdict):
119 items = [sdict.popitem() for rpt in range(100)]
120 keys = [item[0] for item in items]
121 assert all(keys[pos - 1] > keys[pos] for pos in range(1, len(keys)))
122 assert all(key == -value for key, value in items)
123
124 @actor
125 def stress_setdefault(sdict):
126 keys = list(range(200))
127 for key in keys:
128 if key in sdict:
129 assert sdict.setdefault(key) == -key
130 else:
131 sdict.setdefault(key)
132 assert sdict[key] == None
133 del sdict[key]
134
135 def test_stress(repeat=1000):
136 sdict = SortedDict((val, -val) for val in range(1000))
137
138 for rpt in range(repeat):
139 action = random.choice(actions)
140 action(sdict)
141
142 try:
143 sdict._check()
144 except AssertionError:
145 print(action)
146 raise
147
148 start_len = len(sdict)
149
150 while len(sdict) < 500:
151 key = random.randrange(0, 2000)
152 sdict[key] = -key
153
154 while len(sdict) > 2000:
155 key = random.randrange(0, 2000)
156 if key in sdict:
157 del sdict[key]
158
159 if start_len != len(sdict):
160 sdict._check()
161
162 if __name__ == '__main__':
163 import sys
164 from datetime import datetime
165
166 start = datetime.now()
167
168 print('Python', sys.version_info)
169
170 try:
171 num = int(sys.argv[1])
172 print('Setting iterations to', num)
173 except:
174 print('Setting iterations to 1000 (default)')
175 num = 1000
176
177 try:
178 pea = int(sys.argv[2])
179 random.seed(pea)
180 print('Setting seed to', pea)
181 except:
182 print('Setting seed to 0 (default)')
183 random.seed(0)
184
185 try:
186 test_stress(num)
187 except:
188 raise
189 finally:
190 print('Exiting after', (datetime.now() - start))