mirror of
https://github.com/Eaglercraft-TeaVM-Fork/eagler-teavm.git
synced 2024-12-22 16:14:10 -08:00
Add some test cases for ByteBuffer
This commit is contained in:
parent
b7725aa15a
commit
2cd397c81c
|
@ -85,8 +85,6 @@
|
|||
<phase>process-test-classes</phase>
|
||||
<configuration>
|
||||
<minifying>false</minifying>
|
||||
<sourceMapsGenerated>true</sourceMapsGenerated>
|
||||
<sourceFilesCopied>true</sourceFilesCopied>
|
||||
<properties>
|
||||
<java.util.Locale.available>en, en_US, en_GB, ru, ru_RU</java.util.Locale.available>
|
||||
</properties>
|
||||
|
|
|
@ -11,23 +11,30 @@ public abstract class TByteBuffer extends TBuffer implements TComparable<TByteBu
|
|||
byte[] array;
|
||||
TByteOrder order = TByteOrder.BIG_ENDIAN;
|
||||
|
||||
TByteBuffer(int start, byte[] array, int offset, int length) {
|
||||
super(array.length - start);
|
||||
TByteBuffer(int start, int capacity, byte[] array, int offset, int length) {
|
||||
super(capacity);
|
||||
this.start = start;
|
||||
this.array = array;
|
||||
position = offset;
|
||||
limit = position + length;
|
||||
}
|
||||
|
||||
public static TByteBuffer allocateDirect(int capacity) {
|
||||
if (capacity < 0) {
|
||||
throw new IllegalArgumentException("Capacity is negative: " + capacity);
|
||||
}
|
||||
return new TByteBufferImpl(capacity, true);
|
||||
}
|
||||
|
||||
public static TByteBuffer allocate(int capacity) {
|
||||
if (capacity < 0) {
|
||||
throw new IllegalArgumentException("Capacity is negative: " + capacity);
|
||||
}
|
||||
return new TByteBufferImpl(capacity, false);
|
||||
}
|
||||
|
||||
public static TByteBuffer wrap(byte[] array, int offset, int length) {
|
||||
return new TByteBufferImpl(0, array, offset, offset + length, false, false);
|
||||
return new TByteBufferImpl(0, array.length, array, offset, offset + length, false, false);
|
||||
}
|
||||
|
||||
public static TByteBuffer wrap(byte[] array) {
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
package org.teavm.classlib.java.nio;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Alexey Andreev
|
||||
|
@ -10,28 +9,30 @@ class TByteBufferImpl extends TByteBuffer {
|
|||
private boolean readOnly;
|
||||
|
||||
public TByteBufferImpl(int capacity, boolean direct) {
|
||||
this(0, new byte[capacity], 0, capacity, direct, false);
|
||||
this(0, capacity, new byte[capacity], 0, capacity, direct, false);
|
||||
}
|
||||
|
||||
public TByteBufferImpl(int start, byte[] array, int position, int limit, boolean direct, boolean readOnly) {
|
||||
super(start, array, position, limit);
|
||||
public TByteBufferImpl(int start, int capacity, byte[] array, int position, int limit,
|
||||
boolean direct, boolean readOnly) {
|
||||
super(start, capacity, array, position, limit);
|
||||
this.direct = direct;
|
||||
this.readOnly = readOnly;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TByteBuffer slice() {
|
||||
return new TByteBufferImpl(position, array, 0, array.length - position, direct, readOnly);
|
||||
return new TByteBufferImpl(position, array.length - position, array, 0, array.length - position,
|
||||
direct, readOnly);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TByteBuffer duplicate() {
|
||||
return new TByteBufferImpl(start, array, position, limit, direct, readOnly);
|
||||
return new TByteBufferImpl(start, capacity, array, position, limit, direct, readOnly);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TByteBuffer asReadOnlyBuffer() {
|
||||
return new TByteBufferImpl(start, array, position, limit, direct, true);
|
||||
return new TByteBufferImpl(start, capacity, array, position, limit, direct, true);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -0,0 +1,163 @@
|
|||
package org.teavm.classlib.java.nio;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.InvalidMarkException;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Alexey Andreev
|
||||
*/
|
||||
public class ByteBufferTest {
|
||||
@Test
|
||||
public void allocatesDirect() {
|
||||
ByteBuffer buffer = ByteBuffer.allocateDirect(100);
|
||||
assertThat(buffer.isDirect(), is(true));
|
||||
assertThat(buffer.isReadOnly(), is(false));
|
||||
assertThat(buffer.capacity(), is(100));
|
||||
assertThat(buffer.position(), is(0));
|
||||
assertThat(buffer.limit(), is(100));
|
||||
try {
|
||||
buffer.reset();
|
||||
fail("Mark is expected to be undefined");
|
||||
} catch (InvalidMarkException e) {
|
||||
// ok
|
||||
}
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void errorIfAllocatingDirectOfNegativeSize() {
|
||||
ByteBuffer.allocateDirect(-2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void allocatesSimple() {
|
||||
ByteBuffer buffer = ByteBuffer.allocate(100);
|
||||
assertThat(buffer.isDirect(), is(false));
|
||||
assertThat(buffer.isReadOnly(), is(false));
|
||||
assertThat(buffer.hasArray(), is(true));
|
||||
assertThat(buffer.capacity(), is(100));
|
||||
assertThat(buffer.position(), is(0));
|
||||
assertThat(buffer.limit(), is(100));
|
||||
try {
|
||||
buffer.reset();
|
||||
fail("Mark is expected to be undefined");
|
||||
} catch (InvalidMarkException e) {
|
||||
// ok
|
||||
}
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void errorIfAllocatingBufferOfNegativeSize() {
|
||||
ByteBuffer.allocate(-1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void wrapsArray() {
|
||||
byte[] array = new byte[100];
|
||||
ByteBuffer buffer = ByteBuffer.wrap(array, 10, 70);
|
||||
assertThat(buffer.isDirect(), is(false));
|
||||
assertThat(buffer.isReadOnly(), is(false));
|
||||
assertThat(buffer.hasArray(), is(true));
|
||||
assertThat(buffer.array(), is(array));
|
||||
assertThat(buffer.arrayOffset(), is(0));
|
||||
assertThat(buffer.capacity(), is(100));
|
||||
assertThat(buffer.position(), is(10));
|
||||
assertThat(buffer.limit(), is(80));
|
||||
try {
|
||||
buffer.reset();
|
||||
fail("Mark is expected to be undefined");
|
||||
} catch (InvalidMarkException e) {
|
||||
// ok
|
||||
}
|
||||
array[0] = 23;
|
||||
assertThat(buffer.get(0), is((byte)23));
|
||||
buffer.put(1, (byte)24);
|
||||
assertThat(array[1], is((byte)24));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void errorWhenWrappingWithWrongParameters() {
|
||||
byte[] array = new byte[100];
|
||||
try {
|
||||
ByteBuffer.wrap(array, -1, 10);
|
||||
} catch (IndexOutOfBoundsException e) {
|
||||
// ok
|
||||
}
|
||||
try {
|
||||
ByteBuffer.wrap(array, 101, 10);
|
||||
} catch (IndexOutOfBoundsException e) {
|
||||
// ok
|
||||
}
|
||||
try {
|
||||
ByteBuffer.wrap(array, 98, 3);
|
||||
} catch (IndexOutOfBoundsException e) {
|
||||
// ok
|
||||
}
|
||||
try {
|
||||
ByteBuffer.wrap(array, 98, -1);
|
||||
} catch (IndexOutOfBoundsException e) {
|
||||
// ok
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void wrapsArrayWithoutOffset() {
|
||||
byte[] array = new byte[100];
|
||||
ByteBuffer buffer = ByteBuffer.wrap(array);
|
||||
assertThat(buffer.position(), is(0));
|
||||
assertThat(buffer.limit(), is(100));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createsSlice() {
|
||||
ByteBuffer buffer = ByteBuffer.allocate(100);
|
||||
buffer.put(new byte[60]);
|
||||
buffer.flip();
|
||||
buffer.put(new byte[15]);
|
||||
ByteBuffer slice = buffer.slice();
|
||||
assertThat(slice.array(), is(buffer.array()));
|
||||
assertThat(slice.position(), is(0));
|
||||
assertThat(slice.capacity(), is(45));
|
||||
assertThat(slice.limit(), is(45));
|
||||
assertThat(slice.isDirect(), is(false));
|
||||
assertThat(slice.isReadOnly(), is(false));
|
||||
slice.put(3, (byte)23);
|
||||
assertThat(buffer.get(18), is((byte)23));
|
||||
slice.put((byte)24);
|
||||
assertThat(buffer.get(15), is((byte)24));
|
||||
buffer.put(16, (byte)25);
|
||||
assertThat(slice.get(1), is((byte)25));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void slicePropertiesSameWithOriginal() {
|
||||
ByteBuffer buffer = ByteBuffer.allocate(100).asReadOnlyBuffer().slice();
|
||||
assertThat(buffer.isReadOnly(), is(true));
|
||||
buffer = ByteBuffer.allocateDirect(100);
|
||||
assertThat(buffer.isDirect(), is(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createsDuplicate() {
|
||||
ByteBuffer buffer = ByteBuffer.allocate(100);
|
||||
buffer.put(new byte[60]);
|
||||
buffer.flip();
|
||||
buffer.put(new byte[15]);
|
||||
ByteBuffer duplicate = buffer.duplicate();
|
||||
assertThat(duplicate.array(), is(buffer.array()));
|
||||
assertThat(duplicate.position(), is(15));
|
||||
assertThat(duplicate.capacity(), is(100));
|
||||
assertThat(duplicate.limit(), is(60));
|
||||
assertThat(duplicate.isDirect(), is(false));
|
||||
assertThat(duplicate.isReadOnly(), is(false));
|
||||
duplicate.put(3, (byte)23);
|
||||
assertThat(buffer.get(3), is((byte)23));
|
||||
duplicate.put((byte)24);
|
||||
assertThat(buffer.get(15), is((byte)24));
|
||||
buffer.put(1, (byte)25);
|
||||
assertThat(duplicate.get(1), is((byte)25));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user