blob: b826e18d48324fa8b5ddd8831c5b5da2c8975688 [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 VarHandleTestMethodHandleAccessFloat
27 */
28
29import org.testng.annotations.BeforeClass;
30import org.testng.annotations.DataProvider;
31import org.testng.annotations.Test;
32
33import java.lang.invoke.MethodHandles;
34import java.lang.invoke.VarHandle;
35import java.util.ArrayList;
36import java.util.Arrays;
37import java.util.List;
38
39import static org.testng.Assert.*;
40
41public class VarHandleTestMethodHandleAccessFloat extends VarHandleBaseTest {
42 static final float static_final_v = 1.0f;
43
44 static float static_v;
45
46 final float final_v = 1.0f;
47
48 float v;
49
50 VarHandle vhFinalField;
51
52 VarHandle vhField;
53
54 VarHandle vhStaticField;
55
56 VarHandle vhStaticFinalField;
57
58 VarHandle vhArray;
59
60 @BeforeClass
61 public void setup() throws Exception {
62 vhFinalField = MethodHandles.lookup().findVarHandle(
63 VarHandleTestMethodHandleAccessFloat.class, "final_v", float.class);
64
65 vhField = MethodHandles.lookup().findVarHandle(
66 VarHandleTestMethodHandleAccessFloat.class, "v", float.class);
67
68 vhStaticFinalField = MethodHandles.lookup().findStaticVarHandle(
69 VarHandleTestMethodHandleAccessFloat.class, "static_final_v", float.class);
70
71 vhStaticField = MethodHandles.lookup().findStaticVarHandle(
72 VarHandleTestMethodHandleAccessFloat.class, "static_v", float.class);
73
74 vhArray = MethodHandles.arrayElementVarHandle(float[].class);
75 }
76
77
78 @DataProvider
79 public Object[][] accessTestCaseProvider() throws Exception {
80 List<AccessTestCase<?>> cases = new ArrayList<>();
81
82 for (VarHandleToMethodHandle f : VarHandleToMethodHandle.values()) {
83 cases.add(new MethodHandleAccessTestCase("Instance field",
84 vhField, f, hs -> testInstanceField(this, hs)));
85 cases.add(new MethodHandleAccessTestCase("Instance field unsupported",
86 vhField, f, hs -> testInstanceFieldUnsupported(this, hs),
87 false));
88
89 cases.add(new MethodHandleAccessTestCase("Static field",
90 vhStaticField, f, VarHandleTestMethodHandleAccessFloat::testStaticField));
91 cases.add(new MethodHandleAccessTestCase("Static field unsupported",
92 vhStaticField, f, VarHandleTestMethodHandleAccessFloat::testStaticFieldUnsupported,
93 false));
94
95 cases.add(new MethodHandleAccessTestCase("Array",
96 vhArray, f, VarHandleTestMethodHandleAccessFloat::testArray));
97 cases.add(new MethodHandleAccessTestCase("Array unsupported",
98 vhArray, f, VarHandleTestMethodHandleAccessFloat::testArrayUnsupported,
99 false));
100 cases.add(new MethodHandleAccessTestCase("Array index out of bounds",
101 vhArray, f, VarHandleTestMethodHandleAccessFloat::testArrayIndexOutOfBounds,
102 false));
103 }
104
105 // Work around issue with jtreg summary reporting which truncates
106 // the String result of Object.toString to 30 characters, hence
107 // the first dummy argument
108 return cases.stream().map(tc -> new Object[]{tc.toString(), tc}).toArray(Object[][]::new);
109 }
110
111 @Test(dataProvider = "accessTestCaseProvider")
112 public <T> void testAccess(String desc, AccessTestCase<T> atc) throws Throwable {
113 T t = atc.get();
114 int iters = atc.requiresLoop() ? ITERS : 1;
115 for (int c = 0; c < iters; c++) {
116 atc.testAccess(t);
117 }
118 }
119
120
121 static void testInstanceField(VarHandleTestMethodHandleAccessFloat recv, Handles hs) throws Throwable {
122 // Plain
123 {
Paul Sandoza7aff442016-04-13 15:05:48 +0200124 hs.get(TestAccessMode.SET).invokeExact(recv, 1.0f);
125 float x = (float) hs.get(TestAccessMode.GET).invokeExact(recv);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100126 assertEquals(x, 1.0f, "set float value");
127 }
128
129
130 // Volatile
131 {
Paul Sandoza7aff442016-04-13 15:05:48 +0200132 hs.get(TestAccessMode.SET_VOLATILE).invokeExact(recv, 2.0f);
133 float x = (float) hs.get(TestAccessMode.GET_VOLATILE).invokeExact(recv);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100134 assertEquals(x, 2.0f, "setVolatile float value");
135 }
136
137 // Lazy
138 {
Paul Sandoza7aff442016-04-13 15:05:48 +0200139 hs.get(TestAccessMode.SET_RELEASE).invokeExact(recv, 1.0f);
140 float x = (float) hs.get(TestAccessMode.GET_ACQUIRE).invokeExact(recv);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100141 assertEquals(x, 1.0f, "setRelease float value");
142 }
143
144 // Opaque
145 {
Paul Sandoza7aff442016-04-13 15:05:48 +0200146 hs.get(TestAccessMode.SET_OPAQUE).invokeExact(recv, 2.0f);
147 float x = (float) hs.get(TestAccessMode.GET_OPAQUE).invokeExact(recv);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100148 assertEquals(x, 2.0f, "setOpaque float value");
149 }
150
Paul Sandoz734dbe42016-06-20 17:57:19 +0200151 hs.get(TestAccessMode.SET).invokeExact(recv, 1.0f);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100152
Paul Sandoz734dbe42016-06-20 17:57:19 +0200153 // Compare
154 {
155 boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(recv, 1.0f, 2.0f);
156 assertEquals(r, true, "success compareAndSet float");
157 float x = (float) hs.get(TestAccessMode.GET).invokeExact(recv);
158 assertEquals(x, 2.0f, "success compareAndSet float value");
159 }
160
161 {
162 boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(recv, 1.0f, 3.0f);
163 assertEquals(r, false, "failing compareAndSet float");
164 float x = (float) hs.get(TestAccessMode.GET).invokeExact(recv);
165 assertEquals(x, 2.0f, "failing compareAndSet float value");
166 }
167
168 {
Paul Sandoz3f0273a2016-06-23 13:46:48 +0200169 float r = (float) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE).invokeExact(recv, 2.0f, 1.0f);
170 assertEquals(r, 2.0f, "success compareAndExchange float");
Paul Sandoz734dbe42016-06-20 17:57:19 +0200171 float x = (float) hs.get(TestAccessMode.GET).invokeExact(recv);
Paul Sandoz3f0273a2016-06-23 13:46:48 +0200172 assertEquals(x, 1.0f, "success compareAndExchange float value");
Paul Sandoz734dbe42016-06-20 17:57:19 +0200173 }
174
175 {
Paul Sandoz3f0273a2016-06-23 13:46:48 +0200176 float r = (float) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE).invokeExact(recv, 2.0f, 3.0f);
177 assertEquals(r, 1.0f, "failing compareAndExchange float");
Paul Sandoz734dbe42016-06-20 17:57:19 +0200178 float x = (float) hs.get(TestAccessMode.GET).invokeExact(recv);
Paul Sandoz3f0273a2016-06-23 13:46:48 +0200179 assertEquals(x, 1.0f, "failing compareAndExchange float value");
Paul Sandoz734dbe42016-06-20 17:57:19 +0200180 }
181
182 {
183 float r = (float) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(recv, 1.0f, 2.0f);
184 assertEquals(r, 1.0f, "success compareAndExchangeAcquire float");
185 float x = (float) hs.get(TestAccessMode.GET).invokeExact(recv);
186 assertEquals(x, 2.0f, "success compareAndExchangeAcquire float value");
187 }
188
189 {
190 float r = (float) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(recv, 1.0f, 3.0f);
191 assertEquals(r, 2.0f, "failing compareAndExchangeAcquire float");
192 float x = (float) hs.get(TestAccessMode.GET).invokeExact(recv);
193 assertEquals(x, 2.0f, "failing compareAndExchangeAcquire float value");
194 }
195
196 {
197 float r = (float) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(recv, 2.0f, 1.0f);
198 assertEquals(r, 2.0f, "success compareAndExchangeRelease float");
199 float x = (float) hs.get(TestAccessMode.GET).invokeExact(recv);
200 assertEquals(x, 1.0f, "success compareAndExchangeRelease float value");
201 }
202
203 {
204 float r = (float) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(recv, 2.0f, 3.0f);
205 assertEquals(r, 1.0f, "failing compareAndExchangeRelease float");
206 float x = (float) hs.get(TestAccessMode.GET).invokeExact(recv);
207 assertEquals(x, 1.0f, "failing compareAndExchangeRelease float value");
208 }
209
210 {
211 boolean success = false;
212 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
Paul Sandoz3bd5ebe2016-09-01 13:56:13 -0700213 success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_PLAIN).invokeExact(recv, 1.0f, 2.0f);
Paul Sandoz734dbe42016-06-20 17:57:19 +0200214 }
Paul Sandoz3bd5ebe2016-09-01 13:56:13 -0700215 assertEquals(success, true, "weakCompareAndSetPlain float");
Paul Sandoz734dbe42016-06-20 17:57:19 +0200216 float x = (float) hs.get(TestAccessMode.GET).invokeExact(recv);
Paul Sandoz3bd5ebe2016-09-01 13:56:13 -0700217 assertEquals(x, 2.0f, "weakCompareAndSetPlain float value");
Paul Sandoz734dbe42016-06-20 17:57:19 +0200218 }
219
220 {
221 boolean success = false;
222 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
223 success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_ACQUIRE).invokeExact(recv, 2.0f, 1.0f);
224 }
225 assertEquals(success, true, "weakCompareAndSetAcquire float");
226 float x = (float) hs.get(TestAccessMode.GET).invokeExact(recv);
227 assertEquals(x, 1.0f, "weakCompareAndSetAcquire float");
228 }
229
230 {
231 boolean success = false;
232 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
233 success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_RELEASE).invokeExact(recv, 1.0f, 2.0f);
234 }
235 assertEquals(success, true, "weakCompareAndSetRelease float");
236 float x = (float) hs.get(TestAccessMode.GET).invokeExact(recv);
237 assertEquals(x, 2.0f, "weakCompareAndSetRelease float");
238 }
239
240 {
241 boolean success = false;
242 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
Paul Sandoz3bd5ebe2016-09-01 13:56:13 -0700243 success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET).invokeExact(recv, 2.0f, 1.0f);
Paul Sandoz734dbe42016-06-20 17:57:19 +0200244 }
Paul Sandoz3bd5ebe2016-09-01 13:56:13 -0700245 assertEquals(success, true, "weakCompareAndSet float");
Paul Sandoz734dbe42016-06-20 17:57:19 +0200246 float x = (float) hs.get(TestAccessMode.GET).invokeExact(recv);
Paul Sandoz3bd5ebe2016-09-01 13:56:13 -0700247 assertEquals(x, 1.0f, "weakCompareAndSet float");
Paul Sandoz734dbe42016-06-20 17:57:19 +0200248 }
249
250 // Compare set and get
251 {
252 float o = (float) hs.get(TestAccessMode.GET_AND_SET).invokeExact(recv, 2.0f);
253 assertEquals(o, 1.0f, "getAndSet float");
254 float x = (float) hs.get(TestAccessMode.GET).invokeExact(recv);
255 assertEquals(x, 2.0f, "getAndSet float value");
256 }
257
Paul Sandoz734dbe42016-06-20 17:57:19 +0200258 // get and add, add and get
259 {
Paul Sandoz82d48912016-09-01 10:16:57 -0700260 hs.get(TestAccessMode.SET).invokeExact(recv, 1.0f);
261
Paul Sandozc073edc2016-09-01 10:17:01 -0700262 float o = (float) hs.get(TestAccessMode.GET_AND_ADD).invokeExact(recv, 2.0f);
Paul Sandoz734dbe42016-06-20 17:57:19 +0200263 assertEquals(o, 1.0f, "getAndAdd float");
Paul Sandozc073edc2016-09-01 10:17:01 -0700264 float x = (float) hs.get(TestAccessMode.GET).invokeExact(recv);
265 assertEquals(x, (float)(1.0f + 2.0f), "getAndAdd float value");
Paul Sandoz734dbe42016-06-20 17:57:19 +0200266 }
Paul Sandoz82d48912016-09-01 10:16:57 -0700267
268 {
269 hs.get(TestAccessMode.SET).invokeExact(recv, 1.0f);
270
271 float o = (float) hs.get(TestAccessMode.GET_AND_ADD_ACQUIRE).invokeExact(recv, 2.0f);
272 assertEquals(o, 1.0f, "getAndAddAcquire float");
273 float x = (float) hs.get(TestAccessMode.GET).invokeExact(recv);
274 assertEquals(x, (float)(1.0f + 2.0f), "getAndAddAcquire float value");
275 }
276
277 {
278 hs.get(TestAccessMode.SET).invokeExact(recv, 1.0f);
279
280 float o = (float) hs.get(TestAccessMode.GET_AND_ADD_RELEASE).invokeExact(recv, 2.0f);
281 assertEquals(o, 1.0f, "getAndAddRelease float");
282 float x = (float) hs.get(TestAccessMode.GET).invokeExact(recv);
283 assertEquals(x, (float)(1.0f + 2.0f), "getAndAddRelease float value");
284 }
285
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100286 }
287
288 static void testInstanceFieldUnsupported(VarHandleTestMethodHandleAccessFloat recv, Handles hs) throws Throwable {
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100289
Paul Sandoz82d48912016-09-01 10:16:57 -0700290
291 for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_BITWISE)) {
292 checkUOE(am, () -> {
293 float r = (float) hs.get(am).invokeExact(recv, 1.0f);
294 });
295 }
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100296 }
297
298
299 static void testStaticField(Handles hs) throws Throwable {
300 // Plain
301 {
Paul Sandoza7aff442016-04-13 15:05:48 +0200302 hs.get(TestAccessMode.SET).invokeExact(1.0f);
303 float x = (float) hs.get(TestAccessMode.GET).invokeExact();
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100304 assertEquals(x, 1.0f, "set float value");
305 }
306
307
308 // Volatile
309 {
Paul Sandoza7aff442016-04-13 15:05:48 +0200310 hs.get(TestAccessMode.SET_VOLATILE).invokeExact(2.0f);
311 float x = (float) hs.get(TestAccessMode.GET_VOLATILE).invokeExact();
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100312 assertEquals(x, 2.0f, "setVolatile float value");
313 }
314
315 // Lazy
316 {
Paul Sandoza7aff442016-04-13 15:05:48 +0200317 hs.get(TestAccessMode.SET_RELEASE).invokeExact(1.0f);
318 float x = (float) hs.get(TestAccessMode.GET_ACQUIRE).invokeExact();
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100319 assertEquals(x, 1.0f, "setRelease float value");
320 }
321
322 // Opaque
323 {
Paul Sandoza7aff442016-04-13 15:05:48 +0200324 hs.get(TestAccessMode.SET_OPAQUE).invokeExact(2.0f);
325 float x = (float) hs.get(TestAccessMode.GET_OPAQUE).invokeExact();
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100326 assertEquals(x, 2.0f, "setOpaque float value");
327 }
328
Paul Sandoz734dbe42016-06-20 17:57:19 +0200329 hs.get(TestAccessMode.SET).invokeExact(1.0f);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100330
Paul Sandoz734dbe42016-06-20 17:57:19 +0200331 // Compare
332 {
333 boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(1.0f, 2.0f);
334 assertEquals(r, true, "success compareAndSet float");
335 float x = (float) hs.get(TestAccessMode.GET).invokeExact();
336 assertEquals(x, 2.0f, "success compareAndSet float value");
337 }
338
339 {
340 boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(1.0f, 3.0f);
341 assertEquals(r, false, "failing compareAndSet float");
342 float x = (float) hs.get(TestAccessMode.GET).invokeExact();
343 assertEquals(x, 2.0f, "failing compareAndSet float value");
344 }
345
346 {
Paul Sandoz3f0273a2016-06-23 13:46:48 +0200347 float r = (float) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE).invokeExact(2.0f, 1.0f);
348 assertEquals(r, 2.0f, "success compareAndExchange float");
Paul Sandoz734dbe42016-06-20 17:57:19 +0200349 float x = (float) hs.get(TestAccessMode.GET).invokeExact();
Paul Sandoz3f0273a2016-06-23 13:46:48 +0200350 assertEquals(x, 1.0f, "success compareAndExchange float value");
Paul Sandoz734dbe42016-06-20 17:57:19 +0200351 }
352
353 {
Paul Sandoz3f0273a2016-06-23 13:46:48 +0200354 float r = (float) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE).invokeExact(2.0f, 3.0f);
355 assertEquals(r, 1.0f, "failing compareAndExchange float");
Paul Sandoz734dbe42016-06-20 17:57:19 +0200356 float x = (float) hs.get(TestAccessMode.GET).invokeExact();
Paul Sandoz3f0273a2016-06-23 13:46:48 +0200357 assertEquals(x, 1.0f, "failing compareAndExchange float value");
Paul Sandoz734dbe42016-06-20 17:57:19 +0200358 }
359
360 {
361 float r = (float) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(1.0f, 2.0f);
362 assertEquals(r, 1.0f, "success compareAndExchangeAcquire float");
363 float x = (float) hs.get(TestAccessMode.GET).invokeExact();
364 assertEquals(x, 2.0f, "success compareAndExchangeAcquire float value");
365 }
366
367 {
368 float r = (float) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(1.0f, 3.0f);
369 assertEquals(r, 2.0f, "failing compareAndExchangeAcquire float");
370 float x = (float) hs.get(TestAccessMode.GET).invokeExact();
371 assertEquals(x, 2.0f, "failing compareAndExchangeAcquire float value");
372 }
373
374 {
375 float r = (float) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(2.0f, 1.0f);
376 assertEquals(r, 2.0f, "success compareAndExchangeRelease float");
377 float x = (float) hs.get(TestAccessMode.GET).invokeExact();
378 assertEquals(x, 1.0f, "success compareAndExchangeRelease float value");
379 }
380
381 {
382 float r = (float) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(2.0f, 3.0f);
383 assertEquals(r, 1.0f, "failing compareAndExchangeRelease float");
384 float x = (float) hs.get(TestAccessMode.GET).invokeExact();
385 assertEquals(x, 1.0f, "failing compareAndExchangeRelease float value");
386 }
387
388 {
389 boolean success = false;
390 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
Paul Sandoz3bd5ebe2016-09-01 13:56:13 -0700391 success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_PLAIN).invokeExact(1.0f, 2.0f);
Paul Sandoz734dbe42016-06-20 17:57:19 +0200392 }
Paul Sandoz3bd5ebe2016-09-01 13:56:13 -0700393 assertEquals(success, true, "weakCompareAndSetPlain float");
Paul Sandoz734dbe42016-06-20 17:57:19 +0200394 float x = (float) hs.get(TestAccessMode.GET).invokeExact();
Paul Sandoz3bd5ebe2016-09-01 13:56:13 -0700395 assertEquals(x, 2.0f, "weakCompareAndSetPlain float value");
Paul Sandoz734dbe42016-06-20 17:57:19 +0200396 }
397
398 {
399 boolean success = false;
400 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
401 success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_ACQUIRE).invokeExact(2.0f, 1.0f);
402 }
403 assertEquals(success, true, "weakCompareAndSetAcquire float");
404 float x = (float) hs.get(TestAccessMode.GET).invokeExact();
405 assertEquals(x, 1.0f, "weakCompareAndSetAcquire float");
406 }
407
408 {
409 boolean success = false;
410 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
411 success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_RELEASE).invokeExact(1.0f, 2.0f);
412 }
413 assertEquals(success, true, "weakCompareAndSetRelease float");
414 float x = (float) hs.get(TestAccessMode.GET).invokeExact();
415 assertEquals(x, 2.0f, "weakCompareAndSetRelease float");
416 }
417
418 {
419 boolean success = false;
420 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
Paul Sandoz3bd5ebe2016-09-01 13:56:13 -0700421 success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET).invokeExact(2.0f, 1.0f);
Paul Sandoz734dbe42016-06-20 17:57:19 +0200422 }
Paul Sandoz3bd5ebe2016-09-01 13:56:13 -0700423 assertEquals(success, true, "weakCompareAndSet float");
Paul Sandoz734dbe42016-06-20 17:57:19 +0200424 float x = (float) hs.get(TestAccessMode.GET).invokeExact();
Paul Sandoz3bd5ebe2016-09-01 13:56:13 -0700425 assertEquals(x, 1.0f, "weakCompareAndSet float");
Paul Sandoz734dbe42016-06-20 17:57:19 +0200426 }
427
428 // Compare set and get
429 {
Paul Sandoz82d48912016-09-01 10:16:57 -0700430 hs.get(TestAccessMode.SET).invokeExact(1.0f);
431
432 float o = (float) hs.get(TestAccessMode.GET_AND_SET).invokeExact(2.0f);
Paul Sandoz734dbe42016-06-20 17:57:19 +0200433 assertEquals(o, 1.0f, "getAndSet float");
434 float x = (float) hs.get(TestAccessMode.GET).invokeExact();
435 assertEquals(x, 2.0f, "getAndSet float value");
436 }
437
Paul Sandoz82d48912016-09-01 10:16:57 -0700438 // Compare set and get
439 {
440 hs.get(TestAccessMode.SET).invokeExact(1.0f);
441
442 float o = (float) hs.get(TestAccessMode.GET_AND_SET_ACQUIRE).invokeExact(2.0f);
443 assertEquals(o, 1.0f, "getAndSetAcquire float");
444 float x = (float) hs.get(TestAccessMode.GET).invokeExact();
445 assertEquals(x, 2.0f, "getAndSetAcquire float value");
446 }
447
448 // Compare set and get
449 {
450 hs.get(TestAccessMode.SET).invokeExact(1.0f);
451
452 float o = (float) hs.get(TestAccessMode.GET_AND_SET_RELEASE).invokeExact(2.0f);
453 assertEquals(o, 1.0f, "getAndSetRelease float");
454 float x = (float) hs.get(TestAccessMode.GET).invokeExact();
455 assertEquals(x, 2.0f, "getAndSetRelease float value");
456 }
Paul Sandoz734dbe42016-06-20 17:57:19 +0200457
458 // get and add, add and get
459 {
Paul Sandoz82d48912016-09-01 10:16:57 -0700460 hs.get(TestAccessMode.SET).invokeExact(1.0f);
461
Paul Sandozc073edc2016-09-01 10:17:01 -0700462 float o = (float) hs.get(TestAccessMode.GET_AND_ADD).invokeExact(2.0f);
Paul Sandoz734dbe42016-06-20 17:57:19 +0200463 assertEquals(o, 1.0f, "getAndAdd float");
Paul Sandozc073edc2016-09-01 10:17:01 -0700464 float x = (float) hs.get(TestAccessMode.GET).invokeExact();
465 assertEquals(x, (float)(1.0f + 2.0f), "getAndAdd float value");
Paul Sandoz734dbe42016-06-20 17:57:19 +0200466 }
Paul Sandoz82d48912016-09-01 10:16:57 -0700467
468 {
469 hs.get(TestAccessMode.SET).invokeExact(1.0f);
470
471 float o = (float) hs.get(TestAccessMode.GET_AND_ADD_ACQUIRE).invokeExact(2.0f);
472 assertEquals(o, 1.0f, "getAndAddAcquire float");
473 float x = (float) hs.get(TestAccessMode.GET).invokeExact();
474 assertEquals(x, (float)(1.0f + 2.0f), "getAndAddAcquire float value");
475 }
476
477 {
478 hs.get(TestAccessMode.SET).invokeExact(1.0f);
479
480 float o = (float) hs.get(TestAccessMode.GET_AND_ADD_RELEASE).invokeExact(2.0f);
481 assertEquals(o, 1.0f, "getAndAddRelease float");
482 float x = (float) hs.get(TestAccessMode.GET).invokeExact();
483 assertEquals(x, (float)(1.0f + 2.0f), "getAndAddRelease float value");
484 }
485
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100486 }
487
488 static void testStaticFieldUnsupported(Handles hs) throws Throwable {
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100489
Paul Sandoz82d48912016-09-01 10:16:57 -0700490
491 for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_BITWISE)) {
492 checkUOE(am, () -> {
493 float r = (float) hs.get(am).invokeExact(1.0f);
494 });
495 }
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100496 }
497
498
499 static void testArray(Handles hs) throws Throwable {
500 float[] array = new float[10];
501
502 for (int i = 0; i < array.length; i++) {
503 // Plain
504 {
Paul Sandoza7aff442016-04-13 15:05:48 +0200505 hs.get(TestAccessMode.SET).invokeExact(array, i, 1.0f);
506 float x = (float) hs.get(TestAccessMode.GET).invokeExact(array, i);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100507 assertEquals(x, 1.0f, "get float value");
508 }
509
510
511 // Volatile
512 {
Paul Sandoza7aff442016-04-13 15:05:48 +0200513 hs.get(TestAccessMode.SET_VOLATILE).invokeExact(array, i, 2.0f);
514 float x = (float) hs.get(TestAccessMode.GET_VOLATILE).invokeExact(array, i);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100515 assertEquals(x, 2.0f, "setVolatile float value");
516 }
517
518 // Lazy
519 {
Paul Sandoza7aff442016-04-13 15:05:48 +0200520 hs.get(TestAccessMode.SET_RELEASE).invokeExact(array, i, 1.0f);
521 float x = (float) hs.get(TestAccessMode.GET_ACQUIRE).invokeExact(array, i);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100522 assertEquals(x, 1.0f, "setRelease float value");
523 }
524
525 // Opaque
526 {
Paul Sandoza7aff442016-04-13 15:05:48 +0200527 hs.get(TestAccessMode.SET_OPAQUE).invokeExact(array, i, 2.0f);
528 float x = (float) hs.get(TestAccessMode.GET_OPAQUE).invokeExact(array, i);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100529 assertEquals(x, 2.0f, "setOpaque float value");
530 }
531
Paul Sandoz734dbe42016-06-20 17:57:19 +0200532 hs.get(TestAccessMode.SET).invokeExact(array, i, 1.0f);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100533
Paul Sandoz734dbe42016-06-20 17:57:19 +0200534 // Compare
535 {
536 boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(array, i, 1.0f, 2.0f);
537 assertEquals(r, true, "success compareAndSet float");
538 float x = (float) hs.get(TestAccessMode.GET).invokeExact(array, i);
539 assertEquals(x, 2.0f, "success compareAndSet float value");
540 }
541
542 {
543 boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(array, i, 1.0f, 3.0f);
544 assertEquals(r, false, "failing compareAndSet float");
545 float x = (float) hs.get(TestAccessMode.GET).invokeExact(array, i);
546 assertEquals(x, 2.0f, "failing compareAndSet float value");
547 }
548
549 {
Paul Sandoz3f0273a2016-06-23 13:46:48 +0200550 float r = (float) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE).invokeExact(array, i, 2.0f, 1.0f);
551 assertEquals(r, 2.0f, "success compareAndExchange float");
Paul Sandoz734dbe42016-06-20 17:57:19 +0200552 float x = (float) hs.get(TestAccessMode.GET).invokeExact(array, i);
Paul Sandoz3f0273a2016-06-23 13:46:48 +0200553 assertEquals(x, 1.0f, "success compareAndExchange float value");
Paul Sandoz734dbe42016-06-20 17:57:19 +0200554 }
555
556 {
Paul Sandoz3f0273a2016-06-23 13:46:48 +0200557 float r = (float) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE).invokeExact(array, i, 2.0f, 3.0f);
558 assertEquals(r, 1.0f, "failing compareAndExchange float");
Paul Sandoz734dbe42016-06-20 17:57:19 +0200559 float x = (float) hs.get(TestAccessMode.GET).invokeExact(array, i);
Paul Sandoz3f0273a2016-06-23 13:46:48 +0200560 assertEquals(x, 1.0f, "failing compareAndExchange float value");
Paul Sandoz734dbe42016-06-20 17:57:19 +0200561 }
562
563 {
564 float r = (float) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(array, i, 1.0f, 2.0f);
565 assertEquals(r, 1.0f, "success compareAndExchangeAcquire float");
566 float x = (float) hs.get(TestAccessMode.GET).invokeExact(array, i);
567 assertEquals(x, 2.0f, "success compareAndExchangeAcquire float value");
568 }
569
570 {
571 float r = (float) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(array, i, 1.0f, 3.0f);
572 assertEquals(r, 2.0f, "failing compareAndExchangeAcquire float");
573 float x = (float) hs.get(TestAccessMode.GET).invokeExact(array, i);
574 assertEquals(x, 2.0f, "failing compareAndExchangeAcquire float value");
575 }
576
577 {
578 float r = (float) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(array, i, 2.0f, 1.0f);
579 assertEquals(r, 2.0f, "success compareAndExchangeRelease float");
580 float x = (float) hs.get(TestAccessMode.GET).invokeExact(array, i);
581 assertEquals(x, 1.0f, "success compareAndExchangeRelease float value");
582 }
583
584 {
585 float r = (float) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(array, i, 2.0f, 3.0f);
586 assertEquals(r, 1.0f, "failing compareAndExchangeRelease float");
587 float x = (float) hs.get(TestAccessMode.GET).invokeExact(array, i);
588 assertEquals(x, 1.0f, "failing compareAndExchangeRelease float value");
589 }
590
591 {
592 boolean success = false;
593 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
Paul Sandoz3bd5ebe2016-09-01 13:56:13 -0700594 success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_PLAIN).invokeExact(array, i, 1.0f, 2.0f);
Paul Sandoz734dbe42016-06-20 17:57:19 +0200595 }
Paul Sandoz3bd5ebe2016-09-01 13:56:13 -0700596 assertEquals(success, true, "weakCompareAndSetPlain float");
Paul Sandoz734dbe42016-06-20 17:57:19 +0200597 float x = (float) hs.get(TestAccessMode.GET).invokeExact(array, i);
Paul Sandoz3bd5ebe2016-09-01 13:56:13 -0700598 assertEquals(x, 2.0f, "weakCompareAndSetPlain float value");
Paul Sandoz734dbe42016-06-20 17:57:19 +0200599 }
600
601 {
602 boolean success = false;
603 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
604 success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_ACQUIRE).invokeExact(array, i, 2.0f, 1.0f);
605 }
606 assertEquals(success, true, "weakCompareAndSetAcquire float");
607 float x = (float) hs.get(TestAccessMode.GET).invokeExact(array, i);
608 assertEquals(x, 1.0f, "weakCompareAndSetAcquire float");
609 }
610
611 {
612 boolean success = false;
613 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
614 success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_RELEASE).invokeExact(array, i, 1.0f, 2.0f);
615 }
616 assertEquals(success, true, "weakCompareAndSetRelease float");
617 float x = (float) hs.get(TestAccessMode.GET).invokeExact(array, i);
618 assertEquals(x, 2.0f, "weakCompareAndSetRelease float");
619 }
620
621 {
622 boolean success = false;
623 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
Paul Sandoz3bd5ebe2016-09-01 13:56:13 -0700624 success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET).invokeExact(array, i, 2.0f, 1.0f);
Paul Sandoz734dbe42016-06-20 17:57:19 +0200625 }
Paul Sandoz3bd5ebe2016-09-01 13:56:13 -0700626 assertEquals(success, true, "weakCompareAndSet float");
Paul Sandoz734dbe42016-06-20 17:57:19 +0200627 float x = (float) hs.get(TestAccessMode.GET).invokeExact(array, i);
Paul Sandoz3bd5ebe2016-09-01 13:56:13 -0700628 assertEquals(x, 1.0f, "weakCompareAndSet float");
Paul Sandoz734dbe42016-06-20 17:57:19 +0200629 }
630
631 // Compare set and get
632 {
Paul Sandoz82d48912016-09-01 10:16:57 -0700633 hs.get(TestAccessMode.SET).invokeExact(array, i, 1.0f);
634
Paul Sandoz734dbe42016-06-20 17:57:19 +0200635 float o = (float) hs.get(TestAccessMode.GET_AND_SET).invokeExact(array, i, 2.0f);
636 assertEquals(o, 1.0f, "getAndSet float");
637 float x = (float) hs.get(TestAccessMode.GET).invokeExact(array, i);
638 assertEquals(x, 2.0f, "getAndSet float value");
639 }
640
Paul Sandoz82d48912016-09-01 10:16:57 -0700641 {
642 hs.get(TestAccessMode.SET).invokeExact(array, i, 1.0f);
643
644 float o = (float) hs.get(TestAccessMode.GET_AND_SET_ACQUIRE).invokeExact(array, i, 2.0f);
645 assertEquals(o, 1.0f, "getAndSetAcquire float");
646 float x = (float) hs.get(TestAccessMode.GET).invokeExact(array, i);
647 assertEquals(x, 2.0f, "getAndSetAcquire float value");
648 }
649
650 {
651 hs.get(TestAccessMode.SET).invokeExact(array, i, 1.0f);
652
653 float o = (float) hs.get(TestAccessMode.GET_AND_SET_RELEASE).invokeExact(array, i, 2.0f);
654 assertEquals(o, 1.0f, "getAndSetRelease float");
655 float x = (float) hs.get(TestAccessMode.GET).invokeExact(array, i);
656 assertEquals(x, 2.0f, "getAndSetRelease float value");
657 }
Paul Sandoz734dbe42016-06-20 17:57:19 +0200658
659 // get and add, add and get
660 {
Paul Sandoz82d48912016-09-01 10:16:57 -0700661 hs.get(TestAccessMode.SET).invokeExact(array, i, 1.0f);
662
Paul Sandozc073edc2016-09-01 10:17:01 -0700663 float o = (float) hs.get(TestAccessMode.GET_AND_ADD).invokeExact(array, i, 2.0f);
Paul Sandoz734dbe42016-06-20 17:57:19 +0200664 assertEquals(o, 1.0f, "getAndAdd float");
Paul Sandozc073edc2016-09-01 10:17:01 -0700665 float x = (float) hs.get(TestAccessMode.GET).invokeExact(array, i);
666 assertEquals(x, (float)(1.0f + 2.0f), "getAndAdd float value");
Paul Sandoz734dbe42016-06-20 17:57:19 +0200667 }
Paul Sandoz82d48912016-09-01 10:16:57 -0700668
669 {
670 hs.get(TestAccessMode.SET).invokeExact(array, i, 1.0f);
671
672 float o = (float) hs.get(TestAccessMode.GET_AND_ADD_ACQUIRE).invokeExact(array, i, 2.0f);
673 assertEquals(o, 1.0f, "getAndAddAcquire float");
674 float x = (float) hs.get(TestAccessMode.GET).invokeExact(array, i);
675 assertEquals(x, (float)(1.0f + 2.0f), "getAndAddAcquire float value");
676 }
677
678 {
679 hs.get(TestAccessMode.SET).invokeExact(array, i, 1.0f);
680
681 float o = (float) hs.get(TestAccessMode.GET_AND_ADD_RELEASE).invokeExact(array, i, 2.0f);
682 assertEquals(o, 1.0f, "getAndAddRelease float");
683 float x = (float) hs.get(TestAccessMode.GET).invokeExact(array, i);
684 assertEquals(x, (float)(1.0f + 2.0f), "getAndAddRelease float value");
685 }
686
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100687 }
688 }
689
690 static void testArrayUnsupported(Handles hs) throws Throwable {
691 float[] array = new float[10];
692
693 final int i = 0;
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100694
Paul Sandoz82d48912016-09-01 10:16:57 -0700695
696 for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_BITWISE)) {
697 checkUOE(am, () -> {
698 float o = (float) hs.get(am).invokeExact(array, i, 1.0f);
699 });
700 }
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100701 }
702
703 static void testArrayIndexOutOfBounds(Handles hs) throws Throwable {
704 float[] array = new float[10];
705
706 for (int i : new int[]{-1, Integer.MIN_VALUE, 10, 11, Integer.MAX_VALUE}) {
707 final int ci = i;
708
Paul Sandoza7aff442016-04-13 15:05:48 +0200709 for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET)) {
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100710 checkIOOBE(am, () -> {
711 float x = (float) hs.get(am).invokeExact(array, ci);
712 });
713 }
714
Paul Sandoza7aff442016-04-13 15:05:48 +0200715 for (TestAccessMode am : testAccessModesOfType(TestAccessType.SET)) {
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100716 checkIOOBE(am, () -> {
717 hs.get(am).invokeExact(array, ci, 1.0f);
718 });
719 }
720
Paul Sandoz734dbe42016-06-20 17:57:19 +0200721 for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_SET)) {
722 checkIOOBE(am, () -> {
723 boolean r = (boolean) hs.get(am).invokeExact(array, ci, 1.0f, 2.0f);
724 });
725 }
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100726
Paul Sandoz734dbe42016-06-20 17:57:19 +0200727 for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_EXCHANGE)) {
728 checkIOOBE(am, () -> {
729 float r = (float) hs.get(am).invokeExact(array, ci, 2.0f, 1.0f);
730 });
731 }
732
733 for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_SET)) {
734 checkIOOBE(am, () -> {
735 float o = (float) hs.get(am).invokeExact(array, ci, 1.0f);
736 });
737 }
738
739 for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_ADD)) {
740 checkIOOBE(am, () -> {
741 float o = (float) hs.get(am).invokeExact(array, ci, 3.0f);
742 });
743 }
Paul Sandoz82d48912016-09-01 10:16:57 -0700744
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100745 }
746 }
747}
748