test/jdk/java/nio/Buffer/Basic-X.java.template
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: 4502
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4502
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4502
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
/* Type-specific source code for unit test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * Regenerate the BasicX classes via genBasic.sh whenever this file changes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * We check in the generated source files so that the test tree can be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * independently of the rest of the source tree.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
#warn This file is preprocessed before being compiled
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.nio.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
public class Basic$Type$
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    extends Basic
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
4502
18f387917b89 6903754: (bf) Improve floating-point buffer comparison
martin
parents: 4115
diff changeset
    40
    private static final $type$[] VALUES = {
18f387917b89 6903754: (bf) Improve floating-point buffer comparison
martin
parents: 4115
diff changeset
    41
        $Fulltype$.MIN_VALUE,
18f387917b89 6903754: (bf) Improve floating-point buffer comparison
martin
parents: 4115
diff changeset
    42
        ($type$) -1,
18f387917b89 6903754: (bf) Improve floating-point buffer comparison
martin
parents: 4115
diff changeset
    43
        ($type$) 0,
18f387917b89 6903754: (bf) Improve floating-point buffer comparison
martin
parents: 4115
diff changeset
    44
        ($type$) 1,
18f387917b89 6903754: (bf) Improve floating-point buffer comparison
martin
parents: 4115
diff changeset
    45
        $Fulltype$.MAX_VALUE,
18f387917b89 6903754: (bf) Improve floating-point buffer comparison
martin
parents: 4115
diff changeset
    46
#if[float]
18f387917b89 6903754: (bf) Improve floating-point buffer comparison
martin
parents: 4115
diff changeset
    47
        $Fulltype$.NEGATIVE_INFINITY,
18f387917b89 6903754: (bf) Improve floating-point buffer comparison
martin
parents: 4115
diff changeset
    48
        $Fulltype$.POSITIVE_INFINITY,
18f387917b89 6903754: (bf) Improve floating-point buffer comparison
martin
parents: 4115
diff changeset
    49
        $Fulltype$.NaN,
18f387917b89 6903754: (bf) Improve floating-point buffer comparison
martin
parents: 4115
diff changeset
    50
        ($type$) -0.0,
18f387917b89 6903754: (bf) Improve floating-point buffer comparison
martin
parents: 4115
diff changeset
    51
#end[float]
18f387917b89 6903754: (bf) Improve floating-point buffer comparison
martin
parents: 4115
diff changeset
    52
#if[double]
18f387917b89 6903754: (bf) Improve floating-point buffer comparison
martin
parents: 4115
diff changeset
    53
        $Fulltype$.NEGATIVE_INFINITY,
18f387917b89 6903754: (bf) Improve floating-point buffer comparison
martin
parents: 4115
diff changeset
    54
        $Fulltype$.POSITIVE_INFINITY,
18f387917b89 6903754: (bf) Improve floating-point buffer comparison
martin
parents: 4115
diff changeset
    55
        $Fulltype$.NaN,
18f387917b89 6903754: (bf) Improve floating-point buffer comparison
martin
parents: 4115
diff changeset
    56
        ($type$) -0.0,
18f387917b89 6903754: (bf) Improve floating-point buffer comparison
martin
parents: 4115
diff changeset
    57
#end[double]
18f387917b89 6903754: (bf) Improve floating-point buffer comparison
martin
parents: 4115
diff changeset
    58
    };
18f387917b89 6903754: (bf) Improve floating-point buffer comparison
martin
parents: 4115
diff changeset
    59
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private static void relGet($Type$Buffer b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        int n = b.capacity();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        for (int i = 0; i < n; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            ck(b, (long)b.get(), (long)(($type$)ic(i)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        b.rewind();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private static void relGet($Type$Buffer b, int start) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        int n = b.remaining();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        for (int i = start; i < n; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            ck(b, (long)b.get(), (long)(($type$)ic(i)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        b.rewind();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private static void absGet($Type$Buffer b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        int n = b.capacity();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        for (int i = 0; i < n; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            ck(b, (long)b.get(), (long)(($type$)ic(i)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        b.rewind();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private static void bulkGet($Type$Buffer b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        int n = b.capacity();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        $type$[] a = new $type$[n + 7];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        b.get(a, 7, n);
37913
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
    85
        for (int i = 0; i < n; i++) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            ck(b, (long)a[i + 7], (long)(($type$)ic(i)));
37913
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
    87
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
53855
7c362992527a 5029431: (bf) Add absolute bulk put and get methods
bpb
parents: 47216
diff changeset
    90
    private static void absBulkGet($Type$Buffer b) {
7c362992527a 5029431: (bf) Add absolute bulk put and get methods
bpb
parents: 47216
diff changeset
    91
        int n = b.capacity();
7c362992527a 5029431: (bf) Add absolute bulk put and get methods
bpb
parents: 47216
diff changeset
    92
        int len = n - 7*2;
7c362992527a 5029431: (bf) Add absolute bulk put and get methods
bpb
parents: 47216
diff changeset
    93
        $type$[] a = new $type$[n + 7];
7c362992527a 5029431: (bf) Add absolute bulk put and get methods
bpb
parents: 47216
diff changeset
    94
        b.position(42);
7c362992527a 5029431: (bf) Add absolute bulk put and get methods
bpb
parents: 47216
diff changeset
    95
        b.get(7, a, 7, len);
7c362992527a 5029431: (bf) Add absolute bulk put and get methods
bpb
parents: 47216
diff changeset
    96
        ck(b, b.position() == 42);
7c362992527a 5029431: (bf) Add absolute bulk put and get methods
bpb
parents: 47216
diff changeset
    97
        for (int i = 0; i < len; i++) {
7c362992527a 5029431: (bf) Add absolute bulk put and get methods
bpb
parents: 47216
diff changeset
    98
            ck(b, (long)a[i + 7], (long)(($type$)ic(i)));
7c362992527a 5029431: (bf) Add absolute bulk put and get methods
bpb
parents: 47216
diff changeset
    99
        }
7c362992527a 5029431: (bf) Add absolute bulk put and get methods
bpb
parents: 47216
diff changeset
   100
    }
7c362992527a 5029431: (bf) Add absolute bulk put and get methods
bpb
parents: 47216
diff changeset
   101
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    private static void relPut($Type$Buffer b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        int n = b.capacity();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        b.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        for (int i = 0; i < n; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            b.put(($type$)ic(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        b.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    private static void absPut($Type$Buffer b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        int n = b.capacity();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        b.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        for (int i = 0; i < n; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            b.put(i, ($type$)ic(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        b.limit(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        b.position(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    private static void bulkPutArray($Type$Buffer b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        int n = b.capacity();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        b.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        $type$[] a = new $type$[n + 7];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        for (int i = 0; i < n; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            a[i + 7] = ($type$)ic(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        b.put(a, 7, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        b.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    private static void bulkPutBuffer($Type$Buffer b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        int n = b.capacity();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        b.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        $Type$Buffer c = $Type$Buffer.allocate(n + 7);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        c.position(7);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        for (int i = 0; i < n; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            c.put(($type$)ic(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        c.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        c.position(7);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        b.put(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        b.flip();
31873
87b015c2cd36 8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents: 14342
diff changeset
   140
        try {
87b015c2cd36 8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents: 14342
diff changeset
   141
            b.put(b);
87b015c2cd36 8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents: 14342
diff changeset
   142
            fail("IllegalArgumentException expected for put into same buffer");
87b015c2cd36 8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents: 14342
diff changeset
   143
        } catch (IllegalArgumentException e) {
87b015c2cd36 8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents: 14342
diff changeset
   144
            if (e.getMessage() == null) {
87b015c2cd36 8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents: 14342
diff changeset
   145
                fail("Non-null IllegalArgumentException message expected from"
87b015c2cd36 8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents: 14342
diff changeset
   146
                     + " put into same buffer");
87b015c2cd36 8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents: 14342
diff changeset
   147
            }
87b015c2cd36 8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents: 14342
diff changeset
   148
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
53855
7c362992527a 5029431: (bf) Add absolute bulk put and get methods
bpb
parents: 47216
diff changeset
   151
    private static void absBulkPutArray($Type$Buffer b) {
7c362992527a 5029431: (bf) Add absolute bulk put and get methods
bpb
parents: 47216
diff changeset
   152
        int n = b.capacity();
7c362992527a 5029431: (bf) Add absolute bulk put and get methods
bpb
parents: 47216
diff changeset
   153
        b.clear();
7c362992527a 5029431: (bf) Add absolute bulk put and get methods
bpb
parents: 47216
diff changeset
   154
        int lim = n - 7;
7c362992527a 5029431: (bf) Add absolute bulk put and get methods
bpb
parents: 47216
diff changeset
   155
        int len = lim - 7;
7c362992527a 5029431: (bf) Add absolute bulk put and get methods
bpb
parents: 47216
diff changeset
   156
        b.limit(lim);
7c362992527a 5029431: (bf) Add absolute bulk put and get methods
bpb
parents: 47216
diff changeset
   157
        $type$[] a = new $type$[len + 7];
7c362992527a 5029431: (bf) Add absolute bulk put and get methods
bpb
parents: 47216
diff changeset
   158
        for (int i = 0; i < len; i++)
7c362992527a 5029431: (bf) Add absolute bulk put and get methods
bpb
parents: 47216
diff changeset
   159
            a[i + 7] = ($type$)ic(i);
7c362992527a 5029431: (bf) Add absolute bulk put and get methods
bpb
parents: 47216
diff changeset
   160
        b.position(42);
7c362992527a 5029431: (bf) Add absolute bulk put and get methods
bpb
parents: 47216
diff changeset
   161
        b.put(7, a, 7, len);
7c362992527a 5029431: (bf) Add absolute bulk put and get methods
bpb
parents: 47216
diff changeset
   162
        ck(b, b.position() == 42);
7c362992527a 5029431: (bf) Add absolute bulk put and get methods
bpb
parents: 47216
diff changeset
   163
    }
7c362992527a 5029431: (bf) Add absolute bulk put and get methods
bpb
parents: 47216
diff changeset
   164
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    //6231529
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    private static void callReset($Type$Buffer b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        b.position(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        b.mark();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        b.duplicate().reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        b.asReadOnlyBuffer().reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
#if[byte]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
#else[byte]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    // 6221101-6234263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    private static void putBuffer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        final int cap = 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        $Type$Buffer direct1 = ByteBuffer.allocateDirect(cap).as$Type$Buffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        $Type$Buffer nondirect1 = ByteBuffer.allocate(cap).as$Type$Buffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        direct1.put(nondirect1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        $Type$Buffer direct2 = ByteBuffer.allocateDirect(cap).as$Type$Buffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        $Type$Buffer nondirect2 = ByteBuffer.allocate(cap).as$Type$Buffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        nondirect2.put(direct2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        $Type$Buffer direct3 = ByteBuffer.allocateDirect(cap).as$Type$Buffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        $Type$Buffer direct4 = ByteBuffer.allocateDirect(cap).as$Type$Buffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        direct3.put(direct4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        $Type$Buffer nondirect3 = ByteBuffer.allocate(cap).as$Type$Buffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        $Type$Buffer nondirect4 = ByteBuffer.allocate(cap).as$Type$Buffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        nondirect3.put(nondirect4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
#end[byte]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
#if[char]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    private static void bulkPutString($Type$Buffer b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        int n = b.capacity();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        b.clear();
37913
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   204
        StringBuilder sb = new StringBuilder(n + 7);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        sb.append("1234567");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        for (int i = 0; i < n; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            sb.append((char)ic(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        b.put(sb.toString(), 7, 7 + n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        b.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
#end[char]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    private static void checkSlice($Type$Buffer b, $Type$Buffer slice) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        ck(slice, 0, slice.position());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        ck(slice, b.remaining(), slice.limit());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        ck(slice, b.remaining(), slice.capacity());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        if (b.isDirect() != slice.isDirect())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            fail("Lost direction", slice);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        if (b.isReadOnly() != slice.isReadOnly())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            fail("Lost read-only", slice);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
#if[byte]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    private static void checkBytes(ByteBuffer b, byte[] bs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        int n = bs.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        int p = b.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        if (b.order() == ByteOrder.BIG_ENDIAN) {
37913
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   230
            for (int i = 0; i < n; i++) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                ck(b, b.get(), bs[i]);
37913
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   232
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        } else {
37913
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   234
            for (int i = n - 1; i >= 0; i--) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                ck(b, b.get(), bs[i]);
37913
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   236
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        b.position(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
1634
3871c2046043 6593946: (bf) X-Buffer.compact() does not discard mark as specified
alanb
parents: 2
diff changeset
   241
    private static void compact(Buffer b) {
3871c2046043 6593946: (bf) X-Buffer.compact() does not discard mark as specified
alanb
parents: 2
diff changeset
   242
        try {
3871c2046043 6593946: (bf) X-Buffer.compact() does not discard mark as specified
alanb
parents: 2
diff changeset
   243
            Class<?> cl = b.getClass();
37913
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   244
            java.lang.reflect.Method m = cl.getDeclaredMethod("compact");
1634
3871c2046043 6593946: (bf) X-Buffer.compact() does not discard mark as specified
alanb
parents: 2
diff changeset
   245
            m.setAccessible(true);
3871c2046043 6593946: (bf) X-Buffer.compact() does not discard mark as specified
alanb
parents: 2
diff changeset
   246
            m.invoke(b);
3871c2046043 6593946: (bf) X-Buffer.compact() does not discard mark as specified
alanb
parents: 2
diff changeset
   247
        } catch (Exception e) {
3871c2046043 6593946: (bf) X-Buffer.compact() does not discard mark as specified
alanb
parents: 2
diff changeset
   248
            fail(e.getMessage(), b);
3871c2046043 6593946: (bf) X-Buffer.compact() does not discard mark as specified
alanb
parents: 2
diff changeset
   249
        }
3871c2046043 6593946: (bf) X-Buffer.compact() does not discard mark as specified
alanb
parents: 2
diff changeset
   250
    }
3871c2046043 6593946: (bf) X-Buffer.compact() does not discard mark as specified
alanb
parents: 2
diff changeset
   251
3871c2046043 6593946: (bf) X-Buffer.compact() does not discard mark as specified
alanb
parents: 2
diff changeset
   252
    private static void checkInvalidMarkException(final Buffer b) {
37913
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   253
        tryCatch(b, InvalidMarkException.class, () -> {
1634
3871c2046043 6593946: (bf) X-Buffer.compact() does not discard mark as specified
alanb
parents: 2
diff changeset
   254
                b.mark();
3871c2046043 6593946: (bf) X-Buffer.compact() does not discard mark as specified
alanb
parents: 2
diff changeset
   255
                compact(b);
3871c2046043 6593946: (bf) X-Buffer.compact() does not discard mark as specified
alanb
parents: 2
diff changeset
   256
                b.reset();
37913
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   257
            });
1634
3871c2046043 6593946: (bf) X-Buffer.compact() does not discard mark as specified
alanb
parents: 2
diff changeset
   258
    }
3871c2046043 6593946: (bf) X-Buffer.compact() does not discard mark as specified
alanb
parents: 2
diff changeset
   259
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    private static void testViews(int level, ByteBuffer b, boolean direct) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        ShortBuffer sb = b.asShortBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        BasicShort.test(level, sb, direct);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        checkBytes(b, new byte[] { 0, (byte)ic(0) });
1634
3871c2046043 6593946: (bf) X-Buffer.compact() does not discard mark as specified
alanb
parents: 2
diff changeset
   265
        checkInvalidMarkException(sb);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        CharBuffer cb = b.asCharBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        BasicChar.test(level, cb, direct);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        checkBytes(b, new byte[] { 0, (byte)ic(0) });
1634
3871c2046043 6593946: (bf) X-Buffer.compact() does not discard mark as specified
alanb
parents: 2
diff changeset
   270
        checkInvalidMarkException(cb);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        IntBuffer ib = b.asIntBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        BasicInt.test(level, ib, direct);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        checkBytes(b, new byte[] { 0, 0, 0, (byte)ic(0) });
1634
3871c2046043 6593946: (bf) X-Buffer.compact() does not discard mark as specified
alanb
parents: 2
diff changeset
   275
        checkInvalidMarkException(ib);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        LongBuffer lb = b.asLongBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        BasicLong.test(level, lb, direct);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        checkBytes(b, new byte[] { 0, 0, 0, 0, 0, 0, 0, (byte)ic(0) });
1634
3871c2046043 6593946: (bf) X-Buffer.compact() does not discard mark as specified
alanb
parents: 2
diff changeset
   280
        checkInvalidMarkException(lb);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        FloatBuffer fb = b.asFloatBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        BasicFloat.test(level, fb, direct);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        checkBytes(b, new byte[] { 0x42, (byte)0xc2, 0, 0 });
1634
3871c2046043 6593946: (bf) X-Buffer.compact() does not discard mark as specified
alanb
parents: 2
diff changeset
   285
        checkInvalidMarkException(fb);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        DoubleBuffer db = b.asDoubleBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        BasicDouble.test(level, db, direct);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        checkBytes(b, new byte[] { 0x40, 0x58, 0x40, 0, 0, 0, 0, 0 });
1634
3871c2046043 6593946: (bf) X-Buffer.compact() does not discard mark as specified
alanb
parents: 2
diff changeset
   290
        checkInvalidMarkException(db);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    private static void testHet(int level, ByteBuffer b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        int p = b.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        b.limit(b.capacity());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        show(level, b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        out.print("    put:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        b.putChar((char)1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        b.putChar((char)Character.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        out.print(" char");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        b.putShort((short)1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        b.putShort((short)Short.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        out.print(" short");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        b.putInt(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        b.putInt(Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        out.print(" int");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        b.putLong((long)1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        b.putLong((long)Long.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        out.print(" long");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        b.putFloat((float)1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        b.putFloat((float)Float.MIN_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        b.putFloat((float)Float.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        out.print(" float");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        b.putDouble((double)1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        b.putDouble((double)Double.MIN_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        b.putDouble((double)Double.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        out.print(" double");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        b.limit(b.position());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        b.position(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        show(level, b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        out.print("    get:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        ck(b, b.getChar(), 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        ck(b, b.getChar(), Character.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        out.print(" char");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        ck(b, b.getShort(), 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        ck(b, b.getShort(), Short.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        out.print(" short");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        ck(b, b.getInt(), 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        ck(b, b.getInt(), Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        out.print(" int");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        ck(b, b.getLong(), 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        ck(b, b.getLong(), Long.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        out.print(" long");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        ck(b, (long)b.getFloat(), 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        ck(b, (long)b.getFloat(), (long)Float.MIN_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        ck(b, (long)b.getFloat(), (long)Float.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        out.print(" float");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        ck(b, (long)b.getDouble(), 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        ck(b, (long)b.getDouble(), (long)Double.MIN_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        ck(b, (long)b.getDouble(), (long)Double.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        out.print(" double");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
36933
3e6453e2d833 8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents: 31873
diff changeset
   362
    private static void testAlign(final ByteBuffer b, boolean direct) {
3e6453e2d833 8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents: 31873
diff changeset
   363
        // index out-of bounds
37913
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   364
        catchIllegalArgument(b, () -> b.alignmentOffset(-1, (short) 1));
36933
3e6453e2d833 8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents: 31873
diff changeset
   365
3e6453e2d833 8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents: 31873
diff changeset
   366
        // unit size values
37913
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   367
        catchIllegalArgument(b, () -> b.alignmentOffset(0, (short) 0));
36933
3e6453e2d833 8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents: 31873
diff changeset
   368
        for (int us = 1; us < 65; us++) {
3e6453e2d833 8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents: 31873
diff changeset
   369
            int _us = us;
3e6453e2d833 8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents: 31873
diff changeset
   370
            if ((us & (us - 1)) != 0) {
3e6453e2d833 8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents: 31873
diff changeset
   371
                // unit size not a power of two
37913
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   372
                catchIllegalArgument(b, () -> b.alignmentOffset(0, _us));
36933
3e6453e2d833 8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents: 31873
diff changeset
   373
            } else {
3e6453e2d833 8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents: 31873
diff changeset
   374
                if (direct || us <= 8) {
3e6453e2d833 8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents: 31873
diff changeset
   375
                    b.alignmentOffset(0, us);
3e6453e2d833 8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents: 31873
diff changeset
   376
                } else {
3e6453e2d833 8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents: 31873
diff changeset
   377
                    // unit size > 8 with non-direct buffer
37913
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   378
                    tryCatch(b, UnsupportedOperationException.class,
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   379
                            () -> b.alignmentOffset(0, _us));
36933
3e6453e2d833 8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents: 31873
diff changeset
   380
                }
3e6453e2d833 8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents: 31873
diff changeset
   381
            }
3e6453e2d833 8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents: 31873
diff changeset
   382
        }
3e6453e2d833 8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents: 31873
diff changeset
   383
3e6453e2d833 8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents: 31873
diff changeset
   384
        // Probe for long misalignment at index zero for a newly created buffer
37913
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   385
        ByteBuffer empty =
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   386
                direct ? ByteBuffer.allocateDirect(0) : ByteBuffer.allocate(0);
36933
3e6453e2d833 8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents: 31873
diff changeset
   387
        int longMisalignmentAtZero = empty.alignmentOffset(0, 8);
3e6453e2d833 8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents: 31873
diff changeset
   388
3e6453e2d833 8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents: 31873
diff changeset
   389
        if (direct) {
3e6453e2d833 8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents: 31873
diff changeset
   390
            // Freshly created direct byte buffers should be aligned at index 0
3e6453e2d833 8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents: 31873
diff changeset
   391
            // for ref and primitive values (see Unsafe.allocateMemory)
37913
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   392
            if (longMisalignmentAtZero != 0) {
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   393
                fail("Direct byte buffer misaligned at index 0"
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   394
                        + " for ref and primitive values "
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   395
                        + longMisalignmentAtZero);
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   396
            }
36933
3e6453e2d833 8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents: 31873
diff changeset
   397
        } else {
3e6453e2d833 8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents: 31873
diff changeset
   398
            // For heap byte buffers misalignment may occur on 32-bit systems
3e6453e2d833 8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents: 31873
diff changeset
   399
            // where Unsafe.ARRAY_BYTE_BASE_OFFSET % 8 == 4 and not 0
3e6453e2d833 8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents: 31873
diff changeset
   400
            // Note the GC will preserve alignment of the base address of the
3e6453e2d833 8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents: 31873
diff changeset
   401
            // array
37913
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   402
            if (jdk.internal.misc.Unsafe.ARRAY_BYTE_BASE_OFFSET % 8
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   403
                    != longMisalignmentAtZero) {
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   404
                fail("Heap byte buffer misaligned at index 0"
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   405
                        + " for ref and primitive values "
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   406
                        + longMisalignmentAtZero);
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   407
            }
36933
3e6453e2d833 8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents: 31873
diff changeset
   408
        }
3e6453e2d833 8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents: 31873
diff changeset
   409
3e6453e2d833 8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents: 31873
diff changeset
   410
        // Ensure test buffer is correctly aligned at index 0
3e6453e2d833 8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents: 31873
diff changeset
   411
        if (b.alignmentOffset(0, 8) != longMisalignmentAtZero)
3e6453e2d833 8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents: 31873
diff changeset
   412
            fail("Test input buffer not correctly aligned at index 0", b);
3e6453e2d833 8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents: 31873
diff changeset
   413
3e6453e2d833 8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents: 31873
diff changeset
   414
        // Test misalignment values
3e6453e2d833 8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents: 31873
diff changeset
   415
        for (int us : new int[]{1, 2, 4, 8}) {
3e6453e2d833 8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents: 31873
diff changeset
   416
            for (int i = 0; i < us * 2; i++) {
3e6453e2d833 8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents: 31873
diff changeset
   417
                int am = b.alignmentOffset(i, us);
3e6453e2d833 8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents: 31873
diff changeset
   418
                int expectedAm = (longMisalignmentAtZero + i) % us;
3e6453e2d833 8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents: 31873
diff changeset
   419
37913
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   420
                if (am != expectedAm) {
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   421
                    String f = "b.alignmentOffset(%d, %d) == %d incorrect, expected %d";
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   422
                    fail(String.format(f, i, us, am, expectedAm));
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   423
                }
36933
3e6453e2d833 8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents: 31873
diff changeset
   424
            }
3e6453e2d833 8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents: 31873
diff changeset
   425
        }
3e6453e2d833 8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents: 31873
diff changeset
   426
3e6453e2d833 8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents: 31873
diff changeset
   427
        // Created aligned slice to test against
3e6453e2d833 8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents: 31873
diff changeset
   428
        int ap = 8 - longMisalignmentAtZero;
3e6453e2d833 8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents: 31873
diff changeset
   429
        int al = b.limit() - b.alignmentOffset(b.limit(), 8);
3e6453e2d833 8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents: 31873
diff changeset
   430
        ByteBuffer ab = b.position(ap).limit(al).
3e6453e2d833 8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents: 31873
diff changeset
   431
                slice();
37913
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   432
        if (ab.limit() == 0) {
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   433
            fail("Test input buffer not sufficiently sized to cover" +
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   434
                    " an aligned region for all values", b);
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   435
        }
36933
3e6453e2d833 8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents: 31873
diff changeset
   436
        if (ab.alignmentOffset(0, 8) != 0)
3e6453e2d833 8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents: 31873
diff changeset
   437
            fail("Aligned test input buffer not correctly aligned at index 0", ab);
3e6453e2d833 8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents: 31873
diff changeset
   438
3e6453e2d833 8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents: 31873
diff changeset
   439
        for (int us : new int[]{1, 2, 4, 8}) {
3e6453e2d833 8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents: 31873
diff changeset
   440
            for (int p = 1; p < 16; p++) {
3e6453e2d833 8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents: 31873
diff changeset
   441
                int l = ab.limit() - p;
3e6453e2d833 8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents: 31873
diff changeset
   442
3e6453e2d833 8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents: 31873
diff changeset
   443
                ByteBuffer as = ab.slice().position(p).limit(l).
3e6453e2d833 8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents: 31873
diff changeset
   444
                        alignedSlice(us);
3e6453e2d833 8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents: 31873
diff changeset
   445
3e6453e2d833 8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents: 31873
diff changeset
   446
                ck(as, 0, as.position());
3e6453e2d833 8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents: 31873
diff changeset
   447
                ck(as, as.capacity(), as.limit());
3e6453e2d833 8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents: 31873
diff changeset
   448
                if (b.isDirect() != as.isDirect())
3e6453e2d833 8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents: 31873
diff changeset
   449
                    fail("Lost direction", as);
3e6453e2d833 8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents: 31873
diff changeset
   450
                if (b.isReadOnly() != as.isReadOnly())
3e6453e2d833 8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents: 31873
diff changeset
   451
                    fail("Lost read-only", as);
3e6453e2d833 8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents: 31873
diff changeset
   452
3e6453e2d833 8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents: 31873
diff changeset
   453
                if (as.alignmentOffset(0, us) != 0)
3e6453e2d833 8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents: 31873
diff changeset
   454
                    fail("Buffer not correctly aligned at index 0", as);
3e6453e2d833 8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents: 31873
diff changeset
   455
3e6453e2d833 8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents: 31873
diff changeset
   456
                if (as.alignmentOffset(as.limit(), us) != 0)
3e6453e2d833 8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents: 31873
diff changeset
   457
                    fail("Buffer not correctly aligned at limit", as);
3e6453e2d833 8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents: 31873
diff changeset
   458
3e6453e2d833 8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents: 31873
diff changeset
   459
                int p_mod = ab.alignmentOffset(p, us);
3e6453e2d833 8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents: 31873
diff changeset
   460
                int l_mod = ab.alignmentOffset(l, us);
3e6453e2d833 8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents: 31873
diff changeset
   461
                // Round up position
3e6453e2d833 8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents: 31873
diff changeset
   462
                p = (p_mod > 0) ? p + (us - p_mod) : p;
3e6453e2d833 8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents: 31873
diff changeset
   463
                // Round down limit
3e6453e2d833 8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents: 31873
diff changeset
   464
                l = l - l_mod;
3e6453e2d833 8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents: 31873
diff changeset
   465
3e6453e2d833 8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents: 31873
diff changeset
   466
                int ec = l - p;
37913
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   467
                if (as.limit() != ec) {
36933
3e6453e2d833 8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents: 31873
diff changeset
   468
                    fail("Buffer capacity incorrect, expected: " + ec, as);
37913
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   469
                }
36933
3e6453e2d833 8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents: 31873
diff changeset
   470
            }
3e6453e2d833 8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents: 31873
diff changeset
   471
        }
3e6453e2d833 8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents: 31873
diff changeset
   472
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
#end[byte]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
4502
18f387917b89 6903754: (bf) Improve floating-point buffer comparison
martin
parents: 4115
diff changeset
   475
    private static void fail(String problem,
18f387917b89 6903754: (bf) Improve floating-point buffer comparison
martin
parents: 4115
diff changeset
   476
                             $Type$Buffer xb, $Type$Buffer yb,
18f387917b89 6903754: (bf) Improve floating-point buffer comparison
martin
parents: 4115
diff changeset
   477
                             $type$ x, $type$ y) {
18f387917b89 6903754: (bf) Improve floating-point buffer comparison
martin
parents: 4115
diff changeset
   478
        fail(problem + String.format(": x=%s y=%s", x, y), xb, yb);
18f387917b89 6903754: (bf) Improve floating-point buffer comparison
martin
parents: 4115
diff changeset
   479
    }
18f387917b89 6903754: (bf) Improve floating-point buffer comparison
martin
parents: 4115
diff changeset
   480
53855
7c362992527a 5029431: (bf) Add absolute bulk put and get methods
bpb
parents: 47216
diff changeset
   481
    private static void catchNullArgument(Buffer b, Runnable thunk) {
7c362992527a 5029431: (bf) Add absolute bulk put and get methods
bpb
parents: 47216
diff changeset
   482
        tryCatch(b, NullPointerException.class, thunk);
7c362992527a 5029431: (bf) Add absolute bulk put and get methods
bpb
parents: 47216
diff changeset
   483
    }
7c362992527a 5029431: (bf) Add absolute bulk put and get methods
bpb
parents: 47216
diff changeset
   484
37913
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   485
    private static void catchIllegalArgument(Buffer b, Runnable thunk) {
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   486
        tryCatch(b, IllegalArgumentException.class, thunk);
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   487
    }
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   488
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   489
    private static void catchReadOnlyBuffer(Buffer b, Runnable thunk) {
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   490
        tryCatch(b, ReadOnlyBufferException.class, thunk);
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   491
    }
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   492
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   493
    private static void catchIndexOutOfBounds(Buffer b, Runnable thunk) {
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   494
        tryCatch(b, IndexOutOfBoundsException.class, thunk);
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   495
    }
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   496
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   497
    private static void catchIndexOutOfBounds($type$[] t, Runnable thunk) {
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   498
        tryCatch(t, IndexOutOfBoundsException.class, thunk);
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   499
    }
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   500
14001
e8e8413ad49a 7199551: (bf) CharBuffer.append(CharSequence) throws BufferOverflowException for read-only buffer
alanb
parents: 13563
diff changeset
   501
    private static void tryCatch(Buffer b, Class<?> ex, Runnable thunk) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        boolean caught = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            thunk.run();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        } catch (Throwable x) {
1634
3871c2046043 6593946: (bf) X-Buffer.compact() does not discard mark as specified
alanb
parents: 2
diff changeset
   506
            if (ex.isAssignableFrom(x.getClass())) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                caught = true;
1634
3871c2046043 6593946: (bf) X-Buffer.compact() does not discard mark as specified
alanb
parents: 2
diff changeset
   508
            } else {
53855
7c362992527a 5029431: (bf) Add absolute bulk put and get methods
bpb
parents: 47216
diff changeset
   509
                String s = x.getMessage();
7c362992527a 5029431: (bf) Add absolute bulk put and get methods
bpb
parents: 47216
diff changeset
   510
                if (s == null)
7c362992527a 5029431: (bf) Add absolute bulk put and get methods
bpb
parents: 47216
diff changeset
   511
                    s = x.getClass().getName();
7c362992527a 5029431: (bf) Add absolute bulk put and get methods
bpb
parents: 47216
diff changeset
   512
                fail(s + " not expected");
1634
3871c2046043 6593946: (bf) X-Buffer.compact() does not discard mark as specified
alanb
parents: 2
diff changeset
   513
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        }
37913
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   515
        if (!caught) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
            fail(ex.getName() + " not thrown", b);
37913
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   517
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
37913
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   520
    private static void tryCatch($type$[] t, Class<?> ex, Runnable thunk) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        tryCatch($Type$Buffer.wrap(t), ex, thunk);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    public static void test(int level, final $Type$Buffer b, boolean direct) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        show(level, b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        if (direct != b.isDirect())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            fail("Wrong direction", b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        // Gets and puts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        relPut(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        relGet(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        absGet(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        bulkGet(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        absPut(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        relGet(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        absGet(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        bulkGet(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        bulkPutArray(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        relGet(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        bulkPutBuffer(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        relGet(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
53855
7c362992527a 5029431: (bf) Add absolute bulk put and get methods
bpb
parents: 47216
diff changeset
   549
        absBulkPutArray(b);
7c362992527a 5029431: (bf) Add absolute bulk put and get methods
bpb
parents: 47216
diff changeset
   550
        absBulkGet(b);
7c362992527a 5029431: (bf) Add absolute bulk put and get methods
bpb
parents: 47216
diff changeset
   551
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
#if[char]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        bulkPutString(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        relGet(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        b.position(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        b.limit(7);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        ck(b, b.toString().equals("bcdefg"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        // CharSequence ops
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        b.position(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        ck(b, b.charAt(1), 'd');
2593
76032557be03 6795561: (bf) CharBuffer.subSequence() uses wrong capacity value for new buffer
alanb
parents: 1639
diff changeset
   564
        CharBuffer c = b.subSequence(1, 4);
76032557be03 6795561: (bf) CharBuffer.subSequence() uses wrong capacity value for new buffer
alanb
parents: 1639
diff changeset
   565
        ck(c, c.capacity(), b.capacity());
76032557be03 6795561: (bf) CharBuffer.subSequence() uses wrong capacity value for new buffer
alanb
parents: 1639
diff changeset
   566
        ck(c, c.position(), b.position()+1);
76032557be03 6795561: (bf) CharBuffer.subSequence() uses wrong capacity value for new buffer
alanb
parents: 1639
diff changeset
   567
        ck(c, c.limit(), b.position()+4);
76032557be03 6795561: (bf) CharBuffer.subSequence() uses wrong capacity value for new buffer
alanb
parents: 1639
diff changeset
   568
        ck(c, b.subSequence(1, 4).toString().equals("def"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        // 4938424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        b.position(4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        ck(b, b.charAt(1), 'f');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        ck(b, b.subSequence(1, 3).toString().equals("fg"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
13563
13c8e8f0302b 7190219: (bf) CharBuffer.put(String,int,int) modifies position even if BufferOverflowException thrown
luchsh
parents: 5506
diff changeset
   575
        // String ops
13c8e8f0302b 7190219: (bf) CharBuffer.put(String,int,int) modifies position even if BufferOverflowException thrown
luchsh
parents: 5506
diff changeset
   576
13c8e8f0302b 7190219: (bf) CharBuffer.put(String,int,int) modifies position even if BufferOverflowException thrown
luchsh
parents: 5506
diff changeset
   577
        // 7190219
13c8e8f0302b 7190219: (bf) CharBuffer.put(String,int,int) modifies position even if BufferOverflowException thrown
luchsh
parents: 5506
diff changeset
   578
        b.clear();
13c8e8f0302b 7190219: (bf) CharBuffer.put(String,int,int) modifies position even if BufferOverflowException thrown
luchsh
parents: 5506
diff changeset
   579
        int pos = b.position();
37913
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   580
        tryCatch(b, BufferOverflowException.class, () ->
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   581
                b.put(String.valueOf(new char[b.capacity() + 1]), 0, b.capacity() + 1)
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   582
            );
13563
13c8e8f0302b 7190219: (bf) CharBuffer.put(String,int,int) modifies position even if BufferOverflowException thrown
luchsh
parents: 5506
diff changeset
   583
        ck(b, b.position(), pos);
13c8e8f0302b 7190219: (bf) CharBuffer.put(String,int,int) modifies position even if BufferOverflowException thrown
luchsh
parents: 5506
diff changeset
   584
        relGet(b);
13c8e8f0302b 7190219: (bf) CharBuffer.put(String,int,int) modifies position even if BufferOverflowException thrown
luchsh
parents: 5506
diff changeset
   585
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
#end[char]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        // Compact
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        relPut(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        b.position(13);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        b.compact();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        b.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        relGet(b, 13);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        // Exceptions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        relPut(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        b.limit(b.capacity() / 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        b.position(b.limit());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
37913
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   602
        tryCatch(b, BufferUnderflowException.class, () -> b.get());
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   603
        tryCatch(b, BufferOverflowException.class, () -> b.put(($type$)42));
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   604
        // The index must be non-negative and less than the buffer's limit.
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   605
        catchIndexOutOfBounds(b, () -> b.get(b.limit()));
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   606
        catchIndexOutOfBounds(b, () -> b.get(-1));
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   607
        catchIndexOutOfBounds(b, () -> b.put(b.limit(), ($type$)42));
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   608
        tryCatch(b, InvalidMarkException.class,
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   609
                () -> b.position(0).mark().compact().reset());
1634
3871c2046043 6593946: (bf) X-Buffer.compact() does not discard mark as specified
alanb
parents: 2
diff changeset
   610
31873
87b015c2cd36 8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents: 14342
diff changeset
   611
        try {
87b015c2cd36 8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents: 14342
diff changeset
   612
            b.position(b.limit() + 1);
87b015c2cd36 8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents: 14342
diff changeset
   613
            fail("IllegalArgumentException expected for position beyond limit");
87b015c2cd36 8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents: 14342
diff changeset
   614
        } catch (IllegalArgumentException e) {
87b015c2cd36 8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents: 14342
diff changeset
   615
            if (e.getMessage() == null) {
87b015c2cd36 8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents: 14342
diff changeset
   616
                fail("Non-null IllegalArgumentException message expected for"
87b015c2cd36 8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents: 14342
diff changeset
   617
                     + " position beyond limit");
87b015c2cd36 8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents: 14342
diff changeset
   618
            }
87b015c2cd36 8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents: 14342
diff changeset
   619
        }
87b015c2cd36 8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents: 14342
diff changeset
   620
87b015c2cd36 8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents: 14342
diff changeset
   621
        try {
87b015c2cd36 8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents: 14342
diff changeset
   622
            b.position(-1);
87b015c2cd36 8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents: 14342
diff changeset
   623
            fail("IllegalArgumentException expected for negative position");
87b015c2cd36 8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents: 14342
diff changeset
   624
        } catch (IllegalArgumentException e) {
87b015c2cd36 8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents: 14342
diff changeset
   625
            if (e.getMessage() == null) {
87b015c2cd36 8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents: 14342
diff changeset
   626
                fail("Non-null IllegalArgumentException message expected for"
87b015c2cd36 8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents: 14342
diff changeset
   627
                     + " negative position");
87b015c2cd36 8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents: 14342
diff changeset
   628
            }
87b015c2cd36 8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents: 14342
diff changeset
   629
        }
87b015c2cd36 8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents: 14342
diff changeset
   630
87b015c2cd36 8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents: 14342
diff changeset
   631
        try {
87b015c2cd36 8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents: 14342
diff changeset
   632
            b.limit(b.capacity() + 1);
87b015c2cd36 8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents: 14342
diff changeset
   633
            fail("IllegalArgumentException expected for limit beyond capacity");
87b015c2cd36 8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents: 14342
diff changeset
   634
        } catch (IllegalArgumentException e) {
87b015c2cd36 8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents: 14342
diff changeset
   635
            if (e.getMessage() == null) {
87b015c2cd36 8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents: 14342
diff changeset
   636
                fail("Non-null IllegalArgumentException message expected for"
87b015c2cd36 8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents: 14342
diff changeset
   637
                     + " limit beyond capacity");
87b015c2cd36 8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents: 14342
diff changeset
   638
            }
87b015c2cd36 8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents: 14342
diff changeset
   639
        }
87b015c2cd36 8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents: 14342
diff changeset
   640
87b015c2cd36 8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents: 14342
diff changeset
   641
        try {
87b015c2cd36 8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents: 14342
diff changeset
   642
            b.limit(-1);
87b015c2cd36 8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents: 14342
diff changeset
   643
            fail("IllegalArgumentException expected for negative limit");
87b015c2cd36 8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents: 14342
diff changeset
   644
        } catch (IllegalArgumentException e) {
87b015c2cd36 8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents: 14342
diff changeset
   645
            if (e.getMessage() == null) {
87b015c2cd36 8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents: 14342
diff changeset
   646
                fail("Non-null IllegalArgumentException message expected for"
87b015c2cd36 8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents: 14342
diff changeset
   647
                     + " negative limit");
87b015c2cd36 8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents: 14342
diff changeset
   648
            }
87b015c2cd36 8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents: 14342
diff changeset
   649
        }
87b015c2cd36 8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents: 14342
diff changeset
   650
53855
7c362992527a 5029431: (bf) Add absolute bulk put and get methods
bpb
parents: 47216
diff changeset
   651
        // Exceptions in absolute bulk operations
7c362992527a 5029431: (bf) Add absolute bulk put and get methods
bpb
parents: 47216
diff changeset
   652
7c362992527a 5029431: (bf) Add absolute bulk put and get methods
bpb
parents: 47216
diff changeset
   653
        catchNullArgument(b, () -> b.get(7, null, 0, 42));
7c362992527a 5029431: (bf) Add absolute bulk put and get methods
bpb
parents: 47216
diff changeset
   654
        catchNullArgument(b, () -> b.put(7, ($type$[])null, 0, 42));
7c362992527a 5029431: (bf) Add absolute bulk put and get methods
bpb
parents: 47216
diff changeset
   655
7c362992527a 5029431: (bf) Add absolute bulk put and get methods
bpb
parents: 47216
diff changeset
   656
        $type$[] tmpa = new $type$[42];
7c362992527a 5029431: (bf) Add absolute bulk put and get methods
bpb
parents: 47216
diff changeset
   657
        catchIndexOutOfBounds(b, () -> b.get(7, tmpa, -1, 42));
7c362992527a 5029431: (bf) Add absolute bulk put and get methods
bpb
parents: 47216
diff changeset
   658
        catchIndexOutOfBounds(b, () -> b.get(7, tmpa, 42, 1));
7c362992527a 5029431: (bf) Add absolute bulk put and get methods
bpb
parents: 47216
diff changeset
   659
        catchIndexOutOfBounds(b, () -> b.get(7, tmpa, 41, -1));
7c362992527a 5029431: (bf) Add absolute bulk put and get methods
bpb
parents: 47216
diff changeset
   660
        catchIndexOutOfBounds(b, () -> b.get(-1, tmpa, 0, 1));
7c362992527a 5029431: (bf) Add absolute bulk put and get methods
bpb
parents: 47216
diff changeset
   661
        catchIndexOutOfBounds(b, () -> b.get(b.limit(), tmpa, 0, 1));
7c362992527a 5029431: (bf) Add absolute bulk put and get methods
bpb
parents: 47216
diff changeset
   662
        catchIndexOutOfBounds(b, () -> b.get(b.limit() - 41, tmpa, 0, 42));
7c362992527a 5029431: (bf) Add absolute bulk put and get methods
bpb
parents: 47216
diff changeset
   663
7c362992527a 5029431: (bf) Add absolute bulk put and get methods
bpb
parents: 47216
diff changeset
   664
        catchIndexOutOfBounds(b, () -> b.put(7, tmpa, -1, 42));
7c362992527a 5029431: (bf) Add absolute bulk put and get methods
bpb
parents: 47216
diff changeset
   665
        catchIndexOutOfBounds(b, () -> b.put(7, tmpa, 42, 1));
7c362992527a 5029431: (bf) Add absolute bulk put and get methods
bpb
parents: 47216
diff changeset
   666
        catchIndexOutOfBounds(b, () -> b.put(7, tmpa, 41, -1));
7c362992527a 5029431: (bf) Add absolute bulk put and get methods
bpb
parents: 47216
diff changeset
   667
        catchIndexOutOfBounds(b, () -> b.put(-1, tmpa, 0, 1));
7c362992527a 5029431: (bf) Add absolute bulk put and get methods
bpb
parents: 47216
diff changeset
   668
        catchIndexOutOfBounds(b, () -> b.put(b.limit(), tmpa, 0, 1));
7c362992527a 5029431: (bf) Add absolute bulk put and get methods
bpb
parents: 47216
diff changeset
   669
        catchIndexOutOfBounds(b, () -> b.put(b.limit() - 41, tmpa, 0, 42));
7c362992527a 5029431: (bf) Add absolute bulk put and get methods
bpb
parents: 47216
diff changeset
   670
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        // Values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        b.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        b.put(($type$)0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
        b.put(($type$)-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        b.put(($type$)1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        b.put($Fulltype$.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        b.put($Fulltype$.MIN_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
#if[float]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        b.put(-Float.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
        b.put(-Float.MIN_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        b.put(Float.NEGATIVE_INFINITY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        b.put(Float.POSITIVE_INFINITY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        b.put(Float.NaN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        b.put(0.91697687f);             // Changes value if incorrectly swapped
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
#end[float]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
#if[double]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
        b.put(-Double.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        b.put(-Double.MIN_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        b.put(Double.NEGATIVE_INFINITY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        b.put(Double.POSITIVE_INFINITY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
        b.put(Double.NaN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        b.put(0.5121609353879392);      // Changes value if incorrectly swapped
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
#end[double]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        b.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        ck(b, b.get(), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        ck(b, b.get(), ($type$)-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        ck(b, b.get(), 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        ck(b, b.get(), $Fulltype$.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        ck(b, b.get(), $Fulltype$.MIN_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
#if[float]
37913
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   704
        $type$ v;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        ck(b, b.get(), -Float.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        ck(b, b.get(), -Float.MIN_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        ck(b, b.get(), Float.NEGATIVE_INFINITY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        ck(b, b.get(), Float.POSITIVE_INFINITY);
31873
87b015c2cd36 8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents: 14342
diff changeset
   709
        if (Float.floatToRawIntBits(v = b.get()) !=
37913
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   710
            Float.floatToRawIntBits(Float.NaN)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
            fail(b, (long)Float.NaN, (long)v);
37913
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   712
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        ck(b, b.get(), 0.91697687f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
#end[float]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
#if[double]
37913
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   716
        $type$ v;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        ck(b, b.get(), -Double.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
        ck(b, b.get(), -Double.MIN_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        ck(b, b.get(), Double.NEGATIVE_INFINITY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
        ck(b, b.get(), Double.POSITIVE_INFINITY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        if (Double.doubleToRawLongBits(v = b.get())
37913
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   722
            != Double.doubleToRawLongBits(Double.NaN)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
            fail(b, (long)Double.NaN, (long)v);
37913
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   724
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        ck(b, b.get(), 0.5121609353879392);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
#end[double]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        // Comparison
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        b.rewind();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        $Type$Buffer b2 = $Type$Buffer.allocate(b.capacity());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
        b2.put(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        b2.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
        b.position(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
        b2.position(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        if (!b.equals(b2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
            for (int i = 2; i < b.limit(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
                $type$ x = b.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
                $type$ y = b2.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
                if (x != y
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
#if[double]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
                    || Double.compare(x, y) != 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
#end[double]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
#if[float]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
                    || Float.compare(x, y) != 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
#end[float]
37913
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   747
                    ) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
                    out.println("[" + i + "] " + x + " != " + y);
37913
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   749
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
            fail("Identical buffers not equal", b, b2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        }
37913
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   753
        if (b.compareTo(b2) != 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
            fail("Comparison to identical buffer != 0", b, b2);
37913
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   755
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        b.limit(b.limit() + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        b.position(b.limit() - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
        b.put(($type$)99);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        b.rewind();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
        b2.rewind();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
        if (b.equals(b2))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
            fail("Non-identical buffers equal", b, b2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        if (b.compareTo(b2) <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
            fail("Comparison to shorter buffer <= 0", b, b2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        b.limit(b.limit() - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
        b.put(2, ($type$)42);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
        if (b.equals(b2))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
            fail("Non-identical buffers equal", b, b2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        if (b.compareTo(b2) <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
            fail("Comparison to lesser buffer <= 0", b, b2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
4502
18f387917b89 6903754: (bf) Improve floating-point buffer comparison
martin
parents: 4115
diff changeset
   773
        // Check equals and compareTo with interesting values
18f387917b89 6903754: (bf) Improve floating-point buffer comparison
martin
parents: 4115
diff changeset
   774
        for ($type$ x : VALUES) {
18f387917b89 6903754: (bf) Improve floating-point buffer comparison
martin
parents: 4115
diff changeset
   775
            $Type$Buffer xb = $Type$Buffer.wrap(new $type$[] { x });
18f387917b89 6903754: (bf) Improve floating-point buffer comparison
martin
parents: 4115
diff changeset
   776
            if (xb.compareTo(xb) != 0) {
18f387917b89 6903754: (bf) Improve floating-point buffer comparison
martin
parents: 4115
diff changeset
   777
                fail("compareTo not reflexive", xb, xb, x, x);
18f387917b89 6903754: (bf) Improve floating-point buffer comparison
martin
parents: 4115
diff changeset
   778
            }
37913
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   779
            if (!xb.equals(xb)) {
4502
18f387917b89 6903754: (bf) Improve floating-point buffer comparison
martin
parents: 4115
diff changeset
   780
                fail("equals not reflexive", xb, xb, x, x);
18f387917b89 6903754: (bf) Improve floating-point buffer comparison
martin
parents: 4115
diff changeset
   781
            }
18f387917b89 6903754: (bf) Improve floating-point buffer comparison
martin
parents: 4115
diff changeset
   782
            for ($type$ y : VALUES) {
18f387917b89 6903754: (bf) Improve floating-point buffer comparison
martin
parents: 4115
diff changeset
   783
                $Type$Buffer yb = $Type$Buffer.wrap(new $type$[] { y });
18f387917b89 6903754: (bf) Improve floating-point buffer comparison
martin
parents: 4115
diff changeset
   784
                if (xb.compareTo(yb) != - yb.compareTo(xb)) {
18f387917b89 6903754: (bf) Improve floating-point buffer comparison
martin
parents: 4115
diff changeset
   785
                    fail("compareTo not anti-symmetric",
18f387917b89 6903754: (bf) Improve floating-point buffer comparison
martin
parents: 4115
diff changeset
   786
                         xb, yb, x, y);
18f387917b89 6903754: (bf) Improve floating-point buffer comparison
martin
parents: 4115
diff changeset
   787
                }
18f387917b89 6903754: (bf) Improve floating-point buffer comparison
martin
parents: 4115
diff changeset
   788
                if ((xb.compareTo(yb) == 0) != xb.equals(yb)) {
18f387917b89 6903754: (bf) Improve floating-point buffer comparison
martin
parents: 4115
diff changeset
   789
                    fail("compareTo inconsistent with equals",
18f387917b89 6903754: (bf) Improve floating-point buffer comparison
martin
parents: 4115
diff changeset
   790
                         xb, yb, x, y);
18f387917b89 6903754: (bf) Improve floating-point buffer comparison
martin
parents: 4115
diff changeset
   791
                }
18f387917b89 6903754: (bf) Improve floating-point buffer comparison
martin
parents: 4115
diff changeset
   792
                if (xb.compareTo(yb) != $Fulltype$.compare(x, y)) {
18f387917b89 6903754: (bf) Improve floating-point buffer comparison
martin
parents: 4115
diff changeset
   793
#if[float]
18f387917b89 6903754: (bf) Improve floating-point buffer comparison
martin
parents: 4115
diff changeset
   794
                    if (x == 0.0 && y == 0.0) continue;
18f387917b89 6903754: (bf) Improve floating-point buffer comparison
martin
parents: 4115
diff changeset
   795
#end[float]
18f387917b89 6903754: (bf) Improve floating-point buffer comparison
martin
parents: 4115
diff changeset
   796
#if[double]
18f387917b89 6903754: (bf) Improve floating-point buffer comparison
martin
parents: 4115
diff changeset
   797
                    if (x == 0.0 && y == 0.0) continue;
18f387917b89 6903754: (bf) Improve floating-point buffer comparison
martin
parents: 4115
diff changeset
   798
#end[double]
18f387917b89 6903754: (bf) Improve floating-point buffer comparison
martin
parents: 4115
diff changeset
   799
                    fail("Incorrect results for $Type$Buffer.compareTo",
18f387917b89 6903754: (bf) Improve floating-point buffer comparison
martin
parents: 4115
diff changeset
   800
                         xb, yb, x, y);
18f387917b89 6903754: (bf) Improve floating-point buffer comparison
martin
parents: 4115
diff changeset
   801
                }
18f387917b89 6903754: (bf) Improve floating-point buffer comparison
martin
parents: 4115
diff changeset
   802
                if (xb.equals(yb) != ((x == y) || ((x != x) && (y != y)))) {
18f387917b89 6903754: (bf) Improve floating-point buffer comparison
martin
parents: 4115
diff changeset
   803
                    fail("Incorrect results for $Type$Buffer.equals",
18f387917b89 6903754: (bf) Improve floating-point buffer comparison
martin
parents: 4115
diff changeset
   804
                         xb, yb, x, y);
18f387917b89 6903754: (bf) Improve floating-point buffer comparison
martin
parents: 4115
diff changeset
   805
                }
18f387917b89 6903754: (bf) Improve floating-point buffer comparison
martin
parents: 4115
diff changeset
   806
            }
18f387917b89 6903754: (bf) Improve floating-point buffer comparison
martin
parents: 4115
diff changeset
   807
        }
18f387917b89 6903754: (bf) Improve floating-point buffer comparison
martin
parents: 4115
diff changeset
   808
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
        // Sub, dup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
        relPut(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
        relGet(b.duplicate());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
        b.position(13);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
        relGet(b.duplicate(), 13);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
        relGet(b.duplicate().slice(), 13);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
        relGet(b.slice(), 13);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
        relGet(b.slice().duplicate(), 13);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
        // Slice
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
        b.position(5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
        $Type$Buffer sb = b.slice();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
        checkSlice(b, sb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        b.position(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
        $Type$Buffer sb2 = sb.slice();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
        checkSlice(sb, sb2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        if (!sb.equals(sb2))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
            fail("Sliced slices do not match", sb, sb2);
37913
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   830
        if ((sb.hasArray()) && (sb.arrayOffset() != sb2.arrayOffset())) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
            fail("Array offsets do not match: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
                 + sb.arrayOffset() + " != " + sb2.arrayOffset(), sb, sb2);
37913
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   833
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
#if[byte]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
        // Views
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
        b.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
        b.order(ByteOrder.BIG_ENDIAN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
        testViews(level + 1, b, direct);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
        for (int i = 1; i <= 9; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
            b.position(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
            show(level + 1, b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
            testViews(level + 2, b, direct);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
        b.position(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
        b.order(ByteOrder.LITTLE_ENDIAN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
        testViews(level + 1, b, direct);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
        // Heterogeneous accessors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
        b.order(ByteOrder.BIG_ENDIAN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
        for (int i = 0; i <= 9; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
            b.position(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
            testHet(level + 1, b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
        b.order(ByteOrder.LITTLE_ENDIAN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
        b.position(3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
        testHet(level + 1, b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
#end[byte]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
        // Read-only views
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
        b.rewind();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
        final $Type$Buffer rb = b.asReadOnlyBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
        if (!b.equals(rb))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
            fail("Buffer not equal to read-only view", b, rb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
        show(level + 1, rb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
37913
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   874
        catchReadOnlyBuffer(b, () -> relPut(rb));
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   875
        catchReadOnlyBuffer(b, () -> absPut(rb));
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   876
        catchReadOnlyBuffer(b, () -> bulkPutArray(rb));
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   877
        catchReadOnlyBuffer(b, () -> bulkPutBuffer(rb));
53855
7c362992527a 5029431: (bf) Add absolute bulk put and get methods
bpb
parents: 47216
diff changeset
   878
        catchReadOnlyBuffer(b, () -> absBulkPutArray(rb));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
14001
e8e8413ad49a 7199551: (bf) CharBuffer.append(CharSequence) throws BufferOverflowException for read-only buffer
alanb
parents: 13563
diff changeset
   880
        // put($Type$Buffer) should not change source position
e8e8413ad49a 7199551: (bf) CharBuffer.append(CharSequence) throws BufferOverflowException for read-only buffer
alanb
parents: 13563
diff changeset
   881
        final $Type$Buffer src = $Type$Buffer.allocate(1);
37913
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   882
        catchReadOnlyBuffer(b, () -> rb.put(src));
14001
e8e8413ad49a 7199551: (bf) CharBuffer.append(CharSequence) throws BufferOverflowException for read-only buffer
alanb
parents: 13563
diff changeset
   883
        ck(src, src.position(), 0);
e8e8413ad49a 7199551: (bf) CharBuffer.append(CharSequence) throws BufferOverflowException for read-only buffer
alanb
parents: 13563
diff changeset
   884
37913
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   885
        catchReadOnlyBuffer(b, () -> rb.compact());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
#if[byte]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
37913
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   889
        catchReadOnlyBuffer(b, () -> rb.putChar((char)1));
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   890
        catchReadOnlyBuffer(b, () -> rb.putChar(0, (char)1));
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   891
        catchReadOnlyBuffer(b, () -> rb.putShort((short)1));
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   892
        catchReadOnlyBuffer(b, () -> rb.putShort(0, (short)1));
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   893
        catchReadOnlyBuffer(b, () -> rb.putInt(1));
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   894
        catchReadOnlyBuffer(b, () -> rb.putInt(0, 1));
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   895
        catchReadOnlyBuffer(b, () -> rb.putLong((long)1));
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   896
        catchReadOnlyBuffer(b, () -> rb.putLong(0, (long)1));
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   897
        catchReadOnlyBuffer(b, () -> rb.putFloat((float)1));
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   898
        catchReadOnlyBuffer(b, () -> rb.putFloat(0, (float)1));
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   899
        catchReadOnlyBuffer(b, () -> rb.putDouble((double)1));
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   900
        catchReadOnlyBuffer(b, () -> rb.putDouble(0, (double)1));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
#end[byte]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
14001
e8e8413ad49a 7199551: (bf) CharBuffer.append(CharSequence) throws BufferOverflowException for read-only buffer
alanb
parents: 13563
diff changeset
   904
#if[char]
e8e8413ad49a 7199551: (bf) CharBuffer.append(CharSequence) throws BufferOverflowException for read-only buffer
alanb
parents: 13563
diff changeset
   905
e8e8413ad49a 7199551: (bf) CharBuffer.append(CharSequence) throws BufferOverflowException for read-only buffer
alanb
parents: 13563
diff changeset
   906
        // 7199551
37913
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   907
        catchReadOnlyBuffer(b, () -> rb.put(new String(new char[rb.remaining() + 1])));
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   908
        catchReadOnlyBuffer(b, () -> rb.append(new String(new char[rb.remaining() + 1])));
14001
e8e8413ad49a 7199551: (bf) CharBuffer.append(CharSequence) throws BufferOverflowException for read-only buffer
alanb
parents: 13563
diff changeset
   909
e8e8413ad49a 7199551: (bf) CharBuffer.append(CharSequence) throws BufferOverflowException for read-only buffer
alanb
parents: 13563
diff changeset
   910
#end[char]
e8e8413ad49a 7199551: (bf) CharBuffer.append(CharSequence) throws BufferOverflowException for read-only buffer
alanb
parents: 13563
diff changeset
   911
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
        if (rb.getClass().getName().startsWith("java.nio.Heap")) {
37913
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   913
            catchReadOnlyBuffer(b, () -> rb.array());
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   914
            catchReadOnlyBuffer(b, () -> rb.arrayOffset());
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   915
            if (rb.hasArray()) {
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   916
                fail("Read-only heap buffer's backing array is accessible", rb);
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   917
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
        // Bulk puts from read-only buffers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
        b.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
        rb.rewind();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
        b.put(rb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
#if[byte]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
        // For byte buffers, test both the direct and non-direct cases
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
        $Type$Buffer ob
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
            = (b.isDirect()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
               ? $Type$Buffer.allocate(rb.capacity())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
               : $Type$Buffer.allocateDirect(rb.capacity()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
        rb.rewind();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
        ob.put(rb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
#end[byte]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
        relPut(b);                       // Required by testViews
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
36933
3e6453e2d833 8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents: 31873
diff changeset
   938
#if[byte]
3e6453e2d833 8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents: 31873
diff changeset
   939
        // Test alignment
3e6453e2d833 8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents: 31873
diff changeset
   940
3e6453e2d833 8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents: 31873
diff changeset
   941
        testAlign(b, direct);
3e6453e2d833 8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents: 31873
diff changeset
   942
#end[byte]
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
#if[char]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
    private static void testStr() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
        final String s = "abcdefghijklm";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
        int start = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
        int end = 9;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
        final CharBuffer b = CharBuffer.wrap(s, start, end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
        show(0, b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
        ck(b, b.toString().equals(s.substring(start, end)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
        ck(b, b.toString().equals("defghi"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
        ck(b, b.isReadOnly());
37913
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   956
        catchReadOnlyBuffer(b, () -> b.put('x'));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
        ck(b, start, b.position());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
        ck(b, end, b.limit());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
        ck(b, s.length(), b.capacity());
2593
76032557be03 6795561: (bf) CharBuffer.subSequence() uses wrong capacity value for new buffer
alanb
parents: 1639
diff changeset
   960
        b.position(6);
76032557be03 6795561: (bf) CharBuffer.subSequence() uses wrong capacity value for new buffer
alanb
parents: 1639
diff changeset
   961
        ck(b, b.subSequence(0,3).toString().equals("ghi"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
53855
7c362992527a 5029431: (bf) Add absolute bulk put and get methods
bpb
parents: 47216
diff changeset
   963
        // absolute bulk get
7c362992527a 5029431: (bf) Add absolute bulk put and get methods
bpb
parents: 47216
diff changeset
   964
        char[] c = new char[end + 1 - (start - 1) + 1]; // [start - 1, end + 1]
7c362992527a 5029431: (bf) Add absolute bulk put and get methods
bpb
parents: 47216
diff changeset
   965
        b.limit(end + 2);
7c362992527a 5029431: (bf) Add absolute bulk put and get methods
bpb
parents: 47216
diff changeset
   966
        b.get(start - 1, c, 0, c.length);
7c362992527a 5029431: (bf) Add absolute bulk put and get methods
bpb
parents: 47216
diff changeset
   967
        for (int i = 0; i < c.length; i++)
7c362992527a 5029431: (bf) Add absolute bulk put and get methods
bpb
parents: 47216
diff changeset
   968
            ck(b, c[i], s.charAt(start - 1 + i));
7c362992527a 5029431: (bf) Add absolute bulk put and get methods
bpb
parents: 47216
diff changeset
   969
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
        // The index, relative to the position, must be non-negative and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
        // smaller than remaining().
37913
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   972
        catchIndexOutOfBounds(b, () -> b.charAt(-1));
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   973
        catchIndexOutOfBounds(b, () -> b.charAt(b.remaining()));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
        // The index must be non-negative and less than the buffer's limit.
37913
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   975
        catchIndexOutOfBounds(b, () -> b.get(b.limit()));
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   976
        catchIndexOutOfBounds(b, () -> b.get(-1));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
        // The start must be non-negative and no larger than remaining().
37913
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   978
        catchIndexOutOfBounds(b, () -> b.subSequence(-1, b.remaining()));
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   979
        catchIndexOutOfBounds(b, () -> b.subSequence(b.remaining() + 1, b.remaining()));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
        // The end must be no smaller than start and no larger than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
        // remaining().
37913
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   983
        catchIndexOutOfBounds(b, () -> b.subSequence(2, 1));
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   984
        catchIndexOutOfBounds(b, () -> b.subSequence(0, b.remaining() + 1));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
        // The offset must be non-negative and no larger than <array.length>.
37913
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   987
        catchIndexOutOfBounds(b, () -> $Type$Buffer.wrap(s, -1, s.length()));
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   988
        catchIndexOutOfBounds(b, () -> $Type$Buffer.wrap(s, s.length() + 1, s.length()));
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   989
        catchIndexOutOfBounds(b, () -> $Type$Buffer.wrap(s, 1, 0));
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
   990
        catchIndexOutOfBounds(b, () -> $Type$Buffer.wrap(s, 0, s.length() + 1));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
#end[char]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
    public static void test(final $type$ [] ba) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
        int offset = 47;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
        int length = 900;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
        final $Type$Buffer b = $Type$Buffer.wrap(ba, offset, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
        show(0, b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
        ck(b, b.capacity(), ba.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
        ck(b, b.position(), offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
        ck(b, b.limit(), offset + length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
        // The offset must be non-negative and no larger than <array.length>.
37913
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
  1005
        catchIndexOutOfBounds(ba, () -> $Type$Buffer.wrap(ba, -1, ba.length));
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
  1006
        catchIndexOutOfBounds(ba, () -> $Type$Buffer.wrap(ba, ba.length + 1, ba.length));
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
  1007
        catchIndexOutOfBounds(ba, () -> $Type$Buffer.wrap(ba, 0, -1));
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
  1008
        catchIndexOutOfBounds(ba, () -> $Type$Buffer.wrap(ba, 0, ba.length + 1));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
        // A NullPointerException will be thrown if the array is null.
37913
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
  1011
        tryCatch(ba, NullPointerException.class,
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
  1012
                () -> $Type$Buffer.wrap(($type$ []) null, 0, 5));
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
  1013
        tryCatch(ba, NullPointerException.class,
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
  1014
                () -> $Type$Buffer.wrap(($type$ []) null));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
    private static void testAllocate() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
        // An IllegalArgumentException will be thrown for negative capacities.
37913
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
  1019
        catchIllegalArgument((Buffer) null, () -> $Type$Buffer.allocate(-1));
31873
87b015c2cd36 8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents: 14342
diff changeset
  1020
        try {
87b015c2cd36 8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents: 14342
diff changeset
  1021
            $Type$Buffer.allocate(-1);
87b015c2cd36 8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents: 14342
diff changeset
  1022
        } catch (IllegalArgumentException e) {
87b015c2cd36 8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents: 14342
diff changeset
  1023
            if (e.getMessage() == null) {
87b015c2cd36 8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents: 14342
diff changeset
  1024
                fail("Non-null IllegalArgumentException message expected for"
87b015c2cd36 8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents: 14342
diff changeset
  1025
                     + " attempt to allocate negative capacity buffer");
87b015c2cd36 8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents: 14342
diff changeset
  1026
            }
87b015c2cd36 8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents: 14342
diff changeset
  1027
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
#if[byte]
37913
3cc95b690353 8156931: java.nio.Buffer tests cleanup
prappo
parents: 36933
diff changeset
  1029
        catchIllegalArgument((Buffer) null, () -> $Type$Buffer.allocateDirect(-1));
31873
87b015c2cd36 8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents: 14342
diff changeset
  1030
        try {
87b015c2cd36 8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents: 14342
diff changeset
  1031
            $Type$Buffer.allocateDirect(-1);
87b015c2cd36 8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents: 14342
diff changeset
  1032
        } catch (IllegalArgumentException e) {
87b015c2cd36 8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents: 14342
diff changeset
  1033
            if (e.getMessage() == null) {
87b015c2cd36 8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents: 14342
diff changeset
  1034
                fail("Non-null IllegalArgumentException message expected for"
87b015c2cd36 8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents: 14342
diff changeset
  1035
                     + " attempt to allocate negative capacity direct buffer");
87b015c2cd36 8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents: 14342
diff changeset
  1036
            }
87b015c2cd36 8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents: 14342
diff changeset
  1037
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
#end[byte]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
    public static void test() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
        testAllocate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
        test(0, $Type$Buffer.allocate(7 * 1024), false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
        test(0, $Type$Buffer.wrap(new $type$[7 * 1024], 0, 7 * 1024), false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
        test(new $type$[1024]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
#if[byte]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
        $Type$Buffer b = $Type$Buffer.allocateDirect(7 * 1024);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
        for (b.position(0); b.position() < b.limit(); )
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
            ck(b, b.get(), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
        test(0, b, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
#end[byte]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
#if[char]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
        testStr();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
#end[char]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
        callReset($Type$Buffer.allocate(10));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
#if[byte]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
#else[byte]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
        putBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
#end[byte]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
}