From 04efe1c889de25d44f65ee405eff249e399fe29f Mon Sep 17 00:00:00 2001 From: gmungoc Date: Thu, 29 Oct 2020 15:50:58 +0000 Subject: [PATCH] JAL-3761 unit test for MapList.countPos() --- test/jalview/util/MapListTest.java | 64 ++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/test/jalview/util/MapListTest.java b/test/jalview/util/MapListTest.java index d1a9fae..bca778d 100644 --- a/test/jalview/util/MapListTest.java +++ b/test/jalview/util/MapListTest.java @@ -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 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)); + } } -- 1.7.10.2