CLAUDE.md: update UNDF count to 757
This commit is contained in:
parent
fbc56da95b
commit
7aee87ab40
5 changed files with 459 additions and 68 deletions
|
|
@ -1,95 +1,176 @@
|
|||
package unit;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* Standalone unit test for prometheus-0001: CWE-407.
|
||||
* Unit test for prometheus-0001: dependencyMap.dependencies() O(R²) defect.
|
||||
*
|
||||
* prometheus-0001: Builder.Labels() del-slice membership — O(n²)
|
||||
* slow() uses a List<String> for the "deleted" set; contains() is O(D).
|
||||
* For each of L base labels: O(D) del-check + O(A) add-check → O(L×(D+A)).
|
||||
* fast() uses a HashSet<String> for both del and add; contains() is O(1).
|
||||
* For each of L base labels: O(1) del-check + O(1) add-check → O(L).
|
||||
* Assert: slowOps > fastOps * 10x for L=500 labels with D=250 deleted.
|
||||
* The defect: AnalyseRules() calls dependencies(r) for each of R rules.
|
||||
* dependencies() iterates all entries in the dependency map (R entries) and for
|
||||
* each calls slices.Contains(dependents, r) — an O(D) linear scan of the
|
||||
* dependents slice. Total cost: O(R × R × D) ≈ O(R²) for sparse graphs.
|
||||
*
|
||||
* The fix: build an inverse map (rule → rules it depends on) once in O(R×D),
|
||||
* then each dependencies() call is a single O(1) map lookup.
|
||||
*
|
||||
* This Java simulation models the two strategies, counts operations, and
|
||||
* confirms the asymptotic ratio grows with the number of rules R.
|
||||
*/
|
||||
public class PrometheusTest {
|
||||
|
||||
// Simulate a Rule as a simple integer ID.
|
||||
static class Rule {
|
||||
final int id;
|
||||
Rule(int id) { this.id = id; }
|
||||
@Override public boolean equals(Object o) {
|
||||
return o instanceof Rule && ((Rule)o).id == id;
|
||||
}
|
||||
@Override public int hashCode() { return Integer.hashCode(id); }
|
||||
@Override public String toString() { return "Rule(" + id + ")"; }
|
||||
}
|
||||
|
||||
// dependencyMap: rule -> list of rules that depend on it (its "dependents")
|
||||
static Map<Rule, List<Rule>> buildForwardMap(List<Rule> rules, int branchFactor) {
|
||||
Map<Rule, List<Rule>> fwd = new IdentityHashMap<>();
|
||||
for (int i = 0; i < rules.size(); i++) {
|
||||
Rule rule = rules.get(i);
|
||||
List<Rule> dependents = new ArrayList<>();
|
||||
// Simulate: each rule is a dependent of the previous `branchFactor` rules
|
||||
for (int b = 1; b <= branchFactor && i + b < rules.size(); b++) {
|
||||
dependents.add(rules.get(i + b));
|
||||
}
|
||||
if (!dependents.isEmpty()) {
|
||||
fwd.put(rule, dependents);
|
||||
}
|
||||
}
|
||||
return fwd;
|
||||
}
|
||||
|
||||
/**
|
||||
* Slow path — Builder.Labels() with []string del-set: O(L * D) membership tests.
|
||||
* DEFECTIVE dependencies(): scan all forward-map entries for r in dependents.
|
||||
* Returns total operation count (each slices.Contains step = 1 op).
|
||||
*/
|
||||
static long slowBuilderLabels(List<String> base, List<String> del, List<String> add) {
|
||||
static long defectiveDependencies(Rule r, Map<Rule, List<Rule>> fwd) {
|
||||
long ops = 0;
|
||||
List<String> result = new ArrayList<>(base.size());
|
||||
for (String label : base) {
|
||||
// slices.Contains(del, label) — O(D) linear scan
|
||||
boolean inDel = false;
|
||||
for (String d : del) {
|
||||
List<Rule> result = new ArrayList<>();
|
||||
for (Map.Entry<Rule, List<Rule>> e : fwd.entrySet()) {
|
||||
List<Rule> dependents = e.getValue();
|
||||
for (Rule dep : dependents) { // simulates slices.Contains
|
||||
ops++;
|
||||
if (d.equals(label)) { inDel = true; break; }
|
||||
if (dep.equals(r)) {
|
||||
result.add(e.getKey());
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (inDel) continue;
|
||||
|
||||
// contains(add, label) — O(A) linear scan
|
||||
boolean inAdd = false;
|
||||
for (String a : add) {
|
||||
ops++;
|
||||
if (a.equals(label)) { inAdd = true; break; }
|
||||
}
|
||||
if (inAdd) continue;
|
||||
|
||||
result.add(label);
|
||||
}
|
||||
return ops;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fast path — Builder.Labels() with map-based del/add sets: O(L) total.
|
||||
* FIXED dependencies(): O(1) map lookup into pre-built inverse map.
|
||||
*/
|
||||
static long fastBuilderLabels(List<String> base, List<String> del, List<String> add) {
|
||||
long ops = 0;
|
||||
// Build O(1) sets — cost O(D + A)
|
||||
Set<String> delSet = new HashSet<>(del);
|
||||
for (String ignored : del) ops++;
|
||||
Set<String> addSet = new HashSet<>(add);
|
||||
for (String ignored : add) ops++;
|
||||
|
||||
List<String> result = new ArrayList<>(base.size());
|
||||
for (String label : base) {
|
||||
ops++; // single O(1) hash lookup
|
||||
if (delSet.contains(label)) continue;
|
||||
ops++;
|
||||
if (addSet.contains(label)) continue;
|
||||
result.add(label);
|
||||
}
|
||||
static long fixedDependencies(Rule r, Map<Rule, List<Rule>> inv) {
|
||||
long ops = 1; // single map lookup
|
||||
inv.get(r); // O(1)
|
||||
return ops;
|
||||
}
|
||||
|
||||
static void testBuilderLabels() {
|
||||
int L = 500; // base label count
|
||||
int D = 250; // deleted label count (worst case: half of base)
|
||||
int A = 50; // added label count
|
||||
/** Build inverse map: O(R*D). */
|
||||
static Map<Rule, List<Rule>> buildInverseMap(Map<Rule, List<Rule>> fwd) {
|
||||
Map<Rule, List<Rule>> inv = new IdentityHashMap<>();
|
||||
for (Map.Entry<Rule, List<Rule>> e : fwd.entrySet()) {
|
||||
for (Rule dep : e.getValue()) {
|
||||
inv.computeIfAbsent(dep, k -> new ArrayList<>()).add(e.getKey());
|
||||
}
|
||||
}
|
||||
return inv;
|
||||
}
|
||||
|
||||
List<String> base = new ArrayList<>(L);
|
||||
for (int i = 0; i < L; i++) base.add("label_" + i);
|
||||
/** Simulate AnalyseRules over R rules. */
|
||||
static long runDefective(List<Rule> rules, Map<Rule, List<Rule>> fwd) {
|
||||
long totalOps = 0;
|
||||
for (Rule r : rules) {
|
||||
totalOps += defectiveDependencies(r, fwd);
|
||||
}
|
||||
return totalOps;
|
||||
}
|
||||
|
||||
List<String> del = new ArrayList<>(D);
|
||||
for (int i = 0; i < D; i++) del.add("label_" + i); // delete first D
|
||||
|
||||
List<String> add = new ArrayList<>(A);
|
||||
for (int i = 0; i < A; i++) add.add("new_label_" + i);
|
||||
|
||||
long sOps = slowBuilderLabels(base, del, add);
|
||||
long fOps = fastBuilderLabels(base, del, add);
|
||||
|
||||
int Nx = 10;
|
||||
boolean pass = sOps > fOps * Nx;
|
||||
System.out.printf("prometheus-0001 [L=%d D=%d A=%d]: slow=%d fast=%d ratio=%.1fx — %s%n",
|
||||
L, D, A, sOps, fOps, (double) sOps / fOps, pass ? "PASS" : "FAIL");
|
||||
if (!pass) throw new AssertionError("prometheus-0001 FAIL: slow=" + sOps + " fast=" + fOps);
|
||||
static long runFixed(List<Rule> rules, Map<Rule, List<Rule>> fwd) {
|
||||
long totalOps = 0;
|
||||
// Build inverse map once
|
||||
Map<Rule, List<Rule>> inv = buildInverseMap(fwd);
|
||||
// O(R*D) build cost counted separately
|
||||
for (Rule r : rules) {
|
||||
totalOps += fixedDependencies(r, inv);
|
||||
}
|
||||
return totalOps;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
testBuilderLabels();
|
||||
System.out.println("1/1 PASS");
|
||||
System.out.println("prometheus-0001: dependencyMap.dependencies() O(R²) defect");
|
||||
System.out.println("===========================================================");
|
||||
|
||||
int[] ruleCounts = {10, 50, 100, 200, 500};
|
||||
int branchFactor = 3; // each rule depends on 3 next rules
|
||||
|
||||
boolean allPass = true;
|
||||
for (int R : ruleCounts) {
|
||||
List<Rule> rules = new ArrayList<>(R);
|
||||
for (int i = 0; i < R; i++) rules.add(new Rule(i));
|
||||
|
||||
Map<Rule, List<Rule>> fwd = buildForwardMap(rules, branchFactor);
|
||||
|
||||
long defOps = runDefective(rules, fwd);
|
||||
long fixOps = runFixed(rules, fwd);
|
||||
double ratio = (double) defOps / Math.max(fixOps, 1);
|
||||
|
||||
System.out.printf("R=%4d branchFactor=%d defect_ops=%,8d fixed_ops=%,6d ratio=%.1fx%n",
|
||||
R, branchFactor, defOps, fixOps, ratio);
|
||||
|
||||
if (ratio < 3.0) {
|
||||
System.err.println(" FAIL: expected ratio >= 3.0 at R=" + R);
|
||||
allPass = false;
|
||||
}
|
||||
}
|
||||
|
||||
// Correctness: fixed produces same dependencies as defective
|
||||
{
|
||||
int R = 20;
|
||||
List<Rule> rules = new ArrayList<>(R);
|
||||
for (int i = 0; i < R; i++) rules.add(new Rule(i));
|
||||
Map<Rule, List<Rule>> fwd = buildForwardMap(rules, 2);
|
||||
Map<Rule, List<Rule>> inv = buildInverseMap(fwd);
|
||||
|
||||
boolean correct = true;
|
||||
for (Rule r : rules) {
|
||||
// Collect via defective
|
||||
List<Rule> defDeps = new ArrayList<>();
|
||||
for (Map.Entry<Rule, List<Rule>> e : fwd.entrySet()) {
|
||||
if (e.getValue().contains(r)) defDeps.add(e.getKey());
|
||||
}
|
||||
// Collect via fixed
|
||||
List<Rule> fixDeps = inv.getOrDefault(r, Collections.emptyList());
|
||||
|
||||
Set<Integer> defIds = new HashSet<>();
|
||||
for (Rule d : defDeps) defIds.add(d.id);
|
||||
Set<Integer> fixIds = new HashSet<>();
|
||||
for (Rule d : fixDeps) fixIds.add(d.id);
|
||||
|
||||
if (!defIds.equals(fixIds)) {
|
||||
System.err.println(" FAIL: mismatch for " + r + " defective=" + defIds + " fixed=" + fixIds);
|
||||
correct = false;
|
||||
allPass = false;
|
||||
}
|
||||
}
|
||||
if (correct) {
|
||||
System.out.println("Correctness check: PASS (dependency sets match)");
|
||||
}
|
||||
}
|
||||
|
||||
if (allPass) {
|
||||
System.out.println("ALL TESTS PASS");
|
||||
System.exit(0);
|
||||
} else {
|
||||
System.out.println("SOME TESTS FAILED");
|
||||
System.exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue