Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/nio/Buffer/BasicShort.java
38813 views
/*1* Copyright (c) 2000, 2012, 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.*;33import java.lang.reflect.Method;343536public class BasicShort37extends Basic38{3940private static final short[] VALUES = {41Short.MIN_VALUE,42(short) -1,43(short) 0,44(short) 1,45Short.MAX_VALUE,46474849505152535455565758};5960private static void relGet(ShortBuffer b) {61int n = b.capacity();62short v;63for (int i = 0; i < n; i++)64ck(b, (long)b.get(), (long)((short)ic(i)));65b.rewind();66}6768private static void relGet(ShortBuffer b, int start) {69int n = b.remaining();70short v;71for (int i = start; i < n; i++)72ck(b, (long)b.get(), (long)((short)ic(i)));73b.rewind();74}7576private static void absGet(ShortBuffer b) {77int n = b.capacity();78short v;79for (int i = 0; i < n; i++)80ck(b, (long)b.get(), (long)((short)ic(i)));81b.rewind();82}8384private static void bulkGet(ShortBuffer b) {85int n = b.capacity();86short[] a = new short[n + 7];87b.get(a, 7, n);88for (int i = 0; i < n; i++)89ck(b, (long)a[i + 7], (long)((short)ic(i)));90}9192private static void relPut(ShortBuffer b) {93int n = b.capacity();94b.clear();95for (int i = 0; i < n; i++)96b.put((short)ic(i));97b.flip();98}99100private static void absPut(ShortBuffer b) {101int n = b.capacity();102b.clear();103for (int i = 0; i < n; i++)104b.put(i, (short)ic(i));105b.limit(n);106b.position(0);107}108109private static void bulkPutArray(ShortBuffer b) {110int n = b.capacity();111b.clear();112short[] a = new short[n + 7];113for (int i = 0; i < n; i++)114a[i + 7] = (short)ic(i);115b.put(a, 7, n);116b.flip();117}118119private static void bulkPutBuffer(ShortBuffer b) {120int n = b.capacity();121b.clear();122ShortBuffer c = ShortBuffer.allocate(n + 7);123c.position(7);124for (int i = 0; i < n; i++)125c.put((short)ic(i));126c.flip();127c.position(7);128b.put(c);129b.flip();130}131132//6231529133private static void callReset(ShortBuffer b) {134b.position(0);135b.mark();136137b.duplicate().reset();138b.asReadOnlyBuffer().reset();139}140141142143// 6221101-6234263144145private static void putBuffer() {146final int cap = 10;147148ShortBuffer direct1 = ByteBuffer.allocateDirect(cap).asShortBuffer();149ShortBuffer nondirect1 = ByteBuffer.allocate(cap).asShortBuffer();150direct1.put(nondirect1);151152ShortBuffer direct2 = ByteBuffer.allocateDirect(cap).asShortBuffer();153ShortBuffer nondirect2 = ByteBuffer.allocate(cap).asShortBuffer();154nondirect2.put(direct2);155156ShortBuffer direct3 = ByteBuffer.allocateDirect(cap).asShortBuffer();157ShortBuffer direct4 = ByteBuffer.allocateDirect(cap).asShortBuffer();158direct3.put(direct4);159160ShortBuffer nondirect3 = ByteBuffer.allocate(cap).asShortBuffer();161ShortBuffer nondirect4 = ByteBuffer.allocate(cap).asShortBuffer();162nondirect3.put(nondirect4);163}164165166167168169170171172173174175176177178179180181private static void checkSlice(ShortBuffer b, ShortBuffer slice) {182ck(slice, 0, slice.position());183ck(slice, b.remaining(), slice.limit());184ck(slice, b.remaining(), slice.capacity());185if (b.isDirect() != slice.isDirect())186fail("Lost direction", slice);187if (b.isReadOnly() != slice.isReadOnly())188fail("Lost read-only", slice);189}190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331private static void fail(String problem,332ShortBuffer xb, ShortBuffer yb,333short x, short y) {334fail(problem + String.format(": x=%s y=%s", x, y), xb, yb);335}336337private static void tryCatch(Buffer b, Class<?> ex, Runnable thunk) {338boolean caught = false;339try {340thunk.run();341} catch (Throwable x) {342if (ex.isAssignableFrom(x.getClass())) {343caught = true;344} else {345fail(x.getMessage() + " not expected");346}347}348if (!caught)349fail(ex.getName() + " not thrown", b);350}351352private static void tryCatch(short [] t, Class<?> ex, Runnable thunk) {353tryCatch(ShortBuffer.wrap(t), ex, thunk);354}355356public static void test(int level, final ShortBuffer b, boolean direct) {357358show(level, b);359360if (direct != b.isDirect())361fail("Wrong direction", b);362363// Gets and puts364365relPut(b);366relGet(b);367absGet(b);368bulkGet(b);369370absPut(b);371relGet(b);372absGet(b);373bulkGet(b);374375bulkPutArray(b);376relGet(b);377378bulkPutBuffer(b);379relGet(b);380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419// Compact420421relPut(b);422b.position(13);423b.compact();424b.flip();425relGet(b, 13);426427// Exceptions428429relPut(b);430b.limit(b.capacity() / 2);431b.position(b.limit());432433tryCatch(b, BufferUnderflowException.class, new Runnable() {434public void run() {435b.get();436}});437438tryCatch(b, BufferOverflowException.class, new Runnable() {439public void run() {440b.put((short)42);441}});442443// The index must be non-negative and lesss than the buffer's limit.444tryCatch(b, IndexOutOfBoundsException.class, new Runnable() {445public void run() {446b.get(b.limit());447}});448tryCatch(b, IndexOutOfBoundsException.class, new Runnable() {449public void run() {450b.get(-1);451}});452453tryCatch(b, IndexOutOfBoundsException.class, new Runnable() {454public void run() {455b.put(b.limit(), (short)42);456}});457458tryCatch(b, InvalidMarkException.class, new Runnable() {459public void run() {460b.position(0);461b.mark();462b.compact();463b.reset();464}});465466// Values467468b.clear();469b.put((short)0);470b.put((short)-1);471b.put((short)1);472b.put(Short.MAX_VALUE);473b.put(Short.MIN_VALUE);474475476477478479480481482483484485486487488489490491short v;492b.flip();493ck(b, b.get(), 0);494ck(b, b.get(), (short)-1);495ck(b, b.get(), 1);496ck(b, b.get(), Short.MAX_VALUE);497ck(b, b.get(), Short.MIN_VALUE);498499500501502503504505506507508509510511512513514515516517518519520// Comparison521b.rewind();522ShortBuffer b2 = ShortBuffer.allocate(b.capacity());523b2.put(b);524b2.flip();525b.position(2);526b2.position(2);527if (!b.equals(b2)) {528for (int i = 2; i < b.limit(); i++) {529short x = b.get(i);530short y = b2.get(i);531if (x != y532533534535536537538)539out.println("[" + i + "] " + x + " != " + y);540}541fail("Identical buffers not equal", b, b2);542}543if (b.compareTo(b2) != 0)544fail("Comparison to identical buffer != 0", b, b2);545546b.limit(b.limit() + 1);547b.position(b.limit() - 1);548b.put((short)99);549b.rewind();550b2.rewind();551if (b.equals(b2))552fail("Non-identical buffers equal", b, b2);553if (b.compareTo(b2) <= 0)554fail("Comparison to shorter buffer <= 0", b, b2);555b.limit(b.limit() - 1);556557b.put(2, (short)42);558if (b.equals(b2))559fail("Non-identical buffers equal", b, b2);560if (b.compareTo(b2) <= 0)561fail("Comparison to lesser buffer <= 0", b, b2);562563// Check equals and compareTo with interesting values564for (short x : VALUES) {565ShortBuffer xb = ShortBuffer.wrap(new short[] { x });566if (xb.compareTo(xb) != 0) {567fail("compareTo not reflexive", xb, xb, x, x);568}569if (! xb.equals(xb)) {570fail("equals not reflexive", xb, xb, x, x);571}572for (short y : VALUES) {573ShortBuffer yb = ShortBuffer.wrap(new short[] { y });574if (xb.compareTo(yb) != - yb.compareTo(xb)) {575fail("compareTo not anti-symmetric",576xb, yb, x, y);577}578if ((xb.compareTo(yb) == 0) != xb.equals(yb)) {579fail("compareTo inconsistent with equals",580xb, yb, x, y);581}582if (xb.compareTo(yb) != Short.compare(x, y)) {583584585586587588589fail("Incorrect results for ShortBuffer.compareTo",590xb, yb, x, y);591}592if (xb.equals(yb) != ((x == y) || ((x != x) && (y != y)))) {593fail("Incorrect results for ShortBuffer.equals",594xb, yb, x, y);595}596}597}598599// Sub, dup600601relPut(b);602relGet(b.duplicate());603b.position(13);604relGet(b.duplicate(), 13);605relGet(b.duplicate().slice(), 13);606relGet(b.slice(), 13);607relGet(b.slice().duplicate(), 13);608609// Slice610611b.position(5);612ShortBuffer sb = b.slice();613checkSlice(b, sb);614b.position(0);615ShortBuffer sb2 = sb.slice();616checkSlice(sb, sb2);617618if (!sb.equals(sb2))619fail("Sliced slices do not match", sb, sb2);620if ((sb.hasArray()) && (sb.arrayOffset() != sb2.arrayOffset()))621fail("Array offsets do not match: "622+ sb.arrayOffset() + " != " + sb2.arrayOffset(), sb, sb2);623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655// Read-only views656657b.rewind();658final ShortBuffer rb = b.asReadOnlyBuffer();659if (!b.equals(rb))660fail("Buffer not equal to read-only view", b, rb);661show(level + 1, rb);662663tryCatch(b, ReadOnlyBufferException.class, new Runnable() {664public void run() {665relPut(rb);666}});667668tryCatch(b, ReadOnlyBufferException.class, new Runnable() {669public void run() {670absPut(rb);671}});672673tryCatch(b, ReadOnlyBufferException.class, new Runnable() {674public void run() {675bulkPutArray(rb);676}});677678tryCatch(b, ReadOnlyBufferException.class, new Runnable() {679public void run() {680bulkPutBuffer(rb);681}});682683// put(ShortBuffer) should not change source position684final ShortBuffer src = ShortBuffer.allocate(1);685tryCatch(b, ReadOnlyBufferException.class, new Runnable() {686public void run() {687rb.put(src);688}});689ck(src, src.position(), 0);690691tryCatch(b, ReadOnlyBufferException.class, new Runnable() {692public void run() {693rb.compact();694}});695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770if (rb.getClass().getName().startsWith("java.nio.Heap")) {771772tryCatch(b, ReadOnlyBufferException.class, new Runnable() {773public void run() {774rb.array();775}});776777tryCatch(b, ReadOnlyBufferException.class, new Runnable() {778public void run() {779rb.arrayOffset();780}});781782if (rb.hasArray())783fail("Read-only heap buffer's backing array is accessible",784rb);785786}787788// Bulk puts from read-only buffers789790b.clear();791rb.rewind();792b.put(rb);793794795796797798799800801802803804relPut(b); // Required by testViews805806}807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892public static void test(final short [] ba) {893int offset = 47;894int length = 900;895final ShortBuffer b = ShortBuffer.wrap(ba, offset, length);896show(0, b);897ck(b, b.capacity(), ba.length);898ck(b, b.position(), offset);899ck(b, b.limit(), offset + length);900901// The offset must be non-negative and no larger than <array.length>.902tryCatch(ba, IndexOutOfBoundsException.class, new Runnable() {903public void run() {904ShortBuffer.wrap(ba, -1, ba.length);905}});906tryCatch(ba, IndexOutOfBoundsException.class, new Runnable() {907public void run() {908ShortBuffer.wrap(ba, ba.length + 1, ba.length);909}});910tryCatch(ba, IndexOutOfBoundsException.class, new Runnable() {911public void run() {912ShortBuffer.wrap(ba, 0, -1);913}});914tryCatch(ba, IndexOutOfBoundsException.class, new Runnable() {915public void run() {916ShortBuffer.wrap(ba, 0, ba.length + 1);917}});918919// A NullPointerException will be thrown if the array is null.920tryCatch(ba, NullPointerException.class, new Runnable() {921public void run() {922ShortBuffer.wrap((short []) null, 0, 5);923}});924tryCatch(ba, NullPointerException.class, new Runnable() {925public void run() {926ShortBuffer.wrap((short []) null);927}});928}929930private static void testAllocate() {931// An IllegalArgumentException will be thrown for negative capacities.932tryCatch((Buffer) null, IllegalArgumentException.class, new Runnable() {933public void run() {934ShortBuffer.allocate(-1);935}});936937938939940941942}943944public static void test() {945testAllocate();946test(0, ShortBuffer.allocate(7 * 1024), false);947test(0, ShortBuffer.wrap(new short[7 * 1024], 0, 7 * 1024), false);948test(new short[1024]);949950951952953954955956957958959callReset(ShortBuffer.allocate(10));960961962963putBuffer();964965}966967}968969970