You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "kinow (via GitHub)" <gi...@apache.org> on 2023/07/16 19:00:25 UTC

[GitHub] [commons-statistics] kinow commented on a diff in pull request #49: [STATISTICS-76]: Max Implementation

kinow commented on code in PR #49:
URL: https://github.com/apache/commons-statistics/pull/49#discussion_r1264731414


##########
commons-statistics-descriptive/src/test/java/org/apache/commons/statistics/descriptive/MaxTest.java:
##########
@@ -0,0 +1,187 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.statistics.descriptive;
+
+import java.util.Arrays;
+import java.util.function.DoubleSupplier;
+import java.util.stream.Stream;
+import org.apache.commons.rng.UniformRandomProvider;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+
+/**
+ * Test for {@link Max}.
+ */
+final class MaxTest {
+
+    @Test
+    void testEmpty() {
+        Max max = Max.create();
+        Assertions.assertEquals(Double.NEGATIVE_INFINITY, max.getAsDouble());
+    }
+
+    @Test
+    void testIncrement() {
+        // Test the max after each incremental update
+        // First parameter of testArray is the value that would be added
+        // Second parameter of testArray is the max we expect after adding the value
+        double[][] testArray = {
+            {1729.22, 1729.22},
+            {2520.35, 2520.35},
+            {2010.87, 2520.35},
+            {100000000.1, 100000000.1},
+            {+0.0, 100000000.1},
+            {-0.0, 100000000.1},
+            {Double.MAX_VALUE, Double.MAX_VALUE},
+            {Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY},
+            {Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY}
+        };
+
+        Max stat = Max.create();
+        for (final double[] valueAndExpected: testArray) {
+            final double value = valueAndExpected[0];
+            final double expected = valueAndExpected[1];
+            stat.accept(value);
+            Assertions.assertEquals(expected, stat.getAsDouble());
+        }
+    }
+
+    @Test
+    void testNaN() {
+        // Test non-nan values cannot revert a NaN
+        double[] testArray = {Double.NaN, +0.0d, -0.0d, Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY};
+        Max stat = Max.create();
+        for (final double x : testArray) {
+            stat.accept(x);
+            Assertions.assertEquals(Double.NaN, stat.getAsDouble());
+        }
+    }
+
+    @ParameterizedTest
+    @MethodSource
+    void testMax(double[] values, double expected) {
+        Max stat = Max.create();
+        Arrays.stream(values).forEach(stat);
+        double actual = stat.getAsDouble();
+        Assertions.assertEquals(expected, actual, "max");
+        Assertions.assertEquals(expected, Max.of(values).getAsDouble(), "max");
+    }
+
+    static Stream<Arguments> testMax() {
+        return Stream.of(
+            Arguments.of(new double[] {}, Double.NEGATIVE_INFINITY),
+            Arguments.of(new double[] {3.14}, 3.14),
+            Arguments.of(new double[] {12.34, 56.78, -2.0}, 56.78),
+            Arguments.of(new double[] {Double.NaN, 3.14, Double.NaN, Double.NaN}, Double.NaN),
+            Arguments.of(new double[] {-1d, 1d, Double.NaN}, Double.NaN),
+            Arguments.of(new double[] {Double.NaN, Double.NaN, Double.NaN}, Double.NaN),
+            Arguments.of(new double[] {0.0d, Double.NaN, +0.0d, -0.0d, Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY}, Double.NaN),
+            Arguments.of(new double[] {+0.0d, -0.0d, 1.0, 3.14}, 3.14),
+            Arguments.of(new double[] {-0.0, +0.0}, 0.0),
+            Arguments.of(new double[] {0.0, -0.0}, 0.0),
+            Arguments.of(new double[] {0.0, +0.0}, 0.0),
+            Arguments.of(new double[] {1.2, -34.56, 456.789, -5678.9012}, 456.789),
+            Arguments.of(new double[] {-23467824, 23648, 2368, 23749, -23424, -23492, -92397747}, 23749),
+            Arguments.of(new double[] {0.0d, +0.0d, -0.0d, Double.POSITIVE_INFINITY, Double.MIN_VALUE}, Double.POSITIVE_INFINITY),
+            Arguments.of(new double[] {0.0d, +0.0d, -0.0d, Double.POSITIVE_INFINITY, -Double.MIN_VALUE}, Double.POSITIVE_INFINITY),
+            Arguments.of(new double[] {0.0d, +0.0d, -0.0d, Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, Double.MIN_VALUE}, Double.POSITIVE_INFINITY)

Review Comment:
   Quickly compared the code in this PR with the existing Min code, and everything looks OK. The only part I noticed that did not change was this with the `Double.MIN_VALUE`'s. Is that intentional or were we supposed to use `Double.MAX_VALUE` for this test?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@commons.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org