Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | * accompanied this code). |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License version |
| 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
| 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | * or visit www.oracle.com if you need additional information or have any |
| 21 | * questions. |
| 22 | */ |
| 23 | |
| 24 | /* |
| 25 | * @test |
| 26 | * @run testng/othervm -Diters=20000 VarHandleTestMethodHandleAccessLong |
Paul Sandoz | 44afe20 | 2016-05-17 12:06:41 +0200 | [diff] [blame] | 27 | * @run testng/othervm -Diters=20000 -Djava.lang.invoke.VarHandle.VAR_HANDLE_GUARDS=false VarHandleTestMethodHandleAccessLong |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 28 | */ |
| 29 | |
| 30 | import org.testng.annotations.BeforeClass; |
| 31 | import org.testng.annotations.DataProvider; |
| 32 | import org.testng.annotations.Test; |
| 33 | |
| 34 | import java.lang.invoke.MethodHandles; |
| 35 | import java.lang.invoke.VarHandle; |
| 36 | import java.util.ArrayList; |
| 37 | import java.util.Arrays; |
| 38 | import java.util.List; |
| 39 | |
| 40 | import static org.testng.Assert.*; |
| 41 | |
| 42 | public class VarHandleTestMethodHandleAccessLong extends VarHandleBaseTest { |
| 43 | static final long static_final_v = 1L; |
| 44 | |
| 45 | static long static_v; |
| 46 | |
| 47 | final long final_v = 1L; |
| 48 | |
| 49 | long v; |
| 50 | |
| 51 | VarHandle vhFinalField; |
| 52 | |
| 53 | VarHandle vhField; |
| 54 | |
| 55 | VarHandle vhStaticField; |
| 56 | |
| 57 | VarHandle vhStaticFinalField; |
| 58 | |
| 59 | VarHandle vhArray; |
| 60 | |
| 61 | @BeforeClass |
| 62 | public void setup() throws Exception { |
| 63 | vhFinalField = MethodHandles.lookup().findVarHandle( |
| 64 | VarHandleTestMethodHandleAccessLong.class, "final_v", long.class); |
| 65 | |
| 66 | vhField = MethodHandles.lookup().findVarHandle( |
| 67 | VarHandleTestMethodHandleAccessLong.class, "v", long.class); |
| 68 | |
| 69 | vhStaticFinalField = MethodHandles.lookup().findStaticVarHandle( |
| 70 | VarHandleTestMethodHandleAccessLong.class, "static_final_v", long.class); |
| 71 | |
| 72 | vhStaticField = MethodHandles.lookup().findStaticVarHandle( |
| 73 | VarHandleTestMethodHandleAccessLong.class, "static_v", long.class); |
| 74 | |
| 75 | vhArray = MethodHandles.arrayElementVarHandle(long[].class); |
| 76 | } |
| 77 | |
| 78 | |
| 79 | @DataProvider |
| 80 | public Object[][] accessTestCaseProvider() throws Exception { |
| 81 | List<AccessTestCase<?>> cases = new ArrayList<>(); |
| 82 | |
| 83 | for (VarHandleToMethodHandle f : VarHandleToMethodHandle.values()) { |
| 84 | cases.add(new MethodHandleAccessTestCase("Instance field", |
| 85 | vhField, f, hs -> testInstanceField(this, hs))); |
| 86 | cases.add(new MethodHandleAccessTestCase("Instance field unsupported", |
| 87 | vhField, f, hs -> testInstanceFieldUnsupported(this, hs), |
| 88 | false)); |
| 89 | |
| 90 | cases.add(new MethodHandleAccessTestCase("Static field", |
| 91 | vhStaticField, f, VarHandleTestMethodHandleAccessLong::testStaticField)); |
| 92 | cases.add(new MethodHandleAccessTestCase("Static field unsupported", |
| 93 | vhStaticField, f, VarHandleTestMethodHandleAccessLong::testStaticFieldUnsupported, |
| 94 | false)); |
| 95 | |
| 96 | cases.add(new MethodHandleAccessTestCase("Array", |
| 97 | vhArray, f, VarHandleTestMethodHandleAccessLong::testArray)); |
| 98 | cases.add(new MethodHandleAccessTestCase("Array unsupported", |
| 99 | vhArray, f, VarHandleTestMethodHandleAccessLong::testArrayUnsupported, |
| 100 | false)); |
| 101 | cases.add(new MethodHandleAccessTestCase("Array index out of bounds", |
| 102 | vhArray, f, VarHandleTestMethodHandleAccessLong::testArrayIndexOutOfBounds, |
| 103 | false)); |
| 104 | } |
| 105 | |
| 106 | // Work around issue with jtreg summary reporting which truncates |
| 107 | // the String result of Object.toString to 30 characters, hence |
| 108 | // the first dummy argument |
| 109 | return cases.stream().map(tc -> new Object[]{tc.toString(), tc}).toArray(Object[][]::new); |
| 110 | } |
| 111 | |
| 112 | @Test(dataProvider = "accessTestCaseProvider") |
| 113 | public <T> void testAccess(String desc, AccessTestCase<T> atc) throws Throwable { |
| 114 | T t = atc.get(); |
| 115 | int iters = atc.requiresLoop() ? ITERS : 1; |
| 116 | for (int c = 0; c < iters; c++) { |
| 117 | atc.testAccess(t); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | |
| 122 | static void testInstanceField(VarHandleTestMethodHandleAccessLong recv, Handles hs) throws Throwable { |
| 123 | // Plain |
| 124 | { |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 125 | hs.get(TestAccessMode.SET).invokeExact(recv, 1L); |
| 126 | long x = (long) hs.get(TestAccessMode.GET).invokeExact(recv); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 127 | assertEquals(x, 1L, "set long value"); |
| 128 | } |
| 129 | |
| 130 | |
| 131 | // Volatile |
| 132 | { |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 133 | hs.get(TestAccessMode.SET_VOLATILE).invokeExact(recv, 2L); |
| 134 | long x = (long) hs.get(TestAccessMode.GET_VOLATILE).invokeExact(recv); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 135 | assertEquals(x, 2L, "setVolatile long value"); |
| 136 | } |
| 137 | |
| 138 | // Lazy |
| 139 | { |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 140 | hs.get(TestAccessMode.SET_RELEASE).invokeExact(recv, 1L); |
| 141 | long x = (long) hs.get(TestAccessMode.GET_ACQUIRE).invokeExact(recv); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 142 | assertEquals(x, 1L, "setRelease long value"); |
| 143 | } |
| 144 | |
| 145 | // Opaque |
| 146 | { |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 147 | hs.get(TestAccessMode.SET_OPAQUE).invokeExact(recv, 2L); |
| 148 | long x = (long) hs.get(TestAccessMode.GET_OPAQUE).invokeExact(recv); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 149 | assertEquals(x, 2L, "setOpaque long value"); |
| 150 | } |
| 151 | |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 152 | hs.get(TestAccessMode.SET).invokeExact(recv, 1L); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 153 | |
| 154 | // Compare |
| 155 | { |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 156 | boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(recv, 1L, 2L); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 157 | assertEquals(r, true, "success compareAndSet long"); |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 158 | long x = (long) hs.get(TestAccessMode.GET).invokeExact(recv); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 159 | assertEquals(x, 2L, "success compareAndSet long value"); |
| 160 | } |
| 161 | |
| 162 | { |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 163 | boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(recv, 1L, 3L); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 164 | assertEquals(r, false, "failing compareAndSet long"); |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 165 | long x = (long) hs.get(TestAccessMode.GET).invokeExact(recv); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 166 | assertEquals(x, 2L, "failing compareAndSet long value"); |
| 167 | } |
| 168 | |
| 169 | { |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 170 | long r = (long) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_VOLATILE).invokeExact(recv, 2L, 1L); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 171 | assertEquals(r, 2L, "success compareAndExchangeVolatile long"); |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 172 | long x = (long) hs.get(TestAccessMode.GET).invokeExact(recv); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 173 | assertEquals(x, 1L, "success compareAndExchangeVolatile long value"); |
| 174 | } |
| 175 | |
| 176 | { |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 177 | long r = (long) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_VOLATILE).invokeExact(recv, 2L, 3L); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 178 | assertEquals(r, 1L, "failing compareAndExchangeVolatile long"); |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 179 | long x = (long) hs.get(TestAccessMode.GET).invokeExact(recv); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 180 | assertEquals(x, 1L, "failing compareAndExchangeVolatile long value"); |
| 181 | } |
| 182 | |
| 183 | { |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 184 | long r = (long) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(recv, 1L, 2L); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 185 | assertEquals(r, 1L, "success compareAndExchangeAcquire long"); |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 186 | long x = (long) hs.get(TestAccessMode.GET).invokeExact(recv); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 187 | assertEquals(x, 2L, "success compareAndExchangeAcquire long value"); |
| 188 | } |
| 189 | |
| 190 | { |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 191 | long r = (long) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(recv, 1L, 3L); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 192 | assertEquals(r, 2L, "failing compareAndExchangeAcquire long"); |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 193 | long x = (long) hs.get(TestAccessMode.GET).invokeExact(recv); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 194 | assertEquals(x, 2L, "failing compareAndExchangeAcquire long value"); |
| 195 | } |
| 196 | |
| 197 | { |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 198 | long r = (long) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(recv, 2L, 1L); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 199 | assertEquals(r, 2L, "success compareAndExchangeRelease long"); |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 200 | long x = (long) hs.get(TestAccessMode.GET).invokeExact(recv); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 201 | assertEquals(x, 1L, "success compareAndExchangeRelease long value"); |
| 202 | } |
| 203 | |
| 204 | { |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 205 | long r = (long) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(recv, 2L, 3L); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 206 | assertEquals(r, 1L, "failing compareAndExchangeRelease long"); |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 207 | long x = (long) hs.get(TestAccessMode.GET).invokeExact(recv); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 208 | assertEquals(x, 1L, "failing compareAndExchangeRelease long value"); |
| 209 | } |
| 210 | |
| 211 | { |
Aleksey Shipilev | 4f53885 | 2016-05-04 17:17:28 +0300 | [diff] [blame] | 212 | boolean success = false; |
| 213 | for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { |
| 214 | success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET).invokeExact(recv, 1L, 2L); |
| 215 | } |
| 216 | assertEquals(success, true, "weakCompareAndSet long"); |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 217 | long x = (long) hs.get(TestAccessMode.GET).invokeExact(recv); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 218 | assertEquals(x, 2L, "weakCompareAndSet long value"); |
| 219 | } |
| 220 | |
| 221 | { |
Aleksey Shipilev | 4f53885 | 2016-05-04 17:17:28 +0300 | [diff] [blame] | 222 | boolean success = false; |
| 223 | for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { |
| 224 | success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_ACQUIRE).invokeExact(recv, 2L, 1L); |
| 225 | } |
| 226 | assertEquals(success, true, "weakCompareAndSetAcquire long"); |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 227 | long x = (long) hs.get(TestAccessMode.GET).invokeExact(recv); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 228 | assertEquals(x, 1L, "weakCompareAndSetAcquire long"); |
| 229 | } |
| 230 | |
| 231 | { |
Aleksey Shipilev | 4f53885 | 2016-05-04 17:17:28 +0300 | [diff] [blame] | 232 | boolean success = false; |
| 233 | for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { |
| 234 | success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_RELEASE).invokeExact(recv, 1L, 2L); |
| 235 | } |
| 236 | assertEquals(success, true, "weakCompareAndSetRelease long"); |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 237 | long x = (long) hs.get(TestAccessMode.GET).invokeExact(recv); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 238 | assertEquals(x, 2L, "weakCompareAndSetRelease long"); |
| 239 | } |
| 240 | |
Aleksey Shipilev | 33bb922 | 2016-05-17 22:28:00 +0300 | [diff] [blame] | 241 | { |
| 242 | boolean success = false; |
| 243 | for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { |
| 244 | success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_VOLATILE).invokeExact(recv, 2L, 1L); |
| 245 | } |
| 246 | assertEquals(success, true, "weakCompareAndSetVolatile long"); |
| 247 | long x = (long) hs.get(TestAccessMode.GET).invokeExact(recv); |
| 248 | assertEquals(x, 1L, "weakCompareAndSetVolatile long"); |
| 249 | } |
| 250 | |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 251 | // Compare set and get |
| 252 | { |
Aleksey Shipilev | 33bb922 | 2016-05-17 22:28:00 +0300 | [diff] [blame] | 253 | long o = (long) hs.get(TestAccessMode.GET_AND_SET).invokeExact(recv, 2L); |
| 254 | assertEquals(o, 1L, "getAndSet long"); |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 255 | long x = (long) hs.get(TestAccessMode.GET).invokeExact(recv); |
Aleksey Shipilev | 33bb922 | 2016-05-17 22:28:00 +0300 | [diff] [blame] | 256 | assertEquals(x, 2L, "getAndSet long value"); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 257 | } |
| 258 | |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 259 | hs.get(TestAccessMode.SET).invokeExact(recv, 1L); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 260 | |
| 261 | // get and add, add and get |
| 262 | { |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 263 | long o = (long) hs.get(TestAccessMode.GET_AND_ADD).invokeExact(recv, 3L); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 264 | assertEquals(o, 1L, "getAndAdd long"); |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 265 | long c = (long) hs.get(TestAccessMode.ADD_AND_GET).invokeExact(recv, 3L); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 266 | assertEquals(c, 1L + 3L + 3L, "getAndAdd long value"); |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | static void testInstanceFieldUnsupported(VarHandleTestMethodHandleAccessLong recv, Handles hs) throws Throwable { |
| 271 | |
| 272 | } |
| 273 | |
| 274 | |
| 275 | static void testStaticField(Handles hs) throws Throwable { |
| 276 | // Plain |
| 277 | { |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 278 | hs.get(TestAccessMode.SET).invokeExact(1L); |
| 279 | long x = (long) hs.get(TestAccessMode.GET).invokeExact(); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 280 | assertEquals(x, 1L, "set long value"); |
| 281 | } |
| 282 | |
| 283 | |
| 284 | // Volatile |
| 285 | { |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 286 | hs.get(TestAccessMode.SET_VOLATILE).invokeExact(2L); |
| 287 | long x = (long) hs.get(TestAccessMode.GET_VOLATILE).invokeExact(); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 288 | assertEquals(x, 2L, "setVolatile long value"); |
| 289 | } |
| 290 | |
| 291 | // Lazy |
| 292 | { |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 293 | hs.get(TestAccessMode.SET_RELEASE).invokeExact(1L); |
| 294 | long x = (long) hs.get(TestAccessMode.GET_ACQUIRE).invokeExact(); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 295 | assertEquals(x, 1L, "setRelease long value"); |
| 296 | } |
| 297 | |
| 298 | // Opaque |
| 299 | { |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 300 | hs.get(TestAccessMode.SET_OPAQUE).invokeExact(2L); |
| 301 | long x = (long) hs.get(TestAccessMode.GET_OPAQUE).invokeExact(); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 302 | assertEquals(x, 2L, "setOpaque long value"); |
| 303 | } |
| 304 | |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 305 | hs.get(TestAccessMode.SET).invokeExact(1L); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 306 | |
| 307 | // Compare |
| 308 | { |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 309 | boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(1L, 2L); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 310 | assertEquals(r, true, "success compareAndSet long"); |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 311 | long x = (long) hs.get(TestAccessMode.GET).invokeExact(); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 312 | assertEquals(x, 2L, "success compareAndSet long value"); |
| 313 | } |
| 314 | |
| 315 | { |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 316 | boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(1L, 3L); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 317 | assertEquals(r, false, "failing compareAndSet long"); |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 318 | long x = (long) hs.get(TestAccessMode.GET).invokeExact(); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 319 | assertEquals(x, 2L, "failing compareAndSet long value"); |
| 320 | } |
| 321 | |
| 322 | { |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 323 | long r = (long) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_VOLATILE).invokeExact(2L, 1L); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 324 | assertEquals(r, 2L, "success compareAndExchangeVolatile long"); |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 325 | long x = (long) hs.get(TestAccessMode.GET).invokeExact(); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 326 | assertEquals(x, 1L, "success compareAndExchangeVolatile long value"); |
| 327 | } |
| 328 | |
| 329 | { |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 330 | long r = (long) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_VOLATILE).invokeExact(2L, 3L); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 331 | assertEquals(r, 1L, "failing compareAndExchangeVolatile long"); |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 332 | long x = (long) hs.get(TestAccessMode.GET).invokeExact(); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 333 | assertEquals(x, 1L, "failing compareAndExchangeVolatile long value"); |
| 334 | } |
| 335 | |
| 336 | { |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 337 | long r = (long) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(1L, 2L); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 338 | assertEquals(r, 1L, "success compareAndExchangeAcquire long"); |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 339 | long x = (long) hs.get(TestAccessMode.GET).invokeExact(); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 340 | assertEquals(x, 2L, "success compareAndExchangeAcquire long value"); |
| 341 | } |
| 342 | |
| 343 | { |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 344 | long r = (long) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(1L, 3L); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 345 | assertEquals(r, 2L, "failing compareAndExchangeAcquire long"); |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 346 | long x = (long) hs.get(TestAccessMode.GET).invokeExact(); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 347 | assertEquals(x, 2L, "failing compareAndExchangeAcquire long value"); |
| 348 | } |
| 349 | |
| 350 | { |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 351 | long r = (long) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(2L, 1L); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 352 | assertEquals(r, 2L, "success compareAndExchangeRelease long"); |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 353 | long x = (long) hs.get(TestAccessMode.GET).invokeExact(); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 354 | assertEquals(x, 1L, "success compareAndExchangeRelease long value"); |
| 355 | } |
| 356 | |
| 357 | { |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 358 | long r = (long) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(2L, 3L); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 359 | assertEquals(r, 1L, "failing compareAndExchangeRelease long"); |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 360 | long x = (long) hs.get(TestAccessMode.GET).invokeExact(); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 361 | assertEquals(x, 1L, "failing compareAndExchangeRelease long value"); |
| 362 | } |
| 363 | |
| 364 | { |
Aleksey Shipilev | 4f53885 | 2016-05-04 17:17:28 +0300 | [diff] [blame] | 365 | boolean success = false; |
| 366 | for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { |
| 367 | success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET).invokeExact(1L, 2L); |
| 368 | } |
| 369 | assertEquals(success, true, "weakCompareAndSet long"); |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 370 | long x = (long) hs.get(TestAccessMode.GET).invokeExact(); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 371 | assertEquals(x, 2L, "weakCompareAndSet long value"); |
| 372 | } |
| 373 | |
| 374 | { |
Aleksey Shipilev | 4f53885 | 2016-05-04 17:17:28 +0300 | [diff] [blame] | 375 | boolean success = false; |
| 376 | for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { |
| 377 | success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_ACQUIRE).invokeExact(2L, 1L); |
| 378 | } |
| 379 | assertEquals(success, true, "weakCompareAndSetAcquire long"); |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 380 | long x = (long) hs.get(TestAccessMode.GET).invokeExact(); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 381 | assertEquals(x, 1L, "weakCompareAndSetAcquire long"); |
| 382 | } |
| 383 | |
| 384 | { |
Aleksey Shipilev | 4f53885 | 2016-05-04 17:17:28 +0300 | [diff] [blame] | 385 | boolean success = false; |
| 386 | for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { |
| 387 | success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_RELEASE).invokeExact(1L, 2L); |
| 388 | } |
| 389 | assertEquals(success, true, "weakCompareAndSetRelease long"); |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 390 | long x = (long) hs.get(TestAccessMode.GET).invokeExact(); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 391 | assertEquals(x, 2L, "weakCompareAndSetRelease long"); |
| 392 | } |
| 393 | |
Aleksey Shipilev | 33bb922 | 2016-05-17 22:28:00 +0300 | [diff] [blame] | 394 | { |
| 395 | boolean success = false; |
| 396 | for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { |
| 397 | success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_VOLATILE).invokeExact(2L, 1L); |
| 398 | } |
| 399 | assertEquals(success, true, "weakCompareAndSetVolatile long"); |
| 400 | long x = (long) hs.get(TestAccessMode.GET).invokeExact(); |
| 401 | assertEquals(x, 1L, "weakCompareAndSetVolatile long"); |
| 402 | } |
| 403 | |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 404 | // Compare set and get |
| 405 | { |
Aleksey Shipilev | 33bb922 | 2016-05-17 22:28:00 +0300 | [diff] [blame] | 406 | long o = (long) hs.get(TestAccessMode.GET_AND_SET).invokeExact( 2L); |
| 407 | assertEquals(o, 1L, "getAndSet long"); |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 408 | long x = (long) hs.get(TestAccessMode.GET).invokeExact(); |
Aleksey Shipilev | 33bb922 | 2016-05-17 22:28:00 +0300 | [diff] [blame] | 409 | assertEquals(x, 2L, "getAndSet long value"); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 410 | } |
| 411 | |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 412 | hs.get(TestAccessMode.SET).invokeExact(1L); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 413 | |
| 414 | // get and add, add and get |
| 415 | { |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 416 | long o = (long) hs.get(TestAccessMode.GET_AND_ADD).invokeExact( 3L); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 417 | assertEquals(o, 1L, "getAndAdd long"); |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 418 | long c = (long) hs.get(TestAccessMode.ADD_AND_GET).invokeExact(3L); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 419 | assertEquals(c, 1L + 3L + 3L, "getAndAdd long value"); |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | static void testStaticFieldUnsupported(Handles hs) throws Throwable { |
| 424 | |
| 425 | } |
| 426 | |
| 427 | |
| 428 | static void testArray(Handles hs) throws Throwable { |
| 429 | long[] array = new long[10]; |
| 430 | |
| 431 | for (int i = 0; i < array.length; i++) { |
| 432 | // Plain |
| 433 | { |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 434 | hs.get(TestAccessMode.SET).invokeExact(array, i, 1L); |
| 435 | long x = (long) hs.get(TestAccessMode.GET).invokeExact(array, i); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 436 | assertEquals(x, 1L, "get long value"); |
| 437 | } |
| 438 | |
| 439 | |
| 440 | // Volatile |
| 441 | { |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 442 | hs.get(TestAccessMode.SET_VOLATILE).invokeExact(array, i, 2L); |
| 443 | long x = (long) hs.get(TestAccessMode.GET_VOLATILE).invokeExact(array, i); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 444 | assertEquals(x, 2L, "setVolatile long value"); |
| 445 | } |
| 446 | |
| 447 | // Lazy |
| 448 | { |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 449 | hs.get(TestAccessMode.SET_RELEASE).invokeExact(array, i, 1L); |
| 450 | long x = (long) hs.get(TestAccessMode.GET_ACQUIRE).invokeExact(array, i); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 451 | assertEquals(x, 1L, "setRelease long value"); |
| 452 | } |
| 453 | |
| 454 | // Opaque |
| 455 | { |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 456 | hs.get(TestAccessMode.SET_OPAQUE).invokeExact(array, i, 2L); |
| 457 | long x = (long) hs.get(TestAccessMode.GET_OPAQUE).invokeExact(array, i); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 458 | assertEquals(x, 2L, "setOpaque long value"); |
| 459 | } |
| 460 | |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 461 | hs.get(TestAccessMode.SET).invokeExact(array, i, 1L); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 462 | |
| 463 | // Compare |
| 464 | { |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 465 | boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(array, i, 1L, 2L); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 466 | assertEquals(r, true, "success compareAndSet long"); |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 467 | long x = (long) hs.get(TestAccessMode.GET).invokeExact(array, i); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 468 | assertEquals(x, 2L, "success compareAndSet long value"); |
| 469 | } |
| 470 | |
| 471 | { |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 472 | boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(array, i, 1L, 3L); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 473 | assertEquals(r, false, "failing compareAndSet long"); |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 474 | long x = (long) hs.get(TestAccessMode.GET).invokeExact(array, i); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 475 | assertEquals(x, 2L, "failing compareAndSet long value"); |
| 476 | } |
| 477 | |
| 478 | { |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 479 | long r = (long) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_VOLATILE).invokeExact(array, i, 2L, 1L); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 480 | assertEquals(r, 2L, "success compareAndExchangeVolatile long"); |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 481 | long x = (long) hs.get(TestAccessMode.GET).invokeExact(array, i); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 482 | assertEquals(x, 1L, "success compareAndExchangeVolatile long value"); |
| 483 | } |
| 484 | |
| 485 | { |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 486 | long r = (long) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_VOLATILE).invokeExact(array, i, 2L, 3L); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 487 | assertEquals(r, 1L, "failing compareAndExchangeVolatile long"); |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 488 | long x = (long) hs.get(TestAccessMode.GET).invokeExact(array, i); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 489 | assertEquals(x, 1L, "failing compareAndExchangeVolatile long value"); |
| 490 | } |
| 491 | |
| 492 | { |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 493 | long r = (long) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(array, i, 1L, 2L); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 494 | assertEquals(r, 1L, "success compareAndExchangeAcquire long"); |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 495 | long x = (long) hs.get(TestAccessMode.GET).invokeExact(array, i); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 496 | assertEquals(x, 2L, "success compareAndExchangeAcquire long value"); |
| 497 | } |
| 498 | |
| 499 | { |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 500 | long r = (long) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(array, i, 1L, 3L); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 501 | assertEquals(r, 2L, "failing compareAndExchangeAcquire long"); |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 502 | long x = (long) hs.get(TestAccessMode.GET).invokeExact(array, i); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 503 | assertEquals(x, 2L, "failing compareAndExchangeAcquire long value"); |
| 504 | } |
| 505 | |
| 506 | { |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 507 | long r = (long) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(array, i, 2L, 1L); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 508 | assertEquals(r, 2L, "success compareAndExchangeRelease long"); |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 509 | long x = (long) hs.get(TestAccessMode.GET).invokeExact(array, i); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 510 | assertEquals(x, 1L, "success compareAndExchangeRelease long value"); |
| 511 | } |
| 512 | |
| 513 | { |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 514 | long r = (long) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(array, i, 2L, 3L); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 515 | assertEquals(r, 1L, "failing compareAndExchangeRelease long"); |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 516 | long x = (long) hs.get(TestAccessMode.GET).invokeExact(array, i); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 517 | assertEquals(x, 1L, "failing compareAndExchangeRelease long value"); |
| 518 | } |
| 519 | |
| 520 | { |
Aleksey Shipilev | 4f53885 | 2016-05-04 17:17:28 +0300 | [diff] [blame] | 521 | boolean success = false; |
| 522 | for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { |
| 523 | success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET).invokeExact(array, i, 1L, 2L); |
| 524 | } |
| 525 | assertEquals(success, true, "weakCompareAndSet long"); |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 526 | long x = (long) hs.get(TestAccessMode.GET).invokeExact(array, i); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 527 | assertEquals(x, 2L, "weakCompareAndSet long value"); |
| 528 | } |
| 529 | |
| 530 | { |
Aleksey Shipilev | 4f53885 | 2016-05-04 17:17:28 +0300 | [diff] [blame] | 531 | boolean success = false; |
| 532 | for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { |
| 533 | success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_ACQUIRE).invokeExact(array, i, 2L, 1L); |
| 534 | } |
| 535 | assertEquals(success, true, "weakCompareAndSetAcquire long"); |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 536 | long x = (long) hs.get(TestAccessMode.GET).invokeExact(array, i); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 537 | assertEquals(x, 1L, "weakCompareAndSetAcquire long"); |
| 538 | } |
| 539 | |
| 540 | { |
Aleksey Shipilev | 4f53885 | 2016-05-04 17:17:28 +0300 | [diff] [blame] | 541 | boolean success = false; |
| 542 | for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { |
| 543 | success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_RELEASE).invokeExact(array, i, 1L, 2L); |
| 544 | } |
| 545 | assertEquals(success, true, "weakCompareAndSetRelease long"); |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 546 | long x = (long) hs.get(TestAccessMode.GET).invokeExact(array, i); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 547 | assertEquals(x, 2L, "weakCompareAndSetRelease long"); |
| 548 | } |
| 549 | |
Aleksey Shipilev | 33bb922 | 2016-05-17 22:28:00 +0300 | [diff] [blame] | 550 | { |
| 551 | boolean success = false; |
| 552 | for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { |
| 553 | success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_VOLATILE).invokeExact(array, i, 2L, 1L); |
| 554 | } |
| 555 | assertEquals(success, true, "weakCompareAndSetVolatile long"); |
| 556 | long x = (long) hs.get(TestAccessMode.GET).invokeExact(array, i); |
| 557 | assertEquals(x, 1L, "weakCompareAndSetVolatile long"); |
| 558 | } |
| 559 | |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 560 | // Compare set and get |
| 561 | { |
Aleksey Shipilev | 33bb922 | 2016-05-17 22:28:00 +0300 | [diff] [blame] | 562 | long o = (long) hs.get(TestAccessMode.GET_AND_SET).invokeExact(array, i, 2L); |
| 563 | assertEquals(o, 1L, "getAndSet long"); |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 564 | long x = (long) hs.get(TestAccessMode.GET).invokeExact(array, i); |
Aleksey Shipilev | 33bb922 | 2016-05-17 22:28:00 +0300 | [diff] [blame] | 565 | assertEquals(x, 2L, "getAndSet long value"); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 566 | } |
| 567 | |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 568 | hs.get(TestAccessMode.SET).invokeExact(array, i, 1L); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 569 | |
| 570 | // get and add, add and get |
| 571 | { |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 572 | long o = (long) hs.get(TestAccessMode.GET_AND_ADD).invokeExact(array, i, 3L); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 573 | assertEquals(o, 1L, "getAndAdd long"); |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 574 | long c = (long) hs.get(TestAccessMode.ADD_AND_GET).invokeExact(array, i, 3L); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 575 | assertEquals(c, 1L + 3L + 3L, "getAndAdd long value"); |
| 576 | } |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | static void testArrayUnsupported(Handles hs) throws Throwable { |
| 581 | long[] array = new long[10]; |
| 582 | |
| 583 | final int i = 0; |
| 584 | |
| 585 | } |
| 586 | |
| 587 | static void testArrayIndexOutOfBounds(Handles hs) throws Throwable { |
| 588 | long[] array = new long[10]; |
| 589 | |
| 590 | for (int i : new int[]{-1, Integer.MIN_VALUE, 10, 11, Integer.MAX_VALUE}) { |
| 591 | final int ci = i; |
| 592 | |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 593 | for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET)) { |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 594 | checkIOOBE(am, () -> { |
| 595 | long x = (long) hs.get(am).invokeExact(array, ci); |
| 596 | }); |
| 597 | } |
| 598 | |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 599 | for (TestAccessMode am : testAccessModesOfType(TestAccessType.SET)) { |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 600 | checkIOOBE(am, () -> { |
| 601 | hs.get(am).invokeExact(array, ci, 1L); |
| 602 | }); |
| 603 | } |
| 604 | |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 605 | for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_SET)) { |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 606 | checkIOOBE(am, () -> { |
| 607 | boolean r = (boolean) hs.get(am).invokeExact(array, ci, 1L, 2L); |
| 608 | }); |
| 609 | } |
| 610 | |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 611 | for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_EXCHANGE)) { |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 612 | checkIOOBE(am, () -> { |
| 613 | long r = (long) hs.get(am).invokeExact(array, ci, 2L, 1L); |
| 614 | }); |
| 615 | } |
| 616 | |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 617 | for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_SET)) { |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 618 | checkIOOBE(am, () -> { |
| 619 | long o = (long) hs.get(am).invokeExact(array, ci, 1L); |
| 620 | }); |
| 621 | } |
| 622 | |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 623 | for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_ADD)) { |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 624 | checkIOOBE(am, () -> { |
| 625 | long o = (long) hs.get(am).invokeExact(array, ci, 3L); |
| 626 | }); |
| 627 | } |
| 628 | } |
| 629 | } |
| 630 | } |
| 631 | |