9 |
|
import java.util.concurrent.atomic.*; |
10 |
|
import java.io.*; |
11 |
|
|
12 |
< |
public class AtomicReferenceArrayTest extends TestCase |
12 |
> |
public class AtomicReferenceArrayTest extends JSR166TestCase |
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 |
– |
|
14 |
|
public static void main (String[] args) { |
15 |
|
junit.textui.TestRunner.run (suite()); |
16 |
|
} |
18 |
|
return new TestSuite(AtomicReferenceArrayTest.class); |
19 |
|
} |
20 |
|
|
21 |
+ |
/** |
22 |
+ |
* |
23 |
+ |
*/ |
24 |
|
public void testConstructor(){ |
25 |
< |
AtomicReferenceArray<Integer> ai = new AtomicReferenceArray<Integer>(N); |
26 |
< |
for (int i = 0; i < N; ++i) { |
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(N); |
36 |
< |
for (int i = 0; i < N; ++i) { |
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); |
43 |
|
} |
44 |
|
} |
45 |
|
|
46 |
+ |
/** |
47 |
+ |
* |
48 |
+ |
*/ |
49 |
|
public void testCompareAndSet(){ |
50 |
< |
AtomicReferenceArray ai = new AtomicReferenceArray(N); |
51 |
< |
for (int i = 0; i < N; ++i) { |
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)); |
60 |
|
} |
61 |
|
} |
62 |
|
|
63 |
+ |
/** |
64 |
+ |
* |
65 |
+ |
*/ |
66 |
|
public void testWeakCompareAndSet(){ |
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 |
|
while(!ai.weakCompareAndSet(i, one,two)); |
71 |
|
while(!ai.weakCompareAndSet(i, two,m4)); |
75 |
|
} |
76 |
|
} |
77 |
|
|
78 |
+ |
/** |
79 |
+ |
* |
80 |
+ |
*/ |
81 |
|
public void testGetAndSet(){ |
82 |
< |
AtomicReferenceArray ai = new AtomicReferenceArray(N); |
83 |
< |
for (int i = 0; i < N; ++i) { |
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)); |
88 |
|
} |
89 |
|
} |
90 |
|
|
91 |
+ |
/** |
92 |
+ |
* |
93 |
+ |
*/ |
94 |
|
public void testSerialization() { |
95 |
< |
AtomicReferenceArray l = new AtomicReferenceArray(10); |
96 |
< |
for (int i = 0; i < 10; ++i) { |
95 |
> |
AtomicReferenceArray l = new AtomicReferenceArray(SIZE); |
96 |
> |
for (int i = 0; i < SIZE; ++i) { |
97 |
|
l.set(i, new Integer(-i)); |
98 |
|
} |
99 |
|
|
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 < 10; ++i) { |
110 |
> |
for (int i = 0; i < SIZE; ++i) { |
111 |
|
assertEquals(r.get(i), l.get(i)); |
112 |
|
} |
113 |
|
} catch(Exception e){ |
114 |
< |
e.printStackTrace(); |
108 |
< |
fail("unexpected exception"); |
114 |
> |
unexpectedException(); |
115 |
|
} |
116 |
|
} |
117 |
|
|