示例#1
2
  private static void assertFloat(Slice slice, int index) {
    // fill slice with FF
    slice.fill((byte) 0xFF);

    // set and get the value
    slice.setFloat(index, intBitsToFloat(0xAAAA_5555));
    assertEquals(floatToIntBits(slice.getFloat(index)), 0xAAAA_5555);

    try {
      slice.getFloat(-1);
      fail("expected IndexOutOfBoundsException");
    } catch (IndexOutOfBoundsException e) {
    }

    try {
      slice.getFloat((slice.length() - SIZE_OF_FLOAT) + 1);
      fail("expected IndexOutOfBoundsException");
    } catch (IndexOutOfBoundsException e) {
    }

    try {
      slice.getFloat(slice.length());
      fail("expected IndexOutOfBoundsException");
    } catch (IndexOutOfBoundsException e) {
    }

    try {
      slice.getFloat(slice.length() + 1);
      fail("expected IndexOutOfBoundsException");
    } catch (IndexOutOfBoundsException e) {
    }
  }
示例#2
0
  private static void assertBytesStreams(Slice slice, int index) throws Exception {
    // fill slice with FF
    slice.fill((byte) 0xFF);

    byte[] value = new byte[slice.length()];
    Arrays.fill(value, (byte) 0xFF);
    assertEquals(slice.getBytes(), value);

    // set and get the value
    value = new byte[(slice.length() - index) / 2];
    for (int i = 0; i < value.length; i++) {
      value[i] = (byte) i;
    }
    slice.setBytes(index, new ByteArrayInputStream(value), value.length);
    assertEquals(slice.getBytes(index, value.length), value);

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    slice.getBytes(index, out, value.length);
    assertEquals(slice.getBytes(index, value.length), out.toByteArray());

    for (int length = 0; length < value.length; length++) {
      slice.fill((byte) 0xFF);
      slice.setBytes(index, new ByteArrayInputStream(value), length);
      assertEquals(slice.getBytes(index, length), Arrays.copyOf(value, length));

      out = new ByteArrayOutputStream();
      slice.getBytes(index, out, length);
      assertEquals(slice.getBytes(index, length), out.toByteArray());
    }
  }
示例#3
0
  private static void assertDouble(Slice slice, int index) {
    // fill slice with FF
    slice.fill((byte) 0xFF);

    // set and get the value
    slice.setDouble(index, longBitsToDouble(0xAAAA_AAAA_5555_5555L));
    assertEquals(doubleToLongBits(slice.getDouble(index)), 0xAAAA_AAAA_5555_5555L);

    try {
      slice.getDouble(-1);
      fail("expected IndexOutOfBoundsException");
    } catch (IndexOutOfBoundsException e) {
    }

    try {
      slice.getDouble((slice.length() - SIZE_OF_DOUBLE) + 1);
      fail("expected IndexOutOfBoundsException");
    } catch (IndexOutOfBoundsException e) {
    }

    try {
      slice.getDouble(slice.length());
      fail("expected IndexOutOfBoundsException");
    } catch (IndexOutOfBoundsException e) {
    }

    try {
      slice.getDouble(slice.length() + 1);
      fail("expected IndexOutOfBoundsException");
    } catch (IndexOutOfBoundsException e) {
    }
  }
示例#4
0
  private static void assertLexicographicallySortableLong(Slice slice, int index) {
    // fill slice with FF
    slice.fill((byte) 0xFF);

    // set and get the value
    slice.setLexicographicallySortableLong(index, 0xAAAA_AAAA_5555_5555L);
    assertEquals(slice.getLexicographicallySortableLong(index), 0xAAAA_AAAA_5555_5555L);

    try {
      slice.getLexicographicallySortableLong(-1);
      fail("expected IndexOutOfBoundsException");
    } catch (IndexOutOfBoundsException e) {
    }

    try {
      slice.getLexicographicallySortableLong((slice.length() - SIZE_OF_LONG) + 1);
      fail("expected IndexOutOfBoundsException");
    } catch (IndexOutOfBoundsException e) {
    }

    try {
      slice.getLexicographicallySortableLong(slice.length());
      fail("expected IndexOutOfBoundsException");
    } catch (IndexOutOfBoundsException e) {
    }

    try {
      slice.getLexicographicallySortableLong(slice.length() + 1);
      fail("expected IndexOutOfBoundsException");
    } catch (IndexOutOfBoundsException e) {
    }
  }
示例#5
0
  private static void assertShort(Slice slice, short index) {
    // fill slice with FF
    slice.fill((byte) 0xFF);

    // set and get the value
    slice.setShort(index, 0xAA55);
    assertEquals(slice.getShort(index), (short) 0xAA55);

    try {
      slice.getShort(-1);
      fail("expected IndexOutOfBoundsException");
    } catch (IndexOutOfBoundsException e) {
    }

    try {
      slice.getShort((slice.length() - SIZE_OF_SHORT) + 1);
      fail("expected IndexOutOfBoundsException");
    } catch (IndexOutOfBoundsException e) {
    }

    try {
      slice.getShort(slice.length());
      fail("expected IndexOutOfBoundsException");
    } catch (IndexOutOfBoundsException e) {
    }

    try {
      slice.getShort(slice.length() + 1);
      fail("expected IndexOutOfBoundsException");
    } catch (IndexOutOfBoundsException e) {
    }
  }
示例#6
0
 @Test
 public void testFillAndClear() {
   for (byte size = 0; size < 100; size++) {
     Slice slice = allocate(size);
     for (int i = 0; i < slice.length(); i++) {
       assertEquals(slice.getByte(i), (byte) 0);
     }
     slice.fill((byte) 0xA5);
     for (int i = 0; i < slice.length(); i++) {
       assertEquals(slice.getByte(i), (byte) 0xA5);
     }
     slice.clear();
     for (int i = 0; i < slice.length(); i++) {
       assertEquals(slice.getByte(i), (byte) 0);
     }
   }
 }
示例#7
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);
    }
  }
示例#8
0
  @SuppressWarnings("CharUsedInArithmeticContext")
  private static void assertToStrings(Slice slice, int index) {
    // fill slice with FF
    slice.fill((byte) 0xFF);

    // set and get the value
    char[] chars = new char[(slice.length() - index) / 2];
    for (int i = 0; i < chars.length; i++) {
      chars[i] = (char) ('a' + (i % 26));
    }
    String string = new String(chars);
    Slice value = Slices.copiedBuffer(string, UTF_8);
    slice.setBytes(index, value);
    assertEquals(slice.toString(index, value.length(), UTF_8), string);

    for (int length = 0; length < value.length(); length++) {
      slice.fill((byte) 0xFF);
      slice.setBytes(index, value, 0, length);
      assertEquals(slice.toString(index, length, UTF_8), string.substring(0, length));
    }
  }
示例#9
0
  private static void assertBytesArray(Slice slice, int index) {
    // fill slice with FF
    slice.fill((byte) 0xFF);

    byte[] value = new byte[slice.length()];
    Arrays.fill(value, (byte) 0xFF);
    assertEquals(slice.getBytes(), value);

    // set and get the value
    value = new byte[(slice.length() - index) / 2];
    for (int i = 0; i < value.length; i++) {
      value[i] = (byte) i;
    }
    slice.setBytes(index, value);
    assertEquals(slice.getBytes(index, value.length), value);

    for (int length = 0; length < value.length; length++) {
      slice.fill((byte) 0xFF);
      slice.setBytes(index, value, 0, length);
      assertEquals(slice.getBytes(index, length), Arrays.copyOf(value, length));
    }
  }
示例#10
0
  private static void assertByte(Slice slice, byte index) {
    // fill slice with FF
    slice.fill((byte) 0xFF);

    // set and get unsigned value
    slice.setByte(index, 0xA5);
    assertEquals(slice.getUnsignedByte(index), 0x0000_00A5);

    // set and get the value
    slice.setByte(index, 0xA5);
    assertEquals(slice.getByte(index), (byte) 0xA5);

    try {
      slice.getByte(-1);
      fail("expected IndexOutOfBoundsException");
    } catch (IndexOutOfBoundsException e) {
    }

    try {
      slice.getByte((slice.length() - SIZE_OF_BYTE) + 1);
      fail("expected IndexOutOfBoundsException");
    } catch (IndexOutOfBoundsException e) {
    }

    try {
      slice.getByte(slice.length());
      fail("expected IndexOutOfBoundsException");
    } catch (IndexOutOfBoundsException e) {
    }

    try {
      slice.getByte(slice.length() + 1);
      fail("expected IndexOutOfBoundsException");
    } catch (IndexOutOfBoundsException e) {
    }
  }
示例#11
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) {
        }
      }
    }
  }
示例#12
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));
    }
  }