private static void assertSlicesEquals(Slice slice, Slice other) { int size = slice.length(); assertEquals(slice, other); assertTrue(slice.equals(0, size, other, 0, size)); assertEquals(slice.hashCode(), other.hashCode()); assertEquals(slice.hashCode(), other.hashCode(0, size)); assertEquals(slice.compareTo(other), 0); assertEquals(slice.compareTo(0, size, other, 0, size), 0); for (int i = 0; i < slice.length(); i++) { assertTrue(slice.equals(i, size - i, other, i, size - i)); assertEquals(slice.hashCode(i, size - i), other.hashCode(i, size - i)); assertEquals(slice.compareTo(i, size - i, other, i, size - i), 0); } for (int i = 0; i < slice.length(); i++) { assertTrue(slice.equals(0, size - i, other, 0, size - i)); assertEquals(slice.hashCode(0, size - i), other.hashCode(0, size - i)); assertEquals(slice.compareTo(0, size - i, other, 0, size - i), 0); } }
@Test public void testEqualsHashCodeCompare() { for (int size = 0; size < 100; size++) { // self equals Slice slice = allocate(size); assertSlicesEquals(slice, slice); // equals other all zero Slice other = allocate(size); assertSlicesEquals(slice, other); // equals self fill pattern slice = allocate(size); // create a new slice since slices cache the hash code value slice.fill((byte) 0xA5); assertSlicesEquals(slice, slice); // equals other fill pattern other = allocate(size); // create a new slice since slices cache the hash code value other.fill((byte) 0xA5); assertSlicesEquals(slice, other); // different types assertNotEquals(slice, new Object()); //noinspection MisorderedAssertEqualsArgumentsTestNG assertNotEquals(new Object(), slice); // different sizes Slice oneBigger = allocate(size + 1); oneBigger.fill((byte) 0xA5); assertNotEquals(slice, oneBigger); assertNotEquals(oneBigger, slice); assertTrue(slice.compareTo(oneBigger) < 0); assertTrue(oneBigger.compareTo(slice) > 0); assertFalse(slice.equals(0, size, oneBigger, 0, size + 1)); assertFalse(oneBigger.equals(0, size + 1, slice, 0, size)); assertTrue(slice.compareTo(0, size, oneBigger, 0, size + 1) < 0); assertTrue(oneBigger.compareTo(0, size + 1, slice, 0, size) > 0); // different in one byte for (int i = 1; i < slice.length(); i++) { slice.setByte(i - 1, 0xA5); assertTrue(slice.equals(i - 1, size - i, other, i - 1, size - i)); slice.setByte(i, 0xFF); assertNotEquals(slice, other); assertFalse(slice.equals(i, size - i, other, i, size - i)); assertTrue(slice.compareTo(0, size, oneBigger, 0, size + 1) > 0); } // compare with empty slice if (slice.length() > 0) { assertNotEquals(slice, EMPTY_SLICE); //noinspection MisorderedAssertEqualsArgumentsTestNG assertNotEquals(EMPTY_SLICE, slice); assertFalse(slice.equals(0, size, EMPTY_SLICE, 0, 0)); assertFalse(EMPTY_SLICE.equals(0, 0, slice, 0, size)); assertTrue(slice.compareTo(0, size, EMPTY_SLICE, 0, 0) > 0); assertTrue(EMPTY_SLICE.compareTo(0, 0, slice, 0, size) < 0); try { //noinspection ResultOfMethodCallIgnored slice.equals(0, size, EMPTY_SLICE, 0, size); fail("expected IndexOutOfBoundsException"); } catch (IndexOutOfBoundsException expected) { } try { //noinspection ResultOfMethodCallIgnored EMPTY_SLICE.equals(0, size, slice, 0, size); fail("expected IndexOutOfBoundsException"); } catch (IndexOutOfBoundsException expected) { } try { slice.compareTo(0, size, EMPTY_SLICE, 0, size); fail("expected IndexOutOfBoundsException"); } catch (IndexOutOfBoundsException expected) { } try { EMPTY_SLICE.compareTo(0, size, slice, 0, size); fail("expected IndexOutOfBoundsException"); } catch (IndexOutOfBoundsException expected) { } } } }