From 6adba3030c8318131d4c27d531b2612d30de780f Mon Sep 17 00:00:00 2001 From: kiramt Date: Tue, 21 Nov 2017 09:12:35 +0000 Subject: [PATCH] JAL-2759 unit tests for VisibleContigsIterator --- src/jalview/datamodel/VisibleContigsIterator.java | 14 +- .../datamodel/VisibleContigsIteratorTest.java | 223 ++++++++++++++++++++ 2 files changed, 234 insertions(+), 3 deletions(-) create mode 100644 test/jalview/datamodel/VisibleContigsIteratorTest.java diff --git a/src/jalview/datamodel/VisibleContigsIterator.java b/src/jalview/datamodel/VisibleContigsIterator.java index d98121a..6d3f43d 100644 --- a/src/jalview/datamodel/VisibleContigsIterator.java +++ b/src/jalview/datamodel/VisibleContigsIterator.java @@ -39,9 +39,17 @@ public class VisibleContigsIterator implements Iterator } if (hideStart > vstart) { - int[] contig = new int[] { vstart, hideStart - 1 }; - vcontigs.add(contig); - endsAtHidden = true; + if (end - 1 > hideStart - 1) + { + int[] contig = new int[] { vstart, hideStart - 1 }; + vcontigs.add(contig); + endsAtHidden = true; + } + else + { + int[] contig = new int[] { vstart, end - 1 }; + vcontigs.add(contig); + } } vstart = hideEnd + 1; diff --git a/test/jalview/datamodel/VisibleContigsIteratorTest.java b/test/jalview/datamodel/VisibleContigsIteratorTest.java new file mode 100644 index 0000000..8f31dae --- /dev/null +++ b/test/jalview/datamodel/VisibleContigsIteratorTest.java @@ -0,0 +1,223 @@ +/* + * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$) + * Copyright (C) $$Year-Rel$$ The Jalview Authors + * + * This file is part of Jalview. + * + * Jalview is free software: you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation, either version 3 + * of the License, or (at your option) any later version. + * + * Jalview is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Jalview. If not, see . + * The Jalview Authors are detailed in the 'AUTHORS' file. + */ +package jalview.datamodel; + +import static org.testng.Assert.assertEquals; +import static org.testng.AssertJUnit.assertFalse; +import static org.testng.AssertJUnit.assertTrue; + +import java.util.ArrayList; +import java.util.List; + +import org.testng.annotations.Test; + +public class VisibleContigsIteratorTest +{ + /** + * Test the iterator with single visible regions + */ + @Test(groups = { "Functional" }) + public void testSimpleVisibleRegions() + { + List hiddenColumns = null; + + // null hidden columns + VisibleContigsIterator it = new VisibleContigsIterator(3, 10, + hiddenColumns); + assertTrue(it.hasNext()); + assertFalse(it.endsAtHidden()); + int[] result = it.next(); + assertEquals(3, result[0]); + assertEquals(9, result[1]); + assertFalse(it.hasNext()); + assertFalse(it.endsAtHidden()); + + hiddenColumns = new ArrayList<>(); + + // no hidden columns + it = new VisibleContigsIterator(3, 10, + hiddenColumns); + assertTrue(it.hasNext()); + assertFalse(it.endsAtHidden()); + result = it.next(); + assertEquals(3, result[0]); + assertEquals(9, result[1]); + assertFalse(it.hasNext()); + assertFalse(it.endsAtHidden()); + + // hidden columns, but not where we are looking + hiddenColumns.add(new int[] { 5, 10 }); + hiddenColumns.add(new int[] { 25, 40 }); + + it = new VisibleContigsIterator(2, 3, hiddenColumns); + assertTrue(it.hasNext()); + assertFalse(it.endsAtHidden()); + result = it.next(); + assertEquals(2, result[0]); + assertEquals(2, result[1]); + assertFalse(it.hasNext()); + assertFalse(it.endsAtHidden()); + + it = new VisibleContigsIterator(5, 7, hiddenColumns); + assertFalse(it.hasNext()); + assertFalse(it.endsAtHidden()); + + it = new VisibleContigsIterator(11, 15, hiddenColumns); + assertTrue(it.hasNext()); + assertFalse(it.endsAtHidden()); + result = it.next(); + assertEquals(11, result[0]); + assertEquals(14, result[1]); + assertFalse(it.hasNext()); + assertFalse(it.endsAtHidden()); + + it = new VisibleContigsIterator(50, 60, hiddenColumns); + assertTrue(it.hasNext()); + assertFalse(it.endsAtHidden()); + result = it.next(); + assertEquals(50, result[0]); + assertEquals(59, result[1]); + assertFalse(it.hasNext()); + assertFalse(it.endsAtHidden()); + } + + /** + * Test the iterator with multiple visible regions + */ + @Test(groups = { "Functional" }) + public void testMultipleVisibleRegions() + { + List hiddenColumns = new ArrayList<>(); + hiddenColumns.add(new int[] { 5, 10 }); + hiddenColumns.add(new int[] { 25, 40 }); + + // all hidden columns covered + VisibleContigsIterator it = new VisibleContigsIterator(3, 50, + hiddenColumns); + assertTrue(it.hasNext()); + assertFalse(it.endsAtHidden()); + int[] result = it.next(); + assertEquals(3, result[0]); + assertEquals(4, result[1]); + + assertTrue(it.hasNext()); + assertFalse(it.endsAtHidden()); + result = it.next(); + assertEquals(11, result[0]); + assertEquals(24, result[1]); + + assertTrue(it.hasNext()); + assertFalse(it.endsAtHidden()); + result = it.next(); + assertEquals(41, result[0]); + assertEquals(49, result[1]); + + assertFalse(it.hasNext()); + assertFalse(it.endsAtHidden()); + } + + /** + * Test the iterator with regions which start/end at hidden region edges + */ + @Test(groups = { "Functional" }) + public void testVisibleRegionsAtHiddenEdges() + { + List hiddenColumns = new ArrayList<>(); + hiddenColumns.add(new int[] { 5, 10 }); + hiddenColumns.add(new int[] { 25, 40 }); + + VisibleContigsIterator it = new VisibleContigsIterator(0, 10, + hiddenColumns); + assertTrue(it.hasNext()); + assertTrue(it.endsAtHidden()); + int[] result = it.next(); + assertEquals(0, result[0]); + assertEquals(4, result[1]); + assertFalse(it.hasNext()); + assertTrue(it.endsAtHidden()); + + it = new VisibleContigsIterator(2, 11, hiddenColumns); + assertTrue(it.hasNext()); + assertTrue(it.endsAtHidden()); + result = it.next(); + assertEquals(2, result[0]); + assertEquals(4, result[1]); + assertFalse(it.hasNext()); + assertTrue(it.endsAtHidden()); + + it = new VisibleContigsIterator(2, 12, hiddenColumns); + assertTrue(it.hasNext()); + assertFalse(it.endsAtHidden()); + result = it.next(); + assertEquals(2, result[0]); + assertEquals(4, result[1]); + assertTrue(it.hasNext()); + assertFalse(it.endsAtHidden()); + result = it.next(); + assertEquals(11, result[0]); + assertEquals(11, result[1]); + assertFalse(it.hasNext()); + assertFalse(it.endsAtHidden()); + + it = new VisibleContigsIterator(13, 25, hiddenColumns); + assertTrue(it.hasNext()); + assertFalse(it.endsAtHidden()); + result = it.next(); + assertEquals(13, result[0]); + assertEquals(24, result[1]); + assertFalse(it.hasNext()); + + it = new VisibleContigsIterator(13, 26, hiddenColumns); + assertTrue(it.hasNext()); + assertTrue(it.endsAtHidden()); + result = it.next(); + assertEquals(13, result[0]); + assertEquals(24, result[1]); + assertFalse(it.hasNext()); + + it = new VisibleContigsIterator(13, 27, hiddenColumns); + assertTrue(it.hasNext()); + assertTrue(it.endsAtHidden()); + result = it.next(); + assertEquals(13, result[0]); + assertEquals(24, result[1]); + assertFalse(it.hasNext()); + + it = new VisibleContigsIterator(13, 41, hiddenColumns); + assertTrue(it.hasNext()); + assertTrue(it.endsAtHidden()); + result = it.next(); + assertEquals(13, result[0]); + assertEquals(24, result[1]); + assertFalse(it.hasNext()); + + it = new VisibleContigsIterator(13, 42, hiddenColumns); + assertTrue(it.hasNext()); + assertFalse(it.endsAtHidden()); + result = it.next(); + assertEquals(13, result[0]); + assertEquals(24, result[1]); + assertTrue(it.hasNext()); + result = it.next(); + assertEquals(41, result[0]); + assertEquals(41, result[1]); + } +} -- 1.7.10.2