sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1522552 - in /sis/branches/JDK7/core/sis-referencing/src: main/java/org/apache/sis/referencing/operation/matrix/ test/java/org/apache/sis/referencing/operation/matrix/
Date Thu, 12 Sep 2013 12:44:58 GMT
Author: desruisseaux
Date: Thu Sep 12 12:44:57 2013
New Revision: 1522552

URL: http://svn.apache.org/r1522552
Log:
Completed the set of tests related to constructors (not yet operation methods).

Modified:
    sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/Matrices.java
    sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/matrix/GeneralMatrixTest.java
    sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/matrix/Matrix1Test.java
    sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/matrix/Matrix2Test.java
    sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/matrix/Matrix3Test.java
    sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/matrix/Matrix4Test.java
    sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/matrix/MatrixTestCase.java
    sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/matrix/NonSquareMatrixTest.java

Modified: sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/Matrices.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/Matrices.java?rev=1522552&r1=1522551&r2=1522552&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/Matrices.java
[UTF-8] (original)
+++ sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/matrix/Matrices.java
[UTF-8] Thu Sep 12 12:44:57 2013
@@ -111,14 +111,12 @@ public final class Matrices extends Stat
      * @return A matrix of the given size with only zero values.
      */
     public static MatrixSIS createZero(final int numRow, final int numCol) {
-        if (numRow == numCol) {
-            switch (numRow) {
-                case 1:  return new Matrix1(false);
-                case 2:  return new Matrix2(false);
-                case 3:  return new Matrix3(false);
-                case 4:  return new Matrix4(false);
-                default: return new GeneralMatrix(numRow, numCol, false);
-            }
+        if (numRow == numCol) switch (numRow) {
+            case 1:  return new Matrix1(false);
+            case 2:  return new Matrix2(false);
+            case 3:  return new Matrix3(false);
+            case 4:  return new Matrix4(false);
+            default: return new GeneralMatrix(numRow, numCol, false);
         }
         return new NonSquareMatrix(numRow, numCol, false);
     }
@@ -142,12 +140,13 @@ public final class Matrices extends Stat
      */
     public static MatrixSIS create(final int numRow, final int numCol, final double[] elements)
{
         if (numRow == numCol) switch (numRow) {
-            case 1: return new Matrix1(elements);
-            case 2: return new Matrix2(elements);
-            case 3: return new Matrix3(elements);
-            case 4: return new Matrix4(elements);
+            case 1:  return new Matrix1(elements);
+            case 2:  return new Matrix2(elements);
+            case 3:  return new Matrix3(elements);
+            case 4:  return new Matrix4(elements);
+            default: return new GeneralMatrix(numRow, numCol, elements);
         }
-        return new GeneralMatrix(numRow, numCol, elements);
+        return new NonSquareMatrix(numRow, numCol, elements);
     }
 
     /**

Modified: sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/matrix/GeneralMatrixTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/matrix/GeneralMatrixTest.java?rev=1522552&r1=1522551&r2=1522552&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/matrix/GeneralMatrixTest.java
[UTF-8] (original)
+++ sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/matrix/GeneralMatrixTest.java
[UTF-8] Thu Sep 12 12:44:57 2013
@@ -21,6 +21,7 @@ import static org.junit.Assert.*;
 
 /**
  * Tests the {@link GeneralMatrix} implementation with square matrices.
+ * This class inherits all tests defined in {@link MatrixTestCase}.
  *
  * @author  Martin Desruisseaux (Geomatys)
  * @since   0.4

Modified: sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/matrix/Matrix1Test.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/matrix/Matrix1Test.java?rev=1522552&r1=1522551&r2=1522552&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/matrix/Matrix1Test.java
[UTF-8] (original)
+++ sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/matrix/Matrix1Test.java
[UTF-8] Thu Sep 12 12:44:57 2013
@@ -16,11 +16,15 @@
  */
 package org.apache.sis.referencing.operation.matrix;
 
+import org.junit.Test;
+
 import static org.junit.Assert.*;
+import static org.apache.sis.referencing.operation.matrix.Matrix1.SIZE;
 
 
 /**
  * Tests the {@link Matrix1} implementation.
+ * This class inherits all tests defined in {@link MatrixTestCase}.
  *
  * @author  Martin Desruisseaux (Geomatys)
  * @since   0.4
@@ -28,8 +32,11 @@ import static org.junit.Assert.*;
  * @module
  */
 public final strictfp class Matrix1Test extends MatrixTestCase {
-    /** {@inheritDoc} */ @Override int getNumRow() {return 1;}
-    /** {@inheritDoc} */ @Override int getNumCol() {return 1;}
+    /**
+     * Returns the size of the matrix of interest for this test class.
+     */
+    @Override int getNumRow() {return SIZE;}
+    @Override int getNumCol() {return SIZE;}
 
     /**
      * Ensures that the given matrix is an instance of the expected type.
@@ -39,4 +46,17 @@ public final strictfp class Matrix1Test 
         super.validate(matrix);
         assertEquals(Matrix1.class, matrix.getClass());
     }
+
+    /**
+     * Tests the {@link Matrix1#Matrix1(double)} constructor.
+     * This constructor is specific to the implementation class.
+     */
+    @Test
+    public void testConstructor() {
+        final double[] elements = initConstructorTest(SIZE * SIZE);
+        final Matrix1 matrix = new Matrix1(
+                elements[0]);
+        validate(matrix);
+        assertArrayEquals(elements, matrix.getElements(), STRICT);
+    }
 }

Modified: sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/matrix/Matrix2Test.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/matrix/Matrix2Test.java?rev=1522552&r1=1522551&r2=1522552&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/matrix/Matrix2Test.java
[UTF-8] (original)
+++ sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/matrix/Matrix2Test.java
[UTF-8] Thu Sep 12 12:44:57 2013
@@ -16,11 +16,15 @@
  */
 package org.apache.sis.referencing.operation.matrix;
 
+import org.junit.Test;
+
 import static org.junit.Assert.*;
+import static org.apache.sis.referencing.operation.matrix.Matrix2.SIZE;
 
 
 /**
  * Tests the {@link Matrix2} implementation.
+ * This class inherits all tests defined in {@link MatrixTestCase}.
  *
  * @author  Martin Desruisseaux (Geomatys)
  * @since   0.4
@@ -28,8 +32,11 @@ import static org.junit.Assert.*;
  * @module
  */
 public final strictfp class Matrix2Test extends MatrixTestCase {
-    /** {@inheritDoc} */ @Override int getNumRow() {return 2;}
-    /** {@inheritDoc} */ @Override int getNumCol() {return 2;}
+    /**
+     * Returns the size of the matrix of interest for this test class.
+     */
+    @Override int getNumRow() {return SIZE;}
+    @Override int getNumCol() {return SIZE;}
 
     /**
      * Ensures that the given matrix is an instance of the expected type.
@@ -39,4 +46,20 @@ public final strictfp class Matrix2Test 
         super.validate(matrix);
         assertEquals(Matrix2.class, matrix.getClass());
     }
+
+    /**
+     * Tests the {@link Matrix2#Matrix2(double, double, double, double)} constructor.
+     * This constructor is specific to the implementation class.
+     */
+    @Test
+    public void testConstructor() {
+        final double[] elements = initConstructorTest(SIZE * SIZE);
+        final Matrix2 matrix = new Matrix2(
+                elements[0],
+                elements[1],
+                elements[2],
+                elements[3]);
+        validate(matrix);
+        assertArrayEquals(elements, matrix.getElements(), STRICT);
+    }
 }

Modified: sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/matrix/Matrix3Test.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/matrix/Matrix3Test.java?rev=1522552&r1=1522551&r2=1522552&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/matrix/Matrix3Test.java
[UTF-8] (original)
+++ sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/matrix/Matrix3Test.java
[UTF-8] Thu Sep 12 12:44:57 2013
@@ -16,11 +16,15 @@
  */
 package org.apache.sis.referencing.operation.matrix;
 
+import org.junit.Test;
+
 import static org.junit.Assert.*;
+import static org.apache.sis.referencing.operation.matrix.Matrix3.SIZE;
 
 
 /**
  * Tests the {@link Matrix3} implementation.
+ * This class inherits all tests defined in {@link MatrixTestCase}.
  *
  * @author  Martin Desruisseaux (Geomatys)
  * @since   0.4
@@ -28,8 +32,11 @@ import static org.junit.Assert.*;
  * @module
  */
 public final strictfp class Matrix3Test extends MatrixTestCase {
-    /** {@inheritDoc} */ @Override int getNumRow() {return 3;}
-    /** {@inheritDoc} */ @Override int getNumCol() {return 3;}
+    /**
+     * Returns the size of the matrix of interest for this test class.
+     */
+    @Override int getNumRow() {return SIZE;}
+    @Override int getNumCol() {return SIZE;}
 
     /**
      * Ensures that the given matrix is an instance of the expected type.
@@ -39,4 +46,26 @@ public final strictfp class Matrix3Test 
         super.validate(matrix);
         assertEquals(Matrix3.class, matrix.getClass());
     }
+
+    /**
+     * Tests the {@link Matrix3#Matrix3(double, double, double,
+     * double, double, double, double, double, double)} constructor.
+     * This constructor is specific to the implementation class.
+     */
+    @Test
+    public void testConstructor() {
+        final double[] elements = initConstructorTest(SIZE * SIZE);
+        final Matrix3 matrix = new Matrix3(
+                elements[0],
+                elements[1],
+                elements[2],
+                elements[3],
+                elements[4],
+                elements[5],
+                elements[6],
+                elements[7],
+                elements[8]);
+        validate(matrix);
+        assertArrayEquals(elements, matrix.getElements(), STRICT);
+    }
 }

Modified: sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/matrix/Matrix4Test.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/matrix/Matrix4Test.java?rev=1522552&r1=1522551&r2=1522552&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/matrix/Matrix4Test.java
[UTF-8] (original)
+++ sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/matrix/Matrix4Test.java
[UTF-8] Thu Sep 12 12:44:57 2013
@@ -16,11 +16,15 @@
  */
 package org.apache.sis.referencing.operation.matrix;
 
+import org.junit.Test;
+
 import static org.junit.Assert.*;
+import static org.apache.sis.referencing.operation.matrix.Matrix4.SIZE;
 
 
 /**
  * Tests the {@link Matrix4} implementation.
+ * This class inherits all tests defined in {@link MatrixTestCase}.
  *
  * @author  Martin Desruisseaux (Geomatys)
  * @since   0.4
@@ -28,8 +32,11 @@ import static org.junit.Assert.*;
  * @module
  */
 public final strictfp class Matrix4Test extends MatrixTestCase {
-    /** {@inheritDoc} */ @Override int getNumRow() {return 4;}
-    /** {@inheritDoc} */ @Override int getNumCol() {return 4;}
+    /**
+     * Returns the size of the matrix of interest for this test class.
+     */
+    @Override int getNumRow() {return SIZE;}
+    @Override int getNumCol() {return SIZE;}
 
     /**
      * Ensures that the given matrix is an instance of the expected type.
@@ -39,4 +46,33 @@ public final strictfp class Matrix4Test 
         super.validate(matrix);
         assertEquals(Matrix4.class, matrix.getClass());
     }
+
+    /**
+     * Tests the {@link Matrix4#Matrix4(double, double, double, double, double, double, double,
+     * double, double, double, double, double, double, double, double, double)} constructor.
+     * This constructor is specific to the implementation class.
+     */
+    @Test
+    public void testConstructor() {
+        final double[] elements = initConstructorTest(SIZE * SIZE);
+        final Matrix4 matrix = new Matrix4(
+                elements[ 0],
+                elements[ 1],
+                elements[ 2],
+                elements[ 3],
+                elements[ 4],
+                elements[ 5],
+                elements[ 6],
+                elements[ 7],
+                elements[ 8],
+                elements[ 9],
+                elements[10],
+                elements[11],
+                elements[12],
+                elements[13],
+                elements[14],
+                elements[15]);
+        validate(matrix);
+        assertArrayEquals(elements, matrix.getElements(), STRICT);
+    }
 }

Modified: sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/matrix/MatrixTestCase.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/matrix/MatrixTestCase.java?rev=1522552&r1=1522551&r2=1522552&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/matrix/MatrixTestCase.java
[UTF-8] (original)
+++ sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/matrix/MatrixTestCase.java
[UTF-8] Thu Sep 12 12:44:57 2013
@@ -20,6 +20,7 @@ import java.util.Random;
 import Jama.Matrix;
 import org.apache.sis.test.TestCase;
 import org.apache.sis.test.TestUtilities;
+import org.apache.sis.test.DependsOnMethod;
 import org.junit.Test;
 
 import static org.junit.Assert.*;
@@ -47,6 +48,12 @@ import static org.junit.Assert.*;
  */
 public abstract strictfp class MatrixTestCase extends TestCase {
     /**
+     * A constant for any test in this class or a subclass which expect
+     * a floating point value to be strictly equals to an other value.
+     */
+    static final double STRICT = 0;
+
+    /**
      * Random number generator, created by {@link #initialize(String, boolean)} when first
needed.
      */
     Random random;
@@ -83,21 +90,108 @@ public abstract strictfp class MatrixTes
     }
 
     /**
+     * Verifies that the SIS matrix is equals to the JAMA one, up to the given tolerance
value.
+     */
+    private static void assertMatrixEquals(final Matrix expected, final MatrixSIS actual,
final double tolerance) {
+        final int numRow = actual.getNumRow();
+        final int numCol = actual.getNumCol();
+        assertEquals("numRow", expected.getRowDimension(),    numRow);
+        assertEquals("numCol", expected.getColumnDimension(), numCol);
+        for (int j=0; j<numRow; j++) {
+            for (int i=0; i<numCol; i++) {
+                assertEquals(expected.get(j,i), actual.getElement(j,i), tolerance);
+            }
+        }
+    }
+
+    /**
+     * Initializes the random number generator and creates an array of the given length filled
with random values.
+     * This is a convenience method for the {@link testConstructor()} methods in {@code Matrix1…4Test}
subclasses.
+     */
+    final double[] initConstructorTest(final int length) {
+        initialize("testConstructor", true);
+        final double[] elements = new double[length];
+        for (int k=0; k<length; k++) {
+            elements[k] = random.nextDouble() * 100;
+        }
+        return elements;
+    }
+
+    /**
+     * Creates a matrix initialized with a random array of element values,
+     * then tests the {@link MatrixSIS#getElement(int, int)} method for each element.
+     * This test will use {@link Matrices#create(int, int, double[])} for creating the matrix.
+     *
+     * <p>If this test fails, then all other tests in this class will be skipped since
it would
+     * not be possible to verify the result of any matrix operation.</p>
+     */
+    @Test
+    public void testGetElements() {
+        initialize("testGetElements", true);
+        final int numRow = getNumRow();
+        final int numCol = getNumCol();
+        final double[] elements = new double[numRow * numCol];
+        for (int k=0; k<elements.length; k++) {
+            elements[k] = random.nextDouble() * 100;
+        }
+        final MatrixSIS matrix = Matrices.create(numRow, numCol, elements);
+        validate(matrix);
+        /*
+         * The JAMA constructor uses column-major array (FORTRAN convention), while SIS uses
+         * row-major array. So we have to transpose the JAMA matrix after construction.
+         */
+        assertMatrixEquals(new Matrix(elements, numCol).transpose(), matrix, STRICT);
+        assertArrayEquals("getElements", elements, matrix.getElements(), STRICT);
+    }
+
+    /**
+     * Tests {@link MatrixSIS#getElement(int, int)} and {@link MatrixSIS#setElement(int,
int, double)}.
+     * This test sets random values in elements at random index, and compares with a JAMA
matrix taken
+     * as the reference implementation.
+     */
+    @Test
+    @DependsOnMethod("testGetElements")
+    public void testSetElement() {
+        initialize("testSetElement", true);
+        final int numRow = getNumRow();
+        final int numCol = getNumCol();
+        final MatrixSIS matrix = Matrices.createZero(numRow, numCol);
+        validate(matrix);
+        final Matrix reference = new Matrix(numRow, numCol);
+        /*
+         * End of initialization - now perform the actual test.
+         */
+        assertMatrixEquals(reference, matrix, STRICT);
+        for (int k=0; k<50; k++) {
+            final int    j = random.nextInt(numRow);
+            final int    i = random.nextInt(numCol);
+            final double e = random.nextDouble() * 100;
+            reference.set(j, i, e);
+            matrix.setElement(j, i, e);
+            assertMatrixEquals(reference, matrix, STRICT);
+        }
+    }
+
+    /**
      * Tests {@link MatrixSIS#isIdentity()}. This method will first invoke {@link Matrices#create(int,
int)}
      * and ensure that the result contains 1 on the diagonal and 0 elsewhere.
      */
     @Test
+    @DependsOnMethod("testSetElement")
     public void testIsIdentity() {
         initialize("testIsIdentity", false);
         final int numRow = getNumRow();
         final int numCol = getNumCol();
         final MatrixSIS matrix = Matrices.create(numRow, numCol);
         validate(matrix);
+        /*
+         * End of initialization - now perform the actual test.
+         */
         assertEquals("isIdentity", numRow == numCol, matrix.isIdentity());
         for (int j=0; j<numRow; j++) {
             for (int i=0; i<numCol; i++) {
                 final double element = matrix.getElement(j,i);
-                assertEquals((i == j) ? 1 : 0, element, 0);
+                assertEquals((i == j) ? 1 : 0, element, STRICT);
                 matrix.setElement(j, i, 2);
                 assertFalse("isIdentity", matrix.isIdentity());
                 matrix.setElement(j, i, element);

Modified: sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/matrix/NonSquareMatrixTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/matrix/NonSquareMatrixTest.java?rev=1522552&r1=1522551&r2=1522552&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/matrix/NonSquareMatrixTest.java
[UTF-8] (original)
+++ sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/referencing/operation/matrix/NonSquareMatrixTest.java
[UTF-8] Thu Sep 12 12:44:57 2013
@@ -23,6 +23,7 @@ import static org.junit.Assert.*;
 
 /**
  * Tests the {@link NonSquareMatrix} implementation.
+ * This class inherits all tests defined in {@link MatrixTestCase}.
  *
  * @author  Martin Desruisseaux (Geomatys)
  * @since   0.4



Mime
View raw message