Skip to content
Merged
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 @@ -117,7 +117,7 @@ private static Number parseNumber(final String source, final double value,
final int n = sb.length();
final int startIndex = pos.getIndex();
final int endIndex = startIndex + n;
if (endIndex < source.length() &&
if (endIndex <= source.length() &&
source.substring(startIndex, endIndex).compareTo(sb.toString()) == 0) {
ret = Double.valueOf(value);
pos.setIndex(endIndex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,35 @@ public void testPaseNegativeInfinity() {
Assert.assertEquals(expected, actual);
}

@Test
public void testParseRealNan() {
// a real-only special value runs to the last character of the string
Complex expected = Complex.ofCartesian(Double.NaN, 0);
Assert.assertEquals(expected, complexFormat.parse("(NaN)"));
Assert.assertEquals(expected, complexFormat.parse(complexFormat.format(expected)));
}

@Test
public void testParseRealInfinity() {
Complex positive = Complex.ofCartesian(Double.POSITIVE_INFINITY, 0);
Assert.assertEquals(positive, complexFormat.parse("(Infinity)"));
Assert.assertEquals(positive, complexFormat.parse(complexFormat.format(positive)));
Complex negative = Complex.ofCartesian(Double.NEGATIVE_INFINITY, 0);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should test the round-trip:

Assert.assertEquals(positive, complexFormat.parse(complexFormat.format(positive)));
Assert.assertEquals(negative, complexFormat.parse(complexFormat.format(negative)));

Parsing should also test special values in the imaginary part:

Complex.ofCartesian(1.23, Double.NaN);
Complex.ofCartesian(1.23, Double.POSITIVE_INFINITY);
Complex.ofCartesian(1.23, Double.NEGATIVE_INFINITY);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. The infinity test now checks the round trip for both signs, and I added testParseImaginarySpecialValue covering NaN, +Infinity and -Infinity in the imaginary part (parsed back from format output). All pass with the patch.

Assert.assertEquals(negative, complexFormat.parse("(-Infinity)"));
Assert.assertEquals(negative, complexFormat.parse(complexFormat.format(negative)));
}

@Test
public void testParseImaginarySpecialValue() {
// the special value sits in the imaginary part, ahead of the imaginary character
Complex nan = Complex.ofCartesian(1.23, Double.NaN);
Assert.assertEquals(nan, complexFormat.parse(complexFormat.format(nan)));
Complex positive = Complex.ofCartesian(1.23, Double.POSITIVE_INFINITY);
Assert.assertEquals(positive, complexFormat.parse(complexFormat.format(positive)));
Complex negative = Complex.ofCartesian(1.23, Double.NEGATIVE_INFINITY);
Assert.assertEquals(negative, complexFormat.parse(complexFormat.format(negative)));
}

@Test
public void testConstructorSingleFormat() {
NumberFormat nf = NumberFormat.getInstance();
Expand Down
Loading