Skip to content

Commit

Permalink
assertAllElementsOfIterator must also check if next() throws NoSuchEl…
Browse files Browse the repository at this point in the history
…ementException when needed + fix ValueRanges accordingly
  • Loading branch information
ge0ffrey committed Apr 11, 2016
1 parent 198d4bb commit 1b3320a
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ public boolean hasNext() {

@Override
public BigDecimal next() {
if (size <= 0L) {
throw new NoSuchElementException();
}
long index = RandomUtils.nextLong(workingRandom, size);
return incrementUnit.multiply(BigDecimal.valueOf(index)).add(from);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ public boolean hasNext() {

@Override
public BigInteger next() {
if (size <= 0L) {
throw new NoSuchElementException();
}
long index = RandomUtils.nextLong(workingRandom, size);
return incrementUnit.multiply(BigInteger.valueOf(index)).add(from);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.optaplanner.core.impl.domain.valuerange.buildin.primdouble;

import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Random;

import org.optaplanner.core.impl.domain.valuerange.AbstractUncountableValueRange;
Expand Down Expand Up @@ -76,6 +77,9 @@ public boolean hasNext() {

@Override
public Double next() {
if (to == from) {
throw new NoSuchElementException();
}
double diff = to - from;
double next = from + diff * workingRandom.nextDouble();
if (next >= to) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ public boolean hasNext() {

@Override
public Integer next() {
if (size <= 0L) {
throw new NoSuchElementException();
}
long index = RandomUtils.nextLong(workingRandom, size);
return (int) (index * incrementUnit + from);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ public boolean hasNext() {

@Override
public Long next() {
if (size <= 0L) {
throw new NoSuchElementException();
}
long index = RandomUtils.nextLong(workingRandom, size);
return index * incrementUnit + from;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public boolean hasNext() {

@Override
public Temporal next() {
if (upcoming.until(to, incrementUnitType) < 0) {
if (upcoming.until(to, incrementUnitType) <= 0) {
throw new NoSuchElementException();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Random;

import org.optaplanner.core.impl.heuristic.move.Move;
Expand All @@ -30,21 +31,24 @@ public class CachedListRandomIterator<S> extends SelectionIterator<S> {

protected final List<S> cachedList;
protected final Random workingRandom;
protected final boolean notEmpty;
protected final boolean empty;

public CachedListRandomIterator(List<S> cachedList, Random workingRandom) {
this.cachedList = cachedList;
this.workingRandom = workingRandom;
notEmpty = !cachedList.isEmpty();
empty = cachedList.isEmpty();
}

@Override
public boolean hasNext() {
return notEmpty;
return !empty;
}

@Override
public S next() {
if (empty) {
throw new NoSuchElementException();
}
int index = workingRandom.nextInt(cachedList.size());
return cachedList.get(index);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.function.Function;

import org.junit.Assert;
import org.junit.ComparisonFailure;
Expand Down Expand Up @@ -153,6 +155,13 @@ public static <O> void assertElementsOfIterator(Iterator<O> iterator, O... eleme
public static <O> void assertAllElementsOfIterator(Iterator<O> iterator, O... elements) {
assertElementsOfIterator(iterator, elements);
assertFalse(iterator.hasNext());
try {
iterator.next();
fail("The iterator with hasNext() (" + false + ") is expected to throw a "
+ NoSuchElementException.class.getSimpleName() + " when calling next().");
} catch (NoSuchElementException e) {
// Do nothing
}
}

// ************************************************************************
Expand Down

0 comments on commit 1b3320a

Please sign in to comment.