Skip to content

Commit

Permalink
MATH-1565: Add unit tests.
Browse files Browse the repository at this point in the history
Thanks to Randy Strauss.
  • Loading branch information
Gilles Sadowski committed Dec 11, 2020
1 parent 323f6d7 commit a2c3868
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/changes/changes.xml
Expand Up @@ -54,7 +54,7 @@ If the output is not quite correct, check for invisible trailing spaces!
</release>

<release version="4.0" date="XXXX-XX-XX" description="">
<action dev="erans" type="fix" issue="MATH-1565">
<action dev="erans" type="fix" issue="MATH-1565" due-to="Randy Strauss">
Add context to "OutOfRangeException".
</action>
<action dev="erans" type="update" issue="MATH-1562" due-to="Frank Ulbricht">
Expand Down
40 changes: 40 additions & 0 deletions src/test/java/org/apache/commons/math4/linear/MatrixUtilsTest.java
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
}
}

0 comments on commit a2c3868

Please sign in to comment.