Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/nio/Buffer/Basic.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/* @test24* @summary Unit test for buffers25* @bug 4413135 4414911 4416536 4416562 4418782 4471053 4472779 4490253 452372526* 4526177 4463011 4660660 4661219 4663521 4782970 4804304 4938424 623152927* 6221101 6234263 6535542 6591971 6593946 6795561 7190219 719955128* @author Mark Reinhold29*/303132import java.io.PrintStream;3334import java.nio.Buffer;35import java.nio.ByteBuffer;36import java.nio.CharBuffer;373839public class Basic {4041static PrintStream out = System.err;4243static long ic(int i) {44int j = i % 54;45return j + 'a' + ((j > 26) ? 128 : 0);46}4748static String toString(Buffer b) {49return (b.getClass().getName()50+ "[pos=" + b.position()51+ " lim=" + b.limit()52+ " cap=" + b.capacity()53+ "]");54}5556static void show(int level, Buffer b) {57for (int i = 0; i < level; i++)58out.print(" ");59out.println(toString(b) + " " + Integer.toHexString(b.hashCode()));60}6162static void fail(String s) {63throw new RuntimeException(s);64}6566static void fail(String s, Buffer b) {67throw new RuntimeException(s + ": " + toString(b));68}6970static void fail(String s, Buffer b, Buffer b2) {71throw new RuntimeException(s + ": "72+ toString(b) + ", " + toString(b2));73}7475static void fail(Buffer b,76String expected, char expectedChar,77String got, char gotChar)78{79if (b instanceof ByteBuffer) {80ByteBuffer bb = (ByteBuffer)b;81int n = Math.min(16, bb.limit());82for (int i = 0; i < n; i++)83out.print(" " + Integer.toHexString(bb.get(i) & 0xff));84out.println();85}86if (b instanceof CharBuffer) {87CharBuffer bb = (CharBuffer)b;88int n = Math.min(16, bb.limit());89for (int i = 0; i < n; i++)90out.print(" " + Integer.toHexString(bb.get(i) & 0xffff));91out.println();92}93throw new RuntimeException(toString(b)94+ ": Expected '" + expectedChar + "'=0x"95+ expected96+ ", got '" + gotChar + "'=0x"97+ got);98}99100static void fail(Buffer b, long expected, long got) {101fail(b,102Long.toHexString(expected), (char)expected,103Long.toHexString(got), (char)got);104}105106static void ck(Buffer b, boolean cond) {107if (!cond)108fail("Condition failed", b);109}110111static void ck(Buffer b, long got, long expected) {112if (expected != got)113fail(b, expected, got);114}115116static void ck(Buffer b, float got, float expected) {117if (expected != got)118fail(b,119Float.toString(expected), (char)expected,120Float.toString(got), (char)got);121}122123static void ck(Buffer b, double got, double expected) {124if (expected != got)125fail(b,126Double.toString(expected), (char)expected,127Double.toString(got), (char)got);128}129130public static void main(String[] args) {131BasicByte.test();132BasicChar.test();133BasicShort.test();134BasicInt.test();135BasicLong.test();136BasicFloat.test();137BasicDouble.test();138}139140}141142143