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