1 |
dl |
1.1 |
/* |
2 |
|
|
* Written by members of JCP JSR-166 Expert Group and released to the |
3 |
|
|
* public domain. Use, modify, and redistribute this code in any way |
4 |
|
|
* without acknowledgement. Other contributors include Andrew Wright, |
5 |
|
|
* Jeffrey Hayes, Pat Fischer, Mike Judd. |
6 |
|
|
*/ |
7 |
|
|
|
8 |
|
|
import junit.framework.*; |
9 |
|
|
import java.util.concurrent.atomic.*; |
10 |
dl |
1.2 |
import java.io.*; |
11 |
dl |
1.1 |
|
12 |
dl |
1.3 |
public class AtomicReferenceTest extends JSR166TestCase { |
13 |
dl |
1.1 |
public static void main (String[] args) { |
14 |
|
|
junit.textui.TestRunner.run (suite()); |
15 |
|
|
} |
16 |
|
|
public static Test suite() { |
17 |
|
|
return new TestSuite(AtomicReferenceTest.class); |
18 |
|
|
} |
19 |
|
|
|
20 |
|
|
public void testConstructor(){ |
21 |
|
|
AtomicReference ai = new AtomicReference(one); |
22 |
|
|
assertEquals(one,ai.get()); |
23 |
|
|
} |
24 |
|
|
|
25 |
|
|
public void testConstructor2(){ |
26 |
|
|
AtomicReference ai = new AtomicReference(); |
27 |
|
|
assertNull(ai.get()); |
28 |
|
|
} |
29 |
|
|
|
30 |
|
|
public void testGetSet(){ |
31 |
|
|
AtomicReference ai = new AtomicReference(one); |
32 |
|
|
assertEquals(one,ai.get()); |
33 |
|
|
ai.set(two); |
34 |
|
|
assertEquals(two,ai.get()); |
35 |
|
|
ai.set(m3); |
36 |
|
|
assertEquals(m3,ai.get()); |
37 |
|
|
|
38 |
|
|
} |
39 |
|
|
public void testCompareAndSet(){ |
40 |
|
|
AtomicReference ai = new AtomicReference(one); |
41 |
|
|
assertTrue(ai.compareAndSet(one,two)); |
42 |
|
|
assertTrue(ai.compareAndSet(two,m4)); |
43 |
|
|
assertEquals(m4,ai.get()); |
44 |
|
|
assertFalse(ai.compareAndSet(m5,seven)); |
45 |
|
|
assertFalse((seven.equals(ai.get()))); |
46 |
|
|
assertTrue(ai.compareAndSet(m4,seven)); |
47 |
|
|
assertEquals(seven,ai.get()); |
48 |
|
|
} |
49 |
|
|
|
50 |
|
|
public void testWeakCompareAndSet(){ |
51 |
|
|
AtomicReference ai = new AtomicReference(one); |
52 |
|
|
while(!ai.weakCompareAndSet(one,two)); |
53 |
|
|
while(!ai.weakCompareAndSet(two,m4)); |
54 |
|
|
assertEquals(m4,ai.get()); |
55 |
|
|
while(!ai.weakCompareAndSet(m4,seven)); |
56 |
|
|
assertEquals(seven,ai.get()); |
57 |
|
|
} |
58 |
|
|
|
59 |
|
|
public void testGetAndSet(){ |
60 |
|
|
AtomicReference ai = new AtomicReference(one); |
61 |
|
|
assertEquals(one,ai.getAndSet(zero)); |
62 |
|
|
assertEquals(zero,ai.getAndSet(m10)); |
63 |
|
|
assertEquals(m10,ai.getAndSet(one)); |
64 |
dl |
1.2 |
} |
65 |
|
|
|
66 |
|
|
public void testSerialization() { |
67 |
|
|
AtomicReference l = new AtomicReference(); |
68 |
|
|
|
69 |
|
|
try { |
70 |
|
|
l.set(one); |
71 |
|
|
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
72 |
|
|
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
73 |
|
|
out.writeObject(l); |
74 |
|
|
out.close(); |
75 |
|
|
|
76 |
|
|
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
77 |
|
|
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
78 |
|
|
AtomicReference r = (AtomicReference) in.readObject(); |
79 |
|
|
assertEquals(l.get(), r.get()); |
80 |
|
|
} catch(Exception e){ |
81 |
|
|
e.printStackTrace(); |
82 |
|
|
fail("unexpected exception"); |
83 |
|
|
} |
84 |
dl |
1.1 |
} |
85 |
|
|
|
86 |
|
|
} |
87 |
|
|
|