1 /* 2 * Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* @test 25 * @summary Unit test for buffers 26 * @bug 4413135 4414911 4416536 4416562 4418782 4471053 4472779 4490253 4523725 27 * 4526177 4463011 4660660 4661219 4663521 4782970 4804304 4938424 5029431 28 * 5071718 6231529 6221101 6234263 6535542 6591971 6593946 6795561 7190219 29 * 7199551 8065556 8149469 8230665 8237514 30 * @modules java.base/java.nio:open 31 * java.base/jdk.internal.misc 32 * @author Mark Reinhold 33 */ 34 package test.java.nio.Buffer; 35 36 import java.io.OutputStream; 37 import java.io.PrintStream; 38 39 import java.nio.Buffer; 40 import java.nio.ByteBuffer; 41 import java.nio.CharBuffer; 42 import org.testng.annotations.Test; 43 44 45 public class Basic { 46 47 // Android-changed: Discard test messages to avoid spamming the logcat. 48 // static PrintStream out = System.err; 49 private static final boolean DEBUG = false; 50 static PrintStream out = DEBUG ? System.err : new PrintStream(OutputStream.nullOutputStream()); 51 ic(int i)52 static long ic(int i) { 53 int j = i % 54; 54 return j + 'a' + ((j > 26) ? 128 : 0); 55 } 56 toString(Buffer b)57 static String toString(Buffer b) { 58 return (b.getClass().getName() 59 + "[pos=" + b.position() 60 + " lim=" + b.limit() 61 + " cap=" + b.capacity() 62 + "]"); 63 } 64 show(int level, Buffer b)65 static void show(int level, Buffer b) { 66 for (int i = 0; i < level; i++) 67 out.print(" "); 68 out.println(toString(b) + " " + Integer.toHexString(b.hashCode())); 69 } 70 fail(String s)71 static void fail(String s) { 72 throw new RuntimeException(s); 73 } 74 fail(String s, Buffer b)75 static void fail(String s, Buffer b) { 76 throw new RuntimeException(s + ": " + toString(b)); 77 } 78 fail(String s, Buffer b, Buffer b2)79 static void fail(String s, Buffer b, Buffer b2) { 80 throw new RuntimeException(s + ": " 81 + toString(b) + ", " + toString(b2)); 82 } 83 fail(Buffer b, String expected, char expectedChar, String got, char gotChar)84 static void fail(Buffer b, 85 String expected, char expectedChar, 86 String got, char gotChar) 87 { 88 if (b instanceof ByteBuffer) { 89 ByteBuffer bb = (ByteBuffer)b; 90 int n = Math.min(16, bb.limit()); 91 for (int i = 0; i < n; i++) 92 out.print(" " + Integer.toHexString(bb.get(i) & 0xff)); 93 out.println(); 94 } 95 if (b instanceof CharBuffer) { 96 CharBuffer bb = (CharBuffer)b; 97 int n = Math.min(16, bb.limit()); 98 for (int i = 0; i < n; i++) 99 out.print(" " + Integer.toHexString(bb.get(i) & 0xffff)); 100 out.println(); 101 } 102 throw new RuntimeException(toString(b) 103 + ": Expected '" + expectedChar + "'=0x" 104 + expected 105 + ", got '" + gotChar + "'=0x" 106 + got); 107 } 108 fail(Buffer b, long expected, long got)109 static void fail(Buffer b, long expected, long got) { 110 fail(b, 111 Long.toHexString(expected), (char)expected, 112 Long.toHexString(got), (char)got); 113 } 114 ck(Buffer b, boolean cond)115 static void ck(Buffer b, boolean cond) { 116 if (!cond) 117 fail("Condition failed", b); 118 } 119 ck(Buffer b, long got, long expected)120 static void ck(Buffer b, long got, long expected) { 121 if (expected != got) 122 fail(b, expected, got); 123 } 124 ck(Buffer b, float got, float expected)125 static void ck(Buffer b, float got, float expected) { 126 if (expected != got) 127 fail(b, 128 Float.toString(expected), (char)expected, 129 Float.toString(got), (char)got); 130 } 131 ck(Buffer b, double got, double expected)132 static void ck(Buffer b, double got, double expected) { 133 if (expected != got) 134 fail(b, 135 Double.toString(expected), (char)expected, 136 Double.toString(got), (char)got); 137 } 138 139 // Android-change: Use TestNg. 140 // public static void main(String[] args) { 141 @Test testAll()142 public void testAll() { 143 BasicByte.test(); 144 BasicChar.test(); 145 BasicShort.test(); 146 BasicInt.test(); 147 BasicLong.test(); 148 BasicFloat.test(); 149 BasicDouble.test(); 150 } 151 152 }