Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | * accompanied this code). |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License version |
| 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
| 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | * or visit www.oracle.com if you need additional information or have any |
| 21 | * questions. |
| 22 | */ |
| 23 | |
| 24 | /* |
| 25 | * @test |
| 26 | * @run testng/othervm -Diters=20000 VarHandleTestMethodHandleAccessFloat |
Paul Sandoz | 44afe20 | 2016-05-17 12:06:41 +0200 | [diff] [blame] | 27 | * @run testng/othervm -Diters=20000 -Djava.lang.invoke.VarHandle.VAR_HANDLE_GUARDS=false VarHandleTestMethodHandleAccessFloat |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 28 | */ |
| 29 | |
| 30 | import org.testng.annotations.BeforeClass; |
| 31 | import org.testng.annotations.DataProvider; |
| 32 | import org.testng.annotations.Test; |
| 33 | |
| 34 | import java.lang.invoke.MethodHandles; |
| 35 | import java.lang.invoke.VarHandle; |
| 36 | import java.util.ArrayList; |
| 37 | import java.util.Arrays; |
| 38 | import java.util.List; |
| 39 | |
| 40 | import static org.testng.Assert.*; |
| 41 | |
| 42 | public class 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 Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 125 | hs.get(TestAccessMode.SET).invokeExact(recv, 1.0f); |
| 126 | float x = (float) hs.get(TestAccessMode.GET).invokeExact(recv); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 127 | assertEquals(x, 1.0f, "set float value"); |
| 128 | } |
| 129 | |
| 130 | |
| 131 | // Volatile |
| 132 | { |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 133 | hs.get(TestAccessMode.SET_VOLATILE).invokeExact(recv, 2.0f); |
| 134 | float x = (float) hs.get(TestAccessMode.GET_VOLATILE).invokeExact(recv); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 135 | assertEquals(x, 2.0f, "setVolatile float value"); |
| 136 | } |
| 137 | |
| 138 | // Lazy |
| 139 | { |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 140 | hs.get(TestAccessMode.SET_RELEASE).invokeExact(recv, 1.0f); |
| 141 | float x = (float) hs.get(TestAccessMode.GET_ACQUIRE).invokeExact(recv); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 142 | assertEquals(x, 1.0f, "setRelease float value"); |
| 143 | } |
| 144 | |
| 145 | // Opaque |
| 146 | { |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 147 | hs.get(TestAccessMode.SET_OPAQUE).invokeExact(recv, 2.0f); |
| 148 | float x = (float) hs.get(TestAccessMode.GET_OPAQUE).invokeExact(recv); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 149 | assertEquals(x, 2.0f, "setOpaque float value"); |
| 150 | } |
| 151 | |
| 152 | |
| 153 | } |
| 154 | |
| 155 | static void testInstanceFieldUnsupported(VarHandleTestMethodHandleAccessFloat recv, Handles hs) throws Throwable { |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 156 | for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_SET)) { |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 157 | checkUOE(am, () -> { |
| 158 | boolean r = (boolean) hs.get(am).invokeExact(recv, 1.0f, 2.0f); |
| 159 | }); |
| 160 | } |
| 161 | |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 162 | for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_EXCHANGE)) { |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 163 | checkUOE(am, () -> { |
| 164 | float r = (float) hs.get(am).invokeExact(recv, 1.0f, 2.0f); |
| 165 | }); |
| 166 | } |
| 167 | |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 168 | for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_SET)) { |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 169 | checkUOE(am, () -> { |
| 170 | float r = (float) hs.get(am).invokeExact(recv, 1.0f); |
| 171 | }); |
| 172 | } |
| 173 | |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 174 | for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_ADD)) { |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 175 | 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 Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 185 | hs.get(TestAccessMode.SET).invokeExact(1.0f); |
| 186 | float x = (float) hs.get(TestAccessMode.GET).invokeExact(); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 187 | assertEquals(x, 1.0f, "set float value"); |
| 188 | } |
| 189 | |
| 190 | |
| 191 | // Volatile |
| 192 | { |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 193 | hs.get(TestAccessMode.SET_VOLATILE).invokeExact(2.0f); |
| 194 | float x = (float) hs.get(TestAccessMode.GET_VOLATILE).invokeExact(); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 195 | assertEquals(x, 2.0f, "setVolatile float value"); |
| 196 | } |
| 197 | |
| 198 | // Lazy |
| 199 | { |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 200 | hs.get(TestAccessMode.SET_RELEASE).invokeExact(1.0f); |
| 201 | float x = (float) hs.get(TestAccessMode.GET_ACQUIRE).invokeExact(); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 202 | assertEquals(x, 1.0f, "setRelease float value"); |
| 203 | } |
| 204 | |
| 205 | // Opaque |
| 206 | { |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 207 | hs.get(TestAccessMode.SET_OPAQUE).invokeExact(2.0f); |
| 208 | float x = (float) hs.get(TestAccessMode.GET_OPAQUE).invokeExact(); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 209 | assertEquals(x, 2.0f, "setOpaque float value"); |
| 210 | } |
| 211 | |
| 212 | |
| 213 | } |
| 214 | |
| 215 | static void testStaticFieldUnsupported(Handles hs) throws Throwable { |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 216 | for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_SET)) { |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 217 | checkUOE(am, () -> { |
| 218 | boolean r = (boolean) hs.get(am).invokeExact(1.0f, 2.0f); |
| 219 | }); |
| 220 | } |
| 221 | |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 222 | for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_EXCHANGE)) { |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 223 | checkUOE(am, () -> { |
| 224 | float r = (float) hs.get(am).invokeExact(1.0f, 2.0f); |
| 225 | }); |
| 226 | } |
| 227 | |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 228 | for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_SET)) { |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 229 | checkUOE(am, () -> { |
| 230 | float r = (float) hs.get(am).invokeExact(1.0f); |
| 231 | }); |
| 232 | } |
| 233 | |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 234 | for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_ADD)) { |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 235 | 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 Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 248 | hs.get(TestAccessMode.SET).invokeExact(array, i, 1.0f); |
| 249 | float x = (float) hs.get(TestAccessMode.GET).invokeExact(array, i); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 250 | assertEquals(x, 1.0f, "get float value"); |
| 251 | } |
| 252 | |
| 253 | |
| 254 | // Volatile |
| 255 | { |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 256 | hs.get(TestAccessMode.SET_VOLATILE).invokeExact(array, i, 2.0f); |
| 257 | float x = (float) hs.get(TestAccessMode.GET_VOLATILE).invokeExact(array, i); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 258 | assertEquals(x, 2.0f, "setVolatile float value"); |
| 259 | } |
| 260 | |
| 261 | // Lazy |
| 262 | { |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 263 | hs.get(TestAccessMode.SET_RELEASE).invokeExact(array, i, 1.0f); |
| 264 | float x = (float) hs.get(TestAccessMode.GET_ACQUIRE).invokeExact(array, i); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 265 | assertEquals(x, 1.0f, "setRelease float value"); |
| 266 | } |
| 267 | |
| 268 | // Opaque |
| 269 | { |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 270 | hs.get(TestAccessMode.SET_OPAQUE).invokeExact(array, i, 2.0f); |
| 271 | float x = (float) hs.get(TestAccessMode.GET_OPAQUE).invokeExact(array, i); |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 272 | 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 Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 283 | for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_SET)) { |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 284 | checkUOE(am, () -> { |
| 285 | boolean r = (boolean) hs.get(am).invokeExact(array, i, 1.0f, 2.0f); |
| 286 | }); |
| 287 | } |
| 288 | |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 289 | for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_EXCHANGE)) { |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 290 | checkUOE(am, () -> { |
| 291 | float r = (float) hs.get(am).invokeExact(array, i, 1.0f, 2.0f); |
| 292 | }); |
| 293 | } |
| 294 | |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 295 | for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_SET)) { |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 296 | checkUOE(am, () -> { |
| 297 | float r = (float) hs.get(am).invokeExact(array, i, 1.0f); |
| 298 | }); |
| 299 | } |
| 300 | |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 301 | for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_ADD)) { |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 302 | 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 Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 314 | for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET)) { |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 315 | checkIOOBE(am, () -> { |
| 316 | float x = (float) hs.get(am).invokeExact(array, ci); |
| 317 | }); |
| 318 | } |
| 319 | |
Paul Sandoz | a7aff44 | 2016-04-13 15:05:48 +0200 | [diff] [blame] | 320 | for (TestAccessMode am : testAccessModesOfType(TestAccessType.SET)) { |
Paul Sandoz | 9fb30a3 | 2016-03-24 11:21:21 +0100 | [diff] [blame] | 321 | checkIOOBE(am, () -> { |
| 322 | hs.get(am).invokeExact(array, ci, 1.0f); |
| 323 | }); |
| 324 | } |
| 325 | |
| 326 | |
| 327 | } |
| 328 | } |
| 329 | } |
| 330 | |