22 */ 23 24 /* 25 * @test 26 * @run testng/othervm -Diters=20000 VarHandleTestMethodHandleAccessShort 27 */ 28 29 import org.testng.annotations.BeforeClass; 30 import org.testng.annotations.DataProvider; 31 import org.testng.annotations.Test; 32 33 import java.lang.invoke.MethodHandles; 34 import java.lang.invoke.VarHandle; 35 import java.util.ArrayList; 36 import java.util.Arrays; 37 import java.util.List; 38 39 import static org.testng.Assert.*; 40 41 public class VarHandleTestMethodHandleAccessShort extends VarHandleBaseTest { 42 static final short static_final_v = (short)1; 43 44 static short static_v; 45 46 final short final_v = (short)1; 47 48 short v; 49 50 VarHandle vhFinalField; 51 52 VarHandle vhField; 53 54 VarHandle vhStaticField; 55 56 VarHandle vhStaticFinalField; 57 58 VarHandle vhArray; 59 60 @BeforeClass 61 public void setup() throws Exception { 62 vhFinalField = MethodHandles.lookup().findVarHandle( 63 VarHandleTestMethodHandleAccessShort.class, "final_v", short.class); 64 65 vhField = MethodHandles.lookup().findVarHandle( 66 VarHandleTestMethodHandleAccessShort.class, "v", short.class); 104 105 // Work around issue with jtreg summary reporting which truncates 106 // the String result of Object.toString to 30 characters, hence 107 // the first dummy argument 108 return cases.stream().map(tc -> new Object[]{tc.toString(), tc}).toArray(Object[][]::new); 109 } 110 111 @Test(dataProvider = "accessTestCaseProvider") 112 public <T> void testAccess(String desc, AccessTestCase<T> atc) throws Throwable { 113 T t = atc.get(); 114 int iters = atc.requiresLoop() ? ITERS : 1; 115 for (int c = 0; c < iters; c++) { 116 atc.testAccess(t); 117 } 118 } 119 120 121 static void testInstanceField(VarHandleTestMethodHandleAccessShort recv, Handles hs) throws Throwable { 122 // Plain 123 { 124 hs.get(TestAccessMode.SET).invokeExact(recv, (short)1); 125 short x = (short) hs.get(TestAccessMode.GET).invokeExact(recv); 126 assertEquals(x, (short)1, "set short value"); 127 } 128 129 130 // Volatile 131 { 132 hs.get(TestAccessMode.SET_VOLATILE).invokeExact(recv, (short)2); 133 short x = (short) hs.get(TestAccessMode.GET_VOLATILE).invokeExact(recv); 134 assertEquals(x, (short)2, "setVolatile short value"); 135 } 136 137 // Lazy 138 { 139 hs.get(TestAccessMode.SET_RELEASE).invokeExact(recv, (short)1); 140 short x = (short) hs.get(TestAccessMode.GET_ACQUIRE).invokeExact(recv); 141 assertEquals(x, (short)1, "setRelease short value"); 142 } 143 144 // Opaque 145 { 146 hs.get(TestAccessMode.SET_OPAQUE).invokeExact(recv, (short)2); 147 short x = (short) hs.get(TestAccessMode.GET_OPAQUE).invokeExact(recv); 148 assertEquals(x, (short)2, "setOpaque short value"); 149 } 150 151 152 } 153 154 static void testInstanceFieldUnsupported(VarHandleTestMethodHandleAccessShort recv, Handles hs) throws Throwable { 155 for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_SET)) { 156 checkUOE(am, () -> { 157 boolean r = (boolean) hs.get(am).invokeExact(recv, (short)1, (short)2); 158 }); 159 } 160 161 for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_EXCHANGE)) { 162 checkUOE(am, () -> { 163 short r = (short) hs.get(am).invokeExact(recv, (short)1, (short)2); 164 }); 165 } 166 167 for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_SET)) { 168 checkUOE(am, () -> { 169 short r = (short) hs.get(am).invokeExact(recv, (short)1); 170 }); 171 } 172 173 for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_ADD)) { 174 checkUOE(am, () -> { 175 short r = (short) hs.get(am).invokeExact(recv, (short)1); 176 }); 177 } 178 } 179 180 181 static void testStaticField(Handles hs) throws Throwable { 182 // Plain 183 { 184 hs.get(TestAccessMode.SET).invokeExact((short)1); 185 short x = (short) hs.get(TestAccessMode.GET).invokeExact(); 186 assertEquals(x, (short)1, "set short value"); 187 } 188 189 190 // Volatile 191 { 192 hs.get(TestAccessMode.SET_VOLATILE).invokeExact((short)2); 193 short x = (short) hs.get(TestAccessMode.GET_VOLATILE).invokeExact(); 194 assertEquals(x, (short)2, "setVolatile short value"); 195 } 196 197 // Lazy 198 { 199 hs.get(TestAccessMode.SET_RELEASE).invokeExact((short)1); 200 short x = (short) hs.get(TestAccessMode.GET_ACQUIRE).invokeExact(); 201 assertEquals(x, (short)1, "setRelease short value"); 202 } 203 204 // Opaque 205 { 206 hs.get(TestAccessMode.SET_OPAQUE).invokeExact((short)2); 207 short x = (short) hs.get(TestAccessMode.GET_OPAQUE).invokeExact(); 208 assertEquals(x, (short)2, "setOpaque short value"); 209 } 210 211 212 } 213 214 static void testStaticFieldUnsupported(Handles hs) throws Throwable { 215 for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_SET)) { 216 checkUOE(am, () -> { 217 boolean r = (boolean) hs.get(am).invokeExact((short)1, (short)2); 218 }); 219 } 220 221 for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_EXCHANGE)) { 222 checkUOE(am, () -> { 223 short r = (short) hs.get(am).invokeExact((short)1, (short)2); 224 }); 225 } 226 227 for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_SET)) { 228 checkUOE(am, () -> { 229 short r = (short) hs.get(am).invokeExact((short)1); 230 }); 231 } 232 233 for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_ADD)) { 234 checkUOE(am, () -> { 235 short r = (short) hs.get(am).invokeExact((short)1); 236 }); 237 } 238 } 239 240 241 static void testArray(Handles hs) throws Throwable { 242 short[] array = new short[10]; 243 244 for (int i = 0; i < array.length; i++) { 245 // Plain 246 { 247 hs.get(TestAccessMode.SET).invokeExact(array, i, (short)1); 248 short x = (short) hs.get(TestAccessMode.GET).invokeExact(array, i); 249 assertEquals(x, (short)1, "get short value"); 250 } 251 252 253 // Volatile 254 { 255 hs.get(TestAccessMode.SET_VOLATILE).invokeExact(array, i, (short)2); 256 short x = (short) hs.get(TestAccessMode.GET_VOLATILE).invokeExact(array, i); 257 assertEquals(x, (short)2, "setVolatile short value"); 258 } 259 260 // Lazy 261 { 262 hs.get(TestAccessMode.SET_RELEASE).invokeExact(array, i, (short)1); 263 short x = (short) hs.get(TestAccessMode.GET_ACQUIRE).invokeExact(array, i); 264 assertEquals(x, (short)1, "setRelease short value"); 265 } 266 267 // Opaque 268 { 269 hs.get(TestAccessMode.SET_OPAQUE).invokeExact(array, i, (short)2); 270 short x = (short) hs.get(TestAccessMode.GET_OPAQUE).invokeExact(array, i); 271 assertEquals(x, (short)2, "setOpaque short value"); 272 } 273 274 275 } 276 } 277 278 static void testArrayUnsupported(Handles hs) throws Throwable { 279 short[] array = new short[10]; 280 281 final int i = 0; 282 for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_SET)) { 283 checkUOE(am, () -> { 284 boolean r = (boolean) hs.get(am).invokeExact(array, i, (short)1, (short)2); 285 }); 286 } 287 288 for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_EXCHANGE)) { 289 checkUOE(am, () -> { 290 short r = (short) hs.get(am).invokeExact(array, i, (short)1, (short)2); 291 }); 292 } 293 294 for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_SET)) { 295 checkUOE(am, () -> { 296 short r = (short) hs.get(am).invokeExact(array, i, (short)1); 297 }); 298 } 299 300 for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_ADD)) { 301 checkUOE(am, () -> { 302 short o = (short) hs.get(am).invokeExact(array, i, (short)1); 303 }); 304 } 305 } 306 307 static void testArrayIndexOutOfBounds(Handles hs) throws Throwable { 308 short[] array = new short[10]; 309 310 for (int i : new int[]{-1, Integer.MIN_VALUE, 10, 11, Integer.MAX_VALUE}) { 311 final int ci = i; 312 313 for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET)) { 314 checkIOOBE(am, () -> { 315 short x = (short) hs.get(am).invokeExact(array, ci); 316 }); 317 } 318 319 for (TestAccessMode am : testAccessModesOfType(TestAccessType.SET)) { 320 checkIOOBE(am, () -> { 321 hs.get(am).invokeExact(array, ci, (short)1); 322 }); 323 } 324 325 326 } 327 } 328 } 329 | 22 */ 23 24 /* 25 * @test 26 * @run testng/othervm -Diters=20000 VarHandleTestMethodHandleAccessShort 27 */ 28 29 import org.testng.annotations.BeforeClass; 30 import org.testng.annotations.DataProvider; 31 import org.testng.annotations.Test; 32 33 import java.lang.invoke.MethodHandles; 34 import java.lang.invoke.VarHandle; 35 import java.util.ArrayList; 36 import java.util.Arrays; 37 import java.util.List; 38 39 import static org.testng.Assert.*; 40 41 public class VarHandleTestMethodHandleAccessShort extends VarHandleBaseTest { 42 static final short static_final_v = (short)0x0123; 43 44 static short static_v; 45 46 final short final_v = (short)0x0123; 47 48 short v; 49 50 VarHandle vhFinalField; 51 52 VarHandle vhField; 53 54 VarHandle vhStaticField; 55 56 VarHandle vhStaticFinalField; 57 58 VarHandle vhArray; 59 60 @BeforeClass 61 public void setup() throws Exception { 62 vhFinalField = MethodHandles.lookup().findVarHandle( 63 VarHandleTestMethodHandleAccessShort.class, "final_v", short.class); 64 65 vhField = MethodHandles.lookup().findVarHandle( 66 VarHandleTestMethodHandleAccessShort.class, "v", short.class); 104 105 // Work around issue with jtreg summary reporting which truncates 106 // the String result of Object.toString to 30 characters, hence 107 // the first dummy argument 108 return cases.stream().map(tc -> new Object[]{tc.toString(), tc}).toArray(Object[][]::new); 109 } 110 111 @Test(dataProvider = "accessTestCaseProvider") 112 public <T> void testAccess(String desc, AccessTestCase<T> atc) throws Throwable { 113 T t = atc.get(); 114 int iters = atc.requiresLoop() ? ITERS : 1; 115 for (int c = 0; c < iters; c++) { 116 atc.testAccess(t); 117 } 118 } 119 120 121 static void testInstanceField(VarHandleTestMethodHandleAccessShort recv, Handles hs) throws Throwable { 122 // Plain 123 { 124 hs.get(TestAccessMode.SET).invokeExact(recv, (short)0x0123); 125 short x = (short) hs.get(TestAccessMode.GET).invokeExact(recv); 126 assertEquals(x, (short)0x0123, "set short value"); 127 } 128 129 130 // Volatile 131 { 132 hs.get(TestAccessMode.SET_VOLATILE).invokeExact(recv, (short)0x4567); 133 short x = (short) hs.get(TestAccessMode.GET_VOLATILE).invokeExact(recv); 134 assertEquals(x, (short)0x4567, "setVolatile short value"); 135 } 136 137 // Lazy 138 { 139 hs.get(TestAccessMode.SET_RELEASE).invokeExact(recv, (short)0x0123); 140 short x = (short) hs.get(TestAccessMode.GET_ACQUIRE).invokeExact(recv); 141 assertEquals(x, (short)0x0123, "setRelease short value"); 142 } 143 144 // Opaque 145 { 146 hs.get(TestAccessMode.SET_OPAQUE).invokeExact(recv, (short)0x4567); 147 short x = (short) hs.get(TestAccessMode.GET_OPAQUE).invokeExact(recv); 148 assertEquals(x, (short)0x4567, "setOpaque short value"); 149 } 150 151 hs.get(TestAccessMode.SET).invokeExact(recv, (short)0x0123); 152 153 // Compare 154 { 155 boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(recv, (short)0x0123, (short)0x4567); 156 assertEquals(r, true, "success compareAndSet short"); 157 short x = (short) hs.get(TestAccessMode.GET).invokeExact(recv); 158 assertEquals(x, (short)0x4567, "success compareAndSet short value"); 159 } 160 161 { 162 boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(recv, (short)0x0123, (short)0x89AB); 163 assertEquals(r, false, "failing compareAndSet short"); 164 short x = (short) hs.get(TestAccessMode.GET).invokeExact(recv); 165 assertEquals(x, (short)0x4567, "failing compareAndSet short value"); 166 } 167 168 { 169 short r = (short) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_VOLATILE).invokeExact(recv, (short)0x4567, (short)0x0123); 170 assertEquals(r, (short)0x4567, "success compareAndExchangeVolatile short"); 171 short x = (short) hs.get(TestAccessMode.GET).invokeExact(recv); 172 assertEquals(x, (short)0x0123, "success compareAndExchangeVolatile short value"); 173 } 174 175 { 176 short r = (short) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_VOLATILE).invokeExact(recv, (short)0x4567, (short)0x89AB); 177 assertEquals(r, (short)0x0123, "failing compareAndExchangeVolatile short"); 178 short x = (short) hs.get(TestAccessMode.GET).invokeExact(recv); 179 assertEquals(x, (short)0x0123, "failing compareAndExchangeVolatile short value"); 180 } 181 182 { 183 short r = (short) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(recv, (short)0x0123, (short)0x4567); 184 assertEquals(r, (short)0x0123, "success compareAndExchangeAcquire short"); 185 short x = (short) hs.get(TestAccessMode.GET).invokeExact(recv); 186 assertEquals(x, (short)0x4567, "success compareAndExchangeAcquire short value"); 187 } 188 189 { 190 short r = (short) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(recv, (short)0x0123, (short)0x89AB); 191 assertEquals(r, (short)0x4567, "failing compareAndExchangeAcquire short"); 192 short x = (short) hs.get(TestAccessMode.GET).invokeExact(recv); 193 assertEquals(x, (short)0x4567, "failing compareAndExchangeAcquire short value"); 194 } 195 196 { 197 short r = (short) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(recv, (short)0x4567, (short)0x0123); 198 assertEquals(r, (short)0x4567, "success compareAndExchangeRelease short"); 199 short x = (short) hs.get(TestAccessMode.GET).invokeExact(recv); 200 assertEquals(x, (short)0x0123, "success compareAndExchangeRelease short value"); 201 } 202 203 { 204 short r = (short) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(recv, (short)0x4567, (short)0x89AB); 205 assertEquals(r, (short)0x0123, "failing compareAndExchangeRelease short"); 206 short x = (short) hs.get(TestAccessMode.GET).invokeExact(recv); 207 assertEquals(x, (short)0x0123, "failing compareAndExchangeRelease short value"); 208 } 209 210 { 211 boolean success = false; 212 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 213 success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET).invokeExact(recv, (short)0x0123, (short)0x4567); 214 } 215 assertEquals(success, true, "weakCompareAndSet short"); 216 short x = (short) hs.get(TestAccessMode.GET).invokeExact(recv); 217 assertEquals(x, (short)0x4567, "weakCompareAndSet short value"); 218 } 219 220 { 221 boolean success = false; 222 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 223 success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_ACQUIRE).invokeExact(recv, (short)0x4567, (short)0x0123); 224 } 225 assertEquals(success, true, "weakCompareAndSetAcquire short"); 226 short x = (short) hs.get(TestAccessMode.GET).invokeExact(recv); 227 assertEquals(x, (short)0x0123, "weakCompareAndSetAcquire short"); 228 } 229 230 { 231 boolean success = false; 232 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 233 success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_RELEASE).invokeExact(recv, (short)0x0123, (short)0x4567); 234 } 235 assertEquals(success, true, "weakCompareAndSetRelease short"); 236 short x = (short) hs.get(TestAccessMode.GET).invokeExact(recv); 237 assertEquals(x, (short)0x4567, "weakCompareAndSetRelease short"); 238 } 239 240 // Compare set and get 241 { 242 short o = (short) hs.get(TestAccessMode.GET_AND_SET).invokeExact(recv, (short)0x0123); 243 assertEquals(o, (short)0x4567, "getAndSet short"); 244 short x = (short) hs.get(TestAccessMode.GET).invokeExact(recv); 245 assertEquals(x, (short)0x0123, "getAndSet short value"); 246 } 247 248 hs.get(TestAccessMode.SET).invokeExact(recv, (short)0x0123); 249 250 // get and add, add and get 251 { 252 short o = (short) hs.get(TestAccessMode.GET_AND_ADD).invokeExact(recv, (short)0x89AB); 253 assertEquals(o, (short)0x0123, "getAndAdd short"); 254 short c = (short) hs.get(TestAccessMode.ADD_AND_GET).invokeExact(recv, (short)0x89AB); 255 assertEquals(c, (short)((short)0x0123 + (short)0x89AB + (short)0x89AB), "getAndAdd short value"); 256 } 257 } 258 259 static void testInstanceFieldUnsupported(VarHandleTestMethodHandleAccessShort recv, Handles hs) throws Throwable { 260 261 } 262 263 264 static void testStaticField(Handles hs) throws Throwable { 265 // Plain 266 { 267 hs.get(TestAccessMode.SET).invokeExact((short)0x0123); 268 short x = (short) hs.get(TestAccessMode.GET).invokeExact(); 269 assertEquals(x, (short)0x0123, "set short value"); 270 } 271 272 273 // Volatile 274 { 275 hs.get(TestAccessMode.SET_VOLATILE).invokeExact((short)0x4567); 276 short x = (short) hs.get(TestAccessMode.GET_VOLATILE).invokeExact(); 277 assertEquals(x, (short)0x4567, "setVolatile short value"); 278 } 279 280 // Lazy 281 { 282 hs.get(TestAccessMode.SET_RELEASE).invokeExact((short)0x0123); 283 short x = (short) hs.get(TestAccessMode.GET_ACQUIRE).invokeExact(); 284 assertEquals(x, (short)0x0123, "setRelease short value"); 285 } 286 287 // Opaque 288 { 289 hs.get(TestAccessMode.SET_OPAQUE).invokeExact((short)0x4567); 290 short x = (short) hs.get(TestAccessMode.GET_OPAQUE).invokeExact(); 291 assertEquals(x, (short)0x4567, "setOpaque short value"); 292 } 293 294 hs.get(TestAccessMode.SET).invokeExact((short)0x0123); 295 296 // Compare 297 { 298 boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact((short)0x0123, (short)0x4567); 299 assertEquals(r, true, "success compareAndSet short"); 300 short x = (short) hs.get(TestAccessMode.GET).invokeExact(); 301 assertEquals(x, (short)0x4567, "success compareAndSet short value"); 302 } 303 304 { 305 boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact((short)0x0123, (short)0x89AB); 306 assertEquals(r, false, "failing compareAndSet short"); 307 short x = (short) hs.get(TestAccessMode.GET).invokeExact(); 308 assertEquals(x, (short)0x4567, "failing compareAndSet short value"); 309 } 310 311 { 312 short r = (short) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_VOLATILE).invokeExact((short)0x4567, (short)0x0123); 313 assertEquals(r, (short)0x4567, "success compareAndExchangeVolatile short"); 314 short x = (short) hs.get(TestAccessMode.GET).invokeExact(); 315 assertEquals(x, (short)0x0123, "success compareAndExchangeVolatile short value"); 316 } 317 318 { 319 short r = (short) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_VOLATILE).invokeExact((short)0x4567, (short)0x89AB); 320 assertEquals(r, (short)0x0123, "failing compareAndExchangeVolatile short"); 321 short x = (short) hs.get(TestAccessMode.GET).invokeExact(); 322 assertEquals(x, (short)0x0123, "failing compareAndExchangeVolatile short value"); 323 } 324 325 { 326 short r = (short) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact((short)0x0123, (short)0x4567); 327 assertEquals(r, (short)0x0123, "success compareAndExchangeAcquire short"); 328 short x = (short) hs.get(TestAccessMode.GET).invokeExact(); 329 assertEquals(x, (short)0x4567, "success compareAndExchangeAcquire short value"); 330 } 331 332 { 333 short r = (short) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact((short)0x0123, (short)0x89AB); 334 assertEquals(r, (short)0x4567, "failing compareAndExchangeAcquire short"); 335 short x = (short) hs.get(TestAccessMode.GET).invokeExact(); 336 assertEquals(x, (short)0x4567, "failing compareAndExchangeAcquire short value"); 337 } 338 339 { 340 short r = (short) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact((short)0x4567, (short)0x0123); 341 assertEquals(r, (short)0x4567, "success compareAndExchangeRelease short"); 342 short x = (short) hs.get(TestAccessMode.GET).invokeExact(); 343 assertEquals(x, (short)0x0123, "success compareAndExchangeRelease short value"); 344 } 345 346 { 347 short r = (short) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact((short)0x4567, (short)0x89AB); 348 assertEquals(r, (short)0x0123, "failing compareAndExchangeRelease short"); 349 short x = (short) hs.get(TestAccessMode.GET).invokeExact(); 350 assertEquals(x, (short)0x0123, "failing compareAndExchangeRelease short value"); 351 } 352 353 { 354 boolean success = false; 355 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 356 success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET).invokeExact((short)0x0123, (short)0x4567); 357 } 358 assertEquals(success, true, "weakCompareAndSet short"); 359 short x = (short) hs.get(TestAccessMode.GET).invokeExact(); 360 assertEquals(x, (short)0x4567, "weakCompareAndSet short value"); 361 } 362 363 { 364 boolean success = false; 365 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 366 success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_ACQUIRE).invokeExact((short)0x4567, (short)0x0123); 367 } 368 assertEquals(success, true, "weakCompareAndSetAcquire short"); 369 short x = (short) hs.get(TestAccessMode.GET).invokeExact(); 370 assertEquals(x, (short)0x0123, "weakCompareAndSetAcquire short"); 371 } 372 373 { 374 boolean success = false; 375 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 376 success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_RELEASE).invokeExact((short)0x0123, (short)0x4567); 377 } 378 assertEquals(success, true, "weakCompareAndSetRelease short"); 379 short x = (short) hs.get(TestAccessMode.GET).invokeExact(); 380 assertEquals(x, (short)0x4567, "weakCompareAndSetRelease short"); 381 } 382 383 // Compare set and get 384 { 385 short o = (short) hs.get(TestAccessMode.GET_AND_SET).invokeExact( (short)0x0123); 386 assertEquals(o, (short)0x4567, "getAndSet short"); 387 short x = (short) hs.get(TestAccessMode.GET).invokeExact(); 388 assertEquals(x, (short)0x0123, "getAndSet short value"); 389 } 390 391 hs.get(TestAccessMode.SET).invokeExact((short)0x0123); 392 393 // get and add, add and get 394 { 395 short o = (short) hs.get(TestAccessMode.GET_AND_ADD).invokeExact( (short)0x89AB); 396 assertEquals(o, (short)0x0123, "getAndAdd short"); 397 short c = (short) hs.get(TestAccessMode.ADD_AND_GET).invokeExact((short)0x89AB); 398 assertEquals(c, (short)((short)0x0123 + (short)0x89AB + (short)0x89AB), "getAndAdd short value"); 399 } 400 } 401 402 static void testStaticFieldUnsupported(Handles hs) throws Throwable { 403 404 } 405 406 407 static void testArray(Handles hs) throws Throwable { 408 short[] array = new short[10]; 409 410 for (int i = 0; i < array.length; i++) { 411 // Plain 412 { 413 hs.get(TestAccessMode.SET).invokeExact(array, i, (short)0x0123); 414 short x = (short) hs.get(TestAccessMode.GET).invokeExact(array, i); 415 assertEquals(x, (short)0x0123, "get short value"); 416 } 417 418 419 // Volatile 420 { 421 hs.get(TestAccessMode.SET_VOLATILE).invokeExact(array, i, (short)0x4567); 422 short x = (short) hs.get(TestAccessMode.GET_VOLATILE).invokeExact(array, i); 423 assertEquals(x, (short)0x4567, "setVolatile short value"); 424 } 425 426 // Lazy 427 { 428 hs.get(TestAccessMode.SET_RELEASE).invokeExact(array, i, (short)0x0123); 429 short x = (short) hs.get(TestAccessMode.GET_ACQUIRE).invokeExact(array, i); 430 assertEquals(x, (short)0x0123, "setRelease short value"); 431 } 432 433 // Opaque 434 { 435 hs.get(TestAccessMode.SET_OPAQUE).invokeExact(array, i, (short)0x4567); 436 short x = (short) hs.get(TestAccessMode.GET_OPAQUE).invokeExact(array, i); 437 assertEquals(x, (short)0x4567, "setOpaque short value"); 438 } 439 440 hs.get(TestAccessMode.SET).invokeExact(array, i, (short)0x0123); 441 442 // Compare 443 { 444 boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(array, i, (short)0x0123, (short)0x4567); 445 assertEquals(r, true, "success compareAndSet short"); 446 short x = (short) hs.get(TestAccessMode.GET).invokeExact(array, i); 447 assertEquals(x, (short)0x4567, "success compareAndSet short value"); 448 } 449 450 { 451 boolean r = (boolean) hs.get(TestAccessMode.COMPARE_AND_SET).invokeExact(array, i, (short)0x0123, (short)0x89AB); 452 assertEquals(r, false, "failing compareAndSet short"); 453 short x = (short) hs.get(TestAccessMode.GET).invokeExact(array, i); 454 assertEquals(x, (short)0x4567, "failing compareAndSet short value"); 455 } 456 457 { 458 short r = (short) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_VOLATILE).invokeExact(array, i, (short)0x4567, (short)0x0123); 459 assertEquals(r, (short)0x4567, "success compareAndExchangeVolatile short"); 460 short x = (short) hs.get(TestAccessMode.GET).invokeExact(array, i); 461 assertEquals(x, (short)0x0123, "success compareAndExchangeVolatile short value"); 462 } 463 464 { 465 short r = (short) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_VOLATILE).invokeExact(array, i, (short)0x4567, (short)0x89AB); 466 assertEquals(r, (short)0x0123, "failing compareAndExchangeVolatile short"); 467 short x = (short) hs.get(TestAccessMode.GET).invokeExact(array, i); 468 assertEquals(x, (short)0x0123, "failing compareAndExchangeVolatile short value"); 469 } 470 471 { 472 short r = (short) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(array, i, (short)0x0123, (short)0x4567); 473 assertEquals(r, (short)0x0123, "success compareAndExchangeAcquire short"); 474 short x = (short) hs.get(TestAccessMode.GET).invokeExact(array, i); 475 assertEquals(x, (short)0x4567, "success compareAndExchangeAcquire short value"); 476 } 477 478 { 479 short r = (short) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_ACQUIRE).invokeExact(array, i, (short)0x0123, (short)0x89AB); 480 assertEquals(r, (short)0x4567, "failing compareAndExchangeAcquire short"); 481 short x = (short) hs.get(TestAccessMode.GET).invokeExact(array, i); 482 assertEquals(x, (short)0x4567, "failing compareAndExchangeAcquire short value"); 483 } 484 485 { 486 short r = (short) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(array, i, (short)0x4567, (short)0x0123); 487 assertEquals(r, (short)0x4567, "success compareAndExchangeRelease short"); 488 short x = (short) hs.get(TestAccessMode.GET).invokeExact(array, i); 489 assertEquals(x, (short)0x0123, "success compareAndExchangeRelease short value"); 490 } 491 492 { 493 short r = (short) hs.get(TestAccessMode.COMPARE_AND_EXCHANGE_RELEASE).invokeExact(array, i, (short)0x4567, (short)0x89AB); 494 assertEquals(r, (short)0x0123, "failing compareAndExchangeRelease short"); 495 short x = (short) hs.get(TestAccessMode.GET).invokeExact(array, i); 496 assertEquals(x, (short)0x0123, "failing compareAndExchangeRelease short value"); 497 } 498 499 { 500 boolean success = false; 501 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 502 success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET).invokeExact(array, i, (short)0x0123, (short)0x4567); 503 } 504 assertEquals(success, true, "weakCompareAndSet short"); 505 short x = (short) hs.get(TestAccessMode.GET).invokeExact(array, i); 506 assertEquals(x, (short)0x4567, "weakCompareAndSet short value"); 507 } 508 509 { 510 boolean success = false; 511 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 512 success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_ACQUIRE).invokeExact(array, i, (short)0x4567, (short)0x0123); 513 } 514 assertEquals(success, true, "weakCompareAndSetAcquire short"); 515 short x = (short) hs.get(TestAccessMode.GET).invokeExact(array, i); 516 assertEquals(x, (short)0x0123, "weakCompareAndSetAcquire short"); 517 } 518 519 { 520 boolean success = false; 521 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 522 success = (boolean) hs.get(TestAccessMode.WEAK_COMPARE_AND_SET_RELEASE).invokeExact(array, i, (short)0x0123, (short)0x4567); 523 } 524 assertEquals(success, true, "weakCompareAndSetRelease short"); 525 short x = (short) hs.get(TestAccessMode.GET).invokeExact(array, i); 526 assertEquals(x, (short)0x4567, "weakCompareAndSetRelease short"); 527 } 528 529 // Compare set and get 530 { 531 short o = (short) hs.get(TestAccessMode.GET_AND_SET).invokeExact(array, i, (short)0x0123); 532 assertEquals(o, (short)0x4567, "getAndSet short"); 533 short x = (short) hs.get(TestAccessMode.GET).invokeExact(array, i); 534 assertEquals(x, (short)0x0123, "getAndSet short value"); 535 } 536 537 hs.get(TestAccessMode.SET).invokeExact(array, i, (short)0x0123); 538 539 // get and add, add and get 540 { 541 short o = (short) hs.get(TestAccessMode.GET_AND_ADD).invokeExact(array, i, (short)0x89AB); 542 assertEquals(o, (short)0x0123, "getAndAdd short"); 543 short c = (short) hs.get(TestAccessMode.ADD_AND_GET).invokeExact(array, i, (short)0x89AB); 544 assertEquals(c, (short)((short)0x0123 + (short)0x89AB + (short)0x89AB), "getAndAdd short value"); 545 } 546 } 547 } 548 549 static void testArrayUnsupported(Handles hs) throws Throwable { 550 short[] array = new short[10]; 551 552 final int i = 0; 553 554 } 555 556 static void testArrayIndexOutOfBounds(Handles hs) throws Throwable { 557 short[] array = new short[10]; 558 559 for (int i : new int[]{-1, Integer.MIN_VALUE, 10, 11, Integer.MAX_VALUE}) { 560 final int ci = i; 561 562 for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET)) { 563 checkIOOBE(am, () -> { 564 short x = (short) hs.get(am).invokeExact(array, ci); 565 }); 566 } 567 568 for (TestAccessMode am : testAccessModesOfType(TestAccessType.SET)) { 569 checkIOOBE(am, () -> { 570 hs.get(am).invokeExact(array, ci, (short)0x0123); 571 }); 572 } 573 574 for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_SET)) { 575 checkIOOBE(am, () -> { 576 boolean r = (boolean) hs.get(am).invokeExact(array, ci, (short)0x0123, (short)0x4567); 577 }); 578 } 579 580 for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_EXCHANGE)) { 581 checkIOOBE(am, () -> { 582 short r = (short) hs.get(am).invokeExact(array, ci, (short)0x4567, (short)0x0123); 583 }); 584 } 585 586 for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_SET)) { 587 checkIOOBE(am, () -> { 588 short o = (short) hs.get(am).invokeExact(array, ci, (short)0x0123); 589 }); 590 } 591 592 for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_ADD)) { 593 checkIOOBE(am, () -> { 594 short o = (short) hs.get(am).invokeExact(array, ci, (short)0x89AB); 595 }); 596 } 597 } 598 } 599 } 600 |