MATH-1460: Trigger exception as soon as maximal count is reached.
This commit is contained in:
parent
d9b0be1c9a
commit
f43069ac6d
|
@ -318,10 +318,11 @@ public class IntegerSequence {
|
||||||
throw new NotStrictlyPositiveException(nTimes);
|
throw new NotStrictlyPositiveException(nTimes);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
count += nTimes * increment;
|
||||||
|
|
||||||
if (!canIncrement(0)) {
|
if (!canIncrement(0)) {
|
||||||
maxCountCallback.trigger(maximalCount);
|
maxCountCallback.trigger(maximalCount);
|
||||||
}
|
}
|
||||||
count += nTimes * increment;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -352,7 +353,7 @@ public class IntegerSequence {
|
||||||
@Override
|
@Override
|
||||||
public Integer next() {
|
public Integer next() {
|
||||||
final int value = count;
|
final int value = count;
|
||||||
increment();
|
count += increment;
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -217,6 +217,33 @@ public class IntegerSequenceTest {
|
||||||
inc.increment(0);
|
inc.increment(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testIncrementTooManyTimes() {
|
||||||
|
final int start = 0;
|
||||||
|
final int max = 3;
|
||||||
|
final int step = 1;
|
||||||
|
|
||||||
|
for (int i = 1; i <= max + 4; i++) {
|
||||||
|
final IntegerSequence.Incrementor inc
|
||||||
|
= IntegerSequence.Incrementor.create()
|
||||||
|
.withStart(start)
|
||||||
|
.withMaximalCount(max)
|
||||||
|
.withIncrement(step);
|
||||||
|
|
||||||
|
Assert.assertTrue(inc.canIncrement(max - 1));
|
||||||
|
Assert.assertFalse(inc.canIncrement(max));
|
||||||
|
|
||||||
|
try {
|
||||||
|
inc.increment(i);
|
||||||
|
} catch (MaxCountExceededException e) {
|
||||||
|
if (i < max) {
|
||||||
|
Assert.fail("i=" + i);
|
||||||
|
}
|
||||||
|
// Otherwise, the exception is expected.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test(expected=ZeroException.class)
|
@Test(expected=ZeroException.class)
|
||||||
public void testIncrementZeroStep() {
|
public void testIncrementZeroStep() {
|
||||||
final int step = 0;
|
final int step = 0;
|
||||||
|
@ -269,14 +296,8 @@ public class IntegerSequenceTest {
|
||||||
.withIncrement(step)
|
.withIncrement(step)
|
||||||
.withCallback(cb);
|
.withCallback(cb);
|
||||||
|
|
||||||
try {
|
Assert.assertTrue(inc.hasNext());
|
||||||
// One call must succeed.
|
Assert.assertEquals(start, inc.next().intValue());
|
||||||
inc.increment();
|
inc.increment(); // Must fail.
|
||||||
} catch (RuntimeException e) {
|
|
||||||
Assert.fail("unexpected exception");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Second call must fail.
|
|
||||||
inc.increment();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue