jdk/test/java/lang/invoke/VarHandles/VarHandleTestByteArrayAsInt.java
author psandoz
Thu, 19 May 2016 16:08:40 +0200
changeset 38429 0fd11baf20c3
parent 38382 98d5a441bc2f
child 39472 6df82f4c63ac
permissions -rw-r--r--
8157239: java/lang/invoke/VarHandles/ tests fail by timeout with -Xcomp with lambda form linkage Reviewed-by: vlivanov
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
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    30
 */
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    31
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    32
import org.testng.annotations.DataProvider;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    33
import org.testng.annotations.Test;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    34
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    35
import java.lang.invoke.MethodHandles;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    36
import java.lang.invoke.VarHandle;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    37
import java.nio.ByteBuffer;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    38
import java.nio.ByteOrder;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    39
import java.util.ArrayList;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    40
import java.util.Arrays;
37719
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
    41
import java.util.EnumSet;
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    42
import java.util.List;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    43
37719
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
    44
import static org.testng.Assert.*;
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    45
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    46
public class VarHandleTestByteArrayAsInt extends VarHandleBaseByteArrayTest {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    47
    static final int SIZE = Integer.BYTES;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    48
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    49
    static final int VALUE_1 = 0x01020304;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    50
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    51
    static final int VALUE_2 = 0x11121314;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    52
38382
98d5a441bc2f 8157152: Atomic add for VarHandle byte[]/ByteBuffer views is incorrect for endian conversion
psandoz
parents: 38368
diff changeset
    53
    static final int VALUE_3 = 0xFFFEFDFC;
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    54
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    55
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    56
    @Override
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    57
    public void setupVarHandleSources() {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    58
        // Combinations of VarHandle byte[] or ByteBuffer
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    59
        vhss = new ArrayList<>();
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    60
        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
    61
34a002e5168a 8154556: Use java.nio.ByteOrder instead of boolean value
psandoz
parents: 37343
diff changeset
    62
            ByteOrder bo = endianess == MemoryMode.BIG_ENDIAN
34a002e5168a 8154556: Use java.nio.ByteOrder instead of boolean value
psandoz
parents: 37343
diff changeset
    63
                    ? ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN;
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    64
            VarHandleSource aeh = new VarHandleSource(
37668
34a002e5168a 8154556: Use java.nio.ByteOrder instead of boolean value
psandoz
parents: 37343
diff changeset
    65
                    MethodHandles.byteArrayViewVarHandle(int[].class, bo),
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    66
                    endianess, MemoryMode.READ_WRITE);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    67
            vhss.add(aeh);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    68
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    69
            VarHandleSource bbh = new VarHandleSource(
37668
34a002e5168a 8154556: Use java.nio.ByteOrder instead of boolean value
psandoz
parents: 37343
diff changeset
    70
                    MethodHandles.byteBufferViewVarHandle(int[].class, bo),
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    71
                    endianess, MemoryMode.READ_WRITE);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    72
            vhss.add(bbh);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    73
        }
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
    @Test(dataProvider = "varHandlesProvider")
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    78
    public void testIsAccessModeSupported(VarHandleSource vhs) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    79
        VarHandle vh = vhs.s;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    80
37343
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
    81
        assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET));
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
    82
        assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET));
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    83
37343
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
    84
        assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_VOLATILE));
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
    85
        assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET_VOLATILE));
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
    86
        assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_ACQUIRE));
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
    87
        assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET_RELEASE));
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
    88
        assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_OPAQUE));
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
    89
        assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET_OPAQUE));
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    90
37343
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
    91
        assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_SET));
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
    92
        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
    93
        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
    94
        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
    95
        assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET));
37719
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
    96
        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
    97
        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
    98
        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
    99
        assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_SET));
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   100
37343
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
   101
        assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_ADD));
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
   102
        assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.ADD_AND_GET));
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   103
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   104
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   105
    @Test(dataProvider = "typesProvider")
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   106
    public void testTypes(VarHandle vh, List<java.lang.Class<?>> pts) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   107
        assertEquals(vh.varType(), int.class);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   108
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   109
        assertEquals(vh.coordinateTypes(), pts);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   110
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   111
        testTypes(vh);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   112
    }
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
    @DataProvider
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   116
    public Object[][] accessTestCaseProvider() throws Exception {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   117
        List<AccessTestCase<?>> cases = new ArrayList<>();
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   118
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   119
        for (ByteArrayViewSource<?> bav : bavss) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   120
            for (VarHandleSource vh : vhss) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   121
                if (vh.matches(bav)) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   122
                    if (bav instanceof ByteArraySource) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   123
                        ByteArraySource bas = (ByteArraySource) bav;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   124
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   125
                        cases.add(new VarHandleSourceAccessTestCase(
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   126
                                "read write", bav, vh, h -> testArrayReadWrite(bas, h),
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   127
                                true));
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   128
                        cases.add(new VarHandleSourceAccessTestCase(
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   129
                                "unsupported", bav, vh, h -> testArrayUnsupported(bas, h),
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   130
                                false));
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   131
                        cases.add(new VarHandleSourceAccessTestCase(
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   132
                                "index out of bounds", bav, vh, h -> testArrayIndexOutOfBounds(bas, h),
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   133
                                false));
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   134
                        cases.add(new VarHandleSourceAccessTestCase(
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   135
                                "misaligned access", bav, vh, h -> testArrayMisalignedAccess(bas, h),
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   136
                                false));
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   137
                    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   138
                    else {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   139
                        ByteBufferSource bbs = (ByteBufferSource) bav;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   140
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   141
                        if (MemoryMode.READ_WRITE.isSet(bav.memoryModes)) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   142
                            cases.add(new VarHandleSourceAccessTestCase(
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   143
                                    "read write", bav, vh, h -> testArrayReadWrite(bbs, h),
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   144
                                    true));
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   145
                        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   146
                        else {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   147
                            cases.add(new VarHandleSourceAccessTestCase(
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   148
                                    "read only", bav, vh, h -> testArrayReadOnly(bbs, h),
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   149
                                    true));
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   150
                        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   151
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   152
                        cases.add(new VarHandleSourceAccessTestCase(
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   153
                                "unsupported", bav, vh, h -> testArrayUnsupported(bbs, h),
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   154
                                false));
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   155
                        cases.add(new VarHandleSourceAccessTestCase(
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   156
                                "index out of bounds", bav, vh, h -> testArrayIndexOutOfBounds(bbs, h),
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   157
                                false));
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   158
                        cases.add(new VarHandleSourceAccessTestCase(
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   159
                                "misaligned access", bav, vh, h -> testArrayMisalignedAccess(bbs, h),
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   160
                                false));
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   161
                    }
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
        // Work around issue with jtreg summary reporting which truncates
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   167
        // the String result of Object.toString to 30 characters, hence
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   168
        // the first dummy argument
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   169
        return cases.stream().map(tc -> new Object[]{tc.toString(), tc}).toArray(Object[][]::new);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   170
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   171
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   172
    @Test(dataProvider = "accessTestCaseProvider")
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   173
    public <T> void testAccess(String desc, AccessTestCase<T> atc) throws Throwable {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   174
        T t = atc.get();
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   175
        int iters = atc.requiresLoop() ? ITERS : 1;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   176
        for (int c = 0; c < iters; c++) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   177
            atc.testAccess(t);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   178
        }
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
    static void testArrayUnsupported(ByteArraySource bs, VarHandleSource vhs) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   183
        VarHandle vh = vhs.s;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   184
        byte[] array = bs.s;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   185
        int ci = 1;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   186
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
    static void testArrayUnsupported(ByteBufferSource bs, VarHandleSource vhs) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   191
        VarHandle vh = vhs.s;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   192
        ByteBuffer array = bs.s;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   193
        int ci = 0;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   194
        boolean readOnly = MemoryMode.READ_ONLY.isSet(bs.memoryModes);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   195
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   196
        if (readOnly) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   197
            checkROBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   198
                vh.set(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   199
            });
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
        if (readOnly) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   203
            checkROBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   204
                vh.setVolatile(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   205
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   206
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   207
            checkROBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   208
                vh.setRelease(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   209
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   210
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   211
            checkROBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   212
                vh.setOpaque(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   213
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   214
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   215
            checkROBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   216
                boolean r = vh.compareAndSet(array, ci, VALUE_1, VALUE_2);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   217
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   218
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   219
            checkROBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   220
                int r = (int) vh.compareAndExchangeVolatile(array, ci, VALUE_2, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   221
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   222
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   223
            checkROBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   224
                int r = (int) vh.compareAndExchangeAcquire(array, ci, VALUE_2, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   225
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   226
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   227
            checkROBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   228
                int r = (int) vh.compareAndExchangeRelease(array, ci, VALUE_2, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   229
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   230
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   231
            checkROBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   232
                boolean r = vh.weakCompareAndSet(array, ci, VALUE_1, VALUE_2);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   233
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   234
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   235
            checkROBE(() -> {
37719
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   236
                boolean r = vh.weakCompareAndSetVolatile(array, ci, VALUE_1, VALUE_2);
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   237
            });
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
            checkROBE(() -> {
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   240
                boolean r = vh.weakCompareAndSetAcquire(array, ci, VALUE_1, VALUE_2);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   241
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   242
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   243
            checkROBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   244
                boolean r = vh.weakCompareAndSetRelease(array, ci, VALUE_1, VALUE_2);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   245
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   246
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   247
            checkROBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   248
                int o = (int) vh.getAndSet(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   249
            });
38328
40435a469d25 8156485: MethodHandles.varHandleExactInvoker should perform exact checks
psandoz
parents: 37719
diff changeset
   250
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   251
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   252
            checkROBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   253
                int o = (int) vh.getAndAdd(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   254
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   255
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   256
            checkROBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   257
                int o = (int) vh.addAndGet(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   258
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   259
        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   260
        else {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   261
        }
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
    static void testArrayIndexOutOfBounds(ByteArraySource bs, VarHandleSource vhs) throws Throwable {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   266
        VarHandle vh = vhs.s;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   267
        byte[] array = bs.s;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   268
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   269
        int length = array.length - SIZE + 1;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   270
        for (int i : new int[]{-1, Integer.MIN_VALUE, length, length + 1, Integer.MAX_VALUE}) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   271
            final int ci = i;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   272
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   273
            checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   274
                int x = (int) vh.get(array, ci);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   275
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   276
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   277
            checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   278
                vh.set(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   279
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   280
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   281
            checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   282
                int x = (int) vh.getVolatile(array, ci);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   283
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   284
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   285
            checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   286
                int x = (int) vh.getAcquire(array, ci);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   287
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   288
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   289
            checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   290
                int x = (int) vh.getOpaque(array, ci);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   291
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   292
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   293
            checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   294
                vh.setVolatile(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   295
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   296
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   297
            checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   298
                vh.setRelease(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   299
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   300
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   301
            checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   302
                vh.setOpaque(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   303
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   304
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   305
            checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   306
                boolean r = vh.compareAndSet(array, ci, VALUE_1, VALUE_2);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   307
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   308
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   309
            checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   310
                int r = (int) vh.compareAndExchangeVolatile(array, ci, VALUE_2, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   311
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   312
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   313
            checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   314
                int r = (int) vh.compareAndExchangeAcquire(array, ci, VALUE_2, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   315
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   316
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   317
            checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   318
                int r = (int) vh.compareAndExchangeRelease(array, ci, VALUE_2, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   319
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   320
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   321
            checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   322
                boolean r = vh.weakCompareAndSet(array, ci, VALUE_1, VALUE_2);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   323
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   324
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   325
            checkIOOBE(() -> {
37719
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   326
                boolean r = vh.weakCompareAndSetVolatile(array, ci, VALUE_1, VALUE_2);
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   327
            });
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
            checkIOOBE(() -> {
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   330
                boolean r = vh.weakCompareAndSetAcquire(array, ci, VALUE_1, VALUE_2);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   331
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   332
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   333
            checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   334
                boolean r = vh.weakCompareAndSetRelease(array, ci, VALUE_1, VALUE_2);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   335
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   336
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   337
            checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   338
                int o = (int) vh.getAndSet(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   339
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   340
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   341
            checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   342
                int o = (int) vh.getAndAdd(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   343
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   344
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   345
            checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   346
                int o = (int) vh.addAndGet(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   347
            });
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
    static void testArrayIndexOutOfBounds(ByteBufferSource bs, VarHandleSource vhs) throws Throwable {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   353
        VarHandle vh = vhs.s;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   354
        ByteBuffer array = bs.s;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   355
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   356
        boolean readOnly = MemoryMode.READ_ONLY.isSet(bs.memoryModes);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   357
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   358
        int length = array.limit() - SIZE + 1;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   359
        for (int i : new int[]{-1, Integer.MIN_VALUE, length, length + 1, Integer.MAX_VALUE}) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   360
            final int ci = i;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   361
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   362
            checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   363
                int x = (int) vh.get(array, ci);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   364
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   365
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   366
            if (!readOnly) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   367
                checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   368
                    vh.set(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   369
                });
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
            checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   373
                int x = (int) vh.getVolatile(array, ci);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   374
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   375
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   376
            checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   377
                int x = (int) vh.getAcquire(array, ci);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   378
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   379
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   380
            checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   381
                int x = (int) vh.getOpaque(array, ci);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   382
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   383
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   384
            if (!readOnly) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   385
                checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   386
                    vh.setVolatile(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   387
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   388
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   389
                checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   390
                    vh.setRelease(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   391
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   392
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   393
                checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   394
                    vh.setOpaque(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   395
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   396
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   397
                checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   398
                    boolean r = vh.compareAndSet(array, ci, VALUE_1, VALUE_2);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   399
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   400
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   401
                checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   402
                    int r = (int) vh.compareAndExchangeVolatile(array, ci, VALUE_2, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   403
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   404
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   405
                checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   406
                    int r = (int) vh.compareAndExchangeAcquire(array, ci, VALUE_2, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   407
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   408
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   409
                checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   410
                    int r = (int) vh.compareAndExchangeRelease(array, ci, VALUE_2, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   411
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   412
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   413
                checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   414
                    boolean r = vh.weakCompareAndSet(array, ci, VALUE_1, VALUE_2);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   415
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   416
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   417
                checkIOOBE(() -> {
37719
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   418
                    boolean r = vh.weakCompareAndSetVolatile(array, ci, VALUE_1, VALUE_2);
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   419
                });
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
                checkIOOBE(() -> {
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   422
                    boolean r = vh.weakCompareAndSetAcquire(array, ci, VALUE_1, VALUE_2);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   423
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   424
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   425
                checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   426
                    boolean r = vh.weakCompareAndSetRelease(array, ci, VALUE_1, VALUE_2);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   427
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   428
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   429
                checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   430
                    int o = (int) vh.getAndSet(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   431
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   432
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   433
                checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   434
                    int o = (int) vh.getAndAdd(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   435
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   436
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   437
                checkIOOBE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   438
                    int o = (int) vh.addAndGet(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   439
                });
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
    static void testArrayMisalignedAccess(ByteArraySource bs, VarHandleSource vhs) throws Throwable {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   445
        VarHandle vh = vhs.s;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   446
        byte[] array = bs.s;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   447
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   448
        int misalignmentAtZero = ByteBuffer.wrap(array).alignmentOffset(0, SIZE);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   449
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   450
        int length = array.length - SIZE + 1;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   451
        for (int i = 0; i < length; i++) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   452
            boolean iAligned = ((i + misalignmentAtZero) & (SIZE - 1)) == 0;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   453
            final int ci = i;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   454
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   455
            if (!iAligned) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   456
                checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   457
                    int x = (int) vh.getVolatile(array, ci);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   458
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   459
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   460
                checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   461
                    int x = (int) vh.getAcquire(array, ci);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   462
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   463
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   464
                checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   465
                    int x = (int) vh.getOpaque(array, ci);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   466
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   467
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   468
                checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   469
                    vh.setVolatile(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   470
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   471
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   472
                checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   473
                    vh.setRelease(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   474
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   475
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   476
                checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   477
                    vh.setOpaque(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   478
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   479
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   480
                checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   481
                    boolean r = vh.compareAndSet(array, ci, VALUE_1, VALUE_2);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   482
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   483
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   484
                checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   485
                    int r = (int) vh.compareAndExchangeVolatile(array, ci, VALUE_2, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   486
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   487
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   488
                checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   489
                    int r = (int) vh.compareAndExchangeAcquire(array, ci, VALUE_2, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   490
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   491
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   492
                checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   493
                    int r = (int) vh.compareAndExchangeRelease(array, ci, VALUE_2, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   494
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   495
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   496
                checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   497
                    boolean r = vh.weakCompareAndSet(array, ci, VALUE_1, VALUE_2);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   498
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   499
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   500
                checkISE(() -> {
37719
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   501
                    boolean r = vh.weakCompareAndSetVolatile(array, ci, VALUE_1, VALUE_2);
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   502
                });
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
                checkISE(() -> {
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   505
                    boolean r = vh.weakCompareAndSetAcquire(array, ci, VALUE_1, VALUE_2);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   506
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   507
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   508
                checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   509
                    boolean r = vh.weakCompareAndSetRelease(array, ci, VALUE_1, VALUE_2);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   510
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   511
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   512
                checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   513
                    int o = (int) vh.getAndSet(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   514
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   515
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   516
                checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   517
                    int o = (int) vh.getAndAdd(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   518
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   519
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   520
                checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   521
                    int o = (int) vh.addAndGet(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   522
                });
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
    static void testArrayMisalignedAccess(ByteBufferSource bs, VarHandleSource vhs) throws Throwable {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   529
        VarHandle vh = vhs.s;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   530
        ByteBuffer array = bs.s;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   531
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   532
        boolean readOnly = MemoryMode.READ_ONLY.isSet(bs.memoryModes);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   533
        int misalignmentAtZero = array.alignmentOffset(0, SIZE);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   534
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   535
        int length = array.limit() - SIZE + 1;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   536
        for (int i = 0; i < length; i++) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   537
            boolean iAligned = ((i + misalignmentAtZero) & (SIZE - 1)) == 0;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   538
            final int ci = i;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   539
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   540
            if (!iAligned) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   541
                checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   542
                    int x = (int) vh.getVolatile(array, ci);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   543
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   544
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   545
                checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   546
                    int x = (int) vh.getAcquire(array, ci);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   547
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   548
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   549
                checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   550
                    int x = (int) vh.getOpaque(array, ci);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   551
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   552
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   553
                if (!readOnly) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   554
                    checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   555
                        vh.setVolatile(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   556
                    });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   557
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   558
                    checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   559
                        vh.setRelease(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   560
                    });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   561
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   562
                    checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   563
                        vh.setOpaque(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   564
                    });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   565
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   566
                    checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   567
                        boolean r = vh.compareAndSet(array, ci, VALUE_1, VALUE_2);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   568
                    });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   569
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   570
                    checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   571
                        int r = (int) vh.compareAndExchangeVolatile(array, ci, VALUE_2, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   572
                    });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   573
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   574
                    checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   575
                        int r = (int) vh.compareAndExchangeAcquire(array, ci, VALUE_2, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   576
                    });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   577
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   578
                    checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   579
                        int r = (int) vh.compareAndExchangeRelease(array, ci, VALUE_2, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   580
                    });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   581
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   582
                    checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   583
                        boolean r = vh.weakCompareAndSet(array, ci, VALUE_1, VALUE_2);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   584
                    });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   585
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   586
                    checkISE(() -> {
37719
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   587
                        boolean r = vh.weakCompareAndSetVolatile(array, ci, VALUE_1, VALUE_2);
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   588
                    });
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
                    checkISE(() -> {
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   591
                        boolean r = vh.weakCompareAndSetAcquire(array, ci, VALUE_1, VALUE_2);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   592
                    });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   593
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   594
                    checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   595
                        boolean r = vh.weakCompareAndSetRelease(array, ci, VALUE_1, VALUE_2);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   596
                    });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   597
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   598
                    checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   599
                        int o = (int) vh.getAndSet(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   600
                    });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   601
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   602
                    checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   603
                        int o = (int) vh.getAndAdd(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   604
                    });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   605
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   606
                    checkISE(() -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   607
                        int o = (int) vh.addAndGet(array, ci, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   608
                    });
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
    static void testArrayReadWrite(ByteArraySource bs, VarHandleSource vhs) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   615
        VarHandle vh = vhs.s;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   616
        byte[] array = bs.s;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   617
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   618
        int misalignmentAtZero = ByteBuffer.wrap(array).alignmentOffset(0, SIZE);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   619
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   620
        bs.fill((byte) 0xff);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   621
        int length = array.length - SIZE + 1;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   622
        for (int i = 0; i < length; i++) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   623
            boolean iAligned = ((i + misalignmentAtZero) & (SIZE - 1)) == 0;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   624
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   625
            // Plain
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   626
            {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   627
                vh.set(array, i, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   628
                int x = (int) vh.get(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   629
                assertEquals(x, VALUE_1, "get int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   630
            }
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
            if (iAligned) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   634
                // Volatile
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   635
                {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   636
                    vh.setVolatile(array, i, VALUE_2);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   637
                    int x = (int) vh.getVolatile(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   638
                    assertEquals(x, VALUE_2, "setVolatile int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   639
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   640
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   641
                // Lazy
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   642
                {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   643
                    vh.setRelease(array, i, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   644
                    int x = (int) vh.getAcquire(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   645
                    assertEquals(x, VALUE_1, "setRelease int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   646
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   647
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   648
                // Opaque
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   649
                {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   650
                    vh.setOpaque(array, i, VALUE_2);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   651
                    int x = (int) vh.getOpaque(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   652
                    assertEquals(x, VALUE_2, "setOpaque int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   653
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   654
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   655
                vh.set(array, i, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   656
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   657
                // Compare
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   658
                {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   659
                    boolean r = vh.compareAndSet(array, i, VALUE_1, VALUE_2);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   660
                    assertEquals(r, true, "success compareAndSet int");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   661
                    int x = (int) vh.get(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   662
                    assertEquals(x, VALUE_2, "success compareAndSet int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   663
                }
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
                    boolean r = vh.compareAndSet(array, i, VALUE_1, VALUE_3);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   667
                    assertEquals(r, false, "failing compareAndSet int");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   668
                    int x = (int) vh.get(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   669
                    assertEquals(x, VALUE_2, "failing compareAndSet int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   670
                }
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
                    int r = (int) vh.compareAndExchangeVolatile(array, i, VALUE_2, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   674
                    assertEquals(r, VALUE_2, "success compareAndExchangeVolatile int");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   675
                    int x = (int) vh.get(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   676
                    assertEquals(x, VALUE_1, "success compareAndExchangeVolatile int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   677
                }
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
                    int r = (int) vh.compareAndExchangeVolatile(array, i, VALUE_2, VALUE_3);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   681
                    assertEquals(r, VALUE_1, "failing compareAndExchangeVolatile int");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   682
                    int x = (int) vh.get(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   683
                    assertEquals(x, VALUE_1, "failing compareAndExchangeVolatile int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   684
                }
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
                    int r = (int) vh.compareAndExchangeAcquire(array, i, VALUE_1, VALUE_2);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   688
                    assertEquals(r, VALUE_1, "success compareAndExchangeAcquire int");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   689
                    int x = (int) vh.get(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   690
                    assertEquals(x, VALUE_2, "success compareAndExchangeAcquire int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   691
                }
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
                    int r = (int) vh.compareAndExchangeAcquire(array, i, VALUE_1, VALUE_3);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   695
                    assertEquals(r, VALUE_2, "failing compareAndExchangeAcquire int");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   696
                    int x = (int) vh.get(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   697
                    assertEquals(x, VALUE_2, "failing compareAndExchangeAcquire int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   698
                }
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
                    int r = (int) vh.compareAndExchangeRelease(array, i, VALUE_2, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   702
                    assertEquals(r, VALUE_2, "success compareAndExchangeRelease int");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   703
                    int x = (int) vh.get(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   704
                    assertEquals(x, VALUE_1, "success compareAndExchangeRelease int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   705
                }
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
                    int r = (int) vh.compareAndExchangeRelease(array, i, VALUE_2, VALUE_3);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   709
                    assertEquals(r, VALUE_1, "failing compareAndExchangeRelease int");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   710
                    int x = (int) vh.get(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   711
                    assertEquals(x, VALUE_1, "failing compareAndExchangeRelease int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   712
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   713
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   714
                {
38355
674cfd9b90cf 8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents: 37343
diff changeset
   715
                    boolean success = false;
674cfd9b90cf 8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents: 37343
diff changeset
   716
                    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
   717
                        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
   718
                    }
674cfd9b90cf 8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents: 37343
diff changeset
   719
                    assertEquals(success, true, "weakCompareAndSet int");
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   720
                    int x = (int) vh.get(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   721
                    assertEquals(x, VALUE_2, "weakCompareAndSet int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   722
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   723
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   724
                {
38355
674cfd9b90cf 8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents: 37343
diff changeset
   725
                    boolean success = false;
674cfd9b90cf 8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents: 37343
diff changeset
   726
                    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
   727
                        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
   728
                    }
674cfd9b90cf 8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents: 37343
diff changeset
   729
                    assertEquals(success, true, "weakCompareAndSetAcquire int");
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   730
                    int x = (int) vh.get(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   731
                    assertEquals(x, VALUE_1, "weakCompareAndSetAcquire int");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   732
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   733
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   734
                {
38355
674cfd9b90cf 8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents: 37343
diff changeset
   735
                    boolean success = false;
674cfd9b90cf 8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents: 37343
diff changeset
   736
                    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
   737
                        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
   738
                    }
674cfd9b90cf 8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents: 37343
diff changeset
   739
                    assertEquals(success, true, "weakCompareAndSetRelease int");
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   740
                    int x = (int) vh.get(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   741
                    assertEquals(x, VALUE_2, "weakCompareAndSetRelease int");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   742
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   743
37719
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   744
                {
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   745
                    boolean r = vh.weakCompareAndSetVolatile(array, i, VALUE_2, VALUE_1);
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   746
                    assertEquals(r, true, "weakCompareAndSetVolatile int");
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   747
                    int x = (int) vh.get(array, i);
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   748
                    assertEquals(x, VALUE_1, "weakCompareAndSetVolatile int value");
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   749
                }
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   750
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   751
                // Compare set and get
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   752
                {
37719
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   753
                    int o = (int) vh.getAndSet(array, i, VALUE_2);
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   754
                    assertEquals(o, VALUE_1, "getAndSet int");
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   755
                    int x = (int) vh.get(array, i);
37719
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   756
                    assertEquals(x, VALUE_2, "getAndSet int value");
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   757
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   758
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   759
                vh.set(array, i, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   760
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   761
                // get and add, add and get
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   762
                {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   763
                    int o = (int) vh.getAndAdd(array, i, VALUE_3);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   764
                    assertEquals(o, VALUE_1, "getAndAdd int");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   765
                    int c = (int) vh.addAndGet(array, i, VALUE_3);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   766
                    assertEquals(c, VALUE_1 + VALUE_3 + VALUE_3, "getAndAdd int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   767
                }
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
    static void testArrayReadWrite(ByteBufferSource bs, VarHandleSource vhs) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   774
        VarHandle vh = vhs.s;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   775
        ByteBuffer array = bs.s;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   776
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   777
        int misalignmentAtZero = array.alignmentOffset(0, SIZE);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   778
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   779
        bs.fill((byte) 0xff);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   780
        int length = array.limit() - SIZE + 1;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   781
        for (int i = 0; i < length; i++) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   782
            boolean iAligned = ((i + misalignmentAtZero) & (SIZE - 1)) == 0;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   783
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   784
            // Plain
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   785
            {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   786
                vh.set(array, i, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   787
                int x = (int) vh.get(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   788
                assertEquals(x, VALUE_1, "get int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   789
            }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   790
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   791
            if (iAligned) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   792
                // Volatile
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   793
                {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   794
                    vh.setVolatile(array, i, VALUE_2);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   795
                    int x = (int) vh.getVolatile(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   796
                    assertEquals(x, VALUE_2, "setVolatile int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   797
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   798
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   799
                // Lazy
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   800
                {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   801
                    vh.setRelease(array, i, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   802
                    int x = (int) vh.getAcquire(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   803
                    assertEquals(x, VALUE_1, "setRelease int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   804
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   805
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   806
                // Opaque
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   807
                {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   808
                    vh.setOpaque(array, i, VALUE_2);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   809
                    int x = (int) vh.getOpaque(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   810
                    assertEquals(x, VALUE_2, "setOpaque int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   811
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   812
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   813
                vh.set(array, i, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   814
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   815
                // Compare
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   816
                {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   817
                    boolean r = vh.compareAndSet(array, i, VALUE_1, VALUE_2);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   818
                    assertEquals(r, true, "success compareAndSet int");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   819
                    int x = (int) vh.get(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   820
                    assertEquals(x, VALUE_2, "success compareAndSet int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   821
                }
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
                    boolean r = vh.compareAndSet(array, i, VALUE_1, VALUE_3);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   825
                    assertEquals(r, false, "failing compareAndSet int");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   826
                    int x = (int) vh.get(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   827
                    assertEquals(x, VALUE_2, "failing compareAndSet int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   828
                }
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
                    int r = (int) vh.compareAndExchangeVolatile(array, i, VALUE_2, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   832
                    assertEquals(r, VALUE_2, "success compareAndExchangeVolatile int");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   833
                    int x = (int) vh.get(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   834
                    assertEquals(x, VALUE_1, "success compareAndExchangeVolatile int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   835
                }
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
                    int r = (int) vh.compareAndExchangeVolatile(array, i, VALUE_2, VALUE_3);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   839
                    assertEquals(r, VALUE_1, "failing compareAndExchangeVolatile int");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   840
                    int x = (int) vh.get(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   841
                    assertEquals(x, VALUE_1, "failing compareAndExchangeVolatile int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   842
                }
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
                    int r = (int) vh.compareAndExchangeAcquire(array, i, VALUE_1, VALUE_2);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   846
                    assertEquals(r, VALUE_1, "success compareAndExchangeAcquire int");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   847
                    int x = (int) vh.get(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   848
                    assertEquals(x, VALUE_2, "success compareAndExchangeAcquire int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   849
                }
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
                    int r = (int) vh.compareAndExchangeAcquire(array, i, VALUE_1, VALUE_3);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   853
                    assertEquals(r, VALUE_2, "failing compareAndExchangeAcquire int");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   854
                    int x = (int) vh.get(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   855
                    assertEquals(x, VALUE_2, "failing compareAndExchangeAcquire int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   856
                }
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
                    int r = (int) vh.compareAndExchangeRelease(array, i, VALUE_2, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   860
                    assertEquals(r, VALUE_2, "success compareAndExchangeRelease int");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   861
                    int x = (int) vh.get(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   862
                    assertEquals(x, VALUE_1, "success compareAndExchangeRelease int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   863
                }
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
                    int r = (int) vh.compareAndExchangeRelease(array, i, VALUE_2, VALUE_3);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   867
                    assertEquals(r, VALUE_1, "failing compareAndExchangeRelease int");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   868
                    int x = (int) vh.get(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   869
                    assertEquals(x, VALUE_1, "failing compareAndExchangeRelease int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   870
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   871
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   872
                {
38355
674cfd9b90cf 8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents: 37343
diff changeset
   873
                    boolean success = false;
674cfd9b90cf 8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents: 37343
diff changeset
   874
                    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
   875
                        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
   876
                    }
674cfd9b90cf 8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents: 37343
diff changeset
   877
                    assertEquals(success, true, "weakCompareAndSet int");
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   878
                    int x = (int) vh.get(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   879
                    assertEquals(x, VALUE_2, "weakCompareAndSet int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   880
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   881
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   882
                {
38355
674cfd9b90cf 8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents: 37343
diff changeset
   883
                    boolean success = false;
674cfd9b90cf 8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents: 37343
diff changeset
   884
                    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
   885
                        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
   886
                    }
674cfd9b90cf 8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents: 37343
diff changeset
   887
                    assertEquals(success, true, "weakCompareAndSetAcquire int");
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   888
                    int x = (int) vh.get(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   889
                    assertEquals(x, VALUE_1, "weakCompareAndSetAcquire int");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   890
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   891
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   892
                {
38355
674cfd9b90cf 8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents: 37343
diff changeset
   893
                    boolean success = false;
674cfd9b90cf 8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents: 37343
diff changeset
   894
                    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
   895
                        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
   896
                    }
674cfd9b90cf 8155739: [TESTBUG] VarHandles/Unsafe tests for weakCAS should allow spurious failures
shade
parents: 37343
diff changeset
   897
                    assertEquals(success, true, "weakCompareAndSetRelease int");
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   898
                    int x = (int) vh.get(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   899
                    assertEquals(x, VALUE_2, "weakCompareAndSetRelease int");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   900
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   901
37719
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   902
                {
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   903
                    boolean r = vh.weakCompareAndSetVolatile(array, i, VALUE_2, VALUE_1);
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   904
                    assertEquals(r, true, "weakCompareAndSetVolatile int");
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   905
                    int x = (int) vh.get(array, i);
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   906
                    assertEquals(x, VALUE_1, "weakCompareAndSetVolatile int value");
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   907
                }
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   908
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   909
                // Compare set and get
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   910
                {
37719
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   911
                    int o = (int) vh.getAndSet(array, i, VALUE_2);
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   912
                    assertEquals(o, VALUE_1, "getAndSet int");
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   913
                    int x = (int) vh.get(array, i);
37719
add11bc0e6e2 8154755: Add a VarHandle weakCompareAndSet with volatile semantics
psandoz
parents: 37668
diff changeset
   914
                    assertEquals(x, VALUE_2, "getAndSet int value");
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   915
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   916
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   917
                vh.set(array, i, VALUE_1);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   918
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   919
                // get and add, add and get
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   920
                {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   921
                    int o = (int) vh.getAndAdd(array, i, VALUE_3);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   922
                    assertEquals(o, VALUE_1, "getAndAdd int");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   923
                    int c = (int) vh.addAndGet(array, i, VALUE_3);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   924
                    assertEquals(c, VALUE_1 + VALUE_3 + VALUE_3, "getAndAdd int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   925
                }
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
    static void testArrayReadOnly(ByteBufferSource bs, VarHandleSource vhs) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   931
        VarHandle vh = vhs.s;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   932
        ByteBuffer array = bs.s;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   933
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   934
        int misalignmentAtZero = array.alignmentOffset(0, SIZE);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   935
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   936
        ByteBuffer bb = ByteBuffer.allocate(SIZE);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   937
        bb.order(MemoryMode.BIG_ENDIAN.isSet(vhs.memoryModes) ? ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   938
        bs.fill(bb.putInt(0, VALUE_2).array());
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   939
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   940
        int length = array.limit() - SIZE + 1;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   941
        for (int i = 0; i < length; i++) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   942
            boolean iAligned = ((i + misalignmentAtZero) & (SIZE - 1)) == 0;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   943
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   944
            int v = MemoryMode.BIG_ENDIAN.isSet(vhs.memoryModes)
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   945
                    ? rotateLeft(VALUE_2, (i % SIZE) << 3)
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   946
                    : rotateRight(VALUE_2, (i % SIZE) << 3);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   947
            // Plain
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   948
            {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   949
                int x = (int) vh.get(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   950
                assertEquals(x, v, "get int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   951
            }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   952
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   953
            if (iAligned) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   954
                // Volatile
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   955
                {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   956
                    int x = (int) vh.getVolatile(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   957
                    assertEquals(x, v, "getVolatile int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   958
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   959
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   960
                // Lazy
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   961
                {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   962
                    int x = (int) vh.getAcquire(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   963
                    assertEquals(x, v, "getRelease int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   964
                }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   965
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   966
                // Opaque
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   967
                {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   968
                    int x = (int) vh.getOpaque(array, i);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   969
                    assertEquals(x, v, "getOpaque int value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   970
                }
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