You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by er...@apache.org on 2021/06/22 12:38:43 UTC

[commons-numbers] branch master updated: NUMBERS-165: Assume that an empty array is a programming error.

This is an automated email from the ASF dual-hosted git repository.

erans pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-numbers.git


The following commit(s) were added to refs/heads/master by this push:
     new 9d0e749  NUMBERS-165: Assume that an empty array is a programming error.
9d0e749 is described below

commit 9d0e749f7bb4e910a6c97f383afd71b202b3a125
Author: Gilles Sadowski <gi...@gmail.com>
AuthorDate: Tue Jun 22 14:36:56 2021 +0200

    NUMBERS-165: Assume that an empty array is a programming error.
---
 .../src/main/java/org/apache/commons/numbers/core/Norm.java | 13 ++++++++++++-
 .../test/java/org/apache/commons/numbers/core/NormTest.java |  9 ++++++---
 2 files changed, 18 insertions(+), 4 deletions(-)

diff --git a/commons-numbers-core/src/main/java/org/apache/commons/numbers/core/Norm.java b/commons-numbers-core/src/main/java/org/apache/commons/numbers/core/Norm.java
index 92587fc..9b76862 100644
--- a/commons-numbers-core/src/main/java/org/apache/commons/numbers/core/Norm.java
+++ b/commons-numbers-core/src/main/java/org/apache/commons/numbers/core/Norm.java
@@ -180,13 +180,14 @@ public enum Norm {
      *  <li>If any value is {@link Double#NaN}, then the result is {@link Double#NaN}.</li>
      *  <li>If any value is infinite and no value is not {@link Double#NaN}, then the
      *   result is {@link Double#POSITIVE_INFINITY}.</li>
-     *  <li>If the array is empty, then the result is 0.</li>
      * </ul>
      *
      * @param v Argument.
      * @return the norm.
+     * @throws IllegalArgumentException if the array is empty.
      */
     public final double of(double[] v) {
+        ensureNonEmpty(v);
         return array.of(v);
     }
 
@@ -557,4 +558,14 @@ public enum Norm {
         }
         return max;
     }
+
+    /**
+     * @param a Array.
+     * @throws IllegalArgumentException for zero-size array.
+     */
+    private static void ensureNonEmpty(double[] a) {
+        if (a.length == 0) {
+            throw new IllegalArgumentException("Empty array");
+        }
+    }
 }
diff --git a/commons-numbers-core/src/test/java/org/apache/commons/numbers/core/NormTest.java b/commons-numbers-core/src/test/java/org/apache/commons/numbers/core/NormTest.java
index 8d4076c..6c1869c 100644
--- a/commons-numbers-core/src/test/java/org/apache/commons/numbers/core/NormTest.java
+++ b/commons-numbers-core/src/test/java/org/apache/commons/numbers/core/NormTest.java
@@ -89,7 +89,8 @@ class NormTest {
     @Test
     void testManhattan_array() {
         // act/assert
-        Assertions.assertEquals(0d, Norm.L1.of(new double[0]));
+        Assertions.assertThrows(IllegalArgumentException.class, () -> Norm.L1.of(new double[0]));
+
         Assertions.assertEquals(0d, Norm.L1.of(new double[] {0d, -0d}));
         Assertions.assertEquals(6d, Norm.L1.of(new double[] {-1d, 2d, -3d}));
         Assertions.assertEquals(10d, Norm.L1.of(new double[] {-1d, 2d, -3d, 4d}));
@@ -323,7 +324,8 @@ class NormTest {
     @Test
     void testEuclidean_array_simple() {
         // act/assert
-        Assertions.assertEquals(0d, Norm.L2.of(new double[0]));
+        Assertions.assertThrows(IllegalArgumentException.class, () -> Norm.L2.of(new double[0]));
+
         Assertions.assertEquals(5d, Norm.L2.of(new double[] {-3d, 4d}));
 
         Assertions.assertEquals(Math.sqrt(2), Norm.L2.of(new double[] {1d, -1d}));
@@ -428,7 +430,8 @@ class NormTest {
     @Test
     void testMaximum_array() {
         // act/assert
-        Assertions.assertEquals(0d, Norm.LINF.of(new double[0]));
+        Assertions.assertThrows(IllegalArgumentException.class, () -> Norm.LINF.of(new double[0]));
+
         Assertions.assertEquals(0d, Norm.LINF.of(new double[] {0d, -0d}));
         Assertions.assertEquals(3d, Norm.LINF.of(new double[] {-1d, 2d, -3d}));
         Assertions.assertEquals(4d, Norm.LINF.of(new double[] {-1d, 2d, -3d, 4d}));