This commit is contained in:
Looly
2024-08-03 19:59:52 +08:00
parent ec204f8fc2
commit 5a4fbd4386
5 changed files with 147 additions and 28 deletions

View File

@@ -15,10 +15,51 @@ package org.dromara.hutool.core.math;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
public class NumberParserTest {
@Test
void parseLongTest() {
final long value = NumberParser.INSTANCE.parseLong("0.a");
Assertions.assertEquals(0L, value);
}
@Test
void testParseIntWithValidInputs() {
// Test with various valid inputs
assertEquals(123, NumberParser.INSTANCE.parseInt("123".toCharArray(), 10));
assertEquals(-123, NumberParser.INSTANCE.parseInt("-123".toCharArray(), 10));
assertEquals(123, NumberParser.INSTANCE.parseInt("+123".toCharArray(), 10));
assertEquals(0, NumberParser.INSTANCE.parseInt("0".toCharArray(), 10));
assertEquals(255, NumberParser.INSTANCE.parseInt("ff".toCharArray(), 16));
assertEquals(-255, NumberParser.INSTANCE.parseInt("-ff".toCharArray(), 16));
}
@Test
void testParseIntWithInvalidInputs() {
// Test with various invalid inputs
assertThrows(IllegalArgumentException.class, () -> NumberParser.INSTANCE.parseInt("".toCharArray(), 10));
assertThrows(NumberFormatException.class, () -> NumberParser.INSTANCE.parseInt("1234".toCharArray(), 2));
assertThrows(NumberFormatException.class, () -> NumberParser.INSTANCE.parseInt("abc".toCharArray(), 10));
assertThrows(NumberFormatException.class, () -> NumberParser.INSTANCE.parseInt("--123".toCharArray(), 10));
assertThrows(NumberFormatException.class, () -> NumberParser.INSTANCE.parseInt("++123".toCharArray(), 10));
assertThrows(NumberFormatException.class, () -> NumberParser.INSTANCE.parseInt("123".toCharArray(), 1));
}
@Test
void testParseIntWithLeadingAndTrailingWhitespace() {
// Test with leading and trailing whitespace
assertEquals(42, NumberParser.INSTANCE.parseInt(" 42 ".toCharArray(), 10));
}
@Test
void testParseIntWithMaxAndMinInt() {
// Test with values at the edge of int range
final char[] maxIntStr = Integer.toString(Integer.MAX_VALUE).toCharArray();
final char[] minIntStr = Integer.toString(Integer.MIN_VALUE).toCharArray();
assertEquals(Integer.MAX_VALUE, NumberParser.INSTANCE.parseInt(maxIntStr, 10));
assertEquals(Integer.MIN_VALUE, NumberParser.INSTANCE.parseInt(minIntStr, 10));
}
}

View File

@@ -26,6 +26,7 @@ import java.text.NumberFormat;
import java.text.ParseException;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
/**
* {@link NumberUtil} 单元测试类
@@ -320,7 +321,7 @@ public class NumberUtilTest {
@Test
public void decimalFormatNaNTest() {
Assertions.assertThrows(IllegalArgumentException.class, ()->{
assertThrows(IllegalArgumentException.class, ()->{
final Double a = 0D;
final Double b = 0D;
@@ -331,7 +332,7 @@ public class NumberUtilTest {
@Test
public void decimalFormatNaNTest2() {
Assertions.assertThrows(IllegalArgumentException.class, ()->{
assertThrows(IllegalArgumentException.class, ()->{
final Double a = 0D;
final Double b = 0D;
@@ -383,7 +384,7 @@ public class NumberUtilTest {
@Test
void emptyToBigDecimalTest(){
Assertions.assertThrows(IllegalArgumentException.class,()->{
assertThrows(IllegalArgumentException.class,()->{
NumberUtil.toBigDecimal("");
});
}
@@ -445,7 +446,7 @@ public class NumberUtilTest {
@Test
public void parseIntTest3() {
Assertions.assertThrows(NumberFormatException.class, ()->{
assertThrows(NumberFormatException.class, ()->{
final int v1 = NumberUtil.parseInt("d");
assertEquals(0, v1);
});
@@ -453,7 +454,7 @@ public class NumberUtilTest {
@Test
public void parseIntTest4() {
Assertions.assertThrows(NumberFormatException.class, ()->{
assertThrows(NumberFormatException.class, ()->{
// issue#I5M55F
// 科学计数法忽略支持科学计数法一般用于表示非常小和非常大的数字这类数字转换为int后精度丢失没有意义。
final String numberStr = "429900013E20220812163344551";
@@ -692,7 +693,7 @@ public class NumberUtilTest {
@Test
public void rangeMinTest() {
Assertions.assertThrows(NegativeArraySizeException.class, ()->{
assertThrows(NegativeArraySizeException.class, ()->{
NumberUtil.range(0, Integer.MIN_VALUE);
});
}
@@ -757,7 +758,7 @@ public class NumberUtilTest {
@EnabledForJreRange(max = JRE.JAVA_8)
void numberFormatTest() {
// JDK8下NaN解析报错JDK9+中返回0
Assertions.assertThrows(ParseException.class, ()->{
assertThrows(ParseException.class, ()->{
NumberFormat.getInstance().parse("NaN");
});
}