Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Partition.java does not check the validity of input iterators #236

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/main/java/net/imglib2/util/Partition.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import java.util.Comparator;
import java.util.List;
import java.util.ListIterator;
import java.util.NoSuchElementException;

/**
* TODO
Expand Down Expand Up @@ -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();
Expand Down
18 changes: 18 additions & 0 deletions src/test/java/net/imglib2/util/PartitionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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()
{
Expand Down