/** * Tries to write into a {@link ByteBuffer} instance which is read-only. This should fail with a * {@link ReadOnlyBufferException}. * * @param directBuffer * whether the {@link ByteBuffer} instance should be a direct byte buffer or not * * @throws ReadOnlyBufferException * expected exception due to writing to a read-only buffer */ private void testByteBufferGetReadOnly(boolean directBuffer) throws ReadOnlyBufferException { MemorySegment seg = createSegment(pageSize); ByteBuffer target = (directBuffer ? ByteBuffer.allocateDirect(pageSize) : ByteBuffer.allocate(pageSize)).asReadOnlyBuffer(); seg.get(0, target, pageSize); }
segment.get(0); fail("IndexOutOfBoundsException expected");
private void testByteBufferGet(boolean directBuffer) { MemorySegment seg = createSegment(pageSize); byte[] bytes = new byte[pageSize]; random.nextBytes(bytes); seg.put(0, bytes); ByteBuffer target = directBuffer ? ByteBuffer.allocateDirect(3 * pageSize) : ByteBuffer.allocate(3 * pageSize); target.position(2 * pageSize); // transfer the segment in chunks into the byte buffer int pos = 0; while (pos < pageSize) { int len = random.nextInt(pageSize / 10); len = Math.min(len, pageSize - pos); seg.get(pos, target, len); pos += len; } // verify that we wrote the same bytes byte[] result = new byte[pageSize]; target.position(2 * pageSize); target.get(result); assertArrayEquals(bytes, result); }
private void testByteBufferPut(boolean directBuffer) { byte[] bytes = new byte[pageSize]; random.nextBytes(bytes); ByteBuffer source = directBuffer ? ByteBuffer.allocateDirect(pageSize) : ByteBuffer.allocate(pageSize); source.put(bytes); source.clear(); MemorySegment seg = createSegment(3 * pageSize); int offset = 2 * pageSize; // transfer the segment in chunks into the byte buffer int pos = 0; while (pos < pageSize) { int len = random.nextInt(pageSize / 10); len = Math.min(len, pageSize - pos); seg.put(offset + pos, source, len); pos += len; } // verify that we read the same bytes byte[] result = new byte[pageSize]; seg.get(offset, result); assertArrayEquals(bytes, result); }
private void testSlicedByteBufferGet(boolean directBuffer) { MemorySegment seg = createSegment(pageSize); byte[] bytes = new byte[pageSize]; random.nextBytes(bytes); seg.put(0, bytes); ByteBuffer target = directBuffer ? ByteBuffer.allocateDirect(pageSize + 49) : ByteBuffer.allocate(pageSize + 49); target.position(19).limit(19 + pageSize); ByteBuffer slicedTarget = target.slice(); // transfer the segment in chunks into the byte buffer int pos = 0; while (pos < pageSize) { int len = random.nextInt(pageSize / 10); len = Math.min(len, pageSize - pos); seg.get(pos, slicedTarget, len); pos += len; } // verify that we wrote the same bytes byte[] result = new byte[pageSize]; target.position(19); target.get(result); assertArrayEquals(bytes, result); }
private void testSlicedByteBufferPut(boolean directBuffer) { byte[] bytes = new byte[pageSize + 49]; random.nextBytes(bytes); ByteBuffer source = directBuffer ? ByteBuffer.allocateDirect(pageSize + 49) : ByteBuffer.allocate(pageSize + 49); source.put(bytes); source.position(19).limit(19 + pageSize); ByteBuffer slicedSource = source.slice(); MemorySegment seg = createSegment(3 * pageSize); final int offset = 2 * pageSize; // transfer the segment in chunks into the byte buffer int pos = 0; while (pos < pageSize) { int len = random.nextInt(pageSize / 10); len = Math.min(len, pageSize - pos); seg.put(offset + pos, slicedSource, len); pos += len; } // verify that we read the same bytes byte[] result = new byte[pageSize]; seg.get(offset, result); byte[] expected = Arrays.copyOfRange(bytes, 19, 19 + pageSize); assertArrayEquals(expected, result); }
@Test public void testSwapBytes() { final int halfPageSize = pageSize / 2; final byte[] bytes1 = new byte[pageSize]; final byte[] bytes2 = new byte[halfPageSize]; Arrays.fill(bytes2, (byte) 1); MemorySegment seg1 = createSegment(pageSize); MemorySegment seg2 = createSegment(halfPageSize); seg1.put(0, bytes1); seg2.put(0, bytes2); // wap the second half of the first segment with the second segment int pos = 0; while (pos < halfPageSize) { int len = random.nextInt(pageSize / 40); len = Math.min(len, halfPageSize - pos); seg1.swapBytes(new byte[len], seg2, pos + halfPageSize, pos, len); pos += len; } // the second segment should now be all zeros, the first segment should have one in its second half for (int i = 0; i < halfPageSize; i++) { assertEquals((byte) 0, seg1.get(i)); assertEquals((byte) 0, seg2.get(i)); assertEquals((byte) 1, seg1.get(i + halfPageSize)); } }
int len = random.nextInt(segmentSize / 10); len = Math.min(len, pageSize - pos); seg.get(out, pos, len); pos += len;
for (int i = 0; i < 8; i++) { random.nextBytes(expected); segment.get(i * (pageSize / 8), actual); segment.get(i * (pageSize / 16), actual, i * (pageSize / 16), pageSize / 16); segment.get(0, validation); int dataStartPos = random.nextInt(data.length - numBytes + 1); segment.get(pos, data, dataStartPos, numBytes);
private void testSwap(MemorySegment seg1, MemorySegment seg2, Random random, int smallerSize) { assertEquals(pageSize, seg1.size()); assertEquals(smallerSize, seg2.size()); final byte[] bytes1 = new byte[pageSize]; final byte[] bytes2 = new byte[smallerSize]; Arrays.fill(bytes2, (byte) 1); seg1.put(0, bytes1); seg2.put(0, bytes2); // wap the second half of the first segment with the second segment int pos = 0; while (pos < smallerSize) { int len = random.nextInt(pageSize / 40); len = Math.min(len, smallerSize - pos); seg1.swapBytes(new byte[len], seg2, pos + smallerSize, pos, len); pos += len; } // the second segment should now be all zeros, the first segment should have one in its second half for (int i = 0; i < smallerSize; i++) { assertEquals((byte) 0, seg1.get(i)); assertEquals((byte) 0, seg2.get(i)); assertEquals((byte) 1, seg1.get(i + smallerSize)); } }
private static void validateBuffer(BufferOrEvent boe, int expectedSize, int expectedChannelIndex) { assertEquals("wrong channel index", expectedChannelIndex, boe.getChannelIndex()); assertTrue("is not buffer", boe.isBuffer()); Buffer buf = boe.getBuffer(); assertEquals("wrong buffer size", expectedSize, buf.getSize()); MemorySegment seg = buf.getMemorySegment(); for (int i = 0; i < expectedSize; i++) { byte expected = (byte) i; if (expected != seg.get(i)) { fail(String.format( "wrong buffer contents at position %s : expected=%d , found=%d", i, expected, seg.get(i))); } } }
segment.get(-1); fail("IndexOutOfBoundsException expected"); segment.get(pageSize); fail("IndexOutOfBoundsException expected"); segment.get(Integer.MAX_VALUE); fail("IndexOutOfBoundsException expected"); segment.get(Integer.MIN_VALUE); fail("IndexOutOfBoundsException expected"); assertEquals((byte) random.nextInt(), segment.get(i)); assertEquals((byte) random.nextInt(), segment.get(pos));
seg.get(out, -1, segmentSize / 2); fail("IndexOutOfBoundsException expected"); seg.get(out, segmentSize, segmentSize / 2); fail("IndexOutOfBoundsException expected"); seg.get(out, -segmentSize, segmentSize / 2); fail("IndexOutOfBoundsException expected"); seg.get(out, Integer.MIN_VALUE, segmentSize / 2); fail("IndexOutOfBoundsException expected"); seg.get(out, Integer.MAX_VALUE, segmentSize / 2); fail("IndexOutOfBoundsException expected");
private static void validateBuffer(BufferOrEvent boe, int expectedSize, int expectedChannelIndex) { assertEquals("wrong channel index", expectedChannelIndex, boe.getChannelIndex()); assertTrue("is not buffer", boe.isBuffer()); Buffer buf = boe.getBuffer(); assertEquals("wrong buffer size", expectedSize, buf.getSize()); MemorySegment seg = buf.getMemorySegment(); for (int i = 0; i < expectedSize; i++) { assertEquals("wrong buffer contents", (byte) i, seg.get(i)); } } }
seg2.get(0, actual); assertArrayEquals(expected, actual);