sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject [sis] branch geoapi-4.0 updated: Add some tests.
Date Fri, 01 Mar 2019 15:54:46 GMT
This is an automated email from the ASF dual-hosted git repository.

desruisseaux pushed a commit to branch geoapi-4.0
in repository https://gitbox.apache.org/repos/asf/sis.git


The following commit(s) were added to refs/heads/geoapi-4.0 by this push:
     new f7788b2  Add some tests.
f7788b2 is described below

commit f7788b25bc6c93cd470e9d8234957142b9682aaf
Author: Martin Desruisseaux <martin.desruisseaux@geomatys.com>
AuthorDate: Fri Mar 1 16:52:43 2019 +0100

    Add some tests.
---
 .../src/main/java/org/apache/sis/math/Vector.java  | 46 ++++++-------
 .../apache/sis/math/LinearlyDerivedVectorTest.java | 79 ++++++++++++++++++++++
 .../apache/sis/test/suite/UtilityTestSuite.java    |  1 +
 3 files changed, 103 insertions(+), 23 deletions(-)

diff --git a/core/sis-utility/src/main/java/org/apache/sis/math/Vector.java b/core/sis-utility/src/main/java/org/apache/sis/math/Vector.java
index 1c724ae..f25ed0d 100644
--- a/core/sis-utility/src/main/java/org/apache/sis/math/Vector.java
+++ b/core/sis-utility/src/main/java/org/apache/sis/math/Vector.java
@@ -719,7 +719,7 @@ search:     for (;;) {
              * The result will be converted to the same type than the vector element type
if possible,
              * or the next wider type if the increment is an unsigned value too big for the
element type.
              */
-            if (type >= Numbers.BYTE && type <= Numbers.LONG && tolerance
< 1) {
+            if (type >= Numbers.BYTE && type <= Numbers.LONG && tolerance
< 0.5) {
                 long p;
                 final long inc = subtract(longValue(--i), p = longValue(--i));
                 while (i != 0) {
@@ -743,38 +743,38 @@ search:     for (;;) {
              * this vector by an instance of SequenceVector should produce exactly the same
double values,
              * in the limit of the accuracy allowed by the floating point values.
              */
-            if (type >= Numbers.FLOAT && type <= Numbers.DOUBLE) {
-                final double first = doubleValue(0);
-                double inc = (doubleValue(--i) - first) / i;                            
 // First estimation of increment.
+            final double first = doubleValue(0);
+            double inc = (doubleValue(--i) - first) / i;                              //
First estimation of increment.
+            if (type == Numbers.DOUBLE || type == Numbers.BIG_DECIMAL) {
                 final int pz = Math.max(0, Math.min(i, (int) Math.rint(-first / inc))); 
 // Presumed index of value zero.
                 if (doubleValue(pz) == 0) {
-                    final Number value = (pz == i) ? get(pz-1) : get(pz+1);             
 // Value adjacent to zero.
-                    if (!(value instanceof Float)) {
+                    final Number value = (pz == i) ? get(pz-1) : get(pz+1);     // Value
adjacent to zero.
+                    if (value != null && !(value instanceof Float)) {           //
Float type is not accurate enough.
                         inc = value.doubleValue();                              // Presumed
less subject to rounding errors.
                         if (pz == i) inc = -inc;
                     }
                 }
-                if (type == Numbers.FLOAT) {
-                    while (i >= 1) {
-                        final float  value = floatValue(i);
-                        final double delta = Math.abs(first + inc*i-- - value);
-                        final double accur = Math.ulp(value);
-                        if (!((accur > tolerance) ? (delta < accur) : (delta <=
tolerance))) {  // Use '!' for catching NaN.
-                            return null;
-                        }
+            }
+            if (type == Numbers.FLOAT) {
+                while (i >= 1) {
+                    final float  value = floatValue(i);
+                    final double delta = Math.abs(first + inc*i-- - value);
+                    final double accur = Math.ulp(value);
+                    if (!((accur > tolerance) ? (delta < accur) : (delta <= tolerance)))
{  // Use '!' for catching NaN.
+                        return null;
                     }
-                    final float f = (float) inc;
-                    if (f == inc) return f;                            // Use the java.lang.Float
wrapper class if possible.
-                } else {
-                    while (i >= 1) {
-                        final double delta = Math.abs(first + inc*i - doubleValue(i--));
-                        if (!(delta <= tolerance)) {                   // Use '!' for
catching NaN.
-                            return null;
-                        }
+                }
+                final float f = (float) inc;
+                if (f == inc) return f;                            // Use the java.lang.Float
wrapper class if possible.
+            } else {
+                while (i >= 1) {
+                    final double delta = Math.abs(first + inc*i - doubleValue(i--));
+                    if (!(delta <= tolerance)) {                   // Use '!' for catching
NaN.
+                        return null;
                     }
                 }
-                return inc;
             }
+            return inc;
         } catch (ArithmeticException e) {
             warning("increment", e);
         }
diff --git a/core/sis-utility/src/test/java/org/apache/sis/math/LinearlyDerivedVectorTest.java
b/core/sis-utility/src/test/java/org/apache/sis/math/LinearlyDerivedVectorTest.java
new file mode 100644
index 0000000..288c3ea
--- /dev/null
+++ b/core/sis-utility/src/test/java/org/apache/sis/math/LinearlyDerivedVectorTest.java
@@ -0,0 +1,79 @@
+/*
+ * 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.sis.math;
+
+import org.apache.sis.measure.NumberRange;
+import org.apache.sis.test.TestCase;
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+
+
+/**
+ * Tests the {@link LinearlyDerivedVector} class.
+ *
+ * @author  Martin Desruisseaux (Geomatys)
+ * @version 1.0
+ * @since   1.0
+ * @module
+ */
+public final strictfp class LinearlyDerivedVectorTest extends TestCase {
+    /**
+     * Tests creation of a vector and a few methods that haven been overridden.
+     */
+    @Test
+    public void testBasicMethods() {
+        Vector vec = Vector.create(new int[]    {10, 12, 15, -2}, false).transform(2, -3);
+        final double[] expected =  new double[] {17, 21, 27, -7};
+        assertArrayEquals("floats", new float[] {17, 21, 27, -7}, vec.floatValues(), (float)
STRICT);
+        assertArrayEquals(expected, vec.doubleValues(), STRICT);
+        assertTrue("equals", vec.equals(Vector.create(expected)));                      //
'equals' must be invoked on 'vec'.
+        assertEquals("range", NumberRange.create(-7d, true, 27d, true), vec.range());
+    }
+
+    /**
+     * Tests sub-lists of a derived vector.
+     */
+    @Test
+    public void testSubList() {
+        Vector vec = Vector.create(new int[]    {10, 12, 15, -2}, false).transform(2, -3).subList(0,
3);
+        final double[] expected =  new double[] {17, 21, 27};
+        assertArrayEquals("floats", new float[] {17, 21, 27}, vec.floatValues(), (float)
STRICT);
+        assertArrayEquals(expected, vec.doubleValues(), STRICT);
+        assertTrue  ("equals",    vec.equals(Vector.create(expected)));
+        assertEquals("range",     NumberRange.create(17d, true, 27d, true), vec.range());
+        assertNull  ("increment", vec.increment(0.9));
+        assertEquals("increment", 5d, vec.increment(2));
+    }
+
+    /**
+     * Tests application of two {@code transform} method calls, which should be merged in
a single wrapper.
+     */
+    @Test
+    public void testOptimizations() {
+        Vector vec = Vector.create(new int[] {10, 12, 15, -2}, false);
+        vec = vec.transform(2, -3);
+        vec = vec.transform(-1, 2);
+        assertArrayEquals("floats", new float[] {-15, -19, -25, 9}, vec.floatValues(), (float)
STRICT);
+
+        vec = vec.subList(0, 2).transform(-1, 0);
+        assertArrayEquals("floats", new float[] {15, 19}, vec.floatValues(), (float) STRICT);
+
+        vec = vec.concatenate(Vector.create(new double[] {8, 3})).transform(0.25, 4);
+        assertArrayEquals("floats", new float[] {7.75f, 8.75f, 6f, 4.75f}, vec.floatValues(),
(float) STRICT);
+    }
+}
diff --git a/core/sis-utility/src/test/java/org/apache/sis/test/suite/UtilityTestSuite.java
b/core/sis-utility/src/test/java/org/apache/sis/test/suite/UtilityTestSuite.java
index 1b626b5..57d2de5 100644
--- a/core/sis-utility/src/test/java/org/apache/sis/test/suite/UtilityTestSuite.java
+++ b/core/sis-utility/src/test/java/org/apache/sis/test/suite/UtilityTestSuite.java
@@ -58,6 +58,7 @@ import org.junit.BeforeClass;
     org.apache.sis.math.FractionTest.class,
     org.apache.sis.math.VectorTest.class,
     org.apache.sis.math.RepeatedVectorTest.class,
+    org.apache.sis.math.LinearlyDerivedVectorTest.class,
     org.apache.sis.math.MathFunctionsTest.class,
     org.apache.sis.math.DecimalFunctionsTest.class,
     org.apache.sis.math.StatisticsTest.class,


Mime
View raw message