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 |
dl |
1.4 |
/** |
22 |
|
|
* |
23 |
|
|
*/ |
24 |
dl |
1.1 |
public void testConstructor(){ |
25 |
dl |
1.3 |
AtomicReferenceArray<Integer> ai = new AtomicReferenceArray<Integer>(SIZE); |
26 |
|
|
for (int i = 0; i < SIZE; ++i) { |
27 |
dl |
1.1 |
assertNull(ai.get(i)); |
28 |
|
|
} |
29 |
|
|
} |
30 |
|
|
|
31 |
dl |
1.4 |
/** |
32 |
|
|
* |
33 |
|
|
*/ |
34 |
dl |
1.1 |
public void testGetSet(){ |
35 |
dl |
1.3 |
AtomicReferenceArray ai = new AtomicReferenceArray(SIZE); |
36 |
|
|
for (int i = 0; i < SIZE; ++i) { |
37 |
dl |
1.1 |
ai.set(i, one); |
38 |
|
|
assertEquals(one,ai.get(i)); |
39 |
|
|
ai.set(i, two); |
40 |
|
|
assertEquals(two,ai.get(i)); |
41 |
|
|
ai.set(i, m3); |
42 |
|
|
assertEquals(m3,ai.get(i)); |
43 |
|
|
} |
44 |
|
|
} |
45 |
|
|
|
46 |
dl |
1.4 |
/** |
47 |
|
|
* |
48 |
|
|
*/ |
49 |
dl |
1.1 |
public void testCompareAndSet(){ |
50 |
dl |
1.3 |
AtomicReferenceArray ai = new AtomicReferenceArray(SIZE); |
51 |
|
|
for (int i = 0; i < SIZE; ++i) { |
52 |
dl |
1.1 |
ai.set(i, one); |
53 |
|
|
assertTrue(ai.compareAndSet(i, one,two)); |
54 |
|
|
assertTrue(ai.compareAndSet(i, two,m4)); |
55 |
|
|
assertEquals(m4,ai.get(i)); |
56 |
|
|
assertFalse(ai.compareAndSet(i, m5,seven)); |
57 |
|
|
assertFalse((seven.equals(ai.get(i)))); |
58 |
|
|
assertTrue(ai.compareAndSet(i, m4,seven)); |
59 |
|
|
assertEquals(seven,ai.get(i)); |
60 |
|
|
} |
61 |
|
|
} |
62 |
|
|
|
63 |
dl |
1.4 |
/** |
64 |
|
|
* |
65 |
|
|
*/ |
66 |
dl |
1.1 |
public void testWeakCompareAndSet(){ |
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 |
|
|
while(!ai.weakCompareAndSet(i, one,two)); |
71 |
|
|
while(!ai.weakCompareAndSet(i, two,m4)); |
72 |
|
|
assertEquals(m4,ai.get(i)); |
73 |
|
|
while(!ai.weakCompareAndSet(i, m4,seven)); |
74 |
|
|
assertEquals(seven,ai.get(i)); |
75 |
|
|
} |
76 |
|
|
} |
77 |
|
|
|
78 |
dl |
1.4 |
/** |
79 |
|
|
* |
80 |
|
|
*/ |
81 |
dl |
1.1 |
public void testGetAndSet(){ |
82 |
dl |
1.3 |
AtomicReferenceArray ai = new AtomicReferenceArray(SIZE); |
83 |
|
|
for (int i = 0; i < SIZE; ++i) { |
84 |
dl |
1.1 |
ai.set(i, one); |
85 |
|
|
assertEquals(one,ai.getAndSet(i,zero)); |
86 |
|
|
assertEquals(0,ai.getAndSet(i,m10)); |
87 |
|
|
assertEquals(m10,ai.getAndSet(i,one)); |
88 |
|
|
} |
89 |
|
|
} |
90 |
|
|
|
91 |
dl |
1.4 |
/** |
92 |
|
|
* |
93 |
|
|
*/ |
94 |
dl |
1.2 |
public void testSerialization() { |
95 |
dl |
1.3 |
AtomicReferenceArray l = new AtomicReferenceArray(SIZE); |
96 |
|
|
for (int i = 0; i < SIZE; ++i) { |
97 |
dl |
1.2 |
l.set(i, new Integer(-i)); |
98 |
|
|
} |
99 |
|
|
|
100 |
|
|
try { |
101 |
|
|
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
102 |
|
|
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
103 |
|
|
out.writeObject(l); |
104 |
|
|
out.close(); |
105 |
|
|
|
106 |
|
|
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
107 |
|
|
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
108 |
|
|
AtomicReferenceArray r = (AtomicReferenceArray) in.readObject(); |
109 |
|
|
assertEquals(l.length(), r.length()); |
110 |
dl |
1.3 |
for (int i = 0; i < SIZE; ++i) { |
111 |
dl |
1.2 |
assertEquals(r.get(i), l.get(i)); |
112 |
|
|
} |
113 |
|
|
} catch(Exception e){ |
114 |
dl |
1.4 |
unexpectedException(); |
115 |
dl |
1.2 |
} |
116 |
|
|
} |
117 |
dl |
1.1 |
|
118 |
|
|
} |