Path: blob/master/test/jdk/java/nio/Buffer/BasicChar.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 BasicChar36extends Basic37{3839private static final char[] VALUES = {40Character.MIN_VALUE,41(char) -1,42(char) 0,43(char) 1,44Character.MAX_VALUE,45464748495051525354555657};5859private static void relGet(CharBuffer b) {60int n = b.capacity();61for (int i = 0; i < n; i++)62ck(b, (long)b.get(), (long)((char)ic(i)));63b.rewind();64}6566private static void relGet(CharBuffer b, int start) {67int n = b.remaining();68for (int i = start; i < n; i++)69ck(b, (long)b.get(), (long)((char)ic(i)));70b.rewind();71}7273private static void absGet(CharBuffer b) {74int n = b.capacity();75for (int i = 0; i < n; i++)76ck(b, (long)b.get(), (long)((char)ic(i)));77b.rewind();78}7980private static void bulkGet(CharBuffer b) {81int n = b.capacity();82char[] a = new char[n + 7];83b.get(a, 7, n);84for (int i = 0; i < n; i++) {85ck(b, (long)a[i + 7], (long)((char)ic(i)));86}87}8889private static void absBulkGet(CharBuffer b) {90int n = b.capacity();91int len = n - 7*2;92char[] a = new char[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)((char)ic(i)));98}99}100101private static void relPut(CharBuffer b) {102int n = b.capacity();103b.clear();104for (int i = 0; i < n; i++)105b.put((char)ic(i));106b.flip();107}108109private static void absPut(CharBuffer b) {110int n = b.capacity();111b.clear();112for (int i = 0; i < n; i++)113b.put(i, (char)ic(i));114b.limit(n);115b.position(0);116}117118private static void bulkPutArray(CharBuffer b) {119int n = b.capacity();120b.clear();121char[] a = new char[n + 7];122for (int i = 0; i < n; i++)123a[i + 7] = (char)ic(i);124b.put(a, 7, n);125b.flip();126}127128private static void bulkPutBuffer(CharBuffer b) {129int n = b.capacity();130b.clear();131CharBuffer c = CharBuffer.allocate(n + 7);132c.position(7);133for (int i = 0; i < n; i++)134c.put((char)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(CharBuffer b) {151int n = b.capacity();152b.clear();153int lim = n - 7;154int len = lim - 7;155b.limit(lim);156char[] a = new char[len + 7];157for (int i = 0; i < len; i++)158a[i + 7] = (char)ic(i);159b.position(42);160b.put(7, a, 7, len);161ck(b, b.position() == 42);162}163164//6231529165private static void callReset(CharBuffer b) {166b.position(0);167b.mark();168169b.duplicate().reset();170b.asReadOnlyBuffer().reset();171}172173174175// 6221101-6234263176177private static void putBuffer() {178final int cap = 10;179180CharBuffer direct1 = ByteBuffer.allocateDirect(cap).asCharBuffer();181CharBuffer nondirect1 = ByteBuffer.allocate(cap).asCharBuffer();182direct1.put(nondirect1);183184CharBuffer direct2 = ByteBuffer.allocateDirect(cap).asCharBuffer();185CharBuffer nondirect2 = ByteBuffer.allocate(cap).asCharBuffer();186nondirect2.put(direct2);187188CharBuffer direct3 = ByteBuffer.allocateDirect(cap).asCharBuffer();189CharBuffer direct4 = ByteBuffer.allocateDirect(cap).asCharBuffer();190direct3.put(direct4);191192CharBuffer nondirect3 = ByteBuffer.allocate(cap).asCharBuffer();193CharBuffer nondirect4 = ByteBuffer.allocate(cap).asCharBuffer();194nondirect3.put(nondirect4);195}196197198199200private static void bulkPutString(CharBuffer b) {201int n = b.capacity();202b.clear();203StringBuilder sb = new StringBuilder(n + 7);204sb.append("1234567");205for (int i = 0; i < n; i++)206sb.append((char)ic(i));207b.put(sb.toString(), 7, 7 + n);208b.flip();209}210211212213private static void checkSlice(CharBuffer b, CharBuffer 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,475CharBuffer xb, CharBuffer yb,476char x, char 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(char[] 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(char[] t, Class<?> ex, Runnable thunk) {520tryCatch(CharBuffer.wrap(t), ex, thunk);521}522523public static void test(int level, final CharBuffer 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);550551552553bulkPutString(b);554relGet(b);555b.position(1);556b.limit(7);557ck(b, b.toString().equals("bcdefg"));558559// CharSequence ops560561b.position(2);562ck(b, b.charAt(1), 'd');563CharBuffer c = b.subSequence(1, 4);564ck(c, c.capacity(), b.capacity());565ck(c, c.position(), b.position()+1);566ck(c, c.limit(), b.position()+4);567ck(c, b.subSequence(1, 4).toString().equals("def"));568569// 4938424570b.position(4);571ck(b, b.charAt(1), 'f');572ck(b, b.subSequence(1, 3).toString().equals("fg"));573574// String ops575576// 7190219577b.clear();578int pos = b.position();579tryCatch(b, BufferOverflowException.class, () ->580b.put(String.valueOf(new char[b.capacity() + 1]), 0, b.capacity() + 1)581);582ck(b, b.position(), pos);583relGet(b);584585586587// 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((char)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(), (char)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, (char[])null, 0, 42));654655char[] tmpa = new char[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((char)0);679b.put((char)-1);680b.put((char)1);681b.put(Character.MAX_VALUE);682b.put(Character.MIN_VALUE);683684685686687688689690691692693694695696697698699700b.flip();701ck(b, b.get(), 0);702ck(b, b.get(), (char)-1);703ck(b, b.get(), 1);704ck(b, b.get(), Character.MAX_VALUE);705ck(b, b.get(), Character.MIN_VALUE);706707708709710711712713714715716717718719720721722723724725726727728729730731732733// Comparison734b.rewind();735CharBuffer b2 = CharBuffer.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++) {742char x = b.get(i);743char 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((char)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, (char)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 (char x : VALUES) {779CharBuffer xb = CharBuffer.wrap(new char[] { 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 (char y : VALUES) {787CharBuffer yb = CharBuffer.wrap(new char[] { 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) != Character.compare(x, y)) {797798799800801802803fail("Incorrect results for CharBuffer.compareTo",804xb, yb, x, y);805}806if (xb.equals(yb) != ((x == y) || ((x != x) && (y != y)))) {807fail("Incorrect results for CharBuffer.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);826CharBuffer sb = b.slice();827checkSlice(b, sb);828b.position(0);829CharBuffer 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);844CharBuffer rsb = b.slice();845b.position(0);846b.limit(b.capacity());847CharBuffer asb = b.slice(7, 35);848checkSlice(rsb, asb);849850b.position(bPos);851b.limit(bLim);852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884// Read-only views885886b.rewind();887final CharBuffer 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(CharBuffer) should not change source position899final CharBuffer src = CharBuffer.allocate(1);900catchReadOnlyBuffer(b, () -> rb.put(src));901ck(src, src.position(), 0);902903catchReadOnlyBuffer(b, () -> rb.compact());904905906907908909910911912913914915916917918919920921922923924// 7199551925catchReadOnlyBuffer(b, () -> rb.put(new String(new char[rb.remaining() + 1])));926catchReadOnlyBuffer(b, () -> rb.append(new String(new char[rb.remaining() + 1])));927928929930if (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}962963964965private static void testStr() {966final String s = "abcdefghijklm";967int start = 3;968int end = 9;969final CharBuffer b = CharBuffer.wrap(s, start, end);970show(0, b);971ck(b, b.toString().equals(s.substring(start, end)));972ck(b, b.toString().equals("defghi"));973ck(b, b.isReadOnly());974catchReadOnlyBuffer(b, () -> b.put('x'));975ck(b, start, b.position());976ck(b, end, b.limit());977ck(b, s.length(), b.capacity());978b.position(6);979ck(b, b.subSequence(0,3).toString().equals("ghi"));980981// absolute bulk get982char[] c = new char[end + 1 - (start - 1) + 1]; // [start - 1, end + 1]983b.limit(end + 2);984b.get(start - 1, c, 0, c.length);985for (int i = 0; i < c.length; i++)986ck(b, c[i], s.charAt(start - 1 + i));987988// The index, relative to the position, must be non-negative and989// smaller than remaining().990catchIndexOutOfBounds(b, () -> b.charAt(-1));991catchIndexOutOfBounds(b, () -> b.charAt(b.remaining()));992// The index must be non-negative and less than the buffer's limit.993catchIndexOutOfBounds(b, () -> b.get(b.limit()));994catchIndexOutOfBounds(b, () -> b.get(-1));995// The start must be non-negative and no larger than remaining().996catchIndexOutOfBounds(b, () -> b.subSequence(-1, b.remaining()));997catchIndexOutOfBounds(b, () -> b.subSequence(b.remaining() + 1, b.remaining()));998999// The end must be no smaller than start and no larger than1000// remaining().1001catchIndexOutOfBounds(b, () -> b.subSequence(2, 1));1002catchIndexOutOfBounds(b, () -> b.subSequence(0, b.remaining() + 1));10031004// The offset must be non-negative and no larger than <array.length>.1005catchIndexOutOfBounds(b, () -> CharBuffer.wrap(s, -1, s.length()));1006catchIndexOutOfBounds(b, () -> CharBuffer.wrap(s, s.length() + 1, s.length()));1007catchIndexOutOfBounds(b, () -> CharBuffer.wrap(s, 1, 0));1008catchIndexOutOfBounds(b, () -> CharBuffer.wrap(s, 0, s.length() + 1));1009}1010101110121013public static void test(final char [] ba) {1014int offset = 47;1015int length = 900;1016final CharBuffer b = CharBuffer.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, () -> CharBuffer.wrap(ba, -1, ba.length));1024catchIndexOutOfBounds(ba, () -> CharBuffer.wrap(ba, ba.length + 1, ba.length));1025catchIndexOutOfBounds(ba, () -> CharBuffer.wrap(ba, 0, -1));1026catchIndexOutOfBounds(ba, () -> CharBuffer.wrap(ba, 0, ba.length + 1));10271028// A NullPointerException will be thrown if the array is null.1029tryCatch(ba, NullPointerException.class,1030() -> CharBuffer.wrap((char []) null, 0, 5));1031tryCatch(ba, NullPointerException.class,1032() -> CharBuffer.wrap((char []) null));1033}10341035private static void testAllocate() {1036// An IllegalArgumentException will be thrown for negative capacities.1037catchIllegalArgument((Buffer) null, () -> CharBuffer.allocate(-1));1038try {1039CharBuffer.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, CharBuffer.allocate(7 * 1024), false);1062test(0, CharBuffer.wrap(new char[7 * 1024], 0, 7 * 1024), false);1063test(new char[1024]);10641065106610671068106910701071testStr();107210731074callReset(CharBuffer.allocate(10));1075107610771078putBuffer();10791080}10811082}108310841085