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 VarHandleTestMethodHandleAccessByte
27 */
28
29 import org.testng.annotations.BeforeClass;
30 import org.testng.annotations.DataProvider;
31 import org.testng.annotations.Test;
32
33 import java.lang.invoke.MethodHandles;
34 import java.lang.invoke.VarHandle;
35 import java.util.ArrayList;
36 import java.util.Arrays;
37 import java.util.List;
38
39 import static org.testng.Assert.*;
40
41 public class VarHandleTestMethodHandleAccessByte extends VarHandleBaseTest {
42 static final byte static_final_v = (byte)0x01;
43
44 static byte static_v;
45
46 final byte final_v = (byte)0x01;
47
48 byte 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 VarHandleTestMethodHandleAccessByte.class, "final_v", byte.class);
64
65 vhField = MethodHandles.lookup().findVarHandle(
66 VarHandleTestMethodHandleAccessByte.class, "v", byte.class);
67
68 vhStaticFinalField = MethodHandles.lookup().findStaticVarHandle(
69 VarHandleTestMethodHandleAccessByte.class, "static_final_v", byte.class);
70
71 vhStaticField = MethodHandles.lookup().findStaticVarHandle(
72 VarHandleTestMethodHandleAccessByte.class, "static_v", byte.class);
73
74 vhArray = MethodHandles.arrayElementVarHandle(byte[].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, VarHandleTestMethodHandleAccessByte::testStaticField));
91 cases.add(new MethodHandleAccessTestCase("Static field unsupported",
92 vhStaticField, f, VarHandleTestMethodHandleAccessByte::testStaticFieldUnsupported,
93 false));
94
95 cases.add(new MethodHandleAccessTestCase("Array",
96 vhArray, f, VarHandleTestMethodHandleAccessByte::testArray));
97 cases.add(new MethodHandleAccessTestCase("Array unsupported",
98 vhArray, f, VarHandleTestMethodHandleAccessByte::testArrayUnsupported,
99 false));
100 cases.add(new MethodHandleAccessTestCase("Array index out of bounds",
101 vhArray, f, VarHandleTestMethodHandleAccessByte::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(VarHandleTestMethodHandleAccessByte recv, Handles hs) throws Throwable {
122 // Plain
356 assertEquals(o, (byte)0x01, "getAndBitwiseXorAcquire byte");
357 byte x = (byte) hs.get(TestAccessMode.GET).invokeExact(recv);
358 assertEquals(x, (byte)((byte)0x01 ^ (byte)0x23), "getAndBitwiseXorAcquire byte value");
359 }
360
361 {
362 hs.get(TestAccessMode.SET).invokeExact(recv, (byte)0x01);
363
364 byte o = (byte) hs.get(TestAccessMode.GET_AND_BITWISE_XOR_RELEASE).invokeExact(recv, (byte)0x23);
365 assertEquals(o, (byte)0x01, "getAndBitwiseXorRelease byte");
366 byte x = (byte) hs.get(TestAccessMode.GET).invokeExact(recv);
367 assertEquals(x, (byte)((byte)0x01 ^ (byte)0x23), "getAndBitwiseXorRelease byte value");
368 }
369 }
370
371 static void testInstanceFieldUnsupported(VarHandleTestMethodHandleAccessByte recv, Handles hs) throws Throwable {
372
373
374 }
375
376
377 static void testStaticField(Handles hs) throws Throwable {
378 // Plain
379 {
380 hs.get(TestAccessMode.SET).invokeExact((byte)0x01);
381 byte x = (byte) hs.get(TestAccessMode.GET).invokeExact();
382 assertEquals(x, (byte)0x01, "set byte value");
383 }
384
385
386 // Volatile
387 {
388 hs.get(TestAccessMode.SET_VOLATILE).invokeExact((byte)0x23);
389 byte x = (byte) hs.get(TestAccessMode.GET_VOLATILE).invokeExact();
390 assertEquals(x, (byte)0x23, "setVolatile byte value");
391 }
392
393 // Lazy
394 {
395 hs.get(TestAccessMode.SET_RELEASE).invokeExact((byte)0x01);
|
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 // -- This file was mechanically generated: Do not edit! -- //
25
26 /*
27 * @test
28 * @run testng/othervm -Diters=20000 VarHandleTestMethodHandleAccessByte
29 */
30
31 import org.testng.annotations.BeforeClass;
32 import org.testng.annotations.DataProvider;
33 import org.testng.annotations.Test;
34
35 import java.lang.invoke.MethodHandles;
36 import java.lang.invoke.VarHandle;
37 import java.util.ArrayList;
38 import java.util.Arrays;
39 import java.util.List;
40
41 import static org.testng.Assert.*;
42
43 public class VarHandleTestMethodHandleAccessByte extends VarHandleBaseTest {
44 static final Class<?> type = byte.class;
45
46 static final byte static_final_v = (byte)0x01;
47
48 static byte static_v;
49
50 final byte final_v = (byte)0x01;
51
52 byte v;
53
54 VarHandle vhFinalField;
55
56 VarHandle vhField;
57
58 VarHandle vhStaticField;
59
60 VarHandle vhStaticFinalField;
61
62 VarHandle vhArray;
63
64 VarHandle vhValueTypeField;
65
66 @BeforeClass
67 public void setup() throws Exception {
68 vhFinalField = MethodHandles.lookup().findVarHandle(
69 VarHandleTestMethodHandleAccessByte.class, "final_v", type);
70
71 vhField = MethodHandles.lookup().findVarHandle(
72 VarHandleTestMethodHandleAccessByte.class, "v", type);
73
74 vhStaticFinalField = MethodHandles.lookup().findStaticVarHandle(
75 VarHandleTestMethodHandleAccessByte.class, "static_final_v", type);
76
77 vhStaticField = MethodHandles.lookup().findStaticVarHandle(
78 VarHandleTestMethodHandleAccessByte.class, "static_v", type);
79
80 vhArray = MethodHandles.arrayElementVarHandle(byte[].class);
81
82 vhValueTypeField = MethodHandles.lookup().findVarHandle(
83 Value.class, "byte_v", type);
84 }
85
86
87 @DataProvider
88 public Object[][] accessTestCaseProvider() throws Exception {
89 List<AccessTestCase<?>> cases = new ArrayList<>();
90
91 for (VarHandleToMethodHandle f : VarHandleToMethodHandle.values()) {
92 cases.add(new MethodHandleAccessTestCase("Instance field",
93 vhField, f, hs -> testInstanceField(this, hs)));
94 cases.add(new MethodHandleAccessTestCase("Instance field unsupported",
95 vhField, f, hs -> testInstanceFieldUnsupported(this, hs),
96 false));
97
98 cases.add(new MethodHandleAccessTestCase("Static field",
99 vhStaticField, f, VarHandleTestMethodHandleAccessByte::testStaticField));
100 cases.add(new MethodHandleAccessTestCase("Static field unsupported",
101 vhStaticField, f, VarHandleTestMethodHandleAccessByte::testStaticFieldUnsupported,
102 false));
103
104 cases.add(new MethodHandleAccessTestCase("Array",
105 vhArray, f, VarHandleTestMethodHandleAccessByte::testArray));
106 cases.add(new MethodHandleAccessTestCase("Array unsupported",
107 vhArray, f, VarHandleTestMethodHandleAccessByte::testArrayUnsupported,
108 false));
109 cases.add(new MethodHandleAccessTestCase("Array index out of bounds",
110 vhArray, f, VarHandleTestMethodHandleAccessByte::testArrayIndexOutOfBounds,
111 false));
112 cases.add(new MethodHandleAccessTestCase("Value type field",
113 vhValueTypeField, f, hs -> testValueTypeField(Value.getInstance(), hs)));
114 cases.add(new MethodHandleAccessTestCase("Value type field unsupported",
115 vhValueTypeField, f, hs -> testValueTypeFieldUnsupported(Value.getInstance(), hs),
116 false));
117 }
118
119 // Work around issue with jtreg summary reporting which truncates
120 // the String result of Object.toString to 30 characters, hence
121 // the first dummy argument
122 return cases.stream().map(tc -> new Object[]{tc.toString(), tc}).toArray(Object[][]::new);
123 }
124
125 @Test(dataProvider = "accessTestCaseProvider")
126 public <T> void testAccess(String desc, AccessTestCase<T> atc) throws Throwable {
127 T t = atc.get();
128 int iters = atc.requiresLoop() ? ITERS : 1;
129 for (int c = 0; c < iters; c++) {
130 atc.testAccess(t);
131 }
132 }
133
134
135 static void testInstanceField(VarHandleTestMethodHandleAccessByte recv, Handles hs) throws Throwable {
136 // Plain
370 assertEquals(o, (byte)0x01, "getAndBitwiseXorAcquire byte");
371 byte x = (byte) hs.get(TestAccessMode.GET).invokeExact(recv);
372 assertEquals(x, (byte)((byte)0x01 ^ (byte)0x23), "getAndBitwiseXorAcquire byte value");
373 }
374
375 {
376 hs.get(TestAccessMode.SET).invokeExact(recv, (byte)0x01);
377
378 byte o = (byte) hs.get(TestAccessMode.GET_AND_BITWISE_XOR_RELEASE).invokeExact(recv, (byte)0x23);
379 assertEquals(o, (byte)0x01, "getAndBitwiseXorRelease byte");
380 byte x = (byte) hs.get(TestAccessMode.GET).invokeExact(recv);
381 assertEquals(x, (byte)((byte)0x01 ^ (byte)0x23), "getAndBitwiseXorRelease byte value");
382 }
383 }
384
385 static void testInstanceFieldUnsupported(VarHandleTestMethodHandleAccessByte recv, Handles hs) throws Throwable {
386
387
388 }
389
390 static void testValueTypeField(Value recv, Handles hs) throws Throwable {
391 // Plain
392 {
393 byte x = (byte) hs.get(TestAccessMode.GET).invokeExact(recv);
394 assertEquals(x, (byte)0x01, "get byte value");
395 }
396 }
397
398 static void testValueTypeFieldUnsupported(Value recv, Handles hs) throws Throwable {
399 // Plain
400 for (TestAccessMode am : testAccessModesOfType(TestAccessType.SET)) {
401 checkUOE(am, () -> {
402 hs.get(am).invokeExact(recv, (byte)0x01);
403 });
404 }
405
406
407 }
408
409 static void testStaticField(Handles hs) throws Throwable {
410 // Plain
411 {
412 hs.get(TestAccessMode.SET).invokeExact((byte)0x01);
413 byte x = (byte) hs.get(TestAccessMode.GET).invokeExact();
414 assertEquals(x, (byte)0x01, "set byte value");
415 }
416
417
418 // Volatile
419 {
420 hs.get(TestAccessMode.SET_VOLATILE).invokeExact((byte)0x23);
421 byte x = (byte) hs.get(TestAccessMode.GET_VOLATILE).invokeExact();
422 assertEquals(x, (byte)0x23, "setVolatile byte value");
423 }
424
425 // Lazy
426 {
427 hs.get(TestAccessMode.SET_RELEASE).invokeExact((byte)0x01);
|