mirror of
https://github.com/Eaglercraft-TeaVM-Fork/eagler-teavm.git
synced 2024-12-22 08:14:09 -08:00
classlib: support putFloat/getFloat/putDouble/getDouble in ByteBuffer
This commit is contained in:
parent
f33a7578d4
commit
a3f0ec52d4
|
@ -253,8 +253,24 @@ public abstract class TByteBuffer extends TBuffer implements TComparable<TByteBu
|
|||
|
||||
public abstract TLongBuffer asLongBuffer();
|
||||
|
||||
public abstract float getFloat();
|
||||
|
||||
public abstract TByteBuffer putFloat(float value);
|
||||
|
||||
public abstract float getFloat(int index);
|
||||
|
||||
public abstract TByteBuffer putFloat(int index, float value);
|
||||
|
||||
public abstract TFloatBuffer asFloatBuffer();
|
||||
|
||||
public abstract double getDouble();
|
||||
|
||||
public abstract TByteBuffer putDouble(double value);
|
||||
|
||||
public abstract double getDouble(int index);
|
||||
|
||||
public abstract TByteBuffer putDouble(int index, double value);
|
||||
|
||||
public abstract TDoubleBuffer asDoubleBuffer();
|
||||
|
||||
@Override
|
||||
|
|
|
@ -352,6 +352,46 @@ class TByteBufferImpl extends TByteBuffer {
|
|||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getFloat() {
|
||||
return Float.intBitsToFloat(getInt());
|
||||
}
|
||||
|
||||
@Override
|
||||
public TByteBuffer putFloat(float value) {
|
||||
return putInt(Float.floatToRawIntBits(value));
|
||||
}
|
||||
|
||||
@Override
|
||||
public TByteBuffer putFloat(int index, float value) {
|
||||
return putInt(index, Float.floatToRawIntBits(value));
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getFloat(int index) {
|
||||
return Float.intBitsToFloat(getInt(index));
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getDouble() {
|
||||
return Double.longBitsToDouble(getLong());
|
||||
}
|
||||
|
||||
@Override
|
||||
public TByteBuffer putDouble(double value) {
|
||||
return putLong(Double.doubleToLongBits(value));
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getDouble(int index) {
|
||||
return Double.longBitsToDouble(getLong(index));
|
||||
}
|
||||
|
||||
@Override
|
||||
public TByteBuffer putDouble(int index, double value) {
|
||||
return putLong(index, Double.doubleToLongBits(value));
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getLong() {
|
||||
if (position + 7 >= limit) {
|
||||
|
|
|
@ -17,8 +17,9 @@ package org.teavm.classlib.java.nio;
|
|||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.CoreMatchers.sameInstance;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.fail;
|
||||
import java.nio.BufferOverflowException;
|
||||
import java.nio.BufferUnderflowException;
|
||||
|
@ -576,6 +577,80 @@ public class ByteBufferTest {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void putsFloat() {
|
||||
var array = new byte[8];
|
||||
var buffer = ByteBuffer.wrap(array);
|
||||
buffer.putFloat(1f);
|
||||
buffer.putFloat(23f);
|
||||
try {
|
||||
buffer.putFloat(42f);
|
||||
fail("Exception expected");
|
||||
} catch (BufferOverflowException e) {
|
||||
// expected
|
||||
}
|
||||
|
||||
assertArrayEquals(new byte[] { 63, -128, 0, 0, 65, -72, 0, 0 }, array);
|
||||
|
||||
buffer.putFloat(1, 2f);
|
||||
assertArrayEquals(new byte[] { 63, 64, 0, 0, 0, -72, 0, 0 }, array);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getsFloat() {
|
||||
byte[] array = { 63, -128, 0, 0, 65, -72, 0, 0 };
|
||||
var buffer = ByteBuffer.wrap(array);
|
||||
assertEquals(1f, buffer.getFloat(), 0.0001f);
|
||||
assertEquals(23f, buffer.getFloat(), 0.0001f);
|
||||
try {
|
||||
buffer.getFloat();
|
||||
fail("Exception expected");
|
||||
} catch (BufferUnderflowException e) {
|
||||
// expected
|
||||
}
|
||||
|
||||
array[1] = 64;
|
||||
array[4] = 0;
|
||||
assertEquals(2f, buffer.getFloat(1), 0.0001f);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void putsDouble() {
|
||||
var array = new byte[16];
|
||||
var buffer = ByteBuffer.wrap(array);
|
||||
buffer.putDouble(1.0);
|
||||
buffer.putDouble(23.0);
|
||||
try {
|
||||
buffer.putDouble(42.0);
|
||||
fail("Exception expected");
|
||||
} catch (BufferOverflowException e) {
|
||||
// expected
|
||||
}
|
||||
|
||||
assertArrayEquals(new byte[] { 63, -16, 0, 0, 0, 0, 0, 0, 64, 55, 0, 0, 0, 0, 0, 0 }, array);
|
||||
|
||||
buffer.putDouble(1, 2.0);
|
||||
assertArrayEquals(new byte[] { 63, 64, 0, 0, 0, 0, 0, 0, 0, 55, 0, 0, 0, 0, 0, 0 }, array);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getsDouble() {
|
||||
byte[] array = { 63, -16, 0, 0, 0, 0, 0, 0, 64, 55, 0, 0, 0, 0, 0, 0 };
|
||||
var buffer = ByteBuffer.wrap(array);
|
||||
assertEquals(1.0, buffer.getDouble(), 0.0001);
|
||||
assertEquals(23.0, buffer.getDouble(), 0.0001);
|
||||
try {
|
||||
buffer.getDouble();
|
||||
fail("Exception expected");
|
||||
} catch (BufferUnderflowException e) {
|
||||
// expected
|
||||
}
|
||||
|
||||
array[1] = 64;
|
||||
array[8] = 0;
|
||||
assertEquals(2.0, buffer.getDouble(1), 0.0001);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getsLong() {
|
||||
byte[] array = {0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38};
|
||||
|
|
Loading…
Reference in New Issue
Block a user