Path: blob/master/test/jdk/java/nio/Buffer/BasicDouble.java
66644 views
/*1* Copyright (c) 2000, 2020, 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! -- //313233343536import java.nio.*;373839404142434445public class BasicDouble46extends Basic47{4849private static final double[] VALUES = {50Double.MIN_VALUE,51(double) -1,52(double) 0,53(double) 1,54Double.MAX_VALUE,5556575859606162Double.NEGATIVE_INFINITY,63Double.POSITIVE_INFINITY,64Double.NaN,65(double) -0.0,6667};6869private static void relGet(DoubleBuffer b) {70int n = b.capacity();71for (int i = 0; i < n; i++)72ck(b, (long)b.get(), (long)((double)ic(i)));73b.rewind();74}7576private static void relGet(DoubleBuffer b, int start) {77int n = b.remaining();78for (int i = start; i < n; i++)79ck(b, (long)b.get(), (long)((double)ic(i)));80b.rewind();81}8283private static void absGet(DoubleBuffer b) {84int n = b.capacity();85for (int i = 0; i < n; i++)86ck(b, (long)b.get(), (long)((double)ic(i)));87b.rewind();88}8990private static void bulkGet(DoubleBuffer b) {91int n = b.capacity();92double[] a = new double[n + 7];93b.get(a, 7, n);94for (int i = 0; i < n; i++) {95ck(b, (long)a[i + 7], (long)((double)ic(i)));96}97}9899private static void absBulkGet(DoubleBuffer b) {100int n = b.capacity();101int len = n - 7*2;102double[] a = new double[n + 7];103b.position(42);104b.get(7, a, 7, len);105ck(b, b.position() == 42);106for (int i = 0; i < len; i++) {107ck(b, (long)a[i + 7], (long)((double)ic(i)));108}109}110111private static void relPut(DoubleBuffer b) {112int n = b.capacity();113b.clear();114for (int i = 0; i < n; i++)115b.put((double)ic(i));116b.flip();117}118119private static void absPut(DoubleBuffer b) {120int n = b.capacity();121b.clear();122for (int i = 0; i < n; i++)123b.put(i, (double)ic(i));124b.limit(n);125b.position(0);126}127128private static void bulkPutArray(DoubleBuffer b) {129int n = b.capacity();130b.clear();131double[] a = new double[n + 7];132for (int i = 0; i < n; i++)133a[i + 7] = (double)ic(i);134b.put(a, 7, n);135b.flip();136}137138private static void bulkPutBuffer(DoubleBuffer b) {139int n = b.capacity();140b.clear();141DoubleBuffer c = DoubleBuffer.allocate(n + 7);142c.position(7);143for (int i = 0; i < n; i++)144c.put((double)ic(i));145c.flip();146c.position(7);147b.put(c);148b.flip();149try {150b.put(b);151fail("IllegalArgumentException expected for put into same buffer");152} catch (IllegalArgumentException e) {153if (e.getMessage() == null) {154fail("Non-null IllegalArgumentException message expected from"155+ " put into same buffer");156}157}158}159160private static void absBulkPutArray(DoubleBuffer b) {161int n = b.capacity();162b.clear();163int lim = n - 7;164int len = lim - 7;165b.limit(lim);166double[] a = new double[len + 7];167for (int i = 0; i < len; i++)168a[i + 7] = (double)ic(i);169b.position(42);170b.put(7, a, 7, len);171ck(b, b.position() == 42);172}173174//6231529175private static void callReset(DoubleBuffer b) {176b.position(0);177b.mark();178179b.duplicate().reset();180b.asReadOnlyBuffer().reset();181}182183184185// 6221101-6234263186187private static void putBuffer() {188final int cap = 10;189190DoubleBuffer direct1 = ByteBuffer.allocateDirect(cap).asDoubleBuffer();191DoubleBuffer nondirect1 = ByteBuffer.allocate(cap).asDoubleBuffer();192direct1.put(nondirect1);193194DoubleBuffer direct2 = ByteBuffer.allocateDirect(cap).asDoubleBuffer();195DoubleBuffer nondirect2 = ByteBuffer.allocate(cap).asDoubleBuffer();196nondirect2.put(direct2);197198DoubleBuffer direct3 = ByteBuffer.allocateDirect(cap).asDoubleBuffer();199DoubleBuffer direct4 = ByteBuffer.allocateDirect(cap).asDoubleBuffer();200direct3.put(direct4);201202DoubleBuffer nondirect3 = ByteBuffer.allocate(cap).asDoubleBuffer();203DoubleBuffer nondirect4 = ByteBuffer.allocate(cap).asDoubleBuffer();204nondirect3.put(nondirect4);205}206207208209210211212213214215216217218219220221222223private static void checkSlice(DoubleBuffer b, DoubleBuffer slice) {224ck(slice, 0, slice.position());225ck(slice, b.remaining(), slice.limit());226ck(slice, b.remaining(), slice.capacity());227if (b.isDirect() != slice.isDirect())228fail("Lost direction", slice);229if (b.isReadOnly() != slice.isReadOnly())230fail("Lost read-only", slice);231}232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551private static void fail(String problem,552DoubleBuffer xb, DoubleBuffer yb,553double x, double y) {554fail(problem + String.format(": x=%s y=%s", x, y), xb, yb);555}556557private static void catchNullArgument(Buffer b, Runnable thunk) {558tryCatch(b, NullPointerException.class, thunk);559}560561private static void catchIllegalArgument(Buffer b, Runnable thunk) {562tryCatch(b, IllegalArgumentException.class, thunk);563}564565private static void catchReadOnlyBuffer(Buffer b, Runnable thunk) {566tryCatch(b, ReadOnlyBufferException.class, thunk);567}568569private static void catchIndexOutOfBounds(Buffer b, Runnable thunk) {570tryCatch(b, IndexOutOfBoundsException.class, thunk);571}572573private static void catchIndexOutOfBounds(double[] t, Runnable thunk) {574tryCatch(t, IndexOutOfBoundsException.class, thunk);575}576577private static void tryCatch(Buffer b, Class<?> ex, Runnable thunk) {578boolean caught = false;579try {580thunk.run();581} catch (Throwable x) {582if (ex.isAssignableFrom(x.getClass())) {583caught = true;584} else {585String s = x.getMessage();586if (s == null)587s = x.getClass().getName();588fail(s + " not expected");589}590}591if (!caught) {592fail(ex.getName() + " not thrown", b);593}594}595596private static void tryCatch(double[] t, Class<?> ex, Runnable thunk) {597tryCatch(DoubleBuffer.wrap(t), ex, thunk);598}599600public static void test(int level, final DoubleBuffer b, boolean direct) {601602show(level, b);603604if (direct != b.isDirect())605fail("Wrong direction", b);606607// Gets and puts608609relPut(b);610relGet(b);611absGet(b);612bulkGet(b);613614absPut(b);615relGet(b);616absGet(b);617bulkGet(b);618619bulkPutArray(b);620relGet(b);621622bulkPutBuffer(b);623relGet(b);624625absBulkPutArray(b);626absBulkGet(b);627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664// Compact665666relPut(b);667b.position(13);668b.compact();669b.flip();670relGet(b, 13);671672// Exceptions673674relPut(b);675b.limit(b.capacity() / 2);676b.position(b.limit());677678tryCatch(b, BufferUnderflowException.class, () -> b.get());679tryCatch(b, BufferOverflowException.class, () -> b.put((double)42));680// The index must be non-negative and less than the buffer's limit.681catchIndexOutOfBounds(b, () -> b.get(b.limit()));682catchIndexOutOfBounds(b, () -> b.get(-1));683catchIndexOutOfBounds(b, () -> b.put(b.limit(), (double)42));684tryCatch(b, InvalidMarkException.class,685() -> b.position(0).mark().compact().reset());686687try {688b.position(b.limit() + 1);689fail("IllegalArgumentException expected for position beyond limit");690} catch (IllegalArgumentException e) {691if (e.getMessage() == null) {692fail("Non-null IllegalArgumentException message expected for"693+ " position beyond limit");694}695}696697try {698b.position(-1);699fail("IllegalArgumentException expected for negative position");700} catch (IllegalArgumentException e) {701if (e.getMessage() == null) {702fail("Non-null IllegalArgumentException message expected for"703+ " negative position");704}705}706707try {708b.limit(b.capacity() + 1);709fail("IllegalArgumentException expected for limit beyond capacity");710} catch (IllegalArgumentException e) {711if (e.getMessage() == null) {712fail("Non-null IllegalArgumentException message expected for"713+ " limit beyond capacity");714}715}716717try {718b.limit(-1);719fail("IllegalArgumentException expected for negative limit");720} catch (IllegalArgumentException e) {721if (e.getMessage() == null) {722fail("Non-null IllegalArgumentException message expected for"723+ " negative limit");724}725}726727// Exceptions in absolute bulk and slice operations728729catchNullArgument(b, () -> b.get(7, null, 0, 42));730catchNullArgument(b, () -> b.put(7, (double[])null, 0, 42));731732double[] tmpa = new double[42];733catchIndexOutOfBounds(b, () -> b.get(7, tmpa, -1, 42));734catchIndexOutOfBounds(b, () -> b.get(7, tmpa, 42, 1));735catchIndexOutOfBounds(b, () -> b.get(7, tmpa, 41, -1));736catchIndexOutOfBounds(b, () -> b.get(-1, tmpa, 0, 1));737catchIndexOutOfBounds(b, () -> b.get(b.limit(), tmpa, 0, 1));738catchIndexOutOfBounds(b, () -> b.get(b.limit() - 41, tmpa, 0, 42));739740catchIndexOutOfBounds(b, () -> b.put(7, tmpa, -1, 42));741catchIndexOutOfBounds(b, () -> b.put(7, tmpa, 42, 1));742catchIndexOutOfBounds(b, () -> b.put(7, tmpa, 41, -1));743catchIndexOutOfBounds(b, () -> b.put(-1, tmpa, 0, 1));744catchIndexOutOfBounds(b, () -> b.put(b.limit(), tmpa, 0, 1));745catchIndexOutOfBounds(b, () -> b.put(b.limit() - 41, tmpa, 0, 42));746747catchIndexOutOfBounds(b, () -> b.slice(-1, 7));748catchIndexOutOfBounds(b, () -> b.slice(b.limit() + 1, 7));749catchIndexOutOfBounds(b, () -> b.slice(0, -1));750catchIndexOutOfBounds(b, () -> b.slice(7, b.limit() - 7 + 1));751752// Values753754b.clear();755b.put((double)0);756b.put((double)-1);757b.put((double)1);758b.put(Double.MAX_VALUE);759b.put(Double.MIN_VALUE);760761762763764765766767768769b.put(-Double.MAX_VALUE);770b.put(-Double.MIN_VALUE);771b.put(Double.NEGATIVE_INFINITY);772b.put(Double.POSITIVE_INFINITY);773b.put(Double.NaN);774b.put(0.5121609353879392); // Changes value if incorrectly swapped775776777b.flip();778ck(b, b.get(), 0);779ck(b, b.get(), (double)-1);780ck(b, b.get(), 1);781ck(b, b.get(), Double.MAX_VALUE);782ck(b, b.get(), Double.MIN_VALUE);783784785786787788789790791792793794795796797double v;798ck(b, b.get(), -Double.MAX_VALUE);799ck(b, b.get(), -Double.MIN_VALUE);800ck(b, b.get(), Double.NEGATIVE_INFINITY);801ck(b, b.get(), Double.POSITIVE_INFINITY);802if (Double.doubleToRawLongBits(v = b.get())803!= Double.doubleToRawLongBits(Double.NaN)) {804fail(b, (long)Double.NaN, (long)v);805}806ck(b, b.get(), 0.5121609353879392);807808809810// Comparison811b.rewind();812DoubleBuffer b2 = DoubleBuffer.allocate(b.capacity());813b2.put(b);814b2.flip();815b.position(2);816b2.position(2);817if (!b.equals(b2)) {818for (int i = 2; i < b.limit(); i++) {819double x = b.get(i);820double y = b2.get(i);821if (x != y822823|| Double.compare(x, y) != 0824825826827828) {829out.println("[" + i + "] " + x + " != " + y);830}831}832fail("Identical buffers not equal", b, b2);833}834if (b.compareTo(b2) != 0) {835fail("Comparison to identical buffer != 0", b, b2);836}837b.limit(b.limit() + 1);838b.position(b.limit() - 1);839b.put((double)99);840b.rewind();841b2.rewind();842if (b.equals(b2))843fail("Non-identical buffers equal", b, b2);844if (b.compareTo(b2) <= 0)845fail("Comparison to shorter buffer <= 0", b, b2);846b.limit(b.limit() - 1);847848b.put(2, (double)42);849if (b.equals(b2))850fail("Non-identical buffers equal", b, b2);851if (b.compareTo(b2) <= 0)852fail("Comparison to lesser buffer <= 0", b, b2);853854// Check equals and compareTo with interesting values855for (double x : VALUES) {856DoubleBuffer xb = DoubleBuffer.wrap(new double[] { x });857if (xb.compareTo(xb) != 0) {858fail("compareTo not reflexive", xb, xb, x, x);859}860if (!xb.equals(xb)) {861fail("equals not reflexive", xb, xb, x, x);862}863for (double y : VALUES) {864DoubleBuffer yb = DoubleBuffer.wrap(new double[] { y });865if (xb.compareTo(yb) != - yb.compareTo(xb)) {866fail("compareTo not anti-symmetric",867xb, yb, x, y);868}869if ((xb.compareTo(yb) == 0) != xb.equals(yb)) {870fail("compareTo inconsistent with equals",871xb, yb, x, y);872}873if (xb.compareTo(yb) != Double.compare(x, y)) {874875876877878if (x == 0.0 && y == 0.0) continue;879880fail("Incorrect results for DoubleBuffer.compareTo",881xb, yb, x, y);882}883if (xb.equals(yb) != ((x == y) || ((x != x) && (y != y)))) {884fail("Incorrect results for DoubleBuffer.equals",885xb, yb, x, y);886}887}888}889890// Sub, dup891892relPut(b);893relGet(b.duplicate());894b.position(13);895relGet(b.duplicate(), 13);896relGet(b.duplicate().slice(), 13);897relGet(b.slice(), 13);898relGet(b.slice().duplicate(), 13);899900// Slice901902b.position(5);903DoubleBuffer sb = b.slice();904checkSlice(b, sb);905b.position(0);906DoubleBuffer sb2 = sb.slice();907checkSlice(sb, sb2);908909if (!sb.equals(sb2))910fail("Sliced slices do not match", sb, sb2);911if ((sb.hasArray()) && (sb.arrayOffset() != sb2.arrayOffset())) {912fail("Array offsets do not match: "913+ sb.arrayOffset() + " != " + sb2.arrayOffset(), sb, sb2);914}915916int bPos = b.position();917int bLim = b.limit();918919b.position(7);920b.limit(42);921DoubleBuffer rsb = b.slice();922b.position(0);923b.limit(b.capacity());924DoubleBuffer asb = b.slice(7, 35);925checkSlice(rsb, asb);926927b.position(bPos);928b.limit(bLim);929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961// Read-only views962963b.rewind();964final DoubleBuffer rb = b.asReadOnlyBuffer();965if (!b.equals(rb))966fail("Buffer not equal to read-only view", b, rb);967show(level + 1, rb);968969catchReadOnlyBuffer(b, () -> relPut(rb));970catchReadOnlyBuffer(b, () -> absPut(rb));971catchReadOnlyBuffer(b, () -> bulkPutArray(rb));972catchReadOnlyBuffer(b, () -> bulkPutBuffer(rb));973catchReadOnlyBuffer(b, () -> absBulkPutArray(rb));974975// put(DoubleBuffer) should not change source position976final DoubleBuffer src = DoubleBuffer.allocate(1);977catchReadOnlyBuffer(b, () -> rb.put(src));978ck(src, src.position(), 0);979980catchReadOnlyBuffer(b, () -> rb.compact());98198298398498598698798898999099199299399499599699799899910001001100210031004100510061007if (rb.getClass().getName().startsWith("java.nio.Heap")) {1008catchReadOnlyBuffer(b, () -> rb.array());1009catchReadOnlyBuffer(b, () -> rb.arrayOffset());1010if (rb.hasArray()) {1011fail("Read-only heap buffer's backing array is accessible", rb);1012}1013}10141015// Bulk puts from read-only buffers10161017b.clear();1018rb.rewind();1019b.put(rb);102010211022102310241025102610271028102910301031relPut(b); // Required by testViews1032103310341035103610371038}1039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090public static void test(final double [] ba) {1091int offset = 47;1092int length = 900;1093final DoubleBuffer b = DoubleBuffer.wrap(ba, offset, length);1094show(0, b);1095ck(b, b.capacity(), ba.length);1096ck(b, b.position(), offset);1097ck(b, b.limit(), offset + length);10981099// The offset must be non-negative and no larger than <array.length>.1100catchIndexOutOfBounds(ba, () -> DoubleBuffer.wrap(ba, -1, ba.length));1101catchIndexOutOfBounds(ba, () -> DoubleBuffer.wrap(ba, ba.length + 1, ba.length));1102catchIndexOutOfBounds(ba, () -> DoubleBuffer.wrap(ba, 0, -1));1103catchIndexOutOfBounds(ba, () -> DoubleBuffer.wrap(ba, 0, ba.length + 1));11041105// A NullPointerException will be thrown if the array is null.1106tryCatch(ba, NullPointerException.class,1107() -> DoubleBuffer.wrap((double []) null, 0, 5));1108tryCatch(ba, NullPointerException.class,1109() -> DoubleBuffer.wrap((double []) null));1110}11111112private static void testAllocate() {1113// An IllegalArgumentException will be thrown for negative capacities.1114catchIllegalArgument((Buffer) null, () -> DoubleBuffer.allocate(-1));1115try {1116DoubleBuffer.allocate(-1);1117} catch (IllegalArgumentException e) {1118if (e.getMessage() == null) {1119fail("Non-null IllegalArgumentException message expected for"1120+ " attempt to allocate negative capacity buffer");1121}1122}112311241125112611271128112911301131113211331134}11351136public static void testToString() {1137final int cap = 10;11381139114011411142114311441145114611471148DoubleBuffer nondirect1 = DoubleBuffer.allocate(cap);1149if (!nondirect1.toString().equals(Basic.toString(nondirect1))) {1150fail("Heap buffer toString is incorrect: "1151+ nondirect1.toString() + " vs " + Basic.toString(nondirect1));1152}11531154}11551156public static void test() {1157testAllocate();1158test(0, DoubleBuffer.allocate(7 * 1024), false);1159test(0, DoubleBuffer.wrap(new double[7 * 1024], 0, 7 * 1024), false);1160test(new double[1024]);11611162116311641165116611671168116911701171callReset(DoubleBuffer.allocate(10));1172117311741175putBuffer();117611771178testToString();1179}11801181}118211831184