java-topology/defects/libvirt/unit/Libvirt0001Test.java

148 lines
6.6 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package defects.libvirt.unit;
import java.util.*;
/**
* Unit tests for libvirt-0001: virCPUx86UpdateLive addedFeatures O(F×A).
*/
public class Libvirt0001Test {
static int passed = 0;
static int failed = 0;
static void assertTrue(String label, boolean condition) {
if (condition) {
System.out.println(" PASS: " + label);
passed++;
} else {
System.out.println(" FAIL: " + label);
failed++;
}
}
public static void main(String[] args) {
System.out.println("libvirt-0001 unit tests");
System.out.println("=".repeat(50));
testDefectiveCountsCorrectly();
testFixedCountsCorrectly();
testBothFindSameIgnoredSet();
testNullAddedFeatures();
testExplicitFeaturesNotIgnored();
testOpRatioAbove30xAtF500();
System.out.println();
System.out.printf("Result: %d passed, %d failed%n", passed, failed);
if (failed > 0) System.exit(1);
}
static void testDefectiveCountsCorrectly() {
System.out.println("\n--- testDefectiveCountsCorrectly ---");
int f = 10, a = 3;
List<String> features = Libvirt0001CpuUpdateLiveAlgorithm.buildFeatureMap(f);
String[] added = Libvirt0001CpuUpdateLiveAlgorithm.buildAddedArray(features, a);
Libvirt0001CpuUpdateLiveAlgorithm.DefectiveUpdateLive d =
new Libvirt0001CpuUpdateLiveAlgorithm.DefectiveUpdateLive();
d.computeIgnored(features, added, new HashSet<>());
// For each of F features, scans until match or exhausts A.
// First 3 features (added) are found at pos 0,1,2 → 1+2+3 = 6 comparisons
// Next 7 features are not in added → 3 comparisons each → 21
// Total: 27
assertTrue("Defective comparisons = 27 at F=10, A=3: " + d.comparisons,
d.comparisons == 27);
}
static void testFixedCountsCorrectly() {
System.out.println("\n--- testFixedCountsCorrectly ---");
int f = 10, a = 3;
List<String> features = Libvirt0001CpuUpdateLiveAlgorithm.buildFeatureMap(f);
Set<String> addedSet = Libvirt0001CpuUpdateLiveAlgorithm.buildAddedSet(features, a);
Libvirt0001CpuUpdateLiveAlgorithm.FixedUpdateLive fix =
new Libvirt0001CpuUpdateLiveAlgorithm.FixedUpdateLive();
fix.computeIgnored(features, addedSet, new HashSet<>());
// Fixed: exactly F comparisons (one per feature)
assertTrue("Fixed comparisons = F = " + f + ": " + fix.comparisons,
fix.comparisons == f);
}
static void testBothFindSameIgnoredSet() {
System.out.println("\n--- testBothFindSameIgnoredSet ---");
int f = 50, a = 15;
List<String> features = Libvirt0001CpuUpdateLiveAlgorithm.buildFeatureMap(f);
String[] addedArr = Libvirt0001CpuUpdateLiveAlgorithm.buildAddedArray(features, a);
Set<String> addedSet = Libvirt0001CpuUpdateLiveAlgorithm.buildAddedSet(features, a);
Set<String> modelFeatures = new HashSet<>(List.of("feat-0", "feat-5"));
Libvirt0001CpuUpdateLiveAlgorithm.DefectiveUpdateLive def =
new Libvirt0001CpuUpdateLiveAlgorithm.DefectiveUpdateLive();
Set<String> defIgnored = def.computeIgnored(features, addedArr, modelFeatures);
Libvirt0001CpuUpdateLiveAlgorithm.FixedUpdateLive fix =
new Libvirt0001CpuUpdateLiveAlgorithm.FixedUpdateLive();
Set<String> fixIgnored = fix.computeIgnored(features, addedSet, modelFeatures);
assertTrue("Both produce same ignored set", defIgnored.equals(fixIgnored));
// Explicit features (feat-0, feat-5) should NOT be in ignored
assertTrue("Explicit feat-0 not ignored", !defIgnored.contains("feat-0"));
assertTrue("Explicit feat-5 not ignored", !fixIgnored.contains("feat-5"));
// Non-explicit added features should be ignored
assertTrue("feat-1 (added, non-explicit) is ignored", defIgnored.contains("feat-1"));
}
static void testNullAddedFeatures() {
System.out.println("\n--- testNullAddedFeatures ---");
List<String> features = Libvirt0001CpuUpdateLiveAlgorithm.buildFeatureMap(5);
Libvirt0001CpuUpdateLiveAlgorithm.DefectiveUpdateLive def =
new Libvirt0001CpuUpdateLiveAlgorithm.DefectiveUpdateLive();
Set<String> defIgnored = def.computeIgnored(features, null, new HashSet<>());
assertTrue("Null added → no ignored (defective): " + defIgnored.size(),
defIgnored.isEmpty());
Libvirt0001CpuUpdateLiveAlgorithm.FixedUpdateLive fix =
new Libvirt0001CpuUpdateLiveAlgorithm.FixedUpdateLive();
Set<String> fixIgnored = fix.computeIgnored(features, null, new HashSet<>());
assertTrue("Null added → no ignored (fixed): " + fixIgnored.size(),
fixIgnored.isEmpty());
}
static void testExplicitFeaturesNotIgnored() {
System.out.println("\n--- testExplicitFeaturesNotIgnored ---");
List<String> features = List.of("a", "b", "c", "d");
String[] added = {"a", "b"};
Set<String> addedSet = new HashSet<>(List.of("a", "b"));
Set<String> model = new HashSet<>(List.of("a")); // a is explicit
Libvirt0001CpuUpdateLiveAlgorithm.DefectiveUpdateLive def =
new Libvirt0001CpuUpdateLiveAlgorithm.DefectiveUpdateLive();
Set<String> defIgnored = def.computeIgnored(features, added, model);
assertTrue("Only 'b' ignored (a is explicit): ignored=" + defIgnored,
defIgnored.equals(Set.of("b")));
}
static void testOpRatioAbove30xAtF500() {
System.out.println("\n--- testOpRatioAbove30x at F=500, A=50 ---");
int f = 500, a = 50;
List<String> features = Libvirt0001CpuUpdateLiveAlgorithm.buildFeatureMap(f);
String[] addedArr = Libvirt0001CpuUpdateLiveAlgorithm.buildAddedArray(features, a);
Set<String> addedSet = Libvirt0001CpuUpdateLiveAlgorithm.buildAddedSet(features, a);
Libvirt0001CpuUpdateLiveAlgorithm.DefectiveUpdateLive def =
new Libvirt0001CpuUpdateLiveAlgorithm.DefectiveUpdateLive();
def.computeIgnored(features, addedArr, new HashSet<>());
Libvirt0001CpuUpdateLiveAlgorithm.FixedUpdateLive fix =
new Libvirt0001CpuUpdateLiveAlgorithm.FixedUpdateLive();
fix.computeIgnored(features, addedSet, new HashSet<>());
double ratio = (double) def.comparisons / Math.max(fix.comparisons, 1);
System.out.printf(" defective=%d, fixed=%d, ratio=%.1f×%n",
def.comparisons, fix.comparisons, ratio);
assertTrue("Op-count ratio >= 30× at F=500, A=50: " + ratio, ratio >= 30.0);
}
}