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 |
+ |
* constructor creates array of given size with all elements null |
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 |
+ |
* get and set for out of bound indices throw IndexOutOfBoundsException |
33 |
+ |
*/ |
34 |
+ |
public void testIndexing(){ |
35 |
+ |
AtomicReferenceArray<Integer> ai = new AtomicReferenceArray<Integer>(SIZE); |
36 |
+ |
try { |
37 |
+ |
ai.get(SIZE); |
38 |
+ |
} catch(IndexOutOfBoundsException success){ |
39 |
+ |
} |
40 |
+ |
try { |
41 |
+ |
ai.get(-1); |
42 |
+ |
} catch(IndexOutOfBoundsException success){ |
43 |
+ |
} |
44 |
+ |
try { |
45 |
+ |
ai.set(SIZE, null); |
46 |
+ |
} catch(IndexOutOfBoundsException success){ |
47 |
+ |
} |
48 |
+ |
try { |
49 |
+ |
ai.set(-1, null); |
50 |
+ |
} catch(IndexOutOfBoundsException success){ |
51 |
+ |
} |
52 |
+ |
} |
53 |
+ |
|
54 |
+ |
/** |
55 |
+ |
* get returns the last value set at index |
56 |
+ |
*/ |
57 |
|
public void testGetSet(){ |
58 |
< |
AtomicReferenceArray ai = new AtomicReferenceArray(N); |
59 |
< |
for (int i = 0; i < N; ++i) { |
58 |
> |
AtomicReferenceArray ai = new AtomicReferenceArray(SIZE); |
59 |
> |
for (int i = 0; i < SIZE; ++i) { |
60 |
|
ai.set(i, one); |
61 |
|
assertEquals(one,ai.get(i)); |
62 |
|
ai.set(i, two); |
66 |
|
} |
67 |
|
} |
68 |
|
|
69 |
+ |
/** |
70 |
+ |
* compareAndSet succeeds in changing value if equal to expected else fails |
71 |
+ |
*/ |
72 |
|
public void testCompareAndSet(){ |
73 |
< |
AtomicReferenceArray ai = new AtomicReferenceArray(N); |
74 |
< |
for (int i = 0; i < N; ++i) { |
73 |
> |
AtomicReferenceArray ai = new AtomicReferenceArray(SIZE); |
74 |
> |
for (int i = 0; i < SIZE; ++i) { |
75 |
|
ai.set(i, one); |
76 |
|
assertTrue(ai.compareAndSet(i, one,two)); |
77 |
|
assertTrue(ai.compareAndSet(i, two,m4)); |
83 |
|
} |
84 |
|
} |
85 |
|
|
86 |
+ |
/** |
87 |
+ |
* compareAndSet in one thread enables another waiting for value |
88 |
+ |
* to succeed |
89 |
+ |
*/ |
90 |
+ |
public void testCompareAndSetInMultipleThreads() { |
91 |
+ |
final AtomicReferenceArray a = new AtomicReferenceArray(1); |
92 |
+ |
a.set(0, one); |
93 |
+ |
Thread t = new Thread(new Runnable() { |
94 |
+ |
public void run() { |
95 |
+ |
while(!a.compareAndSet(0, two, three)) Thread.yield(); |
96 |
+ |
}}); |
97 |
+ |
try { |
98 |
+ |
t.start(); |
99 |
+ |
assertTrue(a.compareAndSet(0, one, two)); |
100 |
+ |
t.join(LONG_DELAY_MS); |
101 |
+ |
assertFalse(t.isAlive()); |
102 |
+ |
assertEquals(a.get(0), three); |
103 |
+ |
} |
104 |
+ |
catch(Exception e) { |
105 |
+ |
unexpectedException(); |
106 |
+ |
} |
107 |
+ |
} |
108 |
+ |
|
109 |
+ |
/** |
110 |
+ |
* repeated weakCompareAndSet succeeds in changing value when equal |
111 |
+ |
* to expected |
112 |
+ |
*/ |
113 |
|
public void testWeakCompareAndSet(){ |
114 |
< |
AtomicReferenceArray ai = new AtomicReferenceArray(N); |
115 |
< |
for (int i = 0; i < N; ++i) { |
114 |
> |
AtomicReferenceArray ai = new AtomicReferenceArray(SIZE); |
115 |
> |
for (int i = 0; i < SIZE; ++i) { |
116 |
|
ai.set(i, one); |
117 |
|
while(!ai.weakCompareAndSet(i, one,two)); |
118 |
|
while(!ai.weakCompareAndSet(i, two,m4)); |
122 |
|
} |
123 |
|
} |
124 |
|
|
125 |
+ |
/** |
126 |
+ |
* getAndSet returns previous value and sets to given value at given index |
127 |
+ |
*/ |
128 |
|
public void testGetAndSet(){ |
129 |
< |
AtomicReferenceArray ai = new AtomicReferenceArray(N); |
130 |
< |
for (int i = 0; i < N; ++i) { |
129 |
> |
AtomicReferenceArray ai = new AtomicReferenceArray(SIZE); |
130 |
> |
for (int i = 0; i < SIZE; ++i) { |
131 |
|
ai.set(i, one); |
132 |
|
assertEquals(one,ai.getAndSet(i,zero)); |
133 |
|
assertEquals(0,ai.getAndSet(i,m10)); |
135 |
|
} |
136 |
|
} |
137 |
|
|
138 |
+ |
/** |
139 |
+ |
* a deserialized serialized array holds same values |
140 |
+ |
*/ |
141 |
|
public void testSerialization() { |
142 |
< |
AtomicReferenceArray l = new AtomicReferenceArray(10); |
143 |
< |
for (int i = 0; i < 10; ++i) { |
142 |
> |
AtomicReferenceArray l = new AtomicReferenceArray(SIZE); |
143 |
> |
for (int i = 0; i < SIZE; ++i) { |
144 |
|
l.set(i, new Integer(-i)); |
145 |
|
} |
146 |
|
|
154 |
|
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
155 |
|
AtomicReferenceArray r = (AtomicReferenceArray) in.readObject(); |
156 |
|
assertEquals(l.length(), r.length()); |
157 |
< |
for (int i = 0; i < 10; ++i) { |
157 |
> |
for (int i = 0; i < SIZE; ++i) { |
158 |
|
assertEquals(r.get(i), l.get(i)); |
159 |
|
} |
160 |
|
} catch(Exception e){ |
161 |
< |
e.printStackTrace(); |
108 |
< |
fail("unexpected exception"); |
161 |
> |
unexpectedException(); |
162 |
|
} |
163 |
|
} |
164 |
|
|