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/licenses/publicdomain |
5 |
< |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
< |
* Pat Fisher, Mike Judd. |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
> |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
> |
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
|
import junit.framework.*; |
11 |
|
import java.io.*; |
12 |
|
|
13 |
|
public class AtomicBooleanTest extends JSR166TestCase { |
14 |
< |
public static void main (String[] args) { |
15 |
< |
junit.textui.TestRunner.run (suite()); |
14 |
> |
public static void main(String[] args) { |
15 |
> |
junit.textui.TestRunner.run(suite()); |
16 |
|
} |
17 |
|
public static Test suite() { |
18 |
|
return new TestSuite(AtomicBooleanTest.class); |
22 |
|
* constructor initializes to given value |
23 |
|
*/ |
24 |
|
public void testConstructor() { |
25 |
< |
AtomicBoolean ai = new AtomicBoolean(true); |
26 |
< |
assertEquals(true,ai.get()); |
25 |
> |
assertTrue(new AtomicBoolean(true).get()); |
26 |
> |
assertFalse(new AtomicBoolean(false).get()); |
27 |
|
} |
28 |
|
|
29 |
|
/** |
31 |
|
*/ |
32 |
|
public void testConstructor2() { |
33 |
|
AtomicBoolean ai = new AtomicBoolean(); |
34 |
< |
assertEquals(false,ai.get()); |
34 |
> |
assertFalse(ai.get()); |
35 |
|
} |
36 |
|
|
37 |
|
/** |
39 |
|
*/ |
40 |
|
public void testGetSet() { |
41 |
|
AtomicBoolean ai = new AtomicBoolean(true); |
42 |
< |
assertEquals(true,ai.get()); |
43 |
< |
ai.set(false); |
44 |
< |
assertEquals(false,ai.get()); |
45 |
< |
ai.set(true); |
46 |
< |
assertEquals(true,ai.get()); |
47 |
< |
|
42 |
> |
assertTrue(ai.get()); |
43 |
> |
ai.set(false); |
44 |
> |
assertFalse(ai.get()); |
45 |
> |
ai.set(true); |
46 |
> |
assertTrue(ai.get()); |
47 |
|
} |
48 |
|
|
49 |
|
/** |
51 |
|
*/ |
52 |
|
public void testGetLazySet() { |
53 |
|
AtomicBoolean ai = new AtomicBoolean(true); |
54 |
< |
assertEquals(true,ai.get()); |
55 |
< |
ai.lazySet(false); |
56 |
< |
assertEquals(false,ai.get()); |
57 |
< |
ai.lazySet(true); |
58 |
< |
assertEquals(true,ai.get()); |
60 |
< |
|
54 |
> |
assertTrue(ai.get()); |
55 |
> |
ai.lazySet(false); |
56 |
> |
assertFalse(ai.get()); |
57 |
> |
ai.lazySet(true); |
58 |
> |
assertTrue(ai.get()); |
59 |
|
} |
60 |
|
|
61 |
|
/** |
63 |
|
*/ |
64 |
|
public void testCompareAndSet() { |
65 |
|
AtomicBoolean ai = new AtomicBoolean(true); |
66 |
< |
assertTrue(ai.compareAndSet(true,false)); |
67 |
< |
assertEquals(false,ai.get()); |
68 |
< |
assertTrue(ai.compareAndSet(false,false)); |
69 |
< |
assertEquals(false,ai.get()); |
70 |
< |
assertFalse(ai.compareAndSet(true,false)); |
71 |
< |
assertFalse((ai.get())); |
72 |
< |
assertTrue(ai.compareAndSet(false,true)); |
73 |
< |
assertEquals(true,ai.get()); |
66 |
> |
assertTrue(ai.compareAndSet(true,false)); |
67 |
> |
assertFalse(ai.get()); |
68 |
> |
assertTrue(ai.compareAndSet(false,false)); |
69 |
> |
assertFalse(ai.get()); |
70 |
> |
assertFalse(ai.compareAndSet(true,false)); |
71 |
> |
assertFalse(ai.get()); |
72 |
> |
assertTrue(ai.compareAndSet(false,true)); |
73 |
> |
assertTrue(ai.get()); |
74 |
|
} |
75 |
|
|
76 |
|
/** |
77 |
|
* compareAndSet in one thread enables another waiting for value |
78 |
|
* to succeed |
79 |
|
*/ |
80 |
< |
public void testCompareAndSetInMultipleThreads() { |
80 |
> |
public void testCompareAndSetInMultipleThreads() throws Exception { |
81 |
|
final AtomicBoolean ai = new AtomicBoolean(true); |
82 |
< |
Thread t = new Thread(new Runnable() { |
83 |
< |
public void run() { |
84 |
< |
while(!ai.compareAndSet(false, true)) Thread.yield(); |
85 |
< |
}}); |
86 |
< |
try { |
87 |
< |
t.start(); |
88 |
< |
assertTrue(ai.compareAndSet(true, false)); |
89 |
< |
t.join(LONG_DELAY_MS); |
90 |
< |
assertFalse(t.isAlive()); |
93 |
< |
} |
94 |
< |
catch(Exception e) { |
95 |
< |
unexpectedException(); |
96 |
< |
} |
82 |
> |
Thread t = new Thread(new CheckedRunnable() { |
83 |
> |
public void realRun() { |
84 |
> |
while (!ai.compareAndSet(false, true)) Thread.yield(); |
85 |
> |
}}); |
86 |
> |
|
87 |
> |
t.start(); |
88 |
> |
assertTrue(ai.compareAndSet(true, false)); |
89 |
> |
t.join(LONG_DELAY_MS); |
90 |
> |
assertFalse(t.isAlive()); |
91 |
|
} |
92 |
|
|
93 |
|
/** |
94 |
|
* repeated weakCompareAndSet succeeds in changing value when equal |
95 |
< |
* to expected |
95 |
> |
* to expected |
96 |
|
*/ |
97 |
|
public void testWeakCompareAndSet() { |
98 |
|
AtomicBoolean ai = new AtomicBoolean(true); |
99 |
< |
while(!ai.weakCompareAndSet(true,false)); |
100 |
< |
assertEquals(false,ai.get()); |
101 |
< |
while(!ai.weakCompareAndSet(false,false)); |
102 |
< |
assertEquals(false,ai.get()); |
103 |
< |
while(!ai.weakCompareAndSet(false,true)); |
104 |
< |
assertEquals(true,ai.get()); |
99 |
> |
while (!ai.weakCompareAndSet(true,false)); |
100 |
> |
assertFalse(ai.get()); |
101 |
> |
while (!ai.weakCompareAndSet(false,false)); |
102 |
> |
assertFalse(ai.get()); |
103 |
> |
while (!ai.weakCompareAndSet(false,true)); |
104 |
> |
assertTrue(ai.get()); |
105 |
|
} |
106 |
|
|
107 |
|
/** |
109 |
|
*/ |
110 |
|
public void testGetAndSet() { |
111 |
|
AtomicBoolean ai = new AtomicBoolean(true); |
112 |
< |
assertEquals(true,ai.getAndSet(false)); |
113 |
< |
assertEquals(false,ai.getAndSet(false)); |
114 |
< |
assertEquals(false,ai.getAndSet(true)); |
115 |
< |
assertEquals(true,ai.get()); |
112 |
> |
assertEquals(true,ai.getAndSet(false)); |
113 |
> |
assertEquals(false,ai.getAndSet(false)); |
114 |
> |
assertEquals(false,ai.getAndSet(true)); |
115 |
> |
assertTrue(ai.get()); |
116 |
|
} |
117 |
|
|
118 |
|
/** |
119 |
|
* a deserialized serialized atomic holds same value |
120 |
|
*/ |
121 |
< |
public void testSerialization() { |
121 |
> |
public void testSerialization() throws Exception { |
122 |
|
AtomicBoolean l = new AtomicBoolean(); |
123 |
|
|
124 |
< |
try { |
125 |
< |
l.set(true); |
126 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
127 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
128 |
< |
out.writeObject(l); |
129 |
< |
out.close(); |
130 |
< |
|
131 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
132 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
133 |
< |
AtomicBoolean r = (AtomicBoolean) in.readObject(); |
140 |
< |
assertEquals(l.get(), r.get()); |
141 |
< |
} catch(Exception e){ |
142 |
< |
e.printStackTrace(); |
143 |
< |
unexpectedException(); |
144 |
< |
} |
124 |
> |
l.set(true); |
125 |
> |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
126 |
> |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
127 |
> |
out.writeObject(l); |
128 |
> |
out.close(); |
129 |
> |
|
130 |
> |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
131 |
> |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
132 |
> |
AtomicBoolean r = (AtomicBoolean) in.readObject(); |
133 |
> |
assertEquals(l.get(), r.get()); |
134 |
|
} |
135 |
|
|
136 |
|
/** |
137 |
|
* toString returns current value. |
138 |
< |
*/ |
138 |
> |
*/ |
139 |
|
public void testToString() { |
140 |
< |
AtomicBoolean ai = new AtomicBoolean(); |
140 |
> |
AtomicBoolean ai = new AtomicBoolean(); |
141 |
|
assertEquals(ai.toString(), Boolean.toString(false)); |
142 |
|
ai.set(true); |
143 |
|
assertEquals(ai.toString(), Boolean.toString(true)); |