From cf4ce6598cb377d2c3692edfb1ed07263e5aef98 Mon Sep 17 00:00:00 2001 From: Judd Gaddie Date: Wed, 31 Jul 2019 11:40:50 +0100 Subject: [PATCH] [Java] Modify AsciiEncoding.parseIntAscii() and AsciiEncoding.parseLongAscii() to behave like java.lang.Integer.parseInt() and java.lang.Long.parseLong() respectively when it comes to handling lone signs. --- .../main/java/org/agrona/AsciiEncoding.java | 8 +++-- .../java/org/agrona/AsciiEncodingTest.java | 34 +++++++++++++++++-- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/agrona/src/main/java/org/agrona/AsciiEncoding.java b/agrona/src/main/java/org/agrona/AsciiEncoding.java index 343b2fdc7..9c35c83e9 100644 --- a/agrona/src/main/java/org/agrona/AsciiEncoding.java +++ b/agrona/src/main/java/org/agrona/AsciiEncoding.java @@ -116,6 +116,8 @@ public static int getDigit(final int index, final char value) * @param cs to parse. * @param index at which the number begins. * @param length of the encoded number in characters. + * @throws AsciiNumberFormatException if cs is not an int value + * @throws IndexOutOfBoundsException if cs is empty * @return the parsed value. */ public static int parseIntAscii(final CharSequence cs, final int index, final int length) @@ -124,7 +126,7 @@ public static int parseIntAscii(final CharSequence cs, final int index, final in final int first = cs.charAt(index); int i = index; - if (first == MINUS_SIGN) + if (first == MINUS_SIGN && length > 1) { i++; } @@ -149,6 +151,8 @@ public static int parseIntAscii(final CharSequence cs, final int index, final in * @param cs to parse. * @param index at which the number begins. * @param length of the encoded number in characters. + * @throws AsciiNumberFormatException if cs is not a long value + * @throws IndexOutOfBoundsException if cs is empty * @return the parsed value. */ public static long parseLongAscii(final CharSequence cs, final int index, final int length) @@ -157,7 +161,7 @@ public static long parseLongAscii(final CharSequence cs, final int index, final final int first = cs.charAt(index); int i = index; - if (first == MINUS_SIGN) + if (first == MINUS_SIGN && length > 1) { i++; } diff --git a/agrona/src/test/java/org/agrona/AsciiEncodingTest.java b/agrona/src/test/java/org/agrona/AsciiEncodingTest.java index 69fe0baf6..e5dc00efe 100644 --- a/agrona/src/test/java/org/agrona/AsciiEncodingTest.java +++ b/agrona/src/test/java/org/agrona/AsciiEncodingTest.java @@ -43,14 +43,44 @@ public void shouldParseLong() } @Test(expected = AsciiNumberFormatException.class) - public void shouldThrowExceptionWhenDecodingCharNonAsciiValue() + public void shouldThrowExceptionWhenDecodingCharNonNumericValue() { AsciiEncoding.getDigit(0, 'a'); } @Test(expected = AsciiNumberFormatException.class) - public void shouldThrowExceptionWhenDecodingByteNonAsciiValue() + public void shouldThrowExceptionWhenDecodingByteNonNumericValue() { AsciiEncoding.getDigit(0, (byte)'a'); } + + @Test(expected = AsciiNumberFormatException.class) + public void shouldThrowExceptionWhenParsingLongContainingLoneMinusSign() + { + AsciiEncoding.parseLongAscii("-", 0, 1); + } + + @Test(expected = AsciiNumberFormatException.class) + public void shouldThrowExceptionWhenParsingIntegerContainingLoneMinusSign() + { + AsciiEncoding.parseIntAscii("-", 0, 1); + } + + @Test(expected = AsciiNumberFormatException.class) + public void shouldThrowExceptionWhenParsingLongContainingLonePlusSign() + { + AsciiEncoding.parseLongAscii("+", 0, 1); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void shouldThrowExceptionWhenParsingEmptyLong() + { + AsciiEncoding.parseLongAscii("", 0, 0); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void shouldThrowExceptionWhenParsingEmptyInteger() + { + AsciiEncoding.parseIntAscii("", 0, 0); + } } \ No newline at end of file