ByteUtils { public static boolean areKeyBytesEqual(final byte[] bytes1, final byte[] bytes2) { if (Arrays.equals(bytes1, bytes2)) { return true; } int last = bytes1.length; if (last != bytes2.length) { return false; } if (last == 0) { return true; } last--; if (bytes1[last] == bytes2[last]) { for (int i = 0; i < last; i++) { if (bytes1[i] != bytes2[i]) { return false; } } } else { return false; } return true; } private  ByteUtils(); static int compareBytes(final byte[] bytes1, final byte[] bytes2); static boolean areKeyBytesEqual(final byte[] bytes1, final byte[] bytes2); static boolean areKeyBytesEqual(final BytesAndRange a, final BytesAndRange b);  }
@Test public void shouldPassWithSameByteRangeObject() throws Exception { br1 = new BytesAndRange(a, 0, a.length); assertTrue(ByteUtils.areKeyBytesEqual(br1, br1)); }
@Test public void shouldPassWithSameArraysAndMatchingRange() throws Exception { br1 = new BytesAndRange(a, 0, a.length); br2 = new BytesAndRange(a, 0, a.length); assertTrue(ByteUtils.areKeyBytesEqual(br1, br2)); }
@Test public void shouldFailWithSameArraysAndMismatchedRange() throws Exception { br1 = new BytesAndRange(a, 0, a.length - 1); br2 = new BytesAndRange(a, 1, a.length); assertFalse(ByteUtils.areKeyBytesEqual(br1, br2)); }
@Test public void shouldFailWithSameArraysAndDifferentLengths() throws Exception { br1 = new BytesAndRange(a, 0, a.length - 1); br2 = new BytesAndRange(a, 0, a.length); assertFalse(ByteUtils.areKeyBytesEqual(br1, br2)); }
@Test public void shouldPassWithDuplicatedArrays() throws Exception { br1 = new BytesAndRange(a, 0, a.length); br2 = new BytesAndRange(b, 0, b.length); assertTrue(ByteUtils.areKeyBytesEqual(br1, br2)); }
@Test public void shouldPassWithDifferentSizedBackingArraysButMatchingSelectedRange() throws Exception { br1 = new BytesAndRange(a, 0, a.length - 1); br2 = new BytesAndRange(c, 0, c.length); assertTrue(ByteUtils.areKeyBytesEqual(br1, br2)); }
@Test public void shouldPassWithOffsets() throws Exception { br1 = new BytesAndRange(e, 1, 4); br2 = new BytesAndRange(a, 1, 4); assertTrue(ByteUtils.areKeyBytesEqual(br1, br2)); }
@Test public void shouldPassWithSameBackingArrayButDifferentOffsets() throws Exception { br1 = new BytesAndRange(e, 0, 5); br2 = new BytesAndRange(e, 5, 5); assertTrue(ByteUtils.areKeyBytesEqual(br1, br2)); }
@Test public void shouldFailWithSameLengthAndDifferentArrayContent() throws Exception { br1 = new BytesAndRange(a, 0, a.length); br2 = new BytesAndRange(d, 0, d.length); assertFalse(ByteUtils.areKeyBytesEqual(br1, br2)); }
@Test public void shouldFailWithDifferentSizedBackingArrays() throws Exception { br1 = new BytesAndRange(a, 0, a.length); br2 = new BytesAndRange(c, 0, c.length); assertFalse(ByteUtils.areKeyBytesEqual(br1, br2)); }
@Test public void shouldFailWithSameBackingArrayMatchingSelectionWithDifferentOffsetsDifferentLengthsButLargerLengthIsLimitedByEndOfArray() throws Exception { br1 = new BytesAndRange(e, 0, 5); br2 = new BytesAndRange(e, 5, 9999); assertFalse(ByteUtils.areKeyBytesEqual(br1, br2)); }
@Test public void shouldThrowExceptionFailWithCorrectBackingArraysButInvalidMatchingLength() throws Exception { br1 = new BytesAndRange(a, 0, 999); br2 = new BytesAndRange(d, 0, 999); assertThrows(ArrayIndexOutOfBoundsException.class, () -> assertFalse(ByteUtils.areKeyBytesEqual(br1, br2))); }
@Test public void shouldFailWithCorrectBackingArraysButInvalidNotMatchingLength() throws Exception { br1 = new BytesAndRange(a, 0, 6666); br2 = new BytesAndRange(b, 0, 9999); assertFalse(ByteUtils.areKeyBytesEqual(br1, br2)); }
@Test public void shouldThrowExceptionEmptyArrayWithLength() throws Exception { br1 = new BytesAndRange(f, 0, 1); br2 = new BytesAndRange(f, 0, 1); assertThrows(ArrayIndexOutOfBoundsException.class, () -> assertFalse(ByteUtils.areKeyBytesEqual(br1, br2))); }
@Test public void shouldPassWithTwoEmptyArrays() throws Exception { br1 = new BytesAndRange(f, 0, 0); br2 = new BytesAndRange(f, 0, 0); assertTrue(ByteUtils.areKeyBytesEqual(br1, br2)); }
@Test public void shouldFailWithNullAndEmptyArrays() throws Exception { br1 = new BytesAndRange(f, 0, 0); br2 = new BytesAndRange(null, 0, 0); assertFalse(ByteUtils.areKeyBytesEqual(br1, br2)); }
@Test public void shouldFailWithEmptyAndZeroRange() throws Exception { br1 = new BytesAndRange(a, 0, 0); br2 = new BytesAndRange(f, 0, 0); assertTrue(ByteUtils.areKeyBytesEqual(br1, br2)); }
@Test public void shouldFailWithEmptyAndZeroRangeAndOffset() throws Exception { br1 = new BytesAndRange(a, 3, 0); br2 = new BytesAndRange(f, 0, 0); assertTrue(ByteUtils.areKeyBytesEqual(br1, br2)); }