From 1b548e8307dd533f127252a05c845313a2bf9dd0 Mon Sep 17 00:00:00 2001 From: kiramt Date: Thu, 18 Jan 2018 09:07:41 +0000 Subject: [PATCH] JAL-2759 unit tests (incomplete) --- src/jalview/datamodel/HiddenColumns.java | 28 ++++++----- test/jalview/datamodel/HiddenColumnsTest.java | 64 ++++++++++++++----------- 2 files changed, 52 insertions(+), 40 deletions(-) diff --git a/src/jalview/datamodel/HiddenColumns.java b/src/jalview/datamodel/HiddenColumns.java index 037afc4..d7628b7 100644 --- a/src/jalview/datamodel/HiddenColumns.java +++ b/src/jalview/datamodel/HiddenColumns.java @@ -444,15 +444,10 @@ public class HiddenColumns // take the set of hidden columns, and the set of gaps in origseq, // and remove all the hidden gaps from hiddenColumns - // first get the gaps as a Bitset + // first get the non-gaps as a Bitset + // then calculate hidden ^ not(gap) BitSet gaps = origseq.gapBitset(); - - // now calculate hidden ^ not(gap) - BitSet hidden = new BitSet(); - markHiddenRegions(hidden); - hidden.andNot(gaps); - hiddenColumns.clear(); - this.hideColumns(hidden); + this.andNot(gaps); // for each sequence in the alignment, except the profile sequence, // insert gaps corresponding to each hidden region but where each hidden @@ -1159,9 +1154,12 @@ public class HiddenColumns clearHiddenColumnsInRange(start, end); // make sure only bits between start and end are set - tohide.clear(0, start - 1); - tohide.clear(Math.min(end + 1, tohide.length() - 1), - tohide.length() - 1); + if (!tohide.isEmpty()) + { + tohide.clear(0, start - 1); + tohide.clear(Math.min(end + 1, tohide.length() - 1), + tohide.length() - 1); + } hideColumns(tohide); } @@ -1227,16 +1225,20 @@ public class HiddenColumns * @param inserts * BitSet where hidden columns will be marked */ - public void markHiddenRegions(BitSet inserts) + private void andNot(BitSet updates) { try { LOCK.readLock().lock(); + BitSet hiddenBitSet = new BitSet(); for (int[] range : hiddenColumns) { - inserts.set(range[0], range[1] + 1); + hiddenBitSet.set(range[0], range[1] + 1); } + hiddenBitSet.andNot(updates); + hiddenColumns.clear(); + hideColumns(hiddenBitSet); } finally { LOCK.readLock().unlock(); diff --git a/test/jalview/datamodel/HiddenColumnsTest.java b/test/jalview/datamodel/HiddenColumnsTest.java index dbde943..462957b 100644 --- a/test/jalview/datamodel/HiddenColumnsTest.java +++ b/test/jalview/datamodel/HiddenColumnsTest.java @@ -31,7 +31,6 @@ import jalview.util.Comparison; import java.util.Arrays; import java.util.BitSet; import java.util.Iterator; -import java.util.Random; import org.testng.annotations.Test; @@ -726,32 +725,43 @@ public class HiddenColumnsTest } @Test(groups = { "Functional" }) - public void testMarkHiddenRegions() + public void hideColumns_BitSetAndRange() { - BitSet toMark, fromMark; - long seed = -3241532; - Random number = new Random(seed); - for (int n = 0; n < 1000; n++) - { - // create a random bitfield - toMark = BitSet - .valueOf(new long[] - { 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.hideColumns(toMark); - - // see if we can recover bitfield - hc.markHiddenRegions(fromMark = new BitSet()); - assertEquals(toMark, fromMark); - } + HiddenColumns hc = new HiddenColumns(); + hc.hideColumns(3, 5); + hc.hideColumns(15, 20); + hc.hideColumns(45, 60); + + BitSet tohide = new BitSet(); + + // all unhidden if tohide is empty and range covers hidden + hc.hideColumns(tohide, 1, 70); + assertTrue(!hc.hasHiddenColumns()); + + hc.hideColumns(3, 5); + hc.hideColumns(15, 20); + hc.hideColumns(45, 60); + + // but not if range does not cover hidden + hc.hideColumns(tohide, 23, 40); + assertTrue(hc.hasHiddenColumns()); + + // and partial unhide if range partially covers + hc.hideColumns(tohide, 1, 17); + Iterator it = hc.iterator(); + assertTrue(it.hasNext()); + int[] region = it.next(); + + assertEquals(18, region[0]); + assertEquals(20, region[1]); + + assertTrue(it.hasNext()); + region = it.next(); + + assertEquals(45, region[0]); + assertEquals(60, region[1]); + + assertFalse(it.hasNext()); } @Test(groups = { "Functional" }) @@ -1150,7 +1160,7 @@ public class HiddenColumnsTest } @Test(groups = "Functional") - public void testHideMarkedBits() + public void testHideColumns_BitSet() { HiddenColumns h = new HiddenColumns(); HiddenColumns h2 = new HiddenColumns(); -- 1.7.10.2