JAL-3761 unit test for MapList.countPos()
authorgmungoc <g.m.carstairs@dundee.ac.uk>
Thu, 29 Oct 2020 15:50:58 +0000 (15:50 +0000)
committerBen Soares <b.soares@dundee.ac.uk>
Wed, 15 Sep 2021 20:34:17 +0000 (21:34 +0100)
test/jalview/util/MapListTest.java

index d1a9fae..bca778d 100644 (file)
@@ -25,6 +25,7 @@ import static org.testng.AssertJUnit.assertFalse;
 import static org.testng.AssertJUnit.assertNull;
 import static org.testng.AssertJUnit.assertSame;
 import static org.testng.AssertJUnit.assertTrue;
+import static org.testng.AssertJUnit.fail;
 import static org.testng.internal.junit.ArrayAsserts.assertArrayEquals;
 
 import java.util.ArrayList;
@@ -1021,4 +1022,67 @@ public class MapListTest
     ranges.add(new int[] { 3, 6 });
     assertNull(MapList.traverseToPosition(ranges, 0));
   }
+
+  @Test(groups = { "Functional" })
+  public void testCountPos()
+  {
+    try
+    {
+      MapList.countPos(null, 1);
+      fail("expected exception");
+    } catch (NullPointerException e)
+    {
+      // expected
+    }
+
+    List<int[]> intervals = new ArrayList<>();
+    assertNull(MapList.countPos(intervals,  1));
+    
+    /*
+     * forward strand
+     */
+    intervals.add(new int[] {10, 20});
+    assertNull(MapList.countPos(intervals,  9));
+    assertNull(MapList.countPos(intervals,  21));
+    assertArrayEquals(new int[] {1, 1}, MapList.countPos(intervals,  10));
+    assertArrayEquals(new int[] {6, 1}, MapList.countPos(intervals,  15));
+    assertArrayEquals(new int[] {11, 1}, MapList.countPos(intervals,  20));
+
+    intervals.add(new int[] {25, 25});
+    assertArrayEquals(new int[] {12, 1}, MapList.countPos(intervals,  25));
+
+    // next interval repeats position 25 - which should be counted twice if traversed
+    intervals.add(new int[] {25, 26});
+    assertArrayEquals(new int[] {12, 1}, MapList.countPos(intervals,  25));
+    assertArrayEquals(new int[] {14, 1}, MapList.countPos(intervals,  26));
+    
+    /*
+     * reverse strand
+     */
+    intervals.clear();
+    intervals.add(new int[] {5, -5});
+    assertNull(MapList.countPos(intervals,  6));
+    assertNull(MapList.countPos(intervals,  -6));
+    assertArrayEquals(new int[] {1, -1}, MapList.countPos(intervals,  5));
+    assertArrayEquals(new int[] {7, -1}, MapList.countPos(intervals,  -1));
+    assertArrayEquals(new int[] {11, -1}, MapList.countPos(intervals,  -5));
+    
+    /*
+     * reverse then forward
+     */
+    intervals.add(new int[] {5, 10});
+    assertArrayEquals(new int[] {13, 1}, MapList.countPos(intervals,  6));
+    
+    /*
+     * reverse then forward then reverse
+     */
+    intervals.add(new int[] {-10, -20});
+    assertArrayEquals(new int[] {20, -1}, MapList.countPos(intervals,  -12));
+    
+    /*
+     * an interval [x, x] is treated as forward
+     */
+    intervals.add(new int[] {30, 30});
+    assertArrayEquals(new int[] {29, 1}, MapList.countPos(intervals,  30));
+  }
 }