mirror of
https://github.com/bitcoinj/bitcoinj.git
synced 2025-01-19 05:33:44 +01:00
ParseByteCacheTest: rewrite arrayContains()
helper
The previous implementation was based on hex strings and was prone to raising a false positive when matching at an uneven hex position. This also adds a test.
This commit is contained in:
parent
979490ad97
commit
d222efd6de
@ -426,23 +426,31 @@ public class ParseByteCacheTest {
|
|||||||
assertTrue(arrayContains(containingBytes, b1));
|
assertTrue(arrayContains(containingBytes, b1));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Determine if sub is contained in sup.
|
||||||
public static boolean arrayContains(byte[] sup, byte[] sub) {
|
public static boolean arrayContains(byte[] sup, byte[] sub) {
|
||||||
if (sup.length < sub.length)
|
ByteBuffer subBuf = ByteBuffer.wrap(sub);
|
||||||
return false;
|
int subLength = sub.length;
|
||||||
|
int lengthDiff = sup.length - subLength;
|
||||||
String superstring = ByteUtils.formatHex(sup);
|
if (lengthDiff < 0)
|
||||||
String substring = ByteUtils.formatHex(sub);
|
return false;
|
||||||
|
for (int i = 0; i <= lengthDiff; i++)
|
||||||
int ind = superstring.indexOf(substring);
|
if (ByteBuffer.wrap(sup, i, subLength).equals(subBuf))
|
||||||
|
return true;
|
||||||
StringBuilder sb = new StringBuilder();
|
return false;
|
||||||
for (int i = 0; i < superstring.indexOf(substring); i++)
|
}
|
||||||
sb.append(" ");
|
|
||||||
|
@Test
|
||||||
//System.out.println(superstring);
|
public void testArrayContains() {
|
||||||
//System.out.println(sb.append(substring).toString());
|
byte[] oneToNine = ByteUtils.parseHex("010203040506070809");
|
||||||
//System.out.println();
|
assertTrue(arrayContains(oneToNine, oneToNine));
|
||||||
return ind > -1;
|
assertTrue(arrayContains(oneToNine, ByteUtils.parseHex("010203")));
|
||||||
|
assertTrue(arrayContains(oneToNine, ByteUtils.parseHex("040506")));
|
||||||
|
assertTrue(arrayContains(oneToNine, ByteUtils.parseHex("070809")));
|
||||||
|
assertTrue(arrayContains(oneToNine, new byte[0]));
|
||||||
|
|
||||||
|
assertFalse(arrayContains(oneToNine, ByteUtils.parseHex("123456")));
|
||||||
|
assertFalse(arrayContains(oneToNine, ByteUtils.parseHex("080910")));
|
||||||
|
assertFalse(arrayContains(oneToNine, ByteUtils.parseHex("01020304050607080910")));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user