Skip to content

Commit

Permalink
Merge pull request #178 from TransFICC/master
Browse files Browse the repository at this point in the history
AsciiEncoding.parseLongAscii() throw instead of returning zero for "-"
  • Loading branch information
mjpt777 authored Jul 31, 2019
2 parents cd2addf + cf4ce65 commit 76d84a6
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
8 changes: 6 additions & 2 deletions agrona/src/main/java/org/agrona/AsciiEncoding.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 <code>cs</code> is not an int value
* @throws IndexOutOfBoundsException if <code>cs</code> is empty
* @return the parsed value.
*/
public static int parseIntAscii(final CharSequence cs, final int index, final int length)
Expand All @@ -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++;
}
Expand All @@ -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 <code>cs</code> is not a long value
* @throws IndexOutOfBoundsException if <code>cs</code> is empty
* @return the parsed value.
*/
public static long parseLongAscii(final CharSequence cs, final int index, final int length)
Expand All @@ -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++;
}
Expand Down
34 changes: 32 additions & 2 deletions agrona/src/test/java/org/agrona/AsciiEncodingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 76d84a6

Please sign in to comment.