jdk/test/java/lang/invoke/VarHandles/VarHandleTestByteArrayAsInt.java
author amurillo
Tue, 17 May 2016 05:38:15 -0700
changeset 38368 c8eb5d6812c5
parent 38358 cb99c6d2af1b
parent 38328 40435a469d25
child 38382 98d5a441bc2f
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
     1
/*
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
     2
 * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
     4
 *
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
     7
 * published by the Free Software Foundation.
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
     8
 *
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    13
 * accompanied this code).
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    14
 *
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    18
 *
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    21
 * questions.
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    22
 */
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    23
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    24
/*
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    25
 * @test
37668
34a002e5168a 8154556: Use java.nio.ByteOrder instead of boolean value
psandoz
parents: 37343
diff changeset
    26
 * @bug 8154556
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    27
 * @run testng/othervm -Diters=20000 -XX:TieredStopAtLevel=1 VarHandleTestByteArrayAsInt
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    28
 * @run testng/othervm -Diters=20000                         VarHandleTestByteArrayAsInt
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    29
 * @run testng/othervm -Diters=20000 -XX:-TieredCompilation  VarHandleTestByteArrayAsInt
38328
40435a469d25 8156485: MethodHandles.varHandleExactInvoker should perform exact checks
psandoz
parents: 37719
diff changeset
    30
 * @run testng/othervm -Diters=20000 -Djava.lang.invoke.VarHandle.VAR_HANDLE_GUARDS=false VarHandleTestByteArrayAsInt
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    31
 */
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    32
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    33
import org.testng.annotations.DataProvider;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    34
import org.testng.annotations.Test;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    35
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    36
import java.lang.invoke.MethodHandles;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    37
import java.lang.invoke.VarHandle;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    38
import java.nio.ByteBuffer;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    39
import java.nio.ByteOrder;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    40
import java.util.ArrayList;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    41
import java.util.Arrays;
37719
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
    42
import java.util.EnumSet;
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    43
import java.util.List;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    44
37719
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
    45
import static org.testng.Assert.*;
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    46
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    47
public class VarHandleTestByteArrayAsInt extends VarHandleBaseByteArrayTest {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    48
    static final int SIZE = Integer.BYTES;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    49
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    50
    static final int VALUE_1 = 0x01020304;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    51
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    52
    static final int VALUE_2 = 0x11121314;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    53
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    54
    static final int VALUE_3 = 0x21222324;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    55
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    56
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    57
    @Override
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    58
    public void setupVarHandleSources() {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    59
        // Combinations of VarHandle byte[] or ByteBuffer
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    60
        vhss = new ArrayList<>();
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    61
        for (MemoryMode endianess : Arrays.asList(MemoryMode.BIG_ENDIAN, MemoryMode.LITTLE_ENDIAN)) {
37668
34a002e5168a 8154556: Use java.nio.ByteOrder instead of boolean value
psandoz
parents: 37343
diff changeset
    62
34a002e5168a 8154556: Use java.nio.ByteOrder instead of boolean value
psandoz
parents: 37343
diff changeset
    63
            ByteOrder bo = endianess == MemoryMode.BIG_ENDIAN
34a002e5168a 8154556: Use java.nio.ByteOrder instead of boolean value
psandoz
parents: 37343
diff changeset
    64
                    ? ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN;
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    65
            VarHandleSource aeh = new VarHandleSource(
37668
34a002e5168a 8154556: Use java.nio.ByteOrder instead of boolean value
psandoz
parents: 37343
diff changeset
    66
                    MethodHandles.byteArrayViewVarHandle(int[].class, bo),
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    67
                    endianess, MemoryMode.READ_WRITE);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    68
            vhss.add(aeh);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    69
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    70
            VarHandleSource bbh = new VarHandleSource(
37668
34a002e5168a 8154556: Use java.nio.ByteOrder instead of boolean value
psandoz
parents: 37343
diff changeset
    71
                    MethodHandles.byteBufferViewVarHandle(int[].class, bo),
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    72
                    endianess, MemoryMode.READ_WRITE);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    73
            vhss.add(bbh);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    74
        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    75
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    76
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    77
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    78
    @Test(dataProvider = "varHandlesProvider")
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    79
    public void testIsAccessModeSupported(VarHandleSource vhs) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    80
        VarHandle vh = vhs.s;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    81
37343
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
    82
        assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET));
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
    83
        assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET));
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    84
37343
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
    85
        assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_VOLATILE));
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
    86
        assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET_VOLATILE));
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
    87
        assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_ACQUIRE));
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
    88
        assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET_RELEASE));
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
    89
        assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_OPAQUE));
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
    90
        assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET_OPAQUE));
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    91
37343
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
    92
        assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_SET));
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
    93
        assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_VOLATILE));
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
    94
        assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_ACQUIRE));
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
    95
        assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_RELEASE));
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
    96
        assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET));
37719
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
    97
        assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_VOLATILE));
37343
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
    98
        assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_ACQUIRE));
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
    99
        assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_RELEASE));
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
   100
        assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_SET));
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   101
37343
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
   102
        assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_ADD));
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
   103
        assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.ADD_AND_GET));
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   104
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   105
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   106
    @Test(dataProvider = "typesProvider")
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   107
    public void testTypes(VarHandle vh, List<java.lang.Class<?>> pts) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   108
        assertEquals(vh.varType(), int.class);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   109
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   110
        assertEquals(vh.coordinateTypes(), pts);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   111
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   112
        testTypes(vh);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   113
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   114
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   115
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   116
    @DataProvider
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   117
    public Object[][] accessTestCaseProvider() throws Exception {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   118
        List<AccessTestCase<?>> cases = new ArrayList<>();
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   119
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   120
        for (ByteArrayViewSource<?> bav : bavss) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   121
            for (VarHandleSource vh : vhss) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   122
                if (vh.matches(bav)) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   123
                    if (bav instanceof ByteArraySource) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   124
                        ByteArraySource bas = (ByteArraySource) bav;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   125
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   126
                        cases.add(new VarHandleSourceAccessTestCase(
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   127
                                "read write", bav, vh, h -> testArrayReadWrite(bas, h),
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   128
                                true));
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   129
                        cases.add(new VarHandleSourceAccessTestCase(
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   130
                                "unsupported", bav, vh, h -> testArrayUnsupported(bas, h),
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   131
                                false));
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   132
                        cases.add(new VarHandleSourceAccessTestCase(
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   133
                                "index out of bounds", bav, vh, h -> testArrayIndexOutOfBounds(bas, h),
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   134
                                false));
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   135
                        cases.add(new VarHandleSourceAccessTestCase(
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   136
                                "misaligned access", bav, vh, h -> testArrayMisalignedAccess(bas, h),
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   137
                                false));
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   138
                    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   139
                    else {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   140
                        ByteBufferSource bbs = (ByteBufferSource) bav;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   141
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   142
                        if (MemoryMode.READ_WRITE.isSet(bav.memoryModes)) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   143
                            cases.add(new VarHandleSourceAccessTestCase(
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   144
                                    "read write", bav, vh, h -> testArrayReadWrite(bbs, h),
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   145
                                    true));
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   146
                        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   147
                        else {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   148
                            cases.add(new VarHandleSourceAccessTestCase(
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   149
                                    "read only", bav, vh, h -> testArrayReadOnly(bbs, h),
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   150
                                    true));
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   151
                        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   152
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   153
                        cases.add(new VarHandleSourceAccessTestCase(
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   154
                                "unsupported", bav, vh, h -> testArrayUnsupported(bbs, h),
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   155
                                false));
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   156
                        cases.add(new VarHandleSourceAccessTestCase(
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   157
                                "index out of bounds", bav, vh, h -> testArrayIndexOutOfBounds(bbs, h),
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   158
                                false));
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   159
                        cases.add(new VarHandleSourceAccessTestCase(
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   160
                                "misaligned access", bav, vh, h -> testArrayMisalignedAccess(bbs, h),
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   161
                                false));
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   162
                    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   163
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   164
            }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   165
        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   166
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   167
        // Work around issue with jtreg summary reporting which truncates
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   168
        // the String result of Object.toString to 30 characters, hence
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   169
        // the first dummy argument
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   170
        return cases.stream().map(tc -> new Object[]{tc.toString(), tc}).toArray(Object[][]::new);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   171
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   172
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   173
    @Test(dataProvider = "accessTestCaseProvider")
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   174
    public <T> void testAccess(String desc, AccessTestCase<T> atc) throws Throwable {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   175
        T t = atc.get();
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   176
        int iters = atc.requiresLoop() ? ITERS : 1;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   177
        for (int c = 0; c < iters; c++) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   178
            atc.testAccess(t);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   179
        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   180
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   181
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   182
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   183
    static void testArrayUnsupported(ByteArraySource bs, VarHandleSource vhs) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   184
        VarHandle vh = vhs.s;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   185
        byte[] array = bs.s;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   186
        int ci = 1;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   187
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   188
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   189
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   190
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   191
    static void testArrayUnsupported(ByteBufferSource bs, VarHandleSource vhs) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   192
        VarHandle vh = vhs.s;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   193
        ByteBuffer array = bs.s;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   194
        int ci = 0;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   195
        boolean readOnly = MemoryMode.READ_ONLY.isSet(bs.memoryModes);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   196
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   197
        if (readOnly) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   198
            checkROBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   199
                vh.set(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   200
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   201
        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   202
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   203
        if (readOnly) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   204
            checkROBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   205
                vh.setVolatile(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   206
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   207
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   208
            checkROBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   209
                vh.setRelease(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   210
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   211
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   212
            checkROBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   213
                vh.setOpaque(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   214
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   215
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   216
            checkROBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   217
                boolean r = vh.compareAndSet(array, ci, VALUE_1, VALUE_2);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   218
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   219
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   220
            checkROBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   221
                int r = (int) vh.compareAndExchangeVolatile(array, ci, VALUE_2, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   222
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   223
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   224
            checkROBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   225
                int r = (int) vh.compareAndExchangeAcquire(array, ci, VALUE_2, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   226
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   227
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   228
            checkROBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   229
                int r = (int) vh.compareAndExchangeRelease(array, ci, VALUE_2, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   230
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   231
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   232
            checkROBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   233
                boolean r = vh.weakCompareAndSet(array, ci, VALUE_1, VALUE_2);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   234
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   235
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   236
            checkROBE(() -> {
37719
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   237
                boolean r = vh.weakCompareAndSetVolatile(array, ci, VALUE_1, VALUE_2);
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   238
            });
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   239
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   240
            checkROBE(() -> {
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   241
                boolean r = vh.weakCompareAndSetAcquire(array, ci, VALUE_1, VALUE_2);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   242
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   243
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   244
            checkROBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   245
                boolean r = vh.weakCompareAndSetRelease(array, ci, VALUE_1, VALUE_2);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   246
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   247
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   248
            checkROBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   249
                int o = (int) vh.getAndSet(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   250
            });
38328
40435a469d25 8156485: MethodHandles.varHandleExactInvoker should perform exact checks
psandoz
parents: 37719
diff changeset
   251
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   252
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   253
            checkROBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   254
                int o = (int) vh.getAndAdd(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   255
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   256
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   257
            checkROBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   258
                int o = (int) vh.addAndGet(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   259
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   260
        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   261
        else {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   262
        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   263
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   264
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   265
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   266
    static void testArrayIndexOutOfBounds(ByteArraySource bs, VarHandleSource vhs) throws Throwable {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   267
        VarHandle vh = vhs.s;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   268
        byte[] array = bs.s;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   269
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   270
        int length = array.length - SIZE + 1;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   271
        for (int i : new int[]{-1, Integer.MIN_VALUE, length, length + 1, Integer.MAX_VALUE}) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   272
            final int ci = i;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   273
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   274
            checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   275
                int x = (int) vh.get(array, ci);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   276
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   277
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   278
            checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   279
                vh.set(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   280
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   281
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   282
            checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   283
                int x = (int) vh.getVolatile(array, ci);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   284
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   285
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   286
            checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   287
                int x = (int) vh.getAcquire(array, ci);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   288
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   289
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   290
            checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   291
                int x = (int) vh.getOpaque(array, ci);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   292
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   293
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   294
            checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   295
                vh.setVolatile(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   296
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   297
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   298
            checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   299
                vh.setRelease(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   300
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   301
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   302
            checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   303
                vh.setOpaque(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   304
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   305
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   306
            checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   307
                boolean r = vh.compareAndSet(array, ci, VALUE_1, VALUE_2);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   308
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   309
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   310
            checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   311
                int r = (int) vh.compareAndExchangeVolatile(array, ci, VALUE_2, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   312
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   313
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   314
            checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   315
                int r = (int) vh.compareAndExchangeAcquire(array, ci, VALUE_2, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   316
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   317
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   318
            checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   319
                int r = (int) vh.compareAndExchangeRelease(array, ci, VALUE_2, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   320
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   321
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   322
            checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   323
                boolean r = vh.weakCompareAndSet(array, ci, VALUE_1, VALUE_2);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   324
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   325
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   326
            checkIOOBE(() -> {
37719
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   327
                boolean r = vh.weakCompareAndSetVolatile(array, ci, VALUE_1, VALUE_2);
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   328
            });
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   329
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   330
            checkIOOBE(() -> {
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   331
                boolean r = vh.weakCompareAndSetAcquire(array, ci, VALUE_1, VALUE_2);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   332
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   333
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   334
            checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   335
                boolean r = vh.weakCompareAndSetRelease(array, ci, VALUE_1, VALUE_2);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   336
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   337
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   338
            checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   339
                int o = (int) vh.getAndSet(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   340
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   341
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   342
            checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   343
                int o = (int) vh.getAndAdd(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   344
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   345
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   346
            checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   347
                int o = (int) vh.addAndGet(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   348
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   349
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   350
        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   351
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   352
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   353
    static void testArrayIndexOutOfBounds(ByteBufferSource bs, VarHandleSource vhs) throws Throwable {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   354
        VarHandle vh = vhs.s;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   355
        ByteBuffer array = bs.s;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   356
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   357
        boolean readOnly = MemoryMode.READ_ONLY.isSet(bs.memoryModes);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   358
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   359
        int length = array.limit() - SIZE + 1;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   360
        for (int i : new int[]{-1, Integer.MIN_VALUE, length, length + 1, Integer.MAX_VALUE}) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   361
            final int ci = i;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   362
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   363
            checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   364
                int x = (int) vh.get(array, ci);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   365
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   366
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   367
            if (!readOnly) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   368
                checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   369
                    vh.set(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   370
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   371
            }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   372
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   373
            checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   374
                int x = (int) vh.getVolatile(array, ci);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   375
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   376
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   377
            checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   378
                int x = (int) vh.getAcquire(array, ci);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   379
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   380
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   381
            checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   382
                int x = (int) vh.getOpaque(array, ci);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   383
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   384
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   385
            if (!readOnly) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   386
                checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   387
                    vh.setVolatile(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   388
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   389
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   390
                checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   391
                    vh.setRelease(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   392
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   393
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   394
                checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   395
                    vh.setOpaque(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   396
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   397
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   398
                checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   399
                    boolean r = vh.compareAndSet(array, ci, VALUE_1, VALUE_2);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   400
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   401
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   402
                checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   403
                    int r = (int) vh.compareAndExchangeVolatile(array, ci, VALUE_2, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   404
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   405
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   406
                checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   407
                    int r = (int) vh.compareAndExchangeAcquire(array, ci, VALUE_2, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   408
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   409
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   410
                checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   411
                    int r = (int) vh.compareAndExchangeRelease(array, ci, VALUE_2, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   412
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   413
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   414
                checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   415
                    boolean r = vh.weakCompareAndSet(array, ci, VALUE_1, VALUE_2);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   416
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   417
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   418
                checkIOOBE(() -> {
37719
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   419
                    boolean r = vh.weakCompareAndSetVolatile(array, ci, VALUE_1, VALUE_2);
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   420
                });
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   421
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   422
                checkIOOBE(() -> {
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   423
                    boolean r = vh.weakCompareAndSetAcquire(array, ci, VALUE_1, VALUE_2);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   424
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   425
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   426
                checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   427
                    boolean r = vh.weakCompareAndSetRelease(array, ci, VALUE_1, VALUE_2);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   428
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   429
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   430
                checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   431
                    int o = (int) vh.getAndSet(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   432
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   433
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   434
                checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   435
                    int o = (int) vh.getAndAdd(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   436
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   437
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   438
                checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   439
                    int o = (int) vh.addAndGet(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   440
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   441
            }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   442
        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   443
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   444
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   445
    static void testArrayMisalignedAccess(ByteArraySource bs, VarHandleSource vhs) throws Throwable {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   446
        VarHandle vh = vhs.s;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   447
        byte[] array = bs.s;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   448
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   449
        int misalignmentAtZero = ByteBuffer.wrap(array).alignmentOffset(0, SIZE);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   450
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   451
        int length = array.length - SIZE + 1;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   452
        for (int i = 0; i < length; i++) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   453
            boolean iAligned = ((i + misalignmentAtZero) & (SIZE - 1)) == 0;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   454
            final int ci = i;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   455
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   456
            if (!iAligned) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   457
                checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   458
                    int x = (int) vh.getVolatile(array, ci);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   459
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   460
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   461
                checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   462
                    int x = (int) vh.getAcquire(array, ci);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   463
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   464
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   465
                checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   466
                    int x = (int) vh.getOpaque(array, ci);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   467
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   468
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   469
                checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   470
                    vh.setVolatile(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   471
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   472
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   473
                checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   474
                    vh.setRelease(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   475
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   476
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   477
                checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   478
                    vh.setOpaque(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   479
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   480
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   481
                checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   482
                    boolean r = vh.compareAndSet(array, ci, VALUE_1, VALUE_2);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   483
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   484
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   485
                checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   486
                    int r = (int) vh.compareAndExchangeVolatile(array, ci, VALUE_2, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   487
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   488
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   489
                checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   490
                    int r = (int) vh.compareAndExchangeAcquire(array, ci, VALUE_2, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   491
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   492
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   493
                checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   494
                    int r = (int) vh.compareAndExchangeRelease(array, ci, VALUE_2, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   495
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   496
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   497
                checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   498
                    boolean r = vh.weakCompareAndSet(array, ci, VALUE_1, VALUE_2);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   499
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   500
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   501
                checkISE(() -> {
37719
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   502
                    boolean r = vh.weakCompareAndSetVolatile(array, ci, VALUE_1, VALUE_2);
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   503
                });
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   504
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   505
                checkISE(() -> {
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   506
                    boolean r = vh.weakCompareAndSetAcquire(array, ci, VALUE_1, VALUE_2);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   507
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   508
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   509
                checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   510
                    boolean r = vh.weakCompareAndSetRelease(array, ci, VALUE_1, VALUE_2);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   511
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   512
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   513
                checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   514
                    int o = (int) vh.getAndSet(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   515
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   516
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   517
                checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   518
                    int o = (int) vh.getAndAdd(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   519
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   520
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   521
                checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   522
                    int o = (int) vh.addAndGet(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   523
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   524
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   525
            }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   526
        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   527
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   528
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   529
    static void testArrayMisalignedAccess(ByteBufferSource bs, VarHandleSource vhs) throws Throwable {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   530
        VarHandle vh = vhs.s;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   531
        ByteBuffer array = bs.s;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   532
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   533
        boolean readOnly = MemoryMode.READ_ONLY.isSet(bs.memoryModes);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   534
        int misalignmentAtZero = array.alignmentOffset(0, SIZE);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   535
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   536
        int length = array.limit() - SIZE + 1;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   537
        for (int i = 0; i < length; i++) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   538
            boolean iAligned = ((i + misalignmentAtZero) & (SIZE - 1)) == 0;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   539
            final int ci = i;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   540
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   541
            if (!iAligned) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   542
                checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   543
                    int x = (int) vh.getVolatile(array, ci);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   544
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   545
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   546
                checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   547
                    int x = (int) vh.getAcquire(array, ci);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   548
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   549
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   550
                checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   551
                    int x = (int) vh.getOpaque(array, ci);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   552
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   553
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   554
                if (!readOnly) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   555
                    checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   556
                        vh.setVolatile(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   557
                    });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   558
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   559
                    checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   560
                        vh.setRelease(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   561
                    });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   562
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   563
                    checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   564
                        vh.setOpaque(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   565
                    });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   566
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   567
                    checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   568
                        boolean r = vh.compareAndSet(array, ci, VALUE_1, VALUE_2);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   569
                    });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   570
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   571
                    checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   572
                        int r = (int) vh.compareAndExchangeVolatile(array, ci, VALUE_2, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   573
                    });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   574
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   575
                    checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   576
                        int r = (int) vh.compareAndExchangeAcquire(array, ci, VALUE_2, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   577
                    });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   578
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   579
                    checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   580
                        int r = (int) vh.compareAndExchangeRelease(array, ci, VALUE_2, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   581
                    });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   582
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   583
                    checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   584
                        boolean r = vh.weakCompareAndSet(array, ci, VALUE_1, VALUE_2);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   585
                    });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   586
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   587
                    checkISE(() -> {
37719
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   588
                        boolean r = vh.weakCompareAndSetVolatile(array, ci, VALUE_1, VALUE_2);
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   589
                    });
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   590
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   591
                    checkISE(() -> {
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   592
                        boolean r = vh.weakCompareAndSetAcquire(array, ci, VALUE_1, VALUE_2);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   593
                    });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   594
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   595
                    checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   596
                        boolean r = vh.weakCompareAndSetRelease(array, ci, VALUE_1, VALUE_2);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   597
                    });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   598
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   599
                    checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   600
                        int o = (int) vh.getAndSet(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   601
                    });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   602
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   603
                    checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   604
                        int o = (int) vh.getAndAdd(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   605
                    });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   606
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   607
                    checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   608
                        int o = (int) vh.addAndGet(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   609
                    });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   610
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   611
            }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   612
        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   613
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   614
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   615
    static void testArrayReadWrite(ByteArraySource bs, VarHandleSource vhs) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   616
        VarHandle vh = vhs.s;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   617
        byte[] array = bs.s;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   618
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   619
        int misalignmentAtZero = ByteBuffer.wrap(array).alignmentOffset(0, SIZE);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   620
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   621
        bs.fill((byte) 0xff);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   622
        int length = array.length - SIZE + 1;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   623
        for (int i = 0; i < length; i++) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   624
            boolean iAligned = ((i + misalignmentAtZero) & (SIZE - 1)) == 0;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   625
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   626
            // Plain
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   627
            {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   628
                vh.set(array, i, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   629
                int x = (int) vh.get(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   630
                assertEquals(x, VALUE_1, "get int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   631
            }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   632
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   633
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   634
            if (iAligned) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   635
                // Volatile
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   636
                {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   637
                    vh.setVolatile(array, i, VALUE_2);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   638
                    int x = (int) vh.getVolatile(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   639
                    assertEquals(x, VALUE_2, "setVolatile int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   640
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   641
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   642
                // Lazy
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   643
                {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   644
                    vh.setRelease(array, i, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   645
                    int x = (int) vh.getAcquire(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   646
                    assertEquals(x, VALUE_1, "setRelease int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   647
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   648
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   649
                // Opaque
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   650
                {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   651
                    vh.setOpaque(array, i, VALUE_2);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   652
                    int x = (int) vh.getOpaque(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   653
                    assertEquals(x, VALUE_2, "setOpaque int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   654
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   655
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   656
                vh.set(array, i, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   657
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   658
                // Compare
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   659
                {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   660
                    boolean r = vh.compareAndSet(array, i, VALUE_1, VALUE_2);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   661
                    assertEquals(r, true, "success compareAndSet int");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   662
                    int x = (int) vh.get(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   663
                    assertEquals(x, VALUE_2, "success compareAndSet int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   664
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   665
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   666
                {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   667
                    boolean r = vh.compareAndSet(array, i, VALUE_1, VALUE_3);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   668
                    assertEquals(r, false, "failing compareAndSet int");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   669
                    int x = (int) vh.get(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   670
                    assertEquals(x, VALUE_2, "failing compareAndSet int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   671
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   672
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   673
                {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   674
                    int r = (int) vh.compareAndExchangeVolatile(array, i, VALUE_2, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   675
                    assertEquals(r, VALUE_2, "success compareAndExchangeVolatile int");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   676
                    int x = (int) vh.get(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   677
                    assertEquals(x, VALUE_1, "success compareAndExchangeVolatile int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   678
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   679
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   680
                {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   681
                    int r = (int) vh.compareAndExchangeVolatile(array, i, VALUE_2, VALUE_3);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   682
                    assertEquals(r, VALUE_1, "failing compareAndExchangeVolatile int");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   683
                    int x = (int) vh.get(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   684
                    assertEquals(x, VALUE_1, "failing compareAndExchangeVolatile int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   685
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   686
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   687
                {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   688
                    int r = (int) vh.compareAndExchangeAcquire(array, i, VALUE_1, VALUE_2);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   689
                    assertEquals(r, VALUE_1, "success compareAndExchangeAcquire int");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   690
                    int x = (int) vh.get(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   691
                    assertEquals(x, VALUE_2, "success compareAndExchangeAcquire int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   692
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   693
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   694
                {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   695
                    int r = (int) vh.compareAndExchangeAcquire(array, i, VALUE_1, VALUE_3);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   696
                    assertEquals(r, VALUE_2, "failing compareAndExchangeAcquire int");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   697
                    int x = (int) vh.get(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   698
                    assertEquals(x, VALUE_2, "failing compareAndExchangeAcquire int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   699
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   700
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   701
                {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   702
                    int r = (int) vh.compareAndExchangeRelease(array, i, VALUE_2, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   703
                    assertEquals(r, VALUE_2, "success compareAndExchangeRelease int");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   704
                    int x = (int) vh.get(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   705
                    assertEquals(x, VALUE_1, "success compareAndExchangeRelease int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   706
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   707
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   708
                {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   709
                    int r = (int) vh.compareAndExchangeRelease(array, i, VALUE_2, VALUE_3);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   710
                    assertEquals(r, VALUE_1, "failing compareAndExchangeRelease int");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   711
                    int x = (int) vh.get(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   712
                    assertEquals(x, VALUE_1, "failing compareAndExchangeRelease int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   713
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   714
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   715
                {
38355
674cfd9b90cf 8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents: 37343
diff changeset
   716
                    boolean success = false;
674cfd9b90cf 8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents: 37343
diff changeset
   717
                    for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
674cfd9b90cf 8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents: 37343
diff changeset
   718
                        success = vh.weakCompareAndSet(array, i, VALUE_1, VALUE_2);
674cfd9b90cf 8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents: 37343
diff changeset
   719
                    }
674cfd9b90cf 8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents: 37343
diff changeset
   720
                    assertEquals(success, true, "weakCompareAndSet int");
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   721
                    int x = (int) vh.get(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   722
                    assertEquals(x, VALUE_2, "weakCompareAndSet int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   723
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   724
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   725
                {
38355
674cfd9b90cf 8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents: 37343
diff changeset
   726
                    boolean success = false;
674cfd9b90cf 8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents: 37343
diff changeset
   727
                    for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
674cfd9b90cf 8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents: 37343
diff changeset
   728
                        success = vh.weakCompareAndSetAcquire(array, i, VALUE_2, VALUE_1);
674cfd9b90cf 8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents: 37343
diff changeset
   729
                    }
674cfd9b90cf 8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents: 37343
diff changeset
   730
                    assertEquals(success, true, "weakCompareAndSetAcquire int");
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   731
                    int x = (int) vh.get(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   732
                    assertEquals(x, VALUE_1, "weakCompareAndSetAcquire int");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   733
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   734
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   735
                {
38355
674cfd9b90cf 8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents: 37343
diff changeset
   736
                    boolean success = false;
674cfd9b90cf 8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents: 37343
diff changeset
   737
                    for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
674cfd9b90cf 8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents: 37343
diff changeset
   738
                        success = vh.weakCompareAndSetRelease(array, i, VALUE_1, VALUE_2);
674cfd9b90cf 8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents: 37343
diff changeset
   739
                    }
674cfd9b90cf 8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents: 37343
diff changeset
   740
                    assertEquals(success, true, "weakCompareAndSetRelease int");
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   741
                    int x = (int) vh.get(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   742
                    assertEquals(x, VALUE_2, "weakCompareAndSetRelease int");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   743
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   744
37719
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   745
                {
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   746
                    boolean r = vh.weakCompareAndSetVolatile(array, i, VALUE_2, VALUE_1);
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   747
                    assertEquals(r, true, "weakCompareAndSetVolatile int");
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   748
                    int x = (int) vh.get(array, i);
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   749
                    assertEquals(x, VALUE_1, "weakCompareAndSetVolatile int value");
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   750
                }
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   751
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   752
                // Compare set and get
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   753
                {
37719
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   754
                    int o = (int) vh.getAndSet(array, i, VALUE_2);
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   755
                    assertEquals(o, VALUE_1, "getAndSet int");
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   756
                    int x = (int) vh.get(array, i);
37719
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   757
                    assertEquals(x, VALUE_2, "getAndSet int value");
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   758
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   759
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   760
                vh.set(array, i, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   761
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   762
                // get and add, add and get
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   763
                {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   764
                    int o = (int) vh.getAndAdd(array, i, VALUE_3);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   765
                    assertEquals(o, VALUE_1, "getAndAdd int");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   766
                    int c = (int) vh.addAndGet(array, i, VALUE_3);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   767
                    assertEquals(c, VALUE_1 + VALUE_3 + VALUE_3, "getAndAdd int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   768
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   769
            }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   770
        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   771
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   772
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   773
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   774
    static void testArrayReadWrite(ByteBufferSource bs, VarHandleSource vhs) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   775
        VarHandle vh = vhs.s;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   776
        ByteBuffer array = bs.s;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   777
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   778
        int misalignmentAtZero = array.alignmentOffset(0, SIZE);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   779
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   780
        bs.fill((byte) 0xff);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   781
        int length = array.limit() - SIZE + 1;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   782
        for (int i = 0; i < length; i++) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   783
            boolean iAligned = ((i + misalignmentAtZero) & (SIZE - 1)) == 0;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   784
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   785
            // Plain
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   786
            {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   787
                vh.set(array, i, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   788
                int x = (int) vh.get(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   789
                assertEquals(x, VALUE_1, "get int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   790
            }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   791
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   792
            if (iAligned) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   793
                // Volatile
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   794
                {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   795
                    vh.setVolatile(array, i, VALUE_2);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   796
                    int x = (int) vh.getVolatile(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   797
                    assertEquals(x, VALUE_2, "setVolatile int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   798
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   799
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   800
                // Lazy
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   801
                {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   802
                    vh.setRelease(array, i, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   803
                    int x = (int) vh.getAcquire(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   804
                    assertEquals(x, VALUE_1, "setRelease int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   805
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   806
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   807
                // Opaque
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   808
                {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   809
                    vh.setOpaque(array, i, VALUE_2);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   810
                    int x = (int) vh.getOpaque(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   811
                    assertEquals(x, VALUE_2, "setOpaque int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   812
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   813
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   814
                vh.set(array, i, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   815
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   816
                // Compare
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   817
                {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   818
                    boolean r = vh.compareAndSet(array, i, VALUE_1, VALUE_2);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   819
                    assertEquals(r, true, "success compareAndSet int");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   820
                    int x = (int) vh.get(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   821
                    assertEquals(x, VALUE_2, "success compareAndSet int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   822
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   823
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   824
                {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   825
                    boolean r = vh.compareAndSet(array, i, VALUE_1, VALUE_3);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   826
                    assertEquals(r, false, "failing compareAndSet int");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   827
                    int x = (int) vh.get(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   828
                    assertEquals(x, VALUE_2, "failing compareAndSet int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   829
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   830
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   831
                {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   832
                    int r = (int) vh.compareAndExchangeVolatile(array, i, VALUE_2, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   833
                    assertEquals(r, VALUE_2, "success compareAndExchangeVolatile int");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   834
                    int x = (int) vh.get(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   835
                    assertEquals(x, VALUE_1, "success compareAndExchangeVolatile int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   836
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   837
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   838
                {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   839
                    int r = (int) vh.compareAndExchangeVolatile(array, i, VALUE_2, VALUE_3);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   840
                    assertEquals(r, VALUE_1, "failing compareAndExchangeVolatile int");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   841
                    int x = (int) vh.get(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   842
                    assertEquals(x, VALUE_1, "failing compareAndExchangeVolatile int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   843
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   844
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   845
                {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   846
                    int r = (int) vh.compareAndExchangeAcquire(array, i, VALUE_1, VALUE_2);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   847
                    assertEquals(r, VALUE_1, "success compareAndExchangeAcquire int");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   848
                    int x = (int) vh.get(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   849
                    assertEquals(x, VALUE_2, "success compareAndExchangeAcquire int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   850
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   851
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   852
                {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   853
                    int r = (int) vh.compareAndExchangeAcquire(array, i, VALUE_1, VALUE_3);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   854
                    assertEquals(r, VALUE_2, "failing compareAndExchangeAcquire int");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   855
                    int x = (int) vh.get(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   856
                    assertEquals(x, VALUE_2, "failing compareAndExchangeAcquire int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   857
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   858
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   859
                {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   860
                    int r = (int) vh.compareAndExchangeRelease(array, i, VALUE_2, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   861
                    assertEquals(r, VALUE_2, "success compareAndExchangeRelease int");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   862
                    int x = (int) vh.get(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   863
                    assertEquals(x, VALUE_1, "success compareAndExchangeRelease int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   864
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   865
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   866
                {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   867
                    int r = (int) vh.compareAndExchangeRelease(array, i, VALUE_2, VALUE_3);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   868
                    assertEquals(r, VALUE_1, "failing compareAndExchangeRelease int");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   869
                    int x = (int) vh.get(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   870
                    assertEquals(x, VALUE_1, "failing compareAndExchangeRelease int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   871
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   872
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   873
                {
38355
674cfd9b90cf 8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents: 37343
diff changeset
   874
                    boolean success = false;
674cfd9b90cf 8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents: 37343
diff changeset
   875
                    for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
674cfd9b90cf 8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents: 37343
diff changeset
   876
                        success = vh.weakCompareAndSet(array, i, VALUE_1, VALUE_2);
674cfd9b90cf 8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents: 37343
diff changeset
   877
                    }
674cfd9b90cf 8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents: 37343
diff changeset
   878
                    assertEquals(success, true, "weakCompareAndSet int");
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   879
                    int x = (int) vh.get(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   880
                    assertEquals(x, VALUE_2, "weakCompareAndSet int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   881
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   882
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   883
                {
38355
674cfd9b90cf 8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents: 37343
diff changeset
   884
                    boolean success = false;
674cfd9b90cf 8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents: 37343
diff changeset
   885
                    for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
674cfd9b90cf 8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents: 37343
diff changeset
   886
                        success = vh.weakCompareAndSetAcquire(array, i, VALUE_2, VALUE_1);
674cfd9b90cf 8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents: 37343
diff changeset
   887
                    }
674cfd9b90cf 8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents: 37343
diff changeset
   888
                    assertEquals(success, true, "weakCompareAndSetAcquire int");
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   889
                    int x = (int) vh.get(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   890
                    assertEquals(x, VALUE_1, "weakCompareAndSetAcquire int");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   891
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   892
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   893
                {
38355
674cfd9b90cf 8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents: 37343
diff changeset
   894
                    boolean success = false;
674cfd9b90cf 8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents: 37343
diff changeset
   895
                    for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
674cfd9b90cf 8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents: 37343
diff changeset
   896
                        success = vh.weakCompareAndSetRelease(array, i, VALUE_1, VALUE_2);
674cfd9b90cf 8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents: 37343
diff changeset
   897
                    }
674cfd9b90cf 8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents: 37343
diff changeset
   898
                    assertEquals(success, true, "weakCompareAndSetRelease int");
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   899
                    int x = (int) vh.get(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   900
                    assertEquals(x, VALUE_2, "weakCompareAndSetRelease int");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   901
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   902
37719
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   903
                {
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   904
                    boolean r = vh.weakCompareAndSetVolatile(array, i, VALUE_2, VALUE_1);
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   905
                    assertEquals(r, true, "weakCompareAndSetVolatile int");
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   906
                    int x = (int) vh.get(array, i);
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   907
                    assertEquals(x, VALUE_1, "weakCompareAndSetVolatile int value");
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   908
                }
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   909
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   910
                // Compare set and get
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   911
                {
37719
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   912
                    int o = (int) vh.getAndSet(array, i, VALUE_2);
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   913
                    assertEquals(o, VALUE_1, "getAndSet int");
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   914
                    int x = (int) vh.get(array, i);
37719
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   915
                    assertEquals(x, VALUE_2, "getAndSet int value");
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   916
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   917
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   918
                vh.set(array, i, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   919
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   920
                // get and add, add and get
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   921
                {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   922
                    int o = (int) vh.getAndAdd(array, i, VALUE_3);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   923
                    assertEquals(o, VALUE_1, "getAndAdd int");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   924
                    int c = (int) vh.addAndGet(array, i, VALUE_3);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   925
                    assertEquals(c, VALUE_1 + VALUE_3 + VALUE_3, "getAndAdd int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   926
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   927
            }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   928
        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   929
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   930
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   931
    static void testArrayReadOnly(ByteBufferSource bs, VarHandleSource vhs) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   932
        VarHandle vh = vhs.s;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   933
        ByteBuffer array = bs.s;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   935
        int misalignmentAtZero = array.alignmentOffset(0, SIZE);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   936
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   937
        ByteBuffer bb = ByteBuffer.allocate(SIZE);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   938
        bb.order(MemoryMode.BIG_ENDIAN.isSet(vhs.memoryModes) ? ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   939
        bs.fill(bb.putInt(0, VALUE_2).array());
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   940
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   941
        int length = array.limit() - SIZE + 1;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   942
        for (int i = 0; i < length; i++) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   943
            boolean iAligned = ((i + misalignmentAtZero) & (SIZE - 1)) == 0;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   944
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   945
            int v = MemoryMode.BIG_ENDIAN.isSet(vhs.memoryModes)
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   946
                    ? rotateLeft(VALUE_2, (i % SIZE) << 3)
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   947
                    : rotateRight(VALUE_2, (i % SIZE) << 3);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   948
            // Plain
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   949
            {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   950
                int x = (int) vh.get(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   951
                assertEquals(x, v, "get int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   952
            }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   953
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   954
            if (iAligned) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   955
                // Volatile
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   956
                {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   957
                    int x = (int) vh.getVolatile(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   958
                    assertEquals(x, v, "getVolatile int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   959
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   960
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   961
                // Lazy
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   962
                {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   963
                    int x = (int) vh.getAcquire(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   964
                    assertEquals(x, v, "getRelease int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   965
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   966
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   967
                // Opaque
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   968
                {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   969
                    int x = (int) vh.getOpaque(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   970
                    assertEquals(x, v, "getOpaque int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   971
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   972
            }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   973
        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   974
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   975
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   976
}
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   977