Files
troupe-standup/wizard-work/beam/test_next_greater.py
T

73 lines
2.5 KiB
Python

"""Tests for next_greater.* -- differential cells against the brute force.
Runs two ways, both from this directory only:
python3 test_next_greater.py # the lane's script-style runner
python3 run_lane_tests.py # the lane gate collects the bare defs
Q153 / 2026-11-02: the module is resolved from THIS file's directory and from
nowhere else. The previous `sys.path.insert(0, '/root/.hermes/wizard-outputs/beam')`
graded the host copy, not the committed bytes: measured at `096ba9b`, the file
run from a directory holding no `next_greater.py` still read rc 0, and run
beside a copy poisoned at `next_greater_right` it still read rc 0, because the
absolute insert answered both from /root/.hermes/wizard-outputs/beam. The host
copy and the tree copy were byte-identical (sha256 f015c464...), which is
exactly why nothing looked wrong. `MODULE_PATH` is asserted below, so the
binding is a graded fact rather than an intention.
"""
import pathlib
import random
import sys
HERE = pathlib.Path(__file__).resolve().parent
sys.path.insert(0, str(HERE))
import next_greater as _ng
from next_greater import (
next_greater_right, next_greater_left, next_greater_circular,
_brute_right, _brute_left, _brute_circ,
)
# The module under test must be the committed file next to this test.
MODULE_PATH = pathlib.Path(_ng.__file__).resolve()
assert MODULE_PATH == (HERE / "next_greater.py"), (MODULE_PATH, HERE)
def test_right_random():
rng = random.Random(42)
for _ in range(2000):
n = rng.randint(0, 40)
nums = [rng.randint(-5, 5) for _ in range(n)]
assert next_greater_right(nums) == _brute_right(nums), nums
def test_left_random():
rng = random.Random(7)
for _ in range(2000):
n = rng.randint(0, 40)
nums = [rng.randint(-5, 5) for _ in range(n)]
assert next_greater_left(nums) == _brute_left(nums), nums
def test_circ_random():
rng = random.Random(99)
for _ in range(2000):
n = rng.randint(0, 40)
nums = [rng.randint(-5, 5) for _ in range(n)]
assert next_greater_circular(nums) == _brute_circ(nums), nums
def test_edge():
assert next_greater_right([]) == []
assert next_greater_left([]) == []
assert next_greater_circular([]) == []
assert next_greater_right([5]) == [-1]
assert next_greater_circular([1, 2, 3]) == [1, 2, -1]
if __name__ == "__main__":
for _name in ("test_right_random", "test_left_random", "test_circ_random",
"test_edge"):
globals()[_name]()
print("PASS %s" % _name)
print("ALL PASS")