rt/emul/compacttest/src/test/java/org/apidesign/bck2brwsr/tck/AtomicTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 09 Feb 2015 20:36:12 +0100
changeset 1779 9d757281c666
parent 1723 3a1f262311cf
child 1787 ea12a3bb4b33
permissions -rw-r--r--
Support for updaters updating field in the same class that defines them
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, version 2 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. Look for COPYING file in the top folder.
    16  * If not, see http://opensource.org/licenses/GPL-2.0.
    17  */
    18 package org.apidesign.bck2brwsr.tck;
    19 
    20 import java.util.concurrent.atomic.AtomicBoolean;
    21 import java.util.concurrent.atomic.AtomicInteger;
    22 import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
    23 import java.util.concurrent.atomic.AtomicLongFieldUpdater;
    24 import java.util.concurrent.atomic.AtomicReference;
    25 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
    26 import org.apidesign.bck2brwsr.vmtest.Compare;
    27 import org.apidesign.bck2brwsr.vmtest.VMTest;
    28 import org.testng.annotations.Factory;
    29 
    30 /**
    31  *
    32  * @author Jaroslav Tulach <jtulach@netbeans.org>
    33  */
    34 public class AtomicTest {
    35     private volatile int intValue;
    36     private static AtomicIntegerFieldUpdater<AtomicTest> intUpdater;
    37     static AtomicIntegerFieldUpdater<AtomicTest> getIntUpdater() {
    38         if (intUpdater == null) {
    39             intUpdater = AtomicIntegerFieldUpdater.newUpdater(AtomicTest.class, "intValue");
    40         }
    41         return intUpdater;
    42     }
    43     private volatile long longValue;
    44     private static AtomicLongFieldUpdater<AtomicTest> longUpdater;
    45     static AtomicLongFieldUpdater<AtomicTest> getLongUpdater() {
    46         if (longUpdater == null) {
    47             longUpdater = AtomicLongFieldUpdater.newUpdater(AtomicTest.class, "longValue");
    48         }
    49         return longUpdater;
    50     }
    51     private volatile Number refValue;
    52     private static AtomicReferenceFieldUpdater<AtomicTest,Number> refUpdater;
    53     static AtomicReferenceFieldUpdater<AtomicTest,Number> getRefUpdater() {
    54         if (refUpdater == null) {
    55             refUpdater = AtomicReferenceFieldUpdater.newUpdater(AtomicTest.class, Number.class, "refValue");
    56         }
    57         return refUpdater;
    58     }
    59     
    60     @Compare public boolean atomicBoolean() {
    61         AtomicBoolean ab = new AtomicBoolean();
    62         ab.set(true);
    63         return ab.compareAndSet(true, false);
    64     }
    65 
    66     @Compare public int atomicInt() {
    67         AtomicInteger ab = new AtomicInteger();
    68         ab.set(30);
    69         assert ab.compareAndSet(30, 10);
    70         return ab.get();
    71     }
    72     
    73     @Compare public String atomicRef() {
    74         AtomicReference<String> ar = new AtomicReference<String>("Ahoj");
    75         assert ar.compareAndSet("Ahoj", "Hello");
    76         return ar.getAndSet("Other");
    77     }
    78     
    79     @Compare public int intUpdater() {
    80         intValue = Integer.MAX_VALUE / 2;
    81         final AtomicIntegerFieldUpdater<AtomicTest> u = getIntUpdater();
    82         u.compareAndSet(this, 0, 10);
    83         u.addAndGet(this, 3);
    84         int thirteen = u.getAndAdd(this, 7);
    85         return thirteen + u.get(this);
    86     }
    87 
    88     @Compare public long longUpdater() {
    89         longValue = Long.MAX_VALUE / 2;
    90         final AtomicLongFieldUpdater<AtomicTest> u = getLongUpdater();
    91         u.compareAndSet(this, 0, 10);
    92         u.addAndGet(this, 3);
    93         long thirteen = u.getAndAdd(this, 7);
    94         return thirteen + u.get(this);
    95     }
    96 
    97     @Compare public int refUpdater() {
    98         final AtomicReferenceFieldUpdater<AtomicTest,Number> u = getRefUpdater();
    99         u.set(this, 0);
   100         u.compareAndSet(this, 0, 10);
   101         Number ten = u.getAndSet(this, 3);
   102         return u.get(this).intValue() + ten.intValue();
   103     }
   104     
   105     @Factory public static Object[] create() {
   106         return VMTest.create(AtomicTest.class);
   107     }
   108 }