Skip to content

Commit

Permalink
Test int16/Uint16 min max only
Browse files Browse the repository at this point in the history
Signed-off-by: tonykwok1992 <[email protected]>
  • Loading branch information
tonykwok1992 committed Jun 26, 2024
1 parent 5947e77 commit d183957
Showing 1 changed file with 31 additions and 27 deletions.
58 changes: 31 additions & 27 deletions abi/src/test/java/org/web3j/abi/TypeDecoderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@
import org.web3j.abi.datatypes.generated.Uint80;
import org.web3j.abi.datatypes.generated.Uint88;
import org.web3j.abi.datatypes.generated.Uint96;
import org.web3j.utils.Numeric;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
Expand Down Expand Up @@ -859,9 +858,6 @@ public void testIntDecode() throws Exception {
Int256.class),
(new Int256(BigInteger.valueOf(-1))));




assertEquals(
TypeDecoder.decodeNumeric(
TypeEncoder.encodeNumeric(new Int256(BigInteger.TWO.pow(248))),
Expand All @@ -870,52 +866,60 @@ public void testIntDecode() throws Exception {

assertEquals(
TypeDecoder.decodeNumeric(
TypeEncoder.encodeNumeric(new Int256(BigInteger.TWO.pow(248).negate().subtract(BigInteger.ONE))),
TypeEncoder.encodeNumeric(
new Int256(
BigInteger.TWO.pow(248).negate().subtract(BigInteger.ONE))),
Int256.class),
new Int256(BigInteger.TWO.pow(248).negate().subtract(BigInteger.ONE)));



assertEquals(
TypeDecoder.decodeNumeric(
"0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
Int256.class),
new Int256(new BigInteger("57896044618658097711785492504343953926634992332820282019728792003956564819967")));
new Int256(
new BigInteger(
"57896044618658097711785492504343953926634992332820282019728792003956564819967")));

assertEquals(
TypeDecoder.decodeNumeric(
"0x8000000000000000000000000000000000000000000000000000000000000000",
Int256.class),
new Int256(new BigInteger("-57896044618658097711785492504343953926634992332820282019728792003956564819968")));
new Int256(
new BigInteger(
"-57896044618658097711785492504343953926634992332820282019728792003956564819968")));

assertEquals(TypeDecoder.instantiateType("int", 123), (new Int(BigInteger.valueOf(123))));

assertEquals(TypeDecoder.instantiateType("int", -123), (new Int(BigInteger.valueOf(-123))));
}

@Test
public void testInt16All() throws Exception {
int boundary = (int) Math.pow(2, 16 - 1);
for (int i = -boundary; i < boundary; i++) {
assertEquals(
TypeDecoder.decodeNumeric(
TypeEncoder.encodeNumeric(new Int16(BigInteger.valueOf(i))),
Int16.class),
new Int16(BigInteger.valueOf(i)));
}
public void testInt16MinMax() throws Exception {
assertEquals(
TypeDecoder.decodeNumeric(
TypeEncoder.encodeNumeric(
new Int16(BigInteger.valueOf((long) Math.pow(2, 15) - 1))),
Int16.class),
new Int16(BigInteger.valueOf((long) Math.pow(2, 15) - 1)));

assertEquals(
TypeDecoder.decodeNumeric(
TypeEncoder.encodeNumeric(
new Int16(BigInteger.valueOf((long) -Math.pow(2, 15)))),
Int16.class),
new Int16(BigInteger.valueOf((long) -Math.pow(2, 15))));
}

@Test
public void testUint16All() throws Exception {
int boundary = (int) Math.pow(2, 16);
for (int i = 0; i < boundary; i++) {
assertEquals(
TypeDecoder.decodeNumeric(
TypeEncoder.encodeNumeric(new Uint16(BigInteger.valueOf(i))),
Uint16.class),
new Uint16(BigInteger.valueOf(i)));
}
public void testUint16Max() throws Exception {
assertEquals(
TypeDecoder.decodeNumeric(
TypeEncoder.encodeNumeric(
new Uint16(BigInteger.valueOf((long) Math.pow(2, 16) - 1))),
Uint16.class),
new Uint16(BigInteger.valueOf((long) Math.pow(2, 16) - 1)));
}

/*
TODO: Enable once Solidity supports fixed types - see
https://github.com/ethereum/solidity/issues/409
Expand Down

0 comments on commit d183957

Please sign in to comment.