Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/nio/Buffer/BasicChar.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 BasicChar37extends Basic38{3940private static final char[] VALUES = {41Character.MIN_VALUE,42(char) -1,43(char) 0,44(char) 1,45Character.MAX_VALUE,46474849505152535455565758};5960private static void relGet(CharBuffer b) {61int n = b.capacity();62char v;63for (int i = 0; i < n; i++)64ck(b, (long)b.get(), (long)((char)ic(i)));65b.rewind();66}6768private static void relGet(CharBuffer b, int start) {69int n = b.remaining();70char v;71for (int i = start; i < n; i++)72ck(b, (long)b.get(), (long)((char)ic(i)));73b.rewind();74}7576private static void absGet(CharBuffer b) {77int n = b.capacity();78char v;79for (int i = 0; i < n; i++)80ck(b, (long)b.get(), (long)((char)ic(i)));81b.rewind();82}8384private static void bulkGet(CharBuffer b) {85int n = b.capacity();86char[] a = new char[n + 7];87b.get(a, 7, n);88for (int i = 0; i < n; i++)89ck(b, (long)a[i + 7], (long)((char)ic(i)));90}9192private static void relPut(CharBuffer b) {93int n = b.capacity();94b.clear();95for (int i = 0; i < n; i++)96b.put((char)ic(i));97b.flip();98}99100private static void absPut(CharBuffer b) {101int n = b.capacity();102b.clear();103for (int i = 0; i < n; i++)104b.put(i, (char)ic(i));105b.limit(n);106b.position(0);107}108109private static void bulkPutArray(CharBuffer b) {110int n = b.capacity();111b.clear();112char[] a = new char[n + 7];113for (int i = 0; i < n; i++)114a[i + 7] = (char)ic(i);115b.put(a, 7, n);116b.flip();117}118119private static void bulkPutBuffer(CharBuffer b) {120int n = b.capacity();121b.clear();122CharBuffer c = CharBuffer.allocate(n + 7);123c.position(7);124for (int i = 0; i < n; i++)125c.put((char)ic(i));126c.flip();127c.position(7);128b.put(c);129b.flip();130}131132//6231529133private static void callReset(CharBuffer b) {134b.position(0);135b.mark();136137b.duplicate().reset();138b.asReadOnlyBuffer().reset();139}140141142143// 6221101-6234263144145private static void putBuffer() {146final int cap = 10;147148CharBuffer direct1 = ByteBuffer.allocateDirect(cap).asCharBuffer();149CharBuffer nondirect1 = ByteBuffer.allocate(cap).asCharBuffer();150direct1.put(nondirect1);151152CharBuffer direct2 = ByteBuffer.allocateDirect(cap).asCharBuffer();153CharBuffer nondirect2 = ByteBuffer.allocate(cap).asCharBuffer();154nondirect2.put(direct2);155156CharBuffer direct3 = ByteBuffer.allocateDirect(cap).asCharBuffer();157CharBuffer direct4 = ByteBuffer.allocateDirect(cap).asCharBuffer();158direct3.put(direct4);159160CharBuffer nondirect3 = ByteBuffer.allocate(cap).asCharBuffer();161CharBuffer nondirect4 = ByteBuffer.allocate(cap).asCharBuffer();162nondirect3.put(nondirect4);163}164165166167168private static void bulkPutString(CharBuffer b) {169int n = b.capacity();170b.clear();171StringBuffer sb = new StringBuffer(n + 7);172sb.append("1234567");173for (int i = 0; i < n; i++)174sb.append((char)ic(i));175b.put(sb.toString(), 7, 7 + n);176b.flip();177}178179180181private static void checkSlice(CharBuffer b, CharBuffer 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,332CharBuffer xb, CharBuffer yb,333char x, char 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(char [] t, Class<?> ex, Runnable thunk) {353tryCatch(CharBuffer.wrap(t), ex, thunk);354}355356public static void test(int level, final CharBuffer 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);380381382383bulkPutString(b);384relGet(b);385b.position(1);386b.limit(7);387ck(b, b.toString().equals("bcdefg"));388389// CharSequence ops390391b.position(2);392ck(b, b.charAt(1), 'd');393CharBuffer c = b.subSequence(1, 4);394ck(c, c.capacity(), b.capacity());395ck(c, c.position(), b.position()+1);396ck(c, c.limit(), b.position()+4);397ck(c, b.subSequence(1, 4).toString().equals("def"));398399// 4938424400b.position(4);401ck(b, b.charAt(1), 'f');402ck(b, b.subSequence(1, 3).toString().equals("fg"));403404// String ops405406// 7190219407b.clear();408int pos = b.position();409tryCatch(b, BufferOverflowException.class, new Runnable() {410public void run() {411b.put(String.valueOf(new char[b.capacity() + 1]), 0,412b.capacity() + 1);413}});414ck(b, b.position(), pos);415relGet(b);416417418419// 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((char)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(), (char)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((char)0);470b.put((char)-1);471b.put((char)1);472b.put(Character.MAX_VALUE);473b.put(Character.MIN_VALUE);474475476477478479480481482483484485486487488489490491char v;492b.flip();493ck(b, b.get(), 0);494ck(b, b.get(), (char)-1);495ck(b, b.get(), 1);496ck(b, b.get(), Character.MAX_VALUE);497ck(b, b.get(), Character.MIN_VALUE);498499500501502503504505506507508509510511512513514515516517518519520// Comparison521b.rewind();522CharBuffer b2 = CharBuffer.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++) {529char x = b.get(i);530char 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((char)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, (char)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 (char x : VALUES) {565CharBuffer xb = CharBuffer.wrap(new char[] { 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 (char y : VALUES) {573CharBuffer yb = CharBuffer.wrap(new char[] { 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) != Character.compare(x, y)) {583584585586587588589fail("Incorrect results for CharBuffer.compareTo",590xb, yb, x, y);591}592if (xb.equals(yb) != ((x == y) || ((x != x) && (y != y)))) {593fail("Incorrect results for CharBuffer.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);612CharBuffer sb = b.slice();613checkSlice(b, sb);614b.position(0);615CharBuffer 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 CharBuffer 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(CharBuffer) should not change source position684final CharBuffer src = CharBuffer.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}});695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756// 7199551757tryCatch(b, ReadOnlyBufferException.class, new Runnable() {758public void run() {759String s = new String(new char[rb.remaining() + 1]);760rb.put(s);761}});762tryCatch(b, ReadOnlyBufferException.class, new Runnable() {763public void run() {764String s = new String(new char[rb.remaining() + 1]);765rb.append(s);766}});767768769770if (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}807808809810private static void testStr() {811final String s = "abcdefghijklm";812int start = 3;813int end = 9;814final CharBuffer b = CharBuffer.wrap(s, start, end);815show(0, b);816ck(b, b.toString().equals(s.substring(start, end)));817ck(b, b.toString().equals("defghi"));818ck(b, b.isReadOnly());819tryCatch(b, ReadOnlyBufferException.class, new Runnable() {820public void run() {821b.put('x');822}});823ck(b, start, b.position());824ck(b, end, b.limit());825ck(b, s.length(), b.capacity());826b.position(6);827ck(b, b.subSequence(0,3).toString().equals("ghi"));828829// The index, relative to the position, must be non-negative and830// smaller than remaining().831tryCatch(b, IndexOutOfBoundsException.class, new Runnable() {832public void run() {833b.charAt(-1);834}});835tryCatch(b, IndexOutOfBoundsException.class, new Runnable() {836public void run() {837b.charAt(b.remaining());838}});839840// The index must be non-negative and less than the buffer's limit.841tryCatch(b, IndexOutOfBoundsException.class, new Runnable() {842public void run() {843b.get(b.limit());844}});845tryCatch(b, IndexOutOfBoundsException.class, new Runnable() {846public void run() {847b.get(-1);848}});849850// The start must be non-negative and no larger than remaining().851tryCatch(b, IndexOutOfBoundsException.class, new Runnable() {852public void run() {853b.subSequence(-1, b.remaining());854}});855tryCatch(b, IndexOutOfBoundsException.class, new Runnable() {856public void run() {857b.subSequence(b.remaining() + 1, b.remaining());858}});859860// The end must be no smaller than start and no larger than861// remaining().862tryCatch(b, IndexOutOfBoundsException.class, new Runnable() {863public void run() {864b.subSequence(2, 1);865}});866tryCatch(b, IndexOutOfBoundsException.class, new Runnable() {867public void run() {868b.subSequence(0, b.remaining() + 1);869}});870871// The offset must be non-negative and no larger than <array.length>.872tryCatch(b, IndexOutOfBoundsException.class, new Runnable() {873public void run() {874CharBuffer.wrap(s, -1, s.length());875}});876tryCatch(b, IndexOutOfBoundsException.class, new Runnable() {877public void run() {878CharBuffer.wrap(s, s.length() + 1, s.length());879}});880tryCatch(b, IndexOutOfBoundsException.class, new Runnable() {881public void run() {882CharBuffer.wrap(s, 1, 0);883}});884tryCatch(b, IndexOutOfBoundsException.class, new Runnable() {885public void run() {886CharBuffer.wrap(s, 0, s.length() + 1);887}});888}889890891892public static void test(final char [] ba) {893int offset = 47;894int length = 900;895final CharBuffer b = CharBuffer.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() {904CharBuffer.wrap(ba, -1, ba.length);905}});906tryCatch(ba, IndexOutOfBoundsException.class, new Runnable() {907public void run() {908CharBuffer.wrap(ba, ba.length + 1, ba.length);909}});910tryCatch(ba, IndexOutOfBoundsException.class, new Runnable() {911public void run() {912CharBuffer.wrap(ba, 0, -1);913}});914tryCatch(ba, IndexOutOfBoundsException.class, new Runnable() {915public void run() {916CharBuffer.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() {922CharBuffer.wrap((char []) null, 0, 5);923}});924tryCatch(ba, NullPointerException.class, new Runnable() {925public void run() {926CharBuffer.wrap((char []) 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() {934CharBuffer.allocate(-1);935}});936937938939940941942}943944public static void test() {945testAllocate();946test(0, CharBuffer.allocate(7 * 1024), false);947test(0, CharBuffer.wrap(new char[7 * 1024], 0, 7 * 1024), false);948test(new char[1024]);949950951952953954955956testStr();957958959callReset(CharBuffer.allocate(10));960961962963putBuffer();964965}966967}968969970