< prev index next >

test/compiler/unsafe/JdkInternalMiscUnsafeAccessTestChar.java

Print this page
rev 12711 : 8181292: Backport Rename internal Unsafe.compare methods from 10 to 9
Reviewed-by: psandoz, dholmes, thartmann
Contributed-by: ron.pressler@oracle.com, claes.redestad@oracle.com
   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  */


 181             char x = UNSAFE.getCharUnaligned(base, offset);
 182             assertEquals(x, '\u4567', "putUnaligned char value");
 183         }
 184 
 185         {
 186             UNSAFE.putCharUnaligned(base, offset, '\u0123', true);
 187             char x = UNSAFE.getCharUnaligned(base, offset, true);
 188             assertEquals(x, '\u0123', "putUnaligned big endian char value");
 189         }
 190 
 191         {
 192             UNSAFE.putCharUnaligned(base, offset, '\u4567', false);
 193             char x = UNSAFE.getCharUnaligned(base, offset, false);
 194             assertEquals(x, '\u4567', "putUnaligned little endian char value");
 195         }
 196 
 197         UNSAFE.putChar(base, offset, '\u0123');
 198 
 199         // Compare
 200         {
 201             boolean r = UNSAFE.compareAndSwapChar(base, offset, '\u0123', '\u4567');
 202             assertEquals(r, true, "success compareAndSwap char");
 203             char x = UNSAFE.getChar(base, offset);
 204             assertEquals(x, '\u4567', "success compareAndSwap char value");
 205         }
 206 
 207         {
 208             boolean r = UNSAFE.compareAndSwapChar(base, offset, '\u0123', '\u89AB');
 209             assertEquals(r, false, "failing compareAndSwap char");
 210             char x = UNSAFE.getChar(base, offset);
 211             assertEquals(x, '\u4567', "failing compareAndSwap char value");
 212         }
 213 
 214         // Advanced compare
 215         {
 216             char r = UNSAFE.compareAndExchangeCharVolatile(base, offset, '\u4567', '\u0123');
 217             assertEquals(r, '\u4567', "success compareAndExchangeVolatile char");
 218             char x = UNSAFE.getChar(base, offset);
 219             assertEquals(x, '\u0123', "success compareAndExchangeVolatile char value");
 220         }
 221 
 222         {
 223             char r = UNSAFE.compareAndExchangeCharVolatile(base, offset, '\u4567', '\u89AB');
 224             assertEquals(r, '\u0123', "failing compareAndExchangeVolatile char");
 225             char x = UNSAFE.getChar(base, offset);
 226             assertEquals(x, '\u0123', "failing compareAndExchangeVolatile char value");
 227         }
 228 
 229         {
 230             char r = UNSAFE.compareAndExchangeCharAcquire(base, offset, '\u0123', '\u4567');
 231             assertEquals(r, '\u0123', "success compareAndExchangeAcquire char");
 232             char x = UNSAFE.getChar(base, offset);
 233             assertEquals(x, '\u4567', "success compareAndExchangeAcquire char value");
 234         }
 235 
 236         {
 237             char r = UNSAFE.compareAndExchangeCharAcquire(base, offset, '\u0123', '\u89AB');
 238             assertEquals(r, '\u4567', "failing compareAndExchangeAcquire char");
 239             char x = UNSAFE.getChar(base, offset);
 240             assertEquals(x, '\u4567', "failing compareAndExchangeAcquire char value");
 241         }
 242 
 243         {
 244             char r = UNSAFE.compareAndExchangeCharRelease(base, offset, '\u4567', '\u0123');
 245             assertEquals(r, '\u4567', "success compareAndExchangeRelease char");
 246             char x = UNSAFE.getChar(base, offset);
 247             assertEquals(x, '\u0123', "success compareAndExchangeRelease char value");
 248         }
 249 
 250         {
 251             char r = UNSAFE.compareAndExchangeCharRelease(base, offset, '\u4567', '\u89AB');
 252             assertEquals(r, '\u0123', "failing compareAndExchangeRelease char");
 253             char x = UNSAFE.getChar(base, offset);
 254             assertEquals(x, '\u0123', "failing compareAndExchangeRelease char value");
 255         }
 256 
 257         {
 258             boolean success = false;
 259             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 260                 success = UNSAFE.weakCompareAndSwapChar(base, offset, '\u0123', '\u4567');
 261             }
 262             assertEquals(success, true, "weakCompareAndSwap char");
 263             char x = UNSAFE.getChar(base, offset);
 264             assertEquals(x, '\u4567', "weakCompareAndSwap char value");
 265         }
 266 
 267         {
 268             boolean success = false;
 269             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 270                 success = UNSAFE.weakCompareAndSwapCharAcquire(base, offset, '\u4567', '\u0123');
 271             }
 272             assertEquals(success, true, "weakCompareAndSwapAcquire char");
 273             char x = UNSAFE.getChar(base, offset);
 274             assertEquals(x, '\u0123', "weakCompareAndSwapAcquire char");
 275         }
 276 
 277         {
 278             boolean success = false;
 279             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 280                 success = UNSAFE.weakCompareAndSwapCharRelease(base, offset, '\u0123', '\u4567');
 281             }
 282             assertEquals(success, true, "weakCompareAndSwapRelease char");
 283             char x = UNSAFE.getChar(base, offset);
 284             assertEquals(x, '\u4567', "weakCompareAndSwapRelease char");
 285         }
 286 
 287         {
 288             boolean success = false;
 289             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 290                 success = UNSAFE.weakCompareAndSwapCharVolatile(base, offset, '\u4567', '\u0123');
 291             }
 292             assertEquals(success, true, "weakCompareAndSwapVolatile char");
 293             char x = UNSAFE.getChar(base, offset);
 294             assertEquals(x, '\u0123', "weakCompareAndSwapVolatile char");
 295         }
 296 
 297         UNSAFE.putChar(base, offset, '\u4567');
 298 
 299         // Compare set and get
 300         {
 301             char o = UNSAFE.getAndSetChar(base, offset, '\u0123');
 302             assertEquals(o, '\u4567', "getAndSet char");
 303             char x = UNSAFE.getChar(base, offset);
 304             assertEquals(x, '\u0123', "getAndSet char value");
 305         }
 306 
 307         UNSAFE.putChar(base, offset, '\u0123');
 308 
 309         // get and add, add and get
 310         {
 311             char o = UNSAFE.getAndAddChar(base, offset, '\u4567');
 312             assertEquals(o, '\u0123', "getAndAdd char");
 313             char x = UNSAFE.getChar(base, offset);
 314             assertEquals(x, (char)('\u0123' + '\u4567'), "getAndAdd char");
 315         }
 316     }
 317 
 318     static void testAccess(long address) {
 319         // Plain
 320         {
 321             UNSAFE.putChar(address, '\u0123');
 322             char x = UNSAFE.getChar(address);
 323             assertEquals(x, '\u0123', "set char value");
 324         }
 325     }
 326 }
 327 
   1 /*
   2  * Copyright (c) 2015, 2017, 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  */


 181             char x = UNSAFE.getCharUnaligned(base, offset);
 182             assertEquals(x, '\u4567', "putUnaligned char value");
 183         }
 184 
 185         {
 186             UNSAFE.putCharUnaligned(base, offset, '\u0123', true);
 187             char x = UNSAFE.getCharUnaligned(base, offset, true);
 188             assertEquals(x, '\u0123', "putUnaligned big endian char value");
 189         }
 190 
 191         {
 192             UNSAFE.putCharUnaligned(base, offset, '\u4567', false);
 193             char x = UNSAFE.getCharUnaligned(base, offset, false);
 194             assertEquals(x, '\u4567', "putUnaligned little endian char value");
 195         }
 196 
 197         UNSAFE.putChar(base, offset, '\u0123');
 198 
 199         // Compare
 200         {
 201             boolean r = UNSAFE.compareAndSetChar(base, offset, '\u0123', '\u4567');
 202             assertEquals(r, true, "success compareAndSet char");
 203             char x = UNSAFE.getChar(base, offset);
 204             assertEquals(x, '\u4567', "success compareAndSet char value");
 205         }
 206 
 207         {
 208             boolean r = UNSAFE.compareAndSetChar(base, offset, '\u0123', '\u89AB');
 209             assertEquals(r, false, "failing compareAndSet char");
 210             char x = UNSAFE.getChar(base, offset);
 211             assertEquals(x, '\u4567', "failing compareAndSet char value");
 212         }
 213 
 214         // Advanced compare
 215         {
 216             char r = UNSAFE.compareAndExchangeChar(base, offset, '\u4567', '\u0123');
 217             assertEquals(r, '\u4567', "success compareAndExchange char");
 218             char x = UNSAFE.getChar(base, offset);
 219             assertEquals(x, '\u0123', "success compareAndExchange char value");
 220         }
 221 
 222         {
 223             char r = UNSAFE.compareAndExchangeChar(base, offset, '\u4567', '\u89AB');
 224             assertEquals(r, '\u0123', "failing compareAndExchange char");
 225             char x = UNSAFE.getChar(base, offset);
 226             assertEquals(x, '\u0123', "failing compareAndExchange char value");
 227         }
 228 
 229         {
 230             char r = UNSAFE.compareAndExchangeCharAcquire(base, offset, '\u0123', '\u4567');
 231             assertEquals(r, '\u0123', "success compareAndExchangeAcquire char");
 232             char x = UNSAFE.getChar(base, offset);
 233             assertEquals(x, '\u4567', "success compareAndExchangeAcquire char value");
 234         }
 235 
 236         {
 237             char r = UNSAFE.compareAndExchangeCharAcquire(base, offset, '\u0123', '\u89AB');
 238             assertEquals(r, '\u4567', "failing compareAndExchangeAcquire char");
 239             char x = UNSAFE.getChar(base, offset);
 240             assertEquals(x, '\u4567', "failing compareAndExchangeAcquire char value");
 241         }
 242 
 243         {
 244             char r = UNSAFE.compareAndExchangeCharRelease(base, offset, '\u4567', '\u0123');
 245             assertEquals(r, '\u4567', "success compareAndExchangeRelease char");
 246             char x = UNSAFE.getChar(base, offset);
 247             assertEquals(x, '\u0123', "success compareAndExchangeRelease char value");
 248         }
 249 
 250         {
 251             char r = UNSAFE.compareAndExchangeCharRelease(base, offset, '\u4567', '\u89AB');
 252             assertEquals(r, '\u0123', "failing compareAndExchangeRelease char");
 253             char x = UNSAFE.getChar(base, offset);
 254             assertEquals(x, '\u0123', "failing compareAndExchangeRelease char value");
 255         }
 256 
 257         {
 258             boolean success = false;
 259             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 260                 success = UNSAFE.weakCompareAndSetCharPlain(base, offset, '\u0123', '\u4567');
 261             }
 262             assertEquals(success, true, "weakCompareAndSetPlain char");
 263             char x = UNSAFE.getChar(base, offset);
 264             assertEquals(x, '\u4567', "weakCompareAndSetPlain char value");
 265         }
 266 
 267         {
 268             boolean success = false;
 269             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 270                 success = UNSAFE.weakCompareAndSetCharAcquire(base, offset, '\u4567', '\u0123');
 271             }
 272             assertEquals(success, true, "weakCompareAndSetAcquire char");
 273             char x = UNSAFE.getChar(base, offset);
 274             assertEquals(x, '\u0123', "weakCompareAndSetAcquire char");
 275         }
 276 
 277         {
 278             boolean success = false;
 279             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 280                 success = UNSAFE.weakCompareAndSetCharRelease(base, offset, '\u0123', '\u4567');
 281             }
 282             assertEquals(success, true, "weakCompareAndSetRelease char");
 283             char x = UNSAFE.getChar(base, offset);
 284             assertEquals(x, '\u4567', "weakCompareAndSetRelease char");
 285         }
 286 
 287         {
 288             boolean success = false;
 289             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 290                 success = UNSAFE.weakCompareAndSetChar(base, offset, '\u4567', '\u0123');
 291             }
 292             assertEquals(success, true, "weakCompareAndSet char");
 293             char x = UNSAFE.getChar(base, offset);
 294             assertEquals(x, '\u0123', "weakCompareAndSet char");
 295         }
 296 
 297         UNSAFE.putChar(base, offset, '\u4567');
 298 
 299         // Compare set and get
 300         {
 301             char o = UNSAFE.getAndSetChar(base, offset, '\u0123');
 302             assertEquals(o, '\u4567', "getAndSet char");
 303             char x = UNSAFE.getChar(base, offset);
 304             assertEquals(x, '\u0123', "getAndSet char value");
 305         }
 306 
 307         UNSAFE.putChar(base, offset, '\u0123');
 308 
 309         // get and add, add and get
 310         {
 311             char o = UNSAFE.getAndAddChar(base, offset, '\u4567');
 312             assertEquals(o, '\u0123', "getAndAdd char");
 313             char x = UNSAFE.getChar(base, offset);
 314             assertEquals(x, (char)('\u0123' + '\u4567'), "getAndAdd char");
 315         }
 316     }
 317 
 318     static void testAccess(long address) {
 319         // Plain
 320         {
 321             UNSAFE.putChar(address, '\u0123');
 322             char x = UNSAFE.getChar(address);
 323             assertEquals(x, '\u0123', "set char value");
 324         }
 325     }
 326 }

< prev index next >