From a3f0ec52d4403d130a81c533ba17b6384b07632d Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Thu, 17 Aug 2023 11:59:48 +0200 Subject: [PATCH] classlib: support putFloat/getFloat/putDouble/getDouble in ByteBuffer --- .../teavm/classlib/java/nio/TByteBuffer.java | 16 ++++ .../classlib/java/nio/TByteBufferImpl.java | 40 ++++++++++ .../classlib/java/nio/ByteBufferTest.java | 77 ++++++++++++++++++- 3 files changed, 132 insertions(+), 1 deletion(-) diff --git a/classlib/src/main/java/org/teavm/classlib/java/nio/TByteBuffer.java b/classlib/src/main/java/org/teavm/classlib/java/nio/TByteBuffer.java index 561a2f51e..af4ea680c 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/nio/TByteBuffer.java +++ b/classlib/src/main/java/org/teavm/classlib/java/nio/TByteBuffer.java @@ -253,8 +253,24 @@ public abstract class TByteBuffer extends TBuffer implements TComparable= limit) { diff --git a/tests/src/test/java/org/teavm/classlib/java/nio/ByteBufferTest.java b/tests/src/test/java/org/teavm/classlib/java/nio/ByteBufferTest.java index 6135eebb5..b4dbe7264 100644 --- a/tests/src/test/java/org/teavm/classlib/java/nio/ByteBufferTest.java +++ b/tests/src/test/java/org/teavm/classlib/java/nio/ByteBufferTest.java @@ -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};