You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by er...@apache.org on 2018/05/02 18:01:27 UTC
[math] MATH-1460: Trigger exception as soon as maximal count is
reached.
Repository: commons-math
Updated Branches:
refs/heads/master d9b0be1c9 -> f43069ac6
MATH-1460: Trigger exception as soon as maximal count is reached.
Project: http://git-wip-us.apache.org/repos/asf/commons-math/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-math/commit/f43069ac
Tree: http://git-wip-us.apache.org/repos/asf/commons-math/tree/f43069ac
Diff: http://git-wip-us.apache.org/repos/asf/commons-math/diff/f43069ac
Branch: refs/heads/master
Commit: f43069ac6d281b8367dad6f78def4b8336a11ff0
Parents: d9b0be1
Author: Gilles <er...@apache.org>
Authored: Wed May 2 19:56:51 2018 +0200
Committer: Gilles <er...@apache.org>
Committed: Wed May 2 19:56:51 2018 +0200
----------------------------------------------------------------------
.../commons/math4/util/IntegerSequence.java | 5 ++-
.../commons/math4/util/IntegerSequenceTest.java | 39 +++++++++++++++-----
2 files changed, 33 insertions(+), 11 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/commons-math/blob/f43069ac/src/main/java/org/apache/commons/math4/util/IntegerSequence.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/math4/util/IntegerSequence.java b/src/main/java/org/apache/commons/math4/util/IntegerSequence.java
index 4712a4b..56dd745 100644
--- a/src/main/java/org/apache/commons/math4/util/IntegerSequence.java
+++ b/src/main/java/org/apache/commons/math4/util/IntegerSequence.java
@@ -318,10 +318,11 @@ public class IntegerSequence {
throw new NotStrictlyPositiveException(nTimes);
}
+ count += nTimes * increment;
+
if (!canIncrement(0)) {
maxCountCallback.trigger(maximalCount);
}
- count += nTimes * increment;
}
/**
@@ -352,7 +353,7 @@ public class IntegerSequence {
@Override
public Integer next() {
final int value = count;
- increment();
+ count += increment;
return value;
}
http://git-wip-us.apache.org/repos/asf/commons-math/blob/f43069ac/src/test/java/org/apache/commons/math4/util/IntegerSequenceTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/math4/util/IntegerSequenceTest.java b/src/test/java/org/apache/commons/math4/util/IntegerSequenceTest.java
index d850729..6023f77 100644
--- a/src/test/java/org/apache/commons/math4/util/IntegerSequenceTest.java
+++ b/src/test/java/org/apache/commons/math4/util/IntegerSequenceTest.java
@@ -217,6 +217,33 @@ public class IntegerSequenceTest {
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)
public void testIncrementZeroStep() {
final int step = 0;
@@ -269,14 +296,8 @@ public class IntegerSequenceTest {
.withIncrement(step)
.withCallback(cb);
- try {
- // One call must succeed.
- inc.increment();
- } catch (RuntimeException e) {
- Assert.fail("unexpected exception");
- }
-
- // Second call must fail.
- inc.increment();
+ Assert.assertTrue(inc.hasNext());
+ Assert.assertEquals(start, inc.next().intValue());
+ inc.increment(); // Must fail.
}
}