From 2e98dc5b5e8c0251579fa38f0d53901b42ccaf18 Mon Sep 17 00:00:00 2001 From: Danno Ferrin Date: Mon, 19 Nov 2018 12:42:41 -0700 Subject: [PATCH] The ShortHex of 0 should be '0x0', not '0x' (#272) And some tests of toHexString and toShortHexString. --- .../pantheon/util/uint/UInt256Value.java | 2 +- .../pantheon/util/uint/UInt256BytesTest.java | 31 +++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/util/src/main/java/tech/pegasys/pantheon/util/uint/UInt256Value.java b/util/src/main/java/tech/pegasys/pantheon/util/uint/UInt256Value.java index f6d28ea36f..d44c86b205 100644 --- a/util/src/main/java/tech/pegasys/pantheon/util/uint/UInt256Value.java +++ b/util/src/main/java/tech/pegasys/pantheon/util/uint/UInt256Value.java @@ -160,7 +160,7 @@ public interface UInt256Value> 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); diff --git a/util/src/test/java/tech/pegasys/pantheon/util/uint/UInt256BytesTest.java b/util/src/test/java/tech/pegasys/pantheon/util/uint/UInt256BytesTest.java index 7f6e5f7d66..9b557045f9 100644 --- a/util/src/test/java/tech/pegasys/pantheon/util/uint/UInt256BytesTest.java +++ b/util/src/test/java/tech/pegasys/pantheon/util/uint/UInt256BytesTest.java @@ -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)));