author | psandoz |
Tue, 26 Apr 2016 18:30:00 -0700 | |
changeset 37668 | 34a002e5168a |
parent 37343 | 35a2231828a7 |
child 37719 | add11bc0e6e2 |
child 38357 | f18eb84a3b96 |
permissions | -rw-r--r-- |
36934 | 1 |
/* |
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 VarHandleTestAccessInt |
|
27 |
* @run testng/othervm -Diters=20000 -XX:TieredStopAtLevel=1 VarHandleTestAccessInt |
|
28 |
* @run testng/othervm -Diters=20000 VarHandleTestAccessInt |
|
29 |
* @run testng/othervm -Diters=20000 -XX:-TieredCompilation VarHandleTestAccessInt |
|
30 |
*/ |
|
31 |
||
32 |
import org.testng.annotations.BeforeClass; |
|
33 |
import org.testng.annotations.DataProvider; |
|
34 |
import org.testng.annotations.Test; |
|
35 |
||
36 |
import java.lang.invoke.MethodHandles; |
|
37 |
import java.lang.invoke.VarHandle; |
|
38 |
import java.util.ArrayList; |
|
39 |
import java.util.Arrays; |
|
40 |
import java.util.List; |
|
41 |
||
42 |
import static org.testng.Assert.*; |
|
43 |
||
44 |
public class VarHandleTestAccessInt extends VarHandleBaseTest { |
|
45 |
static final int static_final_v = 1; |
|
46 |
||
47 |
static int static_v; |
|
48 |
||
49 |
final int final_v = 1; |
|
50 |
||
51 |
int 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 |
VarHandleTestAccessInt.class, "final_v", int.class); |
|
67 |
||
68 |
vhField = MethodHandles.lookup().findVarHandle( |
|
69 |
VarHandleTestAccessInt.class, "v", int.class); |
|
70 |
||
71 |
vhStaticFinalField = MethodHandles.lookup().findStaticVarHandle( |
|
72 |
VarHandleTestAccessInt.class, "static_final_v", int.class); |
|
73 |
||
74 |
vhStaticField = MethodHandles.lookup().findStaticVarHandle( |
|
75 |
VarHandleTestAccessInt.class, "static_v", int.class); |
|
76 |
||
77 |
vhArray = MethodHandles.arrayElementVarHandle(int[].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) { |
|
37343
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36934
diff
changeset
|
93 |
assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET)); |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36934
diff
changeset
|
94 |
assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET)); |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36934
diff
changeset
|
95 |
assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_VOLATILE)); |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36934
diff
changeset
|
96 |
assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET_VOLATILE)); |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36934
diff
changeset
|
97 |
assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_ACQUIRE)); |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36934
diff
changeset
|
98 |
assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET_RELEASE)); |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36934
diff
changeset
|
99 |
assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_OPAQUE)); |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36934
diff
changeset
|
100 |
assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET_OPAQUE)); |
36934 | 101 |
|
37343
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36934
diff
changeset
|
102 |
assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_SET)); |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36934
diff
changeset
|
103 |
assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_VOLATILE)); |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36934
diff
changeset
|
104 |
assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_ACQUIRE)); |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36934
diff
changeset
|
105 |
assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_RELEASE)); |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36934
diff
changeset
|
106 |
assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET)); |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36934
diff
changeset
|
107 |
assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_ACQUIRE)); |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36934
diff
changeset
|
108 |
assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_RELEASE)); |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36934
diff
changeset
|
109 |
assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_SET)); |
36934 | 110 |
|
37343
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36934
diff
changeset
|
111 |
assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_ADD)); |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36934
diff
changeset
|
112 |
assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.ADD_AND_GET)); |
36934 | 113 |
} |
114 |
||
115 |
||
116 |
@DataProvider |
|
117 |
public Object[][] typesProvider() throws Exception { |
|
118 |
List<Object[]> types = new ArrayList<>(); |
|
119 |
types.add(new Object[] {vhField, Arrays.asList(VarHandleTestAccessInt.class)}); |
|
120 |
types.add(new Object[] {vhStaticField, Arrays.asList()}); |
|
121 |
types.add(new Object[] {vhArray, Arrays.asList(int[].class, int.class)}); |
|
122 |
||
123 |
return types.stream().toArray(Object[][]::new); |
|
124 |
} |
|
125 |
||
126 |
@Test(dataProvider = "typesProvider") |
|
127 |
public void testTypes(VarHandle vh, List<Class<?>> pts) { |
|
128 |
assertEquals(vh.varType(), int.class); |
|
129 |
||
130 |
assertEquals(vh.coordinateTypes(), pts); |
|
131 |
||
132 |
testTypes(vh); |
|
133 |
} |
|
134 |
||
135 |
||
136 |
@Test |
|
137 |
public void testLookupInstanceToStatic() { |
|
138 |
checkIAE("Lookup of static final field to instance final field", () -> { |
|
139 |
MethodHandles.lookup().findStaticVarHandle( |
|
140 |
VarHandleTestAccessInt.class, "final_v", int.class); |
|
141 |
}); |
|
142 |
||
143 |
checkIAE("Lookup of static field to instance field", () -> { |
|
144 |
MethodHandles.lookup().findStaticVarHandle( |
|
145 |
VarHandleTestAccessInt.class, "v", int.class); |
|
146 |
}); |
|
147 |
} |
|
148 |
||
149 |
@Test |
|
150 |
public void testLookupStaticToInstance() { |
|
151 |
checkIAE("Lookup of instance final field to static final field", () -> { |
|
152 |
MethodHandles.lookup().findVarHandle( |
|
153 |
VarHandleTestAccessInt.class, "static_final_v", int.class); |
|
154 |
}); |
|
155 |
||
156 |
checkIAE("Lookup of instance field to static field", () -> { |
|
157 |
vhStaticField = MethodHandles.lookup().findVarHandle( |
|
158 |
VarHandleTestAccessInt.class, "static_v", int.class); |
|
159 |
}); |
|
160 |
} |
|
161 |
||
162 |
||
163 |
@DataProvider |
|
164 |
public Object[][] accessTestCaseProvider() throws Exception { |
|
165 |
List<AccessTestCase<?>> cases = new ArrayList<>(); |
|
166 |
||
167 |
cases.add(new VarHandleAccessTestCase("Instance final field", |
|
168 |
vhFinalField, vh -> testInstanceFinalField(this, vh))); |
|
169 |
cases.add(new VarHandleAccessTestCase("Instance final field unsupported", |
|
170 |
vhFinalField, vh -> testInstanceFinalFieldUnsupported(this, vh), |
|
171 |
false)); |
|
172 |
||
173 |
cases.add(new VarHandleAccessTestCase("Static final field", |
|
174 |
vhStaticFinalField, VarHandleTestAccessInt::testStaticFinalField)); |
|
175 |
cases.add(new VarHandleAccessTestCase("Static final field unsupported", |
|
176 |
vhStaticFinalField, VarHandleTestAccessInt::testStaticFinalFieldUnsupported, |
|
177 |
false)); |
|
178 |
||
179 |
cases.add(new VarHandleAccessTestCase("Instance field", |
|
180 |
vhField, vh -> testInstanceField(this, vh))); |
|
181 |
cases.add(new VarHandleAccessTestCase("Instance field unsupported", |
|
182 |
vhField, vh -> testInstanceFieldUnsupported(this, vh), |
|
183 |
false)); |
|
184 |
||
185 |
cases.add(new VarHandleAccessTestCase("Static field", |
|
186 |
vhStaticField, VarHandleTestAccessInt::testStaticField)); |
|
187 |
cases.add(new VarHandleAccessTestCase("Static field unsupported", |
|
188 |
vhStaticField, VarHandleTestAccessInt::testStaticFieldUnsupported, |
|
189 |
false)); |
|
190 |
||
191 |
cases.add(new VarHandleAccessTestCase("Array", |
|
192 |
vhArray, VarHandleTestAccessInt::testArray)); |
|
193 |
cases.add(new VarHandleAccessTestCase("Array unsupported", |
|
194 |
vhArray, VarHandleTestAccessInt::testArrayUnsupported, |
|
195 |
false)); |
|
196 |
cases.add(new VarHandleAccessTestCase("Array index out of bounds", |
|
197 |
vhArray, VarHandleTestAccessInt::testArrayIndexOutOfBounds, |
|
198 |
false)); |
|
199 |
||
200 |
// Work around issue with jtreg summary reporting which truncates |
|
201 |
// the String result of Object.toString to 30 characters, hence |
|
202 |
// the first dummy argument |
|
203 |
return cases.stream().map(tc -> new Object[]{tc.toString(), tc}).toArray(Object[][]::new); |
|
204 |
} |
|
205 |
||
206 |
@Test(dataProvider = "accessTestCaseProvider") |
|
207 |
public <T> void testAccess(String desc, AccessTestCase<T> atc) throws Throwable { |
|
208 |
T t = atc.get(); |
|
209 |
int iters = atc.requiresLoop() ? ITERS : 1; |
|
210 |
for (int c = 0; c < iters; c++) { |
|
211 |
atc.testAccess(t); |
|
212 |
} |
|
213 |
} |
|
214 |
||
215 |
||
216 |
||
217 |
||
218 |
static void testInstanceFinalField(VarHandleTestAccessInt recv, VarHandle vh) { |
|
219 |
// Plain |
|
220 |
{ |
|
221 |
int x = (int) vh.get(recv); |
|
222 |
assertEquals(x, 1, "get int value"); |
|
223 |
} |
|
224 |
||
225 |
||
226 |
// Volatile |
|
227 |
{ |
|
228 |
int x = (int) vh.getVolatile(recv); |
|
229 |
assertEquals(x, 1, "getVolatile int value"); |
|
230 |
} |
|
231 |
||
232 |
// Lazy |
|
233 |
{ |
|
234 |
int x = (int) vh.getAcquire(recv); |
|
235 |
assertEquals(x, 1, "getRelease int value"); |
|
236 |
} |
|
237 |
||
238 |
// Opaque |
|
239 |
{ |
|
240 |
int x = (int) vh.getOpaque(recv); |
|
241 |
assertEquals(x, 1, "getOpaque int value"); |
|
242 |
} |
|
243 |
} |
|
244 |
||
245 |
static void testInstanceFinalFieldUnsupported(VarHandleTestAccessInt recv, VarHandle vh) { |
|
246 |
checkUOE(() -> { |
|
247 |
vh.set(recv, 2); |
|
248 |
}); |
|
249 |
||
250 |
checkUOE(() -> { |
|
251 |
vh.setVolatile(recv, 2); |
|
252 |
}); |
|
253 |
||
254 |
checkUOE(() -> { |
|
255 |
vh.setRelease(recv, 2); |
|
256 |
}); |
|
257 |
||
258 |
checkUOE(() -> { |
|
259 |
vh.setOpaque(recv, 2); |
|
260 |
}); |
|
261 |
||
262 |
||
263 |
} |
|
264 |
||
265 |
||
266 |
static void testStaticFinalField(VarHandle vh) { |
|
267 |
// Plain |
|
268 |
{ |
|
269 |
int x = (int) vh.get(); |
|
270 |
assertEquals(x, 1, "get int value"); |
|
271 |
} |
|
272 |
||
273 |
||
274 |
// Volatile |
|
275 |
{ |
|
276 |
int x = (int) vh.getVolatile(); |
|
277 |
assertEquals(x, 1, "getVolatile int value"); |
|
278 |
} |
|
279 |
||
280 |
// Lazy |
|
281 |
{ |
|
282 |
int x = (int) vh.getAcquire(); |
|
283 |
assertEquals(x, 1, "getRelease int value"); |
|
284 |
} |
|
285 |
||
286 |
// Opaque |
|
287 |
{ |
|
288 |
int x = (int) vh.getOpaque(); |
|
289 |
assertEquals(x, 1, "getOpaque int value"); |
|
290 |
} |
|
291 |
} |
|
292 |
||
293 |
static void testStaticFinalFieldUnsupported(VarHandle vh) { |
|
294 |
checkUOE(() -> { |
|
295 |
vh.set(2); |
|
296 |
}); |
|
297 |
||
298 |
checkUOE(() -> { |
|
299 |
vh.setVolatile(2); |
|
300 |
}); |
|
301 |
||
302 |
checkUOE(() -> { |
|
303 |
vh.setRelease(2); |
|
304 |
}); |
|
305 |
||
306 |
checkUOE(() -> { |
|
307 |
vh.setOpaque(2); |
|
308 |
}); |
|
309 |
||
310 |
||
311 |
} |
|
312 |
||
313 |
||
314 |
static void testInstanceField(VarHandleTestAccessInt recv, VarHandle vh) { |
|
315 |
// Plain |
|
316 |
{ |
|
317 |
vh.set(recv, 1); |
|
318 |
int x = (int) vh.get(recv); |
|
319 |
assertEquals(x, 1, "set int value"); |
|
320 |
} |
|
321 |
||
322 |
||
323 |
// Volatile |
|
324 |
{ |
|
325 |
vh.setVolatile(recv, 2); |
|
326 |
int x = (int) vh.getVolatile(recv); |
|
327 |
assertEquals(x, 2, "setVolatile int value"); |
|
328 |
} |
|
329 |
||
330 |
// Lazy |
|
331 |
{ |
|
332 |
vh.setRelease(recv, 1); |
|
333 |
int x = (int) vh.getAcquire(recv); |
|
334 |
assertEquals(x, 1, "setRelease int value"); |
|
335 |
} |
|
336 |
||
337 |
// Opaque |
|
338 |
{ |
|
339 |
vh.setOpaque(recv, 2); |
|
340 |
int x = (int) vh.getOpaque(recv); |
|
341 |
assertEquals(x, 2, "setOpaque int value"); |
|
342 |
} |
|
343 |
||
344 |
vh.set(recv, 1); |
|
345 |
||
346 |
// Compare |
|
347 |
{ |
|
348 |
boolean r = vh.compareAndSet(recv, 1, 2); |
|
349 |
assertEquals(r, true, "success compareAndSet int"); |
|
350 |
int x = (int) vh.get(recv); |
|
351 |
assertEquals(x, 2, "success compareAndSet int value"); |
|
352 |
} |
|
353 |
||
354 |
{ |
|
355 |
boolean r = vh.compareAndSet(recv, 1, 3); |
|
356 |
assertEquals(r, false, "failing compareAndSet int"); |
|
357 |
int x = (int) vh.get(recv); |
|
358 |
assertEquals(x, 2, "failing compareAndSet int value"); |
|
359 |
} |
|
360 |
||
361 |
{ |
|
362 |
int r = (int) vh.compareAndExchangeVolatile(recv, 2, 1); |
|
363 |
assertEquals(r, 2, "success compareAndExchangeVolatile int"); |
|
364 |
int x = (int) vh.get(recv); |
|
365 |
assertEquals(x, 1, "success compareAndExchangeVolatile int value"); |
|
366 |
} |
|
367 |
||
368 |
{ |
|
369 |
int r = (int) vh.compareAndExchangeVolatile(recv, 2, 3); |
|
370 |
assertEquals(r, 1, "failing compareAndExchangeVolatile int"); |
|
371 |
int x = (int) vh.get(recv); |
|
372 |
assertEquals(x, 1, "failing compareAndExchangeVolatile int value"); |
|
373 |
} |
|
374 |
||
375 |
{ |
|
376 |
int r = (int) vh.compareAndExchangeAcquire(recv, 1, 2); |
|
377 |
assertEquals(r, 1, "success compareAndExchangeAcquire int"); |
|
378 |
int x = (int) vh.get(recv); |
|
379 |
assertEquals(x, 2, "success compareAndExchangeAcquire int value"); |
|
380 |
} |
|
381 |
||
382 |
{ |
|
383 |
int r = (int) vh.compareAndExchangeAcquire(recv, 1, 3); |
|
384 |
assertEquals(r, 2, "failing compareAndExchangeAcquire int"); |
|
385 |
int x = (int) vh.get(recv); |
|
386 |
assertEquals(x, 2, "failing compareAndExchangeAcquire int value"); |
|
387 |
} |
|
388 |
||
389 |
{ |
|
390 |
int r = (int) vh.compareAndExchangeRelease(recv, 2, 1); |
|
391 |
assertEquals(r, 2, "success compareAndExchangeRelease int"); |
|
392 |
int x = (int) vh.get(recv); |
|
393 |
assertEquals(x, 1, "success compareAndExchangeRelease int value"); |
|
394 |
} |
|
395 |
||
396 |
{ |
|
397 |
int r = (int) vh.compareAndExchangeRelease(recv, 2, 3); |
|
398 |
assertEquals(r, 1, "failing compareAndExchangeRelease int"); |
|
399 |
int x = (int) vh.get(recv); |
|
400 |
assertEquals(x, 1, "failing compareAndExchangeRelease int value"); |
|
401 |
} |
|
402 |
||
403 |
{ |
|
404 |
boolean r = vh.weakCompareAndSet(recv, 1, 2); |
|
405 |
assertEquals(r, true, "weakCompareAndSet int"); |
|
406 |
int x = (int) vh.get(recv); |
|
407 |
assertEquals(x, 2, "weakCompareAndSet int value"); |
|
408 |
} |
|
409 |
||
410 |
{ |
|
411 |
boolean r = vh.weakCompareAndSetAcquire(recv, 2, 1); |
|
412 |
assertEquals(r, true, "weakCompareAndSetAcquire int"); |
|
413 |
int x = (int) vh.get(recv); |
|
414 |
assertEquals(x, 1, "weakCompareAndSetAcquire int"); |
|
415 |
} |
|
416 |
||
417 |
{ |
|
418 |
boolean r = vh.weakCompareAndSetRelease(recv, 1, 2); |
|
419 |
assertEquals(r, true, "weakCompareAndSetRelease int"); |
|
420 |
int x = (int) vh.get(recv); |
|
421 |
assertEquals(x, 2, "weakCompareAndSetRelease int"); |
|
422 |
} |
|
423 |
||
424 |
// Compare set and get |
|
425 |
{ |
|
426 |
int o = (int) vh.getAndSet(recv, 1); |
|
427 |
assertEquals(o, 2, "getAndSet int"); |
|
428 |
int x = (int) vh.get(recv); |
|
429 |
assertEquals(x, 1, "getAndSet int value"); |
|
430 |
} |
|
431 |
||
432 |
vh.set(recv, 1); |
|
433 |
||
434 |
// get and add, add and get |
|
435 |
{ |
|
436 |
int o = (int) vh.getAndAdd(recv, 3); |
|
437 |
assertEquals(o, 1, "getAndAdd int"); |
|
438 |
int c = (int) vh.addAndGet(recv, 3); |
|
439 |
assertEquals(c, 1 + 3 + 3, "getAndAdd int value"); |
|
440 |
} |
|
441 |
} |
|
442 |
||
443 |
static void testInstanceFieldUnsupported(VarHandleTestAccessInt recv, VarHandle vh) { |
|
444 |
||
445 |
} |
|
446 |
||
447 |
||
448 |
static void testStaticField(VarHandle vh) { |
|
449 |
// Plain |
|
450 |
{ |
|
451 |
vh.set(1); |
|
452 |
int x = (int) vh.get(); |
|
453 |
assertEquals(x, 1, "set int value"); |
|
454 |
} |
|
455 |
||
456 |
||
457 |
// Volatile |
|
458 |
{ |
|
459 |
vh.setVolatile(2); |
|
460 |
int x = (int) vh.getVolatile(); |
|
461 |
assertEquals(x, 2, "setVolatile int value"); |
|
462 |
} |
|
463 |
||
464 |
// Lazy |
|
465 |
{ |
|
466 |
vh.setRelease(1); |
|
467 |
int x = (int) vh.getAcquire(); |
|
468 |
assertEquals(x, 1, "setRelease int value"); |
|
469 |
} |
|
470 |
||
471 |
// Opaque |
|
472 |
{ |
|
473 |
vh.setOpaque(2); |
|
474 |
int x = (int) vh.getOpaque(); |
|
475 |
assertEquals(x, 2, "setOpaque int value"); |
|
476 |
} |
|
477 |
||
478 |
vh.set(1); |
|
479 |
||
480 |
// Compare |
|
481 |
{ |
|
482 |
boolean r = vh.compareAndSet(1, 2); |
|
483 |
assertEquals(r, true, "success compareAndSet int"); |
|
484 |
int x = (int) vh.get(); |
|
485 |
assertEquals(x, 2, "success compareAndSet int value"); |
|
486 |
} |
|
487 |
||
488 |
{ |
|
489 |
boolean r = vh.compareAndSet(1, 3); |
|
490 |
assertEquals(r, false, "failing compareAndSet int"); |
|
491 |
int x = (int) vh.get(); |
|
492 |
assertEquals(x, 2, "failing compareAndSet int value"); |
|
493 |
} |
|
494 |
||
495 |
{ |
|
496 |
int r = (int) vh.compareAndExchangeVolatile(2, 1); |
|
497 |
assertEquals(r, 2, "success compareAndExchangeVolatile int"); |
|
498 |
int x = (int) vh.get(); |
|
499 |
assertEquals(x, 1, "success compareAndExchangeVolatile int value"); |
|
500 |
} |
|
501 |
||
502 |
{ |
|
503 |
int r = (int) vh.compareAndExchangeVolatile(2, 3); |
|
504 |
assertEquals(r, 1, "failing compareAndExchangeVolatile int"); |
|
505 |
int x = (int) vh.get(); |
|
506 |
assertEquals(x, 1, "failing compareAndExchangeVolatile int value"); |
|
507 |
} |
|
508 |
||
509 |
{ |
|
510 |
int r = (int) vh.compareAndExchangeAcquire(1, 2); |
|
511 |
assertEquals(r, 1, "success compareAndExchangeAcquire int"); |
|
512 |
int x = (int) vh.get(); |
|
513 |
assertEquals(x, 2, "success compareAndExchangeAcquire int value"); |
|
514 |
} |
|
515 |
||
516 |
{ |
|
517 |
int r = (int) vh.compareAndExchangeAcquire(1, 3); |
|
518 |
assertEquals(r, 2, "failing compareAndExchangeAcquire int"); |
|
519 |
int x = (int) vh.get(); |
|
520 |
assertEquals(x, 2, "failing compareAndExchangeAcquire int value"); |
|
521 |
} |
|
522 |
||
523 |
{ |
|
524 |
int r = (int) vh.compareAndExchangeRelease(2, 1); |
|
525 |
assertEquals(r, 2, "success compareAndExchangeRelease int"); |
|
526 |
int x = (int) vh.get(); |
|
527 |
assertEquals(x, 1, "success compareAndExchangeRelease int value"); |
|
528 |
} |
|
529 |
||
530 |
{ |
|
531 |
int r = (int) vh.compareAndExchangeRelease(2, 3); |
|
532 |
assertEquals(r, 1, "failing compareAndExchangeRelease int"); |
|
533 |
int x = (int) vh.get(); |
|
534 |
assertEquals(x, 1, "failing compareAndExchangeRelease int value"); |
|
535 |
} |
|
536 |
||
537 |
{ |
|
538 |
boolean r = (boolean) vh.weakCompareAndSet(1, 2); |
|
539 |
assertEquals(r, true, "weakCompareAndSet int"); |
|
540 |
int x = (int) vh.get(); |
|
541 |
assertEquals(x, 2, "weakCompareAndSet int value"); |
|
542 |
} |
|
543 |
||
544 |
{ |
|
545 |
boolean r = (boolean) vh.weakCompareAndSetAcquire(2, 1); |
|
546 |
assertEquals(r, true, "weakCompareAndSetAcquire int"); |
|
547 |
int x = (int) vh.get(); |
|
548 |
assertEquals(x, 1, "weakCompareAndSetAcquire int"); |
|
549 |
} |
|
550 |
||
551 |
{ |
|
552 |
boolean r = (boolean) vh.weakCompareAndSetRelease( 1, 2); |
|
553 |
assertEquals(r, true, "weakCompareAndSetRelease int"); |
|
554 |
int x = (int) vh.get(); |
|
555 |
assertEquals(x, 2, "weakCompareAndSetRelease int"); |
|
556 |
} |
|
557 |
||
558 |
// Compare set and get |
|
559 |
{ |
|
560 |
int o = (int) vh.getAndSet( 1); |
|
561 |
assertEquals(o, 2, "getAndSet int"); |
|
562 |
int x = (int) vh.get(); |
|
563 |
assertEquals(x, 1, "getAndSet int value"); |
|
564 |
} |
|
565 |
||
566 |
vh.set(1); |
|
567 |
||
568 |
// get and add, add and get |
|
569 |
{ |
|
570 |
int o = (int) vh.getAndAdd( 3); |
|
571 |
assertEquals(o, 1, "getAndAdd int"); |
|
572 |
int c = (int) vh.addAndGet(3); |
|
573 |
assertEquals(c, 1 + 3 + 3, "getAndAdd int value"); |
|
574 |
} |
|
575 |
} |
|
576 |
||
577 |
static void testStaticFieldUnsupported(VarHandle vh) { |
|
578 |
||
579 |
} |
|
580 |
||
581 |
||
582 |
static void testArray(VarHandle vh) { |
|
583 |
int[] array = new int[10]; |
|
584 |
||
585 |
for (int i = 0; i < array.length; i++) { |
|
586 |
// Plain |
|
587 |
{ |
|
588 |
vh.set(array, i, 1); |
|
589 |
int x = (int) vh.get(array, i); |
|
590 |
assertEquals(x, 1, "get int value"); |
|
591 |
} |
|
592 |
||
593 |
||
594 |
// Volatile |
|
595 |
{ |
|
596 |
vh.setVolatile(array, i, 2); |
|
597 |
int x = (int) vh.getVolatile(array, i); |
|
598 |
assertEquals(x, 2, "setVolatile int value"); |
|
599 |
} |
|
600 |
||
601 |
// Lazy |
|
602 |
{ |
|
603 |
vh.setRelease(array, i, 1); |
|
604 |
int x = (int) vh.getAcquire(array, i); |
|
605 |
assertEquals(x, 1, "setRelease int value"); |
|
606 |
} |
|
607 |
||
608 |
// Opaque |
|
609 |
{ |
|
610 |
vh.setOpaque(array, i, 2); |
|
611 |
int x = (int) vh.getOpaque(array, i); |
|
612 |
assertEquals(x, 2, "setOpaque int value"); |
|
613 |
} |
|
614 |
||
615 |
vh.set(array, i, 1); |
|
616 |
||
617 |
// Compare |
|
618 |
{ |
|
619 |
boolean r = vh.compareAndSet(array, i, 1, 2); |
|
620 |
assertEquals(r, true, "success compareAndSet int"); |
|
621 |
int x = (int) vh.get(array, i); |
|
622 |
assertEquals(x, 2, "success compareAndSet int value"); |
|
623 |
} |
|
624 |
||
625 |
{ |
|
626 |
boolean r = vh.compareAndSet(array, i, 1, 3); |
|
627 |
assertEquals(r, false, "failing compareAndSet int"); |
|
628 |
int x = (int) vh.get(array, i); |
|
629 |
assertEquals(x, 2, "failing compareAndSet int value"); |
|
630 |
} |
|
631 |
||
632 |
{ |
|
633 |
int r = (int) vh.compareAndExchangeVolatile(array, i, 2, 1); |
|
634 |
assertEquals(r, 2, "success compareAndExchangeVolatile int"); |
|
635 |
int x = (int) vh.get(array, i); |
|
636 |
assertEquals(x, 1, "success compareAndExchangeVolatile int value"); |
|
637 |
} |
|
638 |
||
639 |
{ |
|
640 |
int r = (int) vh.compareAndExchangeVolatile(array, i, 2, 3); |
|
641 |
assertEquals(r, 1, "failing compareAndExchangeVolatile int"); |
|
642 |
int x = (int) vh.get(array, i); |
|
643 |
assertEquals(x, 1, "failing compareAndExchangeVolatile int value"); |
|
644 |
} |
|
645 |
||
646 |
{ |
|
647 |
int r = (int) vh.compareAndExchangeAcquire(array, i, 1, 2); |
|
648 |
assertEquals(r, 1, "success compareAndExchangeAcquire int"); |
|
649 |
int x = (int) vh.get(array, i); |
|
650 |
assertEquals(x, 2, "success compareAndExchangeAcquire int value"); |
|
651 |
} |
|
652 |
||
653 |
{ |
|
654 |
int r = (int) vh.compareAndExchangeAcquire(array, i, 1, 3); |
|
655 |
assertEquals(r, 2, "failing compareAndExchangeAcquire int"); |
|
656 |
int x = (int) vh.get(array, i); |
|
657 |
assertEquals(x, 2, "failing compareAndExchangeAcquire int value"); |
|
658 |
} |
|
659 |
||
660 |
{ |
|
661 |
int r = (int) vh.compareAndExchangeRelease(array, i, 2, 1); |
|
662 |
assertEquals(r, 2, "success compareAndExchangeRelease int"); |
|
663 |
int x = (int) vh.get(array, i); |
|
664 |
assertEquals(x, 1, "success compareAndExchangeRelease int value"); |
|
665 |
} |
|
666 |
||
667 |
{ |
|
668 |
int r = (int) vh.compareAndExchangeRelease(array, i, 2, 3); |
|
669 |
assertEquals(r, 1, "failing compareAndExchangeRelease int"); |
|
670 |
int x = (int) vh.get(array, i); |
|
671 |
assertEquals(x, 1, "failing compareAndExchangeRelease int value"); |
|
672 |
} |
|
673 |
||
674 |
{ |
|
675 |
boolean r = vh.weakCompareAndSet(array, i, 1, 2); |
|
676 |
assertEquals(r, true, "weakCompareAndSet int"); |
|
677 |
int x = (int) vh.get(array, i); |
|
678 |
assertEquals(x, 2, "weakCompareAndSet int value"); |
|
679 |
} |
|
680 |
||
681 |
{ |
|
682 |
boolean r = vh.weakCompareAndSetAcquire(array, i, 2, 1); |
|
683 |
assertEquals(r, true, "weakCompareAndSetAcquire int"); |
|
684 |
int x = (int) vh.get(array, i); |
|
685 |
assertEquals(x, 1, "weakCompareAndSetAcquire int"); |
|
686 |
} |
|
687 |
||
688 |
{ |
|
689 |
boolean r = vh.weakCompareAndSetRelease(array, i, 1, 2); |
|
690 |
assertEquals(r, true, "weakCompareAndSetRelease int"); |
|
691 |
int x = (int) vh.get(array, i); |
|
692 |
assertEquals(x, 2, "weakCompareAndSetRelease int"); |
|
693 |
} |
|
694 |
||
695 |
// Compare set and get |
|
696 |
{ |
|
697 |
int o = (int) vh.getAndSet(array, i, 1); |
|
698 |
assertEquals(o, 2, "getAndSet int"); |
|
699 |
int x = (int) vh.get(array, i); |
|
700 |
assertEquals(x, 1, "getAndSet int value"); |
|
701 |
} |
|
702 |
||
703 |
vh.set(array, i, 1); |
|
704 |
||
705 |
// get and add, add and get |
|
706 |
{ |
|
707 |
int o = (int) vh.getAndAdd(array, i, 3); |
|
708 |
assertEquals(o, 1, "getAndAdd int"); |
|
709 |
int c = (int) vh.addAndGet(array, i, 3); |
|
710 |
assertEquals(c, 1 + 3 + 3, "getAndAdd int value"); |
|
711 |
} |
|
712 |
} |
|
713 |
} |
|
714 |
||
715 |
static void testArrayUnsupported(VarHandle vh) { |
|
716 |
int[] array = new int[10]; |
|
717 |
||
718 |
int i = 0; |
|
719 |
||
720 |
} |
|
721 |
||
722 |
static void testArrayIndexOutOfBounds(VarHandle vh) throws Throwable { |
|
723 |
int[] array = new int[10]; |
|
724 |
||
725 |
for (int i : new int[]{-1, Integer.MIN_VALUE, 10, 11, Integer.MAX_VALUE}) { |
|
726 |
final int ci = i; |
|
727 |
||
728 |
checkIOOBE(() -> { |
|
729 |
int x = (int) vh.get(array, ci); |
|
730 |
}); |
|
731 |
||
732 |
checkIOOBE(() -> { |
|
733 |
vh.set(array, ci, 1); |
|
734 |
}); |
|
735 |
||
736 |
checkIOOBE(() -> { |
|
737 |
int x = (int) vh.getVolatile(array, ci); |
|
738 |
}); |
|
739 |
||
740 |
checkIOOBE(() -> { |
|
741 |
vh.setVolatile(array, ci, 1); |
|
742 |
}); |
|
743 |
||
744 |
checkIOOBE(() -> { |
|
745 |
int x = (int) vh.getAcquire(array, ci); |
|
746 |
}); |
|
747 |
||
748 |
checkIOOBE(() -> { |
|
749 |
vh.setRelease(array, ci, 1); |
|
750 |
}); |
|
751 |
||
752 |
checkIOOBE(() -> { |
|
753 |
int x = (int) vh.getOpaque(array, ci); |
|
754 |
}); |
|
755 |
||
756 |
checkIOOBE(() -> { |
|
757 |
vh.setOpaque(array, ci, 1); |
|
758 |
}); |
|
759 |
||
760 |
checkIOOBE(() -> { |
|
761 |
boolean r = vh.compareAndSet(array, ci, 1, 2); |
|
762 |
}); |
|
763 |
||
764 |
checkIOOBE(() -> { |
|
765 |
int r = (int) vh.compareAndExchangeVolatile(array, ci, 2, 1); |
|
766 |
}); |
|
767 |
||
768 |
checkIOOBE(() -> { |
|
769 |
int r = (int) vh.compareAndExchangeAcquire(array, ci, 2, 1); |
|
770 |
}); |
|
771 |
||
772 |
checkIOOBE(() -> { |
|
773 |
int r = (int) vh.compareAndExchangeRelease(array, ci, 2, 1); |
|
774 |
}); |
|
775 |
||
776 |
checkIOOBE(() -> { |
|
777 |
boolean r = vh.weakCompareAndSet(array, ci, 1, 2); |
|
778 |
}); |
|
779 |
||
780 |
checkIOOBE(() -> { |
|
781 |
boolean r = vh.weakCompareAndSetAcquire(array, ci, 1, 2); |
|
782 |
}); |
|
783 |
||
784 |
checkIOOBE(() -> { |
|
785 |
boolean r = vh.weakCompareAndSetRelease(array, ci, 1, 2); |
|
786 |
}); |
|
787 |
||
788 |
checkIOOBE(() -> { |
|
789 |
int o = (int) vh.getAndSet(array, ci, 1); |
|
790 |
}); |
|
791 |
||
792 |
checkIOOBE(() -> { |
|
793 |
int o = (int) vh.getAndAdd(array, ci, 3); |
|
794 |
}); |
|
795 |
||
796 |
checkIOOBE(() -> { |
|
797 |
int o = (int) vh.addAndGet(array, ci, 3); |
|
798 |
}); |
|
799 |
} |
|
800 |
} |
|
801 |
} |
|
802 |