This is an automated email from the ASF dual-hosted git repository.
toulmean pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-tuweni.git
The following commit(s) were added to refs/heads/main by this push:
new 5d7568b8 fix zero compares of different lengths
new 7f86159a Merge pull request #447 from atoulme/fix_zero_compares
5d7568b8 is described below
commit 5d7568b84ce4e475e9e9b2dc3fdcbac838c2f860
Author: Antoine Toulme <[email protected]>
AuthorDate: Sat Nov 19 21:58:13 2022 -0800
fix zero compares of different lengths
---
bytes/src/main/java/org/apache/tuweni/bytes/Bytes.java | 7 ++++++-
bytes/src/test/java/org/apache/tuweni/bytes/BytesTest.java | 4 ++++
2 files changed, 10 insertions(+), 1 deletion(-)
diff --git a/bytes/src/main/java/org/apache/tuweni/bytes/Bytes.java
b/bytes/src/main/java/org/apache/tuweni/bytes/Bytes.java
index 50291371..668abad8 100644
--- a/bytes/src/main/java/org/apache/tuweni/bytes/Bytes.java
+++ b/bytes/src/main/java/org/apache/tuweni/bytes/Bytes.java
@@ -1687,10 +1687,15 @@ public interface Bytes extends Comparable<Bytes> {
default int compareTo(Bytes b) {
checkNotNull(b);
- int sizeCmp = Integer.compare(bitLength(), b.bitLength());
+ int bitLength = bitLength();
+ int sizeCmp = Integer.compare(bitLength, b.bitLength());
if (sizeCmp != 0) {
return sizeCmp;
}
+ // same bitlength and is zeroes only, return 0.
+ if (bitLength == 0) {
+ return 0;
+ }
for (int i = 0; i < size(); i++) {
int cmp = Integer.compare(get(i) & 0xff, b.get(i) & 0xff);
diff --git a/bytes/src/test/java/org/apache/tuweni/bytes/BytesTest.java
b/bytes/src/test/java/org/apache/tuweni/bytes/BytesTest.java
index 35507ef9..8a293cf0 100644
--- a/bytes/src/test/java/org/apache/tuweni/bytes/BytesTest.java
+++ b/bytes/src/test/java/org/apache/tuweni/bytes/BytesTest.java
@@ -309,6 +309,10 @@ class BytesTest extends CommonBytesTests {
assertEquals(-1, Bytes.of(0x01).compareTo(Bytes.of(0x01, 0xff)));
assertEquals(-1, Bytes.of(0x00, 0x00, 0x01).compareTo(Bytes.of(0x00,
0x02)));
assertEquals(-1, Bytes.of(0x00, 0x01).compareTo(Bytes.of(0x00, 0x00,
0x05)));
+ assertEquals(0,
Bytes.fromHexString("0x0000").compareTo(Bytes.fromHexString("0x00")));
+ assertEquals(0,
Bytes.fromHexString("0x00").compareTo(Bytes.fromHexString("0x0000")));
+ assertEquals(0,
Bytes.fromHexString("0x000000").compareTo(Bytes.fromHexString("0x000000")));
+ assertEquals(-1,
Bytes.fromHexString("0x000001").compareTo(Bytes.fromHexString("0x0001")));
}
@Test
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]