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

59 lines
1.9 KiB
Python

#!/usr/bin/env python3
# UNDF: UNDF-2026-000001294 (testng-0001)
#
# CWE-407: Algorithmic Complexity
#
# Defect:
# testng-0001: DynamicGraph.toDot iterates m_nodesReady and m_nodesRunning
# calling freeNodes.contains(n) per node. List.contains is O(F);
# total cost O(N*F) per .dot emission.
#
# Fix:
# HashSet<T> freeNodeSet built once before the loops; O(1) per contains().
#
# Complexity gate (from bench/results.txt):
# N=2000 defective=70ms, fixed=1.1ms (64x).
# 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-testng-0001.py")
class TestTestng0001Correctness(unittest.TestCase):
def test_set_matches_list_contains_semantics(self):
free_list = [1, 3, 5, 7, 9]
free_set = set(free_list)
for i in range(15):
self.assertEqual(i in free_set, i in free_list)
class TestTestng0001ComplexityGate(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_200 = min(_mod.bench_fixed(200, 200) for _ in range(3))
t_1000 = min(_mod.bench_fixed(1000, 1000) for _ in range(3))
ratio = t_1000 / t_200 if t_200 > 0 else float("inf")
self.assertLess(ratio, 17.5,
f"fixed N=1000/N=200 ratio {ratio:.2f}x, expected <17.5x")
if __name__ == "__main__":
unittest.main(verbosity=2)