1/*
2 * Copyright (c) 2000, 2016, 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 6231529
28 *      6221101 6234263 6535542 6591971 6593946 6795561 7190219 7199551 8065556
29 *      8149469
30 * @modules java.base/java.nio:open
31 *          java.base/jdk.internal.misc
32 * @author Mark Reinhold
33 */
34
35
36import java.io.PrintStream;
37
38import java.nio.Buffer;
39import java.nio.ByteBuffer;
40import java.nio.CharBuffer;
41
42
43public class Basic {
44
45    static PrintStream out = System.err;
46
47    static long ic(int i) {
48        int j = i % 54;
49        return j + 'a' + ((j > 26) ? 128 : 0);
50    }
51
52    static String toString(Buffer b) {
53        return (b.getClass().getName()
54                + "[pos=" + b.position()
55                + " lim=" + b.limit()
56                + " cap=" + b.capacity()
57                + "]");
58    }
59
60    static void show(int level, Buffer b) {
61        for (int i = 0; i < level; i++)
62            out.print("  ");
63        out.println(toString(b) + " " + Integer.toHexString(b.hashCode()));
64    }
65
66    static void fail(String s) {
67        throw new RuntimeException(s);
68    }
69
70    static void fail(String s, Buffer b) {
71        throw new RuntimeException(s + ": " + toString(b));
72    }
73
74    static void fail(String s, Buffer b, Buffer b2) {
75        throw new RuntimeException(s + ": "
76                                   + toString(b) + ", " + toString(b2));
77    }
78
79    static void fail(Buffer b,
80                     String expected, char expectedChar,
81                     String got, char gotChar)
82    {
83        if (b instanceof ByteBuffer) {
84            ByteBuffer bb = (ByteBuffer)b;
85            int n = Math.min(16, bb.limit());
86            for (int i = 0; i < n; i++)
87                out.print(" " + Integer.toHexString(bb.get(i) & 0xff));
88            out.println();
89        }
90        if (b instanceof CharBuffer) {
91            CharBuffer bb = (CharBuffer)b;
92            int n = Math.min(16, bb.limit());
93            for (int i = 0; i < n; i++)
94                out.print(" " + Integer.toHexString(bb.get(i) & 0xffff));
95            out.println();
96        }
97        throw new RuntimeException(toString(b)
98                                   + ": Expected '" + expectedChar + "'=0x"
99                                   + expected
100                                   + ", got '" + gotChar + "'=0x"
101                                   + got);
102    }
103
104    static void fail(Buffer b, long expected, long got) {
105        fail(b,
106             Long.toHexString(expected), (char)expected,
107             Long.toHexString(got), (char)got);
108    }
109
110    static void ck(Buffer b, boolean cond) {
111        if (!cond)
112            fail("Condition failed", b);
113    }
114
115    static void ck(Buffer b, long got, long expected) {
116        if (expected != got)
117            fail(b, expected, got);
118    }
119
120    static void ck(Buffer b, float got, float expected) {
121        if (expected != got)
122            fail(b,
123                 Float.toString(expected), (char)expected,
124                 Float.toString(got), (char)got);
125    }
126
127    static void ck(Buffer b, double got, double expected) {
128        if (expected != got)
129            fail(b,
130                 Double.toString(expected), (char)expected,
131                 Double.toString(got), (char)got);
132    }
133
134    public static void main(String[] args) {
135        BasicByte.test();
136        BasicChar.test();
137        BasicShort.test();
138        BasicInt.test();
139        BasicLong.test();
140        BasicFloat.test();
141        BasicDouble.test();
142    }
143
144}
145