From 8ce9ae9e2669b459903440060cb64a42889d921d Mon Sep 17 00:00:00 2001 From: emopers Date: Tue, 15 Jan 2019 08:20:32 -0600 Subject: [PATCH] Fixing hasNext violation in a public class. Fixing hasNext violation in a public class. --- src/main/java/net/imglib2/util/Partition.java | 4 ++++ .../java/net/imglib2/util/PartitionTest.java | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/main/java/net/imglib2/util/Partition.java b/src/main/java/net/imglib2/util/Partition.java index 847c5191b3..2c0639b8c1 100644 --- a/src/main/java/net/imglib2/util/Partition.java +++ b/src/main/java/net/imglib2/util/Partition.java @@ -37,6 +37,7 @@ import java.util.Comparator; import java.util.List; import java.util.ListIterator; +import java.util.NoSuchElementException; /** * TODO @@ -1115,6 +1116,9 @@ else if ( j.previousIndex() == i.nextIndex() - 1 ) */ public static < T extends Comparable< T > > void partitionSubList( final ListIterator< T > i, final ListIterator< T > j, final int[] permutation ) { + if (!i.hasNext() || !j.hasPrevious()) { + throw new NoSuchElementException("Invalid iterators for partitioning a sublist."); + } final int pivotIndex = j.previousIndex(); final int permutationPivot = permutation[ pivotIndex ]; final T pivot = j.previous(); diff --git a/src/test/java/net/imglib2/util/PartitionTest.java b/src/test/java/net/imglib2/util/PartitionTest.java index bce83b4c46..ad1983d536 100644 --- a/src/test/java/net/imglib2/util/PartitionTest.java +++ b/src/test/java/net/imglib2/util/PartitionTest.java @@ -36,12 +36,15 @@ import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.ListIterator; +import java.util.NoSuchElementException; import org.junit.Test; @@ -405,6 +408,21 @@ public void testPartitionFloatObjectComparatorPermutation() assertTrue( values.get( k ).equals( origvalues.get( permutation[ k ] ) ) ); } + @Test + public void testPartitionEmptyFloatObjectIteratorPermutation() + { + final ArrayList< Float > values = new ArrayList< Float >(); + final ListIterator< Float > iIterator = values.listIterator(); + final ListIterator< Float > jIterator = values.listIterator( values.size() ); + final int[] permutation = new int[ values.size() ]; + try { + Partition.partitionSubList( iIterator, jIterator, permutation ); + fail( "Expected a NoSuchElementException to be thrown" ); + } catch ( NoSuchElementException noSuchElementException ) { + assertEquals( noSuchElementException.getMessage(), "Invalid iterators for partitioning a sublist." ); + } + } + @Test public void testPartitionFloatObjectIteratorPermutation() {