1 /*
2  * Written by Doug Lea with assistance from members of JCP JSR-166
3  * Expert Group and released to the public domain, as explained at
4  * http://creativecommons.org/publicdomain/zero/1.0/
5  * Other contributors include Andrew Wright, Jeffrey Hayes,
6  * Pat Fisher, Mike Judd.
7  */
8 
9 package jsr166;
10 
11 import java.util.concurrent.atomic.AtomicBoolean;
12 
13 import junit.framework.Test;
14 import junit.framework.TestSuite;
15 
16 public class AtomicBooleanTest extends JSR166TestCase {
17     // android-note: Removed because the CTS runner does a bad job of
18     // retrying tests that have suite() declarations.
19     //
20     // public static void main(String[] args) {
21     //     main(suite(), args);
22     // }
23     // public static Test suite() {
24     //     return new TestSuite(AtomicBooleanTest.class);
25     // }
26 
27     /**
28      * constructor initializes to given value
29      */
testConstructor()30     public void testConstructor() {
31         assertTrue(new AtomicBoolean(true).get());
32         assertFalse(new AtomicBoolean(false).get());
33     }
34 
35     /**
36      * default constructed initializes to false
37      */
testConstructor2()38     public void testConstructor2() {
39         AtomicBoolean ai = new AtomicBoolean();
40         assertFalse(ai.get());
41     }
42 
43     /**
44      * get returns the last value set
45      */
testGetSet()46     public void testGetSet() {
47         AtomicBoolean ai = new AtomicBoolean(true);
48         assertTrue(ai.get());
49         ai.set(false);
50         assertFalse(ai.get());
51         ai.set(true);
52         assertTrue(ai.get());
53     }
54 
55     /**
56      * get returns the last value lazySet in same thread
57      */
testGetLazySet()58     public void testGetLazySet() {
59         AtomicBoolean ai = new AtomicBoolean(true);
60         assertTrue(ai.get());
61         ai.lazySet(false);
62         assertFalse(ai.get());
63         ai.lazySet(true);
64         assertTrue(ai.get());
65     }
66 
67     /**
68      * compareAndSet succeeds in changing value if equal to expected else fails
69      */
testCompareAndSet()70     public void testCompareAndSet() {
71         AtomicBoolean ai = new AtomicBoolean(true);
72         assertTrue(ai.compareAndSet(true, false));
73         assertFalse(ai.get());
74         assertTrue(ai.compareAndSet(false, false));
75         assertFalse(ai.get());
76         assertFalse(ai.compareAndSet(true, false));
77         assertFalse(ai.get());
78         assertTrue(ai.compareAndSet(false, true));
79         assertTrue(ai.get());
80     }
81 
82     /**
83      * compareAndSet in one thread enables another waiting for value
84      * to succeed
85      */
testCompareAndSetInMultipleThreads()86     public void testCompareAndSetInMultipleThreads() throws Exception {
87         final AtomicBoolean ai = new AtomicBoolean(true);
88         Thread t = new Thread(new CheckedRunnable() {
89             public void realRun() {
90                 while (!ai.compareAndSet(false, true)) Thread.yield();
91             }});
92 
93         t.start();
94         assertTrue(ai.compareAndSet(true, false));
95         t.join(LONG_DELAY_MS);
96         assertFalse(t.isAlive());
97     }
98 
99     /**
100      * repeated weakCompareAndSet succeeds in changing value when equal
101      * to expected
102      */
testWeakCompareAndSet()103     public void testWeakCompareAndSet() {
104         AtomicBoolean ai = new AtomicBoolean(true);
105         do {} while (!ai.weakCompareAndSet(true, false));
106         assertFalse(ai.get());
107         do {} while (!ai.weakCompareAndSet(false, false));
108         assertFalse(ai.get());
109         do {} while (!ai.weakCompareAndSet(false, true));
110         assertTrue(ai.get());
111     }
112 
113     /**
114      * getAndSet returns previous value and sets to given value
115      */
testGetAndSet()116     public void testGetAndSet() {
117         AtomicBoolean ai = new AtomicBoolean(true);
118         assertEquals(true, ai.getAndSet(false));
119         assertEquals(false, ai.getAndSet(false));
120         assertEquals(false, ai.getAndSet(true));
121         assertTrue(ai.get());
122     }
123 
124     /**
125      * a deserialized serialized atomic holds same value
126      */
testSerialization()127     public void testSerialization() throws Exception {
128         AtomicBoolean x = new AtomicBoolean();
129         AtomicBoolean y = serialClone(x);
130         x.set(true);
131         AtomicBoolean z = serialClone(x);
132         assertTrue(x.get());
133         assertFalse(y.get());
134         assertTrue(z.get());
135     }
136 
137     /**
138      * toString returns current value.
139      */
testToString()140     public void testToString() {
141         AtomicBoolean ai = new AtomicBoolean();
142         assertEquals(Boolean.toString(false), ai.toString());
143         ai.set(true);
144         assertEquals(Boolean.toString(true), ai.toString());
145     }
146 
147 }
148