From 8b2a4c93f2a04d7ea2a19999bc00d321a8412de2 Mon Sep 17 00:00:00 2001 From: kiramt Date: Wed, 22 Nov 2017 08:14:21 +0000 Subject: [PATCH] JAL-2759 HiddenColumns test polishing --- src/jalview/datamodel/HiddenColumns.java | 36 +++--- test/jalview/datamodel/HiddenColumnsTest.java | 154 ++++++++++++++++++++++++- 2 files changed, 167 insertions(+), 23 deletions(-) diff --git a/src/jalview/datamodel/HiddenColumns.java b/src/jalview/datamodel/HiddenColumns.java index 7862975..4e57aa1 100644 --- a/src/jalview/datamodel/HiddenColumns.java +++ b/src/jalview/datamodel/HiddenColumns.java @@ -618,15 +618,14 @@ public class HiddenColumns { // numColumns is out of date, so recalculate int size = 0; - if (hiddenColumns != null) + + Iterator it = hiddenColumns.iterator(); + while (it.hasNext()) { - Iterator it = hiddenColumns.iterator(); - while (it.hasNext()) - { - int[] range = it.next(); - size += range[1] - range[0] + 1; - } + int[] range = it.next(); + size += range[1] - range[0] + 1; } + numColumns = size; } @@ -1075,8 +1074,12 @@ public class HiddenColumns */ public void makeVisibleAnnotation(AlignmentAnnotation alignmentAnnotation) { - makeVisibleAnnotation(0, alignmentAnnotation.annotations.length, + if (alignmentAnnotation != null + && alignmentAnnotation.annotations != null) + { + makeVisibleAnnotation(0, alignmentAnnotation.annotations.length, alignmentAnnotation); + } } /** @@ -1100,7 +1103,8 @@ public class HiddenColumns int startFrom = start; int endAt = end; - if (alignmentAnnotation.annotations != null) + if (alignmentAnnotation != null + && alignmentAnnotation.annotations != null) { if (hiddenColumns != null && hiddenColumns.size() > 0) { @@ -1317,7 +1321,7 @@ public class HiddenColumns higestRange = (range[1] >= endPos) ? range : higestRange; } - if (lowestRange[0] == -1 && lowestRange[1] == -1) + if (lowestRange[0] == -1) // includes (lowestRange[1] == -1) { startPos = alignmentStartEnd[0]; } @@ -1326,7 +1330,7 @@ public class HiddenColumns startPos = lowestRange[1] + 1; } - if (higestRange[0] == -1 && higestRange[1] == -1) + if (higestRange[0] == -1) // includes (higestRange[1] == -1) { endPos = alignmentStartEnd[1]; } @@ -1373,16 +1377,6 @@ public class HiddenColumns { reveal = hiddenColumns.get(regionindex); } - // or try the next region - else - { - regionindex++; - if (regionindex < hiddenColumns.size() - && hiddenColumns.get(regionindex)[0] == adjres + 1) - { - reveal = hiddenColumns.get(regionindex); - } - } } return reveal; diff --git a/test/jalview/datamodel/HiddenColumnsTest.java b/test/jalview/datamodel/HiddenColumnsTest.java index 7b0e8af..e3dd1cd 100644 --- a/test/jalview/datamodel/HiddenColumnsTest.java +++ b/test/jalview/datamodel/HiddenColumnsTest.java @@ -184,14 +184,31 @@ public class HiddenColumnsTest assertFalse(cs.equals(cs2)); assertFalse(cs2.equals(cs)); + // with the wrong kind of object + assertFalse(cs.equals(new HiddenColumnsCursor())); + + // with a different hiddenColumns object - by size + HiddenColumns cs3 = new HiddenColumns(); + cs3.hideColumns(2, 3); + assertFalse(cs.equals(cs3)); + // with hidden columns added in a different order cs2.hideColumns(6, 9); + assertFalse(cs.equals(cs2)); + assertFalse(cs2.equals(cs)); + cs2.hideColumns(5, 8); assertTrue(cs.equals(cs2)); assertTrue(cs.equals(cs)); assertTrue(cs2.equals(cs)); assertTrue(cs2.equals(cs2)); + + // different ranges, same size + cs.hideColumns(10, 12); + cs2.hideColumns(10, 15); + assertFalse(cs.equals(cs2)); + } @Test(groups = "Functional") @@ -248,7 +265,7 @@ public class HiddenColumnsTest * Test the code used to locate the reference sequence ruler origin */ @Test(groups = { "Functional" }) - public void testLocateVisibleBoundsofSequence() + public void testLocateVisibleStartofSequence() { // create random alignment AlignmentGenerator gen = new AlignmentGenerator(false); @@ -396,7 +413,7 @@ public class HiddenColumnsTest } @Test(groups = { "Functional" }) - public void testLocateVisibleBoundsPathologicals() + public void testLocateVisibleStartPathologicals() { // test some pathological cases we missed AlignmentI al = new Alignment( @@ -406,6 +423,9 @@ public class HiddenColumnsTest cs.hideInsertionsFor(al.getSequenceAt(0)); assertEquals("G", "" + al.getSequenceAt(0).getCharAt(cs.adjustForHiddenColumns(9))); + + // KM: no idea what this is meant to be testing... seems to be an unfinished + // test } @Test(groups = { "Functional" }) @@ -502,6 +522,11 @@ public class HiddenColumnsTest { ColumnSelection colsel = new ColumnSelection(); HiddenColumns cs = new HiddenColumns(); + + // test with null hidden columns + cs.revealHiddenColumns(5, colsel); + assertTrue(colsel.getSelected().isEmpty()); + cs.hideColumns(5, 8); colsel.addElement(10); cs.revealHiddenColumns(5, colsel); @@ -521,6 +546,26 @@ public class HiddenColumnsTest cs.revealHiddenColumns(6, colsel); assertEquals(prevSize, cs.getSize()); assertTrue(colsel.getSelected().isEmpty()); + + // reveal hidden columns when there is more than one region + cs.hideColumns(20, 23); + // now there are 2 hidden regions + assertEquals(2, cs.getNumberOfRegions()); + + cs.revealHiddenColumns(20, colsel); + + // hiddenColumns now has one region + assertEquals(1, cs.getNumberOfRegions()); + + // revealed columns are marked as selected (added to selection): + assertEquals("[20, 21, 22, 23]", colsel.getSelected().toString()); + + // call with a column past the end of the hidden column ranges + colsel.clear(); + cs.revealHiddenColumns(20, colsel); + // hiddenColumns still has 1 region + assertEquals(1, cs.getNumberOfRegions()); + assertTrue(colsel.getSelected().isEmpty()); } @Test(groups = { "Functional" }) @@ -528,6 +573,11 @@ public class HiddenColumnsTest { HiddenColumns hidden = new HiddenColumns(); ColumnSelection colsel = new ColumnSelection(); + + // test with null hidden columns + hidden.revealAllHiddenColumns(colsel); + assertTrue(colsel.getSelected().isEmpty()); + hidden.hideColumns(5, 8); hidden.hideColumns(2, 3); colsel.addElement(11); @@ -678,6 +728,13 @@ public class HiddenColumnsTest { number.nextLong(), number.nextLong(), number.nextLong() }); toMark.set(n * number.nextInt(10), n * (25 + number.nextInt(25))); HiddenColumns hc = new HiddenColumns(); + + if (n == 0) + { + hc.markHiddenRegions(fromMark = new BitSet()); + assertTrue(fromMark.isEmpty()); + } + hc.hideMarkedBits(toMark); // see if we can recover bitfield @@ -729,6 +786,13 @@ public class HiddenColumnsTest System.out.println(startEnd[0] + " : " + startEnd[1]); assertEquals(1, startEnd[0]); assertEquals(23, startEnd[1]); + + // force lowest range to start of alignment + hc = new HiddenColumns(); + hc.hideColumns(3, 4); + startEnd = hc.getVisibleStartAndEndIndex(align.getWidth()); + assertEquals(0, startEnd[0]); + assertEquals(25, startEnd[1]); } @Test(groups = "Functional") @@ -747,6 +811,10 @@ public class HiddenColumnsTest assertEquals(3, result[0]); assertEquals(7, result[1]); + result = hc.getRegionWithEdgeAtRes(4); + assertEquals(10, result[0]); + assertEquals(10, result[1]); + result = hc.getRegionWithEdgeAtRes(5); assertEquals(10, result[0]); assertEquals(10, result[1]); @@ -785,6 +853,11 @@ public class HiddenColumnsTest gappedseq.insertCharAt(7, al.getGapCharacter()); gappedseq.insertCharAt(8, al.getGapCharacter()); + // force different kinds of padding + al.getSequenceAt(3).deleteChars(2, 23); + al.getSequenceAt(4).deleteChars(2, 27); + al.getSequenceAt(5).deleteChars(10, 27); + // create an alignment view with the gapped sequence SequenceI[] seqs = new SequenceI[1]; seqs[0] = gappedseq; @@ -1095,6 +1168,38 @@ public class HiddenColumnsTest AlignmentAnnotation ann = new AlignmentAnnotation("an", "some an", anns); + // null annotation + AlignmentAnnotation nullann = null; + h.makeVisibleAnnotation(nullann); + assertNull(nullann); + + h.makeVisibleAnnotation(1, 2, nullann); + assertNull(nullann); + + // null annotations + AlignmentAnnotation emptyann = new AlignmentAnnotation("an", "some ann", null); + h.makeVisibleAnnotation(emptyann); + assertNull(emptyann.annotations); + + h.makeVisibleAnnotation(3, 4, emptyann); + assertNull(emptyann.annotations); + + // without bounds, does everything + h.makeVisibleAnnotation(ann); + assertEquals(12, ann.annotations.length); + assertNull(ann.annotations[0]); + assertNull(ann.annotations[1]); + assertEquals(1.0f, ann.annotations[2].value); + assertEquals(2.0f, ann.annotations[3].value); + assertEquals(3.0f, ann.annotations[4].value); + assertNull(ann.annotations[5]); + assertNull(ann.annotations[6]); + assertEquals(4.0f, ann.annotations[7].value); + assertEquals(5.0f, ann.annotations[8].value); + assertEquals(6.0f, ann.annotations[9].value); + assertEquals(7.0f, ann.annotations[10].value); + assertEquals(8.0f, ann.annotations[11].value); + // without hidden cols, just truncates h.makeVisibleAnnotation(3, 5, ann); assertEquals(3, ann.annotations.length); @@ -1127,6 +1232,24 @@ public class HiddenColumnsTest assertEquals(2.0f, ann.annotations[0].value); assertEquals(5.0f, ann.annotations[1].value); assertEquals(6.0f, ann.annotations[2].value); + + anns = new Annotation[] { null, null, new Annotation(1), + new Annotation(2), new Annotation(3), null, null, new Annotation(4), + new Annotation(5), new Annotation(6), new Annotation(7), + new Annotation(8), new Annotation(9), new Annotation(10), + new Annotation(11), new Annotation(12), new Annotation(13), + new Annotation(14), new Annotation(15) }; + ann = new AlignmentAnnotation("an", "some an", anns); + h = new HiddenColumns(); + h.hideColumns(5, 18); + h.hideColumns(20, 21); + h.makeVisibleAnnotation(1, 21, ann); + assertEquals(5, ann.annotations.length); + assertEquals(1.0f, ann.annotations[1].value); + assertEquals(2.0f, ann.annotations[2].value); + assertEquals(3.0f, ann.annotations[3].value); + assertNull(ann.annotations[0]); + assertNull(ann.annotations[4]); } @Test(groups = "Functional") @@ -1446,4 +1569,31 @@ public class HiddenColumnsTest assertEquals(42, region[1]); assertFalse(regions.hasNext()); } + + /* + * the VisibleColsIterator is tested elsewhere, this just tests that + * it can be retrieved from HiddenColumns + */ + @Test(groups = "Functional") + public void testGetVisibleColsIterator() + { + HiddenColumns h = new HiddenColumns(); + Iterator it = h.getVisibleColsIterator(0, 10); + + assertTrue(it instanceof VisibleColsIterator); + } + + @Test(groups = "Functional") + public void testHashCode() + { + HiddenColumns h = new HiddenColumns(); + h.hideColumns(0, 25); + + int result = h.hashCode(); + assertTrue(result > 0); + + h.hideColumns(30, 50); + assertTrue(h.hashCode() > 0); + assertTrue(result != h.hashCode()); + } } -- 1.7.10.2