MATH-1088

Ensure that bad usage will raise an exception.


git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1558833 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Gilles Sadowski 2014-01-16 15:26:29 +00:00
parent 2c9438da23
commit 63d88c74b7
2 changed files with 49 additions and 6 deletions

View File

@ -17,6 +17,7 @@
package org.apache.commons.math3.util;
import java.util.NoSuchElementException;
import org.apache.commons.math3.exception.DimensionMismatchException;
import org.apache.commons.math3.exception.NotStrictlyPositiveException;
import org.apache.commons.math3.exception.OutOfRangeException;
@ -77,6 +78,10 @@ public class MultidimensionalCounter implements Iterable<Integer> {
* Unidimensional counter.
*/
private int count = -1;
/**
* Maximum value for {@link #count}.
*/
private final int maxCount = totalSize - 1;
/**
* Create an iterator
@ -90,19 +95,20 @@ public class MultidimensionalCounter implements Iterable<Integer> {
* {@inheritDoc}
*/
public boolean hasNext() {
for (int i = 0; i < dimension; i++) {
if (counter[i] != size[i] - 1) {
return true;
}
}
return false;
return count < maxCount;
}
/**
* @return the unidimensional count after the counter has been
* incremented by {@code 1}.
* @throws NoSuchElementException if {@link #hasNext()} would have
* returned {@code false}.
*/
public Integer next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
for (int i = last; i >= 0; i--) {
if (counter[i] == size[i] - 1) {
counter[i] = 0;

View File

@ -17,6 +17,7 @@
package org.apache.commons.math3.util;
import java.util.NoSuchElementException;
import org.apache.commons.math3.exception.DimensionMismatchException;
import org.apache.commons.math3.exception.OutOfRangeException;
import org.apache.commons.math3.exception.NotStrictlyPositiveException;
@ -24,7 +25,9 @@ import org.junit.Assert;
import org.junit.Test;
/**
* Test cases for the {@link MultidimensionalCounter} class.
*
* @version $Id$
*/
public class MultidimensionalCounterTest {
@Test
@ -100,6 +103,38 @@ public class MultidimensionalCounterTest {
}
}
@Test
public void testIterator() {
final int dim1 = 3;
final int dim2 = 4;
final MultidimensionalCounter.Iterator iter
= new MultidimensionalCounter(dim1, dim2).iterator();
final int max = dim1 * dim2;
for (int i = 0; i < max; i++) {
Assert.assertTrue(iter.hasNext());
// Should not throw.
iter.next();
}
Assert.assertFalse(iter.hasNext());
}
@Test(expected=NoSuchElementException.class)
public void testIteratorNoMoreElements() {
final MultidimensionalCounter.Iterator iter
= new MultidimensionalCounter(4, 2).iterator();
while (iter.hasNext()) {
iter.next();
}
// No more elements: should throw.
iter.next();
}
@Test
public void testMulti2UniConversion() {
final MultidimensionalCounter c = new MultidimensionalCounter(2, 4, 5);
@ -150,6 +185,8 @@ public class MultidimensionalCounterTest {
};
final int totalSize = c.getSize();
Assert.assertEquals(expected.length, totalSize);
final int nDim = c.getDimension();
final MultidimensionalCounter.Iterator iter = c.iterator();
for (int i = 0; i < totalSize; i++) {