2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012-2015 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
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.
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.
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.
18 package org.apidesign.bck2brwsr.tck;
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;
32 * @author Jaroslav Tulach <jtulach@netbeans.org>
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");
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");
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");
60 @Compare public boolean atomicBoolean() {
61 AtomicBoolean ab = new AtomicBoolean();
63 return ab.compareAndSet(true, false);
66 @Compare public int atomicInt() {
67 AtomicInteger ab = new AtomicInteger();
69 assert ab.compareAndSet(30, 10);
73 @Compare public String atomicRef() {
74 AtomicReference<String> ar = new AtomicReference<String>("Ahoj");
75 assert ar.compareAndSet("Ahoj", "Hello");
76 return ar.getAndSet("Other");
79 @Compare public int intUpdater() {
80 intValue = Integer.MAX_VALUE / 2;
81 final AtomicIntegerFieldUpdater<AtomicTest> u = getIntUpdater();
82 u.compareAndSet(this, 0, 10);
84 int thirteen = u.getAndAdd(this, 7);
85 return thirteen + u.get(this);
88 @Compare public long longUpdater() {
89 longValue = Long.MAX_VALUE / 2;
90 final AtomicLongFieldUpdater<AtomicTest> u = getLongUpdater();
91 u.compareAndSet(this, 0, 10);
93 long thirteen = u.getAndAdd(this, 7);
94 return thirteen + u.get(this);
97 @Compare public int refUpdater() {
98 final AtomicReferenceFieldUpdater<AtomicTest,Number> u = getRefUpdater();
100 u.compareAndSet(this, 0, 10);
101 Number ten = u.getAndSet(this, 3);
102 return u.get(this).intValue() + ten.intValue();
105 @Factory public static Object[] create() {
106 return VMTest.create(AtomicTest.class);