63 lines
1.7 KiB
Python
63 lines
1.7 KiB
Python
import random
|
|
import sys
|
|
sys.path.insert(0, "/root/.hermes/wizard-outputs/beam")
|
|
from interval_merge import merge_intervals, total_covered, gaps
|
|
|
|
def brute_merge(intervals):
|
|
pts = set()
|
|
# discrete integer points model for small ranges
|
|
pts_all = set()
|
|
for s, e in intervals:
|
|
for x in range(s, e):
|
|
pts_all.add(x)
|
|
if not pts_all:
|
|
return []
|
|
# build runs
|
|
out = []
|
|
for x in sorted(pts_all):
|
|
if not out or x != out[-1][1]:
|
|
out.append([x, x+1])
|
|
else:
|
|
out[-1][1] = x+1
|
|
return [tuple(r) for r in out]
|
|
|
|
random.seed(42)
|
|
for trial in range(2000):
|
|
n = random.randint(0, 8)
|
|
ivs = []
|
|
for _ in range(n):
|
|
s = random.randint(-5, 5)
|
|
e = s + random.randint(1, 6)
|
|
ivs.append((s, e))
|
|
got = merge_intervals(ivs)
|
|
exp = brute_merge(ivs)
|
|
assert got == exp, (ivs, got, exp)
|
|
|
|
# total_covered
|
|
assert total_covered([(1,4),(3,6),(10,12)]) == (6-1)+(12-10) # 5+2=7? 1..6 =5, 10..12=2 =>7
|
|
assert total_covered([]) == 0
|
|
|
|
# gaps
|
|
assert gaps([(2,4)], 0, 6) == [(0,2),(4,6)]
|
|
assert gaps([(0,6)], 0, 6) == []
|
|
assert gaps([], 0, 3) == [(0,3)]
|
|
|
|
# validation
|
|
for bad in [((1,1),(2,3)), (1,2,3), (True,5), (1,True), ("a",2), (2,1)]:
|
|
try:
|
|
merge_intervals([bad] if not isinstance(bad[0], tuple) or len(bad)!=2 or not isinstance(bad[0], int) else [bad])
|
|
except ValueError:
|
|
pass
|
|
else:
|
|
# some cases may be wrapped; ensure at least invalid ones raise
|
|
pass
|
|
|
|
# explicit invalid
|
|
for bad in [(1,1),(2,1),(True,3),(1,True)]:
|
|
try:
|
|
merge_intervals([bad])
|
|
raise AssertionError(f"no raise for {bad}")
|
|
except ValueError:
|
|
pass
|
|
|
|
print("ALL TESTS PASS") |