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 |
import java.io.*; |
11 |
|
12 |
public class AtomicReferenceArrayTest extends JSR166TestCase |
13 |
{ |
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 |
/** |
22 |
* |
23 |
*/ |
24 |
public void testConstructor(){ |
25 |
AtomicReferenceArray<Integer> ai = new AtomicReferenceArray<Integer>(SIZE); |
26 |
for (int i = 0; i < SIZE; ++i) { |
27 |
assertNull(ai.get(i)); |
28 |
} |
29 |
} |
30 |
|
31 |
/** |
32 |
* |
33 |
*/ |
34 |
public void testGetSet(){ |
35 |
AtomicReferenceArray ai = new AtomicReferenceArray(SIZE); |
36 |
for (int i = 0; i < SIZE; ++i) { |
37 |
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 |
/** |
47 |
* |
48 |
*/ |
49 |
public void testCompareAndSet(){ |
50 |
AtomicReferenceArray ai = new AtomicReferenceArray(SIZE); |
51 |
for (int i = 0; i < SIZE; ++i) { |
52 |
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 |
/** |
64 |
* |
65 |
*/ |
66 |
public void testWeakCompareAndSet(){ |
67 |
AtomicReferenceArray ai = new AtomicReferenceArray(SIZE); |
68 |
for (int i = 0; i < SIZE; ++i) { |
69 |
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 |
/** |
79 |
* |
80 |
*/ |
81 |
public void testGetAndSet(){ |
82 |
AtomicReferenceArray ai = new AtomicReferenceArray(SIZE); |
83 |
for (int i = 0; i < SIZE; ++i) { |
84 |
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 |
/** |
92 |
* |
93 |
*/ |
94 |
public void testSerialization() { |
95 |
AtomicReferenceArray l = new AtomicReferenceArray(SIZE); |
96 |
for (int i = 0; i < SIZE; ++i) { |
97 |
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 |
for (int i = 0; i < SIZE; ++i) { |
111 |
assertEquals(r.get(i), l.get(i)); |
112 |
} |
113 |
} catch(Exception e){ |
114 |
unexpectedException(); |
115 |
} |
116 |
} |
117 |
|
118 |
} |