JAL-3205 one more unit test case
[jalview.git] / test / jalview / math / MatrixTest.java
index 0d066dd..fa446e4 100644 (file)
@@ -1,6 +1,10 @@
 package jalview.math;
 
 import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertFalse;
+import static org.testng.Assert.assertNotEquals;
+import static org.testng.Assert.assertNotSame;
+import static org.testng.Assert.assertNull;
 import static org.testng.Assert.assertTrue;
 import static org.testng.Assert.fail;
 
@@ -12,7 +16,7 @@ import org.testng.internal.junit.ArrayAsserts;
 
 public class MatrixTest
 {
-  final static double DELTA = 0.0001d;
+  final static double DELTA = 0.000001d;
 
   @Test(groups = "Timing")
   public void testPreMultiply_timing()
@@ -186,8 +190,23 @@ public class MatrixTest
       }
     }
     Matrix m1 = new Matrix(in);
+
     Matrix m2 = (Matrix) m1.copy();
+    assertNotSame(m1, m2);
     assertTrue(matrixEquals(m1, m2));
+    assertNull(m2.d);
+    assertNull(m2.e);
+
+    /*
+     * now add d and e vectors and recopy
+     */
+    m1.d = Arrays.copyOf(in[2], in[2].length);
+    m1.e = Arrays.copyOf(in[4], in[4].length);
+    m2 = (Matrix) m1.copy();
+    assertNotSame(m2.d, m1.d);
+    assertNotSame(m2.e, m1.e);
+    assertEquals(m2.d, m1.d);
+    assertEquals(m2.e, m1.e);
   }
 
   /**
@@ -353,7 +372,7 @@ public class MatrixTest
     assertMatricesMatch(m1, m2);
   }
 
-  private void assertMatricesMatch(MatrixI m1, MatrixI m2)
+  public static void assertMatricesMatch(MatrixI m1, MatrixI m2)
   {
     if (m1.height() != m2.height())
     {
@@ -375,59 +394,194 @@ public class MatrixTest
         }
       }
     }
-    ArrayAsserts.assertArrayEquals(m1.getD(), m2.getD(), 0.00001d);
-    ArrayAsserts.assertArrayEquals(m1.getE(), m2.getE(), 0.00001d);
+    ArrayAsserts.assertArrayEquals("D vector", m1.getD(), m2.getD(),
+            0.00001d);
+    ArrayAsserts.assertArrayEquals("E vector", m1.getE(), m2.getE(),
+            0.00001d);
   }
 
   @Test(groups = "Functional")
-  public void testGetMaxValue() {
+  public void testFindMinMax()
+  {
+    /*
+     * empty matrix case
+     */
+    Matrix m = new Matrix(new double[][] { {} });
+    assertNull(m.findMinMax());
+
+    /*
+     * normal case
+     */
     double[][] vals = new double[2][];
     vals[0] = new double[] {7d, 1d, -2.3d};
     vals[1] = new double[] {-12d, 94.3d, -102.34d};
-    MatrixI m = new Matrix(vals);
-    assertEquals(m.getMaxValue(), 94.3d);
+    m = new Matrix(vals);
+    double[] minMax = m.findMinMax();
+    assertEquals(minMax[0], -102.34d);
+    assertEquals(minMax[1], 94.3d);
   }
 
   @Test(groups = { "Functional", "Timing" })
-  public void testGetMaxValue_timing()
+  public void testFindMinMax_timing()
   {
     Random r = new Random();
     int size = 1000; // increase to stress test timing
     double[][] vals = new double[size][size];
     double max = -Double.MAX_VALUE;
+    double min = Double.MAX_VALUE;
     for (int i = 0; i < size; i++)
     {
       vals[i] = new double[size];
       for (int j = 0; j < size; j++)
       {
-        double d = r.nextDouble();
+        // use nextLong rather than nextDouble to include negative values
+        double d = r.nextLong();
         if (d > max)
         {
           max = d;
         }
+        if (d < min)
+        {
+          min = d;
+        }
         vals[i][j] = d;
       }
-      i++;
     }
-    MatrixI m = new Matrix(vals);
+    Matrix m = new Matrix(vals);
     long now = System.currentTimeMillis();
-    double theMax = m.getMaxValue();
-    System.out.println(String.format("getMaxValue for %d x %d took %dms",
+    double[] minMax = m.findMinMax();
+    System.out.println(String.format("findMinMax for %d x %d took %dms",
             size, size, (System.currentTimeMillis() - now)));
-    assertEquals(theMax, max);
+    assertEquals(minMax[0], min);
+    assertEquals(minMax[1], max);
   }
 
+  /**
+   * Test range reversal with maximum value becoming zero
+   */
   @Test(groups = "Functional")
-  public void testSubtractAllFrom()
+  public void testReverseRange_maxToZero()
   {
-    Matrix m1 = new Matrix(new double[][] { { 2, 3, 4 }, { -3, 4, 15 } });
-    m1.subtractAllFrom(12.5);
-    assertEquals(m1.getValue(0, 0), 10.5d);
-    assertEquals(m1.getValue(0, 1), 9.5d);
-    assertEquals(m1.getValue(0, 2), 8.5d);
-    assertEquals(m1.getValue(1, 0), 15.5d);
-    assertEquals(m1.getValue(1, 1), 8.5d);
-    assertEquals(m1.getValue(1, 2), -2.5d);
+    Matrix m1 = new Matrix(
+            new double[][] { { 2, 3.5, 4 }, { -3.4, 4, 15 } });
+
+    /*
+     * subtract all from max: range -3.4 to 15 becomes 18.4 to 0
+     */
+    m1.reverseRange(true);
+    assertEquals(m1.getValue(0, 0), 13d, DELTA);
+    assertEquals(m1.getValue(0, 1), 11.5d, DELTA);
+    assertEquals(m1.getValue(0, 2), 11d, DELTA);
+    assertEquals(m1.getValue(1, 0), 18.4d, DELTA);
+    assertEquals(m1.getValue(1, 1), 11d, DELTA);
+    assertEquals(m1.getValue(1, 2), 0d, DELTA);
+
+    /*
+     * repeat operation - range is now 0 to 18.4
+     */
+    m1.reverseRange(true);
+    assertEquals(m1.getValue(0, 0), 5.4d, DELTA);
+    assertEquals(m1.getValue(0, 1), 6.9d, DELTA);
+    assertEquals(m1.getValue(0, 2), 7.4d, DELTA);
+    assertEquals(m1.getValue(1, 0), 0d, DELTA);
+    assertEquals(m1.getValue(1, 1), 7.4d, DELTA);
+    assertEquals(m1.getValue(1, 2), 18.4d, DELTA);
   }
 
+  /**
+   * Test range reversal with minimum and maximum values swapped
+   */
+  @Test(groups = "Functional")
+  public void testReverseRange_swapMinMax()
+  {
+    Matrix m1 = new Matrix(
+            new double[][] { { 2, 3.5, 4 }, { -3.4, 4, 15 } });
+  
+    /*
+     * swap all values in min-max range
+     * = subtract from (min + max = 11.6) 
+     * range -3.4 to 15 becomes 18.4 to -3.4
+     */
+    m1.reverseRange(false);
+    assertEquals(m1.getValue(0, 0), 9.6d, DELTA);
+    assertEquals(m1.getValue(0, 1), 8.1d, DELTA);
+    assertEquals(m1.getValue(0, 2), 7.6d, DELTA);
+    assertEquals(m1.getValue(1, 0), 15d, DELTA);
+    assertEquals(m1.getValue(1, 1), 7.6d, DELTA);
+    assertEquals(m1.getValue(1, 2), -3.4d, DELTA);
+  
+    /*
+     * repeat operation - original values restored
+     */
+    m1.reverseRange(false);
+    assertEquals(m1.getValue(0, 0), 2d, DELTA);
+    assertEquals(m1.getValue(0, 1), 3.5d, DELTA);
+    assertEquals(m1.getValue(0, 2), 4d, DELTA);
+    assertEquals(m1.getValue(1, 0), -3.4d, DELTA);
+    assertEquals(m1.getValue(1, 1), 4d, DELTA);
+    assertEquals(m1.getValue(1, 2), 15d, DELTA);
+  }
+
+  @Test(groups = "Functional")
+  public void testMultiply()
+  {
+    Matrix m = new Matrix(new double[][] { { 2, 3.5, 4 }, { -3.4, 4, 15 } });
+    m.multiply(2d);
+    assertEquals(m.getValue(0, 0), 4d, DELTA);
+    assertEquals(m.getValue(0, 1), 7d, DELTA);
+    assertEquals(m.getValue(0, 2), 8d, DELTA);
+    assertEquals(m.getValue(1, 0), -6.8d, DELTA);
+    assertEquals(m.getValue(1, 1), 8d, DELTA);
+    assertEquals(m.getValue(1, 2), 30d, DELTA);
+  }
+
+  @Test(groups = "Functional")
+  public void testConstructor()
+  {
+    double[][] values = new double[][] { { 1, 2, 3 }, { 4, 5, 6 } };
+    Matrix m = new Matrix(values);
+    assertEquals(m.getValue(0, 0), 1d, DELTA);
+
+    /*
+     * verify the matrix has a copy of the original array
+     */
+    assertNotSame(values[0], m.getRow(0));
+    values[0][0] = -1d;
+    assertEquals(m.getValue(0, 0), 1d, DELTA); // unchanged
+  }
+
+  @Test(groups = "Functional")
+  public void testEquals_hashCode()
+  {
+    double[][] values = new double[][] { { 1, 2, 3 }, { 4, 5, 6 } };
+    Matrix m1 = new Matrix(values);
+    double[][] values2 = new double[][] { { 1, 2, 3 }, { 4, 5, 6 } };
+    Matrix m2 = new Matrix(values2);
+
+    assertTrue(m1.equals(m1));
+    assertTrue(m1.equals(m2));
+    assertTrue(m2.equals(m1));
+    // equal objects should have same hashCode
+    assertEquals(m1.hashCode(), m2.hashCode());
+
+    double[][] values3 = new double[][] { { 1, 2, 3 }, { 4, 5, 7 } };
+    m2 = new Matrix(values3);
+    assertFalse(m1.equals(m2));
+    assertFalse(m2.equals(m1));
+    assertNotEquals(m1.hashCode(), m2.hashCode());
+
+    // same hashCode doesn't always mean equal
+    values2 = new double[][] { { 1, 2, 3 }, { 4, 6, 5 } };
+    m2 = new Matrix(values2);
+    assertFalse(m2.equals(m1));
+    assertEquals(m1.hashCode(), m2.hashCode());
+
+    // must be same shape
+    values2 = new double[][] { { 1, 2, 3 } };
+    m2 = new Matrix(values2);
+    assertFalse(m2.equals(m1));
+
+    assertFalse(m1.equals(null));
+    assertFalse(m1.equals("foo"));
+  }
 }