jdk/test/java/lang/invoke/VarHandles/VarHandleTestMethodHandleAccessDouble.java
author psandoz
Fri, 29 Apr 2016 13:46:19 -0700
changeset 37719 add11bc0e6e2
parent 37343 35a2231828a7
child 38328 40435a469d25
permissions -rw-r--r--
8154755: Add a VarHandle weakCompareAndSet with volatile semantics Reviewed-by: shade, 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
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    26
 * @run testng/othervm -Diters=20000 VarHandleTestMethodHandleAccessDouble
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    27
 */
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    28
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    29
import org.testng.annotations.BeforeClass;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    30
import org.testng.annotations.DataProvider;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    31
import org.testng.annotations.Test;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    32
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    33
import java.lang.invoke.MethodHandles;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    34
import java.lang.invoke.VarHandle;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    35
import java.util.ArrayList;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    36
import java.util.Arrays;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    37
import java.util.List;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    38
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    39
import static org.testng.Assert.*;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    40
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    41
public class VarHandleTestMethodHandleAccessDouble extends VarHandleBaseTest {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    42
    static final double static_final_v = 1.0d;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    43
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    44
    static double static_v;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    45
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    46
    final double final_v = 1.0d;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    47
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    48
    double v;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    49
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    50
    VarHandle vhFinalField;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    51
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    52
    VarHandle vhField;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    53
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    54
    VarHandle vhStaticField;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    55
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    56
    VarHandle vhStaticFinalField;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    57
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    58
    VarHandle vhArray;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    59
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    60
    @BeforeClass
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    61
    public void setup() throws Exception {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    62
        vhFinalField = MethodHandles.lookup().findVarHandle(
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    63
                VarHandleTestMethodHandleAccessDouble.class, "final_v", double.class);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    64
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    65
        vhField = MethodHandles.lookup().findVarHandle(
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    66
                VarHandleTestMethodHandleAccessDouble.class, "v", double.class);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    67
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    68
        vhStaticFinalField = MethodHandles.lookup().findStaticVarHandle(
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    69
            VarHandleTestMethodHandleAccessDouble.class, "static_final_v", double.class);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    70
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    71
        vhStaticField = MethodHandles.lookup().findStaticVarHandle(
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    72
            VarHandleTestMethodHandleAccessDouble.class, "static_v", double.class);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    73
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    74
        vhArray = MethodHandles.arrayElementVarHandle(double[].class);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    75
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    76
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    77
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    78
    @DataProvider
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    79
    public Object[][] accessTestCaseProvider() throws Exception {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    80
        List<AccessTestCase<?>> cases = new ArrayList<>();
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    81
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    82
        for (VarHandleToMethodHandle f : VarHandleToMethodHandle.values()) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    83
            cases.add(new MethodHandleAccessTestCase("Instance field",
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    84
                                                     vhField, f, hs -> testInstanceField(this, hs)));
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    85
            cases.add(new MethodHandleAccessTestCase("Instance field unsupported",
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    86
                                                     vhField, f, hs -> testInstanceFieldUnsupported(this, hs),
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    87
                                                     false));
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    88
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    89
            cases.add(new MethodHandleAccessTestCase("Static field",
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    90
                                                     vhStaticField, f, VarHandleTestMethodHandleAccessDouble::testStaticField));
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    91
            cases.add(new MethodHandleAccessTestCase("Static field unsupported",
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    92
                                                     vhStaticField, f, VarHandleTestMethodHandleAccessDouble::testStaticFieldUnsupported,
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    93
                                                     false));
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    94
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    95
            cases.add(new MethodHandleAccessTestCase("Array",
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    96
                                                     vhArray, f, VarHandleTestMethodHandleAccessDouble::testArray));
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    97
            cases.add(new MethodHandleAccessTestCase("Array unsupported",
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    98
                                                     vhArray, f, VarHandleTestMethodHandleAccessDouble::testArrayUnsupported,
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
    99
                                                     false));
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   100
            cases.add(new MethodHandleAccessTestCase("Array index out of bounds",
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   101
                                                     vhArray, f, VarHandleTestMethodHandleAccessDouble::testArrayIndexOutOfBounds,
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   102
                                                     false));
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
        // Work around issue with jtreg summary reporting which truncates
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   106
        // the String result of Object.toString to 30 characters, hence
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   107
        // the first dummy argument
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   108
        return cases.stream().map(tc -> new Object[]{tc.toString(), tc}).toArray(Object[][]::new);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   109
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   110
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   111
    @Test(dataProvider = "accessTestCaseProvider")
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   112
    public <T> void testAccess(String desc, AccessTestCase<T> atc) throws Throwable {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   113
        T t = atc.get();
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   114
        int iters = atc.requiresLoop() ? ITERS : 1;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   115
        for (int c = 0; c < iters; c++) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   116
            atc.testAccess(t);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   117
        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   118
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   119
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   120
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   121
    static void testInstanceField(VarHandleTestMethodHandleAccessDouble recv, Handles hs) throws Throwable {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   122
        // Plain
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   123
        {
37343
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
   124
            hs.get(TestAccessMode.SET).invokeExact(recv, 1.0d);
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
   125
            double x = (double) hs.get(TestAccessMode.GET).invokeExact(recv);
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   126
            assertEquals(x, 1.0d, "set double value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   127
        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   128
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   129
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   130
        // Volatile
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   131
        {
37343
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
   132
            hs.get(TestAccessMode.SET_VOLATILE).invokeExact(recv, 2.0d);
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
   133
            double x = (double) hs.get(TestAccessMode.GET_VOLATILE).invokeExact(recv);
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   134
            assertEquals(x, 2.0d, "setVolatile double value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   135
        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   136
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   137
        // Lazy
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   138
        {
37343
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
   139
            hs.get(TestAccessMode.SET_RELEASE).invokeExact(recv, 1.0d);
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
   140
            double x = (double) hs.get(TestAccessMode.GET_ACQUIRE).invokeExact(recv);
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   141
            assertEquals(x, 1.0d, "setRelease double value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   142
        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   143
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   144
        // Opaque
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   145
        {
37343
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
   146
            hs.get(TestAccessMode.SET_OPAQUE).invokeExact(recv, 2.0d);
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
   147
            double x = (double) hs.get(TestAccessMode.GET_OPAQUE).invokeExact(recv);
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   148
            assertEquals(x, 2.0d, "setOpaque double value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   149
        }
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
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   153
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   154
    static void testInstanceFieldUnsupported(VarHandleTestMethodHandleAccessDouble recv, Handles hs) throws Throwable {
37343
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
   155
        for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_SET)) {
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   156
            checkUOE(am, () -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   157
                boolean r = (boolean) hs.get(am).invokeExact(recv, 1.0d, 2.0d);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   158
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   159
        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   160
37343
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
   161
        for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_EXCHANGE)) {
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   162
            checkUOE(am, () -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   163
                double r = (double) hs.get(am).invokeExact(recv, 1.0d, 2.0d);
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
37343
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
   167
        for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_SET)) {
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   168
            checkUOE(am, () -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   169
                double r = (double) hs.get(am).invokeExact(recv, 1.0d);
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
37343
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
   173
        for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_ADD)) {
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   174
            checkUOE(am, () -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   175
                double r = (double) hs.get(am).invokeExact(recv, 1.0d);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   176
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   177
        }
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
    static void testStaticField(Handles hs) throws Throwable {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   182
        // Plain
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   183
        {
37343
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
   184
            hs.get(TestAccessMode.SET).invokeExact(1.0d);
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
   185
            double x = (double) hs.get(TestAccessMode.GET).invokeExact();
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   186
            assertEquals(x, 1.0d, "set double value");
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
        // Volatile
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   191
        {
37343
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
   192
            hs.get(TestAccessMode.SET_VOLATILE).invokeExact(2.0d);
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
   193
            double x = (double) hs.get(TestAccessMode.GET_VOLATILE).invokeExact();
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   194
            assertEquals(x, 2.0d, "setVolatile double value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   195
        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   196
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   197
        // Lazy
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   198
        {
37343
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
   199
            hs.get(TestAccessMode.SET_RELEASE).invokeExact(1.0d);
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
   200
            double x = (double) hs.get(TestAccessMode.GET_ACQUIRE).invokeExact();
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   201
            assertEquals(x, 1.0d, "setRelease double value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   202
        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   203
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   204
        // Opaque
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   205
        {
37343
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
   206
            hs.get(TestAccessMode.SET_OPAQUE).invokeExact(2.0d);
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
   207
            double x = (double) hs.get(TestAccessMode.GET_OPAQUE).invokeExact();
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   208
            assertEquals(x, 2.0d, "setOpaque double value");
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
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   212
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   213
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   214
    static void testStaticFieldUnsupported(Handles hs) throws Throwable {
37343
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
   215
        for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_SET)) {
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   216
            checkUOE(am, () -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   217
                boolean r = (boolean) hs.get(am).invokeExact(1.0d, 2.0d);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   218
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   219
        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   220
37343
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
   221
        for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_EXCHANGE)) {
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   222
            checkUOE(am, () -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   223
                double r = (double) hs.get(am).invokeExact(1.0d, 2.0d);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   224
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   225
        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   226
37343
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
   227
        for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_SET)) {
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   228
            checkUOE(am, () -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   229
                double r = (double) hs.get(am).invokeExact(1.0d);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   230
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   231
        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   232
37343
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
   233
        for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_ADD)) {
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   234
            checkUOE(am, () -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   235
                double r = (double) hs.get(am).invokeExact(1.0d);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   236
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   237
        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   238
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   239
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   240
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   241
    static void testArray(Handles hs) throws Throwable {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   242
        double[] array = new double[10];
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   243
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   244
        for (int i = 0; i < array.length; i++) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   245
            // Plain
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   246
            {
37343
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
   247
                hs.get(TestAccessMode.SET).invokeExact(array, i, 1.0d);
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
   248
                double x = (double) hs.get(TestAccessMode.GET).invokeExact(array, i);
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   249
                assertEquals(x, 1.0d, "get double value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   250
            }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   251
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   252
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   253
            // Volatile
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   254
            {
37343
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
   255
                hs.get(TestAccessMode.SET_VOLATILE).invokeExact(array, i, 2.0d);
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
   256
                double x = (double) hs.get(TestAccessMode.GET_VOLATILE).invokeExact(array, i);
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   257
                assertEquals(x, 2.0d, "setVolatile double value");
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
            // Lazy
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   261
            {
37343
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
   262
                hs.get(TestAccessMode.SET_RELEASE).invokeExact(array, i, 1.0d);
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
   263
                double x = (double) hs.get(TestAccessMode.GET_ACQUIRE).invokeExact(array, i);
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   264
                assertEquals(x, 1.0d, "setRelease double value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   265
            }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   266
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   267
            // Opaque
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   268
            {
37343
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
   269
                hs.get(TestAccessMode.SET_OPAQUE).invokeExact(array, i, 2.0d);
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
   270
                double x = (double) hs.get(TestAccessMode.GET_OPAQUE).invokeExact(array, i);
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   271
                assertEquals(x, 2.0d, "setOpaque double value");
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   272
            }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   273
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   274
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
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   278
    static void testArrayUnsupported(Handles hs) throws Throwable {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   279
        double[] array = new double[10];
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   280
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   281
        final int i = 0;
37343
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
   282
        for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_SET)) {
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   283
            checkUOE(am, () -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   284
                boolean r = (boolean) hs.get(am).invokeExact(array, i, 1.0d, 2.0d);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   285
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   286
        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   287
37343
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
   288
        for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_EXCHANGE)) {
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   289
            checkUOE(am, () -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   290
                double r = (double) hs.get(am).invokeExact(array, i, 1.0d, 2.0d);
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
37343
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
   294
        for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_SET)) {
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   295
            checkUOE(am, () -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   296
                double r = (double) hs.get(am).invokeExact(array, i, 1.0d);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   297
            });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   298
        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   299
37343
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
   300
        for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_ADD)) {
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   301
            checkUOE(am, () -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   302
                double o = (double) hs.get(am).invokeExact(array, i, 1.0d);
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
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   306
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   307
    static void testArrayIndexOutOfBounds(Handles hs) throws Throwable {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   308
        double[] array = new double[10];
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   309
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   310
        for (int i : new int[]{-1, Integer.MIN_VALUE, 10, 11, Integer.MAX_VALUE}) {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   311
            final int ci = i;
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   312
37343
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
   313
            for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET)) {
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   314
                checkIOOBE(am, () -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   315
                    double x = (double) hs.get(am).invokeExact(array, ci);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   316
                });
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   317
            }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   318
37343
35a2231828a7 8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents: 36934
diff changeset
   319
            for (TestAccessMode am : testAccessModesOfType(TestAccessType.SET)) {
36934
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   320
                checkIOOBE(am, () -> {
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   321
                    hs.get(am).invokeExact(array, ci, 1.0d);
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   322
                });
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
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   326
        }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   327
    }
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   328
}
590fc47a0aeb 8149644: Integrate VarHandles
psandoz
parents:
diff changeset
   329