Debian Codegraph @

tests/test_stress_sortedset.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 SortedSet
9 from functools import wraps
10 import operator
11
12 if hexversion < 0x03000000:
13 from itertools import izip as zip
14 range = xrange
15
16 random.seed(0)
17 actions = []
18
19 def actor(func):
20 actions.append(func)
21 return func
22
23 def test_init():
24 sst = SortedSet()
25 sst._check()
26
27 sst = SortedSet()
28 sst._reset(10000)
29 assert sst._list._load == 10000
30 sst._check()
31
32 sst = SortedSet(range(10000))
33 assert all(tup[0] == tup[1] for tup in zip(sst, range(10000)))
34
35 sst.clear()
36 assert len(sst) == 0
37 assert list(iter(sst)) == []
38 sst._check()
39
40 @actor
41 def stress_contains(sst):
42 values = list(sst)
43 assert all((val in sst) for val in values)
44
45 @actor
46 def stress_delitem(sst):
47 for rpt in range(100):
48 pos = random.randrange(0, len(sst))
49 del sst[pos]
50
51 @actor
52 def stress_operator(sst):
53 other = SortedSet(sst)
54 stress_delitem(other)
55 assert other < sst
56 assert sst > other
57
58 @actor
59 def stress_getitem(sst):
60 other = list(sst)
61 assert all(sst[pos] == other[pos] for pos in range(len(sst)))
62
63 @actor
64 def stress_reversed(sst):
65 other = list(reversed(list(sst)))
66 assert all(tup[0] == tup[1] for tup in zip(reversed(sst), other))
67
68 @actor
69 def stress_add(sst):
70 for rpt in range(100):
71 val = random.randrange(0, 1000)
72 sst.add(val)
73
74 @actor
75 def stress_count(sst):
76 for val in sst:
77 assert sst.count(val) == 1
78
79 @actor
80 def stress_difference(sst):
81 copy_one = sst.copy()
82 stress_delitem(copy_one)
83 copy_two = sst.copy()
84 stress_delitem(copy_two)
85 sst.difference_update(copy_one, copy_two)
86
87 @actor
88 def stress_discard(sst):
89 for rpt in range(100):
90 pos = random.randrange(0, len(sst))
91 val = sst[pos]
92 sst.discard(val)
93
94 @actor
95 def stress_index(sst):
96 for rpt in range(100):
97 pos = random.randrange(0, len(sst))
98 val = sst[pos]
99 assert pos == sst.index(val)
100
101 @actor
102 def stress_intersection(sst):
103 copy_one = sst.copy()
104 stress_delitem(copy_one)
105 copy_two = sst.copy()
106 stress_delitem(copy_two)
107 sst.intersection_update(copy_one, copy_two)
108
109 @actor
110 def stress_symmetric_difference(sst):
111 copy_one = sst.copy()
112 stress_delitem(copy_one)
113 sst.symmetric_difference_update(copy_one)
114
115 @actor
116 def stress_pop(sst):
117 val = sst[-1]
118 assert val == sst.pop()
119 for rpt in range(100):
120 pos = random.randrange(0, len(sst))
121 val = sst[pos]
122 assert val == sst.pop(pos)
123
124 @actor
125 def stress_remove(sst):
126 for rpt in range(100):
127 pos = random.randrange(0, len(sst))
128 val = sst[pos]
129 sst.remove(val)
130
131 @actor
132 def stress_update(sst):
133 def iter_randomly(start, stop, count):
134 for rpt in range(count):
135 yield random.randrange(start, stop)
136 sst.update(iter_randomly(0, 500, 100),
137 iter_randomly(500, 1000, 100),
138 iter_randomly(1000, 1500, 100),
139 iter_randomly(1500, 2000, 100))
140
141 @actor
142 def stress_isdisjoint(sst):
143 values = [-1, -2, -3]
144 assert sst.isdisjoint(values)
145
146 @actor
147 def stress_issubset(sst):
148 that = SortedSet(sst)
149 that.update(range(1000))
150 assert sst.issubset(that)
151
152 @actor
153 def stress_issuperset(sst):
154 that = SortedSet(sst)
155 assert sst.issuperset(that)
156
157 def test_stress(repeat=1000):
158 sst = SortedSet(range(1000))
159
160 for rpt in range(repeat):
161 action = random.choice(actions)
162 action(sst)
163
164 try:
165 sst._check()
166 except AssertionError:
167 print(action)
168 raise
169
170 start_len = len(sst)
171
172 while len(sst) < 500:
173 sst.add(random.randrange(0, 2000))
174
175 while len(sst) > 2000:
176 del sst[random.randrange(0, len(sst))]
177
178 if start_len != len(sst):
179 sst._check()
180
181 if __name__ == '__main__':
182 import sys
183 from datetime import datetime
184
185 start = datetime.now()
186
187 print('Python', sys.version_info)
188
189 try:
190 num = int(sys.argv[1])
191 print('Setting iterations to', num)
192 except:
193 print('Setting iterations to 1000 (default)')
194 num = 1000
195
196 try:
197 pea = int(sys.argv[2])
198 random.seed(pea)
199 print('Setting seed to', pea)
200 except:
201 print('Setting seed to 0 (default)')
202 random.seed(0)
203
204 try:
205 test_stress(num)
206 except:
207 raise
208 finally:
209 print('Exiting after', (datetime.now() - start))