java-topology/defects/check/tests/test-check-cwe407.py
russell@unturf.com d67ec93a5d test-frameworks wave 3: vitest + testng + jasmine + libcheck (4 patches)
vitest-0001: coverage-v8 coverage.result.find inside merged.result.forEach
  -> Map<url, result> lookup. Bench: 824x at N=M=10000 coverage entries.

testng-0001: DynamicGraph.toDot freeNodes.contains inside two for-each
  loops -> Map<T, String> color lookup via getOrDefault. Bench: 64x at N=2000.

jasmine-0001: SpyRegistry.spyOnAllFunctions propertiesToSkip.indexOf inside
  Array.filter + .concat growth across D prototype levels -> Set.has + O(1)
  growth. Bench: 61x at D=10, P=300.

check-0001: libcheck suite_tcase linear strcmp scan over tclst List
  -> parallel hashtable for O(1) lookup amortized. Bench: 117x at N=1000.
  Shipped as design sketch; full integration requires companion hashtable.

Also ships whitepaper/outreach/test-harness-survey.md documenting 14
clean-scan frameworks across Clojure, OCaml, Haskell, Erlang, Go, F#,
Julia, Shell, Lua, JS. Scope covered 61 targets across 30+ languages.

UNDF IDs: 1292 (check), 1293 (jasmine), 1294 (testng), 1295 (vitest).
All 12 tests pass.
2026-04-23 08:54:44 -04:00

64 lines
2.1 KiB
Python

#!/usr/bin/env python3
# UNDF: UNDF-2026-000001292 (check-0001)
#
# CWE-407: Algorithmic Complexity
#
# Defect:
# check-0001: suite_tcase walks s->tclst linearly with strcmp per entry.
# For N tcases and N lookups in the runner filter path, total
# cost is O(N^2).
#
# Fix:
# Maintain a parallel hashtable keyed by tcase name; lookup drops to O(1)
# amortized. Ordered List preserved for deterministic test-run output.
#
# Complexity gate (from bench/results.txt):
# N=1000, lookups=1000: defective=17ms, fixed=0.14ms (117x).
# Fixed must complete in <5ms at N=500. k-scaling <17.5x.
import importlib.util, os, sys, unittest
HERE = os.path.dirname(os.path.abspath(__file__))
BENCH = os.path.join(os.path.dirname(HERE), "bench")
sys.path.insert(0, BENCH)
def _load(fname):
path = os.path.join(BENCH, fname)
spec = importlib.util.spec_from_file_location(fname, path)
mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(mod)
return mod
_mod = _load("bench-check-0001.py")
class TestCheck0001Correctness(unittest.TestCase):
def test_hashtable_matches_linear_membership(self):
names = [f"tc_{i:03d}" for i in range(100)]
index = {n: {"name": n} for n in names}
# known-present
for n in names[::7]:
self.assertIn(n, index)
# known-absent
for n in ["tc_999", "tc_1000", "nope"]:
self.assertNotIn(n, index)
class TestCheck0001ComplexityGate(unittest.TestCase):
def test_fixed_wallclock_N500(self):
t_s = min(_mod.bench_fixed(500, 500) for _ in range(3))
self.assertLess(t_s * 1000, 5.0,
f"fixed took {t_s*1000:.3f}ms at N=500, expected <5ms")
def test_fixed_scaling_linear(self):
t_100 = min(_mod.bench_fixed(100, 100) for _ in range(3))
t_500 = min(_mod.bench_fixed(500, 500) for _ in range(3))
ratio = t_500 / t_100 if t_100 > 0 else float("inf")
self.assertLess(ratio, 17.5,
f"fixed N=500/N=100 ratio {ratio:.2f}x, expected <17.5x")
if __name__ == "__main__":
unittest.main(verbosity=2)