wave16: dart-0001/2/3 + octave-0002 + php-0003/4 + cassandra-0005 + erlang-0003 + chef-0001 — 542/240

This commit is contained in:
russell@unturf.com 2026-03-27 19:36:35 -04:00
parent 31850d5ef6
commit d816d3c74c
18 changed files with 1902 additions and 6 deletions

View file

@ -1,5 +1,6 @@
package unit;
import java.util.*;
import java.nio.ByteBuffer;
/**
* Cassandra CWE-407 unit tests — standalone, no JUnit.
@ -82,6 +83,67 @@ public class CassandraTest {
return ops;
}
// -----------------------------------------------------------------------
// cassandra-0005: Lists.Discarder.execute() — List.contains() per cell
//
// CQL: DELETE items[item_val] FROM t WHERE pk = x
// → for each cell in complexData, checks toDiscard.contains(cell.buffer())
// → toDiscard is a plain ArrayList<ByteBuffer>
// → O(|existingList| * |toDiscard|) — quadratic when both sides are large.
//
// Slow: List<ByteBuffer> — O(D) per cell lookup
// Fast: HashSet<ByteBuffer> — O(1) per cell lookup
//
// src/java/org/apache/cassandra/cql3/terms/Lists.java line 524-528
// -----------------------------------------------------------------------
/**
* Build a byte-buffer list of D distinct "discard" values, then scan E existing cells
* checking each against the discard collection. Returns total comparisons performed.
*/
static long discardSlowOps(int existingCells, int discardValues) {
// Simulate toDiscard = value.getElements() — a plain ArrayList
List<ByteBuffer> toDiscard = new ArrayList<>(discardValues);
for (int i = 0; i < discardValues; i++) {
toDiscard.add(ByteBuffer.wrap(new byte[]{(byte)(i & 0xff), (byte)((i >> 8) & 0xff)}));
}
long ops = 0;
// Simulate complexData — cells with values drawn from a wider range
// (some will match toDiscard, most won't)
for (int i = 0; i < existingCells; i++) {
ByteBuffer cellBuf = ByteBuffer.wrap(
new byte[]{(byte)(i & 0xff), (byte)((i >> 8) & 0xff)});
// List.contains(): scan toDiscard until found or exhausted
boolean found = false;
for (int j = 0; j < toDiscard.size(); j++) {
ops++;
if (toDiscard.get(j).equals(cellBuf)) {
found = true;
break;
}
}
}
return ops;
}
static long discardFastOps(int existingCells, int discardValues) {
// Simulate fix: HashSet built from value.getElements() before the loop
Set<ByteBuffer> toDiscard = new HashSet<>(discardValues);
for (int i = 0; i < discardValues; i++) {
toDiscard.add(ByteBuffer.wrap(new byte[]{(byte)(i & 0xff), (byte)((i >> 8) & 0xff)}));
}
long ops = 0;
for (int i = 0; i < existingCells; i++) {
ByteBuffer cellBuf = ByteBuffer.wrap(
new byte[]{(byte)(i & 0xff), (byte)((i >> 8) & 0xff)});
ops++; // O(1) hash lookup — count as single op
toDiscard.contains(cellBuf);
}
return ops;
}
// -----------------------------------------------------------------------
// Test runner
// -----------------------------------------------------------------------
@ -104,6 +166,22 @@ public class CassandraTest {
if (pass) passed++; else failed++;
}
// cassandra-0005: Discarder.execute() — existingCells x discardValues
// Use D=100 discard values (not unreasonable for a batch DELETE) and growing E
int discardValues = 100;
int[] cellCounts = {500, 1000, 2000, 5000};
for (int e : cellCounts) {
long slow = discardSlowOps(e, discardValues);
long fast = discardFastOps(e, discardValues);
// Ratio should be ~D/2 on average (linear scan finds match halfway through)
boolean pass = slow >= fast * 5;
System.out.printf(
"cassandra-0005 E=%-5d D=%-4d slow=%8d fast=%8d ratio=%.1fx %s%n",
e, discardValues, slow, fast,
(double) slow / fast, pass ? "PASS" : "FAIL");
if (pass) passed++; else failed++;
}
System.out.printf("%nTotal: %d/%d PASS%n", passed, passed + failed);
if (failed > 0) System.exit(1);
}