Esempio n. 1
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());
    }
  }
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 testMemoryMappedReads() throws IOException {
    Path path = Files.createTempFile("longs", null);
    ImmutableList<Long> values = createRandomLongs(20000);

    Slice output = allocate(values.size() * Longs.BYTES);
    for (int i = 0; i < values.size(); i++) {
      output.setLong(i * Longs.BYTES, values.get(i));
    }

    Files.write(path, output.getBytes());

    Slice slice = Slices.mapFileReadOnly(path.toFile());
    for (int i = 0; i < values.size(); i++) {
      long actual = slice.getLong(i * Longs.BYTES);
      long expected = values.get(i);
      assertEquals(actual, expected);
    }

    assertEquals(slice.getBytes(), output.getBytes());
  }
Esempio n. 4
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));
    }
  }
Esempio n. 5
0
  @Test
  public void testReading() {
    assertEquals(ByteOrder.nativeOrder(), ByteOrder.LITTLE_ENDIAN);

    byte[] bytes = new byte[10];
    Slice slice = Slices.wrappedBuffer(bytes);
    slice.setInt(0, 0xDEADBEEF);
    slice.setInt(4, 0xCAFEBABE);

    // little endian memory layout: EF BE AD DE BE BA FE CA 00 00
    assertEquals(
        slice.getBytes(),
        new byte[] {
          (byte) 0xEF,
          (byte) 0xBE,
          (byte) 0xAD,
          (byte) 0xDE,
          (byte) 0xBE,
          (byte) 0xBA,
          (byte) 0xFE,
          (byte) 0xCA,
          0x00,
          0x00
        });

    assertEquals(ByteArrays.getShort(bytes, 0), (short) 0xBEEF);
    assertEquals(ByteArrays.getShort(bytes, 1), (short) 0xADBE);
    assertEquals(ByteArrays.getShort(bytes, 2), (short) 0xDEAD);
    assertEquals(ByteArrays.getShort(bytes, 3), (short) 0xBEDE);

    assertEquals(ByteArrays.getInt(bytes, 0), 0xDEADBEEF);
    assertEquals(ByteArrays.getInt(bytes, 1), 0xBEDEADBE);
    assertEquals(ByteArrays.getInt(bytes, 2), 0xBABEDEAD);
    assertEquals(ByteArrays.getInt(bytes, 3), 0xFEBABEDE);
    assertEquals(ByteArrays.getInt(bytes, 4), 0xCAFEBABE);

    assertEquals(ByteArrays.getLong(bytes, 0), 0xCAFEBABE_DEADBEEFL);
    assertEquals(ByteArrays.getLong(bytes, 1), 0x00CAFEBA_BEDEADBEL);
    assertEquals(ByteArrays.getLong(bytes, 2), 0x0000CAFE_BABEDEADL);

    assertEquals(ByteArrays.getFloat(bytes, 0), intBitsToFloat(0xDEADBEEF));
    assertEquals(ByteArrays.getFloat(bytes, 1), intBitsToFloat(0xBEDEADBE));
    assertEquals(ByteArrays.getFloat(bytes, 2), intBitsToFloat(0xBABEDEAD));
    assertEquals(ByteArrays.getFloat(bytes, 3), intBitsToFloat(0xFEBABEDE));
    assertEquals(ByteArrays.getFloat(bytes, 4), intBitsToFloat(0xCAFEBABE));

    assertEquals(ByteArrays.getDouble(bytes, 0), longBitsToDouble(0xCAFEBABE_DEADBEEFL));
    assertEquals(ByteArrays.getDouble(bytes, 1), longBitsToDouble(0x00CAFEBA_BEDEADBEL));
    assertEquals(ByteArrays.getDouble(bytes, 2), longBitsToDouble(0x0000CAFE_BABEDEADL));
  }