From 2d9a49b4f6f6d236d40be66961d98750b5339f25 Mon Sep 17 00:00:00 2001 From: Seva Safris Date: Tue, 9 Jun 2020 18:21:35 +0700 Subject: [PATCH] Improve tests for MovingAverage, #15 --- .../java/org/libj/math/MovingAverageTest.java | 49 +++++++++++++------ 1 file changed, 33 insertions(+), 16 deletions(-) diff --git a/src/test/java/org/libj/math/MovingAverageTest.java b/src/test/java/org/libj/math/MovingAverageTest.java index 5f09de5c..bb237792 100644 --- a/src/test/java/org/libj/math/MovingAverageTest.java +++ b/src/test/java/org/libj/math/MovingAverageTest.java @@ -21,23 +21,40 @@ import org.junit.Test; public class MovingAverageTest { - @Test - public void testMovingAverage() { - final double[] vals = {1, 2, 4, 1, 2, 3, 7}; - final double[] averages = { - 1.0, - 1.5, - 2.3333333333333335, - 2.0, - 2.0, - 2.1666666666666665, - 2.857142857142857 - }; - - final MovingAverage movingAverage = new MovingAverage(); + private static final double epsilon = 0.0001; + private static final double[] vals = {1, 2, 4, 1, 2, 3, 7}; + private static final double[] averages = { + 1.0, + 1.5, + 2.3333333333333335, + 2.0, + 2.0, + 2.1666666666666665, + 2.857142857142857 + }; + + private static void add(final MovingAverage a, final int i) { + a.add(vals[i]); + assertEquals(averages[i], a.doubleValue(), epsilon); + } + + private static void test(final MovingAverage a, final MovingAverage b) { for (int i = 0; i < vals.length; ++i) { - movingAverage.add(vals[i]); - assertEquals(averages[i], movingAverage.doubleValue(), 0.0001d); + add(a, i); + add(b, i); + assertEquals(a.byteValue(), b.byteValue()); + assertEquals(a.shortValue(), b.shortValue()); + assertEquals(a.intValue(), b.intValue()); + assertEquals(a.longValue(), b.longValue()); + assertEquals(a.floatValue(), b.floatValue(), epsilon); + assertEquals(a.toString(), b.toString()); } + + assertEquals(vals.length, a.getCount()); + } + + @Test + public void testMovingAverage() { + test(new MovingAverage(), new MovingAverage(0)); } } \ No newline at end of file