@Test public void shouldVerifyBufferAlignment() { final AtomicBuffer buffer = new UnsafeBuffer(ByteBuffer.allocateDirect(1024)); try { buffer.verifyAlignment(); } catch (final IllegalStateException ex) { fail("All buffers should be aligned " + ex); } }
@Theory public void shouldAddIntOrderedToNativeBuffer(final AtomicBuffer buffer) { final ByteBuffer duplicateBuffer = byteBuffer(buffer); duplicateBuffer.order(ByteOrder.nativeOrder()); final int initialValue = 7; final int increment = 9; buffer.putIntOrdered(INDEX, initialValue); buffer.addIntOrdered(INDEX, increment); assertThat(duplicateBuffer.getInt(INDEX), is(initialValue + increment)); }
@Theory public void shouldAddLongOrderedToNativeBuffer(final AtomicBuffer buffer) { final ByteBuffer duplicateBuffer = byteBuffer(buffer); duplicateBuffer.order(ByteOrder.nativeOrder()); final long initialValue = Integer.MAX_VALUE + 7L; final long increment = 9L; buffer.putLongOrdered(INDEX, initialValue); buffer.addLongOrdered(INDEX, increment); assertThat(duplicateBuffer.getLong(INDEX), is(initialValue + increment)); }
@Theory public void shouldGetByteArrayFromBuffer(final AtomicBuffer buffer) { final byte[] testArray = {'H', 'e', 'l', 'l', 'o'}; int i = INDEX; for (final byte v : testArray) { buffer.putByte(i, v); i += BitUtil.SIZE_OF_BYTE; } final byte[] result = new byte[testArray.length]; buffer.getBytes(INDEX, result); assertThat(result, is(testArray)); }
@Theory public void shouldGetFloatFromBuffer(final AtomicBuffer buffer) { final ByteBuffer duplicateBuffer = byteBuffer(buffer); duplicateBuffer.putFloat(INDEX, FLOAT_VALUE); assertThat(buffer.getFloat(INDEX, BYTE_ORDER), is(FLOAT_VALUE)); }
@Theory public void shouldPutLongToBuffer(final AtomicBuffer buffer) { final ByteBuffer duplicateBuffer = byteBuffer(buffer); buffer.putLong(INDEX, LONG_VALUE, BYTE_ORDER); assertThat(duplicateBuffer.getLong(INDEX), is(LONG_VALUE)); }
@Theory public void shouldPutDoubleToBuffer(final AtomicBuffer buffer) { final ByteBuffer duplicateBuffer = byteBuffer(buffer); buffer.putDouble(INDEX, DOUBLE_VALUE, BYTE_ORDER); assertThat(duplicateBuffer.getDouble(INDEX), is(DOUBLE_VALUE)); }
@Theory public void shouldPutShortToBuffer(final AtomicBuffer buffer) { final ByteBuffer duplicateBuffer = byteBuffer(buffer); buffer.putShort(INDEX, SHORT_VALUE, BYTE_ORDER); assertThat(duplicateBuffer.getShort(INDEX), is(SHORT_VALUE)); }
@Theory public void shouldPutByteToBuffer(final AtomicBuffer buffer) { final ByteBuffer duplicateBuffer = byteBuffer(buffer); buffer.putByte(INDEX, BYTE_VALUE); assertThat(duplicateBuffer.get(INDEX), is(BYTE_VALUE)); }
@Theory public void shouldPutFloatToNativeBuffer(final AtomicBuffer buffer) { final ByteBuffer duplicateBuffer = byteBuffer(buffer); buffer.putFloat(INDEX, FLOAT_VALUE); assertThat(duplicateBuffer.getFloat(INDEX), is(FLOAT_VALUE)); }
@Theory public void shouldPutLongToNativeBuffer(final AtomicBuffer buffer) { final ByteBuffer duplicateBuffer = byteBuffer(buffer); duplicateBuffer.order(ByteOrder.nativeOrder()); buffer.putLong(INDEX, LONG_VALUE); assertThat(duplicateBuffer.getLong(INDEX), is(LONG_VALUE)); }
@Theory public void shouldPutDoubleToNativeBuffer(final AtomicBuffer buffer) { final ByteBuffer duplicateBuffer = byteBuffer(buffer); duplicateBuffer.order(ByteOrder.nativeOrder()); buffer.putDouble(INDEX, DOUBLE_VALUE); assertThat(duplicateBuffer.getDouble(INDEX), is(DOUBLE_VALUE)); }
@Theory public void shouldPutShortVolatileToNativeBuffer(final AtomicBuffer buffer) { final ByteBuffer duplicateBuffer = byteBuffer(buffer); duplicateBuffer.order(ByteOrder.nativeOrder()); buffer.putShortVolatile(INDEX, SHORT_VALUE); assertThat(duplicateBuffer.getShort(INDEX), is(SHORT_VALUE)); }
@Theory public void shouldGetIntVolatileFromNativeBuffer(final AtomicBuffer buffer) { final ByteBuffer duplicateBuffer = byteBuffer(buffer); duplicateBuffer.order(ByteOrder.nativeOrder()); duplicateBuffer.putInt(INDEX, INT_VALUE); assertThat(buffer.getIntVolatile(INDEX), is(INT_VALUE)); }
@Theory public void shouldPutIntOrderedToNativeBuffer(final AtomicBuffer buffer) { final ByteBuffer duplicateBuffer = byteBuffer(buffer); duplicateBuffer.order(ByteOrder.nativeOrder()); buffer.putIntOrdered(INDEX, INT_VALUE); assertThat(duplicateBuffer.getInt(INDEX), is(INT_VALUE)); }
@Theory public void shouldCompareAndSetIntToNativeBuffer(final AtomicBuffer buffer) { final ByteBuffer duplicateBuffer = byteBuffer(buffer); duplicateBuffer.order(ByteOrder.nativeOrder()); duplicateBuffer.putInt(INDEX, INT_VALUE); assertTrue(buffer.compareAndSetInt(INDEX, INT_VALUE, INT_VALUE + 1)); assertThat(duplicateBuffer.getInt(INDEX), is(INT_VALUE + 1)); }
@Theory public void shouldCopyMemory(final AtomicBuffer buffer) { final byte[] testBytes = "xxxxxxxxxxx".getBytes(); buffer.setMemory(0, testBytes.length, (byte) 'x'); final ByteBuffer duplicateBuffer = byteBuffer(buffer); final byte[] buff = new byte[testBytes.length]; duplicateBuffer.get(buff); assertThat(buff, is(testBytes)); }
@Theory public void shouldGetAndAddLongToNativeBuffer(final AtomicBuffer buffer) { final ByteBuffer duplicateBuffer = byteBuffer(buffer); duplicateBuffer.order(ByteOrder.nativeOrder()); duplicateBuffer.putLong(INDEX, LONG_VALUE); final long delta = 1; final long beforeValue = buffer.getAndAddLong(INDEX, delta); assertThat(beforeValue, is(LONG_VALUE)); assertThat(duplicateBuffer.getLong(INDEX), is(LONG_VALUE + delta)); }
@Theory public void shouldPutBytesToBuffer(final AtomicBuffer buffer) { final byte[] testBytes = "Hello World".getBytes(); buffer.putBytes(INDEX, testBytes); final ByteBuffer duplicateBuffer = byteBuffer(buffer); duplicateBuffer.position(INDEX); final byte[] buff = new byte[testBytes.length]; duplicateBuffer.get(buff); assertThat(buff, is(testBytes)); }
@Theory public void shouldGetBytesFromBufferToBuffer(final AtomicBuffer buffer) { final byte[] testBytes = "Hello World".getBytes(); final ByteBuffer duplicateBuffer = byteBuffer(buffer); duplicateBuffer.position(INDEX); duplicateBuffer.put(testBytes); final ByteBuffer dstBuffer = ByteBuffer.allocate(testBytes.length); buffer.getBytes(INDEX, dstBuffer, testBytes.length); assertThat(dstBuffer.array(), is(testBytes)); }
@Theory public void shouldGetAndAddIntToNativeBuffer(final AtomicBuffer buffer) { final ByteBuffer duplicateBuffer = byteBuffer(buffer); duplicateBuffer.order(ByteOrder.nativeOrder()); duplicateBuffer.putInt(INDEX, INT_VALUE); final int delta = 1; final int beforeValue = buffer.getAndAddInt(INDEX, delta); assertThat(beforeValue, is(INT_VALUE)); assertThat(duplicateBuffer.getInt(INDEX), is(INT_VALUE + delta)); }
@Theory public void shouldPutBytesToBufferFromSlice(final AtomicBuffer buffer) { final byte[] testBytes = "Hello World".getBytes(); final ByteBuffer srcBuffer = ((ByteBuffer) ByteBuffer.allocate(testBytes.length * 2).position(testBytes.length)).slice(); srcBuffer.put(testBytes).flip(); buffer.putBytes(INDEX, srcBuffer, testBytes.length); final ByteBuffer duplicateBuffer = byteBuffer(buffer); duplicateBuffer.position(INDEX); final byte[] buff = new byte[testBytes.length]; duplicateBuffer.get(buff); assertThat(buff, is(testBytes)); }
@Theory public void shouldGetBytesFromBufferToAtomicBuffer(final AtomicBuffer buffer) { final byte[] testBytes = "Hello World".getBytes(); final ByteBuffer duplicateBuffer = byteBuffer(buffer); duplicateBuffer.position(INDEX); duplicateBuffer.put(testBytes); final ByteBuffer dstBuffer = ByteBuffer.allocateDirect(testBytes.length); buffer.getBytes(INDEX, dstBuffer, testBytes.length); dstBuffer.flip(); final byte[] result = new byte[testBytes.length]; dstBuffer.get(result); assertThat(result, is(testBytes)); }
@Theory public void shouldPutBytesToAtomicBufferFromAtomicBuffer(final AtomicBuffer buffer) { final byte[] testBytes = "Hello World".getBytes(); final ByteBuffer srcBuffer = ByteBuffer.allocateDirect(testBytes.length); srcBuffer.put(testBytes).flip(); final UnsafeBuffer srcUnsafeBuffer = new UnsafeBuffer(srcBuffer); buffer.putBytes(INDEX, srcUnsafeBuffer, 0, testBytes.length); final ByteBuffer duplicateBuffer = byteBuffer(buffer); duplicateBuffer.position(INDEX); final byte[] buff = new byte[testBytes.length]; duplicateBuffer.get(buff); assertThat(buff, is(testBytes)); }
@Theory @Test(expected = IndexOutOfBoundsException.class) public void shouldThrowExceptionForLimitAboveCapacity(final AtomicBuffer buffer) { final int position = BUFFER_CAPACITY + 1; buffer.checkLimit(position); }
@Theory public void shouldGetCapacity(final AtomicBuffer buffer) { assertThat(buffer.capacity(), is(BUFFER_CAPACITY)); }