Path: blob/master/test/jdk/java/nio/Buffer/BasicLong.java
41149 views
/*1* Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* Type-specific source code for unit test24*25* Regenerate the BasicX classes via genBasic.sh whenever this file changes.26* We check in the generated source files so that the test tree can be used27* independently of the rest of the source tree.28*/2930// -- This file was mechanically generated: Do not edit! -- //3132import java.nio.*;333435public class BasicLong36extends Basic37{3839private static final long[] VALUES = {40Long.MIN_VALUE,41(long) -1,42(long) 0,43(long) 1,44Long.MAX_VALUE,45464748495051525354555657};5859private static void relGet(LongBuffer b) {60int n = b.capacity();61for (int i = 0; i < n; i++)62ck(b, (long)b.get(), (long)((long)ic(i)));63b.rewind();64}6566private static void relGet(LongBuffer b, int start) {67int n = b.remaining();68for (int i = start; i < n; i++)69ck(b, (long)b.get(), (long)((long)ic(i)));70b.rewind();71}7273private static void absGet(LongBuffer b) {74int n = b.capacity();75for (int i = 0; i < n; i++)76ck(b, (long)b.get(), (long)((long)ic(i)));77b.rewind();78}7980private static void bulkGet(LongBuffer b) {81int n = b.capacity();82long[] a = new long[n + 7];83b.get(a, 7, n);84for (int i = 0; i < n; i++) {85ck(b, (long)a[i + 7], (long)((long)ic(i)));86}87}8889private static void absBulkGet(LongBuffer b) {90int n = b.capacity();91int len = n - 7*2;92long[] a = new long[n + 7];93b.position(42);94b.get(7, a, 7, len);95ck(b, b.position() == 42);96for (int i = 0; i < len; i++) {97ck(b, (long)a[i + 7], (long)((long)ic(i)));98}99}100101private static void relPut(LongBuffer b) {102int n = b.capacity();103b.clear();104for (int i = 0; i < n; i++)105b.put((long)ic(i));106b.flip();107}108109private static void absPut(LongBuffer b) {110int n = b.capacity();111b.clear();112for (int i = 0; i < n; i++)113b.put(i, (long)ic(i));114b.limit(n);115b.position(0);116}117118private static void bulkPutArray(LongBuffer b) {119int n = b.capacity();120b.clear();121long[] a = new long[n + 7];122for (int i = 0; i < n; i++)123a[i + 7] = (long)ic(i);124b.put(a, 7, n);125b.flip();126}127128private static void bulkPutBuffer(LongBuffer b) {129int n = b.capacity();130b.clear();131LongBuffer c = LongBuffer.allocate(n + 7);132c.position(7);133for (int i = 0; i < n; i++)134c.put((long)ic(i));135c.flip();136c.position(7);137b.put(c);138b.flip();139try {140b.put(b);141fail("IllegalArgumentException expected for put into same buffer");142} catch (IllegalArgumentException e) {143if (e.getMessage() == null) {144fail("Non-null IllegalArgumentException message expected from"145+ " put into same buffer");146}147}148}149150private static void absBulkPutArray(LongBuffer b) {151int n = b.capacity();152b.clear();153int lim = n - 7;154int len = lim - 7;155b.limit(lim);156long[] a = new long[len + 7];157for (int i = 0; i < len; i++)158a[i + 7] = (long)ic(i);159b.position(42);160b.put(7, a, 7, len);161ck(b, b.position() == 42);162}163164//6231529165private static void callReset(LongBuffer b) {166b.position(0);167b.mark();168169b.duplicate().reset();170b.asReadOnlyBuffer().reset();171}172173174175// 6221101-6234263176177private static void putBuffer() {178final int cap = 10;179180LongBuffer direct1 = ByteBuffer.allocateDirect(cap).asLongBuffer();181LongBuffer nondirect1 = ByteBuffer.allocate(cap).asLongBuffer();182direct1.put(nondirect1);183184LongBuffer direct2 = ByteBuffer.allocateDirect(cap).asLongBuffer();185LongBuffer nondirect2 = ByteBuffer.allocate(cap).asLongBuffer();186nondirect2.put(direct2);187188LongBuffer direct3 = ByteBuffer.allocateDirect(cap).asLongBuffer();189LongBuffer direct4 = ByteBuffer.allocateDirect(cap).asLongBuffer();190direct3.put(direct4);191192LongBuffer nondirect3 = ByteBuffer.allocate(cap).asLongBuffer();193LongBuffer nondirect4 = ByteBuffer.allocate(cap).asLongBuffer();194nondirect3.put(nondirect4);195}196197198199200201202203204205206207208209210211212213private static void checkSlice(LongBuffer b, LongBuffer slice) {214ck(slice, 0, slice.position());215ck(slice, b.remaining(), slice.limit());216ck(slice, b.remaining(), slice.capacity());217if (b.isDirect() != slice.isDirect())218fail("Lost direction", slice);219if (b.isReadOnly() != slice.isReadOnly())220fail("Lost read-only", slice);221}222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474private static void fail(String problem,475LongBuffer xb, LongBuffer yb,476long x, long y) {477fail(problem + String.format(": x=%s y=%s", x, y), xb, yb);478}479480private static void catchNullArgument(Buffer b, Runnable thunk) {481tryCatch(b, NullPointerException.class, thunk);482}483484private static void catchIllegalArgument(Buffer b, Runnable thunk) {485tryCatch(b, IllegalArgumentException.class, thunk);486}487488private static void catchReadOnlyBuffer(Buffer b, Runnable thunk) {489tryCatch(b, ReadOnlyBufferException.class, thunk);490}491492private static void catchIndexOutOfBounds(Buffer b, Runnable thunk) {493tryCatch(b, IndexOutOfBoundsException.class, thunk);494}495496private static void catchIndexOutOfBounds(long[] t, Runnable thunk) {497tryCatch(t, IndexOutOfBoundsException.class, thunk);498}499500private static void tryCatch(Buffer b, Class<?> ex, Runnable thunk) {501boolean caught = false;502try {503thunk.run();504} catch (Throwable x) {505if (ex.isAssignableFrom(x.getClass())) {506caught = true;507} else {508String s = x.getMessage();509if (s == null)510s = x.getClass().getName();511fail(s + " not expected");512}513}514if (!caught) {515fail(ex.getName() + " not thrown", b);516}517}518519private static void tryCatch(long[] t, Class<?> ex, Runnable thunk) {520tryCatch(LongBuffer.wrap(t), ex, thunk);521}522523public static void test(int level, final LongBuffer b, boolean direct) {524525show(level, b);526527if (direct != b.isDirect())528fail("Wrong direction", b);529530// Gets and puts531532relPut(b);533relGet(b);534absGet(b);535bulkGet(b);536537absPut(b);538relGet(b);539absGet(b);540bulkGet(b);541542bulkPutArray(b);543relGet(b);544545bulkPutBuffer(b);546relGet(b);547548absBulkPutArray(b);549absBulkGet(b);550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587// Compact588589relPut(b);590b.position(13);591b.compact();592b.flip();593relGet(b, 13);594595// Exceptions596597relPut(b);598b.limit(b.capacity() / 2);599b.position(b.limit());600601tryCatch(b, BufferUnderflowException.class, () -> b.get());602tryCatch(b, BufferOverflowException.class, () -> b.put((long)42));603// The index must be non-negative and less than the buffer's limit.604catchIndexOutOfBounds(b, () -> b.get(b.limit()));605catchIndexOutOfBounds(b, () -> b.get(-1));606catchIndexOutOfBounds(b, () -> b.put(b.limit(), (long)42));607tryCatch(b, InvalidMarkException.class,608() -> b.position(0).mark().compact().reset());609610try {611b.position(b.limit() + 1);612fail("IllegalArgumentException expected for position beyond limit");613} catch (IllegalArgumentException e) {614if (e.getMessage() == null) {615fail("Non-null IllegalArgumentException message expected for"616+ " position beyond limit");617}618}619620try {621b.position(-1);622fail("IllegalArgumentException expected for negative position");623} catch (IllegalArgumentException e) {624if (e.getMessage() == null) {625fail("Non-null IllegalArgumentException message expected for"626+ " negative position");627}628}629630try {631b.limit(b.capacity() + 1);632fail("IllegalArgumentException expected for limit beyond capacity");633} catch (IllegalArgumentException e) {634if (e.getMessage() == null) {635fail("Non-null IllegalArgumentException message expected for"636+ " limit beyond capacity");637}638}639640try {641b.limit(-1);642fail("IllegalArgumentException expected for negative limit");643} catch (IllegalArgumentException e) {644if (e.getMessage() == null) {645fail("Non-null IllegalArgumentException message expected for"646+ " negative limit");647}648}649650// Exceptions in absolute bulk and slice operations651652catchNullArgument(b, () -> b.get(7, null, 0, 42));653catchNullArgument(b, () -> b.put(7, (long[])null, 0, 42));654655long[] tmpa = new long[42];656catchIndexOutOfBounds(b, () -> b.get(7, tmpa, -1, 42));657catchIndexOutOfBounds(b, () -> b.get(7, tmpa, 42, 1));658catchIndexOutOfBounds(b, () -> b.get(7, tmpa, 41, -1));659catchIndexOutOfBounds(b, () -> b.get(-1, tmpa, 0, 1));660catchIndexOutOfBounds(b, () -> b.get(b.limit(), tmpa, 0, 1));661catchIndexOutOfBounds(b, () -> b.get(b.limit() - 41, tmpa, 0, 42));662663catchIndexOutOfBounds(b, () -> b.put(7, tmpa, -1, 42));664catchIndexOutOfBounds(b, () -> b.put(7, tmpa, 42, 1));665catchIndexOutOfBounds(b, () -> b.put(7, tmpa, 41, -1));666catchIndexOutOfBounds(b, () -> b.put(-1, tmpa, 0, 1));667catchIndexOutOfBounds(b, () -> b.put(b.limit(), tmpa, 0, 1));668catchIndexOutOfBounds(b, () -> b.put(b.limit() - 41, tmpa, 0, 42));669670catchIndexOutOfBounds(b, () -> b.slice(-1, 7));671catchIndexOutOfBounds(b, () -> b.slice(b.limit() + 1, 7));672catchIndexOutOfBounds(b, () -> b.slice(0, -1));673catchIndexOutOfBounds(b, () -> b.slice(7, b.limit() - 7 + 1));674675// Values676677b.clear();678b.put((long)0);679b.put((long)-1);680b.put((long)1);681b.put(Long.MAX_VALUE);682b.put(Long.MIN_VALUE);683684685686687688689690691692693694695696697698699700b.flip();701ck(b, b.get(), 0);702ck(b, b.get(), (long)-1);703ck(b, b.get(), 1);704ck(b, b.get(), Long.MAX_VALUE);705ck(b, b.get(), Long.MIN_VALUE);706707708709710711712713714715716717718719720721722723724725726727728729730731732733// Comparison734b.rewind();735LongBuffer b2 = LongBuffer.allocate(b.capacity());736b2.put(b);737b2.flip();738b.position(2);739b2.position(2);740if (!b.equals(b2)) {741for (int i = 2; i < b.limit(); i++) {742long x = b.get(i);743long y = b2.get(i);744if (x != y745746747748749750751) {752out.println("[" + i + "] " + x + " != " + y);753}754}755fail("Identical buffers not equal", b, b2);756}757if (b.compareTo(b2) != 0) {758fail("Comparison to identical buffer != 0", b, b2);759}760b.limit(b.limit() + 1);761b.position(b.limit() - 1);762b.put((long)99);763b.rewind();764b2.rewind();765if (b.equals(b2))766fail("Non-identical buffers equal", b, b2);767if (b.compareTo(b2) <= 0)768fail("Comparison to shorter buffer <= 0", b, b2);769b.limit(b.limit() - 1);770771b.put(2, (long)42);772if (b.equals(b2))773fail("Non-identical buffers equal", b, b2);774if (b.compareTo(b2) <= 0)775fail("Comparison to lesser buffer <= 0", b, b2);776777// Check equals and compareTo with interesting values778for (long x : VALUES) {779LongBuffer xb = LongBuffer.wrap(new long[] { x });780if (xb.compareTo(xb) != 0) {781fail("compareTo not reflexive", xb, xb, x, x);782}783if (!xb.equals(xb)) {784fail("equals not reflexive", xb, xb, x, x);785}786for (long y : VALUES) {787LongBuffer yb = LongBuffer.wrap(new long[] { y });788if (xb.compareTo(yb) != - yb.compareTo(xb)) {789fail("compareTo not anti-symmetric",790xb, yb, x, y);791}792if ((xb.compareTo(yb) == 0) != xb.equals(yb)) {793fail("compareTo inconsistent with equals",794xb, yb, x, y);795}796if (xb.compareTo(yb) != Long.compare(x, y)) {797798799800801802803fail("Incorrect results for LongBuffer.compareTo",804xb, yb, x, y);805}806if (xb.equals(yb) != ((x == y) || ((x != x) && (y != y)))) {807fail("Incorrect results for LongBuffer.equals",808xb, yb, x, y);809}810}811}812813// Sub, dup814815relPut(b);816relGet(b.duplicate());817b.position(13);818relGet(b.duplicate(), 13);819relGet(b.duplicate().slice(), 13);820relGet(b.slice(), 13);821relGet(b.slice().duplicate(), 13);822823// Slice824825b.position(5);826LongBuffer sb = b.slice();827checkSlice(b, sb);828b.position(0);829LongBuffer sb2 = sb.slice();830checkSlice(sb, sb2);831832if (!sb.equals(sb2))833fail("Sliced slices do not match", sb, sb2);834if ((sb.hasArray()) && (sb.arrayOffset() != sb2.arrayOffset())) {835fail("Array offsets do not match: "836+ sb.arrayOffset() + " != " + sb2.arrayOffset(), sb, sb2);837}838839int bPos = b.position();840int bLim = b.limit();841842b.position(7);843b.limit(42);844LongBuffer rsb = b.slice();845b.position(0);846b.limit(b.capacity());847LongBuffer asb = b.slice(7, 35);848checkSlice(rsb, asb);849850b.position(bPos);851b.limit(bLim);852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884// Read-only views885886b.rewind();887final LongBuffer rb = b.asReadOnlyBuffer();888if (!b.equals(rb))889fail("Buffer not equal to read-only view", b, rb);890show(level + 1, rb);891892catchReadOnlyBuffer(b, () -> relPut(rb));893catchReadOnlyBuffer(b, () -> absPut(rb));894catchReadOnlyBuffer(b, () -> bulkPutArray(rb));895catchReadOnlyBuffer(b, () -> bulkPutBuffer(rb));896catchReadOnlyBuffer(b, () -> absBulkPutArray(rb));897898// put(LongBuffer) should not change source position899final LongBuffer src = LongBuffer.allocate(1);900catchReadOnlyBuffer(b, () -> rb.put(src));901ck(src, src.position(), 0);902903catchReadOnlyBuffer(b, () -> rb.compact());904905906907908909910911912913914915916917918919920921922923924925926927928929930if (rb.getClass().getName().startsWith("java.nio.Heap")) {931catchReadOnlyBuffer(b, () -> rb.array());932catchReadOnlyBuffer(b, () -> rb.arrayOffset());933if (rb.hasArray()) {934fail("Read-only heap buffer's backing array is accessible", rb);935}936}937938// Bulk puts from read-only buffers939940b.clear();941rb.rewind();942b.put(rb);943944945946947948949950951952953954relPut(b); // Required by testViews955956957958959960961}96296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013public static void test(final long [] ba) {1014int offset = 47;1015int length = 900;1016final LongBuffer b = LongBuffer.wrap(ba, offset, length);1017show(0, b);1018ck(b, b.capacity(), ba.length);1019ck(b, b.position(), offset);1020ck(b, b.limit(), offset + length);10211022// The offset must be non-negative and no larger than <array.length>.1023catchIndexOutOfBounds(ba, () -> LongBuffer.wrap(ba, -1, ba.length));1024catchIndexOutOfBounds(ba, () -> LongBuffer.wrap(ba, ba.length + 1, ba.length));1025catchIndexOutOfBounds(ba, () -> LongBuffer.wrap(ba, 0, -1));1026catchIndexOutOfBounds(ba, () -> LongBuffer.wrap(ba, 0, ba.length + 1));10271028// A NullPointerException will be thrown if the array is null.1029tryCatch(ba, NullPointerException.class,1030() -> LongBuffer.wrap((long []) null, 0, 5));1031tryCatch(ba, NullPointerException.class,1032() -> LongBuffer.wrap((long []) null));1033}10341035private static void testAllocate() {1036// An IllegalArgumentException will be thrown for negative capacities.1037catchIllegalArgument((Buffer) null, () -> LongBuffer.allocate(-1));1038try {1039LongBuffer.allocate(-1);1040} catch (IllegalArgumentException e) {1041if (e.getMessage() == null) {1042fail("Non-null IllegalArgumentException message expected for"1043+ " attempt to allocate negative capacity buffer");1044}1045}104610471048104910501051105210531054105510561057}10581059public static void test() {1060testAllocate();1061test(0, LongBuffer.allocate(7 * 1024), false);1062test(0, LongBuffer.wrap(new long[7 * 1024], 0, 7 * 1024), false);1063test(new long[1024]);10641065106610671068106910701071107210731074callReset(LongBuffer.allocate(10));1075107610771078putBuffer();10791080}10811082}108310841085