blob: 0f265da3f2e84842625f22bc8c1d677f436a76ef [file] [log] [blame]
Paul Sandoz9fb30a32016-03-24 11:21:21 +01001/*
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 Sandoz44afe202016-05-17 12:06:41 +020027 * @run testng/othervm -Diters=20000 -Djava.lang.invoke.VarHandle.VAR_HANDLE_GUARDS=false VarHandleTestMethodHandleAccessLong
Paul Sandoz9fb30a32016-03-24 11:21:21 +010028 */
29
30import org.testng.annotations.BeforeClass;
31import org.testng.annotations.DataProvider;
32import org.testng.annotations.Test;
33
34import java.lang.invoke.MethodHandles;
35import java.lang.invoke.VarHandle;
36import java.util.ArrayList;
37import java.util.Arrays;
38import java.util.List;
39
40import static org.testng.Assert.*;
41
42public 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 Sandoza7aff442016-04-13 15:05:48 +0200125 hs.get(TestAccessMode.SET).invokeExact(recv, 1L);
126 long x = (long) hs.get(TestAccessMode.GET).invokeExact(recv);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100127 assertEquals(x, 1L, "set long value");
128 }
129
130
131 // Volatile
132 {
Paul Sandoza7aff442016-04-13 15:05:48 +0200133 hs.get(TestAccessMode.SET_VOLATILE).invokeExact(recv, 2L);
134 long x = (long) hs.get(TestAccessMode.GET_VOLATILE).invokeExact(recv);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100135 assertEquals(x, 2L, "setVolatile long value");
136 }
137
138 // Lazy
139 {
Paul Sandoza7aff442016-04-13 15:05:48 +0200140 hs.get(TestAccessMode.SET_RELEASE).invokeExact(recv, 1L);
141 long x = (long) hs.get(TestAccessMode.GET_ACQUIRE).invokeExact(recv);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100142 assertEquals(x, 1L, "setRelease long value");
143 }
144
145 // Opaque
146 {
Paul Sandoza7aff442016-04-13 15:05:48 +0200147 hs.get(TestAccessMode.SET_OPAQUE).invokeExact(recv, 2L);
148 long x = (long) hs.get(TestAccessMode.GET_OPAQUE).invokeExact(recv);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100149 assertEquals(x, 2L, "setOpaque long value");
150 }
151
Paul Sandoza7aff442016-04-13 15:05:48 +0200152 hs.get(TestAccessMode.SET).invokeExact(recv, 1L);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100153
154 // Compare
155 {
Paul Sandoza7aff442016-04-13 15:05:48 +0200156 boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(recv, 1L, 2L);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100157 assertEquals(r, true, "success compareAndSet long");
Paul Sandoza7aff442016-04-13 15:05:48 +0200158 long x = (long) hs.get(TestAccessMode.GET).invokeExact(recv);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100159 assertEquals(x, 2L, "success compareAndSet long value");
160 }
161
162 {
Paul Sandoza7aff442016-04-13 15:05:48 +0200163 boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(recv, 1L, 3L);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100164 assertEquals(r, false, "failing compareAndSet long");
Paul Sandoza7aff442016-04-13 15:05:48 +0200165 long x = (long) hs.get(TestAccessMode.GET).invokeExact(recv);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100166 assertEquals(x, 2L, "failing compareAndSet long value");
167 }
168
169 {
Paul Sandoza7aff442016-04-13 15:05:48 +0200170 long r = (long) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_VOLATILE).invokeExact(recv, 2L, 1L);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100171 assertEquals(r, 2L, "success compareAndExchangeVolatile long");
Paul Sandoza7aff442016-04-13 15:05:48 +0200172 long x = (long) hs.get(TestAccessMode.GET).invokeExact(recv);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100173 assertEquals(x, 1L, "success compareAndExchangeVolatile long value");
174 }
175
176 {
Paul Sandoza7aff442016-04-13 15:05:48 +0200177 long r = (long) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_VOLATILE).invokeExact(recv, 2L, 3L);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100178 assertEquals(r, 1L, "failing compareAndExchangeVolatile long");
Paul Sandoza7aff442016-04-13 15:05:48 +0200179 long x = (long) hs.get(TestAccessMode.GET).invokeExact(recv);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100180 assertEquals(x, 1L, "failing compareAndExchangeVolatile long value");
181 }
182
183 {
Paul Sandoza7aff442016-04-13 15:05:48 +0200184 long r = (long) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(recv, 1L, 2L);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100185 assertEquals(r, 1L, "success compareAndExchangeAcquire long");
Paul Sandoza7aff442016-04-13 15:05:48 +0200186 long x = (long) hs.get(TestAccessMode.GET).invokeExact(recv);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100187 assertEquals(x, 2L, "success compareAndExchangeAcquire long value");
188 }
189
190 {
Paul Sandoza7aff442016-04-13 15:05:48 +0200191 long r = (long) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(recv, 1L, 3L);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100192 assertEquals(r, 2L, "failing compareAndExchangeAcquire long");
Paul Sandoza7aff442016-04-13 15:05:48 +0200193 long x = (long) hs.get(TestAccessMode.GET).invokeExact(recv);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100194 assertEquals(x, 2L, "failing compareAndExchangeAcquire long value");
195 }
196
197 {
Paul Sandoza7aff442016-04-13 15:05:48 +0200198 long r = (long) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(recv, 2L, 1L);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100199 assertEquals(r, 2L, "success compareAndExchangeRelease long");
Paul Sandoza7aff442016-04-13 15:05:48 +0200200 long x = (long) hs.get(TestAccessMode.GET).invokeExact(recv);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100201 assertEquals(x, 1L, "success compareAndExchangeRelease long value");
202 }
203
204 {
Paul Sandoza7aff442016-04-13 15:05:48 +0200205 long r = (long) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(recv, 2L, 3L);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100206 assertEquals(r, 1L, "failing compareAndExchangeRelease long");
Paul Sandoza7aff442016-04-13 15:05:48 +0200207 long x = (long) hs.get(TestAccessMode.GET).invokeExact(recv);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100208 assertEquals(x, 1L, "failing compareAndExchangeRelease long value");
209 }
210
211 {
Aleksey Shipilev4f538852016-05-04 17:17:28 +0300212 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 Sandoza7aff442016-04-13 15:05:48 +0200217 long x = (long) hs.get(TestAccessMode.GET).invokeExact(recv);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100218 assertEquals(x, 2L, "weakCompareAndSet long value");
219 }
220
221 {
Aleksey Shipilev4f538852016-05-04 17:17:28 +0300222 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 Sandoza7aff442016-04-13 15:05:48 +0200227 long x = (long) hs.get(TestAccessMode.GET).invokeExact(recv);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100228 assertEquals(x, 1L, "weakCompareAndSetAcquire long");
229 }
230
231 {
Aleksey Shipilev4f538852016-05-04 17:17:28 +0300232 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 Sandoza7aff442016-04-13 15:05:48 +0200237 long x = (long) hs.get(TestAccessMode.GET).invokeExact(recv);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100238 assertEquals(x, 2L, "weakCompareAndSetRelease long");
239 }
240
Aleksey Shipilev33bb9222016-05-17 22:28:00 +0300241 {
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 Sandoz9fb30a32016-03-24 11:21:21 +0100251 // Compare set and get
252 {
Aleksey Shipilev33bb9222016-05-17 22:28:00 +0300253 long o = (long) hs.get(TestAccessMode.GET_AND_SET).invokeExact(recv, 2L);
254 assertEquals(o, 1L, "getAndSet long");
Paul Sandoza7aff442016-04-13 15:05:48 +0200255 long x = (long) hs.get(TestAccessMode.GET).invokeExact(recv);
Aleksey Shipilev33bb9222016-05-17 22:28:00 +0300256 assertEquals(x, 2L, "getAndSet long value");
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100257 }
258
Paul Sandoza7aff442016-04-13 15:05:48 +0200259 hs.get(TestAccessMode.SET).invokeExact(recv, 1L);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100260
261 // get and add, add and get
262 {
Paul Sandoza7aff442016-04-13 15:05:48 +0200263 long o = (long) hs.get(TestAccessMode.GET_AND_ADD).invokeExact(recv, 3L);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100264 assertEquals(o, 1L, "getAndAdd long");
Paul Sandoza7aff442016-04-13 15:05:48 +0200265 long c = (long) hs.get(TestAccessMode.ADD_AND_GET).invokeExact(recv, 3L);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100266 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 Sandoza7aff442016-04-13 15:05:48 +0200278 hs.get(TestAccessMode.SET).invokeExact(1L);
279 long x = (long) hs.get(TestAccessMode.GET).invokeExact();
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100280 assertEquals(x, 1L, "set long value");
281 }
282
283
284 // Volatile
285 {
Paul Sandoza7aff442016-04-13 15:05:48 +0200286 hs.get(TestAccessMode.SET_VOLATILE).invokeExact(2L);
287 long x = (long) hs.get(TestAccessMode.GET_VOLATILE).invokeExact();
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100288 assertEquals(x, 2L, "setVolatile long value");
289 }
290
291 // Lazy
292 {
Paul Sandoza7aff442016-04-13 15:05:48 +0200293 hs.get(TestAccessMode.SET_RELEASE).invokeExact(1L);
294 long x = (long) hs.get(TestAccessMode.GET_ACQUIRE).invokeExact();
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100295 assertEquals(x, 1L, "setRelease long value");
296 }
297
298 // Opaque
299 {
Paul Sandoza7aff442016-04-13 15:05:48 +0200300 hs.get(TestAccessMode.SET_OPAQUE).invokeExact(2L);
301 long x = (long) hs.get(TestAccessMode.GET_OPAQUE).invokeExact();
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100302 assertEquals(x, 2L, "setOpaque long value");
303 }
304
Paul Sandoza7aff442016-04-13 15:05:48 +0200305 hs.get(TestAccessMode.SET).invokeExact(1L);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100306
307 // Compare
308 {
Paul Sandoza7aff442016-04-13 15:05:48 +0200309 boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(1L, 2L);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100310 assertEquals(r, true, "success compareAndSet long");
Paul Sandoza7aff442016-04-13 15:05:48 +0200311 long x = (long) hs.get(TestAccessMode.GET).invokeExact();
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100312 assertEquals(x, 2L, "success compareAndSet long value");
313 }
314
315 {
Paul Sandoza7aff442016-04-13 15:05:48 +0200316 boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(1L, 3L);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100317 assertEquals(r, false, "failing compareAndSet long");
Paul Sandoza7aff442016-04-13 15:05:48 +0200318 long x = (long) hs.get(TestAccessMode.GET).invokeExact();
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100319 assertEquals(x, 2L, "failing compareAndSet long value");
320 }
321
322 {
Paul Sandoza7aff442016-04-13 15:05:48 +0200323 long r = (long) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_VOLATILE).invokeExact(2L, 1L);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100324 assertEquals(r, 2L, "success compareAndExchangeVolatile long");
Paul Sandoza7aff442016-04-13 15:05:48 +0200325 long x = (long) hs.get(TestAccessMode.GET).invokeExact();
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100326 assertEquals(x, 1L, "success compareAndExchangeVolatile long value");
327 }
328
329 {
Paul Sandoza7aff442016-04-13 15:05:48 +0200330 long r = (long) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_VOLATILE).invokeExact(2L, 3L);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100331 assertEquals(r, 1L, "failing compareAndExchangeVolatile long");
Paul Sandoza7aff442016-04-13 15:05:48 +0200332 long x = (long) hs.get(TestAccessMode.GET).invokeExact();
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100333 assertEquals(x, 1L, "failing compareAndExchangeVolatile long value");
334 }
335
336 {
Paul Sandoza7aff442016-04-13 15:05:48 +0200337 long r = (long) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(1L, 2L);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100338 assertEquals(r, 1L, "success compareAndExchangeAcquire long");
Paul Sandoza7aff442016-04-13 15:05:48 +0200339 long x = (long) hs.get(TestAccessMode.GET).invokeExact();
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100340 assertEquals(x, 2L, "success compareAndExchangeAcquire long value");
341 }
342
343 {
Paul Sandoza7aff442016-04-13 15:05:48 +0200344 long r = (long) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(1L, 3L);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100345 assertEquals(r, 2L, "failing compareAndExchangeAcquire long");
Paul Sandoza7aff442016-04-13 15:05:48 +0200346 long x = (long) hs.get(TestAccessMode.GET).invokeExact();
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100347 assertEquals(x, 2L, "failing compareAndExchangeAcquire long value");
348 }
349
350 {
Paul Sandoza7aff442016-04-13 15:05:48 +0200351 long r = (long) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(2L, 1L);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100352 assertEquals(r, 2L, "success compareAndExchangeRelease long");
Paul Sandoza7aff442016-04-13 15:05:48 +0200353 long x = (long) hs.get(TestAccessMode.GET).invokeExact();
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100354 assertEquals(x, 1L, "success compareAndExchangeRelease long value");
355 }
356
357 {
Paul Sandoza7aff442016-04-13 15:05:48 +0200358 long r = (long) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(2L, 3L);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100359 assertEquals(r, 1L, "failing compareAndExchangeRelease long");
Paul Sandoza7aff442016-04-13 15:05:48 +0200360 long x = (long) hs.get(TestAccessMode.GET).invokeExact();
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100361 assertEquals(x, 1L, "failing compareAndExchangeRelease long value");
362 }
363
364 {
Aleksey Shipilev4f538852016-05-04 17:17:28 +0300365 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 Sandoza7aff442016-04-13 15:05:48 +0200370 long x = (long) hs.get(TestAccessMode.GET).invokeExact();
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100371 assertEquals(x, 2L, "weakCompareAndSet long value");
372 }
373
374 {
Aleksey Shipilev4f538852016-05-04 17:17:28 +0300375 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 Sandoza7aff442016-04-13 15:05:48 +0200380 long x = (long) hs.get(TestAccessMode.GET).invokeExact();
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100381 assertEquals(x, 1L, "weakCompareAndSetAcquire long");
382 }
383
384 {
Aleksey Shipilev4f538852016-05-04 17:17:28 +0300385 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 Sandoza7aff442016-04-13 15:05:48 +0200390 long x = (long) hs.get(TestAccessMode.GET).invokeExact();
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100391 assertEquals(x, 2L, "weakCompareAndSetRelease long");
392 }
393
Aleksey Shipilev33bb9222016-05-17 22:28:00 +0300394 {
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 Sandoz9fb30a32016-03-24 11:21:21 +0100404 // Compare set and get
405 {
Aleksey Shipilev33bb9222016-05-17 22:28:00 +0300406 long o = (long) hs.get(TestAccessMode.GET_AND_SET).invokeExact( 2L);
407 assertEquals(o, 1L, "getAndSet long");
Paul Sandoza7aff442016-04-13 15:05:48 +0200408 long x = (long) hs.get(TestAccessMode.GET).invokeExact();
Aleksey Shipilev33bb9222016-05-17 22:28:00 +0300409 assertEquals(x, 2L, "getAndSet long value");
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100410 }
411
Paul Sandoza7aff442016-04-13 15:05:48 +0200412 hs.get(TestAccessMode.SET).invokeExact(1L);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100413
414 // get and add, add and get
415 {
Paul Sandoza7aff442016-04-13 15:05:48 +0200416 long o = (long) hs.get(TestAccessMode.GET_AND_ADD).invokeExact( 3L);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100417 assertEquals(o, 1L, "getAndAdd long");
Paul Sandoza7aff442016-04-13 15:05:48 +0200418 long c = (long) hs.get(TestAccessMode.ADD_AND_GET).invokeExact(3L);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100419 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 Sandoza7aff442016-04-13 15:05:48 +0200434 hs.get(TestAccessMode.SET).invokeExact(array, i, 1L);
435 long x = (long) hs.get(TestAccessMode.GET).invokeExact(array, i);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100436 assertEquals(x, 1L, "get long value");
437 }
438
439
440 // Volatile
441 {
Paul Sandoza7aff442016-04-13 15:05:48 +0200442 hs.get(TestAccessMode.SET_VOLATILE).invokeExact(array, i, 2L);
443 long x = (long) hs.get(TestAccessMode.GET_VOLATILE).invokeExact(array, i);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100444 assertEquals(x, 2L, "setVolatile long value");
445 }
446
447 // Lazy
448 {
Paul Sandoza7aff442016-04-13 15:05:48 +0200449 hs.get(TestAccessMode.SET_RELEASE).invokeExact(array, i, 1L);
450 long x = (long) hs.get(TestAccessMode.GET_ACQUIRE).invokeExact(array, i);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100451 assertEquals(x, 1L, "setRelease long value");
452 }
453
454 // Opaque
455 {
Paul Sandoza7aff442016-04-13 15:05:48 +0200456 hs.get(TestAccessMode.SET_OPAQUE).invokeExact(array, i, 2L);
457 long x = (long) hs.get(TestAccessMode.GET_OPAQUE).invokeExact(array, i);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100458 assertEquals(x, 2L, "setOpaque long value");
459 }
460
Paul Sandoza7aff442016-04-13 15:05:48 +0200461 hs.get(TestAccessMode.SET).invokeExact(array, i, 1L);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100462
463 // Compare
464 {
Paul Sandoza7aff442016-04-13 15:05:48 +0200465 boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(array, i, 1L, 2L);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100466 assertEquals(r, true, "success compareAndSet long");
Paul Sandoza7aff442016-04-13 15:05:48 +0200467 long x = (long) hs.get(TestAccessMode.GET).invokeExact(array, i);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100468 assertEquals(x, 2L, "success compareAndSet long value");
469 }
470
471 {
Paul Sandoza7aff442016-04-13 15:05:48 +0200472 boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(array, i, 1L, 3L);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100473 assertEquals(r, false, "failing compareAndSet long");
Paul Sandoza7aff442016-04-13 15:05:48 +0200474 long x = (long) hs.get(TestAccessMode.GET).invokeExact(array, i);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100475 assertEquals(x, 2L, "failing compareAndSet long value");
476 }
477
478 {
Paul Sandoza7aff442016-04-13 15:05:48 +0200479 long r = (long) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_VOLATILE).invokeExact(array, i, 2L, 1L);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100480 assertEquals(r, 2L, "success compareAndExchangeVolatile long");
Paul Sandoza7aff442016-04-13 15:05:48 +0200481 long x = (long) hs.get(TestAccessMode.GET).invokeExact(array, i);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100482 assertEquals(x, 1L, "success compareAndExchangeVolatile long value");
483 }
484
485 {
Paul Sandoza7aff442016-04-13 15:05:48 +0200486 long r = (long) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_VOLATILE).invokeExact(array, i, 2L, 3L);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100487 assertEquals(r, 1L, "failing compareAndExchangeVolatile long");
Paul Sandoza7aff442016-04-13 15:05:48 +0200488 long x = (long) hs.get(TestAccessMode.GET).invokeExact(array, i);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100489 assertEquals(x, 1L, "failing compareAndExchangeVolatile long value");
490 }
491
492 {
Paul Sandoza7aff442016-04-13 15:05:48 +0200493 long r = (long) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(array, i, 1L, 2L);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100494 assertEquals(r, 1L, "success compareAndExchangeAcquire long");
Paul Sandoza7aff442016-04-13 15:05:48 +0200495 long x = (long) hs.get(TestAccessMode.GET).invokeExact(array, i);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100496 assertEquals(x, 2L, "success compareAndExchangeAcquire long value");
497 }
498
499 {
Paul Sandoza7aff442016-04-13 15:05:48 +0200500 long r = (long) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(array, i, 1L, 3L);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100501 assertEquals(r, 2L, "failing compareAndExchangeAcquire long");
Paul Sandoza7aff442016-04-13 15:05:48 +0200502 long x = (long) hs.get(TestAccessMode.GET).invokeExact(array, i);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100503 assertEquals(x, 2L, "failing compareAndExchangeAcquire long value");
504 }
505
506 {
Paul Sandoza7aff442016-04-13 15:05:48 +0200507 long r = (long) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(array, i, 2L, 1L);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100508 assertEquals(r, 2L, "success compareAndExchangeRelease long");
Paul Sandoza7aff442016-04-13 15:05:48 +0200509 long x = (long) hs.get(TestAccessMode.GET).invokeExact(array, i);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100510 assertEquals(x, 1L, "success compareAndExchangeRelease long value");
511 }
512
513 {
Paul Sandoza7aff442016-04-13 15:05:48 +0200514 long r = (long) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(array, i, 2L, 3L);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100515 assertEquals(r, 1L, "failing compareAndExchangeRelease long");
Paul Sandoza7aff442016-04-13 15:05:48 +0200516 long x = (long) hs.get(TestAccessMode.GET).invokeExact(array, i);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100517 assertEquals(x, 1L, "failing compareAndExchangeRelease long value");
518 }
519
520 {
Aleksey Shipilev4f538852016-05-04 17:17:28 +0300521 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 Sandoza7aff442016-04-13 15:05:48 +0200526 long x = (long) hs.get(TestAccessMode.GET).invokeExact(array, i);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100527 assertEquals(x, 2L, "weakCompareAndSet long value");
528 }
529
530 {
Aleksey Shipilev4f538852016-05-04 17:17:28 +0300531 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 Sandoza7aff442016-04-13 15:05:48 +0200536 long x = (long) hs.get(TestAccessMode.GET).invokeExact(array, i);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100537 assertEquals(x, 1L, "weakCompareAndSetAcquire long");
538 }
539
540 {
Aleksey Shipilev4f538852016-05-04 17:17:28 +0300541 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 Sandoza7aff442016-04-13 15:05:48 +0200546 long x = (long) hs.get(TestAccessMode.GET).invokeExact(array, i);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100547 assertEquals(x, 2L, "weakCompareAndSetRelease long");
548 }
549
Aleksey Shipilev33bb9222016-05-17 22:28:00 +0300550 {
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 Sandoz9fb30a32016-03-24 11:21:21 +0100560 // Compare set and get
561 {
Aleksey Shipilev33bb9222016-05-17 22:28:00 +0300562 long o = (long) hs.get(TestAccessMode.GET_AND_SET).invokeExact(array, i, 2L);
563 assertEquals(o, 1L, "getAndSet long");
Paul Sandoza7aff442016-04-13 15:05:48 +0200564 long x = (long) hs.get(TestAccessMode.GET).invokeExact(array, i);
Aleksey Shipilev33bb9222016-05-17 22:28:00 +0300565 assertEquals(x, 2L, "getAndSet long value");
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100566 }
567
Paul Sandoza7aff442016-04-13 15:05:48 +0200568 hs.get(TestAccessMode.SET).invokeExact(array, i, 1L);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100569
570 // get and add, add and get
571 {
Paul Sandoza7aff442016-04-13 15:05:48 +0200572 long o = (long) hs.get(TestAccessMode.GET_AND_ADD).invokeExact(array, i, 3L);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100573 assertEquals(o, 1L, "getAndAdd long");
Paul Sandoza7aff442016-04-13 15:05:48 +0200574 long c = (long) hs.get(TestAccessMode.ADD_AND_GET).invokeExact(array, i, 3L);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100575 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 Sandoza7aff442016-04-13 15:05:48 +0200593 for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET)) {
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100594 checkIOOBE(am, () -> {
595 long x = (long) hs.get(am).invokeExact(array, ci);
596 });
597 }
598
Paul Sandoza7aff442016-04-13 15:05:48 +0200599 for (TestAccessMode am : testAccessModesOfType(TestAccessType.SET)) {
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100600 checkIOOBE(am, () -> {
601 hs.get(am).invokeExact(array, ci, 1L);
602 });
603 }
604
Paul Sandoza7aff442016-04-13 15:05:48 +0200605 for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_SET)) {
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100606 checkIOOBE(am, () -> {
607 boolean r = (boolean) hs.get(am).invokeExact(array, ci, 1L, 2L);
608 });
609 }
610
Paul Sandoza7aff442016-04-13 15:05:48 +0200611 for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_EXCHANGE)) {
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100612 checkIOOBE(am, () -> {
613 long r = (long) hs.get(am).invokeExact(array, ci, 2L, 1L);
614 });
615 }
616
Paul Sandoza7aff442016-04-13 15:05:48 +0200617 for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_SET)) {
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100618 checkIOOBE(am, () -> {
619 long o = (long) hs.get(am).invokeExact(array, ci, 1L);
620 });
621 }
622
Paul Sandoza7aff442016-04-13 15:05:48 +0200623 for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_ADD)) {
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100624 checkIOOBE(am, () -> {
625 long o = (long) hs.get(am).invokeExact(array, ci, 3L);
626 });
627 }
628 }
629 }
630}
631