java-topology/defects/vitest/tests/test-vitest-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

73 lines
2.5 KiB
Python

#!/usr/bin/env python3
# UNDF: UNDF-2026-000001295 (vitest-0001)
#
# CWE-407: Algorithmic Complexity
#
# Defect:
# vitest-0001: @vitest/coverage-v8 generateCoverage rebuilds missing
# startOffset via Array.find inside forEach. O(N*M) per
# coverage-file-read callback.
#
# Fix:
# Map<url, RawCoverageResult> built once per callback; O(1) lookup.
#
# Complexity gate (from bench/results.txt):
# N=M=10000 defective=2147ms, fixed=2.6ms (824x).
# Fixed must complete in <10ms at N=M=5000. 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-vitest-0001.py")
class TestVitest0001Correctness(unittest.TestCase):
def test_fixed_matches_defective_lookup(self):
# Sample data: missing startOffsets restored from original
coverage_result = [
{"url": "a", "startOffset": 10},
{"url": "b", "startOffset": 20},
{"url": "c", "startOffset": 30},
]
merged = [
{"url": "a", "startOffset": None},
{"url": "b", "startOffset": None},
{"url": "c", "startOffset": None},
{"url": "d", "startOffset": None}, # not in original
]
by_url = {r["url"]: r for r in coverage_result}
for r in merged:
if r["startOffset"] is None:
o = by_url.get(r["url"])
r["startOffset"] = o["startOffset"] if o else 0
self.assertEqual([r["startOffset"] for r in merged], [10, 20, 30, 0])
class TestVitest0001ComplexityGate(unittest.TestCase):
def test_fixed_wallclock_N5000(self):
t_s = min(_mod.bench_fixed(5000, 5000) for _ in range(3))
self.assertLess(t_s * 1000, 10.0,
f"fixed took {t_s*1000:.3f}ms at N=M=5000, expected <10ms")
def test_fixed_scaling_linear(self):
t_1000 = min(_mod.bench_fixed(1000, 1000) for _ in range(3))
t_5000 = min(_mod.bench_fixed(5000, 5000) for _ in range(3))
ratio = t_5000 / t_1000 if t_1000 > 0 else float("inf")
self.assertLess(ratio, 17.5,
f"fixed N=5000/N=1000 ratio {ratio:.2f}x, expected <17.5x")
if __name__ == "__main__":
unittest.main(verbosity=2)