2020-08-28 14:02:51 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2020 The Netty Project
|
|
|
|
*
|
|
|
|
* The Netty Project licenses this file to you under the Apache License,
|
|
|
|
* version 2.0 (the "License"); you may not use this file except in compliance
|
|
|
|
* with the License. You may obtain a copy of the License at:
|
|
|
|
*
|
2020-10-28 14:38:14 +01:00
|
|
|
* https://www.apache.org/licenses/LICENSE-2.0
|
2020-08-28 14:02:51 +02:00
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*/
|
2020-11-17 15:38:11 +01:00
|
|
|
package io.netty.buffer.api;
|
2020-07-24 19:38:48 +02:00
|
|
|
|
2020-11-17 15:38:11 +01:00
|
|
|
import io.netty.buffer.api.Fixture.Properties;
|
2020-11-17 15:53:40 +01:00
|
|
|
import io.netty.buffer.api.memseg.NativeMemorySegmentManager;
|
2020-11-11 23:00:08 +01:00
|
|
|
import org.junit.jupiter.api.AfterAll;
|
|
|
|
import org.junit.jupiter.api.BeforeAll;
|
2020-11-10 12:09:14 +01:00
|
|
|
import org.junit.jupiter.api.Disabled;
|
|
|
|
import org.junit.jupiter.api.Nested;
|
2020-11-16 18:00:32 +01:00
|
|
|
import org.junit.jupiter.api.Test;
|
2020-11-10 12:09:14 +01:00
|
|
|
import org.junit.jupiter.api.parallel.Isolated;
|
|
|
|
import org.junit.jupiter.params.ParameterizedTest;
|
|
|
|
import org.junit.jupiter.params.provider.MethodSource;
|
2020-07-24 19:38:48 +02:00
|
|
|
|
2020-11-11 23:00:08 +01:00
|
|
|
import java.io.IOException;
|
2020-10-30 14:39:50 +01:00
|
|
|
import java.nio.ByteBuffer;
|
2020-10-06 16:30:49 +02:00
|
|
|
import java.nio.ByteOrder;
|
2020-11-11 23:00:08 +01:00
|
|
|
import java.text.ParseException;
|
|
|
|
import java.util.Arrays;
|
2020-11-10 12:09:14 +01:00
|
|
|
import java.util.List;
|
2020-11-05 15:15:34 +01:00
|
|
|
import java.util.NoSuchElementException;
|
2020-07-24 19:38:48 +02:00
|
|
|
import java.util.concurrent.ArrayBlockingQueue;
|
|
|
|
import java.util.concurrent.ExecutorService;
|
|
|
|
import java.util.concurrent.Executors;
|
|
|
|
import java.util.concurrent.Future;
|
|
|
|
import java.util.concurrent.SynchronousQueue;
|
2020-10-30 14:39:50 +01:00
|
|
|
import java.util.function.Function;
|
2020-11-10 12:09:14 +01:00
|
|
|
import java.util.stream.Stream;
|
|
|
|
import java.util.stream.Stream.Builder;
|
|
|
|
|
2020-11-18 17:16:37 +01:00
|
|
|
import static io.netty.buffer.api.Fixture.Properties.CLEANER;
|
|
|
|
import static io.netty.buffer.api.Fixture.Properties.COMPOSITE;
|
|
|
|
import static io.netty.buffer.api.Fixture.Properties.DIRECT;
|
|
|
|
import static io.netty.buffer.api.Fixture.Properties.HEAP;
|
|
|
|
import static io.netty.buffer.api.Fixture.Properties.POOLED;
|
2020-11-10 12:09:14 +01:00
|
|
|
import static org.assertj.core.api.Assertions.assertThat;
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
|
|
import static org.junit.jupiter.api.Assertions.fail;
|
|
|
|
|
|
|
|
public class BufTest {
|
2020-11-11 23:00:08 +01:00
|
|
|
private static volatile Fixture[] fixtures;
|
|
|
|
private static ExecutorService executor;
|
|
|
|
|
|
|
|
static Fixture[] allocators() {
|
|
|
|
Fixture[] fxs = fixtures;
|
|
|
|
if (fxs != null) {
|
|
|
|
return fxs;
|
|
|
|
}
|
|
|
|
return fixtures = fixtureCombinations().toArray(Fixture[]::new);
|
|
|
|
}
|
|
|
|
|
2020-11-12 14:55:42 +01:00
|
|
|
static Stream<Fixture> nonSliceAllocators() {
|
|
|
|
return fixtureCombinations().filter(f -> !f.isSlice());
|
|
|
|
}
|
|
|
|
|
2020-11-11 23:00:08 +01:00
|
|
|
static Stream<Fixture> heapAllocators() {
|
|
|
|
return fixtureCombinations().filter(Fixture::isHeap);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Stream<Fixture> directAllocators() {
|
|
|
|
return fixtureCombinations().filter(Fixture::isDirect);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Stream<Fixture> directWithCleanerAllocators() {
|
|
|
|
return fixtureCombinations().filter(f -> f.isDirect() && f.isCleaner());
|
|
|
|
}
|
|
|
|
|
|
|
|
static Stream<Fixture> directPooledWithCleanerAllocators() {
|
|
|
|
return fixtureCombinations().filter(f -> f.isDirect() && f.isCleaner() && f.isPooled());
|
|
|
|
}
|
|
|
|
|
|
|
|
private static Stream<Fixture> fixtureCombinations() {
|
|
|
|
Fixture[] fxs = fixtures;
|
|
|
|
if (fxs != null) {
|
|
|
|
return Arrays.stream(fxs);
|
|
|
|
}
|
2020-11-10 12:09:14 +01:00
|
|
|
List<Fixture> initFixtures = List.of(
|
2020-11-18 17:16:37 +01:00
|
|
|
new Fixture("heap", Allocator::heap, HEAP),
|
|
|
|
new Fixture("direct", Allocator::direct, DIRECT),
|
|
|
|
new Fixture("directWithCleaner", Allocator::directWithCleaner, DIRECT, CLEANER),
|
|
|
|
new Fixture("pooledHeap", Allocator::pooledHeap, POOLED, HEAP),
|
|
|
|
new Fixture("pooledDirect", Allocator::pooledDirect, POOLED, DIRECT),
|
|
|
|
new Fixture("pooledDirectWithCleaner", Allocator::pooledDirectWithCleaner, POOLED, DIRECT, CLEANER));
|
2020-11-10 12:09:14 +01:00
|
|
|
Builder<Fixture> builder = Stream.builder();
|
|
|
|
initFixtures.forEach(builder);
|
|
|
|
|
|
|
|
// Add 2-way composite buffers of all combinations.
|
|
|
|
for (Fixture first : initFixtures) {
|
|
|
|
for (Fixture second : initFixtures) {
|
|
|
|
var a = first.get();
|
|
|
|
var b = second.get();
|
|
|
|
builder.add(new Fixture("compose(" + first + ", " + second + ')', () -> {
|
|
|
|
return new Allocator() {
|
|
|
|
@Override
|
|
|
|
public Buf allocate(int size) {
|
|
|
|
int half = size / 2;
|
|
|
|
try (Buf firstHalf = a.allocate(half);
|
|
|
|
Buf secondHalf = b.allocate(size - half)) {
|
2020-11-16 18:00:32 +01:00
|
|
|
return a.compose(firstHalf, secondHalf);
|
2020-11-10 12:09:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void close() {
|
|
|
|
a.close();
|
|
|
|
b.close();
|
|
|
|
}
|
|
|
|
};
|
2020-11-18 17:16:37 +01:00
|
|
|
}, COMPOSITE));
|
2020-07-24 19:38:48 +02:00
|
|
|
}
|
|
|
|
}
|
2020-11-10 12:09:14 +01:00
|
|
|
|
|
|
|
// Also add a 3-way composite buffer.
|
|
|
|
builder.add(new Fixture("compose(heap,heap,heap)", () -> {
|
|
|
|
return new Allocator() {
|
|
|
|
final Allocator alloc = Allocator.heap();
|
|
|
|
@Override
|
|
|
|
public Buf allocate(int size) {
|
|
|
|
int part = size / 3;
|
|
|
|
try (Buf a = alloc.allocate(part);
|
|
|
|
Buf b = alloc.allocate(part);
|
|
|
|
Buf c = alloc.allocate(size - part * 2)) {
|
2020-11-16 18:00:32 +01:00
|
|
|
return alloc.compose(a, b, c);
|
2020-11-10 12:09:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void close() {
|
|
|
|
alloc.close();
|
|
|
|
}
|
|
|
|
};
|
2020-11-18 17:16:37 +01:00
|
|
|
}, COMPOSITE));
|
2020-11-16 19:27:21 +01:00
|
|
|
|
|
|
|
for (Fixture fixture : initFixtures) {
|
|
|
|
builder.add(new Fixture(fixture + ".ensureWritable", () -> {
|
|
|
|
var allocator = fixture.createAllocator();
|
|
|
|
return new Allocator() {
|
|
|
|
@Override
|
|
|
|
public Buf allocate(int size) {
|
|
|
|
if (size < 2) {
|
|
|
|
return allocator.allocate(size);
|
|
|
|
}
|
|
|
|
var buf = allocator.allocate(size - 1);
|
|
|
|
buf.ensureWritable(size);
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void close() {
|
|
|
|
allocator.close();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}, fixture.getProperties()));
|
|
|
|
builder.add(new Fixture(fixture + ".compose.ensureWritable", () -> {
|
|
|
|
var allocator = fixture.createAllocator();
|
|
|
|
return new Allocator() {
|
|
|
|
@Override
|
|
|
|
public Buf allocate(int size) {
|
|
|
|
if (size < 2) {
|
|
|
|
return allocator.allocate(size);
|
|
|
|
}
|
|
|
|
var buf = allocator.compose();
|
|
|
|
buf.ensureWritable(size);
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void close() {
|
|
|
|
allocator.close();
|
|
|
|
}
|
|
|
|
};
|
2020-11-18 17:16:37 +01:00
|
|
|
}, COMPOSITE));
|
2020-11-16 19:27:21 +01:00
|
|
|
}
|
|
|
|
|
2020-11-12 14:55:42 +01:00
|
|
|
return builder.build().flatMap(f -> {
|
|
|
|
// Inject slice versions of everything
|
|
|
|
Builder<Fixture> andSlices = Stream.builder();
|
|
|
|
andSlices.add(f);
|
|
|
|
andSlices.add(new Fixture(f + ".slice(0, capacity())", () -> {
|
|
|
|
var allocatorBase = f.get();
|
|
|
|
return new Allocator() {
|
|
|
|
@Override
|
|
|
|
public Buf allocate(int size) {
|
|
|
|
try (Buf base = allocatorBase.allocate(size)) {
|
|
|
|
return base.slice(0, base.capacity()).writerOffset(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void close() {
|
|
|
|
allocatorBase.close();
|
|
|
|
}
|
|
|
|
};
|
2020-11-17 15:38:11 +01:00
|
|
|
}, Properties.SLICE));
|
2020-11-12 14:55:42 +01:00
|
|
|
andSlices.add(new Fixture(f + ".slice(1, capacity() - 2)", () -> {
|
|
|
|
var allocatorBase = f.get();
|
|
|
|
return new Allocator() {
|
|
|
|
@Override
|
|
|
|
public Buf allocate(int size) {
|
|
|
|
try (Buf base = allocatorBase.allocate(size + 2)) {
|
|
|
|
return base.slice(1, size).writerOffset(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void close() {
|
|
|
|
allocatorBase.close();
|
|
|
|
}
|
|
|
|
};
|
2020-11-17 15:38:11 +01:00
|
|
|
}, Properties.SLICE));
|
2020-11-12 14:55:42 +01:00
|
|
|
return andSlices.build();
|
|
|
|
});
|
2020-11-10 12:09:14 +01:00
|
|
|
}
|
|
|
|
|
2020-11-11 23:00:08 +01:00
|
|
|
@BeforeAll
|
|
|
|
static void startExecutor() throws IOException, ParseException {
|
|
|
|
executor = Executors.newSingleThreadExecutor();
|
2020-11-10 12:09:14 +01:00
|
|
|
}
|
|
|
|
|
2020-11-11 23:00:08 +01:00
|
|
|
@AfterAll
|
|
|
|
static void stopExecutor() throws IOException {
|
|
|
|
executor.shutdown();
|
2020-11-10 12:09:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void allocateAndAccessingBuffer(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
buf.writeByte((byte) 1);
|
|
|
|
buf.writeByte((byte) 2);
|
|
|
|
try (Buf inner = buf.acquire()) {
|
|
|
|
inner.writeByte((byte) 3);
|
|
|
|
inner.writeByte((byte) 4);
|
|
|
|
inner.writeByte((byte) 5);
|
|
|
|
inner.writeByte((byte) 6);
|
|
|
|
inner.writeByte((byte) 7);
|
|
|
|
inner.writeByte((byte) 8);
|
|
|
|
var re = assertThrows(RuntimeException.class, () -> inner.writeByte((byte) 9));
|
|
|
|
assertThat(re).hasMessageContaining("bound");
|
|
|
|
re = assertThrows(RuntimeException.class, () -> inner.writeByte((byte) 9));
|
|
|
|
assertThat(re).hasMessageContaining("bound");
|
|
|
|
re = assertThrows(RuntimeException.class, () -> buf.writeByte((byte) 9));
|
|
|
|
assertThat(re).hasMessageContaining("bound");
|
2020-07-24 19:38:48 +02:00
|
|
|
}
|
2020-11-10 12:09:14 +01:00
|
|
|
assertEquals((byte) 1, buf.readByte());
|
|
|
|
assertEquals((byte) 2, buf.readByte());
|
|
|
|
assertEquals((byte) 3, buf.readByte());
|
|
|
|
assertEquals((byte) 4, buf.readByte());
|
|
|
|
assertEquals((byte) 5, buf.readByte());
|
|
|
|
assertEquals((byte) 6, buf.readByte());
|
|
|
|
assertEquals((byte) 7, buf.readByte());
|
|
|
|
assertEquals((byte) 8, buf.readByte());
|
|
|
|
var re = assertThrows(RuntimeException.class, buf::readByte);
|
|
|
|
assertThat(re).hasMessageContaining("bound");
|
2020-11-11 23:00:08 +01:00
|
|
|
assertThat(toByteArray(buf)).containsExactly(1, 2, 3, 4, 5, 6, 7, 8);
|
2020-11-10 12:09:14 +01:00
|
|
|
}
|
|
|
|
}
|
2020-07-24 19:38:48 +02:00
|
|
|
|
2020-11-10 12:09:14 +01:00
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void acquireOnClosedBufferMustThrow(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator()) {
|
|
|
|
var buf = allocator.allocate(8);
|
|
|
|
buf.close();
|
|
|
|
assertThrows(IllegalStateException.class, buf::acquire);
|
2020-07-24 19:38:48 +02:00
|
|
|
}
|
2020-11-10 12:09:14 +01:00
|
|
|
}
|
2020-08-28 12:17:41 +02:00
|
|
|
|
2020-11-10 12:09:14 +01:00
|
|
|
@ParameterizedTest
|
2020-11-12 14:55:42 +01:00
|
|
|
@MethodSource("nonSliceAllocators")
|
2020-11-10 12:09:14 +01:00
|
|
|
void allocateAndSendToThread(Fixture fixture) throws Exception {
|
|
|
|
try (Allocator allocator = fixture.createAllocator()) {
|
|
|
|
ArrayBlockingQueue<Send<Buf>> queue = new ArrayBlockingQueue<>(10);
|
|
|
|
Future<Byte> future = executor.submit(() -> {
|
|
|
|
try (Buf byteBuf = queue.take().receive()) {
|
|
|
|
return byteBuf.readByte();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
try (Buf buf = allocator.allocate(8)) {
|
|
|
|
buf.writeByte((byte) 42);
|
|
|
|
assertTrue(queue.offer(buf.send()));
|
|
|
|
}
|
|
|
|
|
|
|
|
assertEquals((byte) 42, future.get().byteValue());
|
|
|
|
}
|
2020-07-24 19:38:48 +02:00
|
|
|
}
|
|
|
|
|
2020-11-10 12:09:14 +01:00
|
|
|
@ParameterizedTest
|
2020-11-12 14:55:42 +01:00
|
|
|
@MethodSource("nonSliceAllocators")
|
2020-11-10 12:09:14 +01:00
|
|
|
void allocateAndSendToThreadViaSyncQueue(Fixture fixture) throws Exception {
|
2020-08-28 12:17:41 +02:00
|
|
|
SynchronousQueue<Send<Buf>> queue = new SynchronousQueue<>();
|
|
|
|
Future<Byte> future = executor.submit(() -> {
|
|
|
|
try (Buf byteBuf = queue.take().receive()) {
|
|
|
|
return byteBuf.readByte();
|
2020-07-24 19:38:48 +02:00
|
|
|
}
|
2020-08-28 12:17:41 +02:00
|
|
|
});
|
2020-07-24 19:38:48 +02:00
|
|
|
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
assertThat(buf.writeByte((byte) 42)).isSameAs(buf);
|
2020-08-28 12:17:41 +02:00
|
|
|
queue.put(buf.send());
|
2020-07-24 19:38:48 +02:00
|
|
|
}
|
2020-08-28 12:17:41 +02:00
|
|
|
|
|
|
|
assertEquals((byte) 42, future.get().byteValue());
|
2020-07-24 19:38:48 +02:00
|
|
|
}
|
2020-08-17 16:09:38 +02:00
|
|
|
|
2020-11-10 12:09:14 +01:00
|
|
|
@ParameterizedTest
|
2020-11-12 14:55:42 +01:00
|
|
|
@MethodSource("nonSliceAllocators")
|
2020-11-10 12:09:14 +01:00
|
|
|
void sendMustThrowWhenBufIsAcquired(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
2020-10-15 16:20:26 +02:00
|
|
|
try (Buf ignored = buf.acquire()) {
|
2020-11-16 18:00:32 +01:00
|
|
|
assertFalse(buf.isOwned());
|
2020-11-10 12:09:14 +01:00
|
|
|
assertThrows(IllegalStateException.class, buf::send);
|
2020-10-15 16:20:26 +02:00
|
|
|
}
|
|
|
|
// Now send() should work again.
|
2020-11-16 18:00:32 +01:00
|
|
|
assertTrue(buf.isOwned());
|
2020-10-15 16:20:26 +02:00
|
|
|
buf.send().receive().close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-10 12:09:14 +01:00
|
|
|
@ParameterizedTest
|
2020-11-12 14:55:42 +01:00
|
|
|
@MethodSource("nonSliceAllocators")
|
2020-11-10 12:09:14 +01:00
|
|
|
void mustThrowWhenAllocatingZeroSizedBuffer(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator()) {
|
|
|
|
assertThrows(IllegalArgumentException.class, () -> allocator.allocate(0));
|
2020-08-17 16:09:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-10 12:09:14 +01:00
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void mustThrowWhenAllocatingNegativeSizedBuffer(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator()) {
|
|
|
|
assertThrows(IllegalArgumentException.class, () -> allocator.allocate(-1));
|
2020-08-17 16:09:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-10 12:09:14 +01:00
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void setReaderOffsetMustThrowOnNegativeIndex(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
2020-11-11 23:00:08 +01:00
|
|
|
assertThrows(IndexOutOfBoundsException.class, () -> buf.readerOffset(-1));
|
2020-08-17 16:09:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-10 12:09:14 +01:00
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void setReaderOffsetMustThrowOnOversizedIndex(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
2020-11-11 23:00:08 +01:00
|
|
|
assertThrows(IndexOutOfBoundsException.class, () -> buf.readerOffset(1));
|
2020-11-10 12:09:14 +01:00
|
|
|
buf.writeLong(0);
|
2020-11-11 23:00:08 +01:00
|
|
|
assertThrows(IndexOutOfBoundsException.class, () -> buf.readerOffset(9));
|
2020-08-24 16:03:02 +02:00
|
|
|
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.readerOffset(8);
|
2020-11-10 12:09:14 +01:00
|
|
|
assertThrows(IndexOutOfBoundsException.class, buf::readByte);
|
2020-08-24 16:03:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-10 12:09:14 +01:00
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void setReaderOffsetMustNotThrowWithinBounds(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
2020-11-11 23:00:08 +01:00
|
|
|
assertThat(buf.readerOffset(0)).isSameAs(buf);
|
2020-11-10 12:09:14 +01:00
|
|
|
buf.writeLong(0);
|
2020-11-11 23:00:08 +01:00
|
|
|
assertThat(buf.readerOffset(7)).isSameAs(buf);
|
|
|
|
assertThat(buf.readerOffset(8)).isSameAs(buf);
|
Generate complete and tested Buf API
Motivation:
The Buf API is wide, taking into account signedness, endian-ness, and so on, for all primitive
types.
Modification:
A code generator has been added which add API, implementation, and tests, to the Buf, BBuf,
and BBufTest files respectively.
Result:
We have a complete and fully tested accessor API in the Buf interface, which is still somewhat
easy to bulk-modify using the code generator.
2020-09-10 18:05:36 +02:00
|
|
|
}
|
2020-08-24 16:03:02 +02:00
|
|
|
}
|
|
|
|
|
2020-11-10 12:09:14 +01:00
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void capacityMustBeAllocatedSize(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
assertEquals(8, buf.capacity());
|
|
|
|
try (Buf b = allocator.allocate(13)) {
|
|
|
|
assertEquals(13, b.capacity());
|
|
|
|
}
|
2020-08-28 12:17:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-10 12:09:14 +01:00
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void fill(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(16)) {
|
|
|
|
assertThat(buf.fill((byte) 0xA5)).isSameAs(buf);
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.writerOffset(16);
|
Generate complete and tested Buf API
Motivation:
The Buf API is wide, taking into account signedness, endian-ness, and so on, for all primitive
types.
Modification:
A code generator has been added which add API, implementation, and tests, to the Buf, BBuf,
and BBufTest files respectively.
Result:
We have a complete and fully tested accessor API in the Buf interface, which is still somewhat
easy to bulk-modify using the code generator.
2020-09-10 18:05:36 +02:00
|
|
|
assertEquals(0xA5A5A5A5_A5A5A5A5L, buf.readLong());
|
|
|
|
assertEquals(0xA5A5A5A5_A5A5A5A5L, buf.readLong());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-10 12:09:14 +01:00
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void readerWriterOffsetUpdates(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(22)) {
|
2020-11-11 23:00:08 +01:00
|
|
|
assertEquals(0, buf.writerOffset());
|
|
|
|
assertThat(buf.writerOffset(1)).isSameAs(buf);
|
|
|
|
assertEquals(1, buf.writerOffset());
|
2020-11-10 12:09:14 +01:00
|
|
|
assertThat(buf.writeByte((byte) 7)).isSameAs(buf);
|
2020-11-11 23:00:08 +01:00
|
|
|
assertEquals(2, buf.writerOffset());
|
2020-11-10 12:09:14 +01:00
|
|
|
assertThat(buf.writeShort((short) 3003)).isSameAs(buf);
|
2020-11-11 23:00:08 +01:00
|
|
|
assertEquals(4, buf.writerOffset());
|
2020-11-10 12:09:14 +01:00
|
|
|
assertThat(buf.writeInt(0x5A55_BA55)).isSameAs(buf);
|
2020-11-11 23:00:08 +01:00
|
|
|
assertEquals(8, buf.writerOffset());
|
2020-11-10 12:09:14 +01:00
|
|
|
assertThat(buf.writeLong(0x123456789ABCDEF0L)).isSameAs(buf);
|
2020-11-11 23:00:08 +01:00
|
|
|
assertEquals(16, buf.writerOffset());
|
Generate complete and tested Buf API
Motivation:
The Buf API is wide, taking into account signedness, endian-ness, and so on, for all primitive
types.
Modification:
A code generator has been added which add API, implementation, and tests, to the Buf, BBuf,
and BBufTest files respectively.
Result:
We have a complete and fully tested accessor API in the Buf interface, which is still somewhat
easy to bulk-modify using the code generator.
2020-09-10 18:05:36 +02:00
|
|
|
assertEquals(6, buf.writableBytes());
|
2020-08-28 12:17:41 +02:00
|
|
|
assertEquals(16, buf.readableBytes());
|
|
|
|
|
2020-11-11 23:00:08 +01:00
|
|
|
assertEquals(0, buf.readerOffset());
|
|
|
|
assertThat(buf.readerOffset(1)).isSameAs(buf);
|
|
|
|
assertEquals(1, buf.readerOffset());
|
2020-08-28 12:17:41 +02:00
|
|
|
assertEquals((byte) 7, buf.readByte());
|
2020-11-11 23:00:08 +01:00
|
|
|
assertEquals(2, buf.readerOffset());
|
2020-08-28 12:17:41 +02:00
|
|
|
assertEquals((short) 3003, buf.readShort());
|
2020-11-11 23:00:08 +01:00
|
|
|
assertEquals(4, buf.readerOffset());
|
2020-08-28 12:17:41 +02:00
|
|
|
assertEquals(0x5A55_BA55, buf.readInt());
|
2020-11-11 23:00:08 +01:00
|
|
|
assertEquals(8, buf.readerOffset());
|
2020-08-28 12:17:41 +02:00
|
|
|
assertEquals(0x123456789ABCDEF0L, buf.readLong());
|
2020-11-11 23:00:08 +01:00
|
|
|
assertEquals(16, buf.readerOffset());
|
2020-08-28 12:17:41 +02:00
|
|
|
assertEquals(0, buf.readableBytes());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-10 12:09:14 +01:00
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void readAndWriteBoundsChecksWithIndexUpdates(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
Generate complete and tested Buf API
Motivation:
The Buf API is wide, taking into account signedness, endian-ness, and so on, for all primitive
types.
Modification:
A code generator has been added which add API, implementation, and tests, to the Buf, BBuf,
and BBufTest files respectively.
Result:
We have a complete and fully tested accessor API in the Buf interface, which is still somewhat
easy to bulk-modify using the code generator.
2020-09-10 18:05:36 +02:00
|
|
|
buf.writeLong(0);
|
|
|
|
|
|
|
|
buf.readLong(); // Fine.
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.readerOffset(1);
|
2020-11-10 12:09:14 +01:00
|
|
|
assertThrows(IndexOutOfBoundsException.class, buf::readLong);
|
Generate complete and tested Buf API
Motivation:
The Buf API is wide, taking into account signedness, endian-ness, and so on, for all primitive
types.
Modification:
A code generator has been added which add API, implementation, and tests, to the Buf, BBuf,
and BBufTest files respectively.
Result:
We have a complete and fully tested accessor API in the Buf interface, which is still somewhat
easy to bulk-modify using the code generator.
2020-09-10 18:05:36 +02:00
|
|
|
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.readerOffset(4);
|
Generate complete and tested Buf API
Motivation:
The Buf API is wide, taking into account signedness, endian-ness, and so on, for all primitive
types.
Modification:
A code generator has been added which add API, implementation, and tests, to the Buf, BBuf,
and BBufTest files respectively.
Result:
We have a complete and fully tested accessor API in the Buf interface, which is still somewhat
easy to bulk-modify using the code generator.
2020-09-10 18:05:36 +02:00
|
|
|
buf.readInt(); // Fine.
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.readerOffset(5);
|
Generate complete and tested Buf API
Motivation:
The Buf API is wide, taking into account signedness, endian-ness, and so on, for all primitive
types.
Modification:
A code generator has been added which add API, implementation, and tests, to the Buf, BBuf,
and BBufTest files respectively.
Result:
We have a complete and fully tested accessor API in the Buf interface, which is still somewhat
easy to bulk-modify using the code generator.
2020-09-10 18:05:36 +02:00
|
|
|
|
2020-11-10 12:09:14 +01:00
|
|
|
assertThrows(IndexOutOfBoundsException.class, buf::readInt);
|
2020-08-28 12:17:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-10 12:09:14 +01:00
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void resetMustSetReaderAndWriterOffsetsToTheirInitialPositions(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
2020-10-30 14:39:50 +01:00
|
|
|
buf.writeInt(0).readShort();
|
|
|
|
buf.reset();
|
2020-11-11 23:00:08 +01:00
|
|
|
assertEquals(0, buf.readerOffset());
|
|
|
|
assertEquals(0, buf.writerOffset());
|
2020-10-30 14:39:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-10 12:09:14 +01:00
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void sliceWithoutOffsetAndSizeMustReturnReadableRegion(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
2020-10-15 16:20:26 +02:00
|
|
|
for (byte b : new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 }) {
|
|
|
|
buf.writeByte(b);
|
|
|
|
}
|
|
|
|
assertEquals(0x01, buf.readByte());
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.writerOffset(buf.writerOffset() - 1);
|
2020-10-15 16:20:26 +02:00
|
|
|
try (Buf slice = buf.slice()) {
|
2020-11-11 23:00:08 +01:00
|
|
|
assertThat(toByteArray(slice)).containsExactly(0x02, 0x03, 0x04, 0x05, 0x06, 0x07);
|
|
|
|
assertEquals(0, slice.readerOffset());
|
2020-10-15 16:20:26 +02:00
|
|
|
assertEquals(6, slice.readableBytes());
|
2020-11-11 23:00:08 +01:00
|
|
|
assertEquals(6, slice.writerOffset());
|
2020-10-15 16:20:26 +02:00
|
|
|
assertEquals(6, slice.capacity());
|
|
|
|
assertEquals(0x02, slice.readByte());
|
|
|
|
assertEquals(0x03, slice.readByte());
|
|
|
|
assertEquals(0x04, slice.readByte());
|
|
|
|
assertEquals(0x05, slice.readByte());
|
|
|
|
assertEquals(0x06, slice.readByte());
|
|
|
|
assertEquals(0x07, slice.readByte());
|
2020-11-10 12:09:14 +01:00
|
|
|
assertThrows(IndexOutOfBoundsException.class, slice::readByte);
|
2020-10-15 16:20:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-10 12:09:14 +01:00
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void sliceWithOffsetAndSizeMustReturnGivenRegion(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
2020-10-15 16:20:26 +02:00
|
|
|
for (byte b : new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 }) {
|
|
|
|
buf.writeByte(b);
|
|
|
|
}
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.readerOffset(3); // Reader and writer offsets must be ignored.
|
|
|
|
buf.writerOffset(6);
|
2020-10-15 16:20:26 +02:00
|
|
|
try (Buf slice = buf.slice(1, 6)) {
|
2020-11-11 23:00:08 +01:00
|
|
|
assertThat(toByteArray(slice)).containsExactly(0x02, 0x03, 0x04, 0x05, 0x06, 0x07);
|
|
|
|
assertEquals(0, slice.readerOffset());
|
2020-10-15 16:20:26 +02:00
|
|
|
assertEquals(6, slice.readableBytes());
|
2020-11-11 23:00:08 +01:00
|
|
|
assertEquals(6, slice.writerOffset());
|
2020-10-15 16:20:26 +02:00
|
|
|
assertEquals(6, slice.capacity());
|
|
|
|
assertEquals(0x02, slice.readByte());
|
|
|
|
assertEquals(0x03, slice.readByte());
|
|
|
|
assertEquals(0x04, slice.readByte());
|
|
|
|
assertEquals(0x05, slice.readByte());
|
|
|
|
assertEquals(0x06, slice.readByte());
|
|
|
|
assertEquals(0x07, slice.readByte());
|
2020-11-10 12:09:14 +01:00
|
|
|
assertThrows(IndexOutOfBoundsException.class, slice::readByte);
|
2020-10-15 16:20:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-10 12:09:14 +01:00
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void sliceWithoutOffsetAndSizeWillIncreaseReferenceCount(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
2020-11-12 14:55:42 +01:00
|
|
|
int borrows = buf.countBorrows();
|
2020-10-15 16:20:26 +02:00
|
|
|
try (Buf ignored = buf.slice()) {
|
2020-11-16 18:00:32 +01:00
|
|
|
assertFalse(buf.isOwned());
|
2020-11-10 12:09:14 +01:00
|
|
|
assertThrows(IllegalStateException.class, buf::send);
|
2020-10-15 16:20:26 +02:00
|
|
|
}
|
2020-11-12 14:55:42 +01:00
|
|
|
assertEquals(borrows, buf.countBorrows());
|
2020-10-15 16:20:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-10 12:09:14 +01:00
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void sliceWithOffsetAndSizeWillIncreaseReferenceCount(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
2020-11-12 14:55:42 +01:00
|
|
|
int borrows = buf.countBorrows();
|
2020-10-15 16:20:26 +02:00
|
|
|
try (Buf ignored = buf.slice(0, 8)) {
|
2020-11-16 18:00:32 +01:00
|
|
|
assertFalse(buf.isOwned());
|
2020-11-10 12:09:14 +01:00
|
|
|
assertThrows(IllegalStateException.class, buf::send);
|
2020-10-15 16:20:26 +02:00
|
|
|
}
|
2020-11-12 14:55:42 +01:00
|
|
|
assertEquals(borrows, buf.countBorrows());
|
2020-10-15 16:20:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-10 12:09:14 +01:00
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void sliceWithoutOffsetAndSizeHasSameEndianAsParent(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
2020-10-15 16:20:26 +02:00
|
|
|
buf.order(ByteOrder.BIG_ENDIAN);
|
|
|
|
buf.writeLong(0x0102030405060708L);
|
|
|
|
try (Buf slice = buf.slice()) {
|
|
|
|
assertEquals(0x0102030405060708L, slice.readLong());
|
|
|
|
}
|
|
|
|
buf.order(ByteOrder.LITTLE_ENDIAN);
|
|
|
|
try (Buf slice = buf.slice()) {
|
|
|
|
assertEquals(0x0807060504030201L, slice.readLong());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-10 12:09:14 +01:00
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void sliceWithOffsetAndSizeHasSameEndianAsParent(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
2020-10-15 16:20:26 +02:00
|
|
|
buf.order(ByteOrder.BIG_ENDIAN);
|
|
|
|
buf.writeLong(0x0102030405060708L);
|
|
|
|
try (Buf slice = buf.slice(0, 8)) {
|
|
|
|
assertEquals(0x0102030405060708L, slice.readLong());
|
|
|
|
}
|
|
|
|
buf.order(ByteOrder.LITTLE_ENDIAN);
|
|
|
|
try (Buf slice = buf.slice(0, 8)) {
|
|
|
|
assertEquals(0x0807060504030201L, slice.readLong());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-10 12:09:14 +01:00
|
|
|
@ParameterizedTest
|
2020-11-12 14:55:42 +01:00
|
|
|
@MethodSource("nonSliceAllocators")
|
2020-11-10 12:09:14 +01:00
|
|
|
void sendOnSliceWithoutOffsetAndSizeMustThrow(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
2020-10-15 16:20:26 +02:00
|
|
|
try (Buf slice = buf.slice()) {
|
2020-11-16 18:00:32 +01:00
|
|
|
assertFalse(buf.isOwned());
|
2020-11-10 12:09:14 +01:00
|
|
|
assertThrows(IllegalStateException.class, slice::send);
|
2020-10-15 16:20:26 +02:00
|
|
|
}
|
|
|
|
// Verify that the slice is closed properly afterwards.
|
2020-11-16 18:00:32 +01:00
|
|
|
assertTrue(buf.isOwned());
|
2020-10-15 16:20:26 +02:00
|
|
|
buf.send().receive().close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-10 12:09:14 +01:00
|
|
|
@ParameterizedTest
|
2020-11-12 14:55:42 +01:00
|
|
|
@MethodSource("nonSliceAllocators")
|
2020-11-10 12:09:14 +01:00
|
|
|
void sendOnSliceWithOffsetAndSizeMustThrow(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
2020-10-15 16:20:26 +02:00
|
|
|
try (Buf slice = buf.slice(0, 8)) {
|
2020-11-16 18:00:32 +01:00
|
|
|
assertFalse(buf.isOwned());
|
2020-11-10 12:09:14 +01:00
|
|
|
assertThrows(IllegalStateException.class, slice::send);
|
2020-10-15 16:20:26 +02:00
|
|
|
}
|
|
|
|
// Verify that the slice is closed properly afterwards.
|
2020-11-16 18:00:32 +01:00
|
|
|
assertTrue(buf.isOwned());
|
2020-10-15 16:20:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-10 12:09:14 +01:00
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void sliceWithNegativeOffsetMustThrow(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
2020-11-12 14:55:42 +01:00
|
|
|
int borrows = buf.countBorrows();
|
2020-11-10 12:09:14 +01:00
|
|
|
assertThrows(IndexOutOfBoundsException.class, () -> buf.slice(-1, 1));
|
2020-10-15 16:20:26 +02:00
|
|
|
// Verify that the slice is closed properly afterwards.
|
2020-11-12 14:55:42 +01:00
|
|
|
assertEquals(borrows, buf.countBorrows());
|
2020-10-15 16:20:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-10 12:09:14 +01:00
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void sliceWithNegativeSizeMustThrow(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
2020-11-12 14:55:42 +01:00
|
|
|
int borrows = buf.countBorrows();
|
|
|
|
assertThrows(IllegalArgumentException.class, () -> buf.slice(0, -1));
|
2020-10-15 16:20:26 +02:00
|
|
|
// Verify that the slice is closed properly afterwards.
|
2020-11-12 14:55:42 +01:00
|
|
|
assertEquals(borrows, buf.countBorrows());
|
2020-10-15 16:20:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-10 12:09:14 +01:00
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void sliceWithSizeGreaterThanCapacityMustThrow(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
2020-11-12 14:55:42 +01:00
|
|
|
int borrows = buf.countBorrows();
|
2020-11-10 12:09:14 +01:00
|
|
|
assertThrows(IndexOutOfBoundsException.class, () -> buf.slice(0, 9));
|
2020-10-15 16:20:26 +02:00
|
|
|
buf.slice(0, 8).close(); // This is still fine.
|
2020-11-10 12:09:14 +01:00
|
|
|
assertThrows(IndexOutOfBoundsException.class, () -> buf.slice(1, 8));
|
2020-10-15 16:20:26 +02:00
|
|
|
// Verify that the slice is closed properly afterwards.
|
2020-11-12 14:55:42 +01:00
|
|
|
assertEquals(borrows, buf.countBorrows());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void sliceWithZeroSizeMustBeAllowed(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
int borrows = buf.countBorrows();
|
|
|
|
buf.slice(0, 0).close(); // This is fine.
|
|
|
|
// Verify that the slice is closed properly afterwards.
|
|
|
|
assertEquals(borrows, buf.countBorrows());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
public void acquireComposingAndSlicingMustIncrementBorrows(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
int borrows = buf.countBorrows();
|
|
|
|
try (Buf ignored = buf.acquire()) {
|
|
|
|
assertEquals(borrows + 1, buf.countBorrows());
|
|
|
|
try (Buf slice = buf.slice()) {
|
|
|
|
assertEquals(borrows + 2, buf.countBorrows());
|
2020-11-16 18:00:32 +01:00
|
|
|
try (Buf ignored1 = allocator.compose(buf, slice)) {
|
2020-11-12 14:55:42 +01:00
|
|
|
assertEquals(borrows + 3, buf.countBorrows());
|
|
|
|
}
|
|
|
|
assertEquals(borrows + 2, buf.countBorrows());
|
|
|
|
}
|
|
|
|
assertEquals(borrows + 1, buf.countBorrows());
|
|
|
|
}
|
|
|
|
assertEquals(borrows, buf.countBorrows());
|
2020-10-15 16:20:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-10 12:09:14 +01:00
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void copyIntoByteArray(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
2020-10-30 14:39:50 +01:00
|
|
|
buf.order(ByteOrder.BIG_ENDIAN).writeLong(0x0102030405060708L);
|
|
|
|
byte[] array = new byte[8];
|
|
|
|
buf.copyInto(0, array, 0, array.length);
|
2020-11-10 12:09:14 +01:00
|
|
|
assertThat(array).containsExactly(0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08);
|
2020-10-30 14:39:50 +01:00
|
|
|
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.writerOffset(0).order(ByteOrder.LITTLE_ENDIAN).writeLong(0x0102030405060708L);
|
2020-10-30 14:39:50 +01:00
|
|
|
buf.copyInto(0, array, 0, array.length);
|
2020-11-10 12:09:14 +01:00
|
|
|
assertThat(array).containsExactly(0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01);
|
2020-10-30 14:39:50 +01:00
|
|
|
|
|
|
|
array = new byte[6];
|
|
|
|
buf.copyInto(1, array, 1, 3);
|
2020-11-10 12:09:14 +01:00
|
|
|
assertThat(array).containsExactly(0x00, 0x07, 0x06, 0x05, 0x00, 0x00);
|
2020-10-30 14:39:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-10 12:09:14 +01:00
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void copyIntoHeapByteBuffer(Fixture fixture) {
|
|
|
|
testCopyIntoByteBuffer(fixture, ByteBuffer::allocate);
|
2020-10-30 14:39:50 +01:00
|
|
|
}
|
|
|
|
|
2020-11-10 12:09:14 +01:00
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void copyIntoDirectByteBuffer(Fixture fixture) {
|
|
|
|
testCopyIntoByteBuffer(fixture, ByteBuffer::allocateDirect);
|
2020-10-30 14:39:50 +01:00
|
|
|
}
|
|
|
|
|
2020-11-10 12:09:14 +01:00
|
|
|
private static void testCopyIntoByteBuffer(Fixture fixture, Function<Integer, ByteBuffer> bbAlloc) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
2020-10-30 14:39:50 +01:00
|
|
|
buf.order(ByteOrder.BIG_ENDIAN).writeLong(0x0102030405060708L);
|
|
|
|
ByteBuffer buffer = bbAlloc.apply(8);
|
|
|
|
buf.copyInto(0, buffer, 0, buffer.capacity());
|
|
|
|
assertEquals((byte) 0x01, buffer.get());
|
|
|
|
assertEquals((byte) 0x02, buffer.get());
|
|
|
|
assertEquals((byte) 0x03, buffer.get());
|
|
|
|
assertEquals((byte) 0x04, buffer.get());
|
|
|
|
assertEquals((byte) 0x05, buffer.get());
|
|
|
|
assertEquals((byte) 0x06, buffer.get());
|
|
|
|
assertEquals((byte) 0x07, buffer.get());
|
|
|
|
assertEquals((byte) 0x08, buffer.get());
|
|
|
|
buffer.clear();
|
|
|
|
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.writerOffset(0).order(ByteOrder.LITTLE_ENDIAN).writeLong(0x0102030405060708L);
|
2020-10-30 14:39:50 +01:00
|
|
|
buf.copyInto(0, buffer, 0, buffer.capacity());
|
|
|
|
assertEquals((byte) 0x08, buffer.get());
|
|
|
|
assertEquals((byte) 0x07, buffer.get());
|
|
|
|
assertEquals((byte) 0x06, buffer.get());
|
|
|
|
assertEquals((byte) 0x05, buffer.get());
|
|
|
|
assertEquals((byte) 0x04, buffer.get());
|
|
|
|
assertEquals((byte) 0x03, buffer.get());
|
|
|
|
assertEquals((byte) 0x02, buffer.get());
|
|
|
|
assertEquals((byte) 0x01, buffer.get());
|
|
|
|
buffer.clear();
|
|
|
|
|
|
|
|
buffer = bbAlloc.apply(6);
|
|
|
|
buf.copyInto(1, buffer, 1, 3);
|
|
|
|
assertEquals((byte) 0x00, buffer.get());
|
|
|
|
assertEquals((byte) 0x07, buffer.get());
|
|
|
|
assertEquals((byte) 0x06, buffer.get());
|
|
|
|
assertEquals((byte) 0x05, buffer.get());
|
|
|
|
assertEquals((byte) 0x00, buffer.get());
|
|
|
|
assertEquals((byte) 0x00, buffer.get());
|
|
|
|
buffer.clear();
|
|
|
|
|
|
|
|
buffer = bbAlloc.apply(6);
|
|
|
|
buffer.position(3).limit(3);
|
|
|
|
buf.copyInto(1, buffer, 1, 3);
|
|
|
|
assertEquals(3, buffer.position());
|
|
|
|
assertEquals(3, buffer.limit());
|
|
|
|
buffer.clear();
|
|
|
|
assertEquals((byte) 0x00, buffer.get());
|
|
|
|
assertEquals((byte) 0x07, buffer.get());
|
|
|
|
assertEquals((byte) 0x06, buffer.get());
|
|
|
|
assertEquals((byte) 0x05, buffer.get());
|
|
|
|
assertEquals((byte) 0x00, buffer.get());
|
|
|
|
assertEquals((byte) 0x00, buffer.get());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-10 12:09:14 +01:00
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void copyIntoOnHeapBuf(Fixture fixture) {
|
|
|
|
testCopyIntoBuf(fixture, Allocator.heap()::allocate);
|
2020-10-30 14:39:50 +01:00
|
|
|
}
|
|
|
|
|
2020-11-10 12:09:14 +01:00
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void copyIntoOffHeapBuf(Fixture fixture) {
|
|
|
|
testCopyIntoBuf(fixture, Allocator.direct()::allocate);
|
2020-10-30 14:39:50 +01:00
|
|
|
}
|
|
|
|
|
2020-11-10 12:09:14 +01:00
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void copyIntoOnHeapBufSlice(Fixture fixture) {
|
2020-10-30 14:39:50 +01:00
|
|
|
try (Scope scope = new Scope()) {
|
2020-11-11 23:00:08 +01:00
|
|
|
testCopyIntoBuf(fixture, size -> scope.add(Allocator.heap().allocate(size)).writerOffset(size).slice());
|
2020-10-30 14:39:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-10 12:09:14 +01:00
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void copyIntoOffHeapBufSlice(Fixture fixture) {
|
2020-10-30 14:39:50 +01:00
|
|
|
try (Scope scope = new Scope()) {
|
2020-11-11 23:00:08 +01:00
|
|
|
testCopyIntoBuf(fixture, size -> scope.add(Allocator.direct().allocate(size)).writerOffset(size).slice());
|
2020-10-30 14:39:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-10 12:09:14 +01:00
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void copyIntoCompositeOnHeapOnHeapBuf(Fixture fixture) {
|
2020-10-30 14:39:50 +01:00
|
|
|
try (var a = Allocator.heap();
|
|
|
|
var b = Allocator.heap()) {
|
2020-11-10 12:09:14 +01:00
|
|
|
testCopyIntoBuf(fixture, size -> {
|
2020-10-30 14:39:50 +01:00
|
|
|
int first = size / 2;
|
|
|
|
int second = size - first;
|
|
|
|
try (var bufFirst = a.allocate(first);
|
|
|
|
var bufSecond = b.allocate(second)) {
|
2020-11-16 18:00:32 +01:00
|
|
|
return a.compose(bufFirst, bufSecond);
|
2020-10-30 14:39:50 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-10 12:09:14 +01:00
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void copyIntoCompositeOnHeapOffHeapBuf(Fixture fixture) {
|
2020-10-30 14:39:50 +01:00
|
|
|
try (var a = Allocator.heap();
|
|
|
|
var b = Allocator.direct()) {
|
2020-11-10 12:09:14 +01:00
|
|
|
testCopyIntoBuf(fixture, size -> {
|
2020-10-30 14:39:50 +01:00
|
|
|
int first = size / 2;
|
|
|
|
int second = size - first;
|
|
|
|
try (var bufFirst = a.allocate(first);
|
|
|
|
var bufSecond = b.allocate(second)) {
|
2020-11-16 18:00:32 +01:00
|
|
|
return a.compose(bufFirst, bufSecond);
|
2020-10-30 14:39:50 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-10 12:09:14 +01:00
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void copyIntoCompositeOffHeapOnHeapBuf(Fixture fixture) {
|
2020-10-30 14:39:50 +01:00
|
|
|
try (var a = Allocator.direct();
|
|
|
|
var b = Allocator.heap()) {
|
2020-11-10 12:09:14 +01:00
|
|
|
testCopyIntoBuf(fixture, size -> {
|
2020-10-30 14:39:50 +01:00
|
|
|
int first = size / 2;
|
|
|
|
int second = size - first;
|
|
|
|
try (var bufFirst = a.allocate(first);
|
|
|
|
var bufSecond = b.allocate(second)) {
|
2020-11-16 18:00:32 +01:00
|
|
|
return a.compose(bufFirst, bufSecond);
|
2020-10-30 14:39:50 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-10 12:09:14 +01:00
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void copyIntoCompositeOffHeapOffHeapBuf(Fixture fixture) {
|
2020-10-30 14:39:50 +01:00
|
|
|
try (var a = Allocator.direct();
|
|
|
|
var b = Allocator.direct()) {
|
2020-11-10 12:09:14 +01:00
|
|
|
testCopyIntoBuf(fixture, size -> {
|
2020-10-30 14:39:50 +01:00
|
|
|
int first = size / 2;
|
|
|
|
int second = size - first;
|
|
|
|
try (var bufFirst = a.allocate(first);
|
|
|
|
var bufSecond = b.allocate(second)) {
|
2020-11-16 18:00:32 +01:00
|
|
|
return a.compose(bufFirst, bufSecond);
|
2020-10-30 14:39:50 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-10 12:09:14 +01:00
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void copyIntoCompositeOnHeapOnHeapBufSlice(Fixture fixture) {
|
2020-10-30 14:39:50 +01:00
|
|
|
try (var a = Allocator.heap();
|
|
|
|
var b = Allocator.heap();
|
|
|
|
var scope = new Scope()) {
|
2020-11-10 12:09:14 +01:00
|
|
|
testCopyIntoBuf(fixture, size -> {
|
2020-10-30 14:39:50 +01:00
|
|
|
int first = size / 2;
|
|
|
|
int second = size - first;
|
|
|
|
try (var bufFirst = a.allocate(first);
|
|
|
|
var bufSecond = b.allocate(second)) {
|
2020-11-16 18:00:32 +01:00
|
|
|
return scope.add(a.compose(bufFirst, bufSecond)).writerOffset(size).slice();
|
2020-10-30 14:39:50 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-10 12:09:14 +01:00
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void copyIntoCompositeOnHeapOffHeapBufSlice(Fixture fixture) {
|
2020-10-30 14:39:50 +01:00
|
|
|
try (var a = Allocator.heap();
|
|
|
|
var b = Allocator.direct();
|
|
|
|
var scope = new Scope()) {
|
2020-11-10 12:09:14 +01:00
|
|
|
testCopyIntoBuf(fixture, size -> {
|
2020-10-30 14:39:50 +01:00
|
|
|
int first = size / 2;
|
|
|
|
int second = size - first;
|
|
|
|
try (var bufFirst = a.allocate(first);
|
|
|
|
var bufSecond = b.allocate(second)) {
|
2020-11-16 18:00:32 +01:00
|
|
|
return scope.add(a.compose(bufFirst, bufSecond)).writerOffset(size).slice();
|
2020-10-30 14:39:50 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-10 12:09:14 +01:00
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void copyIntoCompositeOffHeapOnHeapBufSlice(Fixture fixture) {
|
2020-10-30 14:39:50 +01:00
|
|
|
try (var a = Allocator.direct();
|
|
|
|
var b = Allocator.heap();
|
|
|
|
var scope = new Scope()) {
|
2020-11-10 12:09:14 +01:00
|
|
|
testCopyIntoBuf(fixture, size -> {
|
2020-10-30 14:39:50 +01:00
|
|
|
int first = size / 2;
|
|
|
|
int second = size - first;
|
|
|
|
try (var bufFirst = a.allocate(first);
|
|
|
|
var bufSecond = b.allocate(second)) {
|
2020-11-16 18:00:32 +01:00
|
|
|
return scope.add(a.compose(bufFirst, bufSecond)).writerOffset(size).slice();
|
2020-10-30 14:39:50 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-10 12:09:14 +01:00
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void copyIntoCompositeOffHeapOffHeapBufSlice(Fixture fixture) {
|
2020-10-30 14:39:50 +01:00
|
|
|
try (var a = Allocator.direct();
|
|
|
|
var b = Allocator.direct();
|
|
|
|
var scope = new Scope()) {
|
2020-11-10 12:09:14 +01:00
|
|
|
testCopyIntoBuf(fixture, size -> {
|
2020-10-30 14:39:50 +01:00
|
|
|
int first = size / 2;
|
|
|
|
int second = size - first;
|
|
|
|
try (var bufFirst = a.allocate(first);
|
|
|
|
var bufSecond = b.allocate(second)) {
|
2020-11-16 18:00:32 +01:00
|
|
|
return scope.add(a.compose(bufFirst, bufSecond)).writerOffset(size).slice();
|
2020-10-30 14:39:50 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-10 12:09:14 +01:00
|
|
|
private static void testCopyIntoBuf(Fixture fixture, Function<Integer, Buf> bbAlloc) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
2020-10-30 14:39:50 +01:00
|
|
|
buf.order(ByteOrder.BIG_ENDIAN).writeLong(0x0102030405060708L);
|
|
|
|
Buf buffer = bbAlloc.apply(8);
|
2020-11-11 23:00:08 +01:00
|
|
|
buffer.writerOffset(8);
|
2020-10-30 14:39:50 +01:00
|
|
|
buf.copyInto(0, buffer, 0, buffer.capacity());
|
|
|
|
assertEquals((byte) 0x01, buffer.readByte());
|
|
|
|
assertEquals((byte) 0x02, buffer.readByte());
|
|
|
|
assertEquals((byte) 0x03, buffer.readByte());
|
|
|
|
assertEquals((byte) 0x04, buffer.readByte());
|
|
|
|
assertEquals((byte) 0x05, buffer.readByte());
|
|
|
|
assertEquals((byte) 0x06, buffer.readByte());
|
|
|
|
assertEquals((byte) 0x07, buffer.readByte());
|
|
|
|
assertEquals((byte) 0x08, buffer.readByte());
|
|
|
|
buffer.reset();
|
|
|
|
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.writerOffset(0).order(ByteOrder.LITTLE_ENDIAN).writeLong(0x0102030405060708L);
|
2020-10-30 14:39:50 +01:00
|
|
|
buf.copyInto(0, buffer, 0, buffer.capacity());
|
2020-11-11 23:00:08 +01:00
|
|
|
buffer.writerOffset(8);
|
2020-10-30 14:39:50 +01:00
|
|
|
assertEquals((byte) 0x08, buffer.readByte());
|
|
|
|
assertEquals((byte) 0x07, buffer.readByte());
|
|
|
|
assertEquals((byte) 0x06, buffer.readByte());
|
|
|
|
assertEquals((byte) 0x05, buffer.readByte());
|
|
|
|
assertEquals((byte) 0x04, buffer.readByte());
|
|
|
|
assertEquals((byte) 0x03, buffer.readByte());
|
|
|
|
assertEquals((byte) 0x02, buffer.readByte());
|
|
|
|
assertEquals((byte) 0x01, buffer.readByte());
|
|
|
|
buffer.reset();
|
|
|
|
|
|
|
|
buffer.close();
|
|
|
|
buffer = bbAlloc.apply(6);
|
|
|
|
buf.copyInto(1, buffer, 1, 3);
|
2020-11-11 23:00:08 +01:00
|
|
|
buffer.writerOffset(6);
|
2020-10-30 14:39:50 +01:00
|
|
|
assertEquals((byte) 0x00, buffer.readByte());
|
|
|
|
assertEquals((byte) 0x07, buffer.readByte());
|
|
|
|
assertEquals((byte) 0x06, buffer.readByte());
|
|
|
|
assertEquals((byte) 0x05, buffer.readByte());
|
|
|
|
assertEquals((byte) 0x00, buffer.readByte());
|
|
|
|
assertEquals((byte) 0x00, buffer.readByte());
|
|
|
|
|
|
|
|
buffer.close();
|
|
|
|
buffer = bbAlloc.apply(6);
|
2020-11-11 23:00:08 +01:00
|
|
|
buffer.writerOffset(3).readerOffset(3);
|
2020-10-30 14:39:50 +01:00
|
|
|
buf.copyInto(1, buffer, 1, 3);
|
2020-11-11 23:00:08 +01:00
|
|
|
assertEquals(3, buffer.readerOffset());
|
|
|
|
assertEquals(3, buffer.writerOffset());
|
2020-10-30 14:39:50 +01:00
|
|
|
buffer.reset();
|
2020-11-11 23:00:08 +01:00
|
|
|
buffer.writerOffset(6);
|
2020-10-30 14:39:50 +01:00
|
|
|
assertEquals((byte) 0x00, buffer.readByte());
|
|
|
|
assertEquals((byte) 0x07, buffer.readByte());
|
|
|
|
assertEquals((byte) 0x06, buffer.readByte());
|
|
|
|
assertEquals((byte) 0x05, buffer.readByte());
|
|
|
|
assertEquals((byte) 0x00, buffer.readByte());
|
|
|
|
assertEquals((byte) 0x00, buffer.readByte());
|
|
|
|
buffer.close();
|
|
|
|
|
|
|
|
buf.reset();
|
|
|
|
buf.order(ByteOrder.BIG_ENDIAN).writeLong(0x0102030405060708L);
|
|
|
|
// Testing copyInto for overlapping writes:
|
|
|
|
//
|
|
|
|
// 0x0102030405060708
|
|
|
|
// └──┬──┬──┘ │
|
|
|
|
// └─▶└┬───────┘
|
|
|
|
// â–¼
|
|
|
|
// 0x0102030102030405
|
|
|
|
buf.copyInto(0, buf, 3, 5);
|
2020-11-11 23:00:08 +01:00
|
|
|
assertThat(toByteArray(buf)).containsExactly(0x01, 0x02, 0x03, 0x01, 0x02, 0x03, 0x04, 0x05);
|
2020-10-30 14:39:50 +01:00
|
|
|
}
|
|
|
|
}
|
2020-11-05 15:15:34 +01:00
|
|
|
|
2020-11-10 12:09:14 +01:00
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void readableBytesMustMatchWhatWasWritten(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(16)) {
|
2020-11-05 15:15:34 +01:00
|
|
|
buf.writeLong(0);
|
|
|
|
assertEquals(Long.BYTES, buf.readableBytes());
|
|
|
|
buf.readShort();
|
|
|
|
assertEquals(Long.BYTES - Short.BYTES, buf.readableBytes());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-10 12:09:14 +01:00
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void byteIterationOfBigEndianBuffers(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(0x28)) {
|
2020-11-05 15:15:34 +01:00
|
|
|
buf.order(ByteOrder.BIG_ENDIAN); // The byte order should have no impact.
|
|
|
|
checkByteIteration(buf);
|
|
|
|
buf.reset();
|
|
|
|
checkByteIterationOfRegion(buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-10 12:09:14 +01:00
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void byteIterationOfLittleEndianBuffers(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(0x28)) {
|
2020-11-05 15:15:34 +01:00
|
|
|
buf.order(ByteOrder.LITTLE_ENDIAN); // The byte order should have no impact.
|
|
|
|
checkByteIteration(buf);
|
|
|
|
buf.reset();
|
|
|
|
checkByteIterationOfRegion(buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void checkByteIteration(Buf buf) {
|
|
|
|
var itr = buf.iterate();
|
|
|
|
assertFalse(itr.hasNextByte());
|
|
|
|
assertFalse(itr.hasNextLong());
|
|
|
|
assertEquals(0, itr.bytesLeft());
|
2020-11-10 12:09:14 +01:00
|
|
|
assertThrows(NoSuchElementException.class, itr::nextLong);
|
|
|
|
assertThrows(NoSuchElementException.class, itr::nextByte);
|
2020-11-05 15:15:34 +01:00
|
|
|
|
|
|
|
for (int i = 0; i < 0x27; i++) {
|
|
|
|
buf.writeByte((byte) (i + 1));
|
|
|
|
}
|
2020-11-11 23:00:08 +01:00
|
|
|
int roff = buf.readerOffset();
|
|
|
|
int woff = buf.writerOffset();
|
2020-11-05 15:15:34 +01:00
|
|
|
itr = buf.iterate();
|
|
|
|
assertEquals(0x27, itr.bytesLeft());
|
|
|
|
assertTrue(itr.hasNextByte());
|
|
|
|
assertTrue(itr.hasNextLong());
|
|
|
|
assertEquals(0x0102030405060708L, itr.nextLong());
|
|
|
|
assertEquals(0x1F, itr.bytesLeft());
|
|
|
|
assertTrue(itr.hasNextLong());
|
|
|
|
assertEquals(0x090A0B0C0D0E0F10L, itr.nextLong());
|
|
|
|
assertTrue(itr.hasNextLong());
|
|
|
|
assertEquals(0x17, itr.bytesLeft());
|
|
|
|
assertEquals(0x1112131415161718L, itr.nextLong());
|
|
|
|
assertTrue(itr.hasNextLong());
|
|
|
|
assertEquals(0x0F, itr.bytesLeft());
|
|
|
|
assertEquals(0x191A1B1C1D1E1F20L, itr.nextLong());
|
|
|
|
assertFalse(itr.hasNextLong());
|
|
|
|
assertEquals(7, itr.bytesLeft());
|
2020-11-10 12:09:14 +01:00
|
|
|
assertThrows(NoSuchElementException.class, itr::nextLong);
|
2020-11-05 15:15:34 +01:00
|
|
|
assertTrue(itr.hasNextByte());
|
|
|
|
assertEquals((byte) 0x21, itr.nextByte());
|
|
|
|
assertTrue(itr.hasNextByte());
|
|
|
|
assertEquals(6, itr.bytesLeft());
|
|
|
|
assertEquals((byte) 0x22, itr.nextByte());
|
|
|
|
assertTrue(itr.hasNextByte());
|
|
|
|
assertEquals(5, itr.bytesLeft());
|
|
|
|
assertEquals((byte) 0x23, itr.nextByte());
|
|
|
|
assertTrue(itr.hasNextByte());
|
|
|
|
assertEquals(4, itr.bytesLeft());
|
|
|
|
assertEquals((byte) 0x24, itr.nextByte());
|
|
|
|
assertTrue(itr.hasNextByte());
|
|
|
|
assertEquals(3, itr.bytesLeft());
|
|
|
|
assertEquals((byte) 0x25, itr.nextByte());
|
|
|
|
assertTrue(itr.hasNextByte());
|
|
|
|
assertEquals(2, itr.bytesLeft());
|
|
|
|
assertEquals((byte) 0x26, itr.nextByte());
|
|
|
|
assertTrue(itr.hasNextByte());
|
|
|
|
assertEquals(1, itr.bytesLeft());
|
|
|
|
assertEquals((byte) 0x27, itr.nextByte());
|
|
|
|
assertFalse(itr.hasNextByte());
|
|
|
|
assertEquals(0, itr.bytesLeft());
|
2020-11-10 12:09:14 +01:00
|
|
|
assertThrows(NoSuchElementException.class, itr::nextLong);
|
|
|
|
assertThrows(NoSuchElementException.class, itr::nextByte);
|
2020-11-11 23:00:08 +01:00
|
|
|
assertEquals(roff, buf.readerOffset());
|
|
|
|
assertEquals(woff, buf.writerOffset());
|
2020-11-05 15:15:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private static void checkByteIterationOfRegion(Buf buf) {
|
2020-11-10 12:09:14 +01:00
|
|
|
assertThrows(IllegalArgumentException.class, () -> buf.iterate(-1, 1));
|
|
|
|
assertThrows(IllegalArgumentException.class, () -> buf.iterate(1, -1));
|
|
|
|
assertThrows(IllegalArgumentException.class, () -> buf.iterate(buf.capacity(), 1));
|
|
|
|
assertThrows(IllegalArgumentException.class, () -> buf.iterate(buf.capacity() - 1, 2));
|
|
|
|
assertThrows(IllegalArgumentException.class, () -> buf.iterate(buf.capacity() - 2, 3));
|
2020-11-06 13:39:21 +01:00
|
|
|
|
2020-11-05 15:15:34 +01:00
|
|
|
var itr = buf.iterate(1, 0);
|
|
|
|
assertFalse(itr.hasNextByte());
|
|
|
|
assertFalse(itr.hasNextLong());
|
|
|
|
assertEquals(0, itr.bytesLeft());
|
2020-11-10 12:09:14 +01:00
|
|
|
assertThrows(NoSuchElementException.class, itr::nextLong);
|
|
|
|
assertThrows(NoSuchElementException.class, itr::nextByte);
|
2020-11-05 15:15:34 +01:00
|
|
|
|
|
|
|
for (int i = 0; i < 0x27; i++) {
|
|
|
|
buf.writeByte((byte) (i + 1));
|
|
|
|
}
|
2020-11-11 23:00:08 +01:00
|
|
|
int roff = buf.readerOffset();
|
|
|
|
int woff = buf.writerOffset();
|
|
|
|
itr = buf.iterate(buf.readerOffset() + 1, buf.readableBytes() - 2);
|
2020-11-05 15:15:34 +01:00
|
|
|
assertEquals(0x25, itr.bytesLeft());
|
|
|
|
assertTrue(itr.hasNextByte());
|
|
|
|
assertTrue(itr.hasNextLong());
|
|
|
|
assertEquals(0x0203040506070809L, itr.nextLong());
|
|
|
|
assertEquals(0x1D, itr.bytesLeft());
|
|
|
|
assertTrue(itr.hasNextLong());
|
|
|
|
assertEquals(0x0A0B0C0D0E0F1011L, itr.nextLong());
|
|
|
|
assertTrue(itr.hasNextLong());
|
|
|
|
assertEquals(0x15, itr.bytesLeft());
|
|
|
|
assertEquals(0x1213141516171819L, itr.nextLong());
|
|
|
|
assertTrue(itr.hasNextLong());
|
|
|
|
assertEquals(0x0D, itr.bytesLeft());
|
|
|
|
assertEquals(0x1A1B1C1D1E1F2021L, itr.nextLong());
|
|
|
|
assertFalse(itr.hasNextLong());
|
|
|
|
assertEquals(5, itr.bytesLeft());
|
2020-11-10 12:09:14 +01:00
|
|
|
assertThrows(NoSuchElementException.class, itr::nextLong);
|
2020-11-05 15:15:34 +01:00
|
|
|
assertTrue(itr.hasNextByte());
|
|
|
|
assertEquals((byte) 0x22, itr.nextByte());
|
|
|
|
assertTrue(itr.hasNextByte());
|
|
|
|
assertEquals(4, itr.bytesLeft());
|
|
|
|
assertEquals((byte) 0x23, itr.nextByte());
|
|
|
|
assertTrue(itr.hasNextByte());
|
|
|
|
assertEquals(3, itr.bytesLeft());
|
|
|
|
assertEquals((byte) 0x24, itr.nextByte());
|
|
|
|
assertTrue(itr.hasNextByte());
|
|
|
|
assertEquals(2, itr.bytesLeft());
|
|
|
|
assertEquals((byte) 0x25, itr.nextByte());
|
|
|
|
assertTrue(itr.hasNextByte());
|
|
|
|
assertEquals(1, itr.bytesLeft());
|
|
|
|
assertEquals((byte) 0x26, itr.nextByte());
|
|
|
|
assertFalse(itr.hasNextByte());
|
|
|
|
assertEquals(0, itr.bytesLeft());
|
2020-11-10 12:09:14 +01:00
|
|
|
assertThrows(NoSuchElementException.class, itr::nextLong);
|
|
|
|
assertThrows(NoSuchElementException.class, itr::nextByte);
|
2020-11-05 15:15:34 +01:00
|
|
|
|
2020-11-11 23:00:08 +01:00
|
|
|
itr = buf.iterate(buf.readerOffset() + 1, 2);
|
2020-11-05 15:15:34 +01:00
|
|
|
assertEquals(2, itr.bytesLeft());
|
|
|
|
assertTrue(itr.hasNextByte());
|
|
|
|
assertFalse(itr.hasNextLong());
|
|
|
|
assertEquals((byte) 0x02, itr.nextByte());
|
|
|
|
assertEquals(1, itr.bytesLeft());
|
|
|
|
assertTrue(itr.hasNextByte());
|
|
|
|
assertFalse(itr.hasNextLong());
|
|
|
|
assertEquals((byte) 0x03, itr.nextByte());
|
|
|
|
assertEquals(0, itr.bytesLeft());
|
|
|
|
assertFalse(itr.hasNextByte());
|
|
|
|
assertFalse(itr.hasNextLong());
|
2020-11-11 23:00:08 +01:00
|
|
|
assertEquals(roff, buf.readerOffset());
|
|
|
|
assertEquals(woff, buf.writerOffset());
|
2020-11-05 15:15:34 +01:00
|
|
|
}
|
2020-11-06 13:39:21 +01:00
|
|
|
|
2020-11-10 12:09:14 +01:00
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void reverseByteIterationOfBigEndianBuffers(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(0x28)) {
|
2020-11-06 13:39:21 +01:00
|
|
|
buf.order(ByteOrder.BIG_ENDIAN); // The byte order should have no impact.
|
|
|
|
checkReverseByteIteration(buf);
|
|
|
|
buf.reset();
|
|
|
|
checkReverseByteIterationOfRegion(buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-10 12:09:14 +01:00
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void reverseByteIterationOfLittleEndianBuffers(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(0x28)) {
|
2020-11-06 13:39:21 +01:00
|
|
|
buf.order(ByteOrder.LITTLE_ENDIAN); // The byte order should have no impact.
|
|
|
|
checkReverseByteIteration(buf);
|
|
|
|
buf.reset();
|
|
|
|
checkReverseByteIterationOfRegion(buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void checkReverseByteIteration(Buf buf) {
|
|
|
|
var itr = buf.iterateReverse();
|
|
|
|
assertFalse(itr.hasNextByte());
|
|
|
|
assertFalse(itr.hasNextLong());
|
|
|
|
assertEquals(0, itr.bytesLeft());
|
2020-11-10 12:09:14 +01:00
|
|
|
assertThrows(NoSuchElementException.class, itr::nextLong);
|
|
|
|
assertThrows(NoSuchElementException.class, itr::nextByte);
|
2020-11-06 13:39:21 +01:00
|
|
|
|
|
|
|
for (int i = 0; i < 0x27; i++) {
|
|
|
|
buf.writeByte((byte) (i + 1));
|
|
|
|
}
|
2020-11-11 23:00:08 +01:00
|
|
|
int roff = buf.readerOffset();
|
|
|
|
int woff = buf.writerOffset();
|
2020-11-06 13:39:21 +01:00
|
|
|
itr = buf.iterateReverse();
|
|
|
|
assertEquals(0x27, itr.bytesLeft());
|
|
|
|
assertTrue(itr.hasNextByte());
|
|
|
|
assertTrue(itr.hasNextLong());
|
|
|
|
assertEquals(0x2726252423222120L, itr.nextLong());
|
|
|
|
assertEquals(0x1F, itr.bytesLeft());
|
|
|
|
assertTrue(itr.hasNextLong());
|
|
|
|
assertEquals(0x1F1E1D1C1B1A1918L, itr.nextLong());
|
|
|
|
assertTrue(itr.hasNextLong());
|
|
|
|
assertEquals(0x17, itr.bytesLeft());
|
|
|
|
assertEquals(0x1716151413121110L, itr.nextLong());
|
|
|
|
assertTrue(itr.hasNextLong());
|
|
|
|
assertEquals(0x0F, itr.bytesLeft());
|
|
|
|
assertEquals(0x0F0E0D0C0B0A0908L, itr.nextLong());
|
|
|
|
assertFalse(itr.hasNextLong());
|
|
|
|
assertEquals(7, itr.bytesLeft());
|
2020-11-10 12:09:14 +01:00
|
|
|
assertThrows(NoSuchElementException.class, itr::nextLong);
|
2020-11-06 13:39:21 +01:00
|
|
|
assertTrue(itr.hasNextByte());
|
|
|
|
assertEquals((byte) 0x07, itr.nextByte());
|
|
|
|
assertTrue(itr.hasNextByte());
|
|
|
|
assertEquals(6, itr.bytesLeft());
|
|
|
|
assertEquals((byte) 0x06, itr.nextByte());
|
|
|
|
assertTrue(itr.hasNextByte());
|
|
|
|
assertEquals(5, itr.bytesLeft());
|
|
|
|
assertEquals((byte) 0x05, itr.nextByte());
|
|
|
|
assertTrue(itr.hasNextByte());
|
|
|
|
assertEquals(4, itr.bytesLeft());
|
|
|
|
assertEquals((byte) 0x04, itr.nextByte());
|
|
|
|
assertTrue(itr.hasNextByte());
|
|
|
|
assertEquals(3, itr.bytesLeft());
|
|
|
|
assertEquals((byte) 0x03, itr.nextByte());
|
|
|
|
assertTrue(itr.hasNextByte());
|
|
|
|
assertEquals(2, itr.bytesLeft());
|
|
|
|
assertEquals((byte) 0x02, itr.nextByte());
|
|
|
|
assertTrue(itr.hasNextByte());
|
|
|
|
assertEquals(1, itr.bytesLeft());
|
|
|
|
assertEquals((byte) 0x01, itr.nextByte());
|
|
|
|
assertFalse(itr.hasNextByte());
|
|
|
|
assertEquals(0, itr.bytesLeft());
|
2020-11-10 12:09:14 +01:00
|
|
|
assertThrows(NoSuchElementException.class, itr::nextLong);
|
|
|
|
assertThrows(NoSuchElementException.class, itr::nextByte);
|
2020-11-11 23:00:08 +01:00
|
|
|
assertEquals(roff, buf.readerOffset());
|
|
|
|
assertEquals(woff, buf.writerOffset());
|
2020-11-06 13:39:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private static void checkReverseByteIterationOfRegion(Buf buf) {
|
2020-11-10 12:09:14 +01:00
|
|
|
assertThrows(IllegalArgumentException.class, () -> buf.iterateReverse(-1, 0));
|
|
|
|
assertThrows(IllegalArgumentException.class, () -> buf.iterateReverse(0, -1));
|
|
|
|
assertThrows(IllegalArgumentException.class, () -> buf.iterateReverse(0, 2));
|
|
|
|
assertThrows(IllegalArgumentException.class, () -> buf.iterateReverse(1, 3));
|
|
|
|
assertThrows(IllegalArgumentException.class, () -> buf.iterateReverse(buf.capacity(), 0));
|
2020-11-06 13:39:21 +01:00
|
|
|
|
|
|
|
var itr = buf.iterateReverse(1, 0);
|
|
|
|
assertFalse(itr.hasNextByte());
|
|
|
|
assertFalse(itr.hasNextLong());
|
|
|
|
assertEquals(0, itr.bytesLeft());
|
2020-11-10 12:09:14 +01:00
|
|
|
assertThrows(NoSuchElementException.class, itr::nextLong);
|
|
|
|
assertThrows(NoSuchElementException.class, itr::nextByte);
|
2020-11-06 13:39:21 +01:00
|
|
|
|
|
|
|
for (int i = 0; i < 0x27; i++) {
|
|
|
|
buf.writeByte((byte) (i + 1));
|
|
|
|
}
|
2020-11-11 23:00:08 +01:00
|
|
|
int roff = buf.readerOffset();
|
|
|
|
int woff = buf.writerOffset();
|
|
|
|
itr = buf.iterateReverse(buf.writerOffset() - 2, buf.readableBytes() - 2);
|
2020-11-06 13:39:21 +01:00
|
|
|
assertEquals(0x25, itr.bytesLeft());
|
|
|
|
assertTrue(itr.hasNextByte());
|
|
|
|
assertTrue(itr.hasNextLong());
|
|
|
|
assertEquals(0x262524232221201FL, itr.nextLong());
|
|
|
|
assertEquals(0x1D, itr.bytesLeft());
|
|
|
|
assertTrue(itr.hasNextLong());
|
|
|
|
assertEquals(0x1E1D1C1B1A191817L, itr.nextLong());
|
|
|
|
assertTrue(itr.hasNextLong());
|
|
|
|
assertEquals(0x15, itr.bytesLeft());
|
|
|
|
assertEquals(0x161514131211100FL, itr.nextLong());
|
|
|
|
assertTrue(itr.hasNextLong());
|
|
|
|
assertEquals(0x0D, itr.bytesLeft());
|
|
|
|
assertEquals(0x0E0D0C0B0A090807L, itr.nextLong());
|
|
|
|
assertFalse(itr.hasNextLong());
|
|
|
|
assertEquals(5, itr.bytesLeft());
|
2020-11-10 12:09:14 +01:00
|
|
|
assertThrows(NoSuchElementException.class, itr::nextLong);
|
2020-11-06 13:39:21 +01:00
|
|
|
assertTrue(itr.hasNextByte());
|
|
|
|
assertEquals((byte) 0x06, itr.nextByte());
|
|
|
|
assertTrue(itr.hasNextByte());
|
|
|
|
assertEquals(4, itr.bytesLeft());
|
|
|
|
assertEquals((byte) 0x05, itr.nextByte());
|
|
|
|
assertTrue(itr.hasNextByte());
|
|
|
|
assertEquals(3, itr.bytesLeft());
|
|
|
|
assertEquals((byte) 0x04, itr.nextByte());
|
|
|
|
assertTrue(itr.hasNextByte());
|
|
|
|
assertEquals(2, itr.bytesLeft());
|
|
|
|
assertEquals((byte) 0x03, itr.nextByte());
|
|
|
|
assertTrue(itr.hasNextByte());
|
|
|
|
assertEquals(1, itr.bytesLeft());
|
|
|
|
assertEquals((byte) 0x02, itr.nextByte());
|
|
|
|
assertFalse(itr.hasNextByte());
|
|
|
|
assertEquals(0, itr.bytesLeft());
|
2020-11-10 12:09:14 +01:00
|
|
|
assertThrows(NoSuchElementException.class, itr::nextLong);
|
|
|
|
assertThrows(NoSuchElementException.class, itr::nextByte);
|
2020-11-06 13:39:21 +01:00
|
|
|
|
2020-11-11 23:00:08 +01:00
|
|
|
itr = buf.iterateReverse(buf.readerOffset() + 2, 2);
|
2020-11-06 13:39:21 +01:00
|
|
|
assertEquals(2, itr.bytesLeft());
|
|
|
|
assertTrue(itr.hasNextByte());
|
|
|
|
assertFalse(itr.hasNextLong());
|
|
|
|
assertEquals((byte) 0x03, itr.nextByte());
|
|
|
|
assertEquals(1, itr.bytesLeft());
|
|
|
|
assertTrue(itr.hasNextByte());
|
|
|
|
assertFalse(itr.hasNextLong());
|
|
|
|
assertEquals((byte) 0x02, itr.nextByte());
|
|
|
|
assertEquals(0, itr.bytesLeft());
|
|
|
|
assertFalse(itr.hasNextByte());
|
|
|
|
assertFalse(itr.hasNextLong());
|
2020-11-11 23:00:08 +01:00
|
|
|
assertEquals(roff, buf.readerOffset());
|
|
|
|
assertEquals(woff, buf.writerOffset());
|
2020-11-06 13:39:21 +01:00
|
|
|
}
|
2020-11-05 15:15:34 +01:00
|
|
|
|
2020-11-10 12:09:14 +01:00
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("heapAllocators")
|
|
|
|
public void heapBufferMustHaveZeroAddress(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
assertThat(buf.getNativeAddress()).isZero();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("directAllocators")
|
|
|
|
public void directBufferMustHaveNonZeroAddress(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
assertThat(buf.getNativeAddress()).isNotZero();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nested
|
|
|
|
@Isolated
|
|
|
|
class CleanerTests {
|
|
|
|
@Disabled("Precise native memory accounting does not work since recent panama-foreign changes.")
|
|
|
|
@ParameterizedTest
|
2020-11-17 15:53:40 +01:00
|
|
|
@MethodSource("io.netty.buffer.api.BufTest#directWithCleanerAllocators")
|
2020-11-10 12:09:14 +01:00
|
|
|
public void bufferMustBeClosedByCleaner(Fixture fixture) throws InterruptedException {
|
|
|
|
var allocator = fixture.createAllocator();
|
|
|
|
allocator.close();
|
|
|
|
int iterations = 100;
|
|
|
|
int allocationSize = 1024;
|
|
|
|
for (int i = 0; i < iterations; i++) {
|
|
|
|
allocateAndForget(allocator, allocationSize);
|
|
|
|
System.gc();
|
|
|
|
System.runFinalization();
|
|
|
|
}
|
2020-11-17 15:53:40 +01:00
|
|
|
var sum = NativeMemorySegmentManager.MEM_USAGE_NATIVE.sum();
|
2020-11-10 12:09:14 +01:00
|
|
|
var totalAllocated = (long) allocationSize * iterations;
|
|
|
|
assertThat(sum).isLessThan(totalAllocated);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void allocateAndForget(Allocator allocator, int size) {
|
|
|
|
allocator.allocate(size);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Disabled("Precise native memory accounting does not work since recent panama-foreign changes.")
|
|
|
|
@ParameterizedTest
|
2020-11-17 15:53:40 +01:00
|
|
|
@MethodSource("io.netty.buffer.api.BufTest#directPooledWithCleanerAllocators")
|
2020-11-10 12:09:14 +01:00
|
|
|
public void buffersMustBeReusedByPoolingAllocatorEvenWhenDroppedByCleanerInsteadOfExplicitly(Fixture fixture)
|
|
|
|
throws InterruptedException {
|
|
|
|
try (var allocator = fixture.createAllocator()) {
|
|
|
|
int iterations = 100;
|
|
|
|
int allocationSize = 1024;
|
|
|
|
for (int i = 0; i < iterations; i++) {
|
|
|
|
allocateAndForget(allocator, allocationSize);
|
|
|
|
System.gc();
|
|
|
|
System.runFinalization();
|
|
|
|
}
|
2020-11-17 15:53:40 +01:00
|
|
|
var sum = NativeMemorySegmentManager.MEM_USAGE_NATIVE.sum();
|
2020-11-10 12:09:14 +01:00
|
|
|
var totalAllocated = (long) allocationSize * iterations;
|
|
|
|
assertThat(sum).isLessThan(totalAllocated);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-16 18:00:32 +01:00
|
|
|
@Test
|
|
|
|
public void compositeBufferCanOnlyBeOwnedWhenAllConstituentBuffersAreOwned() {
|
|
|
|
try (Allocator allocator = Allocator.heap()) {
|
|
|
|
Buf composite;
|
|
|
|
try (Buf a = allocator.allocate(8)) {
|
|
|
|
assertTrue(a.isOwned());
|
|
|
|
Buf leakB;
|
|
|
|
try (Buf b = allocator.allocate(8)) {
|
|
|
|
assertTrue(a.isOwned());
|
|
|
|
assertTrue(b.isOwned());
|
|
|
|
composite = allocator.compose(a, b);
|
|
|
|
assertFalse(composite.isOwned());
|
|
|
|
assertFalse(a.isOwned());
|
|
|
|
assertFalse(b.isOwned());
|
|
|
|
leakB = b;
|
|
|
|
}
|
|
|
|
assertFalse(composite.isOwned());
|
|
|
|
assertFalse(a.isOwned());
|
|
|
|
assertTrue(leakB.isOwned());
|
|
|
|
}
|
|
|
|
assertTrue(composite.isOwned());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
public void ensureWritableMustThrowForBorrowedBuffers(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
try (Buf slice = buf.slice()) {
|
|
|
|
assertThrows(IllegalStateException.class, () -> slice.ensureWritable(1));
|
|
|
|
assertThrows(IllegalStateException.class, () -> buf.ensureWritable(1));
|
|
|
|
}
|
|
|
|
try (Buf compose = allocator.compose(buf)) {
|
|
|
|
assertThrows(IllegalStateException.class, () -> compose.ensureWritable(1));
|
|
|
|
assertThrows(IllegalStateException.class, () -> buf.ensureWritable(1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("nonSliceAllocators")
|
|
|
|
public void ensureWritableMustThrowForNegativeSize(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
assertThrows(IllegalArgumentException.class, () -> buf.ensureWritable(-1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("nonSliceAllocators")
|
|
|
|
public void ensureWritableMustThrowIfRequestedSizeWouldGrowBeyondMaxAllowed(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(512)) {
|
|
|
|
assertThrows(IllegalArgumentException.class, () -> buf.ensureWritable(Integer.MAX_VALUE - 8));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("nonSliceAllocators")
|
|
|
|
public void ensureWritableMustNotThrowWhenSpaceIsAlreadyAvailable(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
buf.ensureWritable(8);
|
|
|
|
buf.writeLong(1);
|
|
|
|
assertThrows(IndexOutOfBoundsException.class, () -> buf.writeByte((byte) 1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("nonSliceAllocators")
|
|
|
|
public void ensureWritableMustExpandBufferCapacity(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
assertThat(buf.writableBytes()).isEqualTo(8);
|
|
|
|
buf.writeLong(0x0102030405060708L);
|
|
|
|
assertThat(buf.writableBytes()).isEqualTo(0);
|
|
|
|
buf.ensureWritable(8);
|
|
|
|
assertThat(buf.writableBytes()).isGreaterThanOrEqualTo(8);
|
|
|
|
buf.writeLong(0xA1A2A3A4A5A6A7A8L);
|
|
|
|
assertThat(buf.readableBytes()).isEqualTo(16);
|
|
|
|
assertThat(buf.readLong()).isEqualTo(0x0102030405060708L);
|
|
|
|
assertThat(buf.readLong()).isEqualTo(0xA1A2A3A4A5A6A7A8L);
|
|
|
|
assertThrows(IndexOutOfBoundsException.class, buf::readByte);
|
|
|
|
// Is it implementation dependent if the capacity increased by *exactly* the requested size, or more.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void ensureWritableMustExpandCapacityOfEmptyCompositeBuffer() {
|
|
|
|
try (Allocator allocator = Allocator.heap();
|
|
|
|
Buf buf = allocator.compose()) {
|
|
|
|
assertThat(buf.writableBytes()).isEqualTo(0);
|
|
|
|
buf.ensureWritable(8);
|
|
|
|
assertThat(buf.writableBytes()).isGreaterThanOrEqualTo(8);
|
|
|
|
buf.writeLong(0xA1A2A3A4A5A6A7A8L);
|
|
|
|
assertThat(buf.readableBytes()).isEqualTo(8);
|
|
|
|
assertThat(buf.readLong()).isEqualTo(0xA1A2A3A4A5A6A7A8L);
|
|
|
|
assertThrows(IndexOutOfBoundsException.class, buf::readByte);
|
|
|
|
// Is it implementation dependent if the capacity increased by *exactly* the requested size, or more.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-16 19:27:21 +01:00
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("nonSliceAllocators")
|
|
|
|
public void mustBeAbleToSliceAfterEnsureWritable(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(4)) {
|
|
|
|
buf.ensureWritable(8);
|
|
|
|
assertThat(buf.writableBytes()).isGreaterThanOrEqualTo(8);
|
|
|
|
buf.writeLong(0x0102030405060708L);
|
|
|
|
try (Buf slice = buf.slice()) {
|
|
|
|
assertEquals(0x0102030405060708L, slice.readLong());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-11 23:00:08 +01:00
|
|
|
// <editor-fold defaultstate="collapsed" desc="Primitive accessors tests.">
|
2020-11-10 12:09:14 +01:00
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void relativeReadOfByteMustNotBoundsCheckWhenReadOffsetAndSizeIsEqualToWriteOffset(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
assertEquals(0, buf.readableBytes());
|
|
|
|
assertEquals(Long.BYTES, buf.writableBytes());
|
|
|
|
byte value = 0x01;
|
|
|
|
buf.writeByte(value);
|
|
|
|
assertEquals(1, buf.readableBytes());
|
|
|
|
assertEquals(7, buf.writableBytes());
|
|
|
|
assertEquals(value, buf.readByte());
|
|
|
|
assertEquals(0, buf.readableBytes());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void relativeReadOfByteMustReadWithDefaultEndianByteOrder(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
buf.order(ByteOrder.BIG_ENDIAN);
|
|
|
|
assertEquals(0, buf.readableBytes());
|
|
|
|
assertEquals(Long.BYTES, buf.writableBytes());
|
|
|
|
byte value = 0x01;
|
|
|
|
buf.writeByte(value);
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.setByte(0, (byte) 0x10);
|
2020-11-10 12:09:14 +01:00
|
|
|
assertEquals(1, buf.readableBytes());
|
|
|
|
assertEquals(7, buf.writableBytes());
|
|
|
|
assertEquals(0x10, buf.readByte());
|
|
|
|
assertEquals(0, buf.readableBytes());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void relativeReadOfByteMustBoundsCheckWhenReadOffsetAndSizeIsBeyondWriteOffset(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
assertEquals(0, buf.readableBytes());
|
|
|
|
assertEquals(Long.BYTES, buf.writableBytes());
|
|
|
|
byte value = 0x01;
|
|
|
|
buf.writeByte(value);
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.readerOffset(1);
|
2020-11-10 12:09:14 +01:00
|
|
|
assertEquals(0, buf.readableBytes());
|
|
|
|
assertEquals(7, buf.writableBytes());
|
|
|
|
assertThrows(IndexOutOfBoundsException.class, buf::readByte);
|
|
|
|
assertEquals(0, buf.readableBytes());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedGetOfByteMustBoundsCheckOnNegativeOffset(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
2020-11-11 23:00:08 +01:00
|
|
|
assertThrows(IndexOutOfBoundsException.class, () -> buf.getByte(-1));
|
2020-11-10 12:09:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedGetOfByteMustNotBoundsCheckWhenReadOffsetAndSizeIsEqualToWriteOffset(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
Generate complete and tested Buf API
Motivation:
The Buf API is wide, taking into account signedness, endian-ness, and so on, for all primitive
types.
Modification:
A code generator has been added which add API, implementation, and tests, to the Buf, BBuf,
and BBufTest files respectively.
Result:
We have a complete and fully tested accessor API in the Buf interface, which is still somewhat
easy to bulk-modify using the code generator.
2020-09-10 18:05:36 +02:00
|
|
|
byte value = 0x01;
|
|
|
|
buf.writeByte(value);
|
2020-11-11 23:00:08 +01:00
|
|
|
assertEquals(value, buf.getByte(0));
|
2020-11-10 12:09:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedGetOfByteMustReadWithDefaultEndianByteOrder(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
buf.order(ByteOrder.BIG_ENDIAN);
|
Generate complete and tested Buf API
Motivation:
The Buf API is wide, taking into account signedness, endian-ness, and so on, for all primitive
types.
Modification:
A code generator has been added which add API, implementation, and tests, to the Buf, BBuf,
and BBufTest files respectively.
Result:
We have a complete and fully tested accessor API in the Buf interface, which is still somewhat
easy to bulk-modify using the code generator.
2020-09-10 18:05:36 +02:00
|
|
|
byte value = 0x01;
|
2020-11-10 12:09:14 +01:00
|
|
|
buf.writeByte(value);
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.setByte(0, (byte) 0x10);
|
|
|
|
assertEquals(0x10, buf.getByte(0));
|
Generate complete and tested Buf API
Motivation:
The Buf API is wide, taking into account signedness, endian-ness, and so on, for all primitive
types.
Modification:
A code generator has been added which add API, implementation, and tests, to the Buf, BBuf,
and BBufTest files respectively.
Result:
We have a complete and fully tested accessor API in the Buf interface, which is still somewhat
easy to bulk-modify using the code generator.
2020-09-10 18:05:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-10 12:09:14 +01:00
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedGetOfByteMustBoundsCheckWhenReadOffsetAndSizeIsGreaterThanWriteOffset(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
Generate complete and tested Buf API
Motivation:
The Buf API is wide, taking into account signedness, endian-ness, and so on, for all primitive
types.
Modification:
A code generator has been added which add API, implementation, and tests, to the Buf, BBuf,
and BBufTest files respectively.
Result:
We have a complete and fully tested accessor API in the Buf interface, which is still somewhat
easy to bulk-modify using the code generator.
2020-09-10 18:05:36 +02:00
|
|
|
byte value = 0x01;
|
2020-11-10 12:09:14 +01:00
|
|
|
buf.writeByte(value);
|
2020-11-11 23:00:08 +01:00
|
|
|
assertThrows(IndexOutOfBoundsException.class, () -> buf.getByte(-1));
|
2020-11-10 12:09:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedGetOfByteMustBoundsCheckWhenReadOffsetIsGreaterThanWriteOffset(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
2020-11-11 23:00:08 +01:00
|
|
|
assertThrows(IndexOutOfBoundsException.class, () -> buf.getByte(0));
|
2020-11-10 12:09:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void relativeReadOfUnsignedByteMustNotBoundsCheckWhenReadOffsetAndSizeIsEqualToWriteOffset(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
assertEquals(0, buf.readableBytes());
|
|
|
|
assertEquals(Long.BYTES, buf.writableBytes());
|
|
|
|
int value = 0x01;
|
|
|
|
buf.writeUnsignedByte(value);
|
|
|
|
assertEquals(1, buf.readableBytes());
|
|
|
|
assertEquals(7, buf.writableBytes());
|
|
|
|
assertEquals(value, buf.readUnsignedByte());
|
|
|
|
assertEquals(0, buf.readableBytes());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void relativeReadOfUnsignedByteMustReadWithDefaultEndianByteOrder(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
buf.order(ByteOrder.BIG_ENDIAN);
|
|
|
|
assertEquals(0, buf.readableBytes());
|
|
|
|
assertEquals(Long.BYTES, buf.writableBytes());
|
Generate complete and tested Buf API
Motivation:
The Buf API is wide, taking into account signedness, endian-ness, and so on, for all primitive
types.
Modification:
A code generator has been added which add API, implementation, and tests, to the Buf, BBuf,
and BBufTest files respectively.
Result:
We have a complete and fully tested accessor API in the Buf interface, which is still somewhat
easy to bulk-modify using the code generator.
2020-09-10 18:05:36 +02:00
|
|
|
int value = 0x01;
|
|
|
|
buf.writeUnsignedByte(value);
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.setByte(0, (byte) 0x10);
|
2020-11-10 12:09:14 +01:00
|
|
|
assertEquals(1, buf.readableBytes());
|
|
|
|
assertEquals(7, buf.writableBytes());
|
|
|
|
assertEquals(0x10, buf.readUnsignedByte());
|
|
|
|
assertEquals(0, buf.readableBytes());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void relativeReadOfUnsignedByteMustBoundsCheckWhenReadOffsetAndSizeIsBeyondWriteOffset(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
assertEquals(0, buf.readableBytes());
|
|
|
|
assertEquals(Long.BYTES, buf.writableBytes());
|
Generate complete and tested Buf API
Motivation:
The Buf API is wide, taking into account signedness, endian-ness, and so on, for all primitive
types.
Modification:
A code generator has been added which add API, implementation, and tests, to the Buf, BBuf,
and BBufTest files respectively.
Result:
We have a complete and fully tested accessor API in the Buf interface, which is still somewhat
easy to bulk-modify using the code generator.
2020-09-10 18:05:36 +02:00
|
|
|
int value = 0x01;
|
2020-11-10 12:09:14 +01:00
|
|
|
buf.writeUnsignedByte(value);
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.readerOffset(1);
|
2020-11-10 12:09:14 +01:00
|
|
|
assertEquals(0, buf.readableBytes());
|
|
|
|
assertEquals(7, buf.writableBytes());
|
|
|
|
assertThrows(IndexOutOfBoundsException.class, () -> buf.readUnsignedByte());
|
|
|
|
assertEquals(0, buf.readableBytes());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedGetOfUnsignedByteMustBoundsCheckOnNegativeOffset(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
2020-11-11 23:00:08 +01:00
|
|
|
assertThrows(IndexOutOfBoundsException.class, () -> buf.getUnsignedByte(-1));
|
Generate complete and tested Buf API
Motivation:
The Buf API is wide, taking into account signedness, endian-ness, and so on, for all primitive
types.
Modification:
A code generator has been added which add API, implementation, and tests, to the Buf, BBuf,
and BBufTest files respectively.
Result:
We have a complete and fully tested accessor API in the Buf interface, which is still somewhat
easy to bulk-modify using the code generator.
2020-09-10 18:05:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-10 12:09:14 +01:00
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedGetOfUnsignedByteMustNotBoundsCheckWhenReadOffsetAndSizeIsEqualToWriteOffset(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
Generate complete and tested Buf API
Motivation:
The Buf API is wide, taking into account signedness, endian-ness, and so on, for all primitive
types.
Modification:
A code generator has been added which add API, implementation, and tests, to the Buf, BBuf,
and BBufTest files respectively.
Result:
We have a complete and fully tested accessor API in the Buf interface, which is still somewhat
easy to bulk-modify using the code generator.
2020-09-10 18:05:36 +02:00
|
|
|
int value = 0x01;
|
2020-11-10 12:09:14 +01:00
|
|
|
buf.writeUnsignedByte(value);
|
2020-11-11 23:00:08 +01:00
|
|
|
assertEquals(value, buf.getUnsignedByte(0));
|
2020-10-12 16:26:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-10 12:09:14 +01:00
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedGetOfUnsignedByteMustReadWithDefaultEndianByteOrder(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
buf.order(ByteOrder.BIG_ENDIAN);
|
|
|
|
int value = 0x01;
|
|
|
|
buf.writeUnsignedByte(value);
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.setByte(0, (byte) 0x10);
|
|
|
|
assertEquals(0x10, buf.getUnsignedByte(0));
|
2020-08-24 16:03:02 +02:00
|
|
|
}
|
|
|
|
}
|
2020-07-24 19:38:48 +02:00
|
|
|
|
2020-11-10 12:09:14 +01:00
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedGetOfUnsignedByteMustBoundsCheckWhenReadOffsetAndSizeIsGreaterThanWriteOffset(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
int value = 0x01;
|
|
|
|
buf.writeUnsignedByte(value);
|
2020-11-11 23:00:08 +01:00
|
|
|
assertThrows(IndexOutOfBoundsException.class, () -> buf.getUnsignedByte(1));
|
Generate complete and tested Buf API
Motivation:
The Buf API is wide, taking into account signedness, endian-ness, and so on, for all primitive
types.
Modification:
A code generator has been added which add API, implementation, and tests, to the Buf, BBuf,
and BBufTest files respectively.
Result:
We have a complete and fully tested accessor API in the Buf interface, which is still somewhat
easy to bulk-modify using the code generator.
2020-09-10 18:05:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-10 12:09:14 +01:00
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedGetOfUnsignedByteMustBoundsCheckWhenReadOffsetIsGreaterThanWriteOffset(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
2020-11-11 23:00:08 +01:00
|
|
|
assertThrows(IndexOutOfBoundsException.class, () -> buf.getUnsignedByte(0));
|
Generate complete and tested Buf API
Motivation:
The Buf API is wide, taking into account signedness, endian-ness, and so on, for all primitive
types.
Modification:
A code generator has been added which add API, implementation, and tests, to the Buf, BBuf,
and BBufTest files respectively.
Result:
We have a complete and fully tested accessor API in the Buf interface, which is still somewhat
easy to bulk-modify using the code generator.
2020-09-10 18:05:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-10 12:09:14 +01:00
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void relativeWriteOfByteMustBoundsCheckWhenWriteOffsetAndSizeIsBeyondCapacity(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
assertEquals(Long.BYTES, buf.capacity());
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.writerOffset(8);
|
2020-11-10 12:09:14 +01:00
|
|
|
byte value = 0x01;
|
|
|
|
assertThrows(IndexOutOfBoundsException.class, () -> buf.writeByte(value));
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.writerOffset(Long.BYTES);
|
2020-11-10 12:09:14 +01:00
|
|
|
// Verify contents are unchanged.
|
|
|
|
assertEquals(0, buf.readLong());
|
2020-10-06 16:30:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-10 12:09:14 +01:00
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void relativeWriteOfByteMustHaveDefaultEndianByteOrder(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
buf.order(ByteOrder.BIG_ENDIAN);
|
|
|
|
byte value = 0x01;
|
|
|
|
buf.writeByte(value);
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.writerOffset(Long.BYTES);
|
2020-11-10 12:09:14 +01:00
|
|
|
assertEquals((byte) 0x01, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedSetOfByteMustBoundsCheckWhenWriteOffsetIsNegative(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
assertEquals(Long.BYTES, buf.capacity());
|
|
|
|
byte value = 0x01;
|
2020-11-11 23:00:08 +01:00
|
|
|
assertThrows(IndexOutOfBoundsException.class, () -> buf.setByte(-1, value));
|
|
|
|
buf.writerOffset(Long.BYTES);
|
2020-11-10 12:09:14 +01:00
|
|
|
// Verify contents are unchanged.
|
|
|
|
assertEquals(0, buf.readLong());
|
Generate complete and tested Buf API
Motivation:
The Buf API is wide, taking into account signedness, endian-ness, and so on, for all primitive
types.
Modification:
A code generator has been added which add API, implementation, and tests, to the Buf, BBuf,
and BBufTest files respectively.
Result:
We have a complete and fully tested accessor API in the Buf interface, which is still somewhat
easy to bulk-modify using the code generator.
2020-09-10 18:05:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-10 12:09:14 +01:00
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedSetOfByteMustBoundsCheckWhenWriteOffsetAndSizeIsBeyondCapacity(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
assertEquals(Long.BYTES, buf.capacity());
|
|
|
|
byte value = 0x01;
|
2020-11-11 23:00:08 +01:00
|
|
|
assertThrows(IndexOutOfBoundsException.class, () -> buf.setByte(8, value));
|
|
|
|
buf.writerOffset(Long.BYTES);
|
2020-11-10 12:09:14 +01:00
|
|
|
// Verify contents are unchanged.
|
|
|
|
assertEquals(0, buf.readLong());
|
2020-10-06 16:30:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-10 12:09:14 +01:00
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedSetOfByteMustHaveDefaultEndianByteOrder(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
buf.order(ByteOrder.BIG_ENDIAN);
|
|
|
|
byte value = 0x01;
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.setByte(0, value);
|
|
|
|
buf.writerOffset(Long.BYTES);
|
2020-11-10 12:09:14 +01:00
|
|
|
assertEquals((byte) 0x01, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void relativeWriteOfUnsignedByteMustBoundsCheckWhenWriteOffsetAndSizeIsBeyondCapacity(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
assertEquals(Long.BYTES, buf.capacity());
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.writerOffset(8);
|
2020-11-10 12:09:14 +01:00
|
|
|
int value = 0x01;
|
|
|
|
assertThrows(IndexOutOfBoundsException.class, () -> buf.writeUnsignedByte(value));
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.writerOffset(Long.BYTES);
|
2020-11-10 12:09:14 +01:00
|
|
|
// Verify contents are unchanged.
|
|
|
|
assertEquals(0, buf.readLong());
|
Generate complete and tested Buf API
Motivation:
The Buf API is wide, taking into account signedness, endian-ness, and so on, for all primitive
types.
Modification:
A code generator has been added which add API, implementation, and tests, to the Buf, BBuf,
and BBufTest files respectively.
Result:
We have a complete and fully tested accessor API in the Buf interface, which is still somewhat
easy to bulk-modify using the code generator.
2020-09-10 18:05:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-10 12:09:14 +01:00
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void relativeWriteOfUnsignedByteMustHaveDefaultEndianByteOrder(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
buf.order(ByteOrder.BIG_ENDIAN);
|
|
|
|
int value = 0x01;
|
|
|
|
buf.writeUnsignedByte(value);
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.writerOffset(Long.BYTES);
|
2020-11-10 12:09:14 +01:00
|
|
|
assertEquals((byte) 0x01, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedSetOfUnsignedByteMustBoundsCheckWhenWriteOffsetIsNegative(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
assertEquals(Long.BYTES, buf.capacity());
|
|
|
|
int value = 0x01;
|
2020-11-11 23:00:08 +01:00
|
|
|
assertThrows(IndexOutOfBoundsException.class, () -> buf.setUnsignedByte(-1, value));
|
|
|
|
buf.writerOffset(Long.BYTES);
|
2020-11-10 12:09:14 +01:00
|
|
|
// Verify contents are unchanged.
|
|
|
|
assertEquals(0, buf.readLong());
|
Generate complete and tested Buf API
Motivation:
The Buf API is wide, taking into account signedness, endian-ness, and so on, for all primitive
types.
Modification:
A code generator has been added which add API, implementation, and tests, to the Buf, BBuf,
and BBufTest files respectively.
Result:
We have a complete and fully tested accessor API in the Buf interface, which is still somewhat
easy to bulk-modify using the code generator.
2020-09-10 18:05:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-10 12:09:14 +01:00
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedSetOfUnsignedByteMustBoundsCheckWhenWriteOffsetAndSizeIsBeyondCapacity(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
assertEquals(Long.BYTES, buf.capacity());
|
|
|
|
int value = 0x01;
|
2020-11-11 23:00:08 +01:00
|
|
|
assertThrows(IndexOutOfBoundsException.class, () -> buf.setUnsignedByte(8, value));
|
|
|
|
buf.writerOffset(Long.BYTES);
|
2020-11-10 12:09:14 +01:00
|
|
|
// Verify contents are unchanged.
|
|
|
|
assertEquals(0, buf.readLong());
|
2020-10-06 16:30:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-10 12:09:14 +01:00
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedSetOfUnsignedByteMustHaveDefaultEndianByteOrder(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
buf.order(ByteOrder.BIG_ENDIAN);
|
|
|
|
int value = 0x01;
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.setUnsignedByte(0, value);
|
|
|
|
buf.writerOffset(Long.BYTES);
|
2020-11-10 12:09:14 +01:00
|
|
|
assertEquals((byte) 0x01, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void relativeReadOfCharMustNotBoundsCheckWhenReadOffsetAndSizeIsEqualToWriteOffset(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
assertEquals(0, buf.readableBytes());
|
|
|
|
assertEquals(Long.BYTES, buf.writableBytes());
|
|
|
|
char value = 0x0102;
|
|
|
|
buf.writeChar(value);
|
|
|
|
assertEquals(2, buf.readableBytes());
|
|
|
|
assertEquals(6, buf.writableBytes());
|
|
|
|
assertEquals(value, buf.readChar());
|
|
|
|
assertEquals(0, buf.readableBytes());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void relativeReadOfCharMustReadWithDefaultEndianByteOrder(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
buf.order(ByteOrder.BIG_ENDIAN);
|
|
|
|
assertEquals(0, buf.readableBytes());
|
|
|
|
assertEquals(Long.BYTES, buf.writableBytes());
|
|
|
|
char value = 0x0102;
|
|
|
|
buf.writeChar(value);
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.setByte(0, (byte) 0x10);
|
2020-11-10 12:09:14 +01:00
|
|
|
assertEquals(2, buf.readableBytes());
|
|
|
|
assertEquals(6, buf.writableBytes());
|
|
|
|
assertEquals(0x1002, buf.readChar());
|
|
|
|
assertEquals(0, buf.readableBytes());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void relativeReadOfCharMustBoundsCheckWhenReadOffsetAndSizeIsBeyondWriteOffset(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
assertEquals(0, buf.readableBytes());
|
|
|
|
assertEquals(Long.BYTES, buf.writableBytes());
|
|
|
|
char value = 0x0102;
|
|
|
|
buf.writeChar(value);
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.readerOffset(1);
|
2020-11-10 12:09:14 +01:00
|
|
|
assertEquals(1, buf.readableBytes());
|
|
|
|
assertEquals(6, buf.writableBytes());
|
|
|
|
assertThrows(IndexOutOfBoundsException.class, buf::readChar);
|
|
|
|
assertEquals(1, buf.readableBytes());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedGetOfCharMustBoundsCheckOnNegativeOffset(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
2020-11-11 23:00:08 +01:00
|
|
|
assertThrows(IndexOutOfBoundsException.class, () -> buf.getChar(-1));
|
2020-11-10 12:09:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedGetOfCharMustNotBoundsCheckWhenReadOffsetAndSizeIsEqualToWriteOffset(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
char value = 0x0102;
|
|
|
|
buf.writeChar(value);
|
2020-11-11 23:00:08 +01:00
|
|
|
assertEquals(value, buf.getChar(0));
|
2020-11-10 12:09:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedGetOfCharMustReadWithDefaultEndianByteOrder(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
buf.order(ByteOrder.BIG_ENDIAN);
|
|
|
|
char value = 0x0102;
|
|
|
|
buf.writeChar(value);
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.setByte(0, (byte) 0x10);
|
|
|
|
assertEquals(0x1002, buf.getChar(0));
|
2020-11-10 12:09:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedGetOfCharMustBoundsCheckWhenReadOffsetAndSizeIsGreaterThanWriteOffset(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
char value = 0x0102;
|
|
|
|
buf.writeChar(value);
|
2020-11-11 23:00:08 +01:00
|
|
|
assertThrows(IndexOutOfBoundsException.class, () -> buf.getChar(1));
|
2020-11-10 12:09:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedGetOfCharMustBoundsCheckWhenReadOffsetIsGreaterThanWriteOffset(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
2020-11-11 23:00:08 +01:00
|
|
|
assertThrows(IndexOutOfBoundsException.class, () -> buf.getChar(0));
|
2020-11-10 12:09:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void relativeWriteOfCharMustBoundsCheckWhenWriteOffsetAndSizeIsBeyondCapacity(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
assertEquals(Long.BYTES, buf.capacity());
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.writerOffset(7);
|
2020-11-10 12:09:14 +01:00
|
|
|
char value = 0x0102;
|
|
|
|
assertThrows(IndexOutOfBoundsException.class, () -> buf.writeChar(value));
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.writerOffset(Long.BYTES);
|
2020-11-10 12:09:14 +01:00
|
|
|
// Verify contents are unchanged.
|
|
|
|
assertEquals(0, buf.readLong());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void relativeWriteOfCharMustHaveDefaultEndianByteOrder(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
buf.order(ByteOrder.BIG_ENDIAN);
|
|
|
|
char value = 0x0102;
|
|
|
|
buf.writeChar(value);
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.writerOffset(Long.BYTES);
|
2020-11-10 12:09:14 +01:00
|
|
|
assertEquals((byte) 0x01, buf.readByte());
|
|
|
|
assertEquals((byte) 0x02, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedSetOfCharMustBoundsCheckWhenWriteOffsetIsNegative(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
assertEquals(Long.BYTES, buf.capacity());
|
|
|
|
char value = 0x0102;
|
2020-11-11 23:00:08 +01:00
|
|
|
assertThrows(IndexOutOfBoundsException.class, () -> buf.setChar(-1, value));
|
|
|
|
buf.writerOffset(Long.BYTES);
|
2020-11-10 12:09:14 +01:00
|
|
|
// Verify contents are unchanged.
|
|
|
|
assertEquals(0, buf.readLong());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedSetOfCharMustBoundsCheckWhenWriteOffsetAndSizeIsBeyondCapacity(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
assertEquals(Long.BYTES, buf.capacity());
|
|
|
|
char value = 0x0102;
|
2020-11-11 23:00:08 +01:00
|
|
|
assertThrows(IndexOutOfBoundsException.class, () -> buf.setChar(7, value));
|
|
|
|
buf.writerOffset(Long.BYTES);
|
2020-11-10 12:09:14 +01:00
|
|
|
// Verify contents are unchanged.
|
|
|
|
assertEquals(0, buf.readLong());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedSetOfCharMustHaveDefaultEndianByteOrder(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
buf.order(ByteOrder.BIG_ENDIAN);
|
|
|
|
char value = 0x0102;
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.setChar(0, value);
|
|
|
|
buf.writerOffset(Long.BYTES);
|
2020-11-10 12:09:14 +01:00
|
|
|
assertEquals((byte) 0x01, buf.readByte());
|
|
|
|
assertEquals((byte) 0x02, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void relativeReadOfShortMustNotBoundsCheckWhenReadOffsetAndSizeIsEqualToWriteOffset(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
assertEquals(0, buf.readableBytes());
|
|
|
|
assertEquals(Long.BYTES, buf.writableBytes());
|
|
|
|
short value = 0x0102;
|
|
|
|
buf.writeShort(value);
|
|
|
|
assertEquals(2, buf.readableBytes());
|
|
|
|
assertEquals(6, buf.writableBytes());
|
|
|
|
assertEquals(value, buf.readShort());
|
|
|
|
assertEquals(0, buf.readableBytes());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void relativeReadOfShortMustReadWithDefaultEndianByteOrder(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
buf.order(ByteOrder.BIG_ENDIAN);
|
|
|
|
assertEquals(0, buf.readableBytes());
|
|
|
|
assertEquals(Long.BYTES, buf.writableBytes());
|
|
|
|
short value = 0x0102;
|
|
|
|
buf.writeShort(value);
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.setByte(0, (byte) 0x10);
|
2020-11-10 12:09:14 +01:00
|
|
|
assertEquals(2, buf.readableBytes());
|
|
|
|
assertEquals(6, buf.writableBytes());
|
|
|
|
assertEquals(0x1002, buf.readShort());
|
|
|
|
assertEquals(0, buf.readableBytes());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void relativeReadOfShortMustBoundsCheckWhenReadOffsetAndSizeIsBeyondWriteOffset(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
assertEquals(0, buf.readableBytes());
|
|
|
|
assertEquals(Long.BYTES, buf.writableBytes());
|
|
|
|
short value = 0x0102;
|
|
|
|
buf.writeShort(value);
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.readerOffset(1);
|
2020-11-10 12:09:14 +01:00
|
|
|
assertEquals(1, buf.readableBytes());
|
|
|
|
assertEquals(6, buf.writableBytes());
|
|
|
|
assertThrows(IndexOutOfBoundsException.class, buf::readShort);
|
|
|
|
assertEquals(1, buf.readableBytes());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedGetOfShortMustBoundsCheckOnNegativeOffset(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
2020-11-11 23:00:08 +01:00
|
|
|
assertThrows(IndexOutOfBoundsException.class, () -> buf.getShort(-1));
|
2020-11-10 12:09:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedGetOfShortMustNotBoundsCheckWhenReadOffsetAndSizeIsEqualToWriteOffset(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
short value = 0x0102;
|
|
|
|
buf.writeShort(value);
|
2020-11-11 23:00:08 +01:00
|
|
|
assertEquals(value, buf.getShort(0));
|
2020-11-10 12:09:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedGetOfShortMustReadWithDefaultEndianByteOrder(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
buf.order(ByteOrder.BIG_ENDIAN);
|
|
|
|
short value = 0x0102;
|
|
|
|
buf.writeShort(value);
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.setByte(0, (byte) 0x10);
|
|
|
|
assertEquals(0x1002, buf.getShort(0));
|
2020-11-10 12:09:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedGetOfShortMustBoundsCheckWhenReadOffsetAndSizeIsGreaterThanWriteOffset(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
short value = 0x0102;
|
|
|
|
buf.writeShort(value);
|
2020-11-11 23:00:08 +01:00
|
|
|
assertThrows(IndexOutOfBoundsException.class, () -> buf.getShort(1));
|
2020-11-10 12:09:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedGetOfShortMustBoundsCheckWhenReadOffsetIsGreaterThanWriteOffset(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
2020-11-11 23:00:08 +01:00
|
|
|
assertThrows(IndexOutOfBoundsException.class, () -> buf.getShort(0));
|
2020-11-10 12:09:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void relativeReadOfUnsignedShortMustNotBoundsCheckWhenReadOffsetAndSizeIsEqualToWriteOffset(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
assertEquals(0, buf.readableBytes());
|
|
|
|
assertEquals(Long.BYTES, buf.writableBytes());
|
|
|
|
int value = 0x0102;
|
|
|
|
buf.writeUnsignedShort(value);
|
|
|
|
assertEquals(2, buf.readableBytes());
|
|
|
|
assertEquals(6, buf.writableBytes());
|
|
|
|
assertEquals(value, buf.readUnsignedShort());
|
|
|
|
assertEquals(0, buf.readableBytes());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void relativeReadOfUnsignedShortMustReadWithDefaultEndianByteOrder(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
buf.order(ByteOrder.BIG_ENDIAN);
|
|
|
|
assertEquals(0, buf.readableBytes());
|
|
|
|
assertEquals(Long.BYTES, buf.writableBytes());
|
|
|
|
int value = 0x0102;
|
|
|
|
buf.writeUnsignedShort(value);
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.setByte(0, (byte) 0x10);
|
2020-11-10 12:09:14 +01:00
|
|
|
assertEquals(2, buf.readableBytes());
|
|
|
|
assertEquals(6, buf.writableBytes());
|
|
|
|
assertEquals(0x1002, buf.readUnsignedShort());
|
|
|
|
assertEquals(0, buf.readableBytes());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void relativeReadOfUnsignedShortMustBoundsCheckWhenReadOffsetAndSizeIsBeyondWriteOffset(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
assertEquals(0, buf.readableBytes());
|
|
|
|
assertEquals(Long.BYTES, buf.writableBytes());
|
|
|
|
int value = 0x0102;
|
|
|
|
buf.writeUnsignedShort(value);
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.readerOffset(1);
|
2020-11-10 12:09:14 +01:00
|
|
|
assertEquals(1, buf.readableBytes());
|
|
|
|
assertEquals(6, buf.writableBytes());
|
|
|
|
assertThrows(IndexOutOfBoundsException.class, buf::readUnsignedShort);
|
|
|
|
assertEquals(1, buf.readableBytes());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedGetOfUnsignedShortMustBoundsCheckOnNegativeOffset(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
2020-11-11 23:00:08 +01:00
|
|
|
assertThrows(IndexOutOfBoundsException.class, () -> buf.getUnsignedShort(-1));
|
2020-11-10 12:09:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedGetOfUnsignedShortMustNotBoundsCheckWhenReadOffsetAndSizeIsEqualToWriteOffset(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
int value = 0x0102;
|
|
|
|
buf.writeUnsignedShort(value);
|
2020-11-11 23:00:08 +01:00
|
|
|
assertEquals(value, buf.getUnsignedShort(0));
|
2020-11-10 12:09:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedGetOfUnsignedShortMustReadWithDefaultEndianByteOrder(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
buf.order(ByteOrder.BIG_ENDIAN);
|
|
|
|
int value = 0x0102;
|
|
|
|
buf.writeUnsignedShort(value);
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.setByte(0, (byte) 0x10);
|
|
|
|
assertEquals(0x1002, buf.getUnsignedShort(0));
|
2020-11-10 12:09:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedGetOfUnsignedShortMustBoundsCheckWhenReadOffsetAndSizeIsGreaterThanWriteOffset(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
int value = 0x0102;
|
|
|
|
buf.writeUnsignedShort(value);
|
2020-11-11 23:00:08 +01:00
|
|
|
assertThrows(IndexOutOfBoundsException.class, () -> buf.getUnsignedShort(1));
|
2020-11-10 12:09:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedGetOfUnsignedShortMustBoundsCheckWhenReadOffsetIsGreaterThanWriteOffset(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
2020-11-11 23:00:08 +01:00
|
|
|
assertThrows(IndexOutOfBoundsException.class, () -> buf.getUnsignedShort(0));
|
2020-11-10 12:09:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void relativeWriteOfShortMustBoundsCheckWhenWriteOffsetAndSizeIsBeyondCapacity(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
assertEquals(Long.BYTES, buf.capacity());
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.writerOffset(7);
|
2020-11-10 12:09:14 +01:00
|
|
|
short value = 0x0102;
|
|
|
|
assertThrows(IndexOutOfBoundsException.class, () -> buf.writeShort(value));
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.writerOffset(Long.BYTES);
|
2020-11-10 12:09:14 +01:00
|
|
|
// Verify contents are unchanged.
|
|
|
|
assertEquals(0, buf.readLong());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void relativeWriteOfShortMustHaveDefaultEndianByteOrder(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
buf.order(ByteOrder.BIG_ENDIAN);
|
|
|
|
short value = 0x0102;
|
|
|
|
buf.writeShort(value);
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.writerOffset(Long.BYTES);
|
2020-11-10 12:09:14 +01:00
|
|
|
assertEquals((byte) 0x01, buf.readByte());
|
|
|
|
assertEquals((byte) 0x02, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedSetOfShortMustBoundsCheckWhenWriteOffsetIsNegative(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
assertEquals(Long.BYTES, buf.capacity());
|
|
|
|
short value = 0x0102;
|
2020-11-11 23:00:08 +01:00
|
|
|
assertThrows(IndexOutOfBoundsException.class, () -> buf.setShort(-1, value));
|
|
|
|
buf.writerOffset(Long.BYTES);
|
2020-11-10 12:09:14 +01:00
|
|
|
// Verify contents are unchanged.
|
|
|
|
assertEquals(0, buf.readLong());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedSetOfShortMustBoundsCheckWhenWriteOffsetAndSizeIsBeyondCapacity(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
assertEquals(Long.BYTES, buf.capacity());
|
|
|
|
short value = 0x0102;
|
2020-11-11 23:00:08 +01:00
|
|
|
assertThrows(IndexOutOfBoundsException.class, () -> buf.setShort(7, value));
|
|
|
|
buf.writerOffset(Long.BYTES);
|
2020-11-10 12:09:14 +01:00
|
|
|
// Verify contents are unchanged.
|
|
|
|
assertEquals(0, buf.readLong());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedSetOfShortMustHaveDefaultEndianByteOrder(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
buf.order(ByteOrder.BIG_ENDIAN);
|
|
|
|
short value = 0x0102;
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.setShort(0, value);
|
|
|
|
buf.writerOffset(Long.BYTES);
|
2020-11-10 12:09:14 +01:00
|
|
|
assertEquals((byte) 0x01, buf.readByte());
|
|
|
|
assertEquals((byte) 0x02, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void relativeWriteOfUnsignedShortMustBoundsCheckWhenWriteOffsetAndSizeIsBeyondCapacity(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
assertEquals(Long.BYTES, buf.capacity());
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.writerOffset(7);
|
2020-11-10 12:09:14 +01:00
|
|
|
int value = 0x0102;
|
|
|
|
assertThrows(IndexOutOfBoundsException.class, () -> buf.writeUnsignedShort(value));
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.writerOffset(Long.BYTES);
|
2020-11-10 12:09:14 +01:00
|
|
|
// Verify contents are unchanged.
|
|
|
|
assertEquals(0, buf.readLong());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void relativeWriteOfUnsignedShortMustHaveDefaultEndianByteOrder(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
buf.order(ByteOrder.BIG_ENDIAN);
|
|
|
|
int value = 0x0102;
|
|
|
|
buf.writeUnsignedShort(value);
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.writerOffset(Long.BYTES);
|
2020-11-10 12:09:14 +01:00
|
|
|
assertEquals((byte) 0x01, buf.readByte());
|
|
|
|
assertEquals((byte) 0x02, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedSetOfUnsignedShortMustBoundsCheckWhenWriteOffsetIsNegative(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
assertEquals(Long.BYTES, buf.capacity());
|
|
|
|
int value = 0x0102;
|
2020-11-11 23:00:08 +01:00
|
|
|
assertThrows(IndexOutOfBoundsException.class, () -> buf.setUnsignedShort(-1, value));
|
|
|
|
buf.writerOffset(Long.BYTES);
|
2020-11-10 12:09:14 +01:00
|
|
|
// Verify contents are unchanged.
|
|
|
|
assertEquals(0, buf.readLong());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedSetOfUnsignedShortMustBoundsCheckWhenWriteOffsetAndSizeIsBeyondCapacity(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
assertEquals(Long.BYTES, buf.capacity());
|
|
|
|
int value = 0x0102;
|
2020-11-11 23:00:08 +01:00
|
|
|
assertThrows(IndexOutOfBoundsException.class, () -> buf.setUnsignedShort(7, value));
|
|
|
|
buf.writerOffset(Long.BYTES);
|
2020-11-10 12:09:14 +01:00
|
|
|
// Verify contents are unchanged.
|
|
|
|
assertEquals(0, buf.readLong());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedSetOfUnsignedShortMustHaveDefaultEndianByteOrder(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
buf.order(ByteOrder.BIG_ENDIAN);
|
|
|
|
int value = 0x0102;
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.setUnsignedShort(0, value);
|
|
|
|
buf.writerOffset(Long.BYTES);
|
2020-11-10 12:09:14 +01:00
|
|
|
assertEquals((byte) 0x01, buf.readByte());
|
|
|
|
assertEquals((byte) 0x02, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void relativeReadOfMediumMustNotBoundsCheckWhenReadOffsetAndSizeIsEqualToWriteOffset(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
assertEquals(0, buf.readableBytes());
|
|
|
|
assertEquals(Long.BYTES, buf.writableBytes());
|
|
|
|
int value = 0x010203;
|
|
|
|
buf.writeMedium(value);
|
|
|
|
assertEquals(3, buf.readableBytes());
|
|
|
|
assertEquals(5, buf.writableBytes());
|
|
|
|
assertEquals(value, buf.readMedium());
|
|
|
|
assertEquals(0, buf.readableBytes());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void relativeReadOfMediumMustReadWithDefaultEndianByteOrder(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
buf.order(ByteOrder.BIG_ENDIAN);
|
|
|
|
assertEquals(0, buf.readableBytes());
|
|
|
|
assertEquals(Long.BYTES, buf.writableBytes());
|
|
|
|
int value = 0x010203;
|
|
|
|
buf.writeMedium(value);
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.setByte(0, (byte) 0x10);
|
2020-11-10 12:09:14 +01:00
|
|
|
assertEquals(3, buf.readableBytes());
|
|
|
|
assertEquals(5, buf.writableBytes());
|
|
|
|
assertEquals(0x100203, buf.readMedium());
|
|
|
|
assertEquals(0, buf.readableBytes());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void relativeReadOfMediumMustBoundsCheckWhenReadOffsetAndSizeIsBeyondWriteOffset(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
assertEquals(0, buf.readableBytes());
|
|
|
|
assertEquals(Long.BYTES, buf.writableBytes());
|
|
|
|
int value = 0x010203;
|
|
|
|
buf.writeMedium(value);
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.readerOffset(1);
|
2020-11-10 12:09:14 +01:00
|
|
|
assertEquals(2, buf.readableBytes());
|
|
|
|
assertEquals(5, buf.writableBytes());
|
|
|
|
assertThrows(IndexOutOfBoundsException.class, buf::readMedium);
|
|
|
|
assertEquals(2, buf.readableBytes());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedGetOfMediumMustBoundsCheckOnNegativeOffset(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
2020-11-11 23:00:08 +01:00
|
|
|
assertThrows(IndexOutOfBoundsException.class, () -> buf.getMedium(-1));
|
2020-11-10 12:09:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedGetOfMediumMustNotBoundsCheckWhenReadOffsetAndSizeIsEqualToWriteOffset(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
int value = 0x010203;
|
|
|
|
buf.writeMedium(value);
|
2020-11-11 23:00:08 +01:00
|
|
|
assertEquals(value, buf.getMedium(0));
|
2020-11-10 12:09:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedGetOfMediumMustReadWithDefaultEndianByteOrder(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
buf.order(ByteOrder.BIG_ENDIAN);
|
|
|
|
int value = 0x010203;
|
|
|
|
buf.writeMedium(value);
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.setByte(0, (byte) 0x10);
|
|
|
|
assertEquals(0x100203, buf.getMedium(0));
|
2020-11-10 12:09:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedGetOfMediumMustBoundsCheckWhenReadOffsetAndSizeIsGreaterThanWriteOffset(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
int value = 0x010203;
|
|
|
|
buf.writeMedium(value);
|
2020-11-11 23:00:08 +01:00
|
|
|
assertThrows(IndexOutOfBoundsException.class, () -> buf.getMedium(1));
|
2020-11-10 12:09:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedGetOfMediumMustBoundsCheckWhenReadOffsetIsGreaterThanWriteOffset(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
2020-11-11 23:00:08 +01:00
|
|
|
assertThrows(IndexOutOfBoundsException.class, () -> buf.getMedium(0));
|
2020-11-10 12:09:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void relativeReadOfUnsignedMediumMustNotBoundsCheckWhenReadOffsetAndSizeIsEqualToWriteOffset(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
assertEquals(0, buf.readableBytes());
|
|
|
|
assertEquals(Long.BYTES, buf.writableBytes());
|
|
|
|
int value = 0x010203;
|
|
|
|
buf.writeUnsignedMedium(value);
|
|
|
|
assertEquals(3, buf.readableBytes());
|
|
|
|
assertEquals(5, buf.writableBytes());
|
|
|
|
assertEquals(value, buf.readUnsignedMedium());
|
|
|
|
assertEquals(0, buf.readableBytes());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void relativeReadOfUnsignedMediumMustReadWithDefaultEndianByteOrder(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
buf.order(ByteOrder.BIG_ENDIAN);
|
|
|
|
assertEquals(0, buf.readableBytes());
|
|
|
|
assertEquals(Long.BYTES, buf.writableBytes());
|
|
|
|
int value = 0x010203;
|
|
|
|
buf.writeUnsignedMedium(value);
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.setByte(0, (byte) 0x10);
|
2020-11-10 12:09:14 +01:00
|
|
|
assertEquals(3, buf.readableBytes());
|
|
|
|
assertEquals(5, buf.writableBytes());
|
|
|
|
assertEquals(0x100203, buf.readUnsignedMedium());
|
|
|
|
assertEquals(0, buf.readableBytes());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void relativeReadOfUnsignedMediumMustBoundsCheckWhenReadOffsetAndSizeIsBeyondWriteOffset(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
assertEquals(0, buf.readableBytes());
|
|
|
|
assertEquals(Long.BYTES, buf.writableBytes());
|
|
|
|
int value = 0x010203;
|
|
|
|
buf.writeUnsignedMedium(value);
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.readerOffset(1);
|
2020-11-10 12:09:14 +01:00
|
|
|
assertEquals(2, buf.readableBytes());
|
|
|
|
assertEquals(5, buf.writableBytes());
|
|
|
|
assertThrows(IndexOutOfBoundsException.class, buf::readUnsignedMedium);
|
|
|
|
assertEquals(2, buf.readableBytes());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedGetOfUnsignedMediumMustBoundsCheckOnNegativeOffset(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
2020-11-11 23:00:08 +01:00
|
|
|
assertThrows(IndexOutOfBoundsException.class, () -> buf.getUnsignedMedium(-1));
|
2020-11-10 12:09:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedGetOfUnsignedMediumMustNotBoundsCheckWhenReadOffsetAndSizeIsEqualToWriteOffset(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
int value = 0x010203;
|
|
|
|
buf.writeUnsignedMedium(value);
|
2020-11-11 23:00:08 +01:00
|
|
|
assertEquals(value, buf.getUnsignedMedium(0));
|
2020-11-10 12:09:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedGetOfUnsignedMediumMustReadWithDefaultEndianByteOrder(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
buf.order(ByteOrder.BIG_ENDIAN);
|
|
|
|
int value = 0x010203;
|
|
|
|
buf.writeUnsignedMedium(value);
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.setByte(0, (byte) 0x10);
|
|
|
|
assertEquals(0x100203, buf.getUnsignedMedium(0));
|
2020-11-10 12:09:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedGetOfUnsignedMediumMustBoundsCheckWhenReadOffsetAndSizeIsGreaterThanWriteOffset(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
int value = 0x010203;
|
|
|
|
buf.writeUnsignedMedium(value);
|
2020-11-11 23:00:08 +01:00
|
|
|
assertThrows(IndexOutOfBoundsException.class, () -> buf.getUnsignedMedium(1));
|
2020-11-10 12:09:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedGetOfUnsignedMediumMustBoundsCheckWhenReadOffsetIsGreaterThanWriteOffset(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
2020-11-11 23:00:08 +01:00
|
|
|
assertThrows(IndexOutOfBoundsException.class, () -> buf.getUnsignedMedium(0));
|
2020-11-10 12:09:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void relativeWriteOfMediumMustBoundsCheckWhenWriteOffsetAndSizeIsBeyondCapacity(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
assertEquals(Long.BYTES, buf.capacity());
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.writerOffset(6);
|
2020-11-10 12:09:14 +01:00
|
|
|
int value = 0x010203;
|
|
|
|
assertThrows(IndexOutOfBoundsException.class, () -> buf.writeMedium(value));
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.writerOffset(Long.BYTES);
|
2020-11-10 12:09:14 +01:00
|
|
|
// Verify contents are unchanged.
|
|
|
|
assertEquals(0, buf.readLong());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void relativeWriteOfMediumMustHaveDefaultEndianByteOrder(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
buf.order(ByteOrder.BIG_ENDIAN);
|
|
|
|
int value = 0x010203;
|
|
|
|
buf.writeMedium(value);
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.writerOffset(Long.BYTES);
|
2020-11-10 12:09:14 +01:00
|
|
|
assertEquals((byte) 0x01, buf.readByte());
|
|
|
|
assertEquals((byte) 0x02, buf.readByte());
|
|
|
|
assertEquals((byte) 0x03, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedSetOfMediumMustBoundsCheckWhenWriteOffsetIsNegative(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
assertEquals(Long.BYTES, buf.capacity());
|
|
|
|
int value = 0x010203;
|
2020-11-11 23:00:08 +01:00
|
|
|
assertThrows(IndexOutOfBoundsException.class, () -> buf.setMedium(-1, value));
|
|
|
|
buf.writerOffset(Long.BYTES);
|
2020-11-10 12:09:14 +01:00
|
|
|
// Verify contents are unchanged.
|
|
|
|
assertEquals(0, buf.readLong());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedSetOfMediumMustBoundsCheckWhenWriteOffsetAndSizeIsBeyondCapacity(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
assertEquals(Long.BYTES, buf.capacity());
|
|
|
|
int value = 0x010203;
|
2020-11-11 23:00:08 +01:00
|
|
|
assertThrows(IndexOutOfBoundsException.class, () -> buf.setMedium(6, value));
|
|
|
|
buf.writerOffset(Long.BYTES);
|
2020-11-10 12:09:14 +01:00
|
|
|
// Verify contents are unchanged.
|
|
|
|
assertEquals(0, buf.readLong());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedSetOfMediumMustHaveDefaultEndianByteOrder(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
buf.order(ByteOrder.BIG_ENDIAN);
|
|
|
|
int value = 0x010203;
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.setMedium(0, value);
|
|
|
|
buf.writerOffset(Long.BYTES);
|
2020-11-10 12:09:14 +01:00
|
|
|
assertEquals((byte) 0x01, buf.readByte());
|
|
|
|
assertEquals((byte) 0x02, buf.readByte());
|
|
|
|
assertEquals((byte) 0x03, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void relativeWriteOfUnsignedMediumMustBoundsCheckWhenWriteOffsetAndSizeIsBeyondCapacity(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
assertEquals(Long.BYTES, buf.capacity());
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.writerOffset(6);
|
2020-11-10 12:09:14 +01:00
|
|
|
int value = 0x010203;
|
|
|
|
assertThrows(IndexOutOfBoundsException.class, () -> buf.writeUnsignedMedium(value));
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.writerOffset(Long.BYTES);
|
2020-11-10 12:09:14 +01:00
|
|
|
// Verify contents are unchanged.
|
|
|
|
assertEquals(0, buf.readLong());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void relativeWriteOfUnsignedMediumMustHaveDefaultEndianByteOrder(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
buf.order(ByteOrder.BIG_ENDIAN);
|
|
|
|
int value = 0x010203;
|
|
|
|
buf.writeUnsignedMedium(value);
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.writerOffset(Long.BYTES);
|
2020-11-10 12:09:14 +01:00
|
|
|
assertEquals((byte) 0x01, buf.readByte());
|
|
|
|
assertEquals((byte) 0x02, buf.readByte());
|
|
|
|
assertEquals((byte) 0x03, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedSetOfUnsignedMediumMustBoundsCheckWhenWriteOffsetIsNegative(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
assertEquals(Long.BYTES, buf.capacity());
|
|
|
|
int value = 0x010203;
|
2020-11-11 23:00:08 +01:00
|
|
|
assertThrows(IndexOutOfBoundsException.class, () -> buf.setUnsignedMedium(-1, value));
|
|
|
|
buf.writerOffset(Long.BYTES);
|
2020-11-10 12:09:14 +01:00
|
|
|
// Verify contents are unchanged.
|
|
|
|
assertEquals(0, buf.readLong());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedSetOfUnsignedMediumMustBoundsCheckWhenWriteOffsetAndSizeIsBeyondCapacity(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
assertEquals(Long.BYTES, buf.capacity());
|
|
|
|
int value = 0x010203;
|
2020-11-11 23:00:08 +01:00
|
|
|
assertThrows(IndexOutOfBoundsException.class, () -> buf.setUnsignedMedium(6, value));
|
|
|
|
buf.writerOffset(Long.BYTES);
|
2020-11-10 12:09:14 +01:00
|
|
|
// Verify contents are unchanged.
|
|
|
|
assertEquals(0, buf.readLong());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedSetOfUnsignedMediumMustHaveDefaultEndianByteOrder(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
buf.order(ByteOrder.BIG_ENDIAN);
|
|
|
|
int value = 0x010203;
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.setUnsignedMedium(0, value);
|
|
|
|
buf.writerOffset(Long.BYTES);
|
2020-11-10 12:09:14 +01:00
|
|
|
assertEquals((byte) 0x01, buf.readByte());
|
|
|
|
assertEquals((byte) 0x02, buf.readByte());
|
|
|
|
assertEquals((byte) 0x03, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void relativeReadOfIntMustNotBoundsCheckWhenReadOffsetAndSizeIsEqualToWriteOffset(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
assertEquals(0, buf.readableBytes());
|
|
|
|
assertEquals(Long.BYTES, buf.writableBytes());
|
|
|
|
int value = 0x01020304;
|
|
|
|
buf.writeInt(value);
|
|
|
|
assertEquals(4, buf.readableBytes());
|
|
|
|
assertEquals(4, buf.writableBytes());
|
|
|
|
assertEquals(value, buf.readInt());
|
|
|
|
assertEquals(0, buf.readableBytes());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void relativeReadOfIntMustReadWithDefaultEndianByteOrder(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
buf.order(ByteOrder.BIG_ENDIAN);
|
|
|
|
assertEquals(0, buf.readableBytes());
|
|
|
|
assertEquals(Long.BYTES, buf.writableBytes());
|
|
|
|
int value = 0x01020304;
|
|
|
|
buf.writeInt(value);
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.setByte(0, (byte) 0x10);
|
2020-11-10 12:09:14 +01:00
|
|
|
assertEquals(4, buf.readableBytes());
|
|
|
|
assertEquals(4, buf.writableBytes());
|
|
|
|
assertEquals(0x10020304, buf.readInt());
|
|
|
|
assertEquals(0, buf.readableBytes());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void relativeReadOfIntMustBoundsCheckWhenReadOffsetAndSizeIsBeyondWriteOffset(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
assertEquals(0, buf.readableBytes());
|
|
|
|
assertEquals(Long.BYTES, buf.writableBytes());
|
|
|
|
int value = 0x01020304;
|
|
|
|
buf.writeInt(value);
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.readerOffset(1);
|
2020-11-10 12:09:14 +01:00
|
|
|
assertEquals(3, buf.readableBytes());
|
|
|
|
assertEquals(4, buf.writableBytes());
|
|
|
|
assertThrows(IndexOutOfBoundsException.class, buf::readInt);
|
|
|
|
assertEquals(3, buf.readableBytes());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedGetOfIntMustBoundsCheckOnNegativeOffset(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
2020-11-11 23:00:08 +01:00
|
|
|
assertThrows(IndexOutOfBoundsException.class, () -> buf.getInt(-1));
|
2020-11-10 12:09:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedGetOfIntMustNotBoundsCheckWhenReadOffsetAndSizeIsEqualToWriteOffset(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
int value = 0x01020304;
|
|
|
|
buf.writeInt(value);
|
2020-11-11 23:00:08 +01:00
|
|
|
assertEquals(value, buf.getInt(0));
|
2020-11-10 12:09:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedGetOfIntMustReadWithDefaultEndianByteOrder(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
buf.order(ByteOrder.BIG_ENDIAN);
|
|
|
|
int value = 0x01020304;
|
|
|
|
buf.writeInt(value);
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.setByte(0, (byte) 0x10);
|
|
|
|
assertEquals(0x10020304, buf.getInt(0));
|
2020-11-10 12:09:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedGetOfIntMustBoundsCheckWhenReadOffsetAndSizeIsGreaterThanWriteOffset(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
int value = 0x01020304;
|
|
|
|
buf.writeInt(value);
|
2020-11-11 23:00:08 +01:00
|
|
|
assertThrows(IndexOutOfBoundsException.class, () -> buf.getInt(-1));
|
2020-11-10 12:09:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedGetOfIntMustBoundsCheckWhenReadOffsetIsGreaterThanWriteOffset(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
2020-11-11 23:00:08 +01:00
|
|
|
assertThrows(IndexOutOfBoundsException.class, () -> buf.getInt(0));
|
2020-11-10 12:09:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void relativeReadOfUnsignedIntMustNotBoundsCheckWhenReadOffsetAndSizeIsEqualToWriteOffset(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
assertEquals(0, buf.readableBytes());
|
|
|
|
assertEquals(Long.BYTES, buf.writableBytes());
|
|
|
|
long value = 0x01020304;
|
|
|
|
buf.writeUnsignedInt(value);
|
|
|
|
assertEquals(4, buf.readableBytes());
|
|
|
|
assertEquals(4, buf.writableBytes());
|
|
|
|
assertEquals(value, buf.readUnsignedInt());
|
|
|
|
assertEquals(0, buf.readableBytes());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void relativeReadOfUnsignedIntMustReadWithDefaultEndianByteOrder(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
buf.order(ByteOrder.BIG_ENDIAN);
|
|
|
|
assertEquals(0, buf.readableBytes());
|
|
|
|
assertEquals(Long.BYTES, buf.writableBytes());
|
|
|
|
long value = 0x01020304;
|
|
|
|
buf.writeUnsignedInt(value);
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.setByte(0, (byte) 0x10);
|
2020-11-10 12:09:14 +01:00
|
|
|
assertEquals(4, buf.readableBytes());
|
|
|
|
assertEquals(4, buf.writableBytes());
|
|
|
|
assertEquals(0x10020304, buf.readUnsignedInt());
|
|
|
|
assertEquals(0, buf.readableBytes());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void relativeReadOfUnsignedIntMustBoundsCheckWhenReadOffsetAndSizeIsBeyondWriteOffset(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
assertEquals(0, buf.readableBytes());
|
|
|
|
assertEquals(Long.BYTES, buf.writableBytes());
|
|
|
|
long value = 0x01020304;
|
|
|
|
buf.writeUnsignedInt(value);
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.readerOffset(1);
|
2020-11-10 12:09:14 +01:00
|
|
|
assertEquals(3, buf.readableBytes());
|
|
|
|
assertEquals(4, buf.writableBytes());
|
|
|
|
assertThrows(IndexOutOfBoundsException.class, buf::readUnsignedInt);
|
|
|
|
assertEquals(3, buf.readableBytes());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedGetOfUnsignedIntMustBoundsCheckOnNegativeOffset(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
2020-11-11 23:00:08 +01:00
|
|
|
assertThrows(IndexOutOfBoundsException.class, () -> buf.getUnsignedInt(-1));
|
2020-11-10 12:09:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedGetOfUnsignedIntMustNotBoundsCheckWhenReadOffsetAndSizeIsEqualToWriteOffset(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
long value = 0x01020304;
|
|
|
|
buf.writeUnsignedInt(value);
|
2020-11-11 23:00:08 +01:00
|
|
|
assertEquals(value, buf.getUnsignedInt(0));
|
2020-11-10 12:09:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedGetOfUnsignedIntMustReadWithDefaultEndianByteOrder(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
buf.order(ByteOrder.BIG_ENDIAN);
|
|
|
|
long value = 0x01020304;
|
|
|
|
buf.writeUnsignedInt(value);
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.setByte(0, (byte) 0x10);
|
|
|
|
assertEquals(0x10020304, buf.getUnsignedInt(0));
|
2020-11-10 12:09:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedGetOfUnsignedIntMustBoundsCheckWhenReadOffsetAndSizeIsGreaterThanWriteOffset(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
long value = 0x01020304;
|
|
|
|
buf.writeUnsignedInt(value);
|
2020-11-11 23:00:08 +01:00
|
|
|
assertThrows(IndexOutOfBoundsException.class, () -> buf.getUnsignedInt(1));
|
2020-11-10 12:09:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedGetOfUnsignedIntMustBoundsCheckWhenReadOffsetIsGreaterThanWriteOffset(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
2020-11-11 23:00:08 +01:00
|
|
|
assertThrows(IndexOutOfBoundsException.class, () -> buf.getUnsignedInt(0));
|
2020-11-10 12:09:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void relativeWriteOfIntMustBoundsCheckWhenWriteOffsetAndSizeIsBeyondCapacity(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
assertEquals(Long.BYTES, buf.capacity());
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.writerOffset(5);
|
2020-11-10 12:09:14 +01:00
|
|
|
int value = 0x01020304;
|
|
|
|
assertThrows(IndexOutOfBoundsException.class, () -> buf.writeInt(value));
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.writerOffset(Long.BYTES);
|
2020-11-10 12:09:14 +01:00
|
|
|
// Verify contents are unchanged.
|
|
|
|
assertEquals(0, buf.readLong());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void relativeWriteOfIntMustHaveDefaultEndianByteOrder(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
buf.order(ByteOrder.BIG_ENDIAN);
|
|
|
|
int value = 0x01020304;
|
|
|
|
buf.writeInt(value);
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.writerOffset(Long.BYTES);
|
2020-11-10 12:09:14 +01:00
|
|
|
assertEquals((byte) 0x01, buf.readByte());
|
|
|
|
assertEquals((byte) 0x02, buf.readByte());
|
|
|
|
assertEquals((byte) 0x03, buf.readByte());
|
|
|
|
assertEquals((byte) 0x04, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedSetOfIntMustBoundsCheckWhenWriteOffsetIsNegative(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
assertEquals(Long.BYTES, buf.capacity());
|
|
|
|
int value = 0x01020304;
|
2020-11-11 23:00:08 +01:00
|
|
|
assertThrows(IndexOutOfBoundsException.class, () -> buf.setInt(-1, value));
|
|
|
|
buf.writerOffset(Long.BYTES);
|
2020-11-10 12:09:14 +01:00
|
|
|
// Verify contents are unchanged.
|
|
|
|
assertEquals(0, buf.readLong());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedSetOfIntMustBoundsCheckWhenWriteOffsetAndSizeIsBeyondCapacity(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
assertEquals(Long.BYTES, buf.capacity());
|
|
|
|
int value = 0x01020304;
|
2020-11-11 23:00:08 +01:00
|
|
|
assertThrows(IndexOutOfBoundsException.class, () -> buf.setInt(5, value));
|
|
|
|
buf.writerOffset(Long.BYTES);
|
2020-11-10 12:09:14 +01:00
|
|
|
// Verify contents are unchanged.
|
|
|
|
assertEquals(0, buf.readLong());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedSetOfIntMustHaveDefaultEndianByteOrder(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
buf.order(ByteOrder.BIG_ENDIAN);
|
|
|
|
int value = 0x01020304;
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.setInt(0, value);
|
|
|
|
buf.writerOffset(Long.BYTES);
|
2020-11-10 12:09:14 +01:00
|
|
|
assertEquals((byte) 0x01, buf.readByte());
|
|
|
|
assertEquals((byte) 0x02, buf.readByte());
|
|
|
|
assertEquals((byte) 0x03, buf.readByte());
|
|
|
|
assertEquals((byte) 0x04, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void relativeWriteOfUnsignedIntMustBoundsCheckWhenWriteOffsetAndSizeIsBeyondCapacity(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
assertEquals(Long.BYTES, buf.capacity());
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.writerOffset(5);
|
2020-11-10 12:09:14 +01:00
|
|
|
long value = 0x01020304;
|
|
|
|
assertThrows(IndexOutOfBoundsException.class, () -> buf.writeUnsignedInt(value));
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.writerOffset(Long.BYTES);
|
2020-11-10 12:09:14 +01:00
|
|
|
// Verify contents are unchanged.
|
|
|
|
assertEquals(0, buf.readLong());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void relativeWriteOfUnsignedIntMustHaveDefaultEndianByteOrder(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
buf.order(ByteOrder.BIG_ENDIAN);
|
|
|
|
long value = 0x01020304;
|
|
|
|
buf.writeUnsignedInt(value);
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.writerOffset(Long.BYTES);
|
2020-11-10 12:09:14 +01:00
|
|
|
assertEquals((byte) 0x01, buf.readByte());
|
|
|
|
assertEquals((byte) 0x02, buf.readByte());
|
|
|
|
assertEquals((byte) 0x03, buf.readByte());
|
|
|
|
assertEquals((byte) 0x04, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedSetOfUnsignedIntMustBoundsCheckWhenWriteOffsetIsNegative(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
assertEquals(Long.BYTES, buf.capacity());
|
|
|
|
long value = 0x01020304;
|
2020-11-11 23:00:08 +01:00
|
|
|
assertThrows(IndexOutOfBoundsException.class, () -> buf.setUnsignedInt(-1, value));
|
|
|
|
buf.writerOffset(Long.BYTES);
|
2020-11-10 12:09:14 +01:00
|
|
|
// Verify contents are unchanged.
|
|
|
|
assertEquals(0, buf.readLong());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedSetOfUnsignedIntMustBoundsCheckWhenWriteOffsetAndSizeIsBeyondCapacity(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
assertEquals(Long.BYTES, buf.capacity());
|
|
|
|
long value = 0x01020304;
|
2020-11-11 23:00:08 +01:00
|
|
|
assertThrows(IndexOutOfBoundsException.class, () -> buf.setUnsignedInt(5, value));
|
|
|
|
buf.writerOffset(Long.BYTES);
|
2020-11-10 12:09:14 +01:00
|
|
|
// Verify contents are unchanged.
|
|
|
|
assertEquals(0, buf.readLong());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedSetOfUnsignedIntMustHaveDefaultEndianByteOrder(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
buf.order(ByteOrder.BIG_ENDIAN);
|
|
|
|
long value = 0x01020304;
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.setUnsignedInt(0, value);
|
|
|
|
buf.writerOffset(Long.BYTES);
|
2020-11-10 12:09:14 +01:00
|
|
|
assertEquals((byte) 0x01, buf.readByte());
|
|
|
|
assertEquals((byte) 0x02, buf.readByte());
|
|
|
|
assertEquals((byte) 0x03, buf.readByte());
|
|
|
|
assertEquals((byte) 0x04, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void relativeReadOfFloatMustNotBoundsCheckWhenReadOffsetAndSizeIsEqualToWriteOffset(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
assertEquals(0, buf.readableBytes());
|
|
|
|
assertEquals(Long.BYTES, buf.writableBytes());
|
|
|
|
float value = Float.intBitsToFloat(0x01020304);
|
|
|
|
buf.writeFloat(value);
|
|
|
|
assertEquals(4, buf.readableBytes());
|
|
|
|
assertEquals(4, buf.writableBytes());
|
|
|
|
assertEquals(value, buf.readFloat());
|
|
|
|
assertEquals(0, buf.readableBytes());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void relativeReadOfFloatMustReadWithDefaultEndianByteOrder(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
buf.order(ByteOrder.BIG_ENDIAN);
|
|
|
|
assertEquals(0, buf.readableBytes());
|
|
|
|
assertEquals(Long.BYTES, buf.writableBytes());
|
|
|
|
float value = Float.intBitsToFloat(0x01020304);
|
|
|
|
buf.writeFloat(value);
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.setByte(0, (byte) 0x10);
|
2020-11-10 12:09:14 +01:00
|
|
|
assertEquals(4, buf.readableBytes());
|
|
|
|
assertEquals(4, buf.writableBytes());
|
|
|
|
assertEquals(Float.intBitsToFloat(0x10020304), buf.readFloat());
|
|
|
|
assertEquals(0, buf.readableBytes());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void relativeReadOfFloatMustBoundsCheckWhenReadOffsetAndSizeIsBeyondWriteOffset(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
assertEquals(0, buf.readableBytes());
|
|
|
|
assertEquals(Long.BYTES, buf.writableBytes());
|
|
|
|
float value = Float.intBitsToFloat(0x01020304);
|
|
|
|
buf.writeFloat(value);
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.readerOffset(1);
|
2020-11-10 12:09:14 +01:00
|
|
|
assertEquals(3, buf.readableBytes());
|
|
|
|
assertEquals(4, buf.writableBytes());
|
|
|
|
assertThrows(IndexOutOfBoundsException.class, buf::readFloat);
|
|
|
|
assertEquals(3, buf.readableBytes());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedGetOfFloatMustBoundsCheckOnNegativeOffset(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
2020-11-11 23:00:08 +01:00
|
|
|
assertThrows(IndexOutOfBoundsException.class, () -> buf.getFloat(-1));
|
2020-11-10 12:09:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedGetOfFloatMustNotBoundsCheckWhenReadOffsetAndSizeIsEqualToWriteOffset(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
float value = Float.intBitsToFloat(0x01020304);
|
|
|
|
buf.writeFloat(value);
|
2020-11-11 23:00:08 +01:00
|
|
|
assertEquals(value, buf.getFloat(0));
|
2020-11-10 12:09:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedGetOfFloatMustReadWithDefaultEndianByteOrder(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
buf.order(ByteOrder.BIG_ENDIAN);
|
|
|
|
float value = Float.intBitsToFloat(0x01020304);
|
|
|
|
buf.writeFloat(value);
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.setByte(0, (byte) 0x10);
|
|
|
|
assertEquals(Float.intBitsToFloat(0x10020304), buf.getFloat(0));
|
2020-11-10 12:09:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedGetOfFloatMustBoundsCheckWhenReadOffsetAndSizeIsGreaterThanWriteOffset(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
float value = Float.intBitsToFloat(0x01020304);
|
|
|
|
buf.writeFloat(value);
|
2020-11-11 23:00:08 +01:00
|
|
|
assertThrows(IndexOutOfBoundsException.class, () -> buf.getFloat(1));
|
2020-11-10 12:09:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedGetOfFloatMustBoundsCheckWhenReadOffsetIsGreaterThanWriteOffset(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
2020-11-11 23:00:08 +01:00
|
|
|
assertThrows(IndexOutOfBoundsException.class, () -> buf.getFloat(0));
|
2020-11-10 12:09:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void relativeWriteOfFloatMustBoundsCheckWhenWriteOffsetAndSizeIsBeyondCapacity(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
assertEquals(Long.BYTES, buf.capacity());
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.writerOffset(5);
|
2020-11-10 12:09:14 +01:00
|
|
|
float value = Float.intBitsToFloat(0x01020304);
|
|
|
|
assertThrows(IndexOutOfBoundsException.class, () -> buf.writeFloat(value));
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.writerOffset(Long.BYTES);
|
2020-11-10 12:09:14 +01:00
|
|
|
// Verify contents are unchanged.
|
|
|
|
assertEquals(0, buf.readLong());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void relativeWriteOfFloatMustHaveDefaultEndianByteOrder(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
buf.order(ByteOrder.BIG_ENDIAN);
|
|
|
|
float value = Float.intBitsToFloat(0x01020304);
|
|
|
|
buf.writeFloat(value);
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.writerOffset(Long.BYTES);
|
2020-11-10 12:09:14 +01:00
|
|
|
assertEquals((byte) 0x01, buf.readByte());
|
|
|
|
assertEquals((byte) 0x02, buf.readByte());
|
|
|
|
assertEquals((byte) 0x03, buf.readByte());
|
|
|
|
assertEquals((byte) 0x04, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedSetOfFloatMustBoundsCheckWhenWriteOffsetIsNegative(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
assertEquals(Long.BYTES, buf.capacity());
|
|
|
|
float value = Float.intBitsToFloat(0x01020304);
|
2020-11-11 23:00:08 +01:00
|
|
|
assertThrows(IndexOutOfBoundsException.class, () -> buf.setFloat(-1, value));
|
|
|
|
buf.writerOffset(Long.BYTES);
|
2020-11-10 12:09:14 +01:00
|
|
|
// Verify contents are unchanged.
|
|
|
|
assertEquals(0, buf.readLong());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedSetOfFloatMustBoundsCheckWhenWriteOffsetAndSizeIsBeyondCapacity(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
assertEquals(Long.BYTES, buf.capacity());
|
|
|
|
float value = Float.intBitsToFloat(0x01020304);
|
2020-11-11 23:00:08 +01:00
|
|
|
assertThrows(IndexOutOfBoundsException.class, () -> buf.setFloat(5, value));
|
|
|
|
buf.writerOffset(Long.BYTES);
|
2020-11-10 12:09:14 +01:00
|
|
|
// Verify contents are unchanged.
|
|
|
|
assertEquals(0, buf.readLong());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedSetOfFloatMustHaveDefaultEndianByteOrder(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
buf.order(ByteOrder.BIG_ENDIAN);
|
|
|
|
float value = Float.intBitsToFloat(0x01020304);
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.setFloat(0, value);
|
|
|
|
buf.writerOffset(Long.BYTES);
|
2020-11-10 12:09:14 +01:00
|
|
|
assertEquals((byte) 0x01, buf.readByte());
|
|
|
|
assertEquals((byte) 0x02, buf.readByte());
|
|
|
|
assertEquals((byte) 0x03, buf.readByte());
|
|
|
|
assertEquals((byte) 0x04, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
assertEquals((byte) 0x00, buf.readByte());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void relativeReadOfLongMustNotBoundsCheckWhenReadOffsetAndSizeIsEqualToWriteOffset(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
assertEquals(0, buf.readableBytes());
|
|
|
|
assertEquals(Long.BYTES, buf.writableBytes());
|
|
|
|
long value = 0x0102030405060708L;
|
|
|
|
buf.writeLong(value);
|
|
|
|
assertEquals(8, buf.readableBytes());
|
|
|
|
assertEquals(0, buf.writableBytes());
|
|
|
|
assertEquals(value, buf.readLong());
|
|
|
|
assertEquals(0, buf.readableBytes());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void relativeReadOfLongMustReadWithDefaultEndianByteOrder(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
buf.order(ByteOrder.BIG_ENDIAN);
|
|
|
|
assertEquals(0, buf.readableBytes());
|
|
|
|
assertEquals(Long.BYTES, buf.writableBytes());
|
|
|
|
long value = 0x0102030405060708L;
|
|
|
|
buf.writeLong(value);
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.setByte(0, (byte) 0x10);
|
2020-11-10 12:09:14 +01:00
|
|
|
assertEquals(8, buf.readableBytes());
|
|
|
|
assertEquals(0, buf.writableBytes());
|
|
|
|
assertEquals(0x1002030405060708L, buf.readLong());
|
|
|
|
assertEquals(0, buf.readableBytes());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void relativeReadOfLongMustBoundsCheckWhenReadOffsetAndSizeIsBeyondWriteOffset(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
assertEquals(0, buf.readableBytes());
|
|
|
|
assertEquals(Long.BYTES, buf.writableBytes());
|
|
|
|
long value = 0x0102030405060708L;
|
|
|
|
buf.writeLong(value);
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.readerOffset(1);
|
2020-11-10 12:09:14 +01:00
|
|
|
assertEquals(7, buf.readableBytes());
|
|
|
|
assertEquals(0, buf.writableBytes());
|
|
|
|
assertThrows(IndexOutOfBoundsException.class, buf::readLong);
|
|
|
|
assertEquals(7, buf.readableBytes());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedGetOfLongMustBoundsCheckOnNegativeOffset(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
2020-11-11 23:00:08 +01:00
|
|
|
assertThrows(IndexOutOfBoundsException.class, () -> buf.getLong(-1));
|
2020-11-10 12:09:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedGetOfLongMustNotBoundsCheckWhenReadOffsetAndSizeIsEqualToWriteOffset(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
long value = 0x0102030405060708L;
|
|
|
|
buf.writeLong(value);
|
2020-11-11 23:00:08 +01:00
|
|
|
assertEquals(value, buf.getLong(0));
|
2020-11-10 12:09:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedGetOfLongMustReadWithDefaultEndianByteOrder(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
buf.order(ByteOrder.BIG_ENDIAN);
|
|
|
|
long value = 0x0102030405060708L;
|
|
|
|
buf.writeLong(value);
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.setByte(0, (byte) 0x10);
|
|
|
|
assertEquals(0x1002030405060708L, buf.getLong(0));
|
2020-11-10 12:09:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedGetOfLongMustBoundsCheckWhenReadOffsetAndSizeIsGreaterThanWriteOffset(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
long value = 0x0102030405060708L;
|
|
|
|
buf.writeLong(value);
|
2020-11-11 23:00:08 +01:00
|
|
|
assertThrows(IndexOutOfBoundsException.class, () -> buf.getLong(1));
|
2020-11-10 12:09:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedGetOfLongMustBoundsCheckWhenReadOffsetIsGreaterThanWriteOffset(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
2020-11-11 23:00:08 +01:00
|
|
|
assertThrows(IndexOutOfBoundsException.class, () -> buf.getLong(0));
|
2020-11-10 12:09:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void relativeWriteOfLongMustBoundsCheckWhenWriteOffsetAndSizeIsBeyondCapacity(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
assertEquals(Long.BYTES, buf.capacity());
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.writerOffset(1);
|
2020-11-10 12:09:14 +01:00
|
|
|
long value = 0x0102030405060708L;
|
|
|
|
assertThrows(IndexOutOfBoundsException.class, () -> buf.writeLong(value));
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.writerOffset(Long.BYTES);
|
2020-11-10 12:09:14 +01:00
|
|
|
// Verify contents are unchanged.
|
|
|
|
assertEquals(0, buf.readLong());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void relativeWriteOfLongMustHaveDefaultEndianByteOrder(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
buf.order(ByteOrder.BIG_ENDIAN);
|
|
|
|
long value = 0x0102030405060708L;
|
|
|
|
buf.writeLong(value);
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.writerOffset(Long.BYTES);
|
2020-11-10 12:09:14 +01:00
|
|
|
assertEquals((byte) 0x01, buf.readByte());
|
|
|
|
assertEquals((byte) 0x02, buf.readByte());
|
|
|
|
assertEquals((byte) 0x03, buf.readByte());
|
|
|
|
assertEquals((byte) 0x04, buf.readByte());
|
|
|
|
assertEquals((byte) 0x05, buf.readByte());
|
|
|
|
assertEquals((byte) 0x06, buf.readByte());
|
|
|
|
assertEquals((byte) 0x07, buf.readByte());
|
|
|
|
assertEquals((byte) 0x08, buf.readByte());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedSetOfLongMustBoundsCheckWhenWriteOffsetIsNegative(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
assertEquals(Long.BYTES, buf.capacity());
|
|
|
|
long value = 0x0102030405060708L;
|
2020-11-11 23:00:08 +01:00
|
|
|
assertThrows(IndexOutOfBoundsException.class, () -> buf.setLong(-1, value));
|
|
|
|
buf.writerOffset(Long.BYTES);
|
2020-11-10 12:09:14 +01:00
|
|
|
// Verify contents are unchanged.
|
|
|
|
assertEquals(0, buf.readLong());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedSetOfLongMustBoundsCheckWhenWriteOffsetAndSizeIsBeyondCapacity(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
assertEquals(Long.BYTES, buf.capacity());
|
|
|
|
long value = 0x0102030405060708L;
|
2020-11-11 23:00:08 +01:00
|
|
|
assertThrows(IndexOutOfBoundsException.class, () -> buf.setLong(1, value));
|
|
|
|
buf.writerOffset(Long.BYTES);
|
2020-11-10 12:09:14 +01:00
|
|
|
// Verify contents are unchanged.
|
|
|
|
assertEquals(0, buf.readLong());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedSetOfLongMustHaveDefaultEndianByteOrder(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
buf.order(ByteOrder.BIG_ENDIAN);
|
|
|
|
long value = 0x0102030405060708L;
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.setLong(0, value);
|
|
|
|
buf.writerOffset(Long.BYTES);
|
2020-11-10 12:09:14 +01:00
|
|
|
assertEquals((byte) 0x01, buf.readByte());
|
|
|
|
assertEquals((byte) 0x02, buf.readByte());
|
|
|
|
assertEquals((byte) 0x03, buf.readByte());
|
|
|
|
assertEquals((byte) 0x04, buf.readByte());
|
|
|
|
assertEquals((byte) 0x05, buf.readByte());
|
|
|
|
assertEquals((byte) 0x06, buf.readByte());
|
|
|
|
assertEquals((byte) 0x07, buf.readByte());
|
|
|
|
assertEquals((byte) 0x08, buf.readByte());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void relativeReadOfDoubleMustNotBoundsCheckWhenReadOffsetAndSizeIsEqualToWriteOffset(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
assertEquals(0, buf.readableBytes());
|
|
|
|
assertEquals(Long.BYTES, buf.writableBytes());
|
|
|
|
double value = Double.longBitsToDouble(0x0102030405060708L);
|
|
|
|
buf.writeDouble(value);
|
|
|
|
assertEquals(8, buf.readableBytes());
|
|
|
|
assertEquals(0, buf.writableBytes());
|
|
|
|
assertEquals(value, buf.readDouble());
|
|
|
|
assertEquals(0, buf.readableBytes());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void relativeReadOfDoubleMustReadWithDefaultEndianByteOrder(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
buf.order(ByteOrder.BIG_ENDIAN);
|
|
|
|
assertEquals(0, buf.readableBytes());
|
|
|
|
assertEquals(Long.BYTES, buf.writableBytes());
|
|
|
|
double value = Double.longBitsToDouble(0x0102030405060708L);
|
|
|
|
buf.writeDouble(value);
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.setByte(0, (byte) 0x10);
|
2020-11-10 12:09:14 +01:00
|
|
|
assertEquals(8, buf.readableBytes());
|
|
|
|
assertEquals(0, buf.writableBytes());
|
|
|
|
assertEquals(Double.longBitsToDouble(0x1002030405060708L), buf.readDouble());
|
|
|
|
assertEquals(0, buf.readableBytes());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void relativeReadOfDoubleMustBoundsCheckWhenReadOffsetAndSizeIsBeyondWriteOffset(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
assertEquals(0, buf.readableBytes());
|
|
|
|
assertEquals(Long.BYTES, buf.writableBytes());
|
|
|
|
double value = Double.longBitsToDouble(0x0102030405060708L);
|
|
|
|
buf.writeDouble(value);
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.readerOffset(1);
|
2020-11-10 12:09:14 +01:00
|
|
|
assertEquals(7, buf.readableBytes());
|
|
|
|
assertEquals(0, buf.writableBytes());
|
|
|
|
assertThrows(IndexOutOfBoundsException.class, buf::readDouble);
|
|
|
|
assertEquals(7, buf.readableBytes());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedGetOfDoubleMustBoundsCheckOnNegativeOffset(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
2020-11-11 23:00:08 +01:00
|
|
|
assertThrows(IndexOutOfBoundsException.class, () -> buf.getDouble(-1));
|
2020-11-10 12:09:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedGetOfDoubleMustNotBoundsCheckWhenReadOffsetAndSizeIsEqualToWriteOffset(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
double value = Double.longBitsToDouble(0x0102030405060708L);
|
|
|
|
buf.writeDouble(value);
|
2020-11-11 23:00:08 +01:00
|
|
|
assertEquals(value, buf.getDouble(0));
|
2020-11-10 12:09:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedGetOfDoubleMustReadWithDefaultEndianByteOrder(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
buf.order(ByteOrder.BIG_ENDIAN);
|
|
|
|
double value = Double.longBitsToDouble(0x0102030405060708L);
|
|
|
|
buf.writeDouble(value);
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.setByte(0, (byte) 0x10);
|
|
|
|
assertEquals(Double.longBitsToDouble(0x1002030405060708L), buf.getDouble(0));
|
2020-11-10 12:09:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedGetOfDoubleMustBoundsCheckWhenReadOffsetAndSizeIsGreaterThanWriteOffset(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
double value = Double.longBitsToDouble(0x0102030405060708L);
|
|
|
|
buf.writeDouble(value);
|
2020-11-11 23:00:08 +01:00
|
|
|
assertThrows(IndexOutOfBoundsException.class, () -> buf.getDouble(1));
|
2020-11-10 12:09:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedGetOfDoubleMustBoundsCheckWhenReadOffsetIsGreaterThanWriteOffset(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
2020-11-11 23:00:08 +01:00
|
|
|
assertThrows(IndexOutOfBoundsException.class, () -> buf.getDouble(0));
|
2020-11-10 12:09:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void relativeWriteOfDoubleMustBoundsCheckWhenWriteOffsetAndSizeIsBeyondCapacity(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
assertEquals(Long.BYTES, buf.capacity());
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.writerOffset(1);
|
2020-11-10 12:09:14 +01:00
|
|
|
double value = Double.longBitsToDouble(0x0102030405060708L);
|
|
|
|
assertThrows(IndexOutOfBoundsException.class, () -> buf.writeDouble(value));
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.writerOffset(Long.BYTES);
|
2020-11-10 12:09:14 +01:00
|
|
|
// Verify contents are unchanged.
|
|
|
|
assertEquals(0, buf.readLong());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
|
|
|
void relativeWriteOfDoubleMustHaveDefaultEndianByteOrder(Fixture fixture) {
|
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
buf.order(ByteOrder.BIG_ENDIAN);
|
|
|
|
double value = Double.longBitsToDouble(0x0102030405060708L);
|
|
|
|
buf.writeDouble(value);
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.writerOffset(Long.BYTES);
|
2020-11-10 12:09:14 +01:00
|
|
|
assertEquals((byte) 0x01, buf.readByte());
|
|
|
|
assertEquals((byte) 0x02, buf.readByte());
|
|
|
|
assertEquals((byte) 0x03, buf.readByte());
|
|
|
|
assertEquals((byte) 0x04, buf.readByte());
|
|
|
|
assertEquals((byte) 0x05, buf.readByte());
|
|
|
|
assertEquals((byte) 0x06, buf.readByte());
|
|
|
|
assertEquals((byte) 0x07, buf.readByte());
|
|
|
|
assertEquals((byte) 0x08, buf.readByte());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedSetOfDoubleMustBoundsCheckWhenWriteOffsetIsNegative(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
assertEquals(Long.BYTES, buf.capacity());
|
|
|
|
double value = Double.longBitsToDouble(0x0102030405060708L);
|
2020-11-11 23:00:08 +01:00
|
|
|
assertThrows(IndexOutOfBoundsException.class, () -> buf.setDouble(-1, value));
|
|
|
|
buf.writerOffset(Long.BYTES);
|
2020-11-10 12:09:14 +01:00
|
|
|
// Verify contents are unchanged.
|
|
|
|
assertEquals(0, buf.readLong());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedSetOfDoubleMustBoundsCheckWhenWriteOffsetAndSizeIsBeyondCapacity(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
assertEquals(Long.BYTES, buf.capacity());
|
|
|
|
double value = Double.longBitsToDouble(0x0102030405060708L);
|
2020-11-11 23:00:08 +01:00
|
|
|
assertThrows(IndexOutOfBoundsException.class, () -> buf.setDouble(1, value));
|
|
|
|
buf.writerOffset(Long.BYTES);
|
2020-11-10 12:09:14 +01:00
|
|
|
// Verify contents are unchanged.
|
|
|
|
assertEquals(0, buf.readLong());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
@MethodSource("allocators")
|
2020-11-11 23:00:08 +01:00
|
|
|
void offsettedSetOfDoubleMustHaveDefaultEndianByteOrder(Fixture fixture) {
|
2020-11-10 12:09:14 +01:00
|
|
|
try (Allocator allocator = fixture.createAllocator();
|
|
|
|
Buf buf = allocator.allocate(8)) {
|
|
|
|
buf.order(ByteOrder.BIG_ENDIAN);
|
|
|
|
double value = Double.longBitsToDouble(0x0102030405060708L);
|
2020-11-11 23:00:08 +01:00
|
|
|
buf.setDouble(0, value);
|
|
|
|
buf.writerOffset(Long.BYTES);
|
2020-11-10 12:09:14 +01:00
|
|
|
assertEquals((byte) 0x01, buf.readByte());
|
|
|
|
assertEquals((byte) 0x02, buf.readByte());
|
|
|
|
assertEquals((byte) 0x03, buf.readByte());
|
|
|
|
assertEquals((byte) 0x04, buf.readByte());
|
|
|
|
assertEquals((byte) 0x05, buf.readByte());
|
|
|
|
assertEquals((byte) 0x06, buf.readByte());
|
|
|
|
assertEquals((byte) 0x07, buf.readByte());
|
|
|
|
assertEquals((byte) 0x08, buf.readByte());
|
|
|
|
}
|
2020-10-06 16:30:49 +02:00
|
|
|
}
|
Make cleaner in pooledDirectWithCleaner return segments to pool instead of deallocating
Motivation:
Allocating memory is expensive, which is why we pool our allocations.
The cleaner, when used with pooled memory, should then return that memory to the pool instead of deallocating it.
The purpose of the cleaner is, after all, to avoid having to track the reference counts so precisely.
Modification:
The NativeMemoryCleanerDrop is now able to either recover lost memory segments, when a buffer wasn't closed explicitly
and is being cleaned by the GC, or return the buffer to the pool via ordinary drop.
The GatedCleanable has been added, because buffer ownership transfer involves generating new memory segments,
and in those cases we need to invalidate the old cleanables without deallocating the tracked memory segment or returning
it to the pool more than once.
Result:
The pooledDirectWithCleaner allocator is now able to reuse memory segments, even when their references are forgotten and
they processed by the cleaner thread.
2020-09-25 12:15:45 +02:00
|
|
|
// </editor-fold>
|
2020-11-11 23:00:08 +01:00
|
|
|
|
|
|
|
private static byte[] toByteArray(Buf buf) {
|
|
|
|
byte[] bs = new byte[buf.capacity()];
|
|
|
|
buf.copyInto(0, bs, 0, bs.length);
|
|
|
|
return bs;
|
|
|
|
}
|
Generate complete and tested Buf API
Motivation:
The Buf API is wide, taking into account signedness, endian-ness, and so on, for all primitive
types.
Modification:
A code generator has been added which add API, implementation, and tests, to the Buf, BBuf,
and BBufTest files respectively.
Result:
We have a complete and fully tested accessor API in the Buf interface, which is still somewhat
easy to bulk-modify using the code generator.
2020-09-10 18:05:36 +02:00
|
|
|
|
|
|
|
private static void assertEquals(byte expected, byte actual) {
|
|
|
|
if (expected != actual) {
|
|
|
|
fail(String.format("expected: %1$s (0x%1$X) but was: %2$s (0x%2$X)", expected, actual));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void assertEquals(char expected, char actual) {
|
|
|
|
if (expected != actual) {
|
|
|
|
fail(String.format("expected: %s (0x%X) but was: %s (0x%X)",
|
|
|
|
expected, (int) expected, actual, (int) actual));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void assertEquals(short expected, short actual) {
|
|
|
|
if (expected != actual) {
|
|
|
|
fail(String.format("expected: %1$s (0x%1$X) but was: %2$s (0x%2$X)", expected, actual));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void assertEquals(int expected, int actual) {
|
|
|
|
if (expected != actual) {
|
|
|
|
fail(String.format("expected: %1$s (0x%1$X) but was: %2$s (0x%2$X)", expected, actual));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void assertEquals(long expected, long actual) {
|
|
|
|
if (expected != actual) {
|
|
|
|
fail(String.format("expected: %1$s (0x%1$X) but was: %2$s (0x%2$X)", expected, actual));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void assertEquals(float expected, float actual) {
|
|
|
|
//noinspection FloatingPointEquality
|
|
|
|
if (expected != actual) {
|
|
|
|
fail(String.format("expected: %s (0x%X) but was: %s (0x%X)",
|
|
|
|
expected, Float.floatToRawIntBits(expected),
|
|
|
|
actual, Float.floatToRawIntBits(actual)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void assertEquals(double expected, double actual) {
|
|
|
|
//noinspection FloatingPointEquality
|
|
|
|
if (expected != actual) {
|
|
|
|
fail(String.format("expected: %s (0x%X) but was: %s (0x%X)",
|
|
|
|
expected, Double.doubleToRawLongBits(expected),
|
|
|
|
actual, Double.doubleToRawLongBits(actual)));
|
|
|
|
}
|
|
|
|
}
|
2020-11-17 15:25:26 +01:00
|
|
|
}
|