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.*; |
10 |
> |
import java.util.concurrent.atomic.AtomicInteger; |
11 |
|
|
12 |
|
public class AtomicIntegerTest extends JSR166TestCase { |
13 |
< |
public static void main (String[] args) { |
14 |
< |
junit.textui.TestRunner.run (suite()); |
13 |
> |
public static void main(String[] args) { |
14 |
> |
junit.textui.TestRunner.run(suite()); |
15 |
|
} |
16 |
|
public static Test suite() { |
17 |
|
return new TestSuite(AtomicIntegerTest.class); |
47 |
|
assertEquals(2,ai.get()); |
48 |
|
ai.set(-3); |
49 |
|
assertEquals(-3,ai.get()); |
51 |
– |
|
50 |
|
} |
51 |
|
|
52 |
|
/** |
59 |
|
assertEquals(2,ai.get()); |
60 |
|
ai.lazySet(-3); |
61 |
|
assertEquals(-3,ai.get()); |
64 |
– |
|
62 |
|
} |
63 |
+ |
|
64 |
|
/** |
65 |
|
* compareAndSet succeeds in changing value if equal to expected else fails |
66 |
|
*/ |
70 |
|
assertTrue(ai.compareAndSet(2,-4)); |
71 |
|
assertEquals(-4,ai.get()); |
72 |
|
assertFalse(ai.compareAndSet(-5,7)); |
73 |
< |
assertFalse((7 == ai.get())); |
73 |
> |
assertEquals(-4,ai.get()); |
74 |
|
assertTrue(ai.compareAndSet(-4,7)); |
75 |
|
assertEquals(7,ai.get()); |
76 |
|
} |
192 |
|
* a deserialized serialized atomic holds same value |
193 |
|
*/ |
194 |
|
public void testSerialization() throws Exception { |
195 |
< |
AtomicInteger l = new AtomicInteger(); |
196 |
< |
|
197 |
< |
l.set(22); |
198 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
199 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
200 |
< |
out.writeObject(l); |
201 |
< |
out.close(); |
202 |
< |
|
205 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
206 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
207 |
< |
AtomicInteger r = (AtomicInteger) in.readObject(); |
208 |
< |
assertEquals(l.get(), r.get()); |
195 |
> |
AtomicInteger x = new AtomicInteger(); |
196 |
> |
AtomicInteger y = serialClone(x); |
197 |
> |
assertTrue(x != y); |
198 |
> |
x.set(22); |
199 |
> |
AtomicInteger z = serialClone(x); |
200 |
> |
assertEquals(22, x.get()); |
201 |
> |
assertEquals(0, y.get()); |
202 |
> |
assertEquals(22, z.get()); |
203 |
|
} |
204 |
|
|
205 |
|
/** |