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