From 911c8c9f292374b1668ce5e30a539f7e62f75369 Mon Sep 17 00:00:00 2001 From: Phil Burk Date: Sun, 29 Aug 2021 09:23:58 -0700 Subject: tests: fix unit tests Add missing parameters. Create PseudoRandom. Do not run test that generates the WAV files. Fix hang in TestConnections. Fixes #97 --- src/test/java/com/jsyn/engine/TestDevices.java | 7 ++++--- src/test/java/com/jsyn/engine/TestFifo.java | 6 ++++-- src/test/java/com/jsyn/research/RecordVariousRamps.java | 10 +++++++--- src/test/java/com/jsyn/unitgen/TestConnections.java | 2 ++ src/test/java/com/jsyn/unitgen/TestMath.java | 13 +++++++++---- src/test/java/com/jsyn/util/TestPseudoRandom.java | 7 +++++-- 6 files changed, 31 insertions(+), 14 deletions(-) diff --git a/src/test/java/com/jsyn/engine/TestDevices.java b/src/test/java/com/jsyn/engine/TestDevices.java index 307880e..52ee18f 100644 --- a/src/test/java/com/jsyn/engine/TestDevices.java +++ b/src/test/java/com/jsyn/engine/TestDevices.java @@ -58,15 +58,16 @@ public class TestDevices { LOGGER.debug("Audio passthrough started."); // Sleep a while. double sleepTime = 2.0; + double startTime = synth.getCurrentTime(); try { - double time = synth.getCurrentTime(); // Sleep for a few seconds. - synth.sleepUntil(time + sleepTime); + synth.sleepUntil(startTime + sleepTime); } catch (InterruptedException e) { e.printStackTrace(); } double synthTime = synth.getCurrentTime(); - assertEquals(synthTime, 0.2, "Time has advanced. " + synthTime); + double actualSleepTime = synthTime - startTime; + assertEquals(sleepTime, actualSleepTime, 0.1, "Time has advanced. " + actualSleepTime); // Stop everything. synth.stop(); LOGGER.debug("All done."); diff --git a/src/test/java/com/jsyn/engine/TestFifo.java b/src/test/java/com/jsyn/engine/TestFifo.java index d057e19..1c681fb 100644 --- a/src/test/java/com/jsyn/engine/TestFifo.java +++ b/src/test/java/com/jsyn/engine/TestFifo.java @@ -216,7 +216,7 @@ public class TestFifo { @Test public void testBlockReadAndWriteWaitStress() { - final int chunk = 10000000; // 10 Megabytes + final int chunk = 3000000; final AudioFifo fifo = new AudioFifo(); fifo.allocate(8); @@ -236,7 +236,9 @@ public class TestFifo { } }).start(); - Thread watchdog = startWatchdog(10000); + // TODO Watchdog is apparently not working. + // I set the watchdog to be very short and it did not trigger. + Thread watchdog = startWatchdog(10 * 1000); for (int i = 0; i < chunk; i++) { assertEquals(value + i, fifo.read(), "reading back data"); } diff --git a/src/test/java/com/jsyn/research/RecordVariousRamps.java b/src/test/java/com/jsyn/research/RecordVariousRamps.java index 7abb2b1..b2c828e 100644 --- a/src/test/java/com/jsyn/research/RecordVariousRamps.java +++ b/src/test/java/com/jsyn/research/RecordVariousRamps.java @@ -40,6 +40,10 @@ import org.junit.jupiter.api.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +/** + * This is a work in progress. + * It generates WAV files that should probably be deleted when done. + */ public class RecordVariousRamps { private static final Logger LOGGER = LoggerFactory.getLogger(RecordVariousRamps.class); @@ -176,17 +180,17 @@ public class RecordVariousRamps { synth.stop(); } - @Test + // @Test private void stepMode() throws IOException { test(MODE_STEP); } - @Test + // @Test public void linearMode() throws IOException { test(MODE_LINEAR); } - @Test + // @Test public void smoothMode() throws IOException { test(MODE_SMOOTH); } diff --git a/src/test/java/com/jsyn/unitgen/TestConnections.java b/src/test/java/com/jsyn/unitgen/TestConnections.java index 9aee32f..9ce7726 100644 --- a/src/test/java/com/jsyn/unitgen/TestConnections.java +++ b/src/test/java/com/jsyn/unitgen/TestConnections.java @@ -33,6 +33,8 @@ public class TestConnections { @BeforeEach private void beforeEach() { synth = JSyn.createSynthesizer(); + synth.setRealTime(false); + synth.start(); synth.add(add1 = new Add()); synth.add(add2 = new Add()); diff --git a/src/test/java/com/jsyn/unitgen/TestMath.java b/src/test/java/com/jsyn/unitgen/TestMath.java index 7c33223..59d2dfe 100644 --- a/src/test/java/com/jsyn/unitgen/TestMath.java +++ b/src/test/java/com/jsyn/unitgen/TestMath.java @@ -402,9 +402,13 @@ public class TestMath { ugen.setSynthesisEngine(synthesisEngine); final double smallValue = -1.5308084989341915E-17; double[] values = { - 49.0, 49.5, 50.0 + smallValue, - 60.0 -smallValue, - 79.2, 12.9, 118.973 + 49.0, + 49.5, + 50.0 + smallValue, + 60.0 - smallValue, + 79.2, + 12.9, + 118.973 }; // Sanity check AudioMath assertEquals(440.0, AudioMath.pitchToFrequency(69), 0.001, "PitchToFrequency"); @@ -413,7 +417,8 @@ public class TestMath { for (double pitch : values) { ugen.input.setValueInternal(pitch); ugen.generate(); - assertEquals(ugen.output.getValue(), 0.001, "PitchToFrequency: " + AudioMath.pitchToFrequency(pitch)); + double expected = AudioMath.pitchToFrequency(pitch); + assertEquals(expected, ugen.output.getValue(), 0.001, "PitchToFrequency: " + expected); } } diff --git a/src/test/java/com/jsyn/util/TestPseudoRandom.java b/src/test/java/com/jsyn/util/TestPseudoRandom.java index b37475f..b467f3b 100644 --- a/src/test/java/com/jsyn/util/TestPseudoRandom.java +++ b/src/test/java/com/jsyn/util/TestPseudoRandom.java @@ -22,7 +22,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; public class TestPseudoRandom { - PseudoRandom pseudoRandom; + PseudoRandom pseudoRandom = new PseudoRandom(12345); private int[] bins; private final static int BIN_SHIFTER = 8; private final static int BIN_COUNT = 1 << BIN_SHIFTER; @@ -41,6 +41,7 @@ public class TestPseudoRandom { @Test public void testIntegerDistribution() { int scaler = 100; + bins = new int[BIN_COUNT]; for (int i = 0; i < (bins.length * scaler); i++) { int rand = pseudoRandom.nextRandomInteger(); int positiveInt = rand & 0x7FFFFFFF; @@ -54,6 +55,7 @@ public class TestPseudoRandom { @Test public void test01Distribution() { int scaler = 100; + bins = new int[BIN_COUNT]; for (int i = 0; i < (bins.length * scaler); i++) { double rand = pseudoRandom.random(); assertTrue((rand >= 0.0), "not too low, #" + i + " = " + rand); @@ -65,7 +67,8 @@ public class TestPseudoRandom { } private void checkDistribution(int scaler) { - // Generate running average that should stay near scaler + // Generate running average that should stay near scaler. + // TODO This could randomly fail. double average = scaler; double coefficient = 0.9; for (int i = 0; i < (bins.length); i++) { -- cgit v1.2.3