From f4641dc642398ceb51167a5ace32cbbee30acef4 Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Wed, 22 Oct 2014 22:41:31 +0400 Subject: [PATCH] Add unit tests for ByteBuffer get and put methods --- teavm-classlib/pom.xml | 1 + .../org/teavm/classlib/java/nio/TBuffer.java | 1 + .../classlib/java/nio/TByteBufferImpl.java | 3 +- .../classlib/java/nio/ByteBufferTest.java | 129 +++++++++++++++++- 4 files changed, 129 insertions(+), 5 deletions(-) diff --git a/teavm-classlib/pom.xml b/teavm-classlib/pom.xml index be2629a81..667f3b42e 100644 --- a/teavm-classlib/pom.xml +++ b/teavm-classlib/pom.xml @@ -113,6 +113,7 @@ java.io java.math java.net + java.nio java.text java.util java.util.logging diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/nio/TBuffer.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/nio/TBuffer.java index e959e9f0f..7b59fc767 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/nio/TBuffer.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/nio/TBuffer.java @@ -47,6 +47,7 @@ public abstract class TBuffer { if (mark > newLimit) { mark = -1; } + limit = newLimit; if (position > limit) { position = limit; } diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/nio/TByteBufferImpl.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/nio/TByteBufferImpl.java index ada0b5c3b..1eec7893b 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/nio/TByteBufferImpl.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/nio/TByteBufferImpl.java @@ -21,8 +21,7 @@ class TByteBufferImpl extends TByteBuffer { @Override public TByteBuffer slice() { - return new TByteBufferImpl(position, array.length - position, array, 0, array.length - position, - direct, readOnly); + return new TByteBufferImpl(position, limit - position, array, 0, limit - position, direct, readOnly); } @Override diff --git a/teavm-classlib/src/test/java/org/teavm/classlib/java/nio/ByteBufferTest.java b/teavm-classlib/src/test/java/org/teavm/classlib/java/nio/ByteBufferTest.java index 9877f8e12..c0f3fe799 100644 --- a/teavm-classlib/src/test/java/org/teavm/classlib/java/nio/ByteBufferTest.java +++ b/teavm-classlib/src/test/java/org/teavm/classlib/java/nio/ByteBufferTest.java @@ -1,9 +1,9 @@ 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 static org.junit.Assert.assertThat; +import static org.junit.Assert.fail; +import java.nio.*; import org.junit.Test; /** @@ -159,5 +159,128 @@ public class ByteBufferTest { assertThat(buffer.get(15), is((byte)24)); buffer.put(1, (byte)25); assertThat(duplicate.get(1), is((byte)25)); + assertThat(duplicate.array(), is(sameInstance(buffer.array()))); + } + + @Test + public void getsByte() { + byte[] array = { 2, 3, 5, 7 }; + ByteBuffer buffer = ByteBuffer.wrap(array); + assertThat(buffer.get(), is((byte)2)); + assertThat(buffer.get(), is((byte)3)); + buffer = buffer.slice(); + assertThat(buffer.get(), is((byte)5)); + assertThat(buffer.get(), is((byte)7)); + } + + @Test + public void gettingByteFromEmptyBufferCausesError() { + byte[] array = { 2, 3, 5, 7 }; + ByteBuffer buffer = ByteBuffer.wrap(array); + buffer.limit(2); + buffer.get(); + buffer.get(); + try { + buffer.get(); + fail("Should have thrown error"); + } catch (BufferUnderflowException e) { + // ok + } + } + + @Test + public void putsByte() { + byte[] array = new byte[4]; + ByteBuffer buffer = ByteBuffer.wrap(array); + buffer.put((byte)2).put((byte)3).put((byte)5).put((byte)7); + assertThat(array, is(new byte[] { 2, 3, 5, 7 })); + } + + @Test + public void puttingByteToEmptyBufferCausesError() { + byte[] array = new byte[4]; + ByteBuffer buffer = ByteBuffer.wrap(array); + buffer.limit(2); + buffer.put((byte)2).put((byte)3); + try { + buffer.put((byte)5); + fail("Should have thrown error"); + } catch (BufferOverflowException e) { + System.out.println(e); + assertThat(array[2], is((byte)0)); + } + } + + @Test(expected = ReadOnlyBufferException.class) + public void puttingByteToReadOnlyBufferCausesError() { + byte[] array = new byte[4]; + ByteBuffer buffer = ByteBuffer.wrap(array).asReadOnlyBuffer(); + buffer.put((byte)2); + } + + @Test + public void getsByteFromGivenLocation() { + byte[] array = { 2, 3, 5, 7 }; + ByteBuffer buffer = ByteBuffer.wrap(array); + assertThat(buffer.get(0), is((byte)2)); + assertThat(buffer.get(1), is((byte)3)); + buffer.get(); + buffer = buffer.slice(); + assertThat(buffer.get(1), is((byte)5)); + assertThat(buffer.get(2), is((byte)7)); + } + + @Test + public void gettingByteFromWrongLocationCausesError() { + byte[] array = { 2, 3, 5, 7 }; + ByteBuffer buffer = ByteBuffer.wrap(array); + buffer.limit(3); + try { + buffer.get(-1); + } catch (IndexOutOfBoundsException e) { + // ok + } + try { + buffer.get(3); + } catch (IndexOutOfBoundsException e) { + // ok + } + } + + @Test + public void putsByteToGivenLocation() { + byte[] array = new byte[4]; + ByteBuffer buffer = ByteBuffer.wrap(array); + buffer.put(0, (byte)2); + buffer.put(1, (byte)3); + buffer.get(); + buffer = buffer.slice(); + buffer.put(1, (byte)5); + buffer.put(2, (byte)7); + assertThat(array, is(new byte[] { 2, 3, 5, 7 })); + } + + @Test + public void puttingByteToWrongLocationCausesError() { + byte[] array = new byte[4]; + ByteBuffer buffer = ByteBuffer.wrap(array); + buffer.limit(3); + try { + buffer.put(-1, (byte)2); + } catch (IndexOutOfBoundsException e) { + // ok + } + try { + buffer.put(3, (byte)2); + } catch (IndexOutOfBoundsException e) { + // ok + } + } + + @Test(expected = ReadOnlyBufferException.class) + public void puttingByteToGivenLocationOfReadOnlyBufferCausesError() { + byte[] array = new byte[4]; + ByteBuffer buffer = ByteBuffer.wrap(array).asReadOnlyBuffer(); + buffer.put(0, (byte)2); } }