diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 20379fa8d6..0876768af1 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -54,7 +54,7 @@ If the output is not quite correct, check for invisible trailing spaces! - + Add context to "OutOfRangeException". diff --git a/src/test/java/org/apache/commons/math4/linear/MatrixUtilsTest.java b/src/test/java/org/apache/commons/math4/linear/MatrixUtilsTest.java index 9940c1c6a6..e7486f29bd 100644 --- a/src/test/java/org/apache/commons/math4/linear/MatrixUtilsTest.java +++ b/src/test/java/org/apache/commons/math4/linear/MatrixUtilsTest.java @@ -22,6 +22,7 @@ import org.apache.commons.math4.exception.MathIllegalArgumentException; import org.apache.commons.math4.exception.NotStrictlyPositiveException; import org.apache.commons.math4.exception.NullArgumentException; +import org.apache.commons.math4.exception.OutOfRangeException; import org.apache.commons.math4.dfp.Dfp; import org.junit.Assert; import org.junit.Test; @@ -445,4 +446,43 @@ public void testInverseRealMatrix() { MatrixUtils.createRealIdentityMatrix(testData.length), result, 1e-12); } + @Test + public void testCheckMatrixRowIndexError() { + try { + AnyMatrix m = MatrixUtils.createRealMatrix(new double[][] {{9,9}, {9,9}, {9,9}}); + MatrixUtils.checkRowIndex(m, 4); + Assert.fail("expected an OutOfRangeException"); + } catch (OutOfRangeException e) { + String s = e.getMessage(); + int topIx = s.indexOf('2'); + int botIx = s.indexOf('0'); + int rowIx = s.indexOf('4'); + if (topIx < 0 || botIx < 0 || rowIx < 0) { + Assert.fail("expected a message like index 4 is not in 0..3, not: " + s); + } + } catch (Exception e) { + Assert.fail("expected an OutOfRange exception, not: " + + e.getClass().getName() + ": " + e.getMessage()); + } + } + + @Test + public void testCheckMatrixColIndexError() { + try { + AnyMatrix m = MatrixUtils.createRealMatrix(new double[][] {{9,9}, {9,9}, {9,9}}); + MatrixUtils.checkColumnIndex(m, 4); + Assert.fail("expected an OutOfRangeException"); + } catch (OutOfRangeException e) { + String s = e.getMessage(); + int topIx = s.indexOf('1'); + int botIx = s.indexOf('0'); + int rowIx = s.indexOf('4'); + if (topIx < 0 || botIx < 0 || rowIx < 0) { + Assert.fail("expected a message like index 4 is not in 0..3, not: " + s); + } + } catch (Exception e) { + Assert.fail("expected an OutOfRange exception, not: " + + e.getClass().getName() + ": " + e.getMessage()); + } + } }