File: test_stress_sortedset.py

package info (click to toggle)
sortedcontainers 2.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid
  • size: 11,136 kB
  • sloc: python: 6,613; sh: 60; makefile: 18
file content (209 lines) | stat: -rw-r--r-- 4,585 bytes parent folder | download | duplicates (3)
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# -*- coding: utf-8 -*-

from __future__ import print_function
from sys import hexversion

import random
from .context import sortedcontainers
from sortedcontainers import SortedSet
from functools import wraps
import operator

if hexversion < 0x03000000:
    from itertools import izip as zip
    range = xrange

random.seed(0)
actions = []

def actor(func):
    actions.append(func)
    return func

def test_init():
    sst = SortedSet()
    sst._check()

    sst = SortedSet()
    sst._reset(10000)
    assert sst._list._load == 10000
    sst._check()

    sst = SortedSet(range(10000))
    assert all(tup[0] == tup[1] for tup in zip(sst, range(10000)))

    sst.clear()
    assert len(sst) == 0
    assert list(iter(sst)) == []
    sst._check()

@actor
def stress_contains(sst):
    values = list(sst)
    assert all((val in sst) for val in values)

@actor
def stress_delitem(sst):
    for rpt in range(100):
        pos = random.randrange(0, len(sst))
        del sst[pos]

@actor
def stress_operator(sst):
    other = SortedSet(sst)
    stress_delitem(other)
    assert other < sst
    assert sst > other

@actor
def stress_getitem(sst):
    other = list(sst)
    assert all(sst[pos] == other[pos] for pos in range(len(sst)))
    
@actor
def stress_reversed(sst):
    other = list(reversed(list(sst)))
    assert all(tup[0] == tup[1] for tup in zip(reversed(sst), other))
    
@actor
def stress_add(sst):
    for rpt in range(100):
        val = random.randrange(0, 1000)
        sst.add(val)

@actor
def stress_count(sst):
    for val in sst:
        assert sst.count(val) == 1

@actor
def stress_difference(sst):
    copy_one = sst.copy()
    stress_delitem(copy_one)
    copy_two = sst.copy()
    stress_delitem(copy_two)
    sst.difference_update(copy_one, copy_two)

@actor
def stress_discard(sst):
    for rpt in range(100):
        pos = random.randrange(0, len(sst))
        val = sst[pos]
        sst.discard(val)

@actor
def stress_index(sst):
    for rpt in range(100):
        pos = random.randrange(0, len(sst))
        val = sst[pos]
        assert pos == sst.index(val)

@actor
def stress_intersection(sst):
    copy_one = sst.copy()
    stress_delitem(copy_one)
    copy_two = sst.copy()
    stress_delitem(copy_two)
    sst.intersection_update(copy_one, copy_two)

@actor
def stress_symmetric_difference(sst):
    copy_one = sst.copy()
    stress_delitem(copy_one)
    sst.symmetric_difference_update(copy_one)

@actor
def stress_pop(sst):
    val = sst[-1]
    assert val == sst.pop()
    for rpt in range(100):
        pos = random.randrange(0, len(sst))
        val = sst[pos]
        assert val == sst.pop(pos)

@actor
def stress_remove(sst):
    for rpt in range(100):
        pos = random.randrange(0, len(sst))
        val = sst[pos]
        sst.remove(val)

@actor
def stress_update(sst):
    def iter_randomly(start, stop, count):
        for rpt in range(count):
            yield random.randrange(start, stop)
    sst.update(iter_randomly(0, 500, 100),
               iter_randomly(500, 1000, 100),
               iter_randomly(1000, 1500, 100),
               iter_randomly(1500, 2000, 100))

@actor
def stress_isdisjoint(sst):
    values = [-1, -2, -3]
    assert sst.isdisjoint(values)

@actor
def stress_issubset(sst):
    that = SortedSet(sst)
    that.update(range(1000))
    assert sst.issubset(that)

@actor
def stress_issuperset(sst):
    that = SortedSet(sst)
    assert sst.issuperset(that)

def test_stress(repeat=1000):
    sst = SortedSet(range(1000))

    for rpt in range(repeat):
        action = random.choice(actions)
        action(sst)

        try:
            sst._check()
        except AssertionError:
            print(action)
            raise

        start_len = len(sst)

        while len(sst) < 500:
            sst.add(random.randrange(0, 2000))

        while len(sst) > 2000:
            del sst[random.randrange(0, len(sst))]

        if start_len != len(sst):
            sst._check()

if __name__ == '__main__':
    import sys
    from datetime import datetime

    start = datetime.now()

    print('Python', sys.version_info)

    try:
        num = int(sys.argv[1])
        print('Setting iterations to', num)
    except:
        print('Setting iterations to 1000 (default)')
        num = 1000

    try:
        pea = int(sys.argv[2])
        random.seed(pea)
        print('Setting seed to', pea)
    except:
        print('Setting seed to 0 (default)')
        random.seed(0)

    try:
        test_stress(num)
    except:
        raise
    finally:
        print('Exiting after', (datetime.now() - start))