The ShortHex of 0 should be '0x0', not '0x' (#272)

And some tests of toHexString and toShortHexString.
Danno Ferrin 6 years ago committed by GitHub
parent c12e1f5ad9
commit 2e98dc5b5e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      util/src/main/java/tech/pegasys/pantheon/util/uint/UInt256Value.java
  2. 31
      util/src/test/java/tech/pegasys/pantheon/util/uint/UInt256BytesTest.java

@ -160,7 +160,7 @@ public interface UInt256Value<T extends UInt256Value<T>> extends Bytes32Backed,
if (hex.charAt(2) != '0') return hex;
int i = 3;
while (i < hex.length() && hex.charAt(i) == '0') {
while (i < hex.length() - 1 && hex.charAt(i) == '0') {
i++;
}
return "0x" + hex.substring(i);

@ -12,6 +12,8 @@
*/
package tech.pegasys.pantheon.util.uint;
import static org.assertj.core.api.Assertions.assertThat;
import tech.pegasys.pantheon.util.bytes.Bytes32;
import tech.pegasys.pantheon.util.bytes.MutableBytes32;
import tech.pegasys.pantheon.util.uint.UInt256Bytes.BinaryLongOp;
@ -188,6 +190,35 @@ public class UInt256BytesTest {
bitLength("0x100000000", 33);
}
@Test
public void hexStrings() {
assertThat(UInt256.of(0).toShortHexString()).isEqualTo("0x0");
assertThat(UInt256.of(1).toShortHexString()).isEqualTo("0x1");
assertThat(UInt256.fromHexString("0xdeadbeef").toShortHexString()).isEqualTo("0xdeadbeef");
assertThat(
UInt256.fromHexString(
"0x00000000000000000000000000000000000000000000000000000000decafbad")
.toShortHexString())
.isEqualTo("0xdecafbad");
assertThat(UInt256.fromHexString("cafebabe").toShortHexString()).isEqualTo("0xcafebabe");
assertThat(UInt256.fromHexString("facefeed").toShortHexString()).isEqualTo("0xfacefeed");
assertThat(UInt256.of(0).toHexString())
.isEqualTo("0x0000000000000000000000000000000000000000000000000000000000000000");
assertThat(UInt256.of(1).toHexString())
.isEqualTo("0x0000000000000000000000000000000000000000000000000000000000000001");
assertThat(UInt256.fromHexString("0xdeadbeef").toHexString())
.isEqualTo("0x00000000000000000000000000000000000000000000000000000000deadbeef");
assertThat(
UInt256.fromHexString(
"0x00000000000000000000000000000000000000000000000000000000decafbad")
.toHexString())
.isEqualTo("0x00000000000000000000000000000000000000000000000000000000decafbad");
assertThat(UInt256.fromHexString("cafebabe").toHexString())
.isEqualTo("0x00000000000000000000000000000000000000000000000000000000cafebabe");
assertThat(UInt256.fromHexString("facefeed").toHexString())
.isEqualTo("0x00000000000000000000000000000000000000000000000000000000facefeed");
}
private void bitLength(final String input, final int expectedLength) {
Assert.assertEquals(
expectedLength, UInt256Bytes.bitLength(Bytes32.fromHexStringLenient(input)));

Loading…
Cancel
Save