author | psandoz |
Wed, 13 Apr 2016 15:05:48 +0200 | |
changeset 37343 | 35a2231828a7 |
parent 36934 | 590fc47a0aeb |
child 37719 | add11bc0e6e2 |
child 38355 | 674cfd9b90cf |
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 VarHandleTestAccess$Type$ |
|
27 |
* @run testng/othervm -Diters=20000 -XX:TieredStopAtLevel=1 VarHandleTestAccess$Type$ |
|
28 |
* @run testng/othervm -Diters=20000 VarHandleTestAccess$Type$ |
|
29 |
* @run testng/othervm -Diters=20000 -XX:-TieredCompilation VarHandleTestAccess$Type$ |
|
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 VarHandleTestAccess$Type$ extends VarHandleBaseTest { |
|
45 |
static final $type$ static_final_v = $value1$; |
|
46 |
||
47 |
static $type$ static_v; |
|
48 |
||
49 |
final $type$ final_v = $value1$; |
|
50 |
||
51 |
$type$ 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 |
VarHandleTestAccess$Type$.class, "final_v", $type$.class); |
|
67 |
||
68 |
vhField = MethodHandles.lookup().findVarHandle( |
|
69 |
VarHandleTestAccess$Type$.class, "v", $type$.class); |
|
70 |
||
71 |
vhStaticFinalField = MethodHandles.lookup().findStaticVarHandle( |
|
72 |
VarHandleTestAccess$Type$.class, "static_final_v", $type$.class); |
|
73 |
||
74 |
vhStaticField = MethodHandles.lookup().findStaticVarHandle( |
|
75 |
VarHandleTestAccess$Type$.class, "static_v", $type$.class); |
|
76 |
||
77 |
vhArray = MethodHandles.arrayElementVarHandle($type$[].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 |
|
102 |
#if[CAS] |
|
37343
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36934
diff
changeset
|
103 |
assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_SET)); |
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_VOLATILE)); |
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_ACQUIRE)); |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36934
diff
changeset
|
106 |
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
|
107 |
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
|
108 |
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
|
109 |
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
|
110 |
assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_SET)); |
36934 | 111 |
#else[CAS] |
37343
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36934
diff
changeset
|
112 |
assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_SET)); |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36934
diff
changeset
|
113 |
assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_VOLATILE)); |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36934
diff
changeset
|
114 |
assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_ACQUIRE)); |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36934
diff
changeset
|
115 |
assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_RELEASE)); |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36934
diff
changeset
|
116 |
assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET)); |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36934
diff
changeset
|
117 |
assertFalse(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
|
118 |
assertFalse(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
|
119 |
assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_SET)); |
36934 | 120 |
#end[CAS] |
121 |
||
122 |
#if[AtomicAdd] |
|
37343
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36934
diff
changeset
|
123 |
assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_ADD)); |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36934
diff
changeset
|
124 |
assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.ADD_AND_GET)); |
36934 | 125 |
#else[AtomicAdd] |
37343
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36934
diff
changeset
|
126 |
assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_ADD)); |
35a2231828a7
8151705: VarHandle.AccessMode enum names should conform to code style
psandoz
parents:
36934
diff
changeset
|
127 |
assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.ADD_AND_GET)); |
36934 | 128 |
#end[AtomicAdd] |
129 |
} |
|
130 |
||
131 |
||
132 |
@DataProvider |
|
133 |
public Object[][] typesProvider() throws Exception { |
|
134 |
List<Object[]> types = new ArrayList<>(); |
|
135 |
types.add(new Object[] {vhField, Arrays.asList(VarHandleTestAccess$Type$.class)}); |
|
136 |
types.add(new Object[] {vhStaticField, Arrays.asList()}); |
|
137 |
types.add(new Object[] {vhArray, Arrays.asList($type$[].class, int.class)}); |
|
138 |
||
139 |
return types.stream().toArray(Object[][]::new); |
|
140 |
} |
|
141 |
||
142 |
@Test(dataProvider = "typesProvider") |
|
143 |
public void testTypes(VarHandle vh, List<Class<?>> pts) { |
|
144 |
assertEquals(vh.varType(), $type$.class); |
|
145 |
||
146 |
assertEquals(vh.coordinateTypes(), pts); |
|
147 |
||
148 |
testTypes(vh); |
|
149 |
} |
|
150 |
||
151 |
||
152 |
@Test |
|
153 |
public void testLookupInstanceToStatic() { |
|
154 |
checkIAE("Lookup of static final field to instance final field", () -> { |
|
155 |
MethodHandles.lookup().findStaticVarHandle( |
|
156 |
VarHandleTestAccess$Type$.class, "final_v", $type$.class); |
|
157 |
}); |
|
158 |
||
159 |
checkIAE("Lookup of static field to instance field", () -> { |
|
160 |
MethodHandles.lookup().findStaticVarHandle( |
|
161 |
VarHandleTestAccess$Type$.class, "v", $type$.class); |
|
162 |
}); |
|
163 |
} |
|
164 |
||
165 |
@Test |
|
166 |
public void testLookupStaticToInstance() { |
|
167 |
checkIAE("Lookup of instance final field to static final field", () -> { |
|
168 |
MethodHandles.lookup().findVarHandle( |
|
169 |
VarHandleTestAccess$Type$.class, "static_final_v", $type$.class); |
|
170 |
}); |
|
171 |
||
172 |
checkIAE("Lookup of instance field to static field", () -> { |
|
173 |
vhStaticField = MethodHandles.lookup().findVarHandle( |
|
174 |
VarHandleTestAccess$Type$.class, "static_v", $type$.class); |
|
175 |
}); |
|
176 |
} |
|
177 |
||
178 |
||
179 |
@DataProvider |
|
180 |
public Object[][] accessTestCaseProvider() throws Exception { |
|
181 |
List<AccessTestCase<?>> cases = new ArrayList<>(); |
|
182 |
||
183 |
cases.add(new VarHandleAccessTestCase("Instance final field", |
|
184 |
vhFinalField, vh -> testInstanceFinalField(this, vh))); |
|
185 |
cases.add(new VarHandleAccessTestCase("Instance final field unsupported", |
|
186 |
vhFinalField, vh -> testInstanceFinalFieldUnsupported(this, vh), |
|
187 |
false)); |
|
188 |
||
189 |
cases.add(new VarHandleAccessTestCase("Static final field", |
|
190 |
vhStaticFinalField, VarHandleTestAccess$Type$::testStaticFinalField)); |
|
191 |
cases.add(new VarHandleAccessTestCase("Static final field unsupported", |
|
192 |
vhStaticFinalField, VarHandleTestAccess$Type$::testStaticFinalFieldUnsupported, |
|
193 |
false)); |
|
194 |
||
195 |
cases.add(new VarHandleAccessTestCase("Instance field", |
|
196 |
vhField, vh -> testInstanceField(this, vh))); |
|
197 |
cases.add(new VarHandleAccessTestCase("Instance field unsupported", |
|
198 |
vhField, vh -> testInstanceFieldUnsupported(this, vh), |
|
199 |
false)); |
|
200 |
||
201 |
cases.add(new VarHandleAccessTestCase("Static field", |
|
202 |
vhStaticField, VarHandleTestAccess$Type$::testStaticField)); |
|
203 |
cases.add(new VarHandleAccessTestCase("Static field unsupported", |
|
204 |
vhStaticField, VarHandleTestAccess$Type$::testStaticFieldUnsupported, |
|
205 |
false)); |
|
206 |
||
207 |
cases.add(new VarHandleAccessTestCase("Array", |
|
208 |
vhArray, VarHandleTestAccess$Type$::testArray)); |
|
209 |
cases.add(new VarHandleAccessTestCase("Array unsupported", |
|
210 |
vhArray, VarHandleTestAccess$Type$::testArrayUnsupported, |
|
211 |
false)); |
|
212 |
cases.add(new VarHandleAccessTestCase("Array index out of bounds", |
|
213 |
vhArray, VarHandleTestAccess$Type$::testArrayIndexOutOfBounds, |
|
214 |
false)); |
|
215 |
||
216 |
// Work around issue with jtreg summary reporting which truncates |
|
217 |
// the String result of Object.toString to 30 characters, hence |
|
218 |
// the first dummy argument |
|
219 |
return cases.stream().map(tc -> new Object[]{tc.toString(), tc}).toArray(Object[][]::new); |
|
220 |
} |
|
221 |
||
222 |
@Test(dataProvider = "accessTestCaseProvider") |
|
223 |
public <T> void testAccess(String desc, AccessTestCase<T> atc) throws Throwable { |
|
224 |
T t = atc.get(); |
|
225 |
int iters = atc.requiresLoop() ? ITERS : 1; |
|
226 |
for (int c = 0; c < iters; c++) { |
|
227 |
atc.testAccess(t); |
|
228 |
} |
|
229 |
} |
|
230 |
||
231 |
||
232 |
||
233 |
||
234 |
static void testInstanceFinalField(VarHandleTestAccess$Type$ recv, VarHandle vh) { |
|
235 |
// Plain |
|
236 |
{ |
|
237 |
$type$ x = ($type$) vh.get(recv); |
|
238 |
assertEquals(x, $value1$, "get $type$ value"); |
|
239 |
} |
|
240 |
||
241 |
||
242 |
// Volatile |
|
243 |
{ |
|
244 |
$type$ x = ($type$) vh.getVolatile(recv); |
|
245 |
assertEquals(x, $value1$, "getVolatile $type$ value"); |
|
246 |
} |
|
247 |
||
248 |
// Lazy |
|
249 |
{ |
|
250 |
$type$ x = ($type$) vh.getAcquire(recv); |
|
251 |
assertEquals(x, $value1$, "getRelease $type$ value"); |
|
252 |
} |
|
253 |
||
254 |
// Opaque |
|
255 |
{ |
|
256 |
$type$ x = ($type$) vh.getOpaque(recv); |
|
257 |
assertEquals(x, $value1$, "getOpaque $type$ value"); |
|
258 |
} |
|
259 |
} |
|
260 |
||
261 |
static void testInstanceFinalFieldUnsupported(VarHandleTestAccess$Type$ recv, VarHandle vh) { |
|
262 |
checkUOE(() -> { |
|
263 |
vh.set(recv, $value2$); |
|
264 |
}); |
|
265 |
||
266 |
checkUOE(() -> { |
|
267 |
vh.setVolatile(recv, $value2$); |
|
268 |
}); |
|
269 |
||
270 |
checkUOE(() -> { |
|
271 |
vh.setRelease(recv, $value2$); |
|
272 |
}); |
|
273 |
||
274 |
checkUOE(() -> { |
|
275 |
vh.setOpaque(recv, $value2$); |
|
276 |
}); |
|
277 |
||
278 |
#if[!CAS] |
|
279 |
checkUOE(() -> { |
|
280 |
boolean r = vh.compareAndSet(recv, $value1$, $value2$); |
|
281 |
}); |
|
282 |
||
283 |
checkUOE(() -> { |
|
284 |
$type$ r = ($type$) vh.compareAndExchangeVolatile(recv, $value1$, $value2$); |
|
285 |
}); |
|
286 |
||
287 |
checkUOE(() -> { |
|
288 |
$type$ r = ($type$) vh.compareAndExchangeAcquire(recv, $value1$, $value2$); |
|
289 |
}); |
|
290 |
||
291 |
checkUOE(() -> { |
|
292 |
$type$ r = ($type$) vh.compareAndExchangeRelease(recv, $value1$, $value2$); |
|
293 |
}); |
|
294 |
||
295 |
checkUOE(() -> { |
|
296 |
boolean r = vh.weakCompareAndSet(recv, $value1$, $value2$); |
|
297 |
}); |
|
298 |
||
299 |
checkUOE(() -> { |
|
300 |
boolean r = vh.weakCompareAndSetAcquire(recv, $value1$, $value2$); |
|
301 |
}); |
|
302 |
||
303 |
checkUOE(() -> { |
|
304 |
boolean r = vh.weakCompareAndSetRelease(recv, $value1$, $value2$); |
|
305 |
}); |
|
306 |
#end[CAS] |
|
307 |
||
308 |
#if[!AtomicAdd] |
|
309 |
checkUOE(() -> { |
|
310 |
$type$ o = ($type$) vh.getAndAdd(recv, $value1$); |
|
311 |
}); |
|
312 |
||
313 |
checkUOE(() -> { |
|
314 |
$type$ o = ($type$) vh.addAndGet(recv, $value1$); |
|
315 |
}); |
|
316 |
#end[AtomicAdd] |
|
317 |
} |
|
318 |
||
319 |
||
320 |
static void testStaticFinalField(VarHandle vh) { |
|
321 |
// Plain |
|
322 |
{ |
|
323 |
$type$ x = ($type$) vh.get(); |
|
324 |
assertEquals(x, $value1$, "get $type$ value"); |
|
325 |
} |
|
326 |
||
327 |
||
328 |
// Volatile |
|
329 |
{ |
|
330 |
$type$ x = ($type$) vh.getVolatile(); |
|
331 |
assertEquals(x, $value1$, "getVolatile $type$ value"); |
|
332 |
} |
|
333 |
||
334 |
// Lazy |
|
335 |
{ |
|
336 |
$type$ x = ($type$) vh.getAcquire(); |
|
337 |
assertEquals(x, $value1$, "getRelease $type$ value"); |
|
338 |
} |
|
339 |
||
340 |
// Opaque |
|
341 |
{ |
|
342 |
$type$ x = ($type$) vh.getOpaque(); |
|
343 |
assertEquals(x, $value1$, "getOpaque $type$ value"); |
|
344 |
} |
|
345 |
} |
|
346 |
||
347 |
static void testStaticFinalFieldUnsupported(VarHandle vh) { |
|
348 |
checkUOE(() -> { |
|
349 |
vh.set($value2$); |
|
350 |
}); |
|
351 |
||
352 |
checkUOE(() -> { |
|
353 |
vh.setVolatile($value2$); |
|
354 |
}); |
|
355 |
||
356 |
checkUOE(() -> { |
|
357 |
vh.setRelease($value2$); |
|
358 |
}); |
|
359 |
||
360 |
checkUOE(() -> { |
|
361 |
vh.setOpaque($value2$); |
|
362 |
}); |
|
363 |
||
364 |
#if[!CAS] |
|
365 |
checkUOE(() -> { |
|
366 |
boolean r = vh.compareAndSet($value1$, $value2$); |
|
367 |
}); |
|
368 |
||
369 |
checkUOE(() -> { |
|
370 |
$type$ r = ($type$) vh.compareAndExchangeVolatile($value1$, $value2$); |
|
371 |
}); |
|
372 |
||
373 |
checkUOE(() -> { |
|
374 |
$type$ r = ($type$) vh.compareAndExchangeAcquire($value1$, $value2$); |
|
375 |
}); |
|
376 |
||
377 |
checkUOE(() -> { |
|
378 |
$type$ r = ($type$) vh.compareAndExchangeRelease($value1$, $value2$); |
|
379 |
}); |
|
380 |
||
381 |
checkUOE(() -> { |
|
382 |
boolean r = vh.weakCompareAndSet($value1$, $value2$); |
|
383 |
}); |
|
384 |
||
385 |
checkUOE(() -> { |
|
386 |
boolean r = vh.weakCompareAndSetAcquire($value1$, $value2$); |
|
387 |
}); |
|
388 |
||
389 |
checkUOE(() -> { |
|
390 |
boolean r = vh.weakCompareAndSetRelease($value1$, $value2$); |
|
391 |
}); |
|
392 |
#end[CAS] |
|
393 |
||
394 |
#if[!AtomicAdd] |
|
395 |
checkUOE(() -> { |
|
396 |
$type$ o = ($type$) vh.getAndAdd($value1$); |
|
397 |
}); |
|
398 |
||
399 |
checkUOE(() -> { |
|
400 |
$type$ o = ($type$) vh.addAndGet($value1$); |
|
401 |
}); |
|
402 |
#end[AtomicAdd] |
|
403 |
} |
|
404 |
||
405 |
||
406 |
static void testInstanceField(VarHandleTestAccess$Type$ recv, VarHandle vh) { |
|
407 |
// Plain |
|
408 |
{ |
|
409 |
vh.set(recv, $value1$); |
|
410 |
$type$ x = ($type$) vh.get(recv); |
|
411 |
assertEquals(x, $value1$, "set $type$ value"); |
|
412 |
} |
|
413 |
||
414 |
||
415 |
// Volatile |
|
416 |
{ |
|
417 |
vh.setVolatile(recv, $value2$); |
|
418 |
$type$ x = ($type$) vh.getVolatile(recv); |
|
419 |
assertEquals(x, $value2$, "setVolatile $type$ value"); |
|
420 |
} |
|
421 |
||
422 |
// Lazy |
|
423 |
{ |
|
424 |
vh.setRelease(recv, $value1$); |
|
425 |
$type$ x = ($type$) vh.getAcquire(recv); |
|
426 |
assertEquals(x, $value1$, "setRelease $type$ value"); |
|
427 |
} |
|
428 |
||
429 |
// Opaque |
|
430 |
{ |
|
431 |
vh.setOpaque(recv, $value2$); |
|
432 |
$type$ x = ($type$) vh.getOpaque(recv); |
|
433 |
assertEquals(x, $value2$, "setOpaque $type$ value"); |
|
434 |
} |
|
435 |
||
436 |
#if[CAS] |
|
437 |
vh.set(recv, $value1$); |
|
438 |
||
439 |
// Compare |
|
440 |
{ |
|
441 |
boolean r = vh.compareAndSet(recv, $value1$, $value2$); |
|
442 |
assertEquals(r, true, "success compareAndSet $type$"); |
|
443 |
$type$ x = ($type$) vh.get(recv); |
|
444 |
assertEquals(x, $value2$, "success compareAndSet $type$ value"); |
|
445 |
} |
|
446 |
||
447 |
{ |
|
448 |
boolean r = vh.compareAndSet(recv, $value1$, $value3$); |
|
449 |
assertEquals(r, false, "failing compareAndSet $type$"); |
|
450 |
$type$ x = ($type$) vh.get(recv); |
|
451 |
assertEquals(x, $value2$, "failing compareAndSet $type$ value"); |
|
452 |
} |
|
453 |
||
454 |
{ |
|
455 |
$type$ r = ($type$) vh.compareAndExchangeVolatile(recv, $value2$, $value1$); |
|
456 |
assertEquals(r, $value2$, "success compareAndExchangeVolatile $type$"); |
|
457 |
$type$ x = ($type$) vh.get(recv); |
|
458 |
assertEquals(x, $value1$, "success compareAndExchangeVolatile $type$ value"); |
|
459 |
} |
|
460 |
||
461 |
{ |
|
462 |
$type$ r = ($type$) vh.compareAndExchangeVolatile(recv, $value2$, $value3$); |
|
463 |
assertEquals(r, $value1$, "failing compareAndExchangeVolatile $type$"); |
|
464 |
$type$ x = ($type$) vh.get(recv); |
|
465 |
assertEquals(x, $value1$, "failing compareAndExchangeVolatile $type$ value"); |
|
466 |
} |
|
467 |
||
468 |
{ |
|
469 |
$type$ r = ($type$) vh.compareAndExchangeAcquire(recv, $value1$, $value2$); |
|
470 |
assertEquals(r, $value1$, "success compareAndExchangeAcquire $type$"); |
|
471 |
$type$ x = ($type$) vh.get(recv); |
|
472 |
assertEquals(x, $value2$, "success compareAndExchangeAcquire $type$ value"); |
|
473 |
} |
|
474 |
||
475 |
{ |
|
476 |
$type$ r = ($type$) vh.compareAndExchangeAcquire(recv, $value1$, $value3$); |
|
477 |
assertEquals(r, $value2$, "failing compareAndExchangeAcquire $type$"); |
|
478 |
$type$ x = ($type$) vh.get(recv); |
|
479 |
assertEquals(x, $value2$, "failing compareAndExchangeAcquire $type$ value"); |
|
480 |
} |
|
481 |
||
482 |
{ |
|
483 |
$type$ r = ($type$) vh.compareAndExchangeRelease(recv, $value2$, $value1$); |
|
484 |
assertEquals(r, $value2$, "success compareAndExchangeRelease $type$"); |
|
485 |
$type$ x = ($type$) vh.get(recv); |
|
486 |
assertEquals(x, $value1$, "success compareAndExchangeRelease $type$ value"); |
|
487 |
} |
|
488 |
||
489 |
{ |
|
490 |
$type$ r = ($type$) vh.compareAndExchangeRelease(recv, $value2$, $value3$); |
|
491 |
assertEquals(r, $value1$, "failing compareAndExchangeRelease $type$"); |
|
492 |
$type$ x = ($type$) vh.get(recv); |
|
493 |
assertEquals(x, $value1$, "failing compareAndExchangeRelease $type$ value"); |
|
494 |
} |
|
495 |
||
496 |
{ |
|
497 |
boolean r = vh.weakCompareAndSet(recv, $value1$, $value2$); |
|
498 |
assertEquals(r, true, "weakCompareAndSet $type$"); |
|
499 |
$type$ x = ($type$) vh.get(recv); |
|
500 |
assertEquals(x, $value2$, "weakCompareAndSet $type$ value"); |
|
501 |
} |
|
502 |
||
503 |
{ |
|
504 |
boolean r = vh.weakCompareAndSetAcquire(recv, $value2$, $value1$); |
|
505 |
assertEquals(r, true, "weakCompareAndSetAcquire $type$"); |
|
506 |
$type$ x = ($type$) vh.get(recv); |
|
507 |
assertEquals(x, $value1$, "weakCompareAndSetAcquire $type$"); |
|
508 |
} |
|
509 |
||
510 |
{ |
|
511 |
boolean r = vh.weakCompareAndSetRelease(recv, $value1$, $value2$); |
|
512 |
assertEquals(r, true, "weakCompareAndSetRelease $type$"); |
|
513 |
$type$ x = ($type$) vh.get(recv); |
|
514 |
assertEquals(x, $value2$, "weakCompareAndSetRelease $type$"); |
|
515 |
} |
|
516 |
||
517 |
// Compare set and get |
|
518 |
{ |
|
519 |
$type$ o = ($type$) vh.getAndSet(recv, $value1$); |
|
520 |
assertEquals(o, $value2$, "getAndSet $type$"); |
|
521 |
$type$ x = ($type$) vh.get(recv); |
|
522 |
assertEquals(x, $value1$, "getAndSet $type$ value"); |
|
523 |
} |
|
524 |
#end[CAS] |
|
525 |
||
526 |
#if[AtomicAdd] |
|
527 |
vh.set(recv, $value1$); |
|
528 |
||
529 |
// get and add, add and get |
|
530 |
{ |
|
531 |
$type$ o = ($type$) vh.getAndAdd(recv, $value3$); |
|
532 |
assertEquals(o, $value1$, "getAndAdd $type$"); |
|
533 |
$type$ c = ($type$) vh.addAndGet(recv, $value3$); |
|
534 |
assertEquals(c, $value1$ + $value3$ + $value3$, "getAndAdd $type$ value"); |
|
535 |
} |
|
536 |
#end[AtomicAdd] |
|
537 |
} |
|
538 |
||
539 |
static void testInstanceFieldUnsupported(VarHandleTestAccess$Type$ recv, VarHandle vh) { |
|
540 |
#if[!CAS] |
|
541 |
checkUOE(() -> { |
|
542 |
boolean r = vh.compareAndSet(recv, $value1$, $value2$); |
|
543 |
}); |
|
544 |
||
545 |
checkUOE(() -> { |
|
546 |
$type$ r = ($type$) vh.compareAndExchangeVolatile(recv, $value1$, $value2$); |
|
547 |
}); |
|
548 |
||
549 |
checkUOE(() -> { |
|
550 |
$type$ r = ($type$) vh.compareAndExchangeAcquire(recv, $value1$, $value2$); |
|
551 |
}); |
|
552 |
||
553 |
checkUOE(() -> { |
|
554 |
$type$ r = ($type$) vh.compareAndExchangeRelease(recv, $value1$, $value2$); |
|
555 |
}); |
|
556 |
||
557 |
checkUOE(() -> { |
|
558 |
boolean r = vh.weakCompareAndSet(recv, $value1$, $value2$); |
|
559 |
}); |
|
560 |
||
561 |
checkUOE(() -> { |
|
562 |
boolean r = vh.weakCompareAndSetAcquire(recv, $value1$, $value2$); |
|
563 |
}); |
|
564 |
||
565 |
checkUOE(() -> { |
|
566 |
boolean r = vh.weakCompareAndSetRelease(recv, $value1$, $value2$); |
|
567 |
}); |
|
568 |
#end[CAS] |
|
569 |
||
570 |
#if[!AtomicAdd] |
|
571 |
checkUOE(() -> { |
|
572 |
$type$ o = ($type$) vh.getAndAdd(recv, $value1$); |
|
573 |
}); |
|
574 |
||
575 |
checkUOE(() -> { |
|
576 |
$type$ o = ($type$) vh.addAndGet(recv, $value1$); |
|
577 |
}); |
|
578 |
#end[AtomicAdd] |
|
579 |
} |
|
580 |
||
581 |
||
582 |
static void testStaticField(VarHandle vh) { |
|
583 |
// Plain |
|
584 |
{ |
|
585 |
vh.set($value1$); |
|
586 |
$type$ x = ($type$) vh.get(); |
|
587 |
assertEquals(x, $value1$, "set $type$ value"); |
|
588 |
} |
|
589 |
||
590 |
||
591 |
// Volatile |
|
592 |
{ |
|
593 |
vh.setVolatile($value2$); |
|
594 |
$type$ x = ($type$) vh.getVolatile(); |
|
595 |
assertEquals(x, $value2$, "setVolatile $type$ value"); |
|
596 |
} |
|
597 |
||
598 |
// Lazy |
|
599 |
{ |
|
600 |
vh.setRelease($value1$); |
|
601 |
$type$ x = ($type$) vh.getAcquire(); |
|
602 |
assertEquals(x, $value1$, "setRelease $type$ value"); |
|
603 |
} |
|
604 |
||
605 |
// Opaque |
|
606 |
{ |
|
607 |
vh.setOpaque($value2$); |
|
608 |
$type$ x = ($type$) vh.getOpaque(); |
|
609 |
assertEquals(x, $value2$, "setOpaque $type$ value"); |
|
610 |
} |
|
611 |
||
612 |
#if[CAS] |
|
613 |
vh.set($value1$); |
|
614 |
||
615 |
// Compare |
|
616 |
{ |
|
617 |
boolean r = vh.compareAndSet($value1$, $value2$); |
|
618 |
assertEquals(r, true, "success compareAndSet $type$"); |
|
619 |
$type$ x = ($type$) vh.get(); |
|
620 |
assertEquals(x, $value2$, "success compareAndSet $type$ value"); |
|
621 |
} |
|
622 |
||
623 |
{ |
|
624 |
boolean r = vh.compareAndSet($value1$, $value3$); |
|
625 |
assertEquals(r, false, "failing compareAndSet $type$"); |
|
626 |
$type$ x = ($type$) vh.get(); |
|
627 |
assertEquals(x, $value2$, "failing compareAndSet $type$ value"); |
|
628 |
} |
|
629 |
||
630 |
{ |
|
631 |
$type$ r = ($type$) vh.compareAndExchangeVolatile($value2$, $value1$); |
|
632 |
assertEquals(r, $value2$, "success compareAndExchangeVolatile $type$"); |
|
633 |
$type$ x = ($type$) vh.get(); |
|
634 |
assertEquals(x, $value1$, "success compareAndExchangeVolatile $type$ value"); |
|
635 |
} |
|
636 |
||
637 |
{ |
|
638 |
$type$ r = ($type$) vh.compareAndExchangeVolatile($value2$, $value3$); |
|
639 |
assertEquals(r, $value1$, "failing compareAndExchangeVolatile $type$"); |
|
640 |
$type$ x = ($type$) vh.get(); |
|
641 |
assertEquals(x, $value1$, "failing compareAndExchangeVolatile $type$ value"); |
|
642 |
} |
|
643 |
||
644 |
{ |
|
645 |
$type$ r = ($type$) vh.compareAndExchangeAcquire($value1$, $value2$); |
|
646 |
assertEquals(r, $value1$, "success compareAndExchangeAcquire $type$"); |
|
647 |
$type$ x = ($type$) vh.get(); |
|
648 |
assertEquals(x, $value2$, "success compareAndExchangeAcquire $type$ value"); |
|
649 |
} |
|
650 |
||
651 |
{ |
|
652 |
$type$ r = ($type$) vh.compareAndExchangeAcquire($value1$, $value3$); |
|
653 |
assertEquals(r, $value2$, "failing compareAndExchangeAcquire $type$"); |
|
654 |
$type$ x = ($type$) vh.get(); |
|
655 |
assertEquals(x, $value2$, "failing compareAndExchangeAcquire $type$ value"); |
|
656 |
} |
|
657 |
||
658 |
{ |
|
659 |
$type$ r = ($type$) vh.compareAndExchangeRelease($value2$, $value1$); |
|
660 |
assertEquals(r, $value2$, "success compareAndExchangeRelease $type$"); |
|
661 |
$type$ x = ($type$) vh.get(); |
|
662 |
assertEquals(x, $value1$, "success compareAndExchangeRelease $type$ value"); |
|
663 |
} |
|
664 |
||
665 |
{ |
|
666 |
$type$ r = ($type$) vh.compareAndExchangeRelease($value2$, $value3$); |
|
667 |
assertEquals(r, $value1$, "failing compareAndExchangeRelease $type$"); |
|
668 |
$type$ x = ($type$) vh.get(); |
|
669 |
assertEquals(x, $value1$, "failing compareAndExchangeRelease $type$ value"); |
|
670 |
} |
|
671 |
||
672 |
{ |
|
673 |
boolean r = (boolean) vh.weakCompareAndSet($value1$, $value2$); |
|
674 |
assertEquals(r, true, "weakCompareAndSet $type$"); |
|
675 |
$type$ x = ($type$) vh.get(); |
|
676 |
assertEquals(x, $value2$, "weakCompareAndSet $type$ value"); |
|
677 |
} |
|
678 |
||
679 |
{ |
|
680 |
boolean r = (boolean) vh.weakCompareAndSetAcquire($value2$, $value1$); |
|
681 |
assertEquals(r, true, "weakCompareAndSetAcquire $type$"); |
|
682 |
$type$ x = ($type$) vh.get(); |
|
683 |
assertEquals(x, $value1$, "weakCompareAndSetAcquire $type$"); |
|
684 |
} |
|
685 |
||
686 |
{ |
|
687 |
boolean r = (boolean) vh.weakCompareAndSetRelease( $value1$, $value2$); |
|
688 |
assertEquals(r, true, "weakCompareAndSetRelease $type$"); |
|
689 |
$type$ x = ($type$) vh.get(); |
|
690 |
assertEquals(x, $value2$, "weakCompareAndSetRelease $type$"); |
|
691 |
} |
|
692 |
||
693 |
// Compare set and get |
|
694 |
{ |
|
695 |
$type$ o = ($type$) vh.getAndSet( $value1$); |
|
696 |
assertEquals(o, $value2$, "getAndSet $type$"); |
|
697 |
$type$ x = ($type$) vh.get(); |
|
698 |
assertEquals(x, $value1$, "getAndSet $type$ value"); |
|
699 |
} |
|
700 |
#end[CAS] |
|
701 |
||
702 |
#if[AtomicAdd] |
|
703 |
vh.set($value1$); |
|
704 |
||
705 |
// get and add, add and get |
|
706 |
{ |
|
707 |
$type$ o = ($type$) vh.getAndAdd( $value3$); |
|
708 |
assertEquals(o, $value1$, "getAndAdd $type$"); |
|
709 |
$type$ c = ($type$) vh.addAndGet($value3$); |
|
710 |
assertEquals(c, $value1$ + $value3$ + $value3$, "getAndAdd $type$ value"); |
|
711 |
} |
|
712 |
#end[AtomicAdd] |
|
713 |
} |
|
714 |
||
715 |
static void testStaticFieldUnsupported(VarHandle vh) { |
|
716 |
#if[!CAS] |
|
717 |
checkUOE(() -> { |
|
718 |
boolean r = vh.compareAndSet($value1$, $value2$); |
|
719 |
}); |
|
720 |
||
721 |
checkUOE(() -> { |
|
722 |
$type$ r = ($type$) vh.compareAndExchangeVolatile($value1$, $value2$); |
|
723 |
}); |
|
724 |
||
725 |
checkUOE(() -> { |
|
726 |
$type$ r = ($type$) vh.compareAndExchangeAcquire($value1$, $value2$); |
|
727 |
}); |
|
728 |
||
729 |
checkUOE(() -> { |
|
730 |
$type$ r = ($type$) vh.compareAndExchangeRelease($value1$, $value2$); |
|
731 |
}); |
|
732 |
||
733 |
checkUOE(() -> { |
|
734 |
boolean r = vh.weakCompareAndSet($value1$, $value2$); |
|
735 |
}); |
|
736 |
||
737 |
checkUOE(() -> { |
|
738 |
boolean r = vh.weakCompareAndSetAcquire($value1$, $value2$); |
|
739 |
}); |
|
740 |
||
741 |
checkUOE(() -> { |
|
742 |
boolean r = vh.weakCompareAndSetRelease($value1$, $value2$); |
|
743 |
}); |
|
744 |
#end[CAS] |
|
745 |
||
746 |
#if[!AtomicAdd] |
|
747 |
checkUOE(() -> { |
|
748 |
$type$ o = ($type$) vh.getAndAdd($value1$); |
|
749 |
}); |
|
750 |
||
751 |
checkUOE(() -> { |
|
752 |
$type$ o = ($type$) vh.addAndGet($value1$); |
|
753 |
}); |
|
754 |
#end[AtomicAdd] |
|
755 |
} |
|
756 |
||
757 |
||
758 |
static void testArray(VarHandle vh) { |
|
759 |
$type$[] array = new $type$[10]; |
|
760 |
||
761 |
for (int i = 0; i < array.length; i++) { |
|
762 |
// Plain |
|
763 |
{ |
|
764 |
vh.set(array, i, $value1$); |
|
765 |
$type$ x = ($type$) vh.get(array, i); |
|
766 |
assertEquals(x, $value1$, "get $type$ value"); |
|
767 |
} |
|
768 |
||
769 |
||
770 |
// Volatile |
|
771 |
{ |
|
772 |
vh.setVolatile(array, i, $value2$); |
|
773 |
$type$ x = ($type$) vh.getVolatile(array, i); |
|
774 |
assertEquals(x, $value2$, "setVolatile $type$ value"); |
|
775 |
} |
|
776 |
||
777 |
// Lazy |
|
778 |
{ |
|
779 |
vh.setRelease(array, i, $value1$); |
|
780 |
$type$ x = ($type$) vh.getAcquire(array, i); |
|
781 |
assertEquals(x, $value1$, "setRelease $type$ value"); |
|
782 |
} |
|
783 |
||
784 |
// Opaque |
|
785 |
{ |
|
786 |
vh.setOpaque(array, i, $value2$); |
|
787 |
$type$ x = ($type$) vh.getOpaque(array, i); |
|
788 |
assertEquals(x, $value2$, "setOpaque $type$ value"); |
|
789 |
} |
|
790 |
||
791 |
#if[CAS] |
|
792 |
vh.set(array, i, $value1$); |
|
793 |
||
794 |
// Compare |
|
795 |
{ |
|
796 |
boolean r = vh.compareAndSet(array, i, $value1$, $value2$); |
|
797 |
assertEquals(r, true, "success compareAndSet $type$"); |
|
798 |
$type$ x = ($type$) vh.get(array, i); |
|
799 |
assertEquals(x, $value2$, "success compareAndSet $type$ value"); |
|
800 |
} |
|
801 |
||
802 |
{ |
|
803 |
boolean r = vh.compareAndSet(array, i, $value1$, $value3$); |
|
804 |
assertEquals(r, false, "failing compareAndSet $type$"); |
|
805 |
$type$ x = ($type$) vh.get(array, i); |
|
806 |
assertEquals(x, $value2$, "failing compareAndSet $type$ value"); |
|
807 |
} |
|
808 |
||
809 |
{ |
|
810 |
$type$ r = ($type$) vh.compareAndExchangeVolatile(array, i, $value2$, $value1$); |
|
811 |
assertEquals(r, $value2$, "success compareAndExchangeVolatile $type$"); |
|
812 |
$type$ x = ($type$) vh.get(array, i); |
|
813 |
assertEquals(x, $value1$, "success compareAndExchangeVolatile $type$ value"); |
|
814 |
} |
|
815 |
||
816 |
{ |
|
817 |
$type$ r = ($type$) vh.compareAndExchangeVolatile(array, i, $value2$, $value3$); |
|
818 |
assertEquals(r, $value1$, "failing compareAndExchangeVolatile $type$"); |
|
819 |
$type$ x = ($type$) vh.get(array, i); |
|
820 |
assertEquals(x, $value1$, "failing compareAndExchangeVolatile $type$ value"); |
|
821 |
} |
|
822 |
||
823 |
{ |
|
824 |
$type$ r = ($type$) vh.compareAndExchangeAcquire(array, i, $value1$, $value2$); |
|
825 |
assertEquals(r, $value1$, "success compareAndExchangeAcquire $type$"); |
|
826 |
$type$ x = ($type$) vh.get(array, i); |
|
827 |
assertEquals(x, $value2$, "success compareAndExchangeAcquire $type$ value"); |
|
828 |
} |
|
829 |
||
830 |
{ |
|
831 |
$type$ r = ($type$) vh.compareAndExchangeAcquire(array, i, $value1$, $value3$); |
|
832 |
assertEquals(r, $value2$, "failing compareAndExchangeAcquire $type$"); |
|
833 |
$type$ x = ($type$) vh.get(array, i); |
|
834 |
assertEquals(x, $value2$, "failing compareAndExchangeAcquire $type$ value"); |
|
835 |
} |
|
836 |
||
837 |
{ |
|
838 |
$type$ r = ($type$) vh.compareAndExchangeRelease(array, i, $value2$, $value1$); |
|
839 |
assertEquals(r, $value2$, "success compareAndExchangeRelease $type$"); |
|
840 |
$type$ x = ($type$) vh.get(array, i); |
|
841 |
assertEquals(x, $value1$, "success compareAndExchangeRelease $type$ value"); |
|
842 |
} |
|
843 |
||
844 |
{ |
|
845 |
$type$ r = ($type$) vh.compareAndExchangeRelease(array, i, $value2$, $value3$); |
|
846 |
assertEquals(r, $value1$, "failing compareAndExchangeRelease $type$"); |
|
847 |
$type$ x = ($type$) vh.get(array, i); |
|
848 |
assertEquals(x, $value1$, "failing compareAndExchangeRelease $type$ value"); |
|
849 |
} |
|
850 |
||
851 |
{ |
|
852 |
boolean r = vh.weakCompareAndSet(array, i, $value1$, $value2$); |
|
853 |
assertEquals(r, true, "weakCompareAndSet $type$"); |
|
854 |
$type$ x = ($type$) vh.get(array, i); |
|
855 |
assertEquals(x, $value2$, "weakCompareAndSet $type$ value"); |
|
856 |
} |
|
857 |
||
858 |
{ |
|
859 |
boolean r = vh.weakCompareAndSetAcquire(array, i, $value2$, $value1$); |
|
860 |
assertEquals(r, true, "weakCompareAndSetAcquire $type$"); |
|
861 |
$type$ x = ($type$) vh.get(array, i); |
|
862 |
assertEquals(x, $value1$, "weakCompareAndSetAcquire $type$"); |
|
863 |
} |
|
864 |
||
865 |
{ |
|
866 |
boolean r = vh.weakCompareAndSetRelease(array, i, $value1$, $value2$); |
|
867 |
assertEquals(r, true, "weakCompareAndSetRelease $type$"); |
|
868 |
$type$ x = ($type$) vh.get(array, i); |
|
869 |
assertEquals(x, $value2$, "weakCompareAndSetRelease $type$"); |
|
870 |
} |
|
871 |
||
872 |
// Compare set and get |
|
873 |
{ |
|
874 |
$type$ o = ($type$) vh.getAndSet(array, i, $value1$); |
|
875 |
assertEquals(o, $value2$, "getAndSet $type$"); |
|
876 |
$type$ x = ($type$) vh.get(array, i); |
|
877 |
assertEquals(x, $value1$, "getAndSet $type$ value"); |
|
878 |
} |
|
879 |
#end[CAS] |
|
880 |
||
881 |
#if[AtomicAdd] |
|
882 |
vh.set(array, i, $value1$); |
|
883 |
||
884 |
// get and add, add and get |
|
885 |
{ |
|
886 |
$type$ o = ($type$) vh.getAndAdd(array, i, $value3$); |
|
887 |
assertEquals(o, $value1$, "getAndAdd $type$"); |
|
888 |
$type$ c = ($type$) vh.addAndGet(array, i, $value3$); |
|
889 |
assertEquals(c, $value1$ + $value3$ + $value3$, "getAndAdd $type$ value"); |
|
890 |
} |
|
891 |
#end[AtomicAdd] |
|
892 |
} |
|
893 |
} |
|
894 |
||
895 |
static void testArrayUnsupported(VarHandle vh) { |
|
896 |
$type$[] array = new $type$[10]; |
|
897 |
||
898 |
int i = 0; |
|
899 |
#if[!CAS] |
|
900 |
checkUOE(() -> { |
|
901 |
boolean r = vh.compareAndSet(array, i, $value1$, $value2$); |
|
902 |
}); |
|
903 |
||
904 |
checkUOE(() -> { |
|
905 |
$type$ r = ($type$) vh.compareAndExchangeVolatile(array, i, $value1$, $value2$); |
|
906 |
}); |
|
907 |
||
908 |
checkUOE(() -> { |
|
909 |
$type$ r = ($type$) vh.compareAndExchangeAcquire(array, i, $value1$, $value2$); |
|
910 |
}); |
|
911 |
||
912 |
checkUOE(() -> { |
|
913 |
$type$ r = ($type$) vh.compareAndExchangeRelease(array, i, $value1$, $value2$); |
|
914 |
}); |
|
915 |
||
916 |
checkUOE(() -> { |
|
917 |
boolean r = vh.weakCompareAndSet(array, i, $value1$, $value2$); |
|
918 |
}); |
|
919 |
||
920 |
checkUOE(() -> { |
|
921 |
boolean r = vh.weakCompareAndSetAcquire(array, i, $value1$, $value2$); |
|
922 |
}); |
|
923 |
||
924 |
checkUOE(() -> { |
|
925 |
boolean r = vh.weakCompareAndSetRelease(array, i, $value1$, $value2$); |
|
926 |
}); |
|
927 |
#end[CAS] |
|
928 |
||
929 |
#if[!AtomicAdd] |
|
930 |
checkUOE(() -> { |
|
931 |
$type$ o = ($type$) vh.getAndAdd(array, i, $value1$); |
|
932 |
}); |
|
933 |
||
934 |
checkUOE(() -> { |
|
935 |
$type$ o = ($type$) vh.addAndGet(array, i, $value1$); |
|
936 |
}); |
|
937 |
#end[AtomicAdd] |
|
938 |
} |
|
939 |
||
940 |
static void testArrayIndexOutOfBounds(VarHandle vh) throws Throwable { |
|
941 |
$type$[] array = new $type$[10]; |
|
942 |
||
943 |
for (int i : new int[]{-1, Integer.MIN_VALUE, 10, 11, Integer.MAX_VALUE}) { |
|
944 |
final int ci = i; |
|
945 |
||
946 |
checkIOOBE(() -> { |
|
947 |
$type$ x = ($type$) vh.get(array, ci); |
|
948 |
}); |
|
949 |
||
950 |
checkIOOBE(() -> { |
|
951 |
vh.set(array, ci, $value1$); |
|
952 |
}); |
|
953 |
||
954 |
checkIOOBE(() -> { |
|
955 |
$type$ x = ($type$) vh.getVolatile(array, ci); |
|
956 |
}); |
|
957 |
||
958 |
checkIOOBE(() -> { |
|
959 |
vh.setVolatile(array, ci, $value1$); |
|
960 |
}); |
|
961 |
||
962 |
checkIOOBE(() -> { |
|
963 |
$type$ x = ($type$) vh.getAcquire(array, ci); |
|
964 |
}); |
|
965 |
||
966 |
checkIOOBE(() -> { |
|
967 |
vh.setRelease(array, ci, $value1$); |
|
968 |
}); |
|
969 |
||
970 |
checkIOOBE(() -> { |
|
971 |
$type$ x = ($type$) vh.getOpaque(array, ci); |
|
972 |
}); |
|
973 |
||
974 |
checkIOOBE(() -> { |
|
975 |
vh.setOpaque(array, ci, $value1$); |
|
976 |
}); |
|
977 |
||
978 |
#if[CAS] |
|
979 |
checkIOOBE(() -> { |
|
980 |
boolean r = vh.compareAndSet(array, ci, $value1$, $value2$); |
|
981 |
}); |
|
982 |
||
983 |
checkIOOBE(() -> { |
|
984 |
$type$ r = ($type$) vh.compareAndExchangeVolatile(array, ci, $value2$, $value1$); |
|
985 |
}); |
|
986 |
||
987 |
checkIOOBE(() -> { |
|
988 |
$type$ r = ($type$) vh.compareAndExchangeAcquire(array, ci, $value2$, $value1$); |
|
989 |
}); |
|
990 |
||
991 |
checkIOOBE(() -> { |
|
992 |
$type$ r = ($type$) vh.compareAndExchangeRelease(array, ci, $value2$, $value1$); |
|
993 |
}); |
|
994 |
||
995 |
checkIOOBE(() -> { |
|
996 |
boolean r = vh.weakCompareAndSet(array, ci, $value1$, $value2$); |
|
997 |
}); |
|
998 |
||
999 |
checkIOOBE(() -> { |
|
1000 |
boolean r = vh.weakCompareAndSetAcquire(array, ci, $value1$, $value2$); |
|
1001 |
}); |
|
1002 |
||
1003 |
checkIOOBE(() -> { |
|
1004 |
boolean r = vh.weakCompareAndSetRelease(array, ci, $value1$, $value2$); |
|
1005 |
}); |
|
1006 |
||
1007 |
checkIOOBE(() -> { |
|
1008 |
$type$ o = ($type$) vh.getAndSet(array, ci, $value1$); |
|
1009 |
}); |
|
1010 |
#end[CAS] |
|
1011 |
||
1012 |
#if[AtomicAdd] |
|
1013 |
checkIOOBE(() -> { |
|
1014 |
$type$ o = ($type$) vh.getAndAdd(array, ci, $value3$); |
|
1015 |
}); |
|
1016 |
||
1017 |
checkIOOBE(() -> { |
|
1018 |
$type$ o = ($type$) vh.addAndGet(array, ci, $value3$); |
|
1019 |
}); |
|
1020 |
#end[AtomicAdd] |
|
1021 |
} |
|
1022 |
} |
|
1023 |
} |
|
1024 |