blob: 99309e5081f68ef39e1ee448a66aeee330eb78fc [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
Paul Sandoz44afe202016-05-17 12:06:41 +020027 * @run testng/othervm -Diters=20000 -Djava.lang.invoke.VarHandle.VAR_HANDLE_GUARDS=false VarHandleTestMethodHandleAccessFloat
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 VarHandleTestMethodHandleAccessFloat extends VarHandleBaseTest {
43 static final float static_final_v = 1.0f;
44
45 static float static_v;
46
47 final float final_v = 1.0f;
48
49 float 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 VarHandleTestMethodHandleAccessFloat.class, "final_v", float.class);
65
66 vhField = MethodHandles.lookup().findVarHandle(
67 VarHandleTestMethodHandleAccessFloat.class, "v", float.class);
68
69 vhStaticFinalField = MethodHandles.lookup().findStaticVarHandle(
70 VarHandleTestMethodHandleAccessFloat.class, "static_final_v", float.class);
71
72 vhStaticField = MethodHandles.lookup().findStaticVarHandle(
73 VarHandleTestMethodHandleAccessFloat.class, "static_v", float.class);
74
75 vhArray = MethodHandles.arrayElementVarHandle(float[].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, VarHandleTestMethodHandleAccessFloat::testStaticField));
92 cases.add(new MethodHandleAccessTestCase("Static field unsupported",
93 vhStaticField, f, VarHandleTestMethodHandleAccessFloat::testStaticFieldUnsupported,
94 false));
95
96 cases.add(new MethodHandleAccessTestCase("Array",
97 vhArray, f, VarHandleTestMethodHandleAccessFloat::testArray));
98 cases.add(new MethodHandleAccessTestCase("Array unsupported",
99 vhArray, f, VarHandleTestMethodHandleAccessFloat::testArrayUnsupported,
100 false));
101 cases.add(new MethodHandleAccessTestCase("Array index out of bounds",
102 vhArray, f, VarHandleTestMethodHandleAccessFloat::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(VarHandleTestMethodHandleAccessFloat recv, Handles hs) throws Throwable {
123 // Plain
124 {
Paul Sandoza7aff442016-04-13 15:05:48 +0200125 hs.get(TestAccessMode.SET).invokeExact(recv, 1.0f);
126 float x = (float) hs.get(TestAccessMode.GET).invokeExact(recv);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100127 assertEquals(x, 1.0f, "set float value");
128 }
129
130
131 // Volatile
132 {
Paul Sandoza7aff442016-04-13 15:05:48 +0200133 hs.get(TestAccessMode.SET_VOLATILE).invokeExact(recv, 2.0f);
134 float x = (float) hs.get(TestAccessMode.GET_VOLATILE).invokeExact(recv);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100135 assertEquals(x, 2.0f, "setVolatile float value");
136 }
137
138 // Lazy
139 {
Paul Sandoza7aff442016-04-13 15:05:48 +0200140 hs.get(TestAccessMode.SET_RELEASE).invokeExact(recv, 1.0f);
141 float x = (float) hs.get(TestAccessMode.GET_ACQUIRE).invokeExact(recv);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100142 assertEquals(x, 1.0f, "setRelease float value");
143 }
144
145 // Opaque
146 {
Paul Sandoza7aff442016-04-13 15:05:48 +0200147 hs.get(TestAccessMode.SET_OPAQUE).invokeExact(recv, 2.0f);
148 float x = (float) hs.get(TestAccessMode.GET_OPAQUE).invokeExact(recv);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100149 assertEquals(x, 2.0f, "setOpaque float value");
150 }
151
152
153 }
154
155 static void testInstanceFieldUnsupported(VarHandleTestMethodHandleAccessFloat recv, Handles hs) throws Throwable {
Paul Sandoza7aff442016-04-13 15:05:48 +0200156 for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_SET)) {
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100157 checkUOE(am, () -> {
158 boolean r = (boolean) hs.get(am).invokeExact(recv, 1.0f, 2.0f);
159 });
160 }
161
Paul Sandoza7aff442016-04-13 15:05:48 +0200162 for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_EXCHANGE)) {
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100163 checkUOE(am, () -> {
164 float r = (float) hs.get(am).invokeExact(recv, 1.0f, 2.0f);
165 });
166 }
167
Paul Sandoza7aff442016-04-13 15:05:48 +0200168 for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_SET)) {
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100169 checkUOE(am, () -> {
170 float r = (float) hs.get(am).invokeExact(recv, 1.0f);
171 });
172 }
173
Paul Sandoza7aff442016-04-13 15:05:48 +0200174 for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_ADD)) {
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100175 checkUOE(am, () -> {
176 float r = (float) hs.get(am).invokeExact(recv, 1.0f);
177 });
178 }
179 }
180
181
182 static void testStaticField(Handles hs) throws Throwable {
183 // Plain
184 {
Paul Sandoza7aff442016-04-13 15:05:48 +0200185 hs.get(TestAccessMode.SET).invokeExact(1.0f);
186 float x = (float) hs.get(TestAccessMode.GET).invokeExact();
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100187 assertEquals(x, 1.0f, "set float value");
188 }
189
190
191 // Volatile
192 {
Paul Sandoza7aff442016-04-13 15:05:48 +0200193 hs.get(TestAccessMode.SET_VOLATILE).invokeExact(2.0f);
194 float x = (float) hs.get(TestAccessMode.GET_VOLATILE).invokeExact();
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100195 assertEquals(x, 2.0f, "setVolatile float value");
196 }
197
198 // Lazy
199 {
Paul Sandoza7aff442016-04-13 15:05:48 +0200200 hs.get(TestAccessMode.SET_RELEASE).invokeExact(1.0f);
201 float x = (float) hs.get(TestAccessMode.GET_ACQUIRE).invokeExact();
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100202 assertEquals(x, 1.0f, "setRelease float value");
203 }
204
205 // Opaque
206 {
Paul Sandoza7aff442016-04-13 15:05:48 +0200207 hs.get(TestAccessMode.SET_OPAQUE).invokeExact(2.0f);
208 float x = (float) hs.get(TestAccessMode.GET_OPAQUE).invokeExact();
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100209 assertEquals(x, 2.0f, "setOpaque float value");
210 }
211
212
213 }
214
215 static void testStaticFieldUnsupported(Handles hs) throws Throwable {
Paul Sandoza7aff442016-04-13 15:05:48 +0200216 for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_SET)) {
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100217 checkUOE(am, () -> {
218 boolean r = (boolean) hs.get(am).invokeExact(1.0f, 2.0f);
219 });
220 }
221
Paul Sandoza7aff442016-04-13 15:05:48 +0200222 for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_EXCHANGE)) {
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100223 checkUOE(am, () -> {
224 float r = (float) hs.get(am).invokeExact(1.0f, 2.0f);
225 });
226 }
227
Paul Sandoza7aff442016-04-13 15:05:48 +0200228 for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_SET)) {
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100229 checkUOE(am, () -> {
230 float r = (float) hs.get(am).invokeExact(1.0f);
231 });
232 }
233
Paul Sandoza7aff442016-04-13 15:05:48 +0200234 for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_ADD)) {
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100235 checkUOE(am, () -> {
236 float r = (float) hs.get(am).invokeExact(1.0f);
237 });
238 }
239 }
240
241
242 static void testArray(Handles hs) throws Throwable {
243 float[] array = new float[10];
244
245 for (int i = 0; i < array.length; i++) {
246 // Plain
247 {
Paul Sandoza7aff442016-04-13 15:05:48 +0200248 hs.get(TestAccessMode.SET).invokeExact(array, i, 1.0f);
249 float x = (float) hs.get(TestAccessMode.GET).invokeExact(array, i);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100250 assertEquals(x, 1.0f, "get float value");
251 }
252
253
254 // Volatile
255 {
Paul Sandoza7aff442016-04-13 15:05:48 +0200256 hs.get(TestAccessMode.SET_VOLATILE).invokeExact(array, i, 2.0f);
257 float x = (float) hs.get(TestAccessMode.GET_VOLATILE).invokeExact(array, i);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100258 assertEquals(x, 2.0f, "setVolatile float value");
259 }
260
261 // Lazy
262 {
Paul Sandoza7aff442016-04-13 15:05:48 +0200263 hs.get(TestAccessMode.SET_RELEASE).invokeExact(array, i, 1.0f);
264 float x = (float) hs.get(TestAccessMode.GET_ACQUIRE).invokeExact(array, i);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100265 assertEquals(x, 1.0f, "setRelease float value");
266 }
267
268 // Opaque
269 {
Paul Sandoza7aff442016-04-13 15:05:48 +0200270 hs.get(TestAccessMode.SET_OPAQUE).invokeExact(array, i, 2.0f);
271 float x = (float) hs.get(TestAccessMode.GET_OPAQUE).invokeExact(array, i);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100272 assertEquals(x, 2.0f, "setOpaque float value");
273 }
274
275
276 }
277 }
278
279 static void testArrayUnsupported(Handles hs) throws Throwable {
280 float[] array = new float[10];
281
282 final int i = 0;
Paul Sandoza7aff442016-04-13 15:05:48 +0200283 for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_SET)) {
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100284 checkUOE(am, () -> {
285 boolean r = (boolean) hs.get(am).invokeExact(array, i, 1.0f, 2.0f);
286 });
287 }
288
Paul Sandoza7aff442016-04-13 15:05:48 +0200289 for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_EXCHANGE)) {
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100290 checkUOE(am, () -> {
291 float r = (float) hs.get(am).invokeExact(array, i, 1.0f, 2.0f);
292 });
293 }
294
Paul Sandoza7aff442016-04-13 15:05:48 +0200295 for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_SET)) {
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100296 checkUOE(am, () -> {
297 float r = (float) hs.get(am).invokeExact(array, i, 1.0f);
298 });
299 }
300
Paul Sandoza7aff442016-04-13 15:05:48 +0200301 for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_ADD)) {
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100302 checkUOE(am, () -> {
303 float o = (float) hs.get(am).invokeExact(array, i, 1.0f);
304 });
305 }
306 }
307
308 static void testArrayIndexOutOfBounds(Handles hs) throws Throwable {
309 float[] array = new float[10];
310
311 for (int i : new int[]{-1, Integer.MIN_VALUE, 10, 11, Integer.MAX_VALUE}) {
312 final int ci = i;
313
Paul Sandoza7aff442016-04-13 15:05:48 +0200314 for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET)) {
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100315 checkIOOBE(am, () -> {
316 float x = (float) hs.get(am).invokeExact(array, ci);
317 });
318 }
319
Paul Sandoza7aff442016-04-13 15:05:48 +0200320 for (TestAccessMode am : testAccessModesOfType(TestAccessType.SET)) {
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100321 checkIOOBE(am, () -> {
322 hs.get(am).invokeExact(array, ci, 1.0f);
323 });
324 }
325
326
327 }
328 }
329}
330