31 lines
957 B
Python
31 lines
957 B
Python
import random
|
|
from segment_tree import SegmentTree
|
|
|
|
random.seed(42)
|
|
for trial in range(2000):
|
|
n = random.randint(1, 12)
|
|
data = [random.randint(-5, 5) for _ in range(n)]
|
|
st = SegmentTree(data)
|
|
arr = list(data)
|
|
for op in range(50):
|
|
if random.random() < 0.5:
|
|
l = random.randint(0, n-1)
|
|
r = random.randint(l, n-1)
|
|
v = random.randint(-5, 5)
|
|
st.range_add(l, r, v)
|
|
for i in range(l, r+1):
|
|
arr[i] += v
|
|
else:
|
|
l = random.randint(0, n-1)
|
|
r = random.randint(l, n-1)
|
|
got = st.range_sum(l, r)
|
|
exp = sum(arr[l:r+1])
|
|
if got != exp:
|
|
print("MISMATCH trial", trial, "op", op, l, r, got, exp)
|
|
raise SystemExit(1)
|
|
# full list check
|
|
if st.to_list() != arr:
|
|
print("LIST MISMATCH", st.to_list(), arr)
|
|
raise SystemExit(1)
|
|
print("ALL 2000 TRIALS PASSED")
|