From e25fbc90cbc47eb7d5a94b16d353ba494d6caea4 Mon Sep 17 00:00:00 2001 From: nicksavers Date: Mon, 4 Aug 2014 00:19:25 +0200 Subject: [PATCH] Add Unit tests for matchingNibbleLength and move to ByteUtil --- .../src/main/java/org/ethereum/trie/Trie.java | 14 +---- .../main/java/org/ethereum/util/ByteUtil.java | 18 ++++++ .../java/org/ethereum/util/ByteUtilTest.java | 60 +++++++++++++++++++ 3 files changed, 79 insertions(+), 13 deletions(-) diff --git a/ethereumj-core/src/main/java/org/ethereum/trie/Trie.java b/ethereumj-core/src/main/java/org/ethereum/trie/Trie.java index 7510c921..7a806d83 100644 --- a/ethereumj-core/src/main/java/org/ethereum/trie/Trie.java +++ b/ethereumj-core/src/main/java/org/ethereum/trie/Trie.java @@ -3,6 +3,7 @@ package org.ethereum.trie; import static java.util.Arrays.copyOfRange; import static org.spongycastle.util.Arrays.concatenate; import static org.ethereum.util.CompactEncoder.*; +import static org.ethereum.util.ByteUtil.matchingNibbleLength; import java.util.Arrays; @@ -399,19 +400,6 @@ public class Trie implements TrieFacade { * Utility functions * *******************************/ - // Returns the amount of nibbles that match each other from 0 ... - public static int matchingNibbleLength(byte[] a, byte[] b) { - int i = 0; - int length = a.length < b.length ? a.length : b.length; - while (i < length) { - if (a[i] != b[i]) - break; - i++; - } - return i; - } - - // Created an array of empty elements of requred length private Object[] emptyStringSlice(int l) { Object[] slice = new Object[l]; diff --git a/ethereumj-core/src/main/java/org/ethereum/util/ByteUtil.java b/ethereumj-core/src/main/java/org/ethereum/util/ByteUtil.java index 503a7e89..d79a530d 100644 --- a/ethereumj-core/src/main/java/org/ethereum/util/ByteUtil.java +++ b/ethereumj-core/src/main/java/org/ethereum/util/ByteUtil.java @@ -60,6 +60,24 @@ public class ByteUtil { return data; } + /** + * Returns the amount of nibbles that match each other from 0 ... + * amount will never be larger than smallest input + * + * @param a - first input + * @param b second input + * @return number of bytes that match + */ + public static int matchingNibbleLength(byte[] a, byte[] b) { + int i = 0; + int length = a.length < b.length ? a.length : b.length; + while (i < length) { + if (a[i] != b[i]) + break; + i++; + } + return i; + } public static byte[] longToBytes(long l) { return ByteBuffer.allocate(8).putLong(l).array(); diff --git a/ethereumj-core/src/test/java/org/ethereum/util/ByteUtilTest.java b/ethereumj-core/src/test/java/org/ethereum/util/ByteUtilTest.java index e2c77a30..30ecb5db 100644 --- a/ethereumj-core/src/test/java/org/ethereum/util/ByteUtilTest.java +++ b/ethereumj-core/src/test/java/org/ethereum/util/ByteUtilTest.java @@ -62,6 +62,66 @@ public class ByteUtilTest { assertArrayEquals(expected, ByteUtil.stripLeadingZeroes(test1)); assertArrayEquals(expected, ByteUtil.stripLeadingZeroes(test2)); } + + @Test + public void testMatchingNibbleLength1() { + // a larger than b + byte[] a = new byte[] { 0x00, 0x01 }; + byte[] b = new byte[] { 0x00 }; + int result = ByteUtil.matchingNibbleLength(a, b); + assertEquals(1, result); + } + @Test + public void testMatchingNibbleLength2() { + // b larger than a + byte[] a = new byte[] { 0x00 }; + byte[] b = new byte[] { 0x00, 0x01 }; + int result = ByteUtil.matchingNibbleLength(a, b); + assertEquals(1, result); + } + + @Test + public void testMatchingNibbleLength3() { + // a and b the same length equal + byte[] a = new byte[] { 0x00 }; + byte[] b = new byte[] { 0x00 }; + int result = ByteUtil.matchingNibbleLength(a, b); + assertEquals(1, result); + } + + @Test + public void testMatchingNibbleLength4() { + // a and b the same length not equal + byte[] a = new byte[] { 0x01 }; + byte[] b = new byte[] { 0x00 }; + int result = ByteUtil.matchingNibbleLength(a, b); + assertEquals(0, result); + } + + @Test(expected=NullPointerException.class) + public void testMatchingNibbleLength5() { + // a == null + byte[] a = null; + byte[] b = new byte[] { 0x00 }; + ByteUtil.matchingNibbleLength(a, b); + } + + @Test(expected=NullPointerException.class) + public void testMatchingNibbleLength6() { + // b == null + byte[] a = new byte[] { 0x00 }; + byte[] b = null; + ByteUtil.matchingNibbleLength(a, b); + } + + @Test + public void testMatchingNibbleLength7() { + // a or b is empty + byte[] a = new byte[0]; + byte[] b = new byte[] { 0x00 }; + int result = ByteUtil.matchingNibbleLength(a, b); + assertEquals(0, result); + } /** * This test shows the difference between iterating over,