Add Unit tests for matchingNibbleLength and move to ByteUtil
This commit is contained in:
parent
028dc5f1d4
commit
e25fbc90cb
|
@ -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];
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Reference in New Issue