Ejemplo n.º 1
0
 @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);
   }
 }
Ejemplo n.º 2
0
  @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));
  }
Ejemplo n.º 3
0
  @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));
  }
Ejemplo n.º 4
0
  @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));
  }
Ejemplo n.º 5
0
  @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));
  }
Ejemplo n.º 6
0
  @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));
  }
Ejemplo n.º 7
0
  @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));
  }
Ejemplo n.º 8
0
  @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));
  }
Ejemplo n.º 9
0
  @Theory
  public void shouldPutByteToBuffer(final AtomicBuffer buffer) {
    final ByteBuffer duplicateBuffer = byteBuffer(buffer);

    buffer.putByte(INDEX, BYTE_VALUE);

    assertThat(duplicateBuffer.get(INDEX), is(BYTE_VALUE));
  }
Ejemplo n.º 10
0
  @Theory
  public void shouldPutFloatToNativeBuffer(final AtomicBuffer buffer) {
    final ByteBuffer duplicateBuffer = byteBuffer(buffer);

    buffer.putFloat(INDEX, FLOAT_VALUE);

    assertThat(duplicateBuffer.getFloat(INDEX), is(FLOAT_VALUE));
  }
Ejemplo n.º 11
0
  @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));
  }
Ejemplo n.º 12
0
  @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));
  }
Ejemplo n.º 13
0
  @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));
  }
Ejemplo n.º 14
0
  @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));
  }
Ejemplo n.º 15
0
  @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));
  }
Ejemplo n.º 16
0
  @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));
  }
Ejemplo n.º 17
0
  @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));
  }
Ejemplo n.º 18
0
  @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));
  }
Ejemplo n.º 19
0
  @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));
  }
Ejemplo n.º 20
0
  @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));
  }
Ejemplo n.º 21
0
  @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));
  }
Ejemplo n.º 22
0
  @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));
  }
Ejemplo n.º 23
0
  @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));
  }
Ejemplo n.º 24
0
  @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));
  }
Ejemplo n.º 25
0
 @Theory
 @Test(expected = IndexOutOfBoundsException.class)
 public void shouldThrowExceptionForLimitAboveCapacity(final AtomicBuffer buffer) {
   final int position = BUFFER_CAPACITY + 1;
   buffer.checkLimit(position);
 }
Ejemplo n.º 26
0
 @Theory
 public void shouldGetCapacity(final AtomicBuffer buffer) {
   assertThat(buffer.capacity(), is(BUFFER_CAPACITY));
 }