forked from BartMassey/nb-misc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstacktest.py
More file actions
35 lines (32 loc) · 957 Bytes
/
Copy pathstacktest.py
File metadata and controls
35 lines (32 loc) · 957 Bytes
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
# Stack test class.
# Copyright © 2014 Bart Massey
# [This program is licensed under the "MIT License"]
# Please see the file COPYING in the source
# distribution of this software for license terms.
from random import randrange
def stack_test(stack_constructor, *args):
def test():
a = []
for _ in range(randrange(100)):
a += [randrange(100)]
n = len(a)
s = stack_constructor(*args)
i = n // 2
for j in range(i):
s.push(a[j])
while i > 0:
if s.size() < n - 1 and randrange(2) == 1:
s.push(a[i])
i += 1
assert s.size() == i
assert not s.is_empty()
else:
v = s.pop()
i -= 1
assert v == a[i]
assert s.size() == i
assert s.is_empty()
for _ in range(100):
test()
print(".", end="")
print()