Path: blob/master/test/jdk/java/nio/Buffer/Basic.java
41149 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/* @test24* @summary Unit test for buffers25* @bug 4413135 4414911 4416536 4416562 4418782 4471053 4472779 4490253 452372526* 4526177 4463011 4660660 4661219 4663521 4782970 4804304 4938424 502943127* 5071718 6231529 6221101 6234263 6535542 6591971 6593946 6795561 719021928* 7199551 8065556 8149469 8230665 823751429* @modules java.base/java.nio:open30* java.base/jdk.internal.misc31* @author Mark Reinhold32*/333435import java.io.PrintStream;3637import java.nio.Buffer;38import java.nio.ByteBuffer;39import java.nio.CharBuffer;404142public class Basic {4344static PrintStream out = System.err;4546static long ic(int i) {47int j = i % 54;48return j + 'a' + ((j > 26) ? 128 : 0);49}5051static String toString(Buffer b) {52return (b.getClass().getName()53+ "[pos=" + b.position()54+ " lim=" + b.limit()55+ " cap=" + b.capacity()56+ "]");57}5859static void show(int level, Buffer b) {60for (int i = 0; i < level; i++)61out.print(" ");62out.println(toString(b) + " " + Integer.toHexString(b.hashCode()));63}6465static void fail(String s) {66throw new RuntimeException(s);67}6869static void fail(String s, Buffer b) {70throw new RuntimeException(s + ": " + toString(b));71}7273static void fail(String s, Buffer b, Buffer b2) {74throw new RuntimeException(s + ": "75+ toString(b) + ", " + toString(b2));76}7778static void fail(Buffer b,79String expected, char expectedChar,80String got, char gotChar)81{82if (b instanceof ByteBuffer) {83ByteBuffer bb = (ByteBuffer)b;84int n = Math.min(16, bb.limit());85for (int i = 0; i < n; i++)86out.print(" " + Integer.toHexString(bb.get(i) & 0xff));87out.println();88}89if (b instanceof CharBuffer) {90CharBuffer bb = (CharBuffer)b;91int n = Math.min(16, bb.limit());92for (int i = 0; i < n; i++)93out.print(" " + Integer.toHexString(bb.get(i) & 0xffff));94out.println();95}96throw new RuntimeException(toString(b)97+ ": Expected '" + expectedChar + "'=0x"98+ expected99+ ", got '" + gotChar + "'=0x"100+ got);101}102103static void fail(Buffer b, long expected, long got) {104fail(b,105Long.toHexString(expected), (char)expected,106Long.toHexString(got), (char)got);107}108109static void ck(Buffer b, boolean cond) {110if (!cond)111fail("Condition failed", b);112}113114static void ck(Buffer b, long got, long expected) {115if (expected != got)116fail(b, expected, got);117}118119static void ck(Buffer b, float got, float expected) {120if (expected != got)121fail(b,122Float.toString(expected), (char)expected,123Float.toString(got), (char)got);124}125126static void ck(Buffer b, double got, double expected) {127if (expected != got)128fail(b,129Double.toString(expected), (char)expected,130Double.toString(got), (char)got);131}132133public static void main(String[] args) {134BasicByte.test();135BasicChar.test();136BasicShort.test();137BasicInt.test();138BasicLong.test();139BasicFloat.test();140BasicDouble.test();141}142143}144145146