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