0N/A/*
2362N/A * Copyright (c) 2000, 2008, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/* Type-specific source code for unit test
0N/A *
0N/A * Regenerate the BasicX classes via genBasic.sh whenever this file changes.
0N/A * We check in the generated source files so that the test tree can be used
0N/A * independently of the rest of the source tree.
0N/A */
0N/A
0N/A#warn This file is preprocessed before being compiled
0N/A
0N/Aimport java.nio.*;
721N/Aimport java.lang.reflect.Method;
0N/A
0N/A
0N/Apublic class Basic$Type$
0N/A extends Basic
0N/A{
0N/A
2005N/A private static final $type$[] VALUES = {
2005N/A $Fulltype$.MIN_VALUE,
2005N/A ($type$) -1,
2005N/A ($type$) 0,
2005N/A ($type$) 1,
2005N/A $Fulltype$.MAX_VALUE,
2005N/A#if[float]
2005N/A $Fulltype$.NEGATIVE_INFINITY,
2005N/A $Fulltype$.POSITIVE_INFINITY,
2005N/A $Fulltype$.NaN,
2005N/A ($type$) -0.0,
2005N/A#end[float]
2005N/A#if[double]
2005N/A $Fulltype$.NEGATIVE_INFINITY,
2005N/A $Fulltype$.POSITIVE_INFINITY,
2005N/A $Fulltype$.NaN,
2005N/A ($type$) -0.0,
2005N/A#end[double]
2005N/A };
2005N/A
0N/A private static void relGet($Type$Buffer b) {
0N/A int n = b.capacity();
0N/A $type$ v;
0N/A for (int i = 0; i < n; i++)
0N/A ck(b, (long)b.get(), (long)(($type$)ic(i)));
0N/A b.rewind();
0N/A }
0N/A
0N/A private static void relGet($Type$Buffer b, int start) {
0N/A int n = b.remaining();
0N/A $type$ v;
0N/A for (int i = start; i < n; i++)
0N/A ck(b, (long)b.get(), (long)(($type$)ic(i)));
0N/A b.rewind();
0N/A }
0N/A
0N/A private static void absGet($Type$Buffer b) {
0N/A int n = b.capacity();
0N/A $type$ v;
0N/A for (int i = 0; i < n; i++)
0N/A ck(b, (long)b.get(), (long)(($type$)ic(i)));
0N/A b.rewind();
0N/A }
0N/A
0N/A private static void bulkGet($Type$Buffer b) {
0N/A int n = b.capacity();
0N/A $type$[] a = new $type$[n + 7];
0N/A b.get(a, 7, n);
0N/A for (int i = 0; i < n; i++)
0N/A ck(b, (long)a[i + 7], (long)(($type$)ic(i)));
0N/A }
0N/A
0N/A private static void relPut($Type$Buffer b) {
0N/A int n = b.capacity();
0N/A b.clear();
0N/A for (int i = 0; i < n; i++)
0N/A b.put(($type$)ic(i));
0N/A b.flip();
0N/A }
0N/A
0N/A private static void absPut($Type$Buffer b) {
0N/A int n = b.capacity();
0N/A b.clear();
0N/A for (int i = 0; i < n; i++)
0N/A b.put(i, ($type$)ic(i));
0N/A b.limit(n);
0N/A b.position(0);
0N/A }
0N/A
0N/A private static void bulkPutArray($Type$Buffer b) {
0N/A int n = b.capacity();
0N/A b.clear();
0N/A $type$[] a = new $type$[n + 7];
0N/A for (int i = 0; i < n; i++)
0N/A a[i + 7] = ($type$)ic(i);
0N/A b.put(a, 7, n);
0N/A b.flip();
0N/A }
0N/A
0N/A private static void bulkPutBuffer($Type$Buffer b) {
0N/A int n = b.capacity();
0N/A b.clear();
0N/A $Type$Buffer c = $Type$Buffer.allocate(n + 7);
0N/A c.position(7);
0N/A for (int i = 0; i < n; i++)
0N/A c.put(($type$)ic(i));
0N/A c.flip();
0N/A c.position(7);
0N/A b.put(c);
0N/A b.flip();
0N/A }
0N/A
0N/A //6231529
0N/A private static void callReset($Type$Buffer b) {
0N/A b.position(0);
0N/A b.mark();
0N/A
0N/A b.duplicate().reset();
0N/A b.asReadOnlyBuffer().reset();
0N/A }
0N/A
0N/A#if[byte]
0N/A#else[byte]
0N/A // 6221101-6234263
0N/A
0N/A private static void putBuffer() {
0N/A final int cap = 10;
0N/A
0N/A $Type$Buffer direct1 = ByteBuffer.allocateDirect(cap).as$Type$Buffer();
0N/A $Type$Buffer nondirect1 = ByteBuffer.allocate(cap).as$Type$Buffer();
0N/A direct1.put(nondirect1);
0N/A
0N/A $Type$Buffer direct2 = ByteBuffer.allocateDirect(cap).as$Type$Buffer();
0N/A $Type$Buffer nondirect2 = ByteBuffer.allocate(cap).as$Type$Buffer();
0N/A nondirect2.put(direct2);
0N/A
0N/A $Type$Buffer direct3 = ByteBuffer.allocateDirect(cap).as$Type$Buffer();
0N/A $Type$Buffer direct4 = ByteBuffer.allocateDirect(cap).as$Type$Buffer();
0N/A direct3.put(direct4);
0N/A
0N/A $Type$Buffer nondirect3 = ByteBuffer.allocate(cap).as$Type$Buffer();
0N/A $Type$Buffer nondirect4 = ByteBuffer.allocate(cap).as$Type$Buffer();
0N/A nondirect3.put(nondirect4);
0N/A }
0N/A#end[byte]
0N/A
0N/A#if[char]
0N/A
0N/A private static void bulkPutString($Type$Buffer b) {
0N/A int n = b.capacity();
0N/A b.clear();
0N/A StringBuffer sb = new StringBuffer(n + 7);
0N/A sb.append("1234567");
0N/A for (int i = 0; i < n; i++)
0N/A sb.append((char)ic(i));
0N/A b.put(sb.toString(), 7, 7 + n);
0N/A b.flip();
0N/A }
0N/A
0N/A#end[char]
0N/A
0N/A private static void checkSlice($Type$Buffer b, $Type$Buffer slice) {
0N/A ck(slice, 0, slice.position());
0N/A ck(slice, b.remaining(), slice.limit());
0N/A ck(slice, b.remaining(), slice.capacity());
0N/A if (b.isDirect() != slice.isDirect())
0N/A fail("Lost direction", slice);
0N/A if (b.isReadOnly() != slice.isReadOnly())
0N/A fail("Lost read-only", slice);
0N/A }
0N/A
0N/A#if[byte]
0N/A
0N/A private static void checkBytes(ByteBuffer b, byte[] bs) {
0N/A int n = bs.length;
0N/A int p = b.position();
0N/A byte v;
0N/A if (b.order() == ByteOrder.BIG_ENDIAN) {
0N/A for (int i = 0; i < n; i++)
0N/A ck(b, b.get(), bs[i]);
0N/A } else {
0N/A for (int i = n - 1; i >= 0; i--)
0N/A ck(b, b.get(), bs[i]);
0N/A }
0N/A b.position(p);
0N/A }
0N/A
721N/A private static void compact(Buffer b) {
721N/A try {
721N/A Class<?> cl = b.getClass();
721N/A Method m = cl.getDeclaredMethod("compact");
721N/A m.setAccessible(true);
721N/A m.invoke(b);
721N/A } catch (Exception e) {
721N/A fail(e.getMessage(), b);
721N/A }
721N/A }
721N/A
721N/A private static void checkInvalidMarkException(final Buffer b) {
721N/A tryCatch(b, InvalidMarkException.class, new Runnable() {
721N/A public void run() {
721N/A b.mark();
721N/A compact(b);
721N/A b.reset();
721N/A }});
721N/A }
721N/A
0N/A private static void testViews(int level, ByteBuffer b, boolean direct) {
0N/A
0N/A ShortBuffer sb = b.asShortBuffer();
0N/A BasicShort.test(level, sb, direct);
0N/A checkBytes(b, new byte[] { 0, (byte)ic(0) });
721N/A checkInvalidMarkException(sb);
0N/A
0N/A CharBuffer cb = b.asCharBuffer();
0N/A BasicChar.test(level, cb, direct);
0N/A checkBytes(b, new byte[] { 0, (byte)ic(0) });
721N/A checkInvalidMarkException(cb);
0N/A
0N/A IntBuffer ib = b.asIntBuffer();
0N/A BasicInt.test(level, ib, direct);
0N/A checkBytes(b, new byte[] { 0, 0, 0, (byte)ic(0) });
721N/A checkInvalidMarkException(ib);
0N/A
0N/A LongBuffer lb = b.asLongBuffer();
0N/A BasicLong.test(level, lb, direct);
0N/A checkBytes(b, new byte[] { 0, 0, 0, 0, 0, 0, 0, (byte)ic(0) });
721N/A checkInvalidMarkException(lb);
0N/A
0N/A FloatBuffer fb = b.asFloatBuffer();
0N/A BasicFloat.test(level, fb, direct);
0N/A checkBytes(b, new byte[] { 0x42, (byte)0xc2, 0, 0 });
721N/A checkInvalidMarkException(fb);
0N/A
0N/A DoubleBuffer db = b.asDoubleBuffer();
0N/A BasicDouble.test(level, db, direct);
0N/A checkBytes(b, new byte[] { 0x40, 0x58, 0x40, 0, 0, 0, 0, 0 });
721N/A checkInvalidMarkException(db);
0N/A }
0N/A
0N/A private static void testHet(int level, ByteBuffer b) {
0N/A
0N/A int p = b.position();
0N/A b.limit(b.capacity());
0N/A show(level, b);
0N/A out.print(" put:");
0N/A
0N/A b.putChar((char)1);
0N/A b.putChar((char)Character.MAX_VALUE);
0N/A out.print(" char");
0N/A
0N/A b.putShort((short)1);
0N/A b.putShort((short)Short.MAX_VALUE);
0N/A out.print(" short");
0N/A
0N/A b.putInt(1);
0N/A b.putInt(Integer.MAX_VALUE);
0N/A out.print(" int");
0N/A
0N/A b.putLong((long)1);
0N/A b.putLong((long)Long.MAX_VALUE);
0N/A out.print(" long");
0N/A
0N/A b.putFloat((float)1);
0N/A b.putFloat((float)Float.MIN_VALUE);
0N/A b.putFloat((float)Float.MAX_VALUE);
0N/A out.print(" float");
0N/A
0N/A b.putDouble((double)1);
0N/A b.putDouble((double)Double.MIN_VALUE);
0N/A b.putDouble((double)Double.MAX_VALUE);
0N/A out.print(" double");
0N/A
0N/A out.println();
0N/A b.limit(b.position());
0N/A b.position(p);
0N/A show(level, b);
0N/A out.print(" get:");
0N/A
0N/A ck(b, b.getChar(), 1);
0N/A ck(b, b.getChar(), Character.MAX_VALUE);
0N/A out.print(" char");
0N/A
0N/A ck(b, b.getShort(), 1);
0N/A ck(b, b.getShort(), Short.MAX_VALUE);
0N/A out.print(" short");
0N/A
0N/A ck(b, b.getInt(), 1);
0N/A ck(b, b.getInt(), Integer.MAX_VALUE);
0N/A out.print(" int");
0N/A
0N/A ck(b, b.getLong(), 1);
0N/A ck(b, b.getLong(), Long.MAX_VALUE);
0N/A out.print(" long");
0N/A
0N/A ck(b, (long)b.getFloat(), 1);
0N/A ck(b, (long)b.getFloat(), (long)Float.MIN_VALUE);
0N/A ck(b, (long)b.getFloat(), (long)Float.MAX_VALUE);
0N/A out.print(" float");
0N/A
0N/A ck(b, (long)b.getDouble(), 1);
0N/A ck(b, (long)b.getDouble(), (long)Double.MIN_VALUE);
0N/A ck(b, (long)b.getDouble(), (long)Double.MAX_VALUE);
0N/A out.print(" double");
0N/A
0N/A out.println();
0N/A
0N/A }
0N/A
0N/A#end[byte]
0N/A
2005N/A private static void fail(String problem,
2005N/A $Type$Buffer xb, $Type$Buffer yb,
2005N/A $type$ x, $type$ y) {
2005N/A fail(problem + String.format(": x=%s y=%s", x, y), xb, yb);
2005N/A }
2005N/A
0N/A private static void tryCatch(Buffer b, Class ex, Runnable thunk) {
0N/A boolean caught = false;
0N/A try {
0N/A thunk.run();
0N/A } catch (Throwable x) {
721N/A if (ex.isAssignableFrom(x.getClass())) {
0N/A caught = true;
721N/A } else {
721N/A fail(x.getMessage() + " not expected");
721N/A }
0N/A }
0N/A if (!caught)
0N/A fail(ex.getName() + " not thrown", b);
0N/A }
0N/A
0N/A private static void tryCatch($type$ [] t, Class ex, Runnable thunk) {
0N/A tryCatch($Type$Buffer.wrap(t), ex, thunk);
0N/A }
0N/A
0N/A public static void test(int level, final $Type$Buffer b, boolean direct) {
0N/A
0N/A show(level, b);
0N/A
0N/A if (direct != b.isDirect())
0N/A fail("Wrong direction", b);
0N/A
0N/A // Gets and puts
0N/A
0N/A relPut(b);
0N/A relGet(b);
0N/A absGet(b);
0N/A bulkGet(b);
0N/A
0N/A absPut(b);
0N/A relGet(b);
0N/A absGet(b);
0N/A bulkGet(b);
0N/A
0N/A bulkPutArray(b);
0N/A relGet(b);
0N/A
0N/A bulkPutBuffer(b);
0N/A relGet(b);
0N/A
0N/A#if[char]
0N/A
0N/A bulkPutString(b);
0N/A relGet(b);
0N/A b.position(1);
0N/A b.limit(7);
0N/A ck(b, b.toString().equals("bcdefg"));
0N/A
0N/A // CharSequence ops
0N/A
0N/A b.position(2);
0N/A ck(b, b.charAt(1), 'd');
1108N/A CharBuffer c = b.subSequence(1, 4);
1108N/A ck(c, c.capacity(), b.capacity());
1108N/A ck(c, c.position(), b.position()+1);
1108N/A ck(c, c.limit(), b.position()+4);
1108N/A ck(c, b.subSequence(1, 4).toString().equals("def"));
0N/A
0N/A // 4938424
0N/A b.position(4);
0N/A ck(b, b.charAt(1), 'f');
0N/A ck(b, b.subSequence(1, 3).toString().equals("fg"));
0N/A
0N/A#end[char]
0N/A
0N/A // Compact
0N/A
0N/A relPut(b);
0N/A b.position(13);
0N/A b.compact();
0N/A b.flip();
0N/A relGet(b, 13);
0N/A
0N/A // Exceptions
0N/A
0N/A relPut(b);
0N/A b.limit(b.capacity() / 2);
0N/A b.position(b.limit());
0N/A
0N/A tryCatch(b, BufferUnderflowException.class, new Runnable() {
0N/A public void run() {
0N/A b.get();
0N/A }});
0N/A
0N/A tryCatch(b, BufferOverflowException.class, new Runnable() {
0N/A public void run() {
0N/A b.put(($type$)42);
0N/A }});
0N/A
0N/A // The index must be non-negative and lesss than the buffer's limit.
0N/A tryCatch(b, IndexOutOfBoundsException.class, new Runnable() {
0N/A public void run() {
0N/A b.get(b.limit());
0N/A }});
0N/A tryCatch(b, IndexOutOfBoundsException.class, new Runnable() {
0N/A public void run() {
0N/A b.get(-1);
0N/A }});
0N/A
0N/A tryCatch(b, IndexOutOfBoundsException.class, new Runnable() {
0N/A public void run() {
0N/A b.put(b.limit(), ($type$)42);
0N/A }});
0N/A
721N/A tryCatch(b, InvalidMarkException.class, new Runnable() {
721N/A public void run() {
721N/A b.position(0);
721N/A b.mark();
721N/A b.compact();
721N/A b.reset();
721N/A }});
721N/A
0N/A // Values
0N/A
0N/A b.clear();
0N/A b.put(($type$)0);
0N/A b.put(($type$)-1);
0N/A b.put(($type$)1);
0N/A b.put($Fulltype$.MAX_VALUE);
0N/A b.put($Fulltype$.MIN_VALUE);
0N/A#if[float]
0N/A b.put(-Float.MAX_VALUE);
0N/A b.put(-Float.MIN_VALUE);
0N/A b.put(Float.NEGATIVE_INFINITY);
0N/A b.put(Float.POSITIVE_INFINITY);
0N/A b.put(Float.NaN);
0N/A b.put(0.91697687f); // Changes value if incorrectly swapped
0N/A#end[float]
0N/A#if[double]
0N/A b.put(-Double.MAX_VALUE);
0N/A b.put(-Double.MIN_VALUE);
0N/A b.put(Double.NEGATIVE_INFINITY);
0N/A b.put(Double.POSITIVE_INFINITY);
0N/A b.put(Double.NaN);
0N/A b.put(0.5121609353879392); // Changes value if incorrectly swapped
0N/A#end[double]
0N/A
0N/A $type$ v;
0N/A b.flip();
0N/A ck(b, b.get(), 0);
0N/A ck(b, b.get(), ($type$)-1);
0N/A ck(b, b.get(), 1);
0N/A ck(b, b.get(), $Fulltype$.MAX_VALUE);
0N/A ck(b, b.get(), $Fulltype$.MIN_VALUE);
0N/A
0N/A#if[float]
0N/A ck(b, b.get(), -Float.MAX_VALUE);
0N/A ck(b, b.get(), -Float.MIN_VALUE);
0N/A ck(b, b.get(), Float.NEGATIVE_INFINITY);
0N/A ck(b, b.get(), Float.POSITIVE_INFINITY);
0N/A if (Float.floatToRawIntBits(v = b.get()) != Float.floatToRawIntBits(Float.NaN))
0N/A fail(b, (long)Float.NaN, (long)v);
0N/A ck(b, b.get(), 0.91697687f);
0N/A#end[float]
0N/A#if[double]
0N/A ck(b, b.get(), -Double.MAX_VALUE);
0N/A ck(b, b.get(), -Double.MIN_VALUE);
0N/A ck(b, b.get(), Double.NEGATIVE_INFINITY);
0N/A ck(b, b.get(), Double.POSITIVE_INFINITY);
0N/A if (Double.doubleToRawLongBits(v = b.get())
0N/A != Double.doubleToRawLongBits(Double.NaN))
0N/A fail(b, (long)Double.NaN, (long)v);
0N/A ck(b, b.get(), 0.5121609353879392);
0N/A#end[double]
0N/A
0N/A
0N/A // Comparison
0N/A b.rewind();
0N/A $Type$Buffer b2 = $Type$Buffer.allocate(b.capacity());
0N/A b2.put(b);
0N/A b2.flip();
0N/A b.position(2);
0N/A b2.position(2);
0N/A if (!b.equals(b2)) {
0N/A for (int i = 2; i < b.limit(); i++) {
0N/A $type$ x = b.get(i);
0N/A $type$ y = b2.get(i);
0N/A if (x != y
0N/A#if[double]
0N/A || Double.compare(x, y) != 0
0N/A#end[double]
0N/A#if[float]
0N/A || Float.compare(x, y) != 0
0N/A#end[float]
0N/A )
0N/A out.println("[" + i + "] " + x + " != " + y);
0N/A }
0N/A fail("Identical buffers not equal", b, b2);
0N/A }
0N/A if (b.compareTo(b2) != 0)
0N/A fail("Comparison to identical buffer != 0", b, b2);
0N/A
0N/A b.limit(b.limit() + 1);
0N/A b.position(b.limit() - 1);
0N/A b.put(($type$)99);
0N/A b.rewind();
0N/A b2.rewind();
0N/A if (b.equals(b2))
0N/A fail("Non-identical buffers equal", b, b2);
0N/A if (b.compareTo(b2) <= 0)
0N/A fail("Comparison to shorter buffer <= 0", b, b2);
0N/A b.limit(b.limit() - 1);
0N/A
0N/A b.put(2, ($type$)42);
0N/A if (b.equals(b2))
0N/A fail("Non-identical buffers equal", b, b2);
0N/A if (b.compareTo(b2) <= 0)
0N/A fail("Comparison to lesser buffer <= 0", b, b2);
0N/A
2005N/A // Check equals and compareTo with interesting values
2005N/A for ($type$ x : VALUES) {
2005N/A $Type$Buffer xb = $Type$Buffer.wrap(new $type$[] { x });
2005N/A if (xb.compareTo(xb) != 0) {
2005N/A fail("compareTo not reflexive", xb, xb, x, x);
2005N/A }
2005N/A if (! xb.equals(xb)) {
2005N/A fail("equals not reflexive", xb, xb, x, x);
2005N/A }
2005N/A for ($type$ y : VALUES) {
2005N/A $Type$Buffer yb = $Type$Buffer.wrap(new $type$[] { y });
2005N/A if (xb.compareTo(yb) != - yb.compareTo(xb)) {
2005N/A fail("compareTo not anti-symmetric",
2005N/A xb, yb, x, y);
2005N/A }
2005N/A if ((xb.compareTo(yb) == 0) != xb.equals(yb)) {
2005N/A fail("compareTo inconsistent with equals",
2005N/A xb, yb, x, y);
2005N/A }
2005N/A if (xb.compareTo(yb) != $Fulltype$.compare(x, y)) {
2005N/A#if[float]
2005N/A if (x == 0.0 && y == 0.0) continue;
2005N/A#end[float]
2005N/A#if[double]
2005N/A if (x == 0.0 && y == 0.0) continue;
2005N/A#end[double]
2005N/A fail("Incorrect results for $Type$Buffer.compareTo",
2005N/A xb, yb, x, y);
2005N/A }
2005N/A if (xb.equals(yb) != ((x == y) || ((x != x) && (y != y)))) {
2005N/A fail("Incorrect results for $Type$Buffer.equals",
2005N/A xb, yb, x, y);
2005N/A }
2005N/A }
2005N/A }
2005N/A
0N/A // Sub, dup
0N/A
0N/A relPut(b);
0N/A relGet(b.duplicate());
0N/A b.position(13);
0N/A relGet(b.duplicate(), 13);
0N/A relGet(b.duplicate().slice(), 13);
0N/A relGet(b.slice(), 13);
0N/A relGet(b.slice().duplicate(), 13);
0N/A
0N/A // Slice
0N/A
0N/A b.position(5);
0N/A $Type$Buffer sb = b.slice();
0N/A checkSlice(b, sb);
0N/A b.position(0);
0N/A $Type$Buffer sb2 = sb.slice();
0N/A checkSlice(sb, sb2);
0N/A
0N/A if (!sb.equals(sb2))
0N/A fail("Sliced slices do not match", sb, sb2);
0N/A if ((sb.hasArray()) && (sb.arrayOffset() != sb2.arrayOffset()))
0N/A fail("Array offsets do not match: "
0N/A + sb.arrayOffset() + " != " + sb2.arrayOffset(), sb, sb2);
0N/A
0N/A#if[byte]
0N/A
0N/A // Views
0N/A
0N/A b.clear();
0N/A b.order(ByteOrder.BIG_ENDIAN);
0N/A testViews(level + 1, b, direct);
0N/A
0N/A for (int i = 1; i <= 9; i++) {
0N/A b.position(i);
0N/A show(level + 1, b);
0N/A testViews(level + 2, b, direct);
0N/A }
0N/A
0N/A b.position(0);
0N/A b.order(ByteOrder.LITTLE_ENDIAN);
0N/A testViews(level + 1, b, direct);
0N/A
0N/A // Heterogeneous accessors
0N/A
0N/A b.order(ByteOrder.BIG_ENDIAN);
0N/A for (int i = 0; i <= 9; i++) {
0N/A b.position(i);
0N/A testHet(level + 1, b);
0N/A }
0N/A b.order(ByteOrder.LITTLE_ENDIAN);
0N/A b.position(3);
0N/A testHet(level + 1, b);
0N/A
0N/A#end[byte]
0N/A
0N/A // Read-only views
0N/A
0N/A b.rewind();
0N/A final $Type$Buffer rb = b.asReadOnlyBuffer();
0N/A if (!b.equals(rb))
0N/A fail("Buffer not equal to read-only view", b, rb);
0N/A show(level + 1, rb);
0N/A
0N/A tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
0N/A public void run() {
0N/A relPut(rb);
0N/A }});
0N/A
0N/A tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
0N/A public void run() {
0N/A absPut(rb);
0N/A }});
0N/A
0N/A tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
0N/A public void run() {
0N/A bulkPutArray(rb);
0N/A }});
0N/A
0N/A tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
0N/A public void run() {
0N/A bulkPutBuffer(rb);
0N/A }});
0N/A
0N/A tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
0N/A public void run() {
0N/A rb.compact();
0N/A }});
0N/A
0N/A#if[byte]
0N/A
0N/A tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
0N/A public void run() {
0N/A rb.putChar((char)1);
0N/A }});
0N/A tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
0N/A public void run() {
0N/A rb.putChar(0, (char)1);
0N/A }});
0N/A
0N/A tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
0N/A public void run() {
0N/A rb.putShort((short)1);
0N/A }});
0N/A tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
0N/A public void run() {
0N/A rb.putShort(0, (short)1);
0N/A }});
0N/A
0N/A tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
0N/A public void run() {
0N/A rb.putInt(1);
0N/A }});
0N/A tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
0N/A public void run() {
0N/A rb.putInt(0, 1);
0N/A }});
0N/A
0N/A tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
0N/A public void run() {
0N/A rb.putLong((long)1);
0N/A }});
0N/A tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
0N/A public void run() {
0N/A rb.putLong(0, (long)1);
0N/A }});
0N/A
0N/A tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
0N/A public void run() {
0N/A rb.putFloat((float)1);
0N/A }});
0N/A tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
0N/A public void run() {
0N/A rb.putFloat(0, (float)1);
0N/A }});
0N/A
0N/A tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
0N/A public void run() {
0N/A rb.putDouble((double)1);
0N/A }});
0N/A tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
0N/A public void run() {
0N/A rb.putDouble(0, (double)1);
0N/A }});
0N/A
0N/A#end[byte]
0N/A
0N/A if (rb.getClass().getName().startsWith("java.nio.Heap")) {
0N/A
0N/A tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
0N/A public void run() {
0N/A rb.array();
0N/A }});
0N/A
0N/A tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
0N/A public void run() {
0N/A rb.arrayOffset();
0N/A }});
0N/A
0N/A if (rb.hasArray())
0N/A fail("Read-only heap buffer's backing array is accessible",
0N/A rb);
0N/A
0N/A }
0N/A
0N/A // Bulk puts from read-only buffers
0N/A
0N/A b.clear();
0N/A rb.rewind();
0N/A b.put(rb);
0N/A
0N/A#if[byte]
0N/A // For byte buffers, test both the direct and non-direct cases
0N/A $Type$Buffer ob
0N/A = (b.isDirect()
0N/A ? $Type$Buffer.allocate(rb.capacity())
0N/A : $Type$Buffer.allocateDirect(rb.capacity()));
0N/A rb.rewind();
0N/A ob.put(rb);
0N/A#end[byte]
0N/A
0N/A relPut(b); // Required by testViews
0N/A
0N/A }
0N/A
0N/A#if[char]
0N/A
0N/A private static void testStr() {
0N/A final String s = "abcdefghijklm";
0N/A int start = 3;
0N/A int end = 9;
0N/A final CharBuffer b = CharBuffer.wrap(s, start, end);
0N/A show(0, b);
0N/A ck(b, b.toString().equals(s.substring(start, end)));
0N/A ck(b, b.toString().equals("defghi"));
0N/A ck(b, b.isReadOnly());
0N/A tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
0N/A public void run() {
0N/A b.put('x');
0N/A }});
0N/A ck(b, start, b.position());
0N/A ck(b, end, b.limit());
0N/A ck(b, s.length(), b.capacity());
1108N/A b.position(6);
1108N/A ck(b, b.subSequence(0,3).toString().equals("ghi"));
0N/A
0N/A // The index, relative to the position, must be non-negative and
0N/A // smaller than remaining().
0N/A tryCatch(b, IndexOutOfBoundsException.class, new Runnable() {
0N/A public void run() {
0N/A b.charAt(-1);
0N/A }});
0N/A tryCatch(b, IndexOutOfBoundsException.class, new Runnable() {
0N/A public void run() {
0N/A b.charAt(b.remaining());
0N/A }});
0N/A
0N/A // The index must be non-negative and less than the buffer's limit.
0N/A tryCatch(b, IndexOutOfBoundsException.class, new Runnable() {
0N/A public void run() {
0N/A b.get(b.limit());
0N/A }});
0N/A tryCatch(b, IndexOutOfBoundsException.class, new Runnable() {
0N/A public void run() {
0N/A b.get(-1);
0N/A }});
0N/A
0N/A // The start must be non-negative and no larger than remaining().
0N/A tryCatch(b, IndexOutOfBoundsException.class, new Runnable() {
0N/A public void run() {
0N/A b.subSequence(-1, b.remaining());
0N/A }});
0N/A tryCatch(b, IndexOutOfBoundsException.class, new Runnable() {
0N/A public void run() {
0N/A b.subSequence(b.remaining() + 1, b.remaining());
0N/A }});
0N/A
0N/A // The end must be no smaller than start and no larger than
0N/A // remaining().
0N/A tryCatch(b, IndexOutOfBoundsException.class, new Runnable() {
0N/A public void run() {
0N/A b.subSequence(2, 1);
0N/A }});
0N/A tryCatch(b, IndexOutOfBoundsException.class, new Runnable() {
0N/A public void run() {
0N/A b.subSequence(0, b.remaining() + 1);
0N/A }});
0N/A
0N/A // The offset must be non-negative and no larger than <array.length>.
0N/A tryCatch(b, IndexOutOfBoundsException.class, new Runnable() {
0N/A public void run() {
0N/A $Type$Buffer.wrap(s, -1, s.length());
0N/A }});
0N/A tryCatch(b, IndexOutOfBoundsException.class, new Runnable() {
0N/A public void run() {
0N/A $Type$Buffer.wrap(s, s.length() + 1, s.length());
0N/A }});
0N/A tryCatch(b, IndexOutOfBoundsException.class, new Runnable() {
0N/A public void run() {
0N/A $Type$Buffer.wrap(s, 1, 0);
0N/A }});
0N/A tryCatch(b, IndexOutOfBoundsException.class, new Runnable() {
0N/A public void run() {
0N/A $Type$Buffer.wrap(s, 0, s.length() + 1);
0N/A }});
0N/A }
0N/A
0N/A#end[char]
0N/A
0N/A public static void test(final $type$ [] ba) {
0N/A int offset = 47;
0N/A int length = 900;
0N/A final $Type$Buffer b = $Type$Buffer.wrap(ba, offset, length);
0N/A show(0, b);
0N/A ck(b, b.capacity(), ba.length);
0N/A ck(b, b.position(), offset);
0N/A ck(b, b.limit(), offset + length);
0N/A
0N/A // The offset must be non-negative and no larger than <array.length>.
0N/A tryCatch(ba, IndexOutOfBoundsException.class, new Runnable() {
0N/A public void run() {
0N/A $Type$Buffer.wrap(ba, -1, ba.length);
0N/A }});
0N/A tryCatch(ba, IndexOutOfBoundsException.class, new Runnable() {
0N/A public void run() {
0N/A $Type$Buffer.wrap(ba, ba.length + 1, ba.length);
0N/A }});
0N/A tryCatch(ba, IndexOutOfBoundsException.class, new Runnable() {
0N/A public void run() {
0N/A $Type$Buffer.wrap(ba, 0, -1);
0N/A }});
0N/A tryCatch(ba, IndexOutOfBoundsException.class, new Runnable() {
0N/A public void run() {
0N/A $Type$Buffer.wrap(ba, 0, ba.length + 1);
0N/A }});
0N/A
0N/A // A NullPointerException will be thrown if the array is null.
0N/A tryCatch(ba, NullPointerException.class, new Runnable() {
0N/A public void run() {
0N/A $Type$Buffer.wrap(($type$ []) null, 0, 5);
0N/A }});
0N/A tryCatch(ba, NullPointerException.class, new Runnable() {
0N/A public void run() {
0N/A $Type$Buffer.wrap(($type$ []) null);
0N/A }});
0N/A }
0N/A
0N/A private static void testAllocate() {
0N/A // An IllegalArgumentException will be thrown for negative capacities.
0N/A tryCatch((Buffer) null, IllegalArgumentException.class, new Runnable() {
0N/A public void run() {
0N/A $Type$Buffer.allocate(-1);
0N/A }});
0N/A#if[byte]
0N/A tryCatch((Buffer) null, IllegalArgumentException.class, new Runnable() {
0N/A public void run() {
0N/A $Type$Buffer.allocateDirect(-1);
0N/A }});
0N/A#end[byte]
0N/A }
0N/A
0N/A public static void test() {
0N/A testAllocate();
0N/A test(0, $Type$Buffer.allocate(7 * 1024), false);
0N/A test(0, $Type$Buffer.wrap(new $type$[7 * 1024], 0, 7 * 1024), false);
0N/A test(new $type$[1024]);
0N/A#if[byte]
0N/A $Type$Buffer b = $Type$Buffer.allocateDirect(7 * 1024);
0N/A for (b.position(0); b.position() < b.limit(); )
0N/A ck(b, b.get(), 0);
0N/A test(0, b, true);
0N/A#end[byte]
0N/A#if[char]
0N/A testStr();
0N/A#end[char]
0N/A
0N/A callReset($Type$Buffer.allocate(10));
0N/A
0N/A#if[byte]
0N/A#else[byte]
0N/A putBuffer();
0N/A#end[byte]
0N/A }
0N/A
0N/A}