Esempio n. 1
0
  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);
    }
  }
Esempio n. 2
0
  private void assertBytesSlice(Slice slice, int index) {
    // fill slice with FF
    slice.fill((byte) 0xFF);

    // compare to self slice
    assertEquals(slice.slice(0, slice.length()), slice);
    Slice value = allocate(slice.length());
    slice.getBytes(0, value, 0, slice.length());
    assertEquals(value, slice);

    // set and get the value
    value = allocate((slice.length() - index) / 2);
    for (int i = 0; i < value.length(); i++) {
      value.setByte(i, i);
    }

    // check by slicing out the region
    slice.setBytes(index, value);
    assertEquals(value, slice.slice(index, value.length()));

    // check by getting out the region
    Slice tempValue = allocate(value.length());
    slice.getBytes(index, tempValue, 0, tempValue.length());
    assertEquals(tempValue, slice.slice(index, tempValue.length()));
    assertTrue(tempValue.equals(0, tempValue.length(), slice, index, tempValue.length()));

    for (int length = 0; length < value.length(); length++) {
      slice.fill((byte) 0xFF);
      slice.setBytes(index, value, 0, length);

      // check by slicing out the region
      assertEquals(value.slice(0, length), slice.slice(index, length));
      assertTrue(value.equals(0, length, slice, index, length));

      // check by getting out the region
      tempValue = allocate(length);
      slice.getBytes(index, tempValue);
      assertEquals(tempValue, slice.slice(index, length));
      assertTrue(tempValue.equals(0, length, slice, index, length));
    }
  }
Esempio n. 3
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) {
        }
      }
    }
  }