Skip to content

Commit

Permalink
Tests: refactor multiple test runner, simpler syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
barakugav committed Aug 2, 2023
1 parent dd39cfc commit be68daa
Show file tree
Hide file tree
Showing 46 changed files with 799 additions and 579 deletions.
80 changes: 0 additions & 80 deletions jgalgo-bench/src/main/java/com/jgalgo/bench/util/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,92 +16,12 @@

package com.jgalgo.bench.util;

import java.util.Collection;
import java.util.Iterator;
import java.util.Random;

import it.unimi.dsi.fastutil.ints.IntArrays;

@SuppressWarnings("boxing")
public class TestUtils {

public static class Phase {
private final int repeat;
private final int[] args;

private Phase(int repeat, int[] args) {
if (repeat < 0)
throw new IllegalArgumentException();
this.repeat = repeat;
this.args = args;
}

static Phase of(int repeat, int... args) {
return new Phase(repeat, args);
}
}

public static Phase phase(int repeat, int... args) {
return Phase.of(repeat, args);
}

@FunctionalInterface
public static interface TestRunnable {
public void run(TestIterIdx testIter, int[] args);
}

public static void runTestMultiple(Collection<Phase> phases, TestRunnable test) {
int phaseIdx = 0;
for (Phase phase : phases) {
for (int iter = 0; iter < phase.repeat; iter++) {
try {
test.run(new TestIterIdx(phaseIdx, iter), phase.args);
} catch (Throwable e) {
System.err.println("Failed at phase " + phaseIdx + " iter " + iter);
throw e;
}
}
phaseIdx++;
}
}

public static class TestIterIdx {
public final int phase, iter;

private TestIterIdx(int phase, int iter) {
this.phase = phase;
this.iter = iter;
}

@Override
public String toString() {
return "P" + phase + " I" + iter;
}
}

static boolean doubleEql(double a, double b, double precise) {
if (a < b)
return b - a < precise;
if (a > b)
return a - b < precise;
return true;
}

static void printArr(int a[]) {
printArr(a, true);
}

static void printArr(int a[], boolean printIndicies) {
for (int i = 0; i < a.length; i++)
System.out.print("" + String.format("%03d", a[i]) + ", ");
System.out.println();
if (printIndicies) {
for (int i = 0; i < a.length; i++)
System.out.print("" + String.format("%03d", i) + ", ");
System.out.println();
}
}

public static int[] randArray(int n, long seed) {
return randArray(n, 0, Integer.MAX_VALUE, seed);
}
Expand Down
9 changes: 7 additions & 2 deletions jgalgo-core/src/test/java/com/jgalgo/AStarTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package com.jgalgo;

import java.util.List;
import java.util.Random;
import java.util.function.Function;
import java.util.function.IntToDoubleFunction;
Expand All @@ -31,7 +30,13 @@

public class AStarTest extends TestBase {

private static List<Phase> SsspPhases = List.of(phase(64, 16, 32), phase(32, 64, 256), phase(4, 300, 900));
private static PhasedTester SsspPhases;
static {
SsspPhases = new PhasedTester();
SsspPhases.addPhase().withArgs(16, 32).repeat(64);
SsspPhases.addPhase().withArgs(64, 256).repeat(32);
SsspPhases.addPhase().withArgs(300, 900).repeat(4);
}

@Test
public void testRandGraphDirectedNoHeuristic() {
Expand Down
123 changes: 80 additions & 43 deletions jgalgo-core/src/test/java/com/jgalgo/ArraysUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,8 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.Arrays;
import java.util.List;
import java.util.Random;

import org.junit.jupiter.api.Test;
import com.jgalgo.internal.util.TestBase;

Expand All @@ -33,10 +30,14 @@ public class ArraysUtilsTest extends TestBase {
public void testIntGetKthElementRandArrayUnique() {
final long seed = 0xedf92ed1b59ae1e1L;
final SeedGenerator seedGen = new SeedGenerator(seed);
List<Phase> phases =
List.of(phase(256, 8), phase(128, 32), phase(32, 128), phase(16, 256), phase(8, 1024), phase(2, 4567));
runTestMultiple(phases, (testIter, args) -> {
int n = args[0];
PhasedTester tester = new PhasedTester();
tester.addPhase().withArgs(8).repeat(256);
tester.addPhase().withArgs(32).repeat(128);
tester.addPhase().withArgs(128).repeat(32);
tester.addPhase().withArgs(256).repeat(16);
tester.addPhase().withArgs(1024).repeat(8);
tester.addPhase().withArgs(4567).repeat(2);
tester.run(n -> {
int[] a = randPermutation(n, seedGen.nextSeed());
testGetKthElement(a, seedGen.nextSeed());
});
Expand All @@ -46,10 +47,14 @@ public void testIntGetKthElementRandArrayUnique() {
public void testObjGetKthElementRandArrayUnique() {
final long seed = 0x7f7871365f84b52eL;
final SeedGenerator seedGen = new SeedGenerator(seed);
List<Phase> phases =
List.of(phase(256, 8), phase(128, 32), phase(32, 128), phase(16, 256), phase(8, 1024), phase(2, 4567));
runTestMultiple(phases, (testIter, args) -> {
int n = args[0];
PhasedTester tester = new PhasedTester();
tester.addPhase().withArgs(8).repeat(256);
tester.addPhase().withArgs(32).repeat(128);
tester.addPhase().withArgs(128).repeat(32);
tester.addPhase().withArgs(256).repeat(16);
tester.addPhase().withArgs(1024).repeat(8);
tester.addPhase().withArgs(4567).repeat(2);
tester.run(n -> {
Integer[] a = toIntegerArr(randPermutation(n, seedGen.nextSeed()));
testGetKthElement(a, seedGen.nextSeed());
});
Expand All @@ -59,10 +64,14 @@ public void testObjGetKthElementRandArrayUnique() {
public void testIntGetKthElementRandArrayNonUnique() {
final long seed = 0x97e45458f8daefd2L;
final SeedGenerator seedGen = new SeedGenerator(seed);
List<Phase> phases =
List.of(phase(256, 8), phase(128, 32), phase(32, 128), phase(16, 256), phase(8, 1024), phase(2, 4567));
runTestMultiple(phases, (testIter, args) -> {
int n = args[0];
PhasedTester tester = new PhasedTester();
tester.addPhase().withArgs(8).repeat(256);
tester.addPhase().withArgs(32).repeat(128);
tester.addPhase().withArgs(128).repeat(32);
tester.addPhase().withArgs(256).repeat(16);
tester.addPhase().withArgs(1024).repeat(8);
tester.addPhase().withArgs(4567).repeat(2);
tester.run(n -> {
int[] a = randArray(n, 0, n / 4, seedGen.nextSeed());
testGetKthElement(a, seedGen.nextSeed());
});
Expand All @@ -72,10 +81,14 @@ public void testIntGetKthElementRandArrayNonUnique() {
public void testObjGetKthElementRandArrayNonUnique() {
final long seed = 0x6ee2228e9064ab3eL;
final SeedGenerator seedGen = new SeedGenerator(seed);
List<Phase> phases =
List.of(phase(256, 8), phase(128, 32), phase(32, 128), phase(16, 256), phase(8, 1024), phase(2, 4567));
runTestMultiple(phases, (testIter, args) -> {
int n = args[0];
PhasedTester tester = new PhasedTester();
tester.addPhase().withArgs(8).repeat(256);
tester.addPhase().withArgs(32).repeat(128);
tester.addPhase().withArgs(128).repeat(32);
tester.addPhase().withArgs(256).repeat(16);
tester.addPhase().withArgs(1024).repeat(8);
tester.addPhase().withArgs(4567).repeat(2);
tester.run(n -> {
Integer[] a = toIntegerArr(randArray(n, 0, n / 4, seedGen.nextSeed()));
testGetKthElement(a, seedGen.nextSeed());
});
Expand All @@ -85,10 +98,14 @@ public void testObjGetKthElementRandArrayNonUnique() {
public void testIntGetKthElementRandArraySameElm() {
final long seed = 0x77b8bdd802380333L;
final SeedGenerator seedGen = new SeedGenerator(seed);
List<Phase> phases =
List.of(phase(1, 8), phase(1, 32), phase(1, 128), phase(1, 256), phase(1, 1024), phase(1, 3849));
runTestMultiple(phases, (testIter, args) -> {
int n = args[0];
PhasedTester tester = new PhasedTester();
tester.addPhase().withArgs(8).repeat(1);
tester.addPhase().withArgs(32).repeat(1);
tester.addPhase().withArgs(128).repeat(1);
tester.addPhase().withArgs(256).repeat(1);
tester.addPhase().withArgs(1024).repeat(1);
tester.addPhase().withArgs(3849).repeat(1);
tester.run(n -> {
int[] a = new int[n];
Arrays.fill(a, 6);
testGetKthElement(a, seedGen.nextSeed());
Expand All @@ -99,10 +116,14 @@ public void testIntGetKthElementRandArraySameElm() {
public void testObjGetKthElementRandArraySameElm() {
final long seed = 0x656f2a7fcad2e9e8L;
final SeedGenerator seedGen = new SeedGenerator(seed);
List<Phase> phases =
List.of(phase(1, 8), phase(1, 32), phase(1, 128), phase(1, 256), phase(1, 1024), phase(1, 3849));
runTestMultiple(phases, (testIter, args) -> {
int n = args[0];
PhasedTester tester = new PhasedTester();
tester.addPhase().withArgs(8).repeat(1);
tester.addPhase().withArgs(32).repeat(1);
tester.addPhase().withArgs(128).repeat(1);
tester.addPhase().withArgs(256).repeat(1);
tester.addPhase().withArgs(1024).repeat(1);
tester.addPhase().withArgs(3849).repeat(1);
tester.run(n -> {
int[] a = new int[n];
Arrays.fill(a, 6);
testGetKthElement(toIntegerArr(a), seedGen.nextSeed());
Expand All @@ -129,10 +150,14 @@ private static void testGetKthElement(Integer[] a, long seed) {
public void testIntBucketPartition() {
final SeedGenerator seedGen = new SeedGenerator(0x90fc97e52265ff44L);
Random rand = new Random(seedGen.nextSeed());
List<Phase> phases =
List.of(phase(256, 8), phase(128, 32), phase(32, 128), phase(16, 256), phase(8, 1024), phase(2, 4567));
runTestMultiple(phases, (testIter, args) -> {
int n = args[0];
PhasedTester tester = new PhasedTester();
tester.addPhase().withArgs(8).repeat(256);
tester.addPhase().withArgs(32).repeat(128);
tester.addPhase().withArgs(128).repeat(32);
tester.addPhase().withArgs(256).repeat(16);
tester.addPhase().withArgs(1024).repeat(8);
tester.addPhase().withArgs(4567).repeat(2);
tester.run(n -> {
int[] a = randArray(n, 0, n / 4, seedGen.nextSeed());
int bucketSize = rand.nextInt(n / 2) + 1;
ArraysUtils.bucketPartition(a, 0, n, null, bucketSize);
Expand All @@ -154,10 +179,14 @@ public void testIntBucketPartition() {
public void testObjBucketPartition() {
final SeedGenerator seedGen = new SeedGenerator(0x275079aa6f2fc7d7L);
Random rand = new Random(seedGen.nextSeed());
List<Phase> phases =
List.of(phase(256, 8), phase(128, 32), phase(32, 128), phase(16, 256), phase(8, 1024), phase(2, 4567));
runTestMultiple(phases, (testIter, args) -> {
int n = args[0];
PhasedTester tester = new PhasedTester();
tester.addPhase().withArgs(8).repeat(256);
tester.addPhase().withArgs(32).repeat(128);
tester.addPhase().withArgs(128).repeat(32);
tester.addPhase().withArgs(256).repeat(16);
tester.addPhase().withArgs(1024).repeat(8);
tester.addPhase().withArgs(4567).repeat(2);
tester.run(n -> {
Integer[] a = toIntegerArr(randArray(n, 0, n / 4, seedGen.nextSeed()));
int bucketSize = rand.nextInt(n / 2) + 1;
ArraysUtils.bucketPartition(a, 0, n, null, bucketSize);
Expand All @@ -179,10 +208,14 @@ public void testObjBucketPartition() {
public void testIntPivotPartition() {
final SeedGenerator seedGen = new SeedGenerator(0x92f173634ff49309L);
Random rand = new Random(seedGen.nextSeed());
List<Phase> phases =
List.of(phase(256, 8), phase(128, 32), phase(32, 128), phase(16, 256), phase(8, 1024), phase(2, 4567));
runTestMultiple(phases, (testIter, args) -> {
int n = args[0];
PhasedTester tester = new PhasedTester();
tester.addPhase().withArgs(8).repeat(256);
tester.addPhase().withArgs(32).repeat(128);
tester.addPhase().withArgs(128).repeat(32);
tester.addPhase().withArgs(256).repeat(16);
tester.addPhase().withArgs(1024).repeat(8);
tester.addPhase().withArgs(4567).repeat(2);
tester.run(n -> {
int[] a = randArray(n, 0, n / 4, seedGen.nextSeed());
int pivot = a[rand.nextInt(a.length)];
ArraysUtils.pivotPartition(a, 0, a.length, pivot, null);
Expand All @@ -205,10 +238,14 @@ public void testIntPivotPartition() {
public void testObjPivotPartition() {
final SeedGenerator seedGen = new SeedGenerator(0xd693bcfe6327104L);
Random rand = new Random(seedGen.nextSeed());
List<Phase> phases =
List.of(phase(256, 8), phase(128, 32), phase(32, 128), phase(16, 256), phase(8, 1024), phase(2, 4567));
runTestMultiple(phases, (testIter, args) -> {
int n = args[0];
PhasedTester tester = new PhasedTester();
tester.addPhase().withArgs(8).repeat(256);
tester.addPhase().withArgs(32).repeat(128);
tester.addPhase().withArgs(128).repeat(32);
tester.addPhase().withArgs(256).repeat(16);
tester.addPhase().withArgs(1024).repeat(8);
tester.addPhase().withArgs(4567).repeat(2);
tester.run(n -> {
Integer[] a = toIntegerArr(randArray(n, 0, n / 4, seedGen.nextSeed()));
int pivot = a[rand.nextInt(a.length)];
ArraysUtils.pivotPartition(a, 0, a.length, pivot, null);
Expand Down
9 changes: 5 additions & 4 deletions jgalgo-core/src/test/java/com/jgalgo/BFSIterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.List;
import java.util.Random;
import org.junit.jupiter.api.Test;
import com.jgalgo.graph.Graph;
Expand All @@ -35,9 +34,11 @@ public void testBfsConnected() {
final SeedGenerator seedGen = new SeedGenerator(seed);
Random rand = new Random(seedGen.nextSeed());

List<Phase> phases = List.of(phase(256, 16, 8), phase(128, 32, 64), phase(4, 2048, 8192));
runTestMultiple(phases, (testIter, args) -> {
int n = args[0], m = args[1];
PhasedTester tester = new PhasedTester();
tester.addPhase().withArgs(16, 8).repeat(256);
tester.addPhase().withArgs(32, 64).repeat(128);
tester.addPhase().withArgs(2048, 8192).repeat(4);
tester.run((n, m) -> {
Graph g = new RandomGraphBuilder(seedGen.nextSeed()).n(n).m(m).directed(false).parallelEdges(true)
.selfEdges(true).cycles(true).connected(true).build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.List;
import org.junit.jupiter.api.Test;
import com.jgalgo.graph.Graph;
import com.jgalgo.graph.Weights;
Expand All @@ -37,9 +36,12 @@ public class BiConnectedComponentsAlgoHopcroftTarjanTest extends TestBase {
public void randGraphUndirected() {
final long seed = 0xda9272921794ecfaL;
final SeedGenerator seedGen = new SeedGenerator(seed);
List<Phase> phases = List.of(phase(128, 5, 6), phase(64, 16, 32), phase(32, 64, 256), phase(1, 165, 666));
runTestMultiple(phases, (testIter, args) -> {
int n = args[0], m = args[1];
PhasedTester tester = new PhasedTester();
tester.addPhase().withArgs(5, 6).repeat(128);
tester.addPhase().withArgs(16, 32).repeat(64);
tester.addPhase().withArgs(64, 256).repeat(32);
tester.addPhase().withArgs(165, 666).repeat(1);
tester.run((n, m) -> {
Graph g = new RandomGraphBuilder(seedGen.nextSeed()).n(n).m(m).directed(false).parallelEdges(true)
.selfEdges(true).cycles(true).connected(false).build();
testUGraph(BiConnectedComponentsAlgo.newBuilder().build(), g);
Expand Down
10 changes: 6 additions & 4 deletions jgalgo-core/src/test/java/com/jgalgo/ColoringTestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.util.List;
import com.jgalgo.graph.Graph;
import com.jgalgo.graph.GraphFactory;
import com.jgalgo.internal.util.RandomGraphBuilder;
Expand All @@ -33,9 +32,12 @@ class ColoringTestUtils extends TestUtils {

static void testRandGraphs(Coloring algo, long seed) {
final SeedGenerator seedGen = new SeedGenerator(seed);
List<Phase> phases = List.of(phase(256, 16, 8), phase(128, 32, 64), phase(32, 200, 1000), phase(4, 2048, 8192));
runTestMultiple(phases, (testIter, args) -> {
int n = args[0], m = args[1];
PhasedTester tester = new PhasedTester();
tester.addPhase().withArgs(16, 8).repeat(256);
tester.addPhase().withArgs(32, 64).repeat(128);
tester.addPhase().withArgs(200, 1000).repeat(32);
tester.addPhase().withArgs(2048, 8192).repeat(4);
tester.run((n, m) -> {
Graph g = new RandomGraphBuilder(seedGen.nextSeed()).n(n).m(m).directed(false).parallelEdges(true)
.selfEdges(false).cycles(true).connected(false).build();
Coloring.Result coloring = algo.computeColoring(g);
Expand Down
Loading

0 comments on commit be68daa

Please sign in to comment.