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