From 368f17d194d3d03c73cc459f1af6fcc1a1b7d598 Mon Sep 17 00:00:00 2001 From: Gilles Sadowski Date: Fri, 1 Apr 2011 10:10:18 +0000 Subject: [PATCH] MATH-552 Fixed bug in "MultidimensionalCounter". Thanks to James Bence. git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1087637 13f79535-47bb-0310-9956-ffa450edef68 --- .../math/util/MultidimensionalCounter.java | 8 +------- src/site/xdoc/changes.xml | 3 +++ .../math/util/MultidimensionalCounterTest.java | 16 ++++++++++++++-- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/apache/commons/math/util/MultidimensionalCounter.java b/src/main/java/org/apache/commons/math/util/MultidimensionalCounter.java index 56c9ffebc..fb0614eb7 100644 --- a/src/main/java/org/apache/commons/math/util/MultidimensionalCounter.java +++ b/src/main/java/org/apache/commons/math/util/MultidimensionalCounter.java @@ -234,13 +234,7 @@ public class MultidimensionalCounter implements Iterable { indices[i] = idx; } - int idx = 1; - while (count < index) { - count += idx; - ++idx; - } - --idx; - indices[last] = idx; + indices[last] = index - count; return indices; } diff --git a/src/site/xdoc/changes.xml b/src/site/xdoc/changes.xml index 5c0e1d92d..72035c1a9 100644 --- a/src/site/xdoc/changes.xml +++ b/src/site/xdoc/changes.xml @@ -52,6 +52,9 @@ The type attribute can be add,update,fix,remove. If the output is not quite correct, check for invisible trailing spaces! --> + + Fixed bug in "MultidimensionalCounter". + All unit tests have been converted to Junit 4. They need at least Junit 4.5 to run (the ant and maven build systems are currently set to use Junit 4.8.2) diff --git a/src/test/java/org/apache/commons/math/util/MultidimensionalCounterTest.java b/src/test/java/org/apache/commons/math/util/MultidimensionalCounterTest.java index 827030df3..a3110b99a 100644 --- a/src/test/java/org/apache/commons/math/util/MultidimensionalCounterTest.java +++ b/src/test/java/org/apache/commons/math/util/MultidimensionalCounterTest.java @@ -121,20 +121,32 @@ public class MultidimensionalCounterTest { @Test public void testIterationConsistency() { - final MultidimensionalCounter c = new MultidimensionalCounter(2, 3, 2); + final MultidimensionalCounter c = new MultidimensionalCounter(2, 3, 4); final int[][] expected = new int[][] { { 0, 0, 0 }, { 0, 0, 1 }, + { 0, 0, 2 }, + { 0, 0, 3 }, { 0, 1, 0 }, { 0, 1, 1 }, + { 0, 1, 2 }, + { 0, 1, 3 }, { 0, 2, 0 }, { 0, 2, 1 }, + { 0, 2, 2 }, + { 0, 2, 3 }, { 1, 0, 0 }, { 1, 0, 1 }, + { 1, 0, 2 }, + { 1, 0, 3 }, { 1, 1, 0 }, { 1, 1, 1 }, + { 1, 1, 2 }, + { 1, 1, 3 }, { 1, 2, 0 }, - { 1, 2, 1 } + { 1, 2, 1 }, + { 1, 2, 2 }, + { 1, 2, 3 } }; final int totalSize = c.getSize();