| 1 |
# -*- coding: utf-8 -*- |
| 2 |
|
| 3 |
from __future__ import print_function |
| 4 |
from sys import hexversion |
| 5 |
|
| 6 |
import copy |
| 7 |
import bisect |
| 8 |
import random |
| 9 |
from .context import sortedcontainers |
| 10 |
from sortedcontainers import SortedList |
| 11 |
from functools import wraps |
| 12 |
|
| 13 |
if hexversion < 0x03000000: |
| 14 |
from itertools import izip as zip |
| 15 |
range = xrange |
| 16 |
|
| 17 |
random.seed(0) |
| 18 |
actions = [] |
| 19 |
|
| 20 |
def frange(start, stop, step): |
| 21 |
while start < stop: |
| 22 |
yield start |
| 23 |
start += step |
| 24 |
|
| 25 |
class actor: |
| 26 |
def __init__(self, count): |
| 27 |
self._count = count |
| 28 |
def __call__(self, func): |
| 29 |
actions.extend([func] * self._count) |
| 30 |
return func |
| 31 |
|
| 32 |
def not_empty(func): |
| 33 |
@wraps(func) |
| 34 |
def wrapper(slt): |
| 35 |
if len(slt) < 100: |
| 36 |
stress_update(slt) |
| 37 |
func(slt) |
| 38 |
return wrapper |
| 39 |
|
| 40 |
@actor(1) |
| 41 |
def stress_clear(slt): |
| 42 |
if random.randrange(100) < 10: |
| 43 |
slt.clear() |
| 44 |
else: |
| 45 |
values = list(slt) |
| 46 |
slt.clear() |
| 47 |
slt.update(values[:int(len(values) / 2)]) |
| 48 |
|
| 49 |
@actor(1) |
| 50 |
def stress_add(slt): |
| 51 |
if random.randrange(100) < 10: |
| 52 |
slt.clear() |
| 53 |
slt.add(random.random()) |
| 54 |
|
| 55 |
@actor(1) |
| 56 |
def stress_update(slt): |
| 57 |
slt.update((random.random() for rpt in range(350))) |
| 58 |
|
| 59 |
@actor(1) |
| 60 |
@not_empty |
| 61 |
def stress_contains(slt): |
| 62 |
if random.randrange(100) < 10: |
| 63 |
slt.clear() |
| 64 |
assert 0 not in slt |
| 65 |
else: |
| 66 |
val = slt[random.randrange(len(slt))] |
| 67 |
assert val in slt |
| 68 |
assert 1 not in slt |
| 69 |
|
| 70 |
@actor(1) |
| 71 |
@not_empty |
| 72 |
def stress_discard(slt): |
| 73 |
val = slt[random.randrange(len(slt))] |
| 74 |
slt.discard(val) |
| 75 |
|
| 76 |
@actor(1) |
| 77 |
def stress_discard2(slt): |
| 78 |
if random.randrange(100) < 10: |
| 79 |
slt.clear() |
| 80 |
slt.discard(random.random()) |
| 81 |
|
| 82 |
@actor(1) |
| 83 |
def stress_remove(slt): |
| 84 |
if len(slt) > 0: |
| 85 |
val = slt[random.randrange(len(slt))] |
| 86 |
slt.remove(val) |
| 87 |
|
| 88 |
try: |
| 89 |
slt.remove(1) |
| 90 |
assert False |
| 91 |
except ValueError: |
| 92 |
pass |
| 93 |
|
| 94 |
try: |
| 95 |
slt.remove(-1) |
| 96 |
assert False |
| 97 |
except ValueError: |
| 98 |
pass |
| 99 |
|
| 100 |
@actor(1) |
| 101 |
@not_empty |
| 102 |
def stress_delitem(slt): |
| 103 |
del slt[random.randrange(len(slt))] |
| 104 |
|
| 105 |
@actor(1) |
| 106 |
def stress_getitem(slt): |
| 107 |
if len(slt) > 0: |
| 108 |
pos = random.randrange(len(slt)) |
| 109 |
assert slt[pos] == list(slt)[pos] |
| 110 |
|
| 111 |
try: |
| 112 |
slt[-(slt._len + 5)] |
| 113 |
assert False |
| 114 |
except IndexError: |
| 115 |
pass |
| 116 |
|
| 117 |
try: |
| 118 |
slt[slt._len + 5] |
| 119 |
assert False |
| 120 |
except IndexError: |
| 121 |
pass |
| 122 |
else: |
| 123 |
try: |
| 124 |
slt[0] |
| 125 |
assert False |
| 126 |
except IndexError: |
| 127 |
pass |
| 128 |
|
| 129 |
@actor(1) |
| 130 |
@not_empty |
| 131 |
def stress_delitem_slice(slt): |
| 132 |
start, stop = sorted(random.randrange(len(slt)) for rpt in range(2)) |
| 133 |
step = random.choice([-3, -2, -1, 1, 1, 1, 1, 1, 2, 3]) |
| 134 |
del slt[start:stop:step] |
| 135 |
|
| 136 |
@actor(1) |
| 137 |
def stress_iter(slt): |
| 138 |
itr1 = iter(slt) |
| 139 |
itr2 = (slt[pos] for pos in range(len(slt))) |
| 140 |
assert all(tup[0] == tup[1] for tup in zip(itr1, itr2)) |
| 141 |
|
| 142 |
@actor(1) |
| 143 |
def stress_reversed(slt): |
| 144 |
itr = reversed(list(reversed(slt))) |
| 145 |
assert all(tup[0] == tup[1] for tup in zip(slt, itr)) |
| 146 |
|
| 147 |
@actor(1) |
| 148 |
def stress_islice(slt): |
| 149 |
if len(slt) < 10: return |
| 150 |
start = random.randrange(len(slt) - 5) |
| 151 |
stop = random.randrange(start, len(slt)) |
| 152 |
itr = slt.islice(start, stop) |
| 153 |
assert all(slt[pos] == next(itr) for pos in range(start, stop)) |
| 154 |
|
| 155 |
@actor(1) |
| 156 |
def stress_irange(slt): |
| 157 |
values = sorted(set(slt)) |
| 158 |
slt.clear() |
| 159 |
slt.update(values) |
| 160 |
if len(slt) < 10: return |
| 161 |
start = random.randrange(len(slt) - 5) |
| 162 |
stop = random.randrange(start, len(slt)) |
| 163 |
itr = slt.irange(slt[start], slt[stop], inclusive=(True, False)) |
| 164 |
assert all(slt[pos] == next(itr) for pos in range(start, stop)) |
| 165 |
|
| 166 |
@actor(1) |
| 167 |
def stress_bisect_left(slt): |
| 168 |
values = list(slt) |
| 169 |
value = random.random() |
| 170 |
values.sort() |
| 171 |
assert bisect.bisect_left(values, value) == slt.bisect_left(value) |
| 172 |
|
| 173 |
@actor(1) |
| 174 |
def stress_bisect(slt): |
| 175 |
values = list(slt) |
| 176 |
value = random.random() |
| 177 |
values.sort() |
| 178 |
assert bisect.bisect(values, value) == slt.bisect(value) |
| 179 |
|
| 180 |
@actor(1) |
| 181 |
def stress_bisect_right(slt): |
| 182 |
values = list(slt) |
| 183 |
value = random.random() |
| 184 |
values.sort() |
| 185 |
assert bisect.bisect_right(values, value) == slt.bisect_right(value) |
| 186 |
|
| 187 |
@actor(1) |
| 188 |
@not_empty |
| 189 |
def stress_dups(slt): |
| 190 |
pos = min(random.randrange(len(slt)), 300) |
| 191 |
val = slt[pos] |
| 192 |
for rpt in range(pos): |
| 193 |
slt.add(val) |
| 194 |
|
| 195 |
@actor(1) |
| 196 |
@not_empty |
| 197 |
def stress_count(slt): |
| 198 |
values = list(slt) |
| 199 |
val = slt[random.randrange(len(slt))] |
| 200 |
assert slt.count(val) == values.count(val) |
| 201 |
|
| 202 |
@actor(1) |
| 203 |
@not_empty |
| 204 |
def stress_pop(slt): |
| 205 |
pos = random.randrange(len(slt)) + 1 |
| 206 |
assert slt[-pos] == slt.pop(-pos) |
| 207 |
|
| 208 |
@actor(1) |
| 209 |
@not_empty |
| 210 |
def stress_index(slt): |
| 211 |
values = set(slt) |
| 212 |
slt.clear() |
| 213 |
slt.update(values) |
| 214 |
pos = random.randrange(len(slt)) |
| 215 |
assert slt.index(slt[pos]) == pos |
| 216 |
|
| 217 |
@actor(1) |
| 218 |
@not_empty |
| 219 |
def stress_index2(slt): |
| 220 |
values = list(slt)[:3] * 200 |
| 221 |
slt = SortedList(values) |
| 222 |
for idx, val in enumerate(slt): |
| 223 |
assert slt.index(val, idx) == idx |
| 224 |
|
| 225 |
@actor(1) |
| 226 |
def stress_mul(slt): |
| 227 |
values = list(slt) |
| 228 |
mult = random.randrange(10) |
| 229 |
values *= mult |
| 230 |
values.sort() |
| 231 |
assert (slt * mult) == values |
| 232 |
|
| 233 |
@actor(1) |
| 234 |
def stress_imul(slt): |
| 235 |
mult = random.randrange(10) |
| 236 |
slt *= mult |
| 237 |
|
| 238 |
@actor(1) |
| 239 |
@not_empty |
| 240 |
def stress_reversed(slt): |
| 241 |
itr = reversed(slt) |
| 242 |
pos = random.randrange(1, len(slt)) |
| 243 |
for rpt in range(pos): |
| 244 |
val = next(itr) |
| 245 |
assert val == slt[-pos] |
| 246 |
|
| 247 |
@actor(1) |
| 248 |
@not_empty |
| 249 |
def stress_eq(slt): |
| 250 |
values = [] |
| 251 |
assert not (values == slt) |
| 252 |
|
| 253 |
@actor(1) |
| 254 |
@not_empty |
| 255 |
def stress_lt(slt): |
| 256 |
values = list(slt) |
| 257 |
assert not (values < slt) |
| 258 |
values = SortedList(value - 1 for value in values) |
| 259 |
assert values < slt |
| 260 |
values = [] |
| 261 |
assert values < slt |
| 262 |
assert not (slt < values) |
| 263 |
|
| 264 |
def test_stress(repeat=1000): |
| 265 |
slt = SortedList((random.random() for rpt in range(1000))) |
| 266 |
slt._reset(23) |
| 267 |
|
| 268 |
for rpt in range(repeat): |
| 269 |
action = random.choice(actions) |
| 270 |
action(slt) |
| 271 |
|
| 272 |
slt._check() |
| 273 |
|
| 274 |
fourth = int(len(slt) / 4) |
| 275 |
count = 0 if fourth == 0 else random.randrange(-fourth, fourth) |
| 276 |
|
| 277 |
while count > 0: |
| 278 |
slt.add(random.random()) |
| 279 |
count -= 1 |
| 280 |
|
| 281 |
while count < 0: |
| 282 |
pos = random.randrange(len(slt)) |
| 283 |
del slt[pos] |
| 284 |
count += 1 |
| 285 |
|
| 286 |
while len(slt) > 2000: |
| 287 |
# Shorten the sortedlist. This maintains the "jaggedness" |
| 288 |
# of the sublists which helps coverage. |
| 289 |
pos = random.randrange(len(slt._maxes)) |
| 290 |
del slt._maxes[pos] |
| 291 |
del slt._lists[pos] |
| 292 |
slt._len = sum(len(sublist) for sublist in slt._lists) |
| 293 |
slt._index = [] |
| 294 |
slt._check() |
| 295 |
|
| 296 |
slt._check() |
| 297 |
|
| 298 |
slt._check() |
| 299 |
|
| 300 |
stress_update(slt) |
| 301 |
|
| 302 |
while len(slt) > 0: |
| 303 |
pos = random.randrange(len(slt)) |
| 304 |
del slt[pos] |
| 305 |
|
| 306 |
slt._check() |
| 307 |
|
| 308 |
if __name__ == '__main__': |
| 309 |
import sys |
| 310 |
from datetime import datetime |
| 311 |
|
| 312 |
start = datetime.now() |
| 313 |
|
| 314 |
print('Python', sys.version_info) |
| 315 |
|
| 316 |
try: |
| 317 |
num = int(sys.argv[1]) |
| 318 |
print('Setting iterations to', num) |
| 319 |
except: |
| 320 |
print('Setting iterations to 1000 (default)') |
| 321 |
num = 1000 |
| 322 |
|
| 323 |
try: |
| 324 |
pea = int(sys.argv[2]) |
| 325 |
random.seed(pea) |
| 326 |
print('Setting seed to', pea) |
| 327 |
except: |
| 328 |
print('Setting seed to 0 (default)') |
| 329 |
random.seed(0) |
| 330 |
|
| 331 |
try: |
| 332 |
test_stress(num) |
| 333 |
except: |
| 334 |
raise |
| 335 |
finally: |
| 336 |
print('Exiting after', (datetime.now() - start)) |