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 AtomicReferenceArrayTest extends JSR166TestCase |
13 |
dl |
1.1 |
{ |
14 |
|
|
public static void main (String[] args) { |
15 |
|
|
junit.textui.TestRunner.run (suite()); |
16 |
|
|
} |
17 |
|
|
public static Test suite() { |
18 |
|
|
return new TestSuite(AtomicReferenceArrayTest.class); |
19 |
|
|
} |
20 |
|
|
|
21 |
|
|
public void testConstructor(){ |
22 |
dl |
1.3 |
AtomicReferenceArray<Integer> ai = new AtomicReferenceArray<Integer>(SIZE); |
23 |
|
|
for (int i = 0; i < SIZE; ++i) { |
24 |
dl |
1.1 |
assertNull(ai.get(i)); |
25 |
|
|
} |
26 |
|
|
} |
27 |
|
|
|
28 |
|
|
public void testGetSet(){ |
29 |
dl |
1.3 |
AtomicReferenceArray ai = new AtomicReferenceArray(SIZE); |
30 |
|
|
for (int i = 0; i < SIZE; ++i) { |
31 |
dl |
1.1 |
ai.set(i, one); |
32 |
|
|
assertEquals(one,ai.get(i)); |
33 |
|
|
ai.set(i, two); |
34 |
|
|
assertEquals(two,ai.get(i)); |
35 |
|
|
ai.set(i, m3); |
36 |
|
|
assertEquals(m3,ai.get(i)); |
37 |
|
|
} |
38 |
|
|
} |
39 |
|
|
|
40 |
|
|
public void testCompareAndSet(){ |
41 |
dl |
1.3 |
AtomicReferenceArray ai = new AtomicReferenceArray(SIZE); |
42 |
|
|
for (int i = 0; i < SIZE; ++i) { |
43 |
dl |
1.1 |
ai.set(i, one); |
44 |
|
|
assertTrue(ai.compareAndSet(i, one,two)); |
45 |
|
|
assertTrue(ai.compareAndSet(i, two,m4)); |
46 |
|
|
assertEquals(m4,ai.get(i)); |
47 |
|
|
assertFalse(ai.compareAndSet(i, m5,seven)); |
48 |
|
|
assertFalse((seven.equals(ai.get(i)))); |
49 |
|
|
assertTrue(ai.compareAndSet(i, m4,seven)); |
50 |
|
|
assertEquals(seven,ai.get(i)); |
51 |
|
|
} |
52 |
|
|
} |
53 |
|
|
|
54 |
|
|
public void testWeakCompareAndSet(){ |
55 |
dl |
1.3 |
AtomicReferenceArray ai = new AtomicReferenceArray(SIZE); |
56 |
|
|
for (int i = 0; i < SIZE; ++i) { |
57 |
dl |
1.1 |
ai.set(i, one); |
58 |
|
|
while(!ai.weakCompareAndSet(i, one,two)); |
59 |
|
|
while(!ai.weakCompareAndSet(i, two,m4)); |
60 |
|
|
assertEquals(m4,ai.get(i)); |
61 |
|
|
while(!ai.weakCompareAndSet(i, m4,seven)); |
62 |
|
|
assertEquals(seven,ai.get(i)); |
63 |
|
|
} |
64 |
|
|
} |
65 |
|
|
|
66 |
|
|
public void testGetAndSet(){ |
67 |
dl |
1.3 |
AtomicReferenceArray ai = new AtomicReferenceArray(SIZE); |
68 |
|
|
for (int i = 0; i < SIZE; ++i) { |
69 |
dl |
1.1 |
ai.set(i, one); |
70 |
|
|
assertEquals(one,ai.getAndSet(i,zero)); |
71 |
|
|
assertEquals(0,ai.getAndSet(i,m10)); |
72 |
|
|
assertEquals(m10,ai.getAndSet(i,one)); |
73 |
|
|
} |
74 |
|
|
} |
75 |
|
|
|
76 |
dl |
1.2 |
public void testSerialization() { |
77 |
dl |
1.3 |
AtomicReferenceArray l = new AtomicReferenceArray(SIZE); |
78 |
|
|
for (int i = 0; i < SIZE; ++i) { |
79 |
dl |
1.2 |
l.set(i, new Integer(-i)); |
80 |
|
|
} |
81 |
|
|
|
82 |
|
|
try { |
83 |
|
|
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
84 |
|
|
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
85 |
|
|
out.writeObject(l); |
86 |
|
|
out.close(); |
87 |
|
|
|
88 |
|
|
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
89 |
|
|
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
90 |
|
|
AtomicReferenceArray r = (AtomicReferenceArray) in.readObject(); |
91 |
|
|
assertEquals(l.length(), r.length()); |
92 |
dl |
1.3 |
for (int i = 0; i < SIZE; ++i) { |
93 |
dl |
1.2 |
assertEquals(r.get(i), l.get(i)); |
94 |
|
|
} |
95 |
|
|
} catch(Exception e){ |
96 |
|
|
e.printStackTrace(); |
97 |
|
|
fail("unexpected exception"); |
98 |
|
|
} |
99 |
|
|
} |
100 |
dl |
1.1 |
|
101 |
|
|
} |