blob: 7ff96eb10ea9bb83eaf6831d221168f2ce7461a4 [file] [log] [blame]
Paul Sandoz9fb30a32016-03-24 11:21:21 +01001/*
2 * Copyright (c) 2015, 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=10 -Xint VarHandleTestAccessShort
27 * @run testng/othervm -Diters=20000 -XX:TieredStopAtLevel=1 VarHandleTestAccessShort
28 * @run testng/othervm -Diters=20000 VarHandleTestAccessShort
29 * @run testng/othervm -Diters=20000 -XX:-TieredCompilation VarHandleTestAccessShort
30 */
31
32import org.testng.annotations.BeforeClass;
33import org.testng.annotations.DataProvider;
34import org.testng.annotations.Test;
35
36import java.lang.invoke.MethodHandles;
37import java.lang.invoke.VarHandle;
38import java.util.ArrayList;
39import java.util.Arrays;
40import java.util.List;
41
42import static org.testng.Assert.*;
43
44public class VarHandleTestAccessShort extends VarHandleBaseTest {
Aleksey Shipileve6632062016-06-15 11:20:15 +030045 static final short static_final_v = (short)0x0123;
Paul Sandoz9fb30a32016-03-24 11:21:21 +010046
47 static short static_v;
48
Aleksey Shipileve6632062016-06-15 11:20:15 +030049 final short final_v = (short)0x0123;
Paul Sandoz9fb30a32016-03-24 11:21:21 +010050
51 short v;
52
53 VarHandle vhFinalField;
54
55 VarHandle vhField;
56
57 VarHandle vhStaticField;
58
59 VarHandle vhStaticFinalField;
60
61 VarHandle vhArray;
62
63 @BeforeClass
64 public void setup() throws Exception {
65 vhFinalField = MethodHandles.lookup().findVarHandle(
66 VarHandleTestAccessShort.class, "final_v", short.class);
67
68 vhField = MethodHandles.lookup().findVarHandle(
69 VarHandleTestAccessShort.class, "v", short.class);
70
71 vhStaticFinalField = MethodHandles.lookup().findStaticVarHandle(
72 VarHandleTestAccessShort.class, "static_final_v", short.class);
73
74 vhStaticField = MethodHandles.lookup().findStaticVarHandle(
75 VarHandleTestAccessShort.class, "static_v", short.class);
76
77 vhArray = MethodHandles.arrayElementVarHandle(short[].class);
78 }
79
80
81 @DataProvider
82 public Object[][] varHandlesProvider() throws Exception {
83 List<VarHandle> vhs = new ArrayList<>();
84 vhs.add(vhField);
85 vhs.add(vhStaticField);
86 vhs.add(vhArray);
87
88 return vhs.stream().map(tc -> new Object[]{tc}).toArray(Object[][]::new);
89 }
90
91 @Test(dataProvider = "varHandlesProvider")
92 public void testIsAccessModeSupported(VarHandle vh) {
Paul Sandoza7aff442016-04-13 15:05:48 +020093 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET));
94 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET));
95 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_VOLATILE));
96 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET_VOLATILE));
97 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_ACQUIRE));
98 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET_RELEASE));
99 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_OPAQUE));
100 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET_OPAQUE));
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100101
Aleksey Shipileve6632062016-06-15 11:20:15 +0300102 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_SET));
Paul Sandoz3f0273a2016-06-23 13:46:48 +0200103 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE));
Aleksey Shipileve6632062016-06-15 11:20:15 +0300104 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_ACQUIRE));
105 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_RELEASE));
Paul Sandoz3bd5ebe2016-09-01 13:56:13 -0700106 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_PLAIN));
Aleksey Shipileve6632062016-06-15 11:20:15 +0300107 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET));
Aleksey Shipileve6632062016-06-15 11:20:15 +0300108 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_ACQUIRE));
109 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_RELEASE));
110 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_SET));
Paul Sandoz82d48912016-09-01 10:16:57 -0700111 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_SET_ACQUIRE));
112 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_SET_RELEASE));
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100113
Aleksey Shipileve6632062016-06-15 11:20:15 +0300114 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_ADD));
Paul Sandoz82d48912016-09-01 10:16:57 -0700115 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_ADD_ACQUIRE));
116 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_ADD_RELEASE));
Paul Sandoz82d48912016-09-01 10:16:57 -0700117
118 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_OR));
119 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_OR_ACQUIRE));
120 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_OR_RELEASE));
121 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_AND));
122 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_AND_ACQUIRE));
123 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_AND_RELEASE));
124 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_XOR));
125 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_XOR_ACQUIRE));
126 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_XOR_RELEASE));
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100127 }
128
129
130 @DataProvider
131 public Object[][] typesProvider() throws Exception {
132 List<Object[]> types = new ArrayList<>();
133 types.add(new Object[] {vhField, Arrays.asList(VarHandleTestAccessShort.class)});
134 types.add(new Object[] {vhStaticField, Arrays.asList()});
135 types.add(new Object[] {vhArray, Arrays.asList(short[].class, int.class)});
136
137 return types.stream().toArray(Object[][]::new);
138 }
139
140 @Test(dataProvider = "typesProvider")
141 public void testTypes(VarHandle vh, List<Class<?>> pts) {
142 assertEquals(vh.varType(), short.class);
143
144 assertEquals(vh.coordinateTypes(), pts);
145
146 testTypes(vh);
147 }
148
149
150 @Test
151 public void testLookupInstanceToStatic() {
152 checkIAE("Lookup of static final field to instance final field", () -> {
153 MethodHandles.lookup().findStaticVarHandle(
154 VarHandleTestAccessShort.class, "final_v", short.class);
155 });
156
157 checkIAE("Lookup of static field to instance field", () -> {
158 MethodHandles.lookup().findStaticVarHandle(
159 VarHandleTestAccessShort.class, "v", short.class);
160 });
161 }
162
163 @Test
164 public void testLookupStaticToInstance() {
165 checkIAE("Lookup of instance final field to static final field", () -> {
166 MethodHandles.lookup().findVarHandle(
167 VarHandleTestAccessShort.class, "static_final_v", short.class);
168 });
169
170 checkIAE("Lookup of instance field to static field", () -> {
171 vhStaticField = MethodHandles.lookup().findVarHandle(
172 VarHandleTestAccessShort.class, "static_v", short.class);
173 });
174 }
175
176
177 @DataProvider
178 public Object[][] accessTestCaseProvider() throws Exception {
179 List<AccessTestCase<?>> cases = new ArrayList<>();
180
181 cases.add(new VarHandleAccessTestCase("Instance final field",
182 vhFinalField, vh -> testInstanceFinalField(this, vh)));
183 cases.add(new VarHandleAccessTestCase("Instance final field unsupported",
184 vhFinalField, vh -> testInstanceFinalFieldUnsupported(this, vh),
185 false));
186
187 cases.add(new VarHandleAccessTestCase("Static final field",
188 vhStaticFinalField, VarHandleTestAccessShort::testStaticFinalField));
189 cases.add(new VarHandleAccessTestCase("Static final field unsupported",
190 vhStaticFinalField, VarHandleTestAccessShort::testStaticFinalFieldUnsupported,
191 false));
192
193 cases.add(new VarHandleAccessTestCase("Instance field",
194 vhField, vh -> testInstanceField(this, vh)));
195 cases.add(new VarHandleAccessTestCase("Instance field unsupported",
196 vhField, vh -> testInstanceFieldUnsupported(this, vh),
197 false));
198
199 cases.add(new VarHandleAccessTestCase("Static field",
200 vhStaticField, VarHandleTestAccessShort::testStaticField));
201 cases.add(new VarHandleAccessTestCase("Static field unsupported",
202 vhStaticField, VarHandleTestAccessShort::testStaticFieldUnsupported,
203 false));
204
205 cases.add(new VarHandleAccessTestCase("Array",
206 vhArray, VarHandleTestAccessShort::testArray));
207 cases.add(new VarHandleAccessTestCase("Array unsupported",
208 vhArray, VarHandleTestAccessShort::testArrayUnsupported,
209 false));
210 cases.add(new VarHandleAccessTestCase("Array index out of bounds",
211 vhArray, VarHandleTestAccessShort::testArrayIndexOutOfBounds,
212 false));
213
214 // Work around issue with jtreg summary reporting which truncates
215 // the String result of Object.toString to 30 characters, hence
216 // the first dummy argument
217 return cases.stream().map(tc -> new Object[]{tc.toString(), tc}).toArray(Object[][]::new);
218 }
219
220 @Test(dataProvider = "accessTestCaseProvider")
221 public <T> void testAccess(String desc, AccessTestCase<T> atc) throws Throwable {
222 T t = atc.get();
223 int iters = atc.requiresLoop() ? ITERS : 1;
224 for (int c = 0; c < iters; c++) {
225 atc.testAccess(t);
226 }
227 }
228
229
230
231
232 static void testInstanceFinalField(VarHandleTestAccessShort recv, VarHandle vh) {
233 // Plain
234 {
235 short x = (short) vh.get(recv);
Aleksey Shipileve6632062016-06-15 11:20:15 +0300236 assertEquals(x, (short)0x0123, "get short value");
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100237 }
238
239
240 // Volatile
241 {
242 short x = (short) vh.getVolatile(recv);
Aleksey Shipileve6632062016-06-15 11:20:15 +0300243 assertEquals(x, (short)0x0123, "getVolatile short value");
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100244 }
245
246 // Lazy
247 {
248 short x = (short) vh.getAcquire(recv);
Aleksey Shipileve6632062016-06-15 11:20:15 +0300249 assertEquals(x, (short)0x0123, "getRelease short value");
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100250 }
251
252 // Opaque
253 {
254 short x = (short) vh.getOpaque(recv);
Aleksey Shipileve6632062016-06-15 11:20:15 +0300255 assertEquals(x, (short)0x0123, "getOpaque short value");
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100256 }
257 }
258
259 static void testInstanceFinalFieldUnsupported(VarHandleTestAccessShort recv, VarHandle vh) {
260 checkUOE(() -> {
Aleksey Shipileve6632062016-06-15 11:20:15 +0300261 vh.set(recv, (short)0x4567);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100262 });
263
264 checkUOE(() -> {
Aleksey Shipileve6632062016-06-15 11:20:15 +0300265 vh.setVolatile(recv, (short)0x4567);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100266 });
267
268 checkUOE(() -> {
Aleksey Shipileve6632062016-06-15 11:20:15 +0300269 vh.setRelease(recv, (short)0x4567);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100270 });
271
272 checkUOE(() -> {
Aleksey Shipileve6632062016-06-15 11:20:15 +0300273 vh.setOpaque(recv, (short)0x4567);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100274 });
275
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100276
Paul Sandoz82d48912016-09-01 10:16:57 -0700277
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100278 }
279
280
281 static void testStaticFinalField(VarHandle vh) {
282 // Plain
283 {
284 short x = (short) vh.get();
Aleksey Shipileve6632062016-06-15 11:20:15 +0300285 assertEquals(x, (short)0x0123, "get short value");
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100286 }
287
288
289 // Volatile
290 {
291 short x = (short) vh.getVolatile();
Aleksey Shipileve6632062016-06-15 11:20:15 +0300292 assertEquals(x, (short)0x0123, "getVolatile short value");
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100293 }
294
295 // Lazy
296 {
297 short x = (short) vh.getAcquire();
Aleksey Shipileve6632062016-06-15 11:20:15 +0300298 assertEquals(x, (short)0x0123, "getRelease short value");
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100299 }
300
301 // Opaque
302 {
303 short x = (short) vh.getOpaque();
Aleksey Shipileve6632062016-06-15 11:20:15 +0300304 assertEquals(x, (short)0x0123, "getOpaque short value");
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100305 }
306 }
307
308 static void testStaticFinalFieldUnsupported(VarHandle vh) {
309 checkUOE(() -> {
Aleksey Shipileve6632062016-06-15 11:20:15 +0300310 vh.set((short)0x4567);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100311 });
312
313 checkUOE(() -> {
Aleksey Shipileve6632062016-06-15 11:20:15 +0300314 vh.setVolatile((short)0x4567);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100315 });
316
317 checkUOE(() -> {
Aleksey Shipileve6632062016-06-15 11:20:15 +0300318 vh.setRelease((short)0x4567);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100319 });
320
321 checkUOE(() -> {
Aleksey Shipileve6632062016-06-15 11:20:15 +0300322 vh.setOpaque((short)0x4567);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100323 });
324
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100325
Paul Sandoz82d48912016-09-01 10:16:57 -0700326
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100327 }
328
329
330 static void testInstanceField(VarHandleTestAccessShort recv, VarHandle vh) {
331 // Plain
332 {
Aleksey Shipileve6632062016-06-15 11:20:15 +0300333 vh.set(recv, (short)0x0123);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100334 short x = (short) vh.get(recv);
Aleksey Shipileve6632062016-06-15 11:20:15 +0300335 assertEquals(x, (short)0x0123, "set short value");
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100336 }
337
338
339 // Volatile
340 {
Aleksey Shipileve6632062016-06-15 11:20:15 +0300341 vh.setVolatile(recv, (short)0x4567);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100342 short x = (short) vh.getVolatile(recv);
Aleksey Shipileve6632062016-06-15 11:20:15 +0300343 assertEquals(x, (short)0x4567, "setVolatile short value");
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100344 }
345
346 // Lazy
347 {
Aleksey Shipileve6632062016-06-15 11:20:15 +0300348 vh.setRelease(recv, (short)0x0123);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100349 short x = (short) vh.getAcquire(recv);
Aleksey Shipileve6632062016-06-15 11:20:15 +0300350 assertEquals(x, (short)0x0123, "setRelease short value");
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100351 }
352
353 // Opaque
354 {
Aleksey Shipileve6632062016-06-15 11:20:15 +0300355 vh.setOpaque(recv, (short)0x4567);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100356 short x = (short) vh.getOpaque(recv);
Aleksey Shipileve6632062016-06-15 11:20:15 +0300357 assertEquals(x, (short)0x4567, "setOpaque short value");
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100358 }
359
Aleksey Shipileve6632062016-06-15 11:20:15 +0300360 vh.set(recv, (short)0x0123);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100361
Aleksey Shipileve6632062016-06-15 11:20:15 +0300362 // Compare
363 {
364 boolean r = vh.compareAndSet(recv, (short)0x0123, (short)0x4567);
365 assertEquals(r, true, "success compareAndSet short");
366 short x = (short) vh.get(recv);
367 assertEquals(x, (short)0x4567, "success compareAndSet short value");
368 }
369
370 {
371 boolean r = vh.compareAndSet(recv, (short)0x0123, (short)0x89AB);
372 assertEquals(r, false, "failing compareAndSet short");
373 short x = (short) vh.get(recv);
374 assertEquals(x, (short)0x4567, "failing compareAndSet short value");
375 }
376
377 {
Paul Sandoz3f0273a2016-06-23 13:46:48 +0200378 short r = (short) vh.compareAndExchange(recv, (short)0x4567, (short)0x0123);
379 assertEquals(r, (short)0x4567, "success compareAndExchange short");
Aleksey Shipileve6632062016-06-15 11:20:15 +0300380 short x = (short) vh.get(recv);
Paul Sandoz3f0273a2016-06-23 13:46:48 +0200381 assertEquals(x, (short)0x0123, "success compareAndExchange short value");
Aleksey Shipileve6632062016-06-15 11:20:15 +0300382 }
383
384 {
Paul Sandoz3f0273a2016-06-23 13:46:48 +0200385 short r = (short) vh.compareAndExchange(recv, (short)0x4567, (short)0x89AB);
386 assertEquals(r, (short)0x0123, "failing compareAndExchange short");
Aleksey Shipileve6632062016-06-15 11:20:15 +0300387 short x = (short) vh.get(recv);
Paul Sandoz3f0273a2016-06-23 13:46:48 +0200388 assertEquals(x, (short)0x0123, "failing compareAndExchange short value");
Aleksey Shipileve6632062016-06-15 11:20:15 +0300389 }
390
391 {
392 short r = (short) vh.compareAndExchangeAcquire(recv, (short)0x0123, (short)0x4567);
393 assertEquals(r, (short)0x0123, "success compareAndExchangeAcquire short");
394 short x = (short) vh.get(recv);
395 assertEquals(x, (short)0x4567, "success compareAndExchangeAcquire short value");
396 }
397
398 {
399 short r = (short) vh.compareAndExchangeAcquire(recv, (short)0x0123, (short)0x89AB);
400 assertEquals(r, (short)0x4567, "failing compareAndExchangeAcquire short");
401 short x = (short) vh.get(recv);
402 assertEquals(x, (short)0x4567, "failing compareAndExchangeAcquire short value");
403 }
404
405 {
406 short r = (short) vh.compareAndExchangeRelease(recv, (short)0x4567, (short)0x0123);
407 assertEquals(r, (short)0x4567, "success compareAndExchangeRelease short");
408 short x = (short) vh.get(recv);
409 assertEquals(x, (short)0x0123, "success compareAndExchangeRelease short value");
410 }
411
412 {
413 short r = (short) vh.compareAndExchangeRelease(recv, (short)0x4567, (short)0x89AB);
414 assertEquals(r, (short)0x0123, "failing compareAndExchangeRelease short");
415 short x = (short) vh.get(recv);
416 assertEquals(x, (short)0x0123, "failing compareAndExchangeRelease short value");
417 }
418
419 {
420 boolean success = false;
421 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
Paul Sandoz3bd5ebe2016-09-01 13:56:13 -0700422 success = vh.weakCompareAndSetPlain(recv, (short)0x0123, (short)0x4567);
Aleksey Shipileve6632062016-06-15 11:20:15 +0300423 }
Paul Sandoz3bd5ebe2016-09-01 13:56:13 -0700424 assertEquals(success, true, "weakCompareAndSetPlain short");
Aleksey Shipileve6632062016-06-15 11:20:15 +0300425 short x = (short) vh.get(recv);
Paul Sandoz3bd5ebe2016-09-01 13:56:13 -0700426 assertEquals(x, (short)0x4567, "weakCompareAndSetPlain short value");
Aleksey Shipileve6632062016-06-15 11:20:15 +0300427 }
428
429 {
430 boolean success = false;
431 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
432 success = vh.weakCompareAndSetAcquire(recv, (short)0x4567, (short)0x0123);
433 }
434 assertEquals(success, true, "weakCompareAndSetAcquire short");
435 short x = (short) vh.get(recv);
436 assertEquals(x, (short)0x0123, "weakCompareAndSetAcquire short");
437 }
438
439 {
440 boolean success = false;
441 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
442 success = vh.weakCompareAndSetRelease(recv, (short)0x0123, (short)0x4567);
443 }
444 assertEquals(success, true, "weakCompareAndSetRelease short");
445 short x = (short) vh.get(recv);
446 assertEquals(x, (short)0x4567, "weakCompareAndSetRelease short");
447 }
448
449 {
450 boolean success = false;
451 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
Paul Sandoz3bd5ebe2016-09-01 13:56:13 -0700452 success = vh.weakCompareAndSet(recv, (short)0x4567, (short)0x0123);
Aleksey Shipileve6632062016-06-15 11:20:15 +0300453 }
Paul Sandoz3bd5ebe2016-09-01 13:56:13 -0700454 assertEquals(success, true, "weakCompareAndSet short");
Aleksey Shipileve6632062016-06-15 11:20:15 +0300455 short x = (short) vh.get(recv);
Paul Sandoz3bd5ebe2016-09-01 13:56:13 -0700456 assertEquals(x, (short)0x0123, "weakCompareAndSet short value");
Aleksey Shipileve6632062016-06-15 11:20:15 +0300457 }
458
459 // Compare set and get
460 {
Paul Sandoz82d48912016-09-01 10:16:57 -0700461 vh.set(recv, (short)0x0123);
462
Aleksey Shipileve6632062016-06-15 11:20:15 +0300463 short o = (short) vh.getAndSet(recv, (short)0x4567);
464 assertEquals(o, (short)0x0123, "getAndSet short");
465 short x = (short) vh.get(recv);
466 assertEquals(x, (short)0x4567, "getAndSet short value");
467 }
468
Paul Sandoz82d48912016-09-01 10:16:57 -0700469 {
470 vh.set(recv, (short)0x0123);
471
472 short o = (short) vh.getAndSetAcquire(recv, (short)0x4567);
473 assertEquals(o, (short)0x0123, "getAndSetAcquire short");
474 short x = (short) vh.get(recv);
475 assertEquals(x, (short)0x4567, "getAndSetAcquire short value");
476 }
477
478 {
479 vh.set(recv, (short)0x0123);
480
481 short o = (short) vh.getAndSetRelease(recv, (short)0x4567);
482 assertEquals(o, (short)0x0123, "getAndSetRelease short");
483 short x = (short) vh.get(recv);
484 assertEquals(x, (short)0x4567, "getAndSetRelease short value");
485 }
Aleksey Shipileve6632062016-06-15 11:20:15 +0300486
487 // get and add, add and get
488 {
Paul Sandoz82d48912016-09-01 10:16:57 -0700489 vh.set(recv, (short)0x0123);
490
Paul Sandozc073edc2016-09-01 10:17:01 -0700491 short o = (short) vh.getAndAdd(recv, (short)0x4567);
Aleksey Shipileve6632062016-06-15 11:20:15 +0300492 assertEquals(o, (short)0x0123, "getAndAdd short");
Paul Sandozc073edc2016-09-01 10:17:01 -0700493 short x = (short) vh.get(recv);
494 assertEquals(x, (short)((short)0x0123 + (short)0x4567), "getAndAdd short value");
Aleksey Shipileve6632062016-06-15 11:20:15 +0300495 }
Paul Sandoz82d48912016-09-01 10:16:57 -0700496
497 {
498 vh.set(recv, (short)0x0123);
499
500 short o = (short) vh.getAndAddAcquire(recv, (short)0x4567);
501 assertEquals(o, (short)0x0123, "getAndAddAcquire short");
502 short x = (short) vh.get(recv);
503 assertEquals(x, (short)((short)0x0123 + (short)0x4567), "getAndAddAcquire short value");
504 }
505
506 {
507 vh.set(recv, (short)0x0123);
508
509 short o = (short) vh.getAndAddRelease(recv, (short)0x4567);
510 assertEquals(o, (short)0x0123, "getAndAddReleaseshort");
511 short x = (short) vh.get(recv);
512 assertEquals(x, (short)((short)0x0123 + (short)0x4567), "getAndAddRelease short value");
513 }
514
515 // get and bitwise or
516 {
517 vh.set(recv, (short)0x0123);
518
519 short o = (short) vh.getAndBitwiseOr(recv, (short)0x4567);
520 assertEquals(o, (short)0x0123, "getAndBitwiseOr short");
521 short x = (short) vh.get(recv);
522 assertEquals(x, (short)((short)0x0123 | (short)0x4567), "getAndBitwiseOr short value");
523 }
524
525 {
526 vh.set(recv, (short)0x0123);
527
528 short o = (short) vh.getAndBitwiseOrAcquire(recv, (short)0x4567);
529 assertEquals(o, (short)0x0123, "getAndBitwiseOrAcquire short");
530 short x = (short) vh.get(recv);
531 assertEquals(x, (short)((short)0x0123 | (short)0x4567), "getAndBitwiseOrAcquire short value");
532 }
533
534 {
535 vh.set(recv, (short)0x0123);
536
537 short o = (short) vh.getAndBitwiseOrRelease(recv, (short)0x4567);
538 assertEquals(o, (short)0x0123, "getAndBitwiseOrRelease short");
539 short x = (short) vh.get(recv);
540 assertEquals(x, (short)((short)0x0123 | (short)0x4567), "getAndBitwiseOrRelease short value");
541 }
542
543 // get and bitwise and
544 {
545 vh.set(recv, (short)0x0123);
546
547 short o = (short) vh.getAndBitwiseAnd(recv, (short)0x4567);
548 assertEquals(o, (short)0x0123, "getAndBitwiseAnd short");
549 short x = (short) vh.get(recv);
550 assertEquals(x, (short)((short)0x0123 & (short)0x4567), "getAndBitwiseAnd short value");
551 }
552
553 {
554 vh.set(recv, (short)0x0123);
555
556 short o = (short) vh.getAndBitwiseAndAcquire(recv, (short)0x4567);
557 assertEquals(o, (short)0x0123, "getAndBitwiseAndAcquire short");
558 short x = (short) vh.get(recv);
559 assertEquals(x, (short)((short)0x0123 & (short)0x4567), "getAndBitwiseAndAcquire short value");
560 }
561
562 {
563 vh.set(recv, (short)0x0123);
564
565 short o = (short) vh.getAndBitwiseAndRelease(recv, (short)0x4567);
566 assertEquals(o, (short)0x0123, "getAndBitwiseAndRelease short");
567 short x = (short) vh.get(recv);
568 assertEquals(x, (short)((short)0x0123 & (short)0x4567), "getAndBitwiseAndRelease short value");
569 }
570
571 // get and bitwise xor
572 {
573 vh.set(recv, (short)0x0123);
574
575 short o = (short) vh.getAndBitwiseXor(recv, (short)0x4567);
576 assertEquals(o, (short)0x0123, "getAndBitwiseXor short");
577 short x = (short) vh.get(recv);
578 assertEquals(x, (short)((short)0x0123 ^ (short)0x4567), "getAndBitwiseXor short value");
579 }
580
581 {
582 vh.set(recv, (short)0x0123);
583
584 short o = (short) vh.getAndBitwiseXorAcquire(recv, (short)0x4567);
585 assertEquals(o, (short)0x0123, "getAndBitwiseXorAcquire short");
586 short x = (short) vh.get(recv);
587 assertEquals(x, (short)((short)0x0123 ^ (short)0x4567), "getAndBitwiseXorAcquire short value");
588 }
589
590 {
591 vh.set(recv, (short)0x0123);
592
593 short o = (short) vh.getAndBitwiseXorRelease(recv, (short)0x4567);
594 assertEquals(o, (short)0x0123, "getAndBitwiseXorRelease short");
595 short x = (short) vh.get(recv);
596 assertEquals(x, (short)((short)0x0123 ^ (short)0x4567), "getAndBitwiseXorRelease short value");
597 }
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100598 }
599
600 static void testInstanceFieldUnsupported(VarHandleTestAccessShort recv, VarHandle vh) {
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100601
Paul Sandoz82d48912016-09-01 10:16:57 -0700602
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100603 }
604
605
606 static void testStaticField(VarHandle vh) {
607 // Plain
608 {
Aleksey Shipileve6632062016-06-15 11:20:15 +0300609 vh.set((short)0x0123);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100610 short x = (short) vh.get();
Aleksey Shipileve6632062016-06-15 11:20:15 +0300611 assertEquals(x, (short)0x0123, "set short value");
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100612 }
613
614
615 // Volatile
616 {
Aleksey Shipileve6632062016-06-15 11:20:15 +0300617 vh.setVolatile((short)0x4567);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100618 short x = (short) vh.getVolatile();
Aleksey Shipileve6632062016-06-15 11:20:15 +0300619 assertEquals(x, (short)0x4567, "setVolatile short value");
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100620 }
621
622 // Lazy
623 {
Aleksey Shipileve6632062016-06-15 11:20:15 +0300624 vh.setRelease((short)0x0123);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100625 short x = (short) vh.getAcquire();
Aleksey Shipileve6632062016-06-15 11:20:15 +0300626 assertEquals(x, (short)0x0123, "setRelease short value");
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100627 }
628
629 // Opaque
630 {
Aleksey Shipileve6632062016-06-15 11:20:15 +0300631 vh.setOpaque((short)0x4567);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100632 short x = (short) vh.getOpaque();
Aleksey Shipileve6632062016-06-15 11:20:15 +0300633 assertEquals(x, (short)0x4567, "setOpaque short value");
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100634 }
635
Aleksey Shipileve6632062016-06-15 11:20:15 +0300636 vh.set((short)0x0123);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100637
Aleksey Shipileve6632062016-06-15 11:20:15 +0300638 // Compare
639 {
640 boolean r = vh.compareAndSet((short)0x0123, (short)0x4567);
641 assertEquals(r, true, "success compareAndSet short");
642 short x = (short) vh.get();
643 assertEquals(x, (short)0x4567, "success compareAndSet short value");
644 }
645
646 {
647 boolean r = vh.compareAndSet((short)0x0123, (short)0x89AB);
648 assertEquals(r, false, "failing compareAndSet short");
649 short x = (short) vh.get();
650 assertEquals(x, (short)0x4567, "failing compareAndSet short value");
651 }
652
653 {
Paul Sandoz3f0273a2016-06-23 13:46:48 +0200654 short r = (short) vh.compareAndExchange((short)0x4567, (short)0x0123);
655 assertEquals(r, (short)0x4567, "success compareAndExchange short");
Aleksey Shipileve6632062016-06-15 11:20:15 +0300656 short x = (short) vh.get();
Paul Sandoz3f0273a2016-06-23 13:46:48 +0200657 assertEquals(x, (short)0x0123, "success compareAndExchange short value");
Aleksey Shipileve6632062016-06-15 11:20:15 +0300658 }
659
660 {
Paul Sandoz3f0273a2016-06-23 13:46:48 +0200661 short r = (short) vh.compareAndExchange((short)0x4567, (short)0x89AB);
662 assertEquals(r, (short)0x0123, "failing compareAndExchange short");
Aleksey Shipileve6632062016-06-15 11:20:15 +0300663 short x = (short) vh.get();
Paul Sandoz3f0273a2016-06-23 13:46:48 +0200664 assertEquals(x, (short)0x0123, "failing compareAndExchange short value");
Aleksey Shipileve6632062016-06-15 11:20:15 +0300665 }
666
667 {
668 short r = (short) vh.compareAndExchangeAcquire((short)0x0123, (short)0x4567);
669 assertEquals(r, (short)0x0123, "success compareAndExchangeAcquire short");
670 short x = (short) vh.get();
671 assertEquals(x, (short)0x4567, "success compareAndExchangeAcquire short value");
672 }
673
674 {
675 short r = (short) vh.compareAndExchangeAcquire((short)0x0123, (short)0x89AB);
676 assertEquals(r, (short)0x4567, "failing compareAndExchangeAcquire short");
677 short x = (short) vh.get();
678 assertEquals(x, (short)0x4567, "failing compareAndExchangeAcquire short value");
679 }
680
681 {
682 short r = (short) vh.compareAndExchangeRelease((short)0x4567, (short)0x0123);
683 assertEquals(r, (short)0x4567, "success compareAndExchangeRelease short");
684 short x = (short) vh.get();
685 assertEquals(x, (short)0x0123, "success compareAndExchangeRelease short value");
686 }
687
688 {
689 short r = (short) vh.compareAndExchangeRelease((short)0x4567, (short)0x89AB);
690 assertEquals(r, (short)0x0123, "failing compareAndExchangeRelease short");
691 short x = (short) vh.get();
692 assertEquals(x, (short)0x0123, "failing compareAndExchangeRelease short value");
693 }
694
695 {
696 boolean success = false;
697 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
Paul Sandoz3bd5ebe2016-09-01 13:56:13 -0700698 success = vh.weakCompareAndSetPlain((short)0x0123, (short)0x4567);
Aleksey Shipileve6632062016-06-15 11:20:15 +0300699 }
Paul Sandoz3bd5ebe2016-09-01 13:56:13 -0700700 assertEquals(success, true, "weakCompareAndSetPlain short");
Aleksey Shipileve6632062016-06-15 11:20:15 +0300701 short x = (short) vh.get();
Paul Sandoz3bd5ebe2016-09-01 13:56:13 -0700702 assertEquals(x, (short)0x4567, "weakCompareAndSetPlain short value");
Aleksey Shipileve6632062016-06-15 11:20:15 +0300703 }
704
705 {
706 boolean success = false;
707 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
708 success = vh.weakCompareAndSetAcquire((short)0x4567, (short)0x0123);
709 }
710 assertEquals(success, true, "weakCompareAndSetAcquire short");
711 short x = (short) vh.get();
712 assertEquals(x, (short)0x0123, "weakCompareAndSetAcquire short");
713 }
714
715 {
716 boolean success = false;
717 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
718 success = vh.weakCompareAndSetRelease((short)0x0123, (short)0x4567);
719 }
720 assertEquals(success, true, "weakCompareAndSetRelease short");
721 short x = (short) vh.get();
722 assertEquals(x, (short)0x4567, "weakCompareAndSetRelease short");
723 }
724
725 {
726 boolean success = false;
727 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
Paul Sandoz3bd5ebe2016-09-01 13:56:13 -0700728 success = vh.weakCompareAndSet((short)0x4567, (short)0x0123);
Aleksey Shipileve6632062016-06-15 11:20:15 +0300729 }
Paul Sandoz3bd5ebe2016-09-01 13:56:13 -0700730 assertEquals(success, true, "weakCompareAndSet short");
Aleksey Shipileve6632062016-06-15 11:20:15 +0300731 short x = (short) vh.get();
Paul Sandoz3bd5ebe2016-09-01 13:56:13 -0700732 assertEquals(x, (short)0x0123, "weakCompareAndSet short");
Aleksey Shipileve6632062016-06-15 11:20:15 +0300733 }
734
735 // Compare set and get
736 {
Paul Sandoz82d48912016-09-01 10:16:57 -0700737 vh.set((short)0x0123);
738
Aleksey Shipileve6632062016-06-15 11:20:15 +0300739 short o = (short) vh.getAndSet((short)0x4567);
740 assertEquals(o, (short)0x0123, "getAndSet short");
741 short x = (short) vh.get();
742 assertEquals(x, (short)0x4567, "getAndSet short value");
743 }
744
Paul Sandoz82d48912016-09-01 10:16:57 -0700745 {
746 vh.set((short)0x0123);
747
748 short o = (short) vh.getAndSetAcquire((short)0x4567);
749 assertEquals(o, (short)0x0123, "getAndSetAcquire short");
750 short x = (short) vh.get();
751 assertEquals(x, (short)0x4567, "getAndSetAcquire short value");
752 }
753
754 {
755 vh.set((short)0x0123);
756
757 short o = (short) vh.getAndSetRelease((short)0x4567);
758 assertEquals(o, (short)0x0123, "getAndSetRelease short");
759 short x = (short) vh.get();
760 assertEquals(x, (short)0x4567, "getAndSetRelease short value");
761 }
Aleksey Shipileve6632062016-06-15 11:20:15 +0300762
763 // get and add, add and get
764 {
Paul Sandoz82d48912016-09-01 10:16:57 -0700765 vh.set((short)0x0123);
766
Paul Sandozc073edc2016-09-01 10:17:01 -0700767 short o = (short) vh.getAndAdd((short)0x4567);
Aleksey Shipileve6632062016-06-15 11:20:15 +0300768 assertEquals(o, (short)0x0123, "getAndAdd short");
Paul Sandozc073edc2016-09-01 10:17:01 -0700769 short x = (short) vh.get();
770 assertEquals(x, (short)((short)0x0123 + (short)0x4567), "getAndAdd short value");
Aleksey Shipileve6632062016-06-15 11:20:15 +0300771 }
Paul Sandoz82d48912016-09-01 10:16:57 -0700772
773 {
774 vh.set((short)0x0123);
775
776 short o = (short) vh.getAndAddAcquire((short)0x4567);
777 assertEquals(o, (short)0x0123, "getAndAddAcquire short");
778 short x = (short) vh.get();
779 assertEquals(x, (short)((short)0x0123 + (short)0x4567), "getAndAddAcquire short value");
780 }
781
782 {
783 vh.set((short)0x0123);
784
785 short o = (short) vh.getAndAddRelease((short)0x4567);
786 assertEquals(o, (short)0x0123, "getAndAddReleaseshort");
787 short x = (short) vh.get();
788 assertEquals(x, (short)((short)0x0123 + (short)0x4567), "getAndAddRelease short value");
789 }
790
791 // get and bitwise or
792 {
793 vh.set((short)0x0123);
794
795 short o = (short) vh.getAndBitwiseOr((short)0x4567);
796 assertEquals(o, (short)0x0123, "getAndBitwiseOr short");
797 short x = (short) vh.get();
798 assertEquals(x, (short)((short)0x0123 | (short)0x4567), "getAndBitwiseOr short value");
799 }
800
801 {
802 vh.set((short)0x0123);
803
804 short o = (short) vh.getAndBitwiseOrAcquire((short)0x4567);
805 assertEquals(o, (short)0x0123, "getAndBitwiseOrAcquire short");
806 short x = (short) vh.get();
807 assertEquals(x, (short)((short)0x0123 | (short)0x4567), "getAndBitwiseOrAcquire short value");
808 }
809
810 {
811 vh.set((short)0x0123);
812
813 short o = (short) vh.getAndBitwiseOrRelease((short)0x4567);
814 assertEquals(o, (short)0x0123, "getAndBitwiseOrRelease short");
815 short x = (short) vh.get();
816 assertEquals(x, (short)((short)0x0123 | (short)0x4567), "getAndBitwiseOrRelease short value");
817 }
818
819 // get and bitwise and
820 {
821 vh.set((short)0x0123);
822
823 short o = (short) vh.getAndBitwiseAnd((short)0x4567);
824 assertEquals(o, (short)0x0123, "getAndBitwiseAnd short");
825 short x = (short) vh.get();
826 assertEquals(x, (short)((short)0x0123 & (short)0x4567), "getAndBitwiseAnd short value");
827 }
828
829 {
830 vh.set((short)0x0123);
831
832 short o = (short) vh.getAndBitwiseAndAcquire((short)0x4567);
833 assertEquals(o, (short)0x0123, "getAndBitwiseAndAcquire short");
834 short x = (short) vh.get();
835 assertEquals(x, (short)((short)0x0123 & (short)0x4567), "getAndBitwiseAndAcquire short value");
836 }
837
838 {
839 vh.set((short)0x0123);
840
841 short o = (short) vh.getAndBitwiseAndRelease((short)0x4567);
842 assertEquals(o, (short)0x0123, "getAndBitwiseAndRelease short");
843 short x = (short) vh.get();
844 assertEquals(x, (short)((short)0x0123 & (short)0x4567), "getAndBitwiseAndRelease short value");
845 }
846
847 // get and bitwise xor
848 {
849 vh.set((short)0x0123);
850
851 short o = (short) vh.getAndBitwiseXor((short)0x4567);
852 assertEquals(o, (short)0x0123, "getAndBitwiseXor short");
853 short x = (short) vh.get();
854 assertEquals(x, (short)((short)0x0123 ^ (short)0x4567), "getAndBitwiseXor short value");
855 }
856
857 {
858 vh.set((short)0x0123);
859
860 short o = (short) vh.getAndBitwiseXorAcquire((short)0x4567);
861 assertEquals(o, (short)0x0123, "getAndBitwiseXorAcquire short");
862 short x = (short) vh.get();
863 assertEquals(x, (short)((short)0x0123 ^ (short)0x4567), "getAndBitwiseXorAcquire short value");
864 }
865
866 {
867 vh.set((short)0x0123);
868
869 short o = (short) vh.getAndBitwiseXorRelease((short)0x4567);
870 assertEquals(o, (short)0x0123, "getAndBitwiseXorRelease short");
871 short x = (short) vh.get();
872 assertEquals(x, (short)((short)0x0123 ^ (short)0x4567), "getAndBitwiseXorRelease short value");
873 }
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100874 }
875
876 static void testStaticFieldUnsupported(VarHandle vh) {
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100877
Paul Sandoz82d48912016-09-01 10:16:57 -0700878
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100879 }
880
881
882 static void testArray(VarHandle vh) {
883 short[] array = new short[10];
884
885 for (int i = 0; i < array.length; i++) {
886 // Plain
887 {
Aleksey Shipileve6632062016-06-15 11:20:15 +0300888 vh.set(array, i, (short)0x0123);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100889 short x = (short) vh.get(array, i);
Aleksey Shipileve6632062016-06-15 11:20:15 +0300890 assertEquals(x, (short)0x0123, "get short value");
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100891 }
892
893
894 // Volatile
895 {
Aleksey Shipileve6632062016-06-15 11:20:15 +0300896 vh.setVolatile(array, i, (short)0x4567);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100897 short x = (short) vh.getVolatile(array, i);
Aleksey Shipileve6632062016-06-15 11:20:15 +0300898 assertEquals(x, (short)0x4567, "setVolatile short value");
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100899 }
900
901 // Lazy
902 {
Aleksey Shipileve6632062016-06-15 11:20:15 +0300903 vh.setRelease(array, i, (short)0x0123);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100904 short x = (short) vh.getAcquire(array, i);
Aleksey Shipileve6632062016-06-15 11:20:15 +0300905 assertEquals(x, (short)0x0123, "setRelease short value");
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100906 }
907
908 // Opaque
909 {
Aleksey Shipileve6632062016-06-15 11:20:15 +0300910 vh.setOpaque(array, i, (short)0x4567);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100911 short x = (short) vh.getOpaque(array, i);
Aleksey Shipileve6632062016-06-15 11:20:15 +0300912 assertEquals(x, (short)0x4567, "setOpaque short value");
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100913 }
914
Aleksey Shipileve6632062016-06-15 11:20:15 +0300915 vh.set(array, i, (short)0x0123);
Paul Sandoz9fb30a32016-03-24 11:21:21 +0100916
Aleksey Shipileve6632062016-06-15 11:20:15 +0300917 // Compare
918 {
919 boolean r = vh.compareAndSet(array, i, (short)0x0123, (short)0x4567);
920 assertEquals(r, true, "success compareAndSet short");
921 short x = (short) vh.get(array, i);
922 assertEquals(x, (short)0x4567, "success compareAndSet short value");
923 }
924
925 {
926 boolean r = vh.compareAndSet(array, i, (short)0x0123, (short)0x89AB);
927 assertEquals(r, false, "failing compareAndSet short");
928 short x = (short) vh.get(array, i);
929 assertEquals(x, (short)0x4567, "failing compareAndSet short value");
930 }
931
932 {
Paul Sandoz3f0273a2016-06-23 13:46:48 +0200933 short r = (short) vh.compareAndExchange(array, i, (short)0x4567, (short)0x0123);
934 assertEquals(r, (short)0x4567, "success compareAndExchange short");
Aleksey Shipileve6632062016-06-15 11:20:15 +0300935 short x = (short) vh.get(array, i);
Paul Sandoz3f0273a2016-06-23 13:46:48 +0200936 assertEquals(x, (short)0x0123, "success compareAndExchange short value");
Aleksey Shipileve6632062016-06-15 11:20:15 +0300937 }
938
939 {
Paul Sandoz3f0273a2016-06-23 13:46:48 +0200940 short r = (short) vh.compareAndExchange(array, i, (short)0x4567, (short)0x89AB);
941 assertEquals(r, (short)0x0123, "failing compareAndExchange short");
Aleksey Shipileve6632062016-06-15 11:20:15 +0300942 short x = (short) vh.get(array, i);
Paul Sandoz3f0273a2016-06-23 13:46:48 +0200943 assertEquals(x, (short)0x0123, "failing compareAndExchange short value");
Aleksey Shipileve6632062016-06-15 11:20:15 +0300944 }
945
946 {
947 short r = (short) vh.compareAndExchangeAcquire(array, i, (short)0x0123, (short)0x4567);
948 assertEquals(r, (short)0x0123, "success compareAndExchangeAcquire short");
949 short x = (short) vh.get(array, i);
950 assertEquals(x, (short)0x4567, "success compareAndExchangeAcquire short value");
951 }
952
953 {
954 short r = (short) vh.compareAndExchangeAcquire(array, i, (short)0x0123, (short)0x89AB);
955 assertEquals(r, (short)0x4567, "failing compareAndExchangeAcquire short");
956 short x = (short) vh.get(array, i);
957 assertEquals(x, (short)0x4567, "failing compareAndExchangeAcquire short value");
958 }
959
960 {
961 short r = (short) vh.compareAndExchangeRelease(array, i, (short)0x4567, (short)0x0123);
962 assertEquals(r, (short)0x4567, "success compareAndExchangeRelease short");
963 short x = (short) vh.get(array, i);
964 assertEquals(x, (short)0x0123, "success compareAndExchangeRelease short value");
965 }
966
967 {
968 short r = (short) vh.compareAndExchangeRelease(array, i, (short)0x4567, (short)0x89AB);
969 assertEquals(r, (short)0x0123, "failing compareAndExchangeRelease short");
970 short x = (short) vh.get(array, i);
971 assertEquals(x, (short)0x0123, "failing compareAndExchangeRelease short value");
972 }
973
974 {
975 boolean success = false;
976 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
Paul Sandoz3bd5ebe2016-09-01 13:56:13 -0700977 success = vh.weakCompareAndSetPlain(array, i, (short)0x0123, (short)0x4567);
Aleksey Shipileve6632062016-06-15 11:20:15 +0300978 }
Paul Sandoz3bd5ebe2016-09-01 13:56:13 -0700979 assertEquals(success, true, "weakCompareAndSetPlain short");
Aleksey Shipileve6632062016-06-15 11:20:15 +0300980 short x = (short) vh.get(array, i);
Paul Sandoz3bd5ebe2016-09-01 13:56:13 -0700981 assertEquals(x, (short)0x4567, "weakCompareAndSetPlain short value");
Aleksey Shipileve6632062016-06-15 11:20:15 +0300982 }
983
984 {
985 boolean success = false;
986 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
987 success = vh.weakCompareAndSetAcquire(array, i, (short)0x4567, (short)0x0123);
988 }
989 assertEquals(success, true, "weakCompareAndSetAcquire short");
990 short x = (short) vh.get(array, i);
991 assertEquals(x, (short)0x0123, "weakCompareAndSetAcquire short");
992 }
993
994 {
995 boolean success = false;
996 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
997 success = vh.weakCompareAndSetRelease(array, i, (short)0x0123, (short)0x4567);
998 }
999 assertEquals(success, true, "weakCompareAndSetRelease short");
1000 short x = (short) vh.get(array, i);
1001 assertEquals(x, (short)0x4567, "weakCompareAndSetRelease short");
1002 }
1003
1004 {
1005 boolean success = false;
1006 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
Paul Sandoz3bd5ebe2016-09-01 13:56:13 -07001007 success = vh.weakCompareAndSet(array, i, (short)0x4567, (short)0x0123);
Aleksey Shipileve6632062016-06-15 11:20:15 +03001008 }
Paul Sandoz3bd5ebe2016-09-01 13:56:13 -07001009 assertEquals(success, true, "weakCompareAndSet short");
Aleksey Shipileve6632062016-06-15 11:20:15 +03001010 short x = (short) vh.get(array, i);
Paul Sandoz3bd5ebe2016-09-01 13:56:13 -07001011 assertEquals(x, (short)0x0123, "weakCompareAndSet short");
Aleksey Shipileve6632062016-06-15 11:20:15 +03001012 }
1013
1014 // Compare set and get
1015 {
Paul Sandoz82d48912016-09-01 10:16:57 -07001016 vh.set(array, i, (short)0x0123);
1017
Aleksey Shipileve6632062016-06-15 11:20:15 +03001018 short o = (short) vh.getAndSet(array, i, (short)0x4567);
1019 assertEquals(o, (short)0x0123, "getAndSet short");
1020 short x = (short) vh.get(array, i);
1021 assertEquals(x, (short)0x4567, "getAndSet short value");
1022 }
1023
Paul Sandoz82d48912016-09-01 10:16:57 -07001024 {
1025 vh.set(array, i, (short)0x0123);
1026
1027 short o = (short) vh.getAndSetAcquire(array, i, (short)0x4567);
1028 assertEquals(o, (short)0x0123, "getAndSetAcquire short");
1029 short x = (short) vh.get(array, i);
1030 assertEquals(x, (short)0x4567, "getAndSetAcquire short value");
1031 }
1032
1033 {
1034 vh.set(array, i, (short)0x0123);
1035
1036 short o = (short) vh.getAndSetRelease(array, i, (short)0x4567);
1037 assertEquals(o, (short)0x0123, "getAndSetRelease short");
1038 short x = (short) vh.get(array, i);
1039 assertEquals(x, (short)0x4567, "getAndSetRelease short value");
1040 }
Aleksey Shipileve6632062016-06-15 11:20:15 +03001041
1042 // get and add, add and get
1043 {
Paul Sandoz82d48912016-09-01 10:16:57 -07001044 vh.set(array, i, (short)0x0123);
1045
Paul Sandozc073edc2016-09-01 10:17:01 -07001046 short o = (short) vh.getAndAdd(array, i, (short)0x4567);
Aleksey Shipileve6632062016-06-15 11:20:15 +03001047 assertEquals(o, (short)0x0123, "getAndAdd short");
Paul Sandozc073edc2016-09-01 10:17:01 -07001048 short x = (short) vh.get(array, i);
1049 assertEquals(x, (short)((short)0x0123 + (short)0x4567), "getAndAdd short value");
Aleksey Shipileve6632062016-06-15 11:20:15 +03001050 }
Paul Sandoz82d48912016-09-01 10:16:57 -07001051
1052 {
1053 vh.set(array, i, (short)0x0123);
1054
1055 short o = (short) vh.getAndAddAcquire(array, i, (short)0x4567);
1056 assertEquals(o, (short)0x0123, "getAndAddAcquire short");
1057 short x = (short) vh.get(array, i);
1058 assertEquals(x, (short)((short)0x0123 + (short)0x4567), "getAndAddAcquire short value");
1059 }
1060
1061 {
1062 vh.set(array, i, (short)0x0123);
1063
1064 short o = (short) vh.getAndAddRelease(array, i, (short)0x4567);
1065 assertEquals(o, (short)0x0123, "getAndAddReleaseshort");
1066 short x = (short) vh.get(array, i);
1067 assertEquals(x, (short)((short)0x0123 + (short)0x4567), "getAndAddRelease short value");
1068 }
1069
1070 // get and bitwise or
1071 {
1072 vh.set(array, i, (short)0x0123);
1073
1074 short o = (short) vh.getAndBitwiseOr(array, i, (short)0x4567);
1075 assertEquals(o, (short)0x0123, "getAndBitwiseOr short");
1076 short x = (short) vh.get(array, i);
1077 assertEquals(x, (short)((short)0x0123 | (short)0x4567), "getAndBitwiseOr short value");
1078 }
1079
1080 {
1081 vh.set(array, i, (short)0x0123);
1082
1083 short o = (short) vh.getAndBitwiseOrAcquire(array, i, (short)0x4567);
1084 assertEquals(o, (short)0x0123, "getAndBitwiseOrAcquire short");
1085 short x = (short) vh.get(array, i);
1086 assertEquals(x, (short)((short)0x0123 | (short)0x4567), "getAndBitwiseOrAcquire short value");
1087 }
1088
1089 {
1090 vh.set(array, i, (short)0x0123);
1091
1092 short o = (short) vh.getAndBitwiseOrRelease(array, i, (short)0x4567);
1093 assertEquals(o, (short)0x0123, "getAndBitwiseOrRelease short");
1094 short x = (short) vh.get(array, i);
1095 assertEquals(x, (short)((short)0x0123 | (short)0x4567), "getAndBitwiseOrRelease short value");
1096 }
1097
1098 // get and bitwise and
1099 {
1100 vh.set(array, i, (short)0x0123);
1101
1102 short o = (short) vh.getAndBitwiseAnd(array, i, (short)0x4567);
1103 assertEquals(o, (short)0x0123, "getAndBitwiseAnd short");
1104 short x = (short) vh.get(array, i);
1105 assertEquals(x, (short)((short)0x0123 & (short)0x4567), "getAndBitwiseAnd short value");
1106 }
1107
1108 {
1109 vh.set(array, i, (short)0x0123);
1110
1111 short o = (short) vh.getAndBitwiseAndAcquire(array, i, (short)0x4567);
1112 assertEquals(o, (short)0x0123, "getAndBitwiseAndAcquire short");
1113 short x = (short) vh.get(array, i);
1114 assertEquals(x, (short)((short)0x0123 & (short)0x4567), "getAndBitwiseAndAcquire short value");
1115 }
1116
1117 {
1118 vh.set(array, i, (short)0x0123);
1119
1120 short o = (short) vh.getAndBitwiseAndRelease(array, i, (short)0x4567);
1121 assertEquals(o, (short)0x0123, "getAndBitwiseAndRelease short");
1122 short x = (short) vh.get(array, i);
1123 assertEquals(x, (short)((short)0x0123 & (short)0x4567), "getAndBitwiseAndRelease short value");
1124 }
1125
1126 // get and bitwise xor
1127 {
1128 vh.set(array, i, (short)0x0123);
1129
1130 short o = (short) vh.getAndBitwiseXor(array, i, (short)0x4567);
1131 assertEquals(o, (short)0x0123, "getAndBitwiseXor short");
1132 short x = (short) vh.get(array, i);
1133 assertEquals(x, (short)((short)0x0123 ^ (short)0x4567), "getAndBitwiseXor short value");
1134 }
1135
1136 {
1137 vh.set(array, i, (short)0x0123);
1138
1139 short o = (short) vh.getAndBitwiseXorAcquire(array, i, (short)0x4567);
1140 assertEquals(o, (short)0x0123, "getAndBitwiseXorAcquire short");
1141 short x = (short) vh.get(array, i);
1142 assertEquals(x, (short)((short)0x0123 ^ (short)0x4567), "getAndBitwiseXorAcquire short value");
1143 }
1144
1145 {
1146 vh.set(array, i, (short)0x0123);
1147
1148 short o = (short) vh.getAndBitwiseXorRelease(array, i, (short)0x4567);
1149 assertEquals(o, (short)0x0123, "getAndBitwiseXorRelease short");
1150 short x = (short) vh.get(array, i);
1151 assertEquals(x, (short)((short)0x0123 ^ (short)0x4567), "getAndBitwiseXorRelease short value");
1152 }
Paul Sandoz9fb30a32016-03-24 11:21:21 +01001153 }
1154 }
1155
1156 static void testArrayUnsupported(VarHandle vh) {
1157 short[] array = new short[10];
1158
1159 int i = 0;
Paul Sandoz9fb30a32016-03-24 11:21:21 +01001160
Paul Sandoz82d48912016-09-01 10:16:57 -07001161
Paul Sandoz9fb30a32016-03-24 11:21:21 +01001162 }
1163
1164 static void testArrayIndexOutOfBounds(VarHandle vh) throws Throwable {
1165 short[] array = new short[10];
1166
1167 for (int i : new int[]{-1, Integer.MIN_VALUE, 10, 11, Integer.MAX_VALUE}) {
1168 final int ci = i;
1169
1170 checkIOOBE(() -> {
1171 short x = (short) vh.get(array, ci);
1172 });
1173
1174 checkIOOBE(() -> {
Aleksey Shipileve6632062016-06-15 11:20:15 +03001175 vh.set(array, ci, (short)0x0123);
Paul Sandoz9fb30a32016-03-24 11:21:21 +01001176 });
1177
1178 checkIOOBE(() -> {
1179 short x = (short) vh.getVolatile(array, ci);
1180 });
1181
1182 checkIOOBE(() -> {
Aleksey Shipileve6632062016-06-15 11:20:15 +03001183 vh.setVolatile(array, ci, (short)0x0123);
Paul Sandoz9fb30a32016-03-24 11:21:21 +01001184 });
1185
1186 checkIOOBE(() -> {
1187 short x = (short) vh.getAcquire(array, ci);
1188 });
1189
1190 checkIOOBE(() -> {
Aleksey Shipileve6632062016-06-15 11:20:15 +03001191 vh.setRelease(array, ci, (short)0x0123);
Paul Sandoz9fb30a32016-03-24 11:21:21 +01001192 });
1193
1194 checkIOOBE(() -> {
1195 short x = (short) vh.getOpaque(array, ci);
1196 });
1197
1198 checkIOOBE(() -> {
Aleksey Shipileve6632062016-06-15 11:20:15 +03001199 vh.setOpaque(array, ci, (short)0x0123);
Paul Sandoz9fb30a32016-03-24 11:21:21 +01001200 });
1201
Aleksey Shipileve6632062016-06-15 11:20:15 +03001202 checkIOOBE(() -> {
1203 boolean r = vh.compareAndSet(array, ci, (short)0x0123, (short)0x4567);
1204 });
Paul Sandoz9fb30a32016-03-24 11:21:21 +01001205
Aleksey Shipileve6632062016-06-15 11:20:15 +03001206 checkIOOBE(() -> {
Paul Sandoz3f0273a2016-06-23 13:46:48 +02001207 short r = (short) vh.compareAndExchange(array, ci, (short)0x4567, (short)0x0123);
Aleksey Shipileve6632062016-06-15 11:20:15 +03001208 });
1209
1210 checkIOOBE(() -> {
1211 short r = (short) vh.compareAndExchangeAcquire(array, ci, (short)0x4567, (short)0x0123);
1212 });
1213
1214 checkIOOBE(() -> {
1215 short r = (short) vh.compareAndExchangeRelease(array, ci, (short)0x4567, (short)0x0123);
1216 });
1217
1218 checkIOOBE(() -> {
Paul Sandoz3bd5ebe2016-09-01 13:56:13 -07001219 boolean r = vh.weakCompareAndSetPlain(array, ci, (short)0x0123, (short)0x4567);
Aleksey Shipileve6632062016-06-15 11:20:15 +03001220 });
1221
1222 checkIOOBE(() -> {
Paul Sandoz3bd5ebe2016-09-01 13:56:13 -07001223 boolean r = vh.weakCompareAndSet(array, ci, (short)0x0123, (short)0x4567);
Aleksey Shipileve6632062016-06-15 11:20:15 +03001224 });
1225
1226 checkIOOBE(() -> {
1227 boolean r = vh.weakCompareAndSetAcquire(array, ci, (short)0x0123, (short)0x4567);
1228 });
1229
1230 checkIOOBE(() -> {
1231 boolean r = vh.weakCompareAndSetRelease(array, ci, (short)0x0123, (short)0x4567);
1232 });
1233
1234 checkIOOBE(() -> {
1235 short o = (short) vh.getAndSet(array, ci, (short)0x0123);
1236 });
1237
1238 checkIOOBE(() -> {
Paul Sandoz82d48912016-09-01 10:16:57 -07001239 short o = (short) vh.getAndSetAcquire(array, ci, (short)0x0123);
Aleksey Shipileve6632062016-06-15 11:20:15 +03001240 });
1241
1242 checkIOOBE(() -> {
Paul Sandoz82d48912016-09-01 10:16:57 -07001243 short o = (short) vh.getAndSetRelease(array, ci, (short)0x0123);
1244 });
1245
1246 checkIOOBE(() -> {
1247 short o = (short) vh.getAndAdd(array, ci, (short)0x0123);
1248 });
1249
1250 checkIOOBE(() -> {
1251 short o = (short) vh.getAndAddAcquire(array, ci, (short)0x0123);
1252 });
1253
1254 checkIOOBE(() -> {
1255 short o = (short) vh.getAndAddRelease(array, ci, (short)0x0123);
1256 });
1257
1258 checkIOOBE(() -> {
Paul Sandoz82d48912016-09-01 10:16:57 -07001259 short o = (short) vh.getAndBitwiseOr(array, ci, (short)0x0123);
1260 });
1261
1262 checkIOOBE(() -> {
1263 short o = (short) vh.getAndBitwiseOrAcquire(array, ci, (short)0x0123);
1264 });
1265
1266 checkIOOBE(() -> {
1267 short o = (short) vh.getAndBitwiseOrRelease(array, ci, (short)0x0123);
1268 });
1269
1270 checkIOOBE(() -> {
1271 short o = (short) vh.getAndBitwiseAnd(array, ci, (short)0x0123);
1272 });
1273
1274 checkIOOBE(() -> {
1275 short o = (short) vh.getAndBitwiseAndAcquire(array, ci, (short)0x0123);
1276 });
1277
1278 checkIOOBE(() -> {
1279 short o = (short) vh.getAndBitwiseAndRelease(array, ci, (short)0x0123);
1280 });
1281
1282 checkIOOBE(() -> {
1283 short o = (short) vh.getAndBitwiseXor(array, ci, (short)0x0123);
1284 });
1285
1286 checkIOOBE(() -> {
1287 short o = (short) vh.getAndBitwiseXorAcquire(array, ci, (short)0x0123);
1288 });
1289
1290 checkIOOBE(() -> {
1291 short o = (short) vh.getAndBitwiseXorRelease(array, ci, (short)0x0123);
Aleksey Shipileve6632062016-06-15 11:20:15 +03001292 });
Paul Sandoz9fb30a32016-03-24 11:21:21 +01001293 }
1294 }
1295}
1296