You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by te...@apache.org on 2006/01/19 17:45:09 UTC
svn commit: r370544 [2/5] - in
/incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/org:
./ apache/ apache/harmony/ apache/harmony/tests/ apache/harmony/tests/java/
apache/harmony/tests/java/nio/ apache/harmony/tests/nio/
Added: incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/org/apache/harmony/tests/java/nio/ByteBufferTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/org/apache/harmony/tests/java/nio/ByteBufferTest.java?rev=370544&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/org/apache/harmony/tests/java/nio/ByteBufferTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/org/apache/harmony/tests/java/nio/ByteBufferTest.java Thu Jan 19 08:45:02 2006
@@ -0,0 +1,2231 @@
+/* Copyright 2005, 2006 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed 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
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+
+package org.apache.harmony.tests.java.nio;
+
+import java.nio.BufferOverflowException;
+import java.nio.BufferUnderflowException;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.nio.CharBuffer;
+import java.nio.DoubleBuffer;
+import java.nio.FloatBuffer;
+import java.nio.IntBuffer;
+import java.nio.InvalidMarkException;
+import java.nio.LongBuffer;
+import java.nio.ReadOnlyBufferException;
+import java.nio.ShortBuffer;
+import java.util.Arrays;
+
+import junit.framework.TestCase;
+
+/**
+ * Tests java.nio.ByteBuffer
+ *
+ */
+public class ByteBufferTest extends TestCase {
+
+ public static void main(String[] args) {
+ junit.textui.TestRunner.run(ByteBufferTest.class);
+ }
+
+ public static void testByteBufferInstance(ByteBuffer buf) {
+ // test Buffer functions
+ BufferTest.testBufferInstance(buf);
+
+ // test ByteBuffer functions
+ testHashCode(buf);
+ testEquals(buf);
+ testToString(buf);
+ testSlice(buf);
+ testDuplicate(buf);
+ testAsReadOnlyBuffer(buf);
+ testGet(buf);
+ testPutbyte(buf);
+ testGetint(buf);
+ testPutintbyte(buf);
+ testGetbyteArrayintint(buf);
+ testGetbyteArray(buf);
+ testPutByteBuffer(buf);
+ testPutbyteArrayintint(buf);
+ testPutbyteArray(buf);
+ testHasArray(buf);
+ testArray(buf);
+ testArrayOffset(buf);
+ testCompact(buf);
+ testIsDirect(buf);
+ testCompareTo(buf);
+ testOrder(buf);
+
+ testAsCharBuffer(buf);
+ testAsDoubleBuffer(buf);
+ testAsFloatBuffer(buf);
+ testAsIntBuffer(buf);
+ testAsLongBuffer(buf);
+ testAsShortBuffer(buf);
+
+ testGetChar(buf);
+ testGetCharint(buf);
+ testPutChar(buf);
+ testPutCharint(buf);
+ testGetDouble(buf);
+ testGetDoubleint(buf);
+ testPutDouble(buf);
+ testPutDoubleint(buf);
+ testGetFloat(buf);
+ testGetFloatint(buf);
+ testPutFloat(buf);
+ testPutFloatint(buf);
+ testGetInt(buf);
+ testGetIntint(buf);
+ testPutInt(buf);
+ testPutIntint(buf);
+ testGetLong(buf);
+ testGetLongint(buf);
+ testPutLong(buf);
+ testPutLongint(buf);
+ testGetShort(buf);
+ testGetShortint(buf);
+ testPutShort(buf);
+ testPutShortint(buf);
+ }
+
+ public static void testArray(ByteBuffer buf) {
+ if (buf.hasArray()) {
+ byte array[] = buf.array();
+ assertContentEquals(buf, array, buf.arrayOffset(), buf.capacity());
+
+ loadTestData1(array, buf.arrayOffset(), buf.capacity());
+ assertContentEquals(buf, array, buf.arrayOffset(), buf.capacity());
+
+ loadTestData2(array, buf.arrayOffset(), buf.capacity());
+ assertContentEquals(buf, array, buf.arrayOffset(), buf.capacity());
+
+ loadTestData1(buf);
+ assertContentEquals(buf, array, buf.arrayOffset(), buf.capacity());
+
+ loadTestData2(buf);
+ assertContentEquals(buf, array, buf.arrayOffset(), buf.capacity());
+ } else {
+ if (buf.isReadOnly()) {
+ try {
+ buf.array();
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (UnsupportedOperationException e) {
+ // expected
+ // Note:can not tell when to throw
+ // UnsupportedOperationException
+ // or ReadOnlyBufferException, so catch all.
+ }
+ } else {
+ try {
+ buf.array();
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (UnsupportedOperationException e) {
+ // expected
+ }
+ }
+ }
+ }
+
+ public static void testArrayOffset(ByteBuffer buf) {
+ if (buf.hasArray()) {
+ byte array[] = buf.array();
+ assertContentEquals(buf, array, buf.arrayOffset(), buf.capacity());
+
+ loadTestData1(array, buf.arrayOffset(), buf.capacity());
+ assertContentEquals(buf, array, buf.arrayOffset(), buf.capacity());
+
+ loadTestData2(array, buf.arrayOffset(), buf.capacity());
+ assertContentEquals(buf, array, buf.arrayOffset(), buf.capacity());
+
+ loadTestData1(buf);
+ assertContentEquals(buf, array, buf.arrayOffset(), buf.capacity());
+
+ loadTestData2(buf);
+ assertContentEquals(buf, array, buf.arrayOffset(), buf.capacity());
+ } else {
+ if (buf.isReadOnly()) {
+ try {
+ buf.arrayOffset();
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (UnsupportedOperationException e) {
+ // expected
+ // Note:can not tell when to throw
+ // UnsupportedOperationException
+ // or ReadOnlyBufferException, so catch all.
+ }
+ } else {
+ try {
+ buf.arrayOffset();
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (UnsupportedOperationException e) {
+ // expected
+ }
+ }
+ }
+ }
+
+ public static void testAsReadOnlyBuffer(ByteBuffer buf) {
+ buf.clear();
+ buf.mark();
+ buf.position(buf.limit());
+
+ // readonly's contents should be the same as buf
+ ByteBuffer readonly = buf.asReadOnlyBuffer();
+ assertNotSame(buf, readonly);
+ assertTrue(readonly.isReadOnly());
+ assertEquals(buf.position(), readonly.position());
+ assertEquals(buf.limit(), readonly.limit());
+ assertEquals(buf.isDirect(), readonly.isDirect());
+ assertEquals(buf.order(), readonly.order());
+ assertContentEquals(buf, readonly);
+
+ // readonly's position, mark, and limit should be independent to buf
+ readonly.reset();
+ assertEquals(readonly.position(), 0);
+ readonly.clear();
+ assertEquals(buf.position(), buf.limit());
+ buf.reset();
+ assertEquals(buf.position(), 0);
+ }
+
+ public static void testCompact(ByteBuffer buf) {
+ if (buf.isReadOnly()) {
+ try {
+ buf.compact();
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (ReadOnlyBufferException e) {
+ // expected
+ }
+ return;
+ }
+
+ // case: buffer is full
+ buf.clear();
+ buf.mark();
+ loadTestData1(buf);
+ ByteBuffer ret = buf.compact();
+ assertSame(ret, buf);
+ assertEquals(buf.position(), buf.capacity());
+ assertEquals(buf.limit(), buf.capacity());
+ assertContentLikeTestData1(buf, 0, (byte) 0, buf.capacity());
+ try {
+ buf.reset();
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (InvalidMarkException e) {
+ // expected
+ }
+
+ // case: buffer is empty
+ buf.position(0);
+ buf.limit(0);
+ buf.mark();
+ ret = buf.compact();
+ assertSame(ret, buf);
+ assertEquals(buf.position(), 0);
+ assertEquals(buf.limit(), buf.capacity());
+ assertContentLikeTestData1(buf, 0, (byte) 0, buf.capacity());
+ try {
+ buf.reset();
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (InvalidMarkException e) {
+ // expected
+ }
+
+ // case: normal
+ assertTrue(buf.capacity() > 5);
+ buf.position(1);
+ buf.limit(5);
+ buf.mark();
+ ret = buf.compact();
+ assertSame(ret, buf);
+ assertEquals(buf.position(), 4);
+ assertEquals(buf.limit(), buf.capacity());
+ assertContentLikeTestData1(buf, 0, (byte) 1, 4);
+ try {
+ buf.reset();
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (InvalidMarkException e) {
+ // expected
+ }
+ }
+
+ public static void testCompareTo(ByteBuffer buf) {
+ // compare to bad type
+ try {
+ buf.compareTo(CharBuffer.allocate(10));
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (ClassCastException e) {
+ // expected
+ }
+
+ // compare to self
+ assertEquals(0, buf.compareTo(buf));
+
+ // normal cases
+ if (!buf.isReadOnly()) {
+ assertTrue(buf.capacity() > 5);
+ buf.clear();
+ ByteBuffer other = ByteBuffer.allocate(buf.capacity());
+ loadTestData1(buf);
+ loadTestData1(other);
+ assertEquals(0, buf.compareTo(other));
+ assertEquals(0, other.compareTo(buf));
+ buf.position(1);
+ assertTrue(buf.compareTo(other) > 0);
+ assertTrue(other.compareTo(buf) < 0);
+ other.position(2);
+ assertTrue(buf.compareTo(other) < 0);
+ assertTrue(other.compareTo(buf) > 0);
+ buf.position(2);
+ other.limit(5);
+ assertTrue(buf.compareTo(other) > 0);
+ assertTrue(other.compareTo(buf) < 0);
+ }
+ }
+
+ public static void testDuplicate(ByteBuffer buf) {
+ buf.clear();
+ buf.mark();
+ buf.position(buf.limit());
+
+ // duplicate's contents should be the same as buf
+ ByteBuffer duplicate = buf.duplicate();
+ assertNotSame(buf, duplicate);
+ assertEquals(buf.position(), duplicate.position());
+ assertEquals(buf.limit(), duplicate.limit());
+ assertEquals(buf.isReadOnly(), duplicate.isReadOnly());
+ assertEquals(buf.isDirect(), duplicate.isDirect());
+ assertEquals(buf.order(), duplicate.order());
+ assertContentEquals(buf, duplicate);
+
+ // duplicate's position, mark, and limit should be independent to buf
+ duplicate.reset();
+ assertEquals(duplicate.position(), 0);
+ duplicate.clear();
+ assertEquals(buf.position(), buf.limit());
+ buf.reset();
+ assertEquals(buf.position(), 0);
+
+ // duplicate share the same content with buf
+ if (!duplicate.isReadOnly()) {
+ loadTestData1(buf);
+ assertContentEquals(buf, duplicate);
+ loadTestData2(duplicate);
+ assertContentEquals(buf, duplicate);
+ }
+ }
+
+ public static void testEquals(ByteBuffer buf) {
+ // equal to self
+ assertTrue(buf.equals(buf));
+ ByteBuffer readonly = buf.asReadOnlyBuffer();
+ assertTrue(buf.equals(readonly));
+ ByteBuffer duplicate = buf.duplicate();
+ assertTrue(buf.equals(duplicate));
+
+ // always false, if type mismatch
+ assertFalse(buf.equals(Boolean.TRUE));
+
+ assertTrue(buf.capacity() > 5);
+
+ buf.limit(buf.capacity()).position(0);
+ readonly.limit(readonly.capacity()).position(1);
+ assertFalse(buf.equals(readonly));
+
+ buf.limit(buf.capacity() - 1).position(0);
+ duplicate.limit(duplicate.capacity()).position(0);
+ assertFalse(buf.equals(duplicate));
+ }
+
+ /*
+ * Class under test for byte get()
+ */
+ public static void testGet(ByteBuffer buf) {
+ buf.clear();
+ for (int i = 0; i < buf.capacity(); i++) {
+ assertEquals(buf.position(), i);
+ assertEquals(buf.get(), buf.get(i));
+ }
+ try {
+ buf.get();
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (BufferUnderflowException e) {
+ // expected
+ }
+ }
+
+ /*
+ * Class under test for java.nio.ByteBuffer get(byte[])
+ */
+ public static void testGetbyteArray(ByteBuffer buf) {
+ byte array[] = new byte[1];
+ buf.clear();
+ for (int i = 0; i < buf.capacity(); i++) {
+ assertEquals(buf.position(), i);
+ ByteBuffer ret = buf.get(array);
+ assertEquals(array[0], buf.get(i));
+ assertSame(ret, buf);
+ }
+ try {
+ buf.get(array);
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (BufferUnderflowException e) {
+ // expected
+ }
+ }
+
+ /*
+ * Class under test for java.nio.ByteBuffer get(byte[], int, int)
+ */
+ public static void testGetbyteArrayintint(ByteBuffer buf) {
+ buf.clear();
+ byte array[] = new byte[buf.capacity()];
+
+ try {
+ buf.get(new byte[buf.capacity() + 1], 0, buf.capacity() + 1);
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (BufferUnderflowException e) {
+ // expected
+ }
+ assertEquals(buf.position(), 0);
+ try {
+ buf.get(array, -1, array.length);
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (IndexOutOfBoundsException e) {
+ // expected
+ }
+ buf.get(array, array.length, 0);
+ try {
+ buf.get(array, array.length + 1, 1);
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (IndexOutOfBoundsException e) {
+ // expected
+ }
+ assertEquals(buf.position(), 0);
+ try {
+ buf.get(array, 2, -1);
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (IndexOutOfBoundsException e) {
+ // expected
+ }
+ try {
+ buf.get(array, 2, array.length);
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (IndexOutOfBoundsException e) {
+ // expected
+ }
+ assertEquals(buf.position(), 0);
+
+ buf.clear();
+ ByteBuffer ret = buf.get(array, 0, array.length);
+ assertEquals(buf.position(), buf.capacity());
+ assertContentEquals(buf, array, 0, array.length);
+ assertSame(ret, buf);
+ }
+
+ /*
+ * Class under test for byte get(int)
+ */
+ public static void testGetint(ByteBuffer buf) {
+ buf.clear();
+ for (int i = 0; i < buf.capacity(); i++) {
+ assertEquals(buf.position(), i);
+ assertEquals(buf.get(), buf.get(i));
+ }
+ try {
+ buf.get(-1);
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (IndexOutOfBoundsException e) {
+ // expected
+ }
+ try {
+ buf.get(buf.limit());
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (IndexOutOfBoundsException e) {
+ // expected
+ }
+ }
+
+ public static void testHasArray(ByteBuffer buf) {
+ if (buf.hasArray()) {
+ assertNotNull(buf.array());
+ } else {
+ if (buf.isReadOnly()) {
+ try {
+ buf.array();
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (UnsupportedOperationException e) {
+ // expected
+ // Note:can not tell when to throw
+ // UnsupportedOperationException
+ // or ReadOnlyBufferException, so catch all.
+ }
+ } else {
+ try {
+ buf.array();
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (UnsupportedOperationException e) {
+ // expected
+ }
+ }
+ }
+ }
+
+ public static void testHashCode(ByteBuffer buf) {
+ buf.clear();
+ ByteBuffer readonly = buf.asReadOnlyBuffer();
+ ByteBuffer duplicate = buf.duplicate();
+ assertTrue(buf.hashCode() == readonly.hashCode());
+
+ assertTrue(buf.capacity() > 5);
+ duplicate.position(buf.capacity() / 2);
+ assertTrue(buf.hashCode() != duplicate.hashCode());
+ }
+
+ public static void testIsDirect(ByteBuffer buf) {
+ buf.isDirect();
+ }
+
+ public static void testOrder(ByteBuffer buf) {
+ // BIG_ENDIAN is the default byte order
+ assertEquals(ByteOrder.BIG_ENDIAN, buf.order());
+ }
+
+ /*
+ * Class under test for java.nio.ByteBuffer put(byte)
+ */
+ public static void testPutbyte(ByteBuffer buf) {
+ if (buf.isReadOnly()) {
+ try {
+ buf.clear();
+ buf.put((byte) 0);
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (ReadOnlyBufferException e) {
+ // expected
+ }
+ return;
+ }
+
+ buf.clear();
+ for (int i = 0; i < buf.capacity(); i++) {
+ assertEquals(buf.position(), i);
+ ByteBuffer ret = buf.put((byte) i);
+ assertEquals(buf.get(i), (byte) i);
+ assertSame(ret, buf);
+ }
+ try {
+ buf.put((byte) 0);
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (BufferOverflowException e) {
+ // expected
+ }
+ }
+
+ /*
+ * Class under test for java.nio.ByteBuffer put(byte[])
+ */
+ public static void testPutbyteArray(ByteBuffer buf) {
+ byte array[] = new byte[1];
+ if (buf.isReadOnly()) {
+ try {
+ buf.put(array);
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (ReadOnlyBufferException e) {
+ // expected
+ }
+ return;
+ }
+
+ buf.clear();
+ for (int i = 0; i < buf.capacity(); i++) {
+ assertEquals(buf.position(), i);
+ array[0] = (byte) i;
+ ByteBuffer ret = buf.put(array);
+ assertEquals(buf.get(i), (byte) i);
+ assertSame(ret, buf);
+ }
+ try {
+ buf.put(array);
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (BufferOverflowException e) {
+ // expected
+ }
+ }
+
+ /*
+ * Class under test for java.nio.ByteBuffer put(byte[], int, int)
+ */
+ public static void testPutbyteArrayintint(ByteBuffer buf) {
+ buf.clear();
+ byte array[] = new byte[buf.capacity()];
+ if (buf.isReadOnly()) {
+ try {
+ buf.put(array, 0, array.length);
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (ReadOnlyBufferException e) {
+ // expected
+ }
+ return;
+ }
+
+ try {
+ buf.put(new byte[buf.capacity() + 1], 0, buf.capacity() + 1);
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (BufferOverflowException e) {
+ // expected
+ }
+ assertEquals(buf.position(), 0);
+ try {
+ buf.put(array, -1, array.length);
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (IndexOutOfBoundsException e) {
+ // expected
+ }
+ try {
+ buf.put(array, array.length + 1, 0);
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (IndexOutOfBoundsException e) {
+ // expected
+ }
+ buf.put(array, array.length, 0);
+ assertEquals(buf.position(), 0);
+ try {
+ buf.put(array, 0, -1);
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (IndexOutOfBoundsException e) {
+ // expected
+ }
+ try {
+ buf.put(array, 2, array.length);
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (IndexOutOfBoundsException e) {
+ // expected
+ }
+ assertEquals(buf.position(), 0);
+
+ loadTestData2(array, 0, array.length);
+ ByteBuffer ret = buf.put(array, 0, array.length);
+ assertEquals(buf.position(), buf.capacity());
+ assertContentEquals(buf, array, 0, array.length);
+ assertSame(ret, buf);
+ }
+
+ /*
+ * Class under test for java.nio.ByteBuffer put(java.nio.ByteBuffer)
+ */
+ public static void testPutByteBuffer(ByteBuffer buf) {
+ ByteBuffer other = ByteBuffer.allocate(buf.capacity());
+ if (buf.isReadOnly()) {
+ try {
+ buf.clear();
+ buf.put(other);
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (ReadOnlyBufferException e) {
+ // expected
+ }
+ return;
+ }
+
+ try {
+ buf.put(buf);
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (IllegalArgumentException e) {
+ // expected
+ }
+ try {
+ buf.put(ByteBuffer.allocate(buf.capacity() + 1));
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (BufferOverflowException e) {
+ // expected
+ }
+
+ loadTestData2(other);
+ other.clear();
+ buf.clear();
+ ByteBuffer ret = buf.put(other);
+ assertEquals(other.position(), other.capacity());
+ assertEquals(buf.position(), buf.capacity());
+ assertContentEquals(other, buf);
+ assertSame(ret, buf);
+ }
+
+ /*
+ * Class under test for java.nio.ByteBuffer put(int, byte)
+ */
+ public static void testPutintbyte(ByteBuffer buf) {
+ if (buf.isReadOnly()) {
+ try {
+ buf.put(0, (byte) 0);
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (ReadOnlyBufferException e) {
+ // expected
+ }
+ return;
+ }
+
+ buf.clear();
+ for (int i = 0; i < buf.capacity(); i++) {
+ assertEquals(buf.position(), 0);
+ ByteBuffer ret = buf.put(i, (byte) i);
+ assertEquals(buf.get(i), (byte) i);
+ assertSame(ret, buf);
+ }
+ try {
+ buf.put(-1, (byte) 0);
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (IndexOutOfBoundsException e) {
+ // expected
+ }
+ try {
+ buf.put(buf.limit(), (byte) 0);
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (IndexOutOfBoundsException e) {
+ // expected
+ }
+ }
+
+ public static void testSlice(ByteBuffer buf) {
+ assertTrue(buf.capacity() > 5);
+ buf.position(1);
+ buf.limit(buf.capacity() - 1);
+
+ ByteBuffer slice = buf.slice();
+ assertEquals(buf.isReadOnly(), slice.isReadOnly());
+ assertEquals(buf.isDirect(), slice.isDirect());
+ assertEquals(buf.order(), slice.order());
+ assertEquals(slice.position(), 0);
+ assertEquals(slice.limit(), buf.remaining());
+ assertEquals(slice.capacity(), buf.remaining());
+ try {
+ slice.reset();
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (InvalidMarkException e) {
+ // expected
+ }
+
+ // slice share the same content with buf
+ if (!slice.isReadOnly()) {
+ loadTestData1(slice);
+ assertContentLikeTestData1(buf, 1, (byte) 0, slice.capacity());
+ buf.put(2, (byte) 100);
+ assertEquals(slice.get(1), 100);
+ }
+ }
+
+ public static void testToString(ByteBuffer buf) {
+ String str = buf.toString();
+ assertTrue(str.indexOf("Byte") >= 0 || str.indexOf("byte") >= 0);
+ assertTrue(str.indexOf("" + buf.position()) >= 0);
+ assertTrue(str.indexOf("" + buf.limit()) >= 0);
+ assertTrue(str.indexOf("" + buf.capacity()) >= 0);
+ }
+
+ public static void testAsCharBuffer(ByteBuffer buf) {
+ CharBuffer charBuffer;
+ byte bytes[] = new byte[2];
+ char value;
+
+ // test BIG_ENDIAN char buffer, read
+ buf.clear();
+ buf.order(ByteOrder.BIG_ENDIAN);
+ charBuffer = buf.asCharBuffer();
+ assertSame(ByteOrder.BIG_ENDIAN, charBuffer.order());
+ while (charBuffer.remaining() > 0) {
+ buf.get(bytes);
+ value = charBuffer.get();
+ assertEquals(bytes2char(bytes, buf.order()), value);
+ }
+
+ // test LITTLE_ENDIAN char buffer, read
+ buf.clear();
+ buf.order(ByteOrder.LITTLE_ENDIAN);
+ charBuffer = buf.asCharBuffer();
+ assertSame(ByteOrder.LITTLE_ENDIAN, charBuffer.order());
+ while (charBuffer.remaining() > 0) {
+ buf.get(bytes);
+ value = charBuffer.get();
+ assertEquals(bytes2char(bytes, buf.order()), value);
+ }
+
+ if (!buf.isReadOnly()) {
+ // test BIG_ENDIAN char buffer, write
+ buf.clear();
+ buf.order(ByteOrder.BIG_ENDIAN);
+ charBuffer = buf.asCharBuffer();
+ assertSame(ByteOrder.BIG_ENDIAN, charBuffer.order());
+ while (charBuffer.remaining() > 0) {
+ value = (char) charBuffer.remaining();
+ charBuffer.put(value);
+ buf.get(bytes);
+ Arrays.equals(bytes, char2bytes(value, buf.order()));
+ }
+
+ // test LITTLE_ENDIAN char buffer, write
+ buf.clear();
+ buf.order(ByteOrder.LITTLE_ENDIAN);
+ charBuffer = buf.asCharBuffer();
+ assertSame(ByteOrder.LITTLE_ENDIAN, charBuffer.order());
+ while (charBuffer.remaining() > 0) {
+ value = (char) charBuffer.remaining();
+ charBuffer.put(value);
+ buf.get(bytes);
+ Arrays.equals(bytes, char2bytes(value, buf.order()));
+ }
+ }
+
+ buf.clear();
+ buf.order(ByteOrder.BIG_ENDIAN);
+ CharBufferTest.testCharBufferInstance(buf.asCharBuffer());
+ }
+
+ public static void testAsDoubleBuffer(ByteBuffer buf) {
+ DoubleBuffer doubleBuffer;
+ byte bytes[] = new byte[8];
+ double value;
+
+ // test BIG_ENDIAN double buffer, read
+ buf.clear();
+ buf.order(ByteOrder.BIG_ENDIAN);
+ doubleBuffer = buf.asDoubleBuffer();
+ assertSame(ByteOrder.BIG_ENDIAN, doubleBuffer.order());
+ while (doubleBuffer.remaining() > 0) {
+ buf.get(bytes);
+ value = doubleBuffer.get();
+ if (!(Double.isNaN(bytes2double(bytes, buf.order())) && Double
+ .isNaN(value))) {
+ assertEquals(bytes2double(bytes, buf.order()), value, 0.00);
+ }
+ }
+
+ // test LITTLE_ENDIAN double buffer, read
+ buf.clear();
+ buf.order(ByteOrder.LITTLE_ENDIAN);
+ doubleBuffer = buf.asDoubleBuffer();
+ assertSame(ByteOrder.LITTLE_ENDIAN, doubleBuffer.order());
+ while (doubleBuffer.remaining() > 0) {
+ buf.get(bytes);
+ value = doubleBuffer.get();
+ if (!(Double.isNaN(bytes2double(bytes, buf.order())) && Double
+ .isNaN(value))) {
+ assertEquals(bytes2double(bytes, buf.order()), value, 0.00);
+ }
+ }
+
+ if (!buf.isReadOnly()) {
+ // test BIG_ENDIAN double buffer, write
+ buf.clear();
+ buf.order(ByteOrder.BIG_ENDIAN);
+ doubleBuffer = buf.asDoubleBuffer();
+ assertSame(ByteOrder.BIG_ENDIAN, doubleBuffer.order());
+ while (doubleBuffer.remaining() > 0) {
+ value = (double) doubleBuffer.remaining();
+ doubleBuffer.put(value);
+ buf.get(bytes);
+ Arrays.equals(bytes, double2bytes(value, buf.order()));
+ }
+
+ // test LITTLE_ENDIAN double buffer, write
+ buf.clear();
+ buf.order(ByteOrder.LITTLE_ENDIAN);
+ doubleBuffer = buf.asDoubleBuffer();
+ assertSame(ByteOrder.LITTLE_ENDIAN, doubleBuffer.order());
+ while (doubleBuffer.remaining() > 0) {
+ value = (double) doubleBuffer.remaining();
+ doubleBuffer.put(value);
+ buf.get(bytes);
+ Arrays.equals(bytes, double2bytes(value, buf.order()));
+ }
+ }
+
+ buf.clear();
+ buf.order(ByteOrder.BIG_ENDIAN);
+ DoubleBufferTest.testDoubleBufferInstance(buf.asDoubleBuffer());
+ }
+
+ public static void testAsFloatBuffer(ByteBuffer buf) {
+ FloatBuffer floatBuffer;
+ byte bytes[] = new byte[4];
+ float value;
+
+ // test BIG_ENDIAN float buffer, read
+ buf.clear();
+ buf.order(ByteOrder.BIG_ENDIAN);
+ floatBuffer = buf.asFloatBuffer();
+ assertSame(ByteOrder.BIG_ENDIAN, floatBuffer.order());
+ while (floatBuffer.remaining() > 0) {
+ buf.get(bytes);
+ value = floatBuffer.get();
+ if (!(Float.isNaN(bytes2float(bytes, buf.order())) && Float
+ .isNaN(value))) {
+ assertEquals(bytes2float(bytes, buf.order()), value, 0.00);
+ }
+ }
+
+ // test LITTLE_ENDIAN float buffer, read
+ buf.clear();
+ buf.order(ByteOrder.LITTLE_ENDIAN);
+ floatBuffer = buf.asFloatBuffer();
+ assertSame(ByteOrder.LITTLE_ENDIAN, floatBuffer.order());
+ while (floatBuffer.remaining() > 0) {
+ buf.get(bytes);
+ value = floatBuffer.get();
+ if (!(Float.isNaN(bytes2float(bytes, buf.order())) && Float
+ .isNaN(value))) {
+ assertEquals(bytes2float(bytes, buf.order()), value, 0.00);
+ }
+ }
+
+ if (!buf.isReadOnly()) {
+ // test BIG_ENDIAN float buffer, write
+ buf.clear();
+ buf.order(ByteOrder.BIG_ENDIAN);
+ floatBuffer = buf.asFloatBuffer();
+ assertSame(ByteOrder.BIG_ENDIAN, floatBuffer.order());
+ while (floatBuffer.remaining() > 0) {
+ value = (float) floatBuffer.remaining();
+ floatBuffer.put(value);
+ buf.get(bytes);
+ Arrays.equals(bytes, float2bytes(value, buf.order()));
+ }
+
+ // test LITTLE_ENDIAN float buffer, write
+ buf.clear();
+ buf.order(ByteOrder.LITTLE_ENDIAN);
+ floatBuffer = buf.asFloatBuffer();
+ assertSame(ByteOrder.LITTLE_ENDIAN, floatBuffer.order());
+ while (floatBuffer.remaining() > 0) {
+ value = (float) floatBuffer.remaining();
+ floatBuffer.put(value);
+ buf.get(bytes);
+ Arrays.equals(bytes, float2bytes(value, buf.order()));
+ }
+ }
+
+ buf.clear();
+ buf.order(ByteOrder.BIG_ENDIAN);
+ FloatBufferTest.testFloatBufferInstance(buf.asFloatBuffer());
+ }
+
+ public static void testAsIntBuffer(ByteBuffer buf) {
+ IntBuffer intBuffer;
+ byte bytes[] = new byte[4];
+ int value;
+
+ // test BIG_ENDIAN int buffer, read
+ buf.clear();
+ buf.order(ByteOrder.BIG_ENDIAN);
+ intBuffer = buf.asIntBuffer();
+ assertSame(ByteOrder.BIG_ENDIAN, intBuffer.order());
+ while (intBuffer.remaining() > 0) {
+ buf.get(bytes);
+ value = intBuffer.get();
+ assertEquals(bytes2int(bytes, buf.order()), value);
+ }
+
+ // test LITTLE_ENDIAN int buffer, read
+ buf.clear();
+ buf.order(ByteOrder.LITTLE_ENDIAN);
+ intBuffer = buf.asIntBuffer();
+ assertSame(ByteOrder.LITTLE_ENDIAN, intBuffer.order());
+ while (intBuffer.remaining() > 0) {
+ buf.get(bytes);
+ value = intBuffer.get();
+ assertEquals(bytes2int(bytes, buf.order()), value);
+ }
+
+ if (!buf.isReadOnly()) {
+ // test BIG_ENDIAN int buffer, write
+ buf.clear();
+ buf.order(ByteOrder.BIG_ENDIAN);
+ intBuffer = buf.asIntBuffer();
+ assertSame(ByteOrder.BIG_ENDIAN, intBuffer.order());
+ while (intBuffer.remaining() > 0) {
+ value = (int) intBuffer.remaining();
+ intBuffer.put(value);
+ buf.get(bytes);
+ Arrays.equals(bytes, int2bytes(value, buf.order()));
+ }
+
+ // test LITTLE_ENDIAN int buffer, write
+ buf.clear();
+ buf.order(ByteOrder.LITTLE_ENDIAN);
+ intBuffer = buf.asIntBuffer();
+ assertSame(ByteOrder.LITTLE_ENDIAN, intBuffer.order());
+ while (intBuffer.remaining() > 0) {
+ value = (int) intBuffer.remaining();
+ intBuffer.put(value);
+ buf.get(bytes);
+ Arrays.equals(bytes, int2bytes(value, buf.order()));
+ }
+ }
+
+ buf.clear();
+ buf.order(ByteOrder.BIG_ENDIAN);
+ IntBufferTest.testIntBufferInstance(buf.asIntBuffer());
+ }
+
+ public static void testAsLongBuffer(ByteBuffer buf) {
+ LongBuffer longBuffer;
+ byte bytes[] = new byte[8];
+ long value;
+
+ // test BIG_ENDIAN long buffer, read
+ buf.clear();
+ buf.order(ByteOrder.BIG_ENDIAN);
+ longBuffer = buf.asLongBuffer();
+ assertSame(ByteOrder.BIG_ENDIAN, longBuffer.order());
+ while (longBuffer.remaining() > 0) {
+ buf.get(bytes);
+ value = longBuffer.get();
+ assertEquals(bytes2long(bytes, buf.order()), value);
+ }
+
+ // test LITTLE_ENDIAN long buffer, read
+ buf.clear();
+ buf.order(ByteOrder.LITTLE_ENDIAN);
+ longBuffer = buf.asLongBuffer();
+ assertSame(ByteOrder.LITTLE_ENDIAN, longBuffer.order());
+ while (longBuffer.remaining() > 0) {
+ buf.get(bytes);
+ value = longBuffer.get();
+ assertEquals(bytes2long(bytes, buf.order()), value);
+ }
+
+ if (!buf.isReadOnly()) {
+ // test BIG_ENDIAN long buffer, write
+ buf.clear();
+ buf.order(ByteOrder.BIG_ENDIAN);
+ longBuffer = buf.asLongBuffer();
+ assertSame(ByteOrder.BIG_ENDIAN, longBuffer.order());
+ while (longBuffer.remaining() > 0) {
+ value = (long) longBuffer.remaining();
+ longBuffer.put(value);
+ buf.get(bytes);
+ Arrays.equals(bytes, long2bytes(value, buf.order()));
+ }
+
+ // test LITTLE_ENDIAN long buffer, write
+ buf.clear();
+ buf.order(ByteOrder.LITTLE_ENDIAN);
+ longBuffer = buf.asLongBuffer();
+ assertSame(ByteOrder.LITTLE_ENDIAN, longBuffer.order());
+ while (longBuffer.remaining() > 0) {
+ value = (long) longBuffer.remaining();
+ longBuffer.put(value);
+ buf.get(bytes);
+ Arrays.equals(bytes, long2bytes(value, buf.order()));
+ }
+ }
+
+ buf.clear();
+ buf.order(ByteOrder.BIG_ENDIAN);
+ LongBufferTest.testLongBufferInstance(buf.asLongBuffer());
+ }
+
+ public static void testAsShortBuffer(ByteBuffer buf) {
+ ShortBuffer shortBuffer;
+ byte bytes[] = new byte[2];
+ short value;
+
+ // test BIG_ENDIAN short buffer, read
+ buf.clear();
+ buf.order(ByteOrder.BIG_ENDIAN);
+ shortBuffer = buf.asShortBuffer();
+ assertSame(ByteOrder.BIG_ENDIAN, shortBuffer.order());
+ while (shortBuffer.remaining() > 0) {
+ buf.get(bytes);
+ value = shortBuffer.get();
+ assertEquals(bytes2short(bytes, buf.order()), value);
+ }
+
+ // test LITTLE_ENDIAN short buffer, read
+ buf.clear();
+ buf.order(ByteOrder.LITTLE_ENDIAN);
+ shortBuffer = buf.asShortBuffer();
+ assertSame(ByteOrder.LITTLE_ENDIAN, shortBuffer.order());
+ while (shortBuffer.remaining() > 0) {
+ buf.get(bytes);
+ value = shortBuffer.get();
+ assertEquals(bytes2short(bytes, buf.order()), value);
+ }
+
+ if (!buf.isReadOnly()) {
+ // test BIG_ENDIAN short buffer, write
+ buf.clear();
+ buf.order(ByteOrder.BIG_ENDIAN);
+ shortBuffer = buf.asShortBuffer();
+ assertSame(ByteOrder.BIG_ENDIAN, shortBuffer.order());
+ while (shortBuffer.remaining() > 0) {
+ value = (short) shortBuffer.remaining();
+ shortBuffer.put(value);
+ buf.get(bytes);
+ Arrays.equals(bytes, short2bytes(value, buf.order()));
+ }
+
+ // test LITTLE_ENDIAN short buffer, write
+ buf.clear();
+ buf.order(ByteOrder.LITTLE_ENDIAN);
+ shortBuffer = buf.asShortBuffer();
+ assertSame(ByteOrder.LITTLE_ENDIAN, shortBuffer.order());
+ while (shortBuffer.remaining() > 0) {
+ value = (short) shortBuffer.remaining();
+ shortBuffer.put(value);
+ buf.get(bytes);
+ Arrays.equals(bytes, short2bytes(value, buf.order()));
+ }
+ }
+
+ buf.clear();
+ buf.order(ByteOrder.BIG_ENDIAN);
+ ShortBufferTest.testShortBufferInstance(buf.asShortBuffer());
+ }
+
+ public static void testGetChar(ByteBuffer buf) {
+ int nbytes = 2;
+ byte bytes[] = new byte[nbytes];
+ char value;
+ buf.clear();
+ for (int i = 0; buf.remaining() >= nbytes; i++) {
+ buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN
+ : ByteOrder.LITTLE_ENDIAN);
+ assertEquals(i * nbytes, buf.position());
+ buf.mark();
+ buf.get(bytes);
+ buf.reset();
+ value = buf.getChar();
+ assertEquals(bytes2char(bytes, buf.order()), value);
+ }
+
+ try {
+ buf.getChar();
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (BufferUnderflowException e) {
+ // expected
+ }
+
+ buf.order(ByteOrder.BIG_ENDIAN);
+ }
+
+ public static void testGetCharint(ByteBuffer buf) {
+ int nbytes = 2;
+ byte bytes[] = new byte[nbytes];
+ char value;
+ buf.clear();
+ for (int i = 0; i <= buf.limit() - nbytes; i++) {
+ buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN
+ : ByteOrder.LITTLE_ENDIAN);
+ buf.position(i);
+ value = buf.getChar(i);
+ assertEquals(i, buf.position());
+ buf.get(bytes);
+ assertEquals(bytes2char(bytes, buf.order()), value);
+ }
+
+ try {
+ buf.getChar(-1);
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (IndexOutOfBoundsException e) {
+ // expected
+ }
+ try {
+ buf.getChar(buf.limit() - nbytes + 1);
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (IndexOutOfBoundsException e) {
+ // expected
+ }
+
+ buf.order(ByteOrder.BIG_ENDIAN);
+ }
+
+ public static void testPutChar(ByteBuffer buf) {
+ if (buf.isReadOnly()) {
+ try {
+ buf.clear();
+ buf.putChar((char) 1);
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (ReadOnlyBufferException e) {
+ // expected
+ }
+ return;
+ }
+
+ int nbytes = 2;
+ byte bytes[] = new byte[nbytes];
+ char value = 0;
+ buf.clear();
+ for (int i = 0; buf.remaining() >= nbytes; i++) {
+ buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN
+ : ByteOrder.LITTLE_ENDIAN);
+ value = (char) i;
+ buf.mark();
+ buf.putChar(value);
+ assertEquals((i + 1) * nbytes, buf.position());
+ buf.reset();
+ buf.get(bytes);
+ Arrays.equals(char2bytes(value, buf.order()), bytes);
+ }
+
+ try {
+ buf.putChar(value);
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (BufferOverflowException e) {
+ // expected
+ }
+
+ buf.order(ByteOrder.BIG_ENDIAN);
+ }
+
+ public static void testPutCharint(ByteBuffer buf) {
+ if (buf.isReadOnly()) {
+ try {
+ buf.putChar(0, (char) 1);
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (ReadOnlyBufferException e) {
+ // expected
+ }
+ return;
+ }
+
+ int nbytes = 2;
+ byte bytes[] = new byte[nbytes];
+ char value = 0;
+ buf.clear();
+ for (int i = 0; i <= buf.limit() - nbytes; i++) {
+ buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN
+ : ByteOrder.LITTLE_ENDIAN);
+ value = (char) i;
+ buf.position(i);
+ buf.putChar(i, value);
+ assertEquals(i, buf.position());
+ buf.get(bytes);
+ Arrays.equals(char2bytes(value, buf.order()), bytes);
+ }
+
+ try {
+ buf.putChar(-1, value);
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (IndexOutOfBoundsException e) {
+ // expected
+ }
+ try {
+ buf.putChar(buf.limit() - nbytes + 1, value);
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (IndexOutOfBoundsException e) {
+ // expected
+ }
+
+ buf.order(ByteOrder.BIG_ENDIAN);
+ }
+
+ public static void testGetDouble(ByteBuffer buf) {
+ int nbytes = 8;
+ byte bytes[] = new byte[nbytes];
+ double value;
+ buf.clear();
+ for (int i = 0; buf.remaining() >= nbytes; i++) {
+ buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN
+ : ByteOrder.LITTLE_ENDIAN);
+ assertEquals(i * nbytes, buf.position());
+ buf.mark();
+ buf.get(bytes);
+ buf.reset();
+ value = buf.getDouble();
+ if (!(Double.isNaN(bytes2double(bytes, buf.order())) && Double
+ .isNaN(value))) {
+ assertEquals(bytes2double(bytes, buf.order()), value, 0.00);
+ }
+ }
+
+ try {
+ buf.getDouble();
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (BufferUnderflowException e) {
+ // expected
+ }
+
+ buf.order(ByteOrder.BIG_ENDIAN);
+ }
+
+ public static void testGetDoubleint(ByteBuffer buf) {
+ int nbytes = 8;
+ byte bytes[] = new byte[nbytes];
+ double value;
+ buf.clear();
+ for (int i = 0; i <= buf.limit() - nbytes; i++) {
+ buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN
+ : ByteOrder.LITTLE_ENDIAN);
+ buf.position(i);
+ value = buf.getDouble(i);
+ assertEquals(i, buf.position());
+ buf.get(bytes);
+ if (!(Double.isNaN(bytes2double(bytes, buf.order())) && Double
+ .isNaN(value))) {
+ assertEquals(bytes2double(bytes, buf.order()), value, 0.00);
+ }
+ }
+
+ try {
+ buf.getDouble(-1);
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (IndexOutOfBoundsException e) {
+ // expected
+ }
+ try {
+ buf.getDouble(buf.limit() - nbytes + 1);
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (IndexOutOfBoundsException e) {
+ // expected
+ }
+
+ buf.order(ByteOrder.BIG_ENDIAN);
+ }
+
+ public static void testPutDouble(ByteBuffer buf) {
+ if (buf.isReadOnly()) {
+ try {
+ buf.clear();
+ buf.putDouble((double) 1);
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (ReadOnlyBufferException e) {
+ // expected
+ }
+ return;
+ }
+
+ int nbytes = 8;
+ byte bytes[] = new byte[nbytes];
+ double value = 0;
+ buf.clear();
+ for (int i = 0; buf.remaining() >= nbytes; i++) {
+ buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN
+ : ByteOrder.LITTLE_ENDIAN);
+ value = (double) i;
+ buf.mark();
+ buf.putDouble(value);
+ assertEquals((i + 1) * nbytes, buf.position());
+ buf.reset();
+ buf.get(bytes);
+ Arrays.equals(double2bytes(value, buf.order()), bytes);
+ }
+
+ try {
+ buf.putDouble(value);
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (BufferOverflowException e) {
+ // expected
+ }
+
+ buf.order(ByteOrder.BIG_ENDIAN);
+ }
+
+ public static void testPutDoubleint(ByteBuffer buf) {
+ if (buf.isReadOnly()) {
+ try {
+ buf.putDouble(0, (double) 1);
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (ReadOnlyBufferException e) {
+ // expected
+ }
+ return;
+ }
+
+ int nbytes = 8;
+ byte bytes[] = new byte[nbytes];
+ double value = 0;
+ buf.clear();
+ for (int i = 0; i <= buf.limit() - nbytes; i++) {
+ buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN
+ : ByteOrder.LITTLE_ENDIAN);
+ value = (double) i;
+ buf.position(i);
+ buf.putDouble(i, value);
+ assertEquals(i, buf.position());
+ buf.get(bytes);
+ Arrays.equals(double2bytes(value, buf.order()), bytes);
+ }
+
+ try {
+ buf.putDouble(-1, value);
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (IndexOutOfBoundsException e) {
+ // expected
+ }
+ try {
+ buf.putDouble(buf.limit() - nbytes + 1, value);
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (IndexOutOfBoundsException e) {
+ // expected
+ }
+
+ buf.order(ByteOrder.BIG_ENDIAN);
+ }
+
+ public static void testGetFloat(ByteBuffer buf) {
+ int nbytes = 4;
+ byte bytes[] = new byte[nbytes];
+ float value;
+ buf.clear();
+ for (int i = 0; buf.remaining() >= nbytes; i++) {
+ buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN
+ : ByteOrder.LITTLE_ENDIAN);
+ assertEquals(i * nbytes, buf.position());
+ buf.mark();
+ buf.get(bytes);
+ buf.reset();
+ value = buf.getFloat();
+ if (!(Float.isNaN(bytes2float(bytes, buf.order())) && Float
+ .isNaN(value))) {
+ assertEquals(bytes2float(bytes, buf.order()), value, 0.00);
+ }
+ }
+
+ try {
+ buf.getFloat();
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (BufferUnderflowException e) {
+ // expected
+ }
+
+ buf.order(ByteOrder.BIG_ENDIAN);
+ }
+
+ public static void testGetFloatint(ByteBuffer buf) {
+ int nbytes = 4;
+ byte bytes[] = new byte[nbytes];
+ float value;
+ buf.clear();
+ for (int i = 0; i <= buf.limit() - nbytes; i++) {
+ buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN
+ : ByteOrder.LITTLE_ENDIAN);
+ buf.position(i);
+ value = buf.getFloat(i);
+ assertEquals(i, buf.position());
+ buf.get(bytes);
+ if (!(Float.isNaN(bytes2float(bytes, buf.order())) && Float
+ .isNaN(value))) {
+ assertEquals(bytes2float(bytes, buf.order()), value, 0.00);
+ }
+ }
+
+ try {
+ buf.getFloat(-1);
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (IndexOutOfBoundsException e) {
+ // expected
+ }
+ try {
+ buf.getFloat(buf.limit() - nbytes + 1);
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (IndexOutOfBoundsException e) {
+ // expected
+ }
+
+ buf.order(ByteOrder.BIG_ENDIAN);
+ }
+
+ public static void testPutFloat(ByteBuffer buf) {
+ if (buf.isReadOnly()) {
+ try {
+ buf.clear();
+ buf.putFloat((float) 1);
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (ReadOnlyBufferException e) {
+ // expected
+ }
+ return;
+ }
+
+ int nbytes = 4;
+ byte bytes[] = new byte[nbytes];
+ float value = 0;
+ buf.clear();
+ for (int i = 0; buf.remaining() >= nbytes; i++) {
+ buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN
+ : ByteOrder.LITTLE_ENDIAN);
+ value = (float) i;
+ buf.mark();
+ buf.putFloat(value);
+ assertEquals((i + 1) * nbytes, buf.position());
+ buf.reset();
+ buf.get(bytes);
+ Arrays.equals(float2bytes(value, buf.order()), bytes);
+ }
+
+ try {
+ buf.putFloat(value);
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (BufferOverflowException e) {
+ // expected
+ }
+
+ buf.order(ByteOrder.BIG_ENDIAN);
+ }
+
+ public static void testPutFloatint(ByteBuffer buf) {
+ if (buf.isReadOnly()) {
+ try {
+ buf.putFloat(0, (float) 1);
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (ReadOnlyBufferException e) {
+ // expected
+ }
+ return;
+ }
+
+ int nbytes = 4;
+ byte bytes[] = new byte[nbytes];
+ float value = 0;
+ buf.clear();
+ for (int i = 0; i <= buf.limit() - nbytes; i++) {
+ buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN
+ : ByteOrder.LITTLE_ENDIAN);
+ value = (float) i;
+ buf.position(i);
+ buf.putFloat(i, value);
+ assertEquals(i, buf.position());
+ buf.get(bytes);
+ Arrays.equals(float2bytes(value, buf.order()), bytes);
+ }
+
+ try {
+ buf.putFloat(-1, value);
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (IndexOutOfBoundsException e) {
+ // expected
+ }
+ try {
+ buf.putFloat(buf.limit() - nbytes + 1, value);
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (IndexOutOfBoundsException e) {
+ // expected
+ }
+
+ buf.order(ByteOrder.BIG_ENDIAN);
+ }
+
+ public static void testGetInt(ByteBuffer buf) {
+ int nbytes = 4;
+ byte bytes[] = new byte[nbytes];
+ int value;
+ buf.clear();
+ for (int i = 0; buf.remaining() >= nbytes; i++) {
+ buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN
+ : ByteOrder.LITTLE_ENDIAN);
+ assertEquals(i * nbytes, buf.position());
+ buf.mark();
+ buf.get(bytes);
+ buf.reset();
+ value = buf.getInt();
+ assertEquals(bytes2int(bytes, buf.order()), value);
+ }
+
+ try {
+ buf.getInt();
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (BufferUnderflowException e) {
+ // expected
+ }
+
+ buf.order(ByteOrder.BIG_ENDIAN);
+ }
+
+ public static void testGetIntint(ByteBuffer buf) {
+ int nbytes = 4;
+ byte bytes[] = new byte[nbytes];
+ int value;
+ buf.clear();
+ for (int i = 0; i <= buf.limit() - nbytes; i++) {
+ buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN
+ : ByteOrder.LITTLE_ENDIAN);
+ buf.position(i);
+ value = buf.getInt(i);
+ assertEquals(i, buf.position());
+ buf.get(bytes);
+ assertEquals(bytes2int(bytes, buf.order()), value);
+ }
+
+ try {
+ buf.getInt(-1);
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (IndexOutOfBoundsException e) {
+ // expected
+ }
+ try {
+ buf.getInt(buf.limit() - nbytes + 1);
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (IndexOutOfBoundsException e) {
+ // expected
+ }
+
+ buf.order(ByteOrder.BIG_ENDIAN);
+ }
+
+ public static void testPutInt(ByteBuffer buf) {
+ if (buf.isReadOnly()) {
+ try {
+ buf.clear();
+ buf.putInt((int) 1);
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (ReadOnlyBufferException e) {
+ // expected
+ }
+ return;
+ }
+
+ int nbytes = 4;
+ byte bytes[] = new byte[nbytes];
+ int value = 0;
+ buf.clear();
+ for (int i = 0; buf.remaining() >= nbytes; i++) {
+ buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN
+ : ByteOrder.LITTLE_ENDIAN);
+ value = (int) i;
+ buf.mark();
+ buf.putInt(value);
+ assertEquals((i + 1) * nbytes, buf.position());
+ buf.reset();
+ buf.get(bytes);
+ Arrays.equals(int2bytes(value, buf.order()), bytes);
+ }
+
+ try {
+ buf.putInt(value);
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (BufferOverflowException e) {
+ // expected
+ }
+
+ buf.order(ByteOrder.BIG_ENDIAN);
+ }
+
+ public static void testPutIntint(ByteBuffer buf) {
+ if (buf.isReadOnly()) {
+ try {
+ buf.putInt(0, (int) 1);
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (ReadOnlyBufferException e) {
+ // expected
+ }
+ return;
+ }
+
+ int nbytes = 4;
+ byte bytes[] = new byte[nbytes];
+ int value = 0;
+ buf.clear();
+ for (int i = 0; i <= buf.limit() - nbytes; i++) {
+ buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN
+ : ByteOrder.LITTLE_ENDIAN);
+ value = (int) i;
+ buf.position(i);
+ buf.putInt(i, value);
+ assertEquals(i, buf.position());
+ buf.get(bytes);
+ Arrays.equals(int2bytes(value, buf.order()), bytes);
+ }
+
+ try {
+ buf.putInt(-1, value);
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (IndexOutOfBoundsException e) {
+ // expected
+ }
+ try {
+ buf.putInt(buf.limit() - nbytes + 1, value);
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (IndexOutOfBoundsException e) {
+ // expected
+ }
+
+ buf.order(ByteOrder.BIG_ENDIAN);
+ }
+
+ public static void testGetLong(ByteBuffer buf) {
+ int nbytes = 8;
+ byte bytes[] = new byte[nbytes];
+ long value;
+ buf.clear();
+ for (int i = 0; buf.remaining() >= nbytes; i++) {
+ buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN
+ : ByteOrder.LITTLE_ENDIAN);
+ assertEquals(i * nbytes, buf.position());
+ buf.mark();
+ buf.get(bytes);
+ buf.reset();
+ value = buf.getLong();
+ assertEquals(bytes2long(bytes, buf.order()), value);
+ }
+
+ try {
+ buf.getLong();
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (BufferUnderflowException e) {
+ // expected
+ }
+
+ buf.order(ByteOrder.BIG_ENDIAN);
+ }
+
+ public static void testGetLongint(ByteBuffer buf) {
+ int nbytes = 8;
+ byte bytes[] = new byte[nbytes];
+ long value;
+ buf.clear();
+ for (int i = 0; i <= buf.limit() - nbytes; i++) {
+ buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN
+ : ByteOrder.LITTLE_ENDIAN);
+ buf.position(i);
+ value = buf.getLong(i);
+ assertEquals(i, buf.position());
+ buf.get(bytes);
+ assertEquals(bytes2long(bytes, buf.order()), value);
+ }
+
+ try {
+ buf.getLong(-1);
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (IndexOutOfBoundsException e) {
+ // expected
+ }
+ try {
+ buf.getLong(buf.limit() - nbytes + 1);
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (IndexOutOfBoundsException e) {
+ // expected
+ }
+
+ buf.order(ByteOrder.BIG_ENDIAN);
+ }
+
+ public static void testPutLong(ByteBuffer buf) {
+ if (buf.isReadOnly()) {
+ try {
+ buf.clear();
+ buf.putLong((long) 1);
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (ReadOnlyBufferException e) {
+ // expected
+ }
+ return;
+ }
+
+ int nbytes = 8;
+ byte bytes[] = new byte[nbytes];
+ long value = 0;
+ buf.clear();
+ for (int i = 0; buf.remaining() >= nbytes; i++) {
+ buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN
+ : ByteOrder.LITTLE_ENDIAN);
+ value = (long) i;
+ buf.mark();
+ buf.putLong(value);
+ assertEquals((i + 1) * nbytes, buf.position());
+ buf.reset();
+ buf.get(bytes);
+ Arrays.equals(long2bytes(value, buf.order()), bytes);
+ }
+
+ try {
+ buf.putLong(value);
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (BufferOverflowException e) {
+ // expected
+ }
+
+ buf.order(ByteOrder.BIG_ENDIAN);
+ }
+
+ public static void testPutLongint(ByteBuffer buf) {
+ if (buf.isReadOnly()) {
+ try {
+ buf.putLong(0, (long) 1);
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (ReadOnlyBufferException e) {
+ // expected
+ }
+ return;
+ }
+
+ int nbytes = 8;
+ byte bytes[] = new byte[nbytes];
+ long value = 0;
+ buf.clear();
+ for (int i = 0; i <= buf.limit() - nbytes; i++) {
+ buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN
+ : ByteOrder.LITTLE_ENDIAN);
+ value = (long) i;
+ buf.position(i);
+ buf.putLong(i, value);
+ assertEquals(i, buf.position());
+ buf.get(bytes);
+ Arrays.equals(long2bytes(value, buf.order()), bytes);
+ }
+
+ try {
+ buf.putLong(-1, value);
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (IndexOutOfBoundsException e) {
+ // expected
+ }
+ try {
+ buf.putLong(buf.limit() - nbytes + 1, value);
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (IndexOutOfBoundsException e) {
+ // expected
+ }
+
+ buf.order(ByteOrder.BIG_ENDIAN);
+ }
+
+ public static void testGetShort(ByteBuffer buf) {
+ int nbytes = 2;
+ byte bytes[] = new byte[nbytes];
+ short value;
+ buf.clear();
+ for (int i = 0; buf.remaining() >= nbytes; i++) {
+ buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN
+ : ByteOrder.LITTLE_ENDIAN);
+ assertEquals(i * nbytes, buf.position());
+ buf.mark();
+ buf.get(bytes);
+ buf.reset();
+ value = buf.getShort();
+ assertEquals(bytes2short(bytes, buf.order()), value);
+ }
+
+ try {
+ buf.getShort();
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (BufferUnderflowException e) {
+ // expected
+ }
+
+ buf.order(ByteOrder.BIG_ENDIAN);
+ }
+
+ public static void testGetShortint(ByteBuffer buf) {
+ int nbytes = 2;
+ byte bytes[] = new byte[nbytes];
+ short value;
+ buf.clear();
+ for (int i = 0; i <= buf.limit() - nbytes; i++) {
+ buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN
+ : ByteOrder.LITTLE_ENDIAN);
+ buf.position(i);
+ value = buf.getShort(i);
+ assertEquals(i, buf.position());
+ buf.get(bytes);
+ assertEquals(bytes2short(bytes, buf.order()), value);
+ }
+
+ try {
+ buf.getShort(-1);
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (IndexOutOfBoundsException e) {
+ // expected
+ }
+ try {
+ buf.getShort(buf.limit() - nbytes + 1);
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (IndexOutOfBoundsException e) {
+ // expected
+ }
+
+ buf.order(ByteOrder.BIG_ENDIAN);
+ }
+
+ public static void testPutShort(ByteBuffer buf) {
+ if (buf.isReadOnly()) {
+ try {
+ buf.clear();
+ buf.putShort((short) 1);
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (ReadOnlyBufferException e) {
+ // expected
+ }
+ return;
+ }
+
+ int nbytes = 2;
+ byte bytes[] = new byte[nbytes];
+ short value = 0;
+ buf.clear();
+ for (int i = 0; buf.remaining() >= nbytes; i++) {
+ buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN
+ : ByteOrder.LITTLE_ENDIAN);
+ value = (short) i;
+ buf.mark();
+ buf.putShort(value);
+ assertEquals((i + 1) * nbytes, buf.position());
+ buf.reset();
+ buf.get(bytes);
+ Arrays.equals(short2bytes(value, buf.order()), bytes);
+ }
+
+ try {
+ buf.putShort(value);
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (BufferOverflowException e) {
+ // expected
+ }
+
+ buf.order(ByteOrder.BIG_ENDIAN);
+ }
+
+ public static void testPutShortint(ByteBuffer buf) {
+ if (buf.isReadOnly()) {
+ try {
+ buf.putShort(0, (short) 1);
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (ReadOnlyBufferException e) {
+ // expected
+ }
+ return;
+ }
+
+ int nbytes = 2;
+ byte bytes[] = new byte[nbytes];
+ short value = 0;
+ buf.clear();
+ for (int i = 0; i <= buf.limit() - nbytes; i++) {
+ buf.order(i % 2 == 0 ? ByteOrder.BIG_ENDIAN
+ : ByteOrder.LITTLE_ENDIAN);
+ value = (short) i;
+ buf.position(i);
+ buf.putShort(i, value);
+ assertEquals(i, buf.position());
+ buf.get(bytes);
+ Arrays.equals(short2bytes(value, buf.order()), bytes);
+ }
+
+ try {
+ buf.putShort(-1, value);
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (IndexOutOfBoundsException e) {
+ // expected
+ }
+ try {
+ buf.putShort(buf.limit() - nbytes + 1, value);
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (IndexOutOfBoundsException e) {
+ // expected
+ }
+
+ buf.order(ByteOrder.BIG_ENDIAN);
+ }
+
+ // private static void println(byte array[]) {
+ // for (int i = 0; i < array.length; i++) {
+ // System.out.print(array[i]);
+ // System.out.print(" ");
+ // }
+ // System.out.println();
+ // }
+ //
+ private static void loadTestData1(byte array[], int offset, int length) {
+ for (int i = 0; i < length; i++) {
+ array[offset + i] = (byte) i;
+ }
+ }
+
+ private static void loadTestData2(byte array[], int offset, int length) {
+ for (int i = 0; i < length; i++) {
+ array[offset + i] = (byte) (length - i);
+ }
+ }
+
+ private static void loadTestData1(ByteBuffer buf) {
+ buf.clear();
+ for (int i = 0; i < buf.capacity(); i++) {
+ buf.put(i, (byte) i);
+ }
+ }
+
+ private static void loadTestData2(ByteBuffer buf) {
+ buf.clear();
+ for (int i = 0; i < buf.capacity(); i++) {
+ buf.put(i, (byte) (buf.capacity() - i));
+ }
+ }
+
+ private static void assertContentEquals(ByteBuffer buf, byte array[],
+ int offset, int length) {
+ for (int i = 0; i < length; i++) {
+ assertEquals(buf.get(i), array[offset + i]);
+ }
+ }
+
+ private static void assertContentEquals(ByteBuffer buf, ByteBuffer other) {
+ assertEquals(buf.capacity(), other.capacity());
+ for (int i = 0; i < buf.capacity(); i++) {
+ assertEquals(buf.get(i), other.get(i));
+ }
+ }
+
+ private static void assertContentLikeTestData1(ByteBuffer buf,
+ int startIndex, byte startValue, int length) {
+ byte value = startValue;
+ for (int i = 0; i < length; i++) {
+ assertEquals(buf.get(startIndex + i), value);
+ value = (byte) (value + 1);
+ }
+ }
+
+ private static void assertContentLikeTestData1(byte array[],
+ int startIndex, byte startValue, int length) {
+ byte value = startValue;
+ for (int i = 0; i < length; i++) {
+ assertEquals(array[startIndex + i], value);
+ value = (byte) (value + 1);
+ }
+ }
+
+ private static int bytes2int(byte bytes[], ByteOrder order) {
+ int nbytes = 4, bigHead, step;
+ if (order == ByteOrder.BIG_ENDIAN) {
+ bigHead = 0;
+ step = 1;
+ } else {
+ bigHead = nbytes - 1;
+ step = -1;
+ }
+ int result = 0;
+ int p = bigHead;
+ for (int i = 0; i < nbytes; i++) {
+ result = result << 8;
+ result = result | (bytes[p] & 0xff);
+ p += step;
+ }
+ return result;
+ }
+
+ private static long bytes2long(byte bytes[], ByteOrder order) {
+ int nbytes = 8, bigHead, step;
+ if (order == ByteOrder.BIG_ENDIAN) {
+ bigHead = 0;
+ step = 1;
+ } else {
+ bigHead = nbytes - 1;
+ step = -1;
+ }
+ long result = 0;
+ int p = bigHead;
+ for (int i = 0; i < nbytes; i++) {
+ result = result << 8;
+ result = result | (bytes[p] & 0xff);
+ p += step;
+ }
+ return result;
+ }
+
+ private static short bytes2short(byte bytes[], ByteOrder order) {
+ int nbytes = 2, bigHead, step;
+ if (order == ByteOrder.BIG_ENDIAN) {
+ bigHead = 0;
+ step = 1;
+ } else {
+ bigHead = nbytes - 1;
+ step = -1;
+ }
+ short result = 0;
+ int p = bigHead;
+ for (int i = 0; i < nbytes; i++) {
+ result = (short) (result << 8);
+ result = (short) (result | (bytes[p] & 0xff));
+ p += step;
+ }
+ return result;
+ }
+
+ private static char bytes2char(byte bytes[], ByteOrder order) {
+ return (char) bytes2short(bytes, order);
+ }
+
+ private static float bytes2float(byte bytes[], ByteOrder order) {
+ return Float.intBitsToFloat(bytes2int(bytes, order));
+ }
+
+ private static double bytes2double(byte bytes[], ByteOrder order) {
+ return Double.longBitsToDouble(bytes2long(bytes, order));
+ }
+
+ private static byte[] int2bytes(int value, ByteOrder order) {
+ int nbytes = 4, smallHead, step;
+ if (order == ByteOrder.BIG_ENDIAN) {
+ smallHead = nbytes - 1;
+ step = -1;
+ } else {
+ smallHead = 0;
+ step = 0;
+ }
+ byte bytes[] = new byte[nbytes];
+ int p = smallHead;
+ for (int i = 0; i < nbytes; i++) {
+ bytes[p] = (byte) (value & 0xff);
+ value = value >> 8;
+ p += step;
+ }
+ return bytes;
+ }
+
+ private static byte[] long2bytes(long value, ByteOrder order) {
+ int nbytes = 8, smallHead, step;
+ if (order == ByteOrder.BIG_ENDIAN) {
+ smallHead = nbytes - 1;
+ step = -1;
+ } else {
+ smallHead = 0;
+ step = 0;
+ }
+ byte bytes[] = new byte[nbytes];
+ int p = smallHead;
+ for (int i = 0; i < nbytes; i++) {
+ bytes[p] = (byte) (value & 0xff);
+ value = value >> 8;
+ p += step;
+ }
+ return bytes;
+ }
+
+ private static byte[] short2bytes(short value, ByteOrder order) {
+ int nbytes = 2, smallHead, step;
+ if (order == ByteOrder.BIG_ENDIAN) {
+ smallHead = nbytes - 1;
+ step = -1;
+ } else {
+ smallHead = 0;
+ step = 0;
+ }
+ byte bytes[] = new byte[nbytes];
+ int p = smallHead;
+ for (int i = 0; i < nbytes; i++) {
+ bytes[p] = (byte) (value & 0xff);
+ value = (short) (value >> 8);
+ p += step;
+ }
+ return bytes;
+ }
+
+ private static byte[] char2bytes(char value, ByteOrder order) {
+ return short2bytes((short) value, order);
+ }
+
+ private static byte[] float2bytes(float value, ByteOrder order) {
+ return int2bytes(Float.floatToRawIntBits(value), order);
+ }
+
+ private static byte[] double2bytes(double value, ByteOrder order) {
+ return long2bytes(Double.doubleToRawLongBits(value), order);
+ }
+
+ public void testAllocatedByteBuffer() {
+ try {
+ ByteBuffer.allocate(-1);
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (IllegalArgumentException e) {
+ // expected
+ }
+ ByteBuffer buf = ByteBuffer.allocate(250);
+ testByteBufferInstanceThoroughly(buf);
+ }
+
+ public void testAllocatedDirectByteBuffer() {
+ try {
+ ByteBuffer.allocateDirect(-1);
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (IllegalArgumentException e) {
+ // expected
+ }
+ ByteBuffer buf = ByteBuffer.allocateDirect(250);
+ testByteBufferInstanceThoroughly(buf);
+ }
+
+ public void testWrappedByteBuffer() {
+ ByteBuffer buf = ByteBuffer.wrap(new byte[250]);
+ testByteBufferInstanceThoroughly(buf);
+ }
+
+ public void testWrappedByteBuffer2() {
+ byte array[] = new byte[250];
+ try {
+ ByteBuffer.wrap(array, -1, 0);
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (IndexOutOfBoundsException e) {
+ // expected
+ }
+ try {
+ ByteBuffer.wrap(array, 251, 0);
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (IndexOutOfBoundsException e) {
+ // expected
+ }
+ try {
+ ByteBuffer.wrap(array, 0, -1);
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (IndexOutOfBoundsException e) {
+ // expected
+ }
+ try {
+ ByteBuffer.wrap(array, 0, 251);
+ fail("Should throw Exception"); //$NON-NLS-1$
+ } catch (IndexOutOfBoundsException e) {
+ // expected
+ }
+
+ ByteBuffer buf = ByteBuffer.wrap(array, 2, 240);
+ assertEquals(buf.position(), 2);
+ assertEquals(buf.limit(), 242);
+ assertEquals(buf.capacity(), 250);
+ testByteBufferInstanceThoroughly(buf);
+ }
+
+ private void testByteBufferInstanceThoroughly(ByteBuffer buf) {
+ assertTrue(buf.capacity() > 240);
+ buf.clear();
+ loadTestData1(buf);
+
+ buf.limit(230).position(1);
+ testByteBufferInstance(buf);
+ testByteBufferInstance(buf.duplicate());
+ testByteBufferInstance(buf.asReadOnlyBuffer());
+ buf.limit(230).position(1);
+ testByteBufferInstance(buf.slice());
+
+ ByteBuffer duplicate = buf.duplicate();
+ duplicate.limit(230).position(1);
+ testByteBufferInstance(duplicate.duplicate());
+ testByteBufferInstance(duplicate.asReadOnlyBuffer());
+ duplicate.limit(230).position(1);
+ testByteBufferInstance(duplicate.slice());
+
+ ByteBuffer readonly = buf.asReadOnlyBuffer();
+ readonly.limit(230).position(1);
+ testByteBufferInstance(readonly.duplicate());
+ testByteBufferInstance(readonly.asReadOnlyBuffer());
+ readonly.limit(230).position(1);
+ testByteBufferInstance(readonly.slice());
+
+ buf.limit(230).position(1);
+ ByteBuffer slice = buf.slice();
+ slice.limit(200).position(1);
+ testByteBufferInstance(slice.duplicate());
+ testByteBufferInstance(slice.asReadOnlyBuffer());
+ slice.limit(200).position(1);
+ testByteBufferInstance(slice.slice());
+ }
+
+}
Added: incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/org/apache/harmony/tests/java/nio/ByteOrderTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/org/apache/harmony/tests/java/nio/ByteOrderTest.java?rev=370544&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/org/apache/harmony/tests/java/nio/ByteOrderTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/org/apache/harmony/tests/java/nio/ByteOrderTest.java Thu Jan 19 08:45:02 2006
@@ -0,0 +1,42 @@
+/* Copyright 2005, 2006 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed 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
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+
+package org.apache.harmony.tests.java.nio;
+
+import java.nio.ByteOrder;
+
+import junit.framework.TestCase;
+
+/**
+ * Test java.nio.ByteOrder
+ *
+ */
+public class ByteOrderTest extends TestCase {
+
+ public static void main(String[] args) {
+ junit.textui.TestRunner.run(ByteOrderTest.class);
+ }
+
+ public void testToString() {
+ assertEquals(ByteOrder.BIG_ENDIAN.toString(), "BIG_ENDIAN");
+ assertEquals(ByteOrder.LITTLE_ENDIAN.toString(), "LITTLE_ENDIAN");
+ }
+
+ public void testNativeOrder() {
+ ByteOrder o = ByteOrder.nativeOrder();
+ assertTrue(o == ByteOrder.BIG_ENDIAN || o == ByteOrder.LITTLE_ENDIAN);
+ }
+
+}