Skip to content

Commit

Permalink
improved TypeUtils#isInt32 & isInt64
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Aug 18, 2023
1 parent 56f37d0 commit eb28be4
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
13 changes: 12 additions & 1 deletion core/src/main/java/com/alibaba/fastjson2/util/TypeUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -1879,15 +1879,26 @@ public static BigDecimal toBigDecimal(byte[] strBytes) {
}

public static boolean isInt32(BigInteger value) {
if (FIELD_BIGINTEGER_MAG_OFFSET != -1) {
int[] mag = (int[]) UNSAFE.getObject(value, FIELD_BIGINTEGER_MAG_OFFSET);
return mag.length == 0
|| (mag.length == 1 && (mag[0] >= 0 || (mag[0] == Integer.MIN_VALUE && value.signum() == -1)));
}

return value.compareTo(BIGINT_INT32_MIN) >= 0 && value.compareTo(BIGINT_INT32_MAX) <= 0;
}

public static boolean isInt64(BigInteger value) {
if (FIELD_BIGINTEGER_MAG_OFFSET != -1) {
int[] mag = (int[]) UNSAFE.getObject(value, FIELD_BIGINTEGER_MAG_OFFSET);
if (mag.length == 1 || (mag.length == 2 && mag[0] >= 0)) {
if (mag.length <= 1) {
return true;
}

if (mag.length == 2) {
int mag0 = mag[0];
return mag[0] >= 0 || (mag0 == Integer.MIN_VALUE && mag[1] == 0 && value.signum() == -1);
}
}
return value.compareTo(BIGINT_INT64_MIN) >= 0 && value.compareTo(BIGINT_INT64_MAX) <= 0;
}
Expand Down
68 changes: 68 additions & 0 deletions core/src/test/java/com/alibaba/fastjson2/util/TypeUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -736,4 +736,72 @@ public void isInteger() {
assertTrue(TypeUtils.isInteger(new BigDecimal("-92233720368547758081")));
assertFalse(TypeUtils.isInteger(new BigDecimal("-9223372036854775808.1")));
}

@Test
public void isInt64_true() {
BigInteger[] integers = new BigInteger[] {
BigInteger.ZERO,
BigInteger.valueOf(Long.MIN_VALUE),
BigInteger.valueOf(Long.MIN_VALUE + 1),
BigInteger.valueOf(Long.MIN_VALUE + Integer.MAX_VALUE),
BigInteger.valueOf(Long.MAX_VALUE),
BigInteger.valueOf(Long.MAX_VALUE - 1),
BigInteger.valueOf(Long.MAX_VALUE - Integer.MAX_VALUE)
};
for (BigInteger integer : integers) {
assertTrue(TypeUtils.isInt64(integer));
}
}

@Test
public void isInt64_false() {
BigInteger[] integers = new BigInteger[] {
BigInteger.valueOf(Long.MIN_VALUE).subtract(BigInteger.ONE),
BigInteger.valueOf(Long.MIN_VALUE).subtract(BigInteger.TEN),
BigInteger.valueOf(Long.MIN_VALUE).subtract(BigInteger.valueOf(100)),
BigInteger.valueOf(Long.MIN_VALUE).subtract(BigInteger.valueOf(1000)),
BigInteger.valueOf(Long.MIN_VALUE).subtract(BigInteger.valueOf(Integer.MAX_VALUE)),
BigInteger.valueOf(Long.MAX_VALUE).add(BigInteger.ONE),
BigInteger.valueOf(Long.MAX_VALUE).add(BigInteger.TEN),
BigInteger.valueOf(Long.MAX_VALUE).add(BigInteger.valueOf(100)),
BigInteger.valueOf(Long.MAX_VALUE).add(BigInteger.valueOf(1000)),
BigInteger.valueOf(Long.MAX_VALUE).add(BigInteger.valueOf(Integer.MAX_VALUE)),
};
for (BigInteger integer : integers) {
assertFalse(TypeUtils.isInt64(integer));
}
}

@Test
public void isInt32_true() {
BigInteger[] integers = new BigInteger[] {
BigInteger.ZERO,
BigInteger.valueOf(Integer.MIN_VALUE),
BigInteger.valueOf(Integer.MIN_VALUE + 1),
BigInteger.valueOf(Integer.MIN_VALUE + Short.MAX_VALUE),
BigInteger.valueOf(Integer.MAX_VALUE),
BigInteger.valueOf(Integer.MAX_VALUE - 1),
BigInteger.valueOf(Integer.MAX_VALUE - Short.MAX_VALUE)
};
for (BigInteger integer : integers) {
assertTrue(TypeUtils.isInt32(integer));
}
}

@Test
public void isInt32_false() {
BigInteger[] integers = new BigInteger[] {
BigInteger.valueOf(Integer.MIN_VALUE - 1L),
BigInteger.valueOf(Integer.MIN_VALUE - 10L),
BigInteger.valueOf(Integer.MIN_VALUE - 100L),
BigInteger.valueOf(Integer.MIN_VALUE - 1000L),
BigInteger.valueOf(Integer.MAX_VALUE + 1L),
BigInteger.valueOf(Integer.MAX_VALUE + 10L),
BigInteger.valueOf(Integer.MAX_VALUE + 100L),
BigInteger.valueOf(Integer.MAX_VALUE + 1000L),
};
for (BigInteger integer : integers) {
assertFalse(TypeUtils.isInt32(integer));
}
}
}

0 comments on commit eb28be4

Please sign in to comment.