1 /*
2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3  *
4  * This code is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License version 2 only, as
6  * published by the Free Software Foundation.
7  *
8  * This code is distributed in the hope that it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
11  * version 2 for more details (a copy is included in the LICENSE file that
12  * accompanied this code).
13  *
14  * You should have received a copy of the GNU General Public License version
15  * 2 along with this work; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
17  *
18  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
19  * or visit www.oracle.com if you need additional information or have any
20  * questions.
21  */
22 
23 /*
24  * This file is available under and governed by the GNU General Public
25  * License version 2 only, as published by the Free Software Foundation.
26  * However, the following notice accompanied the original version of this
27  * file:
28  *
29  * Written by Doug Lea with assistance from members of JCP JSR-166
30  * Expert Group and released to the public domain, as explained at
31  * http://creativecommons.org/publicdomain/zero/1.0/
32  */
33 
34 import java.util.concurrent.atomic.AtomicBoolean;
35 
36 import junit.framework.Test;
37 import junit.framework.TestSuite;
38 
39 public class AtomicBoolean9Test extends JSR166TestCase {
main(String[] args)40     public static void main(String[] args) {
41         main(suite(), args);
42     }
suite()43     public static Test suite() {
44         return new TestSuite(AtomicBoolean9Test.class);
45     }
46 
47     /**
48      * getPlain returns the last value set
49      */
testGetPlainSet()50     public void testGetPlainSet() {
51         AtomicBoolean ai = new AtomicBoolean(true);
52         assertEquals(true, ai.getPlain());
53         ai.set(false);
54         assertEquals(false, ai.getPlain());
55         ai.set(true);
56         assertEquals(true, ai.getPlain());
57     }
58 
59     /**
60      * getOpaque returns the last value set
61      */
testGetOpaqueSet()62     public void testGetOpaqueSet() {
63         AtomicBoolean ai = new AtomicBoolean(true);
64         assertEquals(true, ai.getOpaque());
65         ai.set(false);
66         assertEquals(false, ai.getOpaque());
67         ai.set(true);
68         assertEquals(true, ai.getOpaque());
69     }
70 
71     /**
72      * getAcquire returns the last value set
73      */
testGetAcquireSet()74     public void testGetAcquireSet() {
75         AtomicBoolean ai = new AtomicBoolean(true);
76         assertEquals(true, ai.getAcquire());
77         ai.set(false);
78         assertEquals(false, ai.getAcquire());
79         ai.set(true);
80         assertEquals(true, ai.getAcquire());
81     }
82 
83     /**
84      * get returns the last value setPlain
85      */
testGetSetPlain()86     public void testGetSetPlain() {
87         AtomicBoolean ai = new AtomicBoolean(true);
88         assertEquals(true, ai.get());
89         ai.setPlain(false);
90         assertEquals(false, ai.get());
91         ai.setPlain(true);
92         assertEquals(true, ai.get());
93     }
94 
95     /**
96      * get returns the last value setOpaque
97      */
testGetSetOpaque()98     public void testGetSetOpaque() {
99         AtomicBoolean ai = new AtomicBoolean(true);
100         assertEquals(true, ai.get());
101         ai.setOpaque(false);
102         assertEquals(false, ai.get());
103         ai.setOpaque(true);
104         assertEquals(true, ai.get());
105     }
106 
107     /**
108      * get returns the last value setRelease
109      */
testGetSetRelease()110     public void testGetSetRelease() {
111         AtomicBoolean ai = new AtomicBoolean(true);
112         assertEquals(true, ai.get());
113         ai.setRelease(false);
114         assertEquals(false, ai.get());
115         ai.setRelease(true);
116         assertEquals(true, ai.get());
117     }
118 
119     /**
120      * compareAndExchange succeeds in changing value if equal to
121      * expected else fails
122      */
testCompareAndExchange()123     public void testCompareAndExchange() {
124         AtomicBoolean ai = new AtomicBoolean(true);
125         assertEquals(true, ai.compareAndExchange(true, false));
126         assertEquals(false, ai.compareAndExchange(false, false));
127         assertEquals(false, ai.get());
128         assertEquals(false, ai.compareAndExchange(true, true));
129         assertEquals(false, ai.get());
130         assertEquals(false, ai.compareAndExchange(false, true));
131         assertEquals(true, ai.get());
132     }
133 
134     /**
135      * compareAndExchangeAcquire succeeds in changing value if equal to
136      * expected else fails
137      */
testCompareAndExchangeAcquire()138     public void testCompareAndExchangeAcquire() {
139         AtomicBoolean ai = new AtomicBoolean(true);
140         assertEquals(true, ai.compareAndExchangeAcquire(true, false));
141         assertEquals(false, ai.compareAndExchangeAcquire(false, false));
142         assertEquals(false, ai.get());
143         assertEquals(false, ai.compareAndExchangeAcquire(true, true));
144         assertEquals(false, ai.get());
145         assertEquals(false, ai.compareAndExchangeAcquire(false, true));
146         assertEquals(true, ai.get());
147     }
148 
149     /**
150      * compareAndExchangeRelease succeeds in changing value if equal to
151      * expected else fails
152      */
testCompareAndExchangeRelease()153     public void testCompareAndExchangeRelease() {
154         AtomicBoolean ai = new AtomicBoolean(true);
155         assertEquals(true, ai.compareAndExchangeRelease(true, false));
156         assertEquals(false, ai.compareAndExchangeRelease(false, false));
157         assertEquals(false, ai.get());
158         assertEquals(false, ai.compareAndExchangeRelease(true, true));
159         assertEquals(false, ai.get());
160         assertEquals(false, ai.compareAndExchangeRelease(false, true));
161         assertEquals(true, ai.get());
162     }
163 
164     /**
165      * repeated weakCompareAndSetPlain succeeds in changing value when equal
166      * to expected
167      */
testWeakCompareAndSetPlain()168     public void testWeakCompareAndSetPlain() {
169         AtomicBoolean ai = new AtomicBoolean(true);
170         do {} while (!ai.weakCompareAndSetPlain(true, false));
171         do {} while (!ai.weakCompareAndSetPlain(false, false));
172         assertFalse(ai.get());
173         do {} while (!ai.weakCompareAndSetPlain(false, true));
174         assertTrue(ai.get());
175     }
176 
177     /**
178      * repeated weakCompareAndSetVolatile succeeds in changing value when equal
179      * to expected
180      */
testWeakCompareAndSetVolatile()181     public void testWeakCompareAndSetVolatile() {
182         AtomicBoolean ai = new AtomicBoolean(true);
183         do {} while (!ai.weakCompareAndSetVolatile(true, false));
184         do {} while (!ai.weakCompareAndSetVolatile(false, false));
185         assertEquals(false, ai.get());
186         do {} while (!ai.weakCompareAndSetVolatile(false, true));
187         assertEquals(true, ai.get());
188     }
189 
190     /**
191      * repeated weakCompareAndSetAcquire succeeds in changing value when equal
192      * to expected
193      */
testWeakCompareAndSetAcquire()194     public void testWeakCompareAndSetAcquire() {
195         AtomicBoolean ai = new AtomicBoolean(true);
196         do {} while (!ai.weakCompareAndSetAcquire(true, false));
197         do {} while (!ai.weakCompareAndSetAcquire(false, false));
198         assertEquals(false, ai.get());
199         do {} while (!ai.weakCompareAndSetAcquire(false, true));
200         assertEquals(true, ai.get());
201     }
202 
203     /**
204      * repeated weakCompareAndSetRelease succeeds in changing value when equal
205      * to expected
206      */
testWeakCompareAndSetRelease()207     public void testWeakCompareAndSetRelease() {
208         AtomicBoolean ai = new AtomicBoolean(true);
209         do {} while (!ai.weakCompareAndSetRelease(true, false));
210         do {} while (!ai.weakCompareAndSetRelease(false, false));
211         assertEquals(false, ai.get());
212         do {} while (!ai.weakCompareAndSetRelease(false, true));
213         assertEquals(true, ai.get());
214     }
215 
216 }
217