blob: 58d9e2952989147f0d18adb0da6f4da5f3ec9fb9 [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
151
152 }
153
154 static void testInstanceFieldUnsupported(VarHandleTestMethodHandleAccessFloat recv, Handles hs) throws Throwable {
Paul Sandoza7aff442016-04-13 15:05:48 +0200155 for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_SET)) {
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100156 checkUOE(am, () -> {
157 boolean r = (boolean) hs.get(am).invokeExact(recv, 1.0f, 2.0f);
158 });
159 }
160
Paul Sandoza7aff442016-04-13 15:05:48 +0200161 for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_EXCHANGE)) {
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100162 checkUOE(am, () -> {
163 float r = (float) hs.get(am).invokeExact(recv, 1.0f, 2.0f);
164 });
165 }
166
Paul Sandoza7aff442016-04-13 15:05:48 +0200167 for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_SET)) {
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100168 checkUOE(am, () -> {
169 float r = (float) hs.get(am).invokeExact(recv, 1.0f);
170 });
171 }
172
Paul Sandoza7aff442016-04-13 15:05:48 +0200173 for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_ADD)) {
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100174 checkUOE(am, () -> {
175 float r = (float) hs.get(am).invokeExact(recv, 1.0f);
176 });
177 }
178 }
179
180
181 static void testStaticField(Handles hs) throws Throwable {
182 // Plain
183 {
Paul Sandoza7aff442016-04-13 15:05:48 +0200184 hs.get(TestAccessMode.SET).invokeExact(1.0f);
185 float x = (float) hs.get(TestAccessMode.GET).invokeExact();
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100186 assertEquals(x, 1.0f, "set float value");
187 }
188
189
190 // Volatile
191 {
Paul Sandoza7aff442016-04-13 15:05:48 +0200192 hs.get(TestAccessMode.SET_VOLATILE).invokeExact(2.0f);
193 float x = (float) hs.get(TestAccessMode.GET_VOLATILE).invokeExact();
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100194 assertEquals(x, 2.0f, "setVolatile float value");
195 }
196
197 // Lazy
198 {
Paul Sandoza7aff442016-04-13 15:05:48 +0200199 hs.get(TestAccessMode.SET_RELEASE).invokeExact(1.0f);
200 float x = (float) hs.get(TestAccessMode.GET_ACQUIRE).invokeExact();
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100201 assertEquals(x, 1.0f, "setRelease float value");
202 }
203
204 // Opaque
205 {
Paul Sandoza7aff442016-04-13 15:05:48 +0200206 hs.get(TestAccessMode.SET_OPAQUE).invokeExact(2.0f);
207 float x = (float) hs.get(TestAccessMode.GET_OPAQUE).invokeExact();
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100208 assertEquals(x, 2.0f, "setOpaque float value");
209 }
210
211
212 }
213
214 static void testStaticFieldUnsupported(Handles hs) throws Throwable {
Paul Sandoza7aff442016-04-13 15:05:48 +0200215 for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_SET)) {
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100216 checkUOE(am, () -> {
217 boolean r = (boolean) hs.get(am).invokeExact(1.0f, 2.0f);
218 });
219 }
220
Paul Sandoza7aff442016-04-13 15:05:48 +0200221 for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_EXCHANGE)) {
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100222 checkUOE(am, () -> {
223 float r = (float) hs.get(am).invokeExact(1.0f, 2.0f);
224 });
225 }
226
Paul Sandoza7aff442016-04-13 15:05:48 +0200227 for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_SET)) {
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100228 checkUOE(am, () -> {
229 float r = (float) hs.get(am).invokeExact(1.0f);
230 });
231 }
232
Paul Sandoza7aff442016-04-13 15:05:48 +0200233 for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_ADD)) {
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100234 checkUOE(am, () -> {
235 float r = (float) hs.get(am).invokeExact(1.0f);
236 });
237 }
238 }
239
240
241 static void testArray(Handles hs) throws Throwable {
242 float[] array = new float[10];
243
244 for (int i = 0; i < array.length; i++) {
245 // Plain
246 {
Paul Sandoza7aff442016-04-13 15:05:48 +0200247 hs.get(TestAccessMode.SET).invokeExact(array, i, 1.0f);
248 float x = (float) hs.get(TestAccessMode.GET).invokeExact(array, i);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100249 assertEquals(x, 1.0f, "get float value");
250 }
251
252
253 // Volatile
254 {
Paul Sandoza7aff442016-04-13 15:05:48 +0200255 hs.get(TestAccessMode.SET_VOLATILE).invokeExact(array, i, 2.0f);
256 float x = (float) hs.get(TestAccessMode.GET_VOLATILE).invokeExact(array, i);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100257 assertEquals(x, 2.0f, "setVolatile float value");
258 }
259
260 // Lazy
261 {
Paul Sandoza7aff442016-04-13 15:05:48 +0200262 hs.get(TestAccessMode.SET_RELEASE).invokeExact(array, i, 1.0f);
263 float x = (float) hs.get(TestAccessMode.GET_ACQUIRE).invokeExact(array, i);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100264 assertEquals(x, 1.0f, "setRelease float value");
265 }
266
267 // Opaque
268 {
Paul Sandoza7aff442016-04-13 15:05:48 +0200269 hs.get(TestAccessMode.SET_OPAQUE).invokeExact(array, i, 2.0f);
270 float x = (float) hs.get(TestAccessMode.GET_OPAQUE).invokeExact(array, i);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100271 assertEquals(x, 2.0f, "setOpaque float value");
272 }
273
274
275 }
276 }
277
278 static void testArrayUnsupported(Handles hs) throws Throwable {
279 float[] array = new float[10];
280
281 final int i = 0;
Paul Sandoza7aff442016-04-13 15:05:48 +0200282 for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_SET)) {
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100283 checkUOE(am, () -> {
284 boolean r = (boolean) hs.get(am).invokeExact(array, i, 1.0f, 2.0f);
285 });
286 }
287
Paul Sandoza7aff442016-04-13 15:05:48 +0200288 for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_EXCHANGE)) {
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100289 checkUOE(am, () -> {
290 float r = (float) hs.get(am).invokeExact(array, i, 1.0f, 2.0f);
291 });
292 }
293
Paul Sandoza7aff442016-04-13 15:05:48 +0200294 for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_SET)) {
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100295 checkUOE(am, () -> {
296 float r = (float) hs.get(am).invokeExact(array, i, 1.0f);
297 });
298 }
299
Paul Sandoza7aff442016-04-13 15:05:48 +0200300 for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_ADD)) {
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100301 checkUOE(am, () -> {
302 float o = (float) hs.get(am).invokeExact(array, i, 1.0f);
303 });
304 }
305 }
306
307 static void testArrayIndexOutOfBounds(Handles hs) throws Throwable {
308 float[] array = new float[10];
309
310 for (int i : new int[]{-1, Integer.MIN_VALUE, 10, 11, Integer.MAX_VALUE}) {
311 final int ci = i;
312
Paul Sandoza7aff442016-04-13 15:05:48 +0200313 for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET)) {
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100314 checkIOOBE(am, () -> {
315 float x = (float) hs.get(am).invokeExact(array, ci);
316 });
317 }
318
Paul Sandoza7aff442016-04-13 15:05:48 +0200319 for (TestAccessMode am : testAccessModesOfType(TestAccessType.SET)) {
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100320 checkIOOBE(am, () -> {
321 hs.get(am).invokeExact(array, ci, 1.0f);
322 });
323 }
324
325
326 }
327 }
328}
329