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 |
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.*; |
10 |
< |
import java.util.concurrent.atomic.*; |
11 |
< |
import java.io.*; |
9 |
> |
import java.util.concurrent.atomic.AtomicBoolean; |
10 |
> |
|
11 |
> |
import junit.framework.Test; |
12 |
> |
import junit.framework.TestSuite; |
13 |
|
|
14 |
|
public class AtomicBooleanTest extends JSR166TestCase { |
15 |
< |
public static void main (String[] args) { |
16 |
< |
junit.textui.TestRunner.run (suite()); |
15 |
> |
public static void main(String[] args) { |
16 |
> |
main(suite(), args); |
17 |
|
} |
18 |
|
public static Test suite() { |
19 |
|
return new TestSuite(AtomicBooleanTest.class); |
64 |
|
*/ |
65 |
|
public void testCompareAndSet() { |
66 |
|
AtomicBoolean ai = new AtomicBoolean(true); |
67 |
< |
assertTrue(ai.compareAndSet(true,false)); |
67 |
> |
assertTrue(ai.compareAndSet(true, false)); |
68 |
|
assertFalse(ai.get()); |
69 |
< |
assertTrue(ai.compareAndSet(false,false)); |
69 |
> |
assertTrue(ai.compareAndSet(false, false)); |
70 |
|
assertFalse(ai.get()); |
71 |
< |
assertFalse(ai.compareAndSet(true,false)); |
71 |
> |
assertFalse(ai.compareAndSet(true, false)); |
72 |
|
assertFalse(ai.get()); |
73 |
< |
assertTrue(ai.compareAndSet(false,true)); |
73 |
> |
assertTrue(ai.compareAndSet(false, true)); |
74 |
|
assertTrue(ai.get()); |
75 |
|
} |
76 |
|
|
97 |
|
*/ |
98 |
|
public void testWeakCompareAndSet() { |
99 |
|
AtomicBoolean ai = new AtomicBoolean(true); |
100 |
< |
while (!ai.weakCompareAndSet(true,false)); |
100 |
> |
do {} while (!ai.weakCompareAndSet(true, false)); |
101 |
|
assertFalse(ai.get()); |
102 |
< |
while (!ai.weakCompareAndSet(false,false)); |
102 |
> |
do {} while (!ai.weakCompareAndSet(false, false)); |
103 |
|
assertFalse(ai.get()); |
104 |
< |
while (!ai.weakCompareAndSet(false,true)); |
104 |
> |
do {} while (!ai.weakCompareAndSet(false, true)); |
105 |
|
assertTrue(ai.get()); |
106 |
|
} |
107 |
|
|
110 |
|
*/ |
111 |
|
public void testGetAndSet() { |
112 |
|
AtomicBoolean ai = new AtomicBoolean(true); |
113 |
< |
assertEquals(true,ai.getAndSet(false)); |
114 |
< |
assertEquals(false,ai.getAndSet(false)); |
115 |
< |
assertEquals(false,ai.getAndSet(true)); |
113 |
> |
assertEquals(true, ai.getAndSet(false)); |
114 |
> |
assertEquals(false, ai.getAndSet(false)); |
115 |
> |
assertEquals(false, ai.getAndSet(true)); |
116 |
|
assertTrue(ai.get()); |
117 |
|
} |
118 |
|
|
120 |
|
* a deserialized serialized atomic holds same value |
121 |
|
*/ |
122 |
|
public void testSerialization() throws Exception { |
123 |
< |
AtomicBoolean l = new AtomicBoolean(); |
124 |
< |
|
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(); |
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()); |
123 |
> |
AtomicBoolean x = new AtomicBoolean(); |
124 |
> |
AtomicBoolean y = serialClone(x); |
125 |
> |
x.set(true); |
126 |
> |
AtomicBoolean z = serialClone(x); |
127 |
> |
assertTrue(x.get()); |
128 |
> |
assertFalse(y.get()); |
129 |
> |
assertTrue(z.get()); |
130 |
|
} |
131 |
|
|
132 |
|
/** |
134 |
|
*/ |
135 |
|
public void testToString() { |
136 |
|
AtomicBoolean ai = new AtomicBoolean(); |
137 |
< |
assertEquals(ai.toString(), Boolean.toString(false)); |
137 |
> |
assertEquals(Boolean.toString(false), ai.toString()); |
138 |
|
ai.set(true); |
139 |
< |
assertEquals(ai.toString(), Boolean.toString(true)); |
139 |
> |
assertEquals(Boolean.toString(true), ai.toString()); |
140 |
|
} |
141 |
|
|
142 |
|
} |