Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ public StringBuffer format(RealMatrix matrix, StringBuffer toAppendTo,
public RealMatrix parse(String source) {
final ParsePosition parsePosition = new ParsePosition(0);
final RealMatrix result = parse(source, parsePosition);
if (parsePosition.getIndex() == 0) {
if (result == null) {
throw new MathParseException(source,
parsePosition.getErrorIndex(),
Array2DRowRealMatrix.class);
Expand Down Expand Up @@ -348,7 +348,8 @@ public RealMatrix parse(String source, ParsePosition pos) {
if (component != null) {
rowComponents.add(component);
} else {
if (rowComponents.isEmpty()) {
if (rowComponents.isEmpty() &&
(matrix.isEmpty() || trimmedRowPrefix.isEmpty())) {
loop = false;
} else {
// invalid component
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ public StringBuffer format(RealVector vector, StringBuffer toAppendTo,
public ArrayRealVector parse(String source) {
final ParsePosition parsePosition = new ParsePosition(0);
final ArrayRealVector result = parse(source, parsePosition);
if (parsePosition.getIndex() == 0) {
if (result == null) {
throw new MathParseException(source,
parsePosition.getErrorIndex(),
ArrayRealVector.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -375,4 +375,31 @@ public void testParseEmptySuffixAtEndOfInput() {
Assert.assertEquals(MatrixUtils.createRealMatrix(new double[][] {{1, 2}, {3, 4}}), parsed);
Assert.assertEquals(10, pos.getIndex());
}

@Test
public void testParseEmptyRow() {
for (final String source : new String[] {"{{1},{}}", "{{1},{}", "{{1, 2}, {}}", "{{1}, {2}, { }}"}) {
final ParsePosition pos = new ParsePosition(0);
Assert.assertNull("Should not parse <" + source + ">", realMatrixFormat.parse(source, pos));
Assert.assertEquals(0, pos.getIndex());
}
}

@Test
public void testParseEmptySuffixClosingRowSeparator() {
final RealMatrix parsed = new RealMatrixFormat("", "", "", "", "; ", ", ").parse("1, 2; ");
Assert.assertEquals(MatrixUtils.createRealMatrix(new double[][] {{1, 2}}), parsed);
}

@Test
public void testParseFailureThrows() {
for (final String source : new String[] {"{}", "{{1},}", "{{1},{2}", "{{1}{2}}", "{{1, 1 1}}", "{{1}"}) {
try {
realMatrixFormat.parse(source);
Assert.fail("Expecting MathParseException for <" + source + ">");
} catch (MathParseException pe) {
// expected behavior
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -351,4 +351,17 @@ public void testForgottenSuffix() {
Assert.assertNull("Should not parse <"+source+">",new RealVectorFormat().parse(source, pos));
Assert.assertEquals(8, pos.getErrorIndex());
}

@Test
public void testParseFailureThrows() {
final RealVectorFormat format = new RealVectorFormat();
for (final String source : new String[] {"{1", "{1; 2", "{1 2}", "{1; 1 1}"}) {
try {
format.parse(source);
Assert.fail("Expecting MathParseException for <" + source + ">");
} catch (MathParseException pe) {
// expected behavior
}
}
}
}