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