35394
|
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 |
* Other contributors include Andrew Wright, Jeffrey Hayes,
|
|
33 |
* Pat Fisher, Mike Judd.
|
|
34 |
*/
|
|
35 |
|
|
36 |
import java.util.concurrent.atomic.AtomicBoolean;
|
|
37 |
|
|
38 |
import junit.framework.Test;
|
|
39 |
import junit.framework.TestSuite;
|
|
40 |
|
|
41 |
public class AtomicBooleanTest extends JSR166TestCase {
|
|
42 |
public static void main(String[] args) {
|
|
43 |
main(suite(), args);
|
|
44 |
}
|
|
45 |
public static Test suite() {
|
|
46 |
return new TestSuite(AtomicBooleanTest.class);
|
|
47 |
}
|
|
48 |
|
|
49 |
/**
|
|
50 |
* constructor initializes to given value
|
|
51 |
*/
|
|
52 |
public void testConstructor() {
|
|
53 |
assertTrue(new AtomicBoolean(true).get());
|
|
54 |
assertFalse(new AtomicBoolean(false).get());
|
|
55 |
}
|
|
56 |
|
|
57 |
/**
|
|
58 |
* default constructed initializes to false
|
|
59 |
*/
|
|
60 |
public void testConstructor2() {
|
|
61 |
AtomicBoolean ai = new AtomicBoolean();
|
|
62 |
assertFalse(ai.get());
|
|
63 |
}
|
|
64 |
|
|
65 |
/**
|
|
66 |
* get returns the last value set
|
|
67 |
*/
|
|
68 |
public void testGetSet() {
|
|
69 |
AtomicBoolean ai = new AtomicBoolean(true);
|
|
70 |
assertTrue(ai.get());
|
|
71 |
ai.set(false);
|
|
72 |
assertFalse(ai.get());
|
|
73 |
ai.set(true);
|
|
74 |
assertTrue(ai.get());
|
|
75 |
}
|
|
76 |
|
|
77 |
/**
|
|
78 |
* get returns the last value lazySet in same thread
|
|
79 |
*/
|
|
80 |
public void testGetLazySet() {
|
|
81 |
AtomicBoolean ai = new AtomicBoolean(true);
|
|
82 |
assertTrue(ai.get());
|
|
83 |
ai.lazySet(false);
|
|
84 |
assertFalse(ai.get());
|
|
85 |
ai.lazySet(true);
|
|
86 |
assertTrue(ai.get());
|
|
87 |
}
|
|
88 |
|
|
89 |
/**
|
|
90 |
* compareAndSet succeeds in changing value if equal to expected else fails
|
|
91 |
*/
|
|
92 |
public void testCompareAndSet() {
|
|
93 |
AtomicBoolean ai = new AtomicBoolean(true);
|
|
94 |
assertTrue(ai.compareAndSet(true, false));
|
|
95 |
assertFalse(ai.get());
|
|
96 |
assertTrue(ai.compareAndSet(false, false));
|
|
97 |
assertFalse(ai.get());
|
|
98 |
assertFalse(ai.compareAndSet(true, false));
|
|
99 |
assertFalse(ai.get());
|
|
100 |
assertTrue(ai.compareAndSet(false, true));
|
|
101 |
assertTrue(ai.get());
|
|
102 |
}
|
|
103 |
|
|
104 |
/**
|
|
105 |
* compareAndSet in one thread enables another waiting for value
|
|
106 |
* to succeed
|
|
107 |
*/
|
|
108 |
public void testCompareAndSetInMultipleThreads() throws Exception {
|
|
109 |
final AtomicBoolean ai = new AtomicBoolean(true);
|
|
110 |
Thread t = new Thread(new CheckedRunnable() {
|
|
111 |
public void realRun() {
|
|
112 |
while (!ai.compareAndSet(false, true)) Thread.yield();
|
|
113 |
}});
|
|
114 |
|
|
115 |
t.start();
|
|
116 |
assertTrue(ai.compareAndSet(true, false));
|
|
117 |
t.join(LONG_DELAY_MS);
|
|
118 |
assertFalse(t.isAlive());
|
|
119 |
}
|
|
120 |
|
|
121 |
/**
|
|
122 |
* repeated weakCompareAndSet succeeds in changing value when equal
|
|
123 |
* to expected
|
|
124 |
*/
|
|
125 |
public void testWeakCompareAndSet() {
|
|
126 |
AtomicBoolean ai = new AtomicBoolean(true);
|
|
127 |
do {} while (!ai.weakCompareAndSet(true, false));
|
|
128 |
assertFalse(ai.get());
|
|
129 |
do {} while (!ai.weakCompareAndSet(false, false));
|
|
130 |
assertFalse(ai.get());
|
|
131 |
do {} while (!ai.weakCompareAndSet(false, true));
|
|
132 |
assertTrue(ai.get());
|
|
133 |
}
|
|
134 |
|
|
135 |
/**
|
|
136 |
* getAndSet returns previous value and sets to given value
|
|
137 |
*/
|
|
138 |
public void testGetAndSet() {
|
40277
|
139 |
AtomicBoolean ai = new AtomicBoolean();
|
|
140 |
boolean[] booleans = { false, true };
|
|
141 |
for (boolean before : booleans)
|
|
142 |
for (boolean after : booleans) {
|
|
143 |
ai.set(before);
|
|
144 |
assertEquals(before, ai.getAndSet(after));
|
|
145 |
assertEquals(after, ai.get());
|
|
146 |
}
|
35394
|
147 |
}
|
|
148 |
|
|
149 |
/**
|
|
150 |
* a deserialized serialized atomic holds same value
|
|
151 |
*/
|
|
152 |
public void testSerialization() throws Exception {
|
|
153 |
AtomicBoolean x = new AtomicBoolean();
|
|
154 |
AtomicBoolean y = serialClone(x);
|
|
155 |
x.set(true);
|
|
156 |
AtomicBoolean z = serialClone(x);
|
|
157 |
assertTrue(x.get());
|
|
158 |
assertFalse(y.get());
|
|
159 |
assertTrue(z.get());
|
|
160 |
}
|
|
161 |
|
|
162 |
/**
|
|
163 |
* toString returns current value.
|
|
164 |
*/
|
|
165 |
public void testToString() {
|
|
166 |
AtomicBoolean ai = new AtomicBoolean();
|
|
167 |
assertEquals(Boolean.toString(false), ai.toString());
|
|
168 |
ai.set(true);
|
|
169 |
assertEquals(Boolean.toString(true), ai.toString());
|
|
170 |
}
|
|
171 |
|
|
172 |
}
|