Skip to content

Commit

Permalink
[LANG-1729] NumberUtils.isParsable() returns true for Fullwidth Unicode
Browse files Browse the repository at this point in the history
digits

Add Byte test case
  • Loading branch information
garydgregory committed Apr 24, 2024
1 parent aacc467 commit e4aba3f
Showing 1 changed file with 39 additions and 15 deletions.
54 changes: 39 additions & 15 deletions src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java
Expand Up @@ -97,7 +97,23 @@ public void compareShort() {
assertTrue(NumberUtils.compare((short) 213, (short) 32) > 0);
}

private boolean isIntegerParsable(final String s) {
private boolean isParsableByte(final String s) {
final NumberFormat instance = NumberFormat.getInstance();
instance.setParseIntegerOnly(false);
try {
instance.parse(s);
} catch (final ParseException e) {
return false;
}
try {
Byte.parseByte(s);
} catch (final NumberFormatException e) {
return false;
}
return NumberUtils.isParsable(s);
}

private boolean isParsableInteger(final String s) {
final NumberFormat instance = NumberFormat.getInstance();
instance.setParseIntegerOnly(false);
try {
Expand All @@ -113,7 +129,7 @@ private boolean isIntegerParsable(final String s) {
return NumberUtils.isParsable(s);
}

private boolean isLongParsable(final String s) {
private boolean isParsableLong(final String s) {
final NumberFormat instance = NumberFormat.getInstance();
instance.setParseIntegerOnly(false);
try {
Expand All @@ -129,7 +145,7 @@ private boolean isLongParsable(final String s) {
return NumberUtils.isParsable(s);
}

private boolean isShortParsable(final String s) {
private boolean isParsableShort(final String s) {
final NumberFormat instance = NumberFormat.getInstance();
instance.setParseIntegerOnly(false);
try {
Expand Down Expand Up @@ -1007,28 +1023,36 @@ public void testLANG1252() {
compareIsCreatableWithCreateNumber("+2.0", true);
}

@Test
public void testLang1729IsParsableByte() {
assertTrue(isParsableByte("1"));
assertFalse(isParsableByte("1 2 3"));
assertTrue(isParsableByte("123"));
assertFalse(isParsableByte("1 2 3"));
}

@Test
public void testLang1729IsParsableInteger() {
assertTrue(isIntegerParsable("1"));
assertFalse(isIntegerParsable("1 2 3"));
assertTrue(isIntegerParsable("123"));
assertFalse(isIntegerParsable("1 2 3"));
assertTrue(isParsableInteger("1"));
assertFalse(isParsableInteger("1 2 3"));
assertTrue(isParsableInteger("123"));
assertFalse(isParsableInteger("1 2 3"));
}

@Test
public void testLang1729IsParsableLong() {
assertTrue(isLongParsable("1"));
assertFalse(isLongParsable("1 2 3"));
assertTrue(isLongParsable("123"));
assertFalse(isLongParsable("1 2 3"));
assertTrue(isParsableLong("1"));
assertFalse(isParsableLong("1 2 3"));
assertTrue(isParsableLong("123"));
assertFalse(isParsableLong("1 2 3"));
}

@Test
public void testLang1729IsParsableShort() {
assertTrue(isShortParsable("1"));
assertFalse(isShortParsable("1 2 3"));
assertTrue(isShortParsable("123"));
assertFalse(isShortParsable("1 2 3"));
assertTrue(isParsableShort("1"));
assertFalse(isParsableShort("1 2 3"));
assertTrue(isParsableShort("123"));
assertFalse(isParsableShort("1 2 3"));
}

@Test
Expand Down

0 comments on commit e4aba3f

Please sign in to comment.