JAL-2416 ScoreMatrix.getMatrix() returns a defensive copy to keep the
[jalview.git] / test / jalview / analysis / scoremodels / ScoreMatrixTest.java
index 97cb742..ae5ad5d 100644 (file)
@@ -1,9 +1,20 @@
 package jalview.analysis.scoremodels;
+
 import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertNotNull;
+import static org.testng.Assert.assertNotSame;
+import static org.testng.Assert.assertTrue;
 import static org.testng.internal.junit.ArrayAsserts.assertArrayEquals;
 
+import jalview.io.DataSourceType;
+import jalview.io.FileParse;
+import jalview.io.ScoreMatrixFile;
 import jalview.math.MatrixI;
 
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.util.Arrays;
+
 import org.testng.annotations.Test;
 
 public class ScoreMatrixTest
@@ -112,6 +123,19 @@ public class ScoreMatrixTest
   }
 
   @Test(groups = "Functional")
+  public void testGetMatrix()
+  {
+    ScoreMatrix sm = ScoreModels.getInstance().getBlosum62();
+    float[][] m = sm.getMatrix();
+    assertEquals(m.length, sm.getSize());
+    assertEquals(m[2][4], -3f);
+    // verify a defensive copy is returned
+    float[][] m2 = sm.getMatrix();
+    assertNotSame(m, m2);
+    assertTrue(Arrays.deepEquals(m, m2));
+  }
+
+  @Test(groups = "Functional")
   public void testGetMatrixIndex()
   {
     ScoreMatrix sm = ScoreModels.getInstance().getBlosum62();
@@ -190,4 +214,34 @@ public class ScoreMatrixTest
     // Q.G + I.W + A.C = -2 + -3 + 0 = -5
     assertEquals(pairwise.getValue(2, 3), -5d);
   }
+
+  /**
+   * Test that the result of outputMatrix can be reparsed to give an identical
+   * ScoreMatrix
+   * 
+   * @throws IOException
+   * @throws MalformedURLException
+   */
+  @Test(groups = "Functional")
+  public void testOutputMatrix_roundTrip() throws MalformedURLException,
+          IOException
+  {
+    ScoreMatrix sm = ScoreModels.getInstance().getBlosum62();
+    String output = sm.outputMatrix(false);
+    FileParse fp = new FileParse(output, DataSourceType.PASTE);
+    ScoreMatrixFile parser = new ScoreMatrixFile(fp);
+    ScoreMatrix sm2 = parser.parseMatrix();
+    assertNotNull(sm2);
+    assertTrue(sm2.equals(sm));
+  }
+
+  @Test(groups = "Functional")
+  public void testEauals()
+  {
+    ScoreMatrix sm = ScoreModels.getInstance().getBlosum62();
+    ScoreMatrix sm2 = new ScoreMatrix(sm.getName(), sm.getSymbols()
+            .toCharArray(), sm.getMatrix());
+    assertTrue(sm.equals(sm2));
+    assertEquals(sm.hashCode(), sm2.hashCode());
+  }
 }