From 18565b7cf82c50da2d6266f885fa6ebef929db79 Mon Sep 17 00:00:00 2001 From: kiramt Date: Thu, 18 May 2017 14:09:21 +0100 Subject: [PATCH] JAL-2256 Corrected AllCols/Rows iterator behaviour for single element --- src/jalview/datamodel/AllColsIterator.java | 4 ++-- src/jalview/datamodel/AllRowsIterator.java | 4 ++-- test/jalview/datamodel/AllColsIteratorTest.java | 17 +++++++++++++++++ test/jalview/datamodel/AllRowsIteratorTest.java | 19 ++++++++++++++++++- 4 files changed, 39 insertions(+), 5 deletions(-) diff --git a/src/jalview/datamodel/AllColsIterator.java b/src/jalview/datamodel/AllColsIterator.java index c7a0bb1..c1296d5 100644 --- a/src/jalview/datamodel/AllColsIterator.java +++ b/src/jalview/datamodel/AllColsIterator.java @@ -48,13 +48,13 @@ public class AllColsIterator implements Iterator @Override public boolean hasNext() { - return current + 1 <= last; + return next <= last; } @Override public Integer next() { - if (current + 1 > last) + if (next > last) { throw new NoSuchElementException(); } diff --git a/src/jalview/datamodel/AllRowsIterator.java b/src/jalview/datamodel/AllRowsIterator.java index aefed60..b6d45f8 100644 --- a/src/jalview/datamodel/AllRowsIterator.java +++ b/src/jalview/datamodel/AllRowsIterator.java @@ -51,13 +51,13 @@ public class AllRowsIterator implements Iterator @Override public boolean hasNext() { - return current + 1 <= last; + return next <= last; } @Override public Integer next() { - if (current + 1 > last) + if (next > last) { throw new NoSuchElementException(); } diff --git a/test/jalview/datamodel/AllColsIteratorTest.java b/test/jalview/datamodel/AllColsIteratorTest.java index fbb20be..3942f0b 100644 --- a/test/jalview/datamodel/AllColsIteratorTest.java +++ b/test/jalview/datamodel/AllColsIteratorTest.java @@ -82,4 +82,21 @@ public class AllColsIteratorTest AllColsIterator it = new AllColsIterator(0, 3, hiddenCols); it.remove(); } + + /* + * Test iterator behaves correctly when there is only one element in the collection + */ + @Test(groups = { "Functional" }) + public void testOneElement() + { + HiddenColumns hidden = new HiddenColumns(); + AllColsIterator it = new AllColsIterator(0, 0, hidden); + int count = 0; + while (it.hasNext()) + { + it.next(); + count++; + } + assertTrue(count == 1, "hasNext() is false after 1 iteration"); + } } diff --git a/test/jalview/datamodel/AllRowsIteratorTest.java b/test/jalview/datamodel/AllRowsIteratorTest.java index fd1d29d..aeff71d 100644 --- a/test/jalview/datamodel/AllRowsIteratorTest.java +++ b/test/jalview/datamodel/AllRowsIteratorTest.java @@ -34,7 +34,7 @@ public class AllRowsIteratorTest { AlignmentI al; - Hashtable hiddenRepSequences = new Hashtable(); + Hashtable hiddenRepSequences = new Hashtable<>(); @BeforeClass public void setup() @@ -110,4 +110,21 @@ public class AllRowsIteratorTest hiddenRepSequences.put(allseqs[start], theseSeqs); } + + /* + * Test iterator behaves correctly when there is only one element in the collection + */ + @Test(groups = { "Functional" }) + public void testOneElement() + { + AllRowsIterator it = new AllRowsIterator(0, 0, al); + int count = 0; + while (it.hasNext()) + { + it.next(); + count++; + } + assertTrue(count == 1, "hasNext() is false after 1 iteration"); + } + } -- 1.7.10.2