Search in sources :

Example 81 with Buffer

use of io.vertx.core.buffer.Buffer in project vert.x by eclipse.

the class BufferTest method testGetByte.

@Test
public void testGetByte() throws Exception {
    int bytesLen = 100;
    byte[] bytes = TestUtils.randomByteArray(bytesLen);
    Buffer b = Buffer.buffer(bytes);
    for (int i = 0; i < bytesLen; i++) {
        assertEquals(bytes[i], b.getByte(i));
    }
}
Also used : Buffer(io.vertx.core.buffer.Buffer) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 82 with Buffer

use of io.vertx.core.buffer.Buffer in project vert.x by eclipse.

the class BufferTest method testAppendBufferWithOffsetAndLen.

@Test
public void testAppendBufferWithOffsetAndLen() throws Exception {
    int bytesLen = 100;
    byte[] bytes = TestUtils.randomByteArray(bytesLen);
    Buffer src = Buffer.buffer(bytes);
    int len = bytesLen - 2;
    Buffer b = Buffer.buffer();
    b.appendBuffer(src, 1, len);
    assertEquals(b.length(), len);
    byte[] copy = new byte[len];
    System.arraycopy(bytes, 1, copy, 0, len);
    assertTrue(TestUtils.byteArraysEqual(copy, b.getBytes()));
    b.appendBuffer(src, 1, len);
    assertEquals(b.length(), 2 * len);
    assertNullPointerException(() -> b.appendBuffer(null, 1, len));
}
Also used : Buffer(io.vertx.core.buffer.Buffer) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 83 with Buffer

use of io.vertx.core.buffer.Buffer in project vert.x by eclipse.

the class BufferTest method testGetOutOfBounds.

@Test
public void testGetOutOfBounds() throws Exception {
    int bytesLen = 100;
    byte[] bytes = TestUtils.randomByteArray(bytesLen);
    Buffer b = Buffer.buffer(bytes);
    assertIndexOutOfBoundsException(() -> b.getByte(bytesLen));
    assertIndexOutOfBoundsException(() -> b.getByte(bytesLen + 1));
    assertIndexOutOfBoundsException(() -> b.getByte(bytesLen + 100));
    assertIndexOutOfBoundsException(() -> b.getByte(-1));
    assertIndexOutOfBoundsException(() -> b.getByte(-100));
    assertIndexOutOfBoundsException(() -> b.getInt(bytesLen));
    assertIndexOutOfBoundsException(() -> b.getInt(bytesLen + 1));
    assertIndexOutOfBoundsException(() -> b.getInt(bytesLen + 100));
    assertIndexOutOfBoundsException(() -> b.getInt(-1));
    assertIndexOutOfBoundsException(() -> b.getInt(-100));
    assertIndexOutOfBoundsException(() -> b.getLong(bytesLen));
    assertIndexOutOfBoundsException(() -> b.getLong(bytesLen + 1));
    assertIndexOutOfBoundsException(() -> b.getLong(bytesLen + 100));
    assertIndexOutOfBoundsException(() -> b.getLong(-1));
    assertIndexOutOfBoundsException(() -> b.getLong(-100));
    assertIndexOutOfBoundsException(() -> b.getFloat(bytesLen));
    assertIndexOutOfBoundsException(() -> b.getFloat(bytesLen + 1));
    assertIndexOutOfBoundsException(() -> b.getFloat(bytesLen + 100));
    assertIndexOutOfBoundsException(() -> b.getFloat(-1));
    assertIndexOutOfBoundsException(() -> b.getFloat(-100));
    assertIndexOutOfBoundsException(() -> b.getDouble(bytesLen));
    assertIndexOutOfBoundsException(() -> b.getDouble(bytesLen + 1));
    assertIndexOutOfBoundsException(() -> b.getDouble(bytesLen + 100));
    assertIndexOutOfBoundsException(() -> b.getDouble(-1));
    assertIndexOutOfBoundsException(() -> b.getDouble(-100));
    assertIndexOutOfBoundsException(() -> b.getShort(bytesLen));
    assertIndexOutOfBoundsException(() -> b.getShort(bytesLen + 1));
    assertIndexOutOfBoundsException(() -> b.getShort(bytesLen + 100));
    assertIndexOutOfBoundsException(() -> b.getShort(-1));
    assertIndexOutOfBoundsException(() -> b.getShort(-100));
    assertIndexOutOfBoundsException(() -> b.getBytes(bytesLen + 1, bytesLen + 1));
    assertIndexOutOfBoundsException(() -> b.getBytes(bytesLen + 100, bytesLen + 100));
    assertIndexOutOfBoundsException(() -> b.getBytes(-1, -1));
    assertIndexOutOfBoundsException(() -> b.getBytes(-100, -100));
    assertIndexOutOfBoundsException(() -> b.getString(-1, bytesLen));
    assertIndexOutOfBoundsException(() -> b.getString(0, bytesLen + 1));
    assertIllegalArgumentException(() -> b.getString(2, 1));
    assertIndexOutOfBoundsException(() -> b.getString(-1, bytesLen, "UTF-8"));
    assertIndexOutOfBoundsException(() -> b.getString(0, bytesLen + 1, "UTF-8"));
    assertIllegalArgumentException(() -> b.getString(2, 1, "UTF-8"));
}
Also used : Buffer(io.vertx.core.buffer.Buffer) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 84 with Buffer

use of io.vertx.core.buffer.Buffer in project vert.x by eclipse.

the class BufferTest method testAppendShort.

@Test
public void testAppendShort() {
    Buffer b = Buffer.buffer(TestUtils.randomByteArray(100));
    b.appendShort(Short.MAX_VALUE);
    assertEquals(102, b.length());
    b.appendShortLE(Short.MAX_VALUE);
    assertEquals(104, b.length());
}
Also used : Buffer(io.vertx.core.buffer.Buffer) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 85 with Buffer

use of io.vertx.core.buffer.Buffer in project vert.x by eclipse.

the class BufferTest method testToJsonArray.

@Test
public void testToJsonArray() throws Exception {
    JsonArray arr = new JsonArray();
    arr.add("wibble");
    arr.add(5);
    arr.add(true);
    Buffer buff = Buffer.buffer(arr.encode());
    assertEquals(arr, buff.toJsonArray());
    buff = Buffer.buffer(TestUtils.randomAlphaString(10));
    try {
        buff.toJsonObject();
        fail();
    } catch (DecodeException ignore) {
    }
}
Also used : JsonArray(io.vertx.core.json.JsonArray) Buffer(io.vertx.core.buffer.Buffer) ByteBuffer(java.nio.ByteBuffer) DecodeException(io.vertx.core.json.DecodeException) Test(org.junit.Test)

Aggregations

Buffer (io.vertx.core.buffer.Buffer)1052 Test (org.junit.Test)604 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)256 Future (io.vertx.core.Future)253 AtomicReference (java.util.concurrent.atomic.AtomicReference)237 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)235 Handler (io.vertx.core.Handler)230 Vertx (io.vertx.core.Vertx)185 TestUtils (io.vertx.test.core.TestUtils)177 JsonObject (io.vertx.core.json.JsonObject)172 TimeUnit (java.util.concurrent.TimeUnit)161 CountDownLatch (java.util.concurrent.CountDownLatch)153 List (java.util.List)152 Consumer (java.util.function.Consumer)149 Function (java.util.function.Function)142 ReadStream (io.vertx.core.streams.ReadStream)139 Context (io.vertx.core.Context)135 ArrayList (java.util.ArrayList)132 MultiMap (io.vertx.core.MultiMap)130 Assume (org.junit.Assume)130