7 |
|
|
8 |
|
import junit.framework.*; |
9 |
|
import java.util.concurrent.atomic.*; |
10 |
+ |
import java.io.*; |
11 |
|
|
12 |
< |
public class AtomicReferenceArrayTest extends TestCase |
12 |
> |
public class AtomicReferenceArrayTest extends JSR166TestCase |
13 |
|
{ |
13 |
– |
static final int N = 10; |
14 |
– |
|
15 |
– |
static final Integer zero = new Integer(0); |
16 |
– |
static final Integer one = new Integer(1); |
17 |
– |
static final Integer two = new Integer(2); |
18 |
– |
static final Integer m3 = new Integer(-3); |
19 |
– |
static final Integer m4 = new Integer(-4); |
20 |
– |
static final Integer m5 = new Integer(-5); |
21 |
– |
static final Integer seven = new Integer(7); |
22 |
– |
static final Integer m10 = new Integer(-10); |
23 |
– |
|
14 |
|
public static void main (String[] args) { |
15 |
|
junit.textui.TestRunner.run (suite()); |
16 |
|
} |
19 |
|
} |
20 |
|
|
21 |
|
public void testConstructor(){ |
22 |
< |
AtomicReferenceArray<Integer> ai = new AtomicReferenceArray<Integer>(N); |
23 |
< |
for (int i = 0; i < N; ++i) { |
22 |
> |
AtomicReferenceArray<Integer> ai = new AtomicReferenceArray<Integer>(SIZE); |
23 |
> |
for (int i = 0; i < SIZE; ++i) { |
24 |
|
assertNull(ai.get(i)); |
25 |
|
} |
26 |
|
} |
27 |
|
|
28 |
|
public void testGetSet(){ |
29 |
< |
AtomicReferenceArray ai = new AtomicReferenceArray(N); |
30 |
< |
for (int i = 0; i < N; ++i) { |
29 |
> |
AtomicReferenceArray ai = new AtomicReferenceArray(SIZE); |
30 |
> |
for (int i = 0; i < SIZE; ++i) { |
31 |
|
ai.set(i, one); |
32 |
|
assertEquals(one,ai.get(i)); |
33 |
|
ai.set(i, two); |
38 |
|
} |
39 |
|
|
40 |
|
public void testCompareAndSet(){ |
41 |
< |
AtomicReferenceArray ai = new AtomicReferenceArray(N); |
42 |
< |
for (int i = 0; i < N; ++i) { |
41 |
> |
AtomicReferenceArray ai = new AtomicReferenceArray(SIZE); |
42 |
> |
for (int i = 0; i < SIZE; ++i) { |
43 |
|
ai.set(i, one); |
44 |
|
assertTrue(ai.compareAndSet(i, one,two)); |
45 |
|
assertTrue(ai.compareAndSet(i, two,m4)); |
52 |
|
} |
53 |
|
|
54 |
|
public void testWeakCompareAndSet(){ |
55 |
< |
AtomicReferenceArray ai = new AtomicReferenceArray(N); |
56 |
< |
for (int i = 0; i < N; ++i) { |
55 |
> |
AtomicReferenceArray ai = new AtomicReferenceArray(SIZE); |
56 |
> |
for (int i = 0; i < SIZE; ++i) { |
57 |
|
ai.set(i, one); |
58 |
|
while(!ai.weakCompareAndSet(i, one,two)); |
59 |
|
while(!ai.weakCompareAndSet(i, two,m4)); |
64 |
|
} |
65 |
|
|
66 |
|
public void testGetAndSet(){ |
67 |
< |
AtomicReferenceArray ai = new AtomicReferenceArray(N); |
68 |
< |
for (int i = 0; i < N; ++i) { |
67 |
> |
AtomicReferenceArray ai = new AtomicReferenceArray(SIZE); |
68 |
> |
for (int i = 0; i < SIZE; ++i) { |
69 |
|
ai.set(i, one); |
70 |
|
assertEquals(one,ai.getAndSet(i,zero)); |
71 |
|
assertEquals(0,ai.getAndSet(i,m10)); |
73 |
|
} |
74 |
|
} |
75 |
|
|
76 |
+ |
public void testSerialization() { |
77 |
+ |
AtomicReferenceArray l = new AtomicReferenceArray(SIZE); |
78 |
+ |
for (int i = 0; i < SIZE; ++i) { |
79 |
+ |
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 |
+ |
for (int i = 0; i < SIZE; ++i) { |
93 |
+ |
assertEquals(r.get(i), l.get(i)); |
94 |
+ |
} |
95 |
+ |
} catch(Exception e){ |
96 |
+ |
e.printStackTrace(); |
97 |
+ |
fail("unexpected exception"); |
98 |
+ |
} |
99 |
+ |
} |
100 |
|
|
101 |
|
} |