31 lines
1.1 KiB
Python
31 lines
1.1 KiB
Python
import pathlib, random, sys
|
|
|
|
# Bind the module from THIS file's directory, never from a host path (Q155:
|
|
# this line read "/root/.hermes/wizard-outputs/beam", so the committed
|
|
# largest_rect_hist.py was never the module the cells graded).
|
|
HERE = pathlib.Path(__file__).resolve().parent
|
|
sys.path.insert(0, str(HERE))
|
|
from largest_rect_hist import largest_rectangle_area, _brute
|
|
_MODULE_FILE = sys.modules["largest_rect_hist"].__file__
|
|
assert _MODULE_FILE is not None, "largest_rect_hist imported without a __file__"
|
|
MODULE_PATH = pathlib.Path(_MODULE_FILE).resolve()
|
|
assert MODULE_PATH == HERE / "largest_rect_hist.py", MODULE_PATH
|
|
|
|
def test_random():
|
|
rng = random.Random(2024)
|
|
for _ in range(5000):
|
|
n = rng.randint(0, 30)
|
|
hs = [rng.randint(0, 8) for _ in range(n)]
|
|
assert largest_rectangle_area(hs) == _brute(hs), hs
|
|
|
|
def test_edges():
|
|
assert largest_rectangle_area([]) == 0
|
|
assert largest_rectangle_area([2,1,5,6,2,3]) == 10
|
|
assert largest_rectangle_area([2,4]) == 4
|
|
assert largest_rectangle_area([1]) == 1
|
|
assert largest_rectangle_area([0,0,0]) == 0
|
|
|
|
test_random()
|
|
test_edges()
|
|
print("ALL OK")
|