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