Files
troupe-standup/wizard-work/beam/test_sliding_window_median.py
spark bfa590269c beam: Q76 -- median cells are collected by the lane gate, and CHECK is green unmasked
test_sliding_window_median.py: the seven cells were module-level functions
behind `if __name__ == "__main__"`, so `unittest discover` collected none of
them -- `python3 -m unittest test_sliding_window_median` printed `Ran 0
tests` (rc 5 on Python 3.14) and the lane gate stayed at `Ran 151` while a
module with a floored even-median passed it at rc 0. They are TestCase
methods now, with the same seven assertions, the module resolved from this
file's directory (Q61 rule), and one added cell, test_cell_census, which
pins the count at 8 so a deleted cell is a red gate.

test_robin_hood_hash.py: `sys.path.insert(0, "/root/.hermes/wizard-outputs
/beam")` -> `str(pathlib.Path(__file__).resolve().parent)`. That host insert
is what made CHECK rc 1 unmasked with `ImportError: 'test_safe_eval' module
incorrectly imported from /root/.hermes/wizard-outputs/beam`.

Measured at this commit: `bash wizard-work/beam/CHECK` rc 0, `Ran 159 tests`,
OK -- unmasked, with /root/.hermes/wizard-outputs masked to an empty tmpfs,
and in a copied lane with no .git. 151 + 8 = 159. Mutants: 8 of 9 killed
(M8 is an equivalent mutant, 0 differences in 20,000 batch and 5,000
incremental cases); with the old test file and a floored even-median the gate
is rc 0, Ran 151, OK. Record: wizard-work/volt/q76-2026-10-26.md.
2026-10-26 11:18:04 +00:00

93 lines
3.4 KiB
Python

"""Tests for sliding_window_median.
Runs two ways, both against the bytes in this directory:
python3 -m unittest test_sliding_window_median -v
python3 -m unittest discover -p 'test_*.py' (this lane's CHECK)
Q76 / 2026-10-26: the seven cells were module-level functions called from an
`if __name__ == "__main__"` loop, so `unittest` collected none of them --
`python3 -m unittest test_sliding_window_median` printed `Ran 0 tests` and
returned rc 0 while 158 lines of module + tests sat outside the lane gate.
They are TestCase methods now, which is what `discover` collects. The module
under test is resolved from THIS file's directory and from nowhere else (the
Q61 / test_toposort.py rule): a host-directory `sys.path.insert` here is what
made this lane's CHECK red from 2026-10-25 08:02 onward.
"""
import pathlib
import random
import sys
import unittest
HERE = pathlib.Path(__file__).resolve().parent
sys.path.insert(0, str(HERE))
from sliding_window_median import sliding_window_median, SlidingWindowMedian # noqa: E402
def brute(nums, k):
out = []
for i in range(len(nums) - k + 1):
w = sorted(nums[i:i + k])
if k % 2:
out.append(float(w[k // 2]))
else:
out.append((w[k // 2 - 1] + w[k // 2]) / 2.0)
return out
class TestSlidingWindowMedian(unittest.TestCase):
def test_basic(self):
self.assertEqual(
sliding_window_median([1, 3, -1, -3, 5, 3, 6, 7], 3),
[1.0, -1.0, -1.0, 3.0, 5.0, 6.0],
)
def test_even(self):
self.assertEqual(sliding_window_median([1, 2, 3, 4], 2), [1.5, 2.5, 3.5])
def test_single(self):
self.assertEqual(sliding_window_median([5], 1), [5.0])
def test_empty(self):
self.assertEqual(sliding_window_median([], 3), [])
self.assertEqual(sliding_window_median([1, 2], 0), [])
def test_random(self):
for _ in range(300):
n = random.randint(1, 40)
k = random.randint(1, n)
nums = [random.randint(-20, 20) for _ in range(n)]
got = sliding_window_median(nums, k)
exp = brute(nums, k)
self.assertEqual(len(got), len(exp))
for a, b in zip(got, exp):
self.assertLess(abs(a - b), 1e-9, (nums, k, a, b))
def test_incremental(self):
swm = SlidingWindowMedian(3)
self.assertIsNone(swm.add(1))
self.assertIsNone(swm.add(3))
self.assertEqual(swm.add(-1), 1.0)
self.assertEqual(swm.add(-3), -1.0)
def test_duplicates_removal(self):
# duplicates where removal ambiguity exists
self.assertEqual(sliding_window_median([2, 2, 2, 2], 2), [2.0, 2.0, 2.0])
def test_cell_census(self):
"""Pin how many cells this file has, so a deleted cell is a red gate.
The Q76 fault was quantitative: 158 lines landed, 0 cells were
collected, and the lane's `Ran 151` did not move. A deletion cannot
redden a green suite on its own -- nothing fails when a passing cell
disappears -- so the count is asserted here, next to the cells it
counts. Adding a cell means bumping this number in the same commit,
exactly as a `Ran N tests` move is stated in the commit message.
"""
cells = sorted(n for n in dir(self) if n.startswith("test_"))
self.assertEqual(len(cells), 8, cells) # 7 behaviour cells + this census
if __name__ == "__main__":
unittest.main()