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 2019/10/01 13:19:23 UTC

[commons-numbers] branch master updated: Unit tests.

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 9ad130f  Unit tests.
9ad130f is described below

commit 9ad130f4dcec59f5e090f24f7292c81ec2ddd027
Author: Gilles Sadowski <gi...@harfang.homelinux.org>
AuthorDate: Tue Oct 1 15:18:01 2019 +0200

    Unit tests.
---
 .../org/apache/commons/numbers/field/FP64Test.java | 92 ++++++++++++++++++++++
 .../commons/numbers/field/FieldParametricTest.java | 90 +++++++++++++++++++++
 2 files changed, 182 insertions(+)

diff --git a/commons-numbers-field/src/test/java/org/apache/commons/numbers/field/FP64Test.java b/commons-numbers-field/src/test/java/org/apache/commons/numbers/field/FP64Test.java
new file mode 100644
index 0000000..cbd6137
--- /dev/null
+++ b/commons-numbers-field/src/test/java/org/apache/commons/numbers/field/FP64Test.java
@@ -0,0 +1,92 @@
+/*
+ * 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.numbers.field;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Tests for {@link FP64}.
+ */
+public class FP64Test {
+    @Test
+    public void testConsistencyWithDouble() {
+        final double v = -5.67e89;
+        final Double a = Double.valueOf(v);
+        final FP64 b = FP64.of(v);
+
+        Assertions.assertEquals(a.doubleValue(), b.doubleValue(), 0d);
+        Assertions.assertEquals(a.floatValue(), b.floatValue(), 0f);
+        Assertions.assertEquals(a.intValue(), b.intValue());
+        Assertions.assertEquals(a.longValue(), b.longValue());
+        Assertions.assertEquals(a.byteValue(), b.byteValue());
+        Assertions.assertEquals(a.hashCode(), b.hashCode());
+    }
+
+    @Test
+    public void testOne() {
+        Assertions.assertEquals(1d, FP64.of(-3.4).one().doubleValue(), 0d);
+    }
+    @Test
+    public void testZero() {
+        Assertions.assertEquals(0d, FP64.of(-3.4).zero().doubleValue(), 0d);
+    }
+
+    @Test
+    public void testSubtract() {
+        final double a = 123.4;
+        final double b = 5678.9;
+
+        Assertions.assertEquals(a - b, FP64.of(a).subtract(FP64.of(b)).doubleValue(), 0d);
+    }
+    @Test
+    public void testDivide() {
+        final double a = 123.4;
+        final double b = 5678.9;
+
+        Assertions.assertEquals(a / b, FP64.of(a).divide(FP64.of(b)).doubleValue(), 0d);
+    }
+
+    @Test
+    public void testMultiplyInt() {
+        final double a = 123.4;
+        final int n = 3456789;
+
+        Assertions.assertEquals(n * a, FP64.of(a).multiply(n).doubleValue(), 0d);
+    }
+
+    @Test
+    public void testPowInt() {
+        final double a = 123.4;
+        final int n = 5;
+
+        Assertions.assertEquals(Math.pow(a, n), FP64.of(a).pow(n).doubleValue(), 0d);
+    }
+    @Test
+    public void testZeroPow() {
+        Assertions.assertSame(FP64.of(9876.5).one(), FP64.of(2.3456).pow(0));
+    }
+
+    @Test
+    public void testCompare() {
+        Assertions.assertTrue(FP64.of(0).compareTo(FP64.of(-1)) > 0);
+        Assertions.assertTrue(FP64.of(1).compareTo(FP64.of(2)) < 0);
+
+        final double v = 123.45;
+        Assertions.assertTrue(FP64.of(v).compareTo(FP64.of(v)) == 0);
+    }
+}
diff --git a/commons-numbers-field/src/test/java/org/apache/commons/numbers/field/FieldParametricTest.java b/commons-numbers-field/src/test/java/org/apache/commons/numbers/field/FieldParametricTest.java
index 4658a01..fa46bbc 100644
--- a/commons-numbers-field/src/test/java/org/apache/commons/numbers/field/FieldParametricTest.java
+++ b/commons-numbers-field/src/test/java/org/apache/commons/numbers/field/FieldParametricTest.java
@@ -22,6 +22,9 @@ import org.junit.jupiter.params.provider.MethodSource;
 
 import java.util.stream.Stream;
 
+import org.apache.commons.numbers.core.Addition;
+import org.apache.commons.numbers.core.Multiplication;
+
 /**
  * Tests for fields.
  */
@@ -129,6 +132,93 @@ public class FieldParametricTest {
         assertEquals(r1, r2);
     }
 
+    @ParameterizedTest
+    @MethodSource("getList")
+    public <T extends Addition<T>> void testAdd(FieldTestData<T> data) {
+        Field<T> field = data.getField();
+        T a = data.getA();
+        T b = data.getB();
+
+        final T r1 = field.add(a, b);
+        final T r2 = a.add(b);
+        assertEquals(r1, r2);
+    }
+
+    @ParameterizedTest
+    @MethodSource("getList")
+    public <T extends Addition<T>> void testSubtract(FieldTestData<T> data) {
+        Field<T> field = data.getField();
+        T a = data.getA();
+        T b = data.getB();
+
+        final T r1 = field.subtract(a, b);
+        final T r2 = a.add(b.negate());
+        assertEquals(r1, r2);
+    }
+
+    @ParameterizedTest
+    @MethodSource("getList")
+    public <T extends Addition<T>> void testMultiplyInt(FieldTestData<T> data) {
+        Field<T> field = data.getField();
+        T a = data.getA();
+        final int n = 5;
+
+        final T r1 = field.multiply(n, a);
+
+        T r2 = field.zero();
+        for (int i = 0; i < n; i++) {
+            r2 = r2.add(a);
+        }
+
+        assertEquals(r1, r2);
+    }
+
+    @ParameterizedTest
+    @MethodSource("getList")
+    public <T extends Addition<T>> void testZero(FieldTestData<T> data) {
+        Field<T> field = data.getField();
+        T a = data.getA();
+
+        final T r1 = field.zero();
+        final T r2 = a.zero();
+        assertEquals(r1, r2);
+    }
+
+    @ParameterizedTest
+    @MethodSource("getList")
+    public <T extends Multiplication<T>> void testMultiply(FieldTestData<T> data) {
+        Field<T> field = data.getField();
+        T a = data.getA();
+        T b = data.getB();
+
+        final T r1 = field.multiply(a, b);
+        final T r2 = a.multiply(b);
+        assertEquals(r1, r2);
+    }
+
+    @ParameterizedTest
+    @MethodSource("getList")
+    public <T extends Multiplication<T>> void testDivide(FieldTestData<T> data) {
+        Field<T> field = data.getField();
+        T a = data.getA();
+        T b = data.getB();
+
+        final T r1 = field.divide(a, b);
+        final T r2 = a.multiply(b.reciprocal());
+        assertEquals(r1, r2);
+    }
+
+    @ParameterizedTest
+    @MethodSource("getList")
+    public <T extends Multiplication<T>> void testOne(FieldTestData<T> data) {
+        Field<T> field = data.getField();
+        T a = data.getA();
+
+        final T r1 = field.one();
+        final T r2 = a.one();
+        assertEquals(r1, r2);
+    }
+
     /**
      * @param a Instance.
      * @param b Instance.