1 |
dl |
1.1 |
/* |
2 |
dl |
1.6 |
* 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 |
jsr166 |
1.18 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
jsr166 |
1.10 |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
|
* Pat Fisher, Mike Judd. |
7 |
dl |
1.1 |
*/ |
8 |
|
|
|
9 |
jsr166 |
1.19 |
import java.util.concurrent.atomic.AtomicBoolean; |
10 |
dl |
1.1 |
|
11 |
jsr166 |
1.22 |
import junit.framework.Test; |
12 |
|
|
import junit.framework.TestSuite; |
13 |
|
|
|
14 |
dl |
1.3 |
public class AtomicBooleanTest extends JSR166TestCase { |
15 |
jsr166 |
1.17 |
public static void main(String[] args) { |
16 |
jsr166 |
1.24 |
main(suite(), args); |
17 |
dl |
1.1 |
} |
18 |
|
|
public static Test suite() { |
19 |
|
|
return new TestSuite(AtomicBooleanTest.class); |
20 |
|
|
} |
21 |
|
|
|
22 |
dl |
1.4 |
/** |
23 |
dl |
1.5 |
* constructor initializes to given value |
24 |
dl |
1.4 |
*/ |
25 |
|
|
public void testConstructor() { |
26 |
jsr166 |
1.16 |
assertTrue(new AtomicBoolean(true).get()); |
27 |
|
|
assertFalse(new AtomicBoolean(false).get()); |
28 |
dl |
1.1 |
} |
29 |
|
|
|
30 |
dl |
1.4 |
/** |
31 |
dl |
1.7 |
* default constructed initializes to false |
32 |
dl |
1.4 |
*/ |
33 |
|
|
public void testConstructor2() { |
34 |
dl |
1.1 |
AtomicBoolean ai = new AtomicBoolean(); |
35 |
jsr166 |
1.16 |
assertFalse(ai.get()); |
36 |
dl |
1.1 |
} |
37 |
|
|
|
38 |
dl |
1.4 |
/** |
39 |
dl |
1.5 |
* get returns the last value set |
40 |
dl |
1.4 |
*/ |
41 |
|
|
public void testGetSet() { |
42 |
dl |
1.1 |
AtomicBoolean ai = new AtomicBoolean(true); |
43 |
jsr166 |
1.16 |
assertTrue(ai.get()); |
44 |
jsr166 |
1.15 |
ai.set(false); |
45 |
jsr166 |
1.16 |
assertFalse(ai.get()); |
46 |
jsr166 |
1.15 |
ai.set(true); |
47 |
jsr166 |
1.16 |
assertTrue(ai.get()); |
48 |
dl |
1.1 |
} |
49 |
dl |
1.5 |
|
50 |
dl |
1.4 |
/** |
51 |
dl |
1.9 |
* get returns the last value lazySet in same thread |
52 |
|
|
*/ |
53 |
|
|
public void testGetLazySet() { |
54 |
|
|
AtomicBoolean ai = new AtomicBoolean(true); |
55 |
jsr166 |
1.16 |
assertTrue(ai.get()); |
56 |
jsr166 |
1.15 |
ai.lazySet(false); |
57 |
jsr166 |
1.16 |
assertFalse(ai.get()); |
58 |
jsr166 |
1.15 |
ai.lazySet(true); |
59 |
jsr166 |
1.16 |
assertTrue(ai.get()); |
60 |
dl |
1.9 |
} |
61 |
|
|
|
62 |
|
|
/** |
63 |
dl |
1.5 |
* compareAndSet succeeds in changing value if equal to expected else fails |
64 |
dl |
1.4 |
*/ |
65 |
|
|
public void testCompareAndSet() { |
66 |
dl |
1.1 |
AtomicBoolean ai = new AtomicBoolean(true); |
67 |
jsr166 |
1.20 |
assertTrue(ai.compareAndSet(true, false)); |
68 |
jsr166 |
1.16 |
assertFalse(ai.get()); |
69 |
jsr166 |
1.20 |
assertTrue(ai.compareAndSet(false, false)); |
70 |
jsr166 |
1.16 |
assertFalse(ai.get()); |
71 |
jsr166 |
1.20 |
assertFalse(ai.compareAndSet(true, false)); |
72 |
jsr166 |
1.16 |
assertFalse(ai.get()); |
73 |
jsr166 |
1.20 |
assertTrue(ai.compareAndSet(false, true)); |
74 |
jsr166 |
1.16 |
assertTrue(ai.get()); |
75 |
dl |
1.1 |
} |
76 |
|
|
|
77 |
dl |
1.4 |
/** |
78 |
dl |
1.5 |
* compareAndSet in one thread enables another waiting for value |
79 |
|
|
* to succeed |
80 |
|
|
*/ |
81 |
jsr166 |
1.13 |
public void testCompareAndSetInMultipleThreads() throws Exception { |
82 |
dl |
1.5 |
final AtomicBoolean ai = new AtomicBoolean(true); |
83 |
jsr166 |
1.14 |
Thread t = new Thread(new CheckedRunnable() { |
84 |
|
|
public void realRun() { |
85 |
|
|
while (!ai.compareAndSet(false, true)) Thread.yield(); |
86 |
|
|
}}); |
87 |
jsr166 |
1.13 |
|
88 |
|
|
t.start(); |
89 |
|
|
assertTrue(ai.compareAndSet(true, false)); |
90 |
|
|
t.join(LONG_DELAY_MS); |
91 |
|
|
assertFalse(t.isAlive()); |
92 |
dl |
1.5 |
} |
93 |
|
|
|
94 |
|
|
/** |
95 |
|
|
* repeated weakCompareAndSet succeeds in changing value when equal |
96 |
jsr166 |
1.10 |
* to expected |
97 |
dl |
1.4 |
*/ |
98 |
dl |
1.30 |
@SuppressWarnings("deprecation") |
99 |
dl |
1.4 |
public void testWeakCompareAndSet() { |
100 |
dl |
1.1 |
AtomicBoolean ai = new AtomicBoolean(true); |
101 |
jsr166 |
1.23 |
do {} while (!ai.weakCompareAndSet(true, false)); |
102 |
jsr166 |
1.16 |
assertFalse(ai.get()); |
103 |
jsr166 |
1.23 |
do {} while (!ai.weakCompareAndSet(false, false)); |
104 |
jsr166 |
1.16 |
assertFalse(ai.get()); |
105 |
jsr166 |
1.23 |
do {} while (!ai.weakCompareAndSet(false, true)); |
106 |
jsr166 |
1.16 |
assertTrue(ai.get()); |
107 |
dl |
1.1 |
} |
108 |
|
|
|
109 |
dl |
1.4 |
/** |
110 |
dl |
1.5 |
* getAndSet returns previous value and sets to given value |
111 |
dl |
1.4 |
*/ |
112 |
|
|
public void testGetAndSet() { |
113 |
jsr166 |
1.28 |
AtomicBoolean ai = new AtomicBoolean(); |
114 |
|
|
boolean[] booleans = { false, true }; |
115 |
|
|
for (boolean before : booleans) |
116 |
|
|
for (boolean after : booleans) { |
117 |
|
|
ai.set(before); |
118 |
|
|
assertEquals(before, ai.getAndSet(after)); |
119 |
|
|
assertEquals(after, ai.get()); |
120 |
|
|
} |
121 |
dl |
1.2 |
} |
122 |
|
|
|
123 |
dl |
1.4 |
/** |
124 |
jsr166 |
1.29 |
* a deserialized/reserialized atomic holds same value |
125 |
dl |
1.4 |
*/ |
126 |
jsr166 |
1.13 |
public void testSerialization() throws Exception { |
127 |
jsr166 |
1.19 |
AtomicBoolean x = new AtomicBoolean(); |
128 |
|
|
AtomicBoolean y = serialClone(x); |
129 |
|
|
x.set(true); |
130 |
|
|
AtomicBoolean z = serialClone(x); |
131 |
|
|
assertTrue(x.get()); |
132 |
|
|
assertFalse(y.get()); |
133 |
|
|
assertTrue(z.get()); |
134 |
dl |
1.1 |
} |
135 |
|
|
|
136 |
dl |
1.8 |
/** |
137 |
|
|
* toString returns current value. |
138 |
jsr166 |
1.10 |
*/ |
139 |
dl |
1.8 |
public void testToString() { |
140 |
jsr166 |
1.10 |
AtomicBoolean ai = new AtomicBoolean(); |
141 |
jsr166 |
1.21 |
assertEquals(Boolean.toString(false), ai.toString()); |
142 |
dl |
1.8 |
ai.set(true); |
143 |
jsr166 |
1.21 |
assertEquals(Boolean.toString(true), ai.toString()); |
144 |
dl |
1.8 |
} |
145 |
dl |
1.1 |
|
146 |
|
|
} |