/* * Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/
/* Type-specific source code for unit test * * Regenerate the BasicX classes via genBasic.sh whenever this file changes. * We check in the generated source files so that the test tree can be used * independently of the rest of the source tree.
*/
// -- This file was mechanically generated: Do not edit! -- //
privatestaticvoid relGet(ByteBuffer b) { int n = b.capacity(); for (int i = 0; i < n; i++)
ck(b, (long)b.get(), (long)((byte)ic(i)));
b.rewind();
}
privatestaticvoid relGet(ByteBuffer b, int start) { int n = b.remaining(); for (int i = start; i < n; i++)
ck(b, (long)b.get(), (long)((byte)ic(i)));
b.rewind();
}
privatestaticvoid absGet(ByteBuffer b) { int n = b.capacity(); for (int i = 0; i < n; i++)
ck(b, (long)b.get(), (long)((byte)ic(i)));
b.rewind();
}
privatestaticvoid bulkGet(ByteBuffer b) { int n = b.capacity(); byte[] a = newbyte[n + 7];
b.get(a, 7, n); for (int i = 0; i < n; i++) {
ck(b, (long)a[i + 7], (long)((byte)ic(i)));
}
}
privatestaticvoid absBulkGet(ByteBuffer b) { int n = b.capacity(); int len = n - 7*2; byte[] a = newbyte[n + 7];
b.position(42);
b.get(7, a, 7, len);
ck(b, b.position() == 42); for (int i = 0; i < len; i++) {
ck(b, (long)a[i + 7], (long)((byte)ic(i)));
}
}
privatestaticvoid relPut(ByteBuffer b) { int n = b.capacity();
b.clear(); for (int i = 0; i < n; i++)
b.put((byte)ic(i));
b.flip();
}
privatestaticvoid absPut(ByteBuffer b) { int n = b.capacity();
b.clear(); for (int i = 0; i < n; i++)
b.put(i, (byte)ic(i));
b.limit(n);
b.position(0);
}
privatestaticvoid bulkPutArray(ByteBuffer b) { int n = b.capacity();
b.clear(); byte[] a = newbyte[n + 7]; for (int i = 0; i < n; i++)
a[i + 7] = (byte)ic(i);
b.put(a, 7, n);
b.flip();
}
privatestaticvoid bulkPutBuffer(ByteBuffer b) { int n = b.capacity();
b.clear();
ByteBuffer c = ByteBuffer.allocate(n + 7);
c.position(7); for (int i = 0; i < n; i++)
c.put((byte)ic(i));
c.flip();
c.position(7);
b.put(c);
b.flip(); try {
b.put(b);
fail("IllegalArgumentException expected for put into same buffer");
} catch (IllegalArgumentException e) { if (e.getMessage() == null) {
fail("Non-null IllegalArgumentException message expected from"
+ " put into same buffer");
}
}
}
privatestaticvoid absBulkPutArray(ByteBuffer b) { int n = b.capacity();
b.clear(); int lim = n - 7; int len = lim - 7;
b.limit(lim); byte[] a = newbyte[len + 7]; for (int i = 0; i < len; i++)
a[i + 7] = (byte)ic(i);
b.position(42);
b.put(7, a, 7, len);
ck(b, b.position() == 42);
}
//6231529 privatestaticvoid callReset(ByteBuffer b) {
b.position(0);
b.mark();
privatestaticvoid checkBytes(ByteBuffer b, byte[] bs) { int n = bs.length; int p = b.position(); if (b.order() == ByteOrder.BIG_ENDIAN) { for (int i = 0; i < n; i++) {
ck(b, b.get(), bs[i]);
}
} else { for (int i = n - 1; i >= 0; i--) {
ck(b, b.get(), bs[i]);
}
}
b.position(p);
}
// unit size values
catchIllegalArgument(b, () -> b.alignmentOffset(0, (short) 0)); for (int us = 1; us < 65; us++) { int _us = us; if ((us & (us - 1)) != 0) { // unit size not a power of two
catchIllegalArgument(b, () -> b.alignmentOffset(0, _us));
} else { if (direct || us <= 8) {
b.alignmentOffset(0, us);
} else { // unit size > 8 with non-direct buffer
tryCatch(b, UnsupportedOperationException.class,
() -> b.alignmentOffset(0, _us));
}
}
}
// Probe for long misalignment at index zero for a newly created buffer
ByteBuffer empty =
direct ? ByteBuffer.allocateDirect(0) : ByteBuffer.allocate(0); int longMisalignmentAtZero = empty.alignmentOffset(0, 8);
if (direct) { // Freshly created direct byte buffers should be aligned at index 0 // for ref and primitive values (see Unsafe.allocateMemory) if (longMisalignmentAtZero != 0) {
fail("Direct byte buffer misaligned at index 0"
+ " for ref and primitive values "
+ longMisalignmentAtZero);
}
} else { // For heap byte buffers misalignment may occur on 32-bit systems // where Unsafe.ARRAY_BYTE_BASE_OFFSET % 8 == 4 and not 0 // Note the GC will preserve alignment of the base address of the // array if (jdk.internal.misc.Unsafe.ARRAY_BYTE_BASE_OFFSET % 8
!= longMisalignmentAtZero) {
fail("Heap byte buffer misaligned at index 0"
+ " for ref and primitive values "
+ longMisalignmentAtZero);
}
}
// Ensure test buffer is correctly aligned at index 0 if (b.alignmentOffset(0, 8) != longMisalignmentAtZero)
fail("Test input buffer not correctly aligned at index 0", b);
// Test misalignment values for (int us : newint[]{1, 2, 4, 8}) { for (int i = 0; i < us * 2; i++) { int am = b.alignmentOffset(i, us); int expectedAm = (longMisalignmentAtZero + i) % us;
if (am != expectedAm) {
String f = "b.alignmentOffset(%d, %d) == %d incorrect, expected %d";
fail(String.format(f, i, us, am, expectedAm));
}
}
}
// Created aligned slice to test against int ap = 8 - longMisalignmentAtZero; int al = b.limit() - b.alignmentOffset(b.limit(), 8);
ByteBuffer ab = b.position(ap).limit(al).
slice(); if (ab.limit() == 0) {
fail("Test input buffer not sufficiently sized to cover" + " an aligned region for all values", b);
} if (ab.alignmentOffset(0, 8) != 0)
fail("Aligned test input buffer not correctly aligned at index 0", ab);
for (int us : newint[]{1, 2, 4, 8}) { for (int p = 1; p < 16; p++) { int l = ab.limit() - p;
ByteBuffer as = ab.slice().position(p).limit(l).
alignedSlice(us);
ck(as, 0, as.position());
ck(as, as.capacity(), as.limit()); if (b.isDirect() != as.isDirect())
fail("Lost direction", as); if (b.isReadOnly() != as.isReadOnly())
fail("Lost read-only", as);
if (as.alignmentOffset(0, us) != 0)
fail("Buffer not correctly aligned at index 0", as);
if (as.alignmentOffset(as.limit(), us) != 0)
fail("Buffer not correctly aligned at limit", as);
int p_mod = ab.alignmentOffset(p, us); int l_mod = ab.alignmentOffset(l, us); // Round up position
p = (p_mod > 0) ? p + (us - p_mod) : p; // Round down limit
l = l - l_mod;
int ec = l - p; if (as.limit() != ec) {
fail("Buffer capacity incorrect, expected: " + ec, as);
}
}
}
tryCatch(b, BufferUnderflowException.class, () -> b.get());
tryCatch(b, BufferOverflowException.class, () -> b.put((byte)42)); // The index must be non-negative and less than the buffer's limit.
catchIndexOutOfBounds(b, () -> b.get(b.limit()));
catchIndexOutOfBounds(b, () -> b.get(-1));
catchIndexOutOfBounds(b, () -> b.put(b.limit(), (byte)42));
tryCatch(b, InvalidMarkException.class,
() -> b.position(0).mark().compact().reset());
try {
b.position(b.limit() + 1);
fail("IllegalArgumentException expected for position beyond limit");
} catch (IllegalArgumentException e) { if (e.getMessage() == null) {
fail("Non-null IllegalArgumentException message expected for"
+ " position beyond limit");
}
}
// Comparison
b.rewind();
ByteBuffer b2 = ByteBuffer.allocate(b.capacity());
b2.put(b);
b2.flip();
b.position(2);
b2.position(2); if (!b.equals(b2)) { for (int i = 2; i < b.limit(); i++) { byte x = b.get(i); byte y = b2.get(i); if (x != y
) {
out.println("[" + i + "] " + x + " != " + y);
}
}
fail("Identical buffers not equal", b, b2);
} if (b.compareTo(b2) != 0) {
fail("Comparison to identical buffer != 0", b, b2);
}
b.limit(b.limit() + 1);
b.position(b.limit() - 1);
b.put((byte)99);
b.rewind();
b2.rewind(); if (b.equals(b2))
fail("Non-identical buffers equal", b, b2); if (b.compareTo(b2) <= 0)
fail("Comparison to shorter buffer <= 0", b, b2);
b.limit(b.limit() - 1);
b.put(2, (byte)42); if (b.equals(b2))
fail("Non-identical buffers equal", b, b2); if (b.compareTo(b2) <= 0)
fail("Comparison to lesser buffer <= 0", b, b2);
// Check equals and compareTo with interesting values for (byte x : VALUES) {
ByteBuffer xb = ByteBuffer.wrap(newbyte[] { x }); if (xb.compareTo(xb) != 0) {
fail("compareTo not reflexive", xb, xb, x, x);
} if (!xb.equals(xb)) {
fail("equals not reflexive", xb, xb, x, x);
} for (byte y : VALUES) {
ByteBuffer yb = ByteBuffer.wrap(newbyte[] { y }); if (xb.compareTo(yb) != - yb.compareTo(xb)) {
fail("compareTo not anti-symmetric",
xb, yb, x, y);
} if ((xb.compareTo(yb) == 0) != xb.equals(yb)) {
fail("compareTo inconsistent with equals",
xb, yb, x, y);
} if (xb.compareTo(yb) != Byte.compare(x, y)) {
// put(ByteBuffer) should not change source position final ByteBuffer src = ByteBuffer.allocate(1);
catchReadOnlyBuffer(b, () -> rb.put(src));
ck(src, src.position(), 0);
if (rb.getClass().getName().startsWith("java.nio.Heap")) {
catchReadOnlyBuffer(b, () -> rb.array());
catchReadOnlyBuffer(b, () -> rb.arrayOffset()); if (rb.hasArray()) {
fail("Read-only heap buffer's backing array is accessible", rb);
}
}
// Bulk puts from read-only buffers
b.clear();
rb.rewind();
b.put(rb);
// For byte buffers, test both the direct and non-direct cases
ByteBuffer ob
= (b.isDirect()
? ByteBuffer.allocate(rb.capacity())
: ByteBuffer.allocateDirect(rb.capacity()));
rb.rewind();
ob.put(rb);
relPut(b); // Required by testViews
// Test alignment
testAlign(b, direct);
}
publicstaticvoid test(finalbyte [] ba) { int offset = 47; int length = 900; final ByteBuffer b = ByteBuffer.wrap(ba, offset, length);
show(0, b);
ck(b, b.capacity(), ba.length);
ck(b, b.position(), offset);
ck(b, b.limit(), offset + length);
// The offset must be non-negative and no larger than <array.length>.
catchIndexOutOfBounds(ba, () -> ByteBuffer.wrap(ba, -1, ba.length));
catchIndexOutOfBounds(ba, () -> ByteBuffer.wrap(ba, ba.length + 1, ba.length));
catchIndexOutOfBounds(ba, () -> ByteBuffer.wrap(ba, 0, -1));
catchIndexOutOfBounds(ba, () -> ByteBuffer.wrap(ba, 0, ba.length + 1));
// A NullPointerException will be thrown if the array is null.
tryCatch(ba, NullPointerException.class,
() -> ByteBuffer.wrap((byte []) null, 0, 5));
tryCatch(ba, NullPointerException.class,
() -> ByteBuffer.wrap((byte []) null));
}
privatestaticvoid testAllocate() { // An IllegalArgumentException will be thrown for negative capacities.
catchIllegalArgument((Buffer) null, () -> ByteBuffer.allocate(-1)); try {
ByteBuffer.allocate(-1);
} catch (IllegalArgumentException e) { if (e.getMessage() == null) {
fail("Non-null IllegalArgumentException message expected for"
+ " attempt to allocate negative capacity buffer");
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.