7 |
|
|
8 |
|
import junit.framework.*; |
9 |
|
import java.util.concurrent.atomic.*; |
10 |
+ |
import java.io.*; |
11 |
|
|
12 |
< |
public class AtomicLongArrayTest extends TestCase |
12 |
< |
{ |
13 |
< |
static final int N = 10; |
14 |
< |
|
12 |
> |
public class AtomicLongArrayTest extends JSR166TestCase { |
13 |
|
public static void main (String[] args) { |
14 |
|
junit.textui.TestRunner.run (suite()); |
15 |
|
} |
17 |
|
return new TestSuite(AtomicLongArrayTest.class); |
18 |
|
} |
19 |
|
|
20 |
+ |
/** |
21 |
+ |
* constructor creates array of given size with all elements zero |
22 |
+ |
*/ |
23 |
|
public void testConstructor(){ |
24 |
< |
AtomicLongArray ai = new AtomicLongArray(N); |
25 |
< |
for (int i = 0; i < N; ++i) |
24 |
> |
AtomicLongArray ai = new AtomicLongArray(SIZE); |
25 |
> |
for (int i = 0; i < SIZE; ++i) |
26 |
|
assertEquals(0,ai.get(i)); |
27 |
|
} |
28 |
|
|
29 |
+ |
/** |
30 |
+ |
* get and set for out of bound indices throw IndexOutOfBoundsException |
31 |
+ |
*/ |
32 |
+ |
public void testIndexing(){ |
33 |
+ |
AtomicLongArray ai = new AtomicLongArray(SIZE); |
34 |
+ |
try { |
35 |
+ |
ai.get(SIZE); |
36 |
+ |
} catch(IndexOutOfBoundsException success){ |
37 |
+ |
} |
38 |
+ |
try { |
39 |
+ |
ai.get(-1); |
40 |
+ |
} catch(IndexOutOfBoundsException success){ |
41 |
+ |
} |
42 |
+ |
try { |
43 |
+ |
ai.set(SIZE, 0); |
44 |
+ |
} catch(IndexOutOfBoundsException success){ |
45 |
+ |
} |
46 |
+ |
try { |
47 |
+ |
ai.set(-1, 0); |
48 |
+ |
} catch(IndexOutOfBoundsException success){ |
49 |
+ |
} |
50 |
+ |
} |
51 |
+ |
|
52 |
+ |
/** |
53 |
+ |
* get returns the last value set at index |
54 |
+ |
*/ |
55 |
|
public void testGetSet(){ |
56 |
< |
AtomicLongArray ai = new AtomicLongArray(N); |
57 |
< |
for (int i = 0; i < N; ++i) { |
56 |
> |
AtomicLongArray ai = new AtomicLongArray(SIZE); |
57 |
> |
for (int i = 0; i < SIZE; ++i) { |
58 |
|
ai.set(i, 1); |
59 |
|
assertEquals(1,ai.get(i)); |
60 |
|
ai.set(i, 2); |
64 |
|
} |
65 |
|
} |
66 |
|
|
67 |
+ |
/** |
68 |
+ |
* compareAndSet succeeds in changing value if equal to expected else fails |
69 |
+ |
*/ |
70 |
|
public void testCompareAndSet(){ |
71 |
< |
AtomicLongArray ai = new AtomicLongArray(N); |
72 |
< |
for (int i = 0; i < N; ++i) { |
71 |
> |
AtomicLongArray ai = new AtomicLongArray(SIZE); |
72 |
> |
for (int i = 0; i < SIZE; ++i) { |
73 |
|
ai.set(i, 1); |
74 |
|
assertTrue(ai.compareAndSet(i, 1,2)); |
75 |
|
assertTrue(ai.compareAndSet(i, 2,-4)); |
81 |
|
} |
82 |
|
} |
83 |
|
|
84 |
+ |
/** |
85 |
+ |
* compareAndSet in one thread enables another waiting for value |
86 |
+ |
* to succeed |
87 |
+ |
*/ |
88 |
+ |
public void testCompareAndSetInMultipleThreads() { |
89 |
+ |
final AtomicLongArray a = new AtomicLongArray(1); |
90 |
+ |
a.set(0, 1); |
91 |
+ |
Thread t = new Thread(new Runnable() { |
92 |
+ |
public void run() { |
93 |
+ |
while(!a.compareAndSet(0, 2, 3)) Thread.yield(); |
94 |
+ |
}}); |
95 |
+ |
try { |
96 |
+ |
t.start(); |
97 |
+ |
assertTrue(a.compareAndSet(0, 1, 2)); |
98 |
+ |
t.join(LONG_DELAY_MS); |
99 |
+ |
assertFalse(t.isAlive()); |
100 |
+ |
assertEquals(a.get(0), 3); |
101 |
+ |
} |
102 |
+ |
catch(Exception e) { |
103 |
+ |
unexpectedException(); |
104 |
+ |
} |
105 |
+ |
} |
106 |
+ |
|
107 |
+ |
/** |
108 |
+ |
* repeated weakCompareAndSet succeeds in changing value when equal |
109 |
+ |
* to expected |
110 |
+ |
*/ |
111 |
|
public void testWeakCompareAndSet(){ |
112 |
< |
AtomicLongArray ai = new AtomicLongArray(N); |
113 |
< |
for (int i = 0; i < N; ++i) { |
112 |
> |
AtomicLongArray ai = new AtomicLongArray(SIZE); |
113 |
> |
for (int i = 0; i < SIZE; ++i) { |
114 |
|
ai.set(i, 1); |
115 |
|
while(!ai.weakCompareAndSet(i, 1,2)); |
116 |
|
while(!ai.weakCompareAndSet(i, 2,-4)); |
120 |
|
} |
121 |
|
} |
122 |
|
|
123 |
+ |
/** |
124 |
+ |
* getAndSet returns previous value and sets to given value at given index |
125 |
+ |
*/ |
126 |
|
public void testGetAndSet(){ |
127 |
< |
AtomicLongArray ai = new AtomicLongArray(N); |
128 |
< |
for (int i = 0; i < N; ++i) { |
127 |
> |
AtomicLongArray ai = new AtomicLongArray(SIZE); |
128 |
> |
for (int i = 0; i < SIZE; ++i) { |
129 |
|
ai.set(i, 1); |
130 |
|
assertEquals(1,ai.getAndSet(i,0)); |
131 |
|
assertEquals(0,ai.getAndSet(i,-10)); |
133 |
|
} |
134 |
|
} |
135 |
|
|
136 |
+ |
/** |
137 |
+ |
* getAndAdd returns previous value and adds given value |
138 |
+ |
*/ |
139 |
|
public void testGetAndAdd(){ |
140 |
< |
AtomicLongArray ai = new AtomicLongArray(N); |
141 |
< |
for (int i = 0; i < N; ++i) { |
140 |
> |
AtomicLongArray ai = new AtomicLongArray(SIZE); |
141 |
> |
for (int i = 0; i < SIZE; ++i) { |
142 |
|
ai.set(i, 1); |
143 |
|
assertEquals(1,ai.getAndAdd(i,2)); |
144 |
|
assertEquals(3,ai.get(i)); |
147 |
|
} |
148 |
|
} |
149 |
|
|
150 |
+ |
/** |
151 |
+ |
* getAndDecrement returns previous value and decrements |
152 |
+ |
*/ |
153 |
|
public void testGetAndDecrement(){ |
154 |
< |
AtomicLongArray ai = new AtomicLongArray(N); |
155 |
< |
for (int i = 0; i < N; ++i) { |
154 |
> |
AtomicLongArray ai = new AtomicLongArray(SIZE); |
155 |
> |
for (int i = 0; i < SIZE; ++i) { |
156 |
|
ai.set(i, 1); |
157 |
|
assertEquals(1,ai.getAndDecrement(i)); |
158 |
|
assertEquals(0,ai.getAndDecrement(i)); |
160 |
|
} |
161 |
|
} |
162 |
|
|
163 |
+ |
/** |
164 |
+ |
* getAndIncrement returns previous value and increments |
165 |
+ |
*/ |
166 |
|
public void testGetAndIncrement(){ |
167 |
< |
AtomicLongArray ai = new AtomicLongArray(N); |
168 |
< |
for (int i = 0; i < N; ++i) { |
167 |
> |
AtomicLongArray ai = new AtomicLongArray(SIZE); |
168 |
> |
for (int i = 0; i < SIZE; ++i) { |
169 |
|
ai.set(i, 1); |
170 |
|
assertEquals(1,ai.getAndIncrement(i)); |
171 |
|
assertEquals(2,ai.get(i)); |
177 |
|
} |
178 |
|
} |
179 |
|
|
180 |
+ |
/** |
181 |
+ |
* addAndGet adds given value to current, and returns current value |
182 |
+ |
*/ |
183 |
|
public void testAddAndGet() { |
184 |
< |
AtomicLongArray ai = new AtomicLongArray(N); |
185 |
< |
for (int i = 0; i < N; ++i) { |
184 |
> |
AtomicLongArray ai = new AtomicLongArray(SIZE); |
185 |
> |
for (int i = 0; i < SIZE; ++i) { |
186 |
|
ai.set(i, 1); |
187 |
|
assertEquals(3,ai.addAndGet(i,2)); |
188 |
|
assertEquals(3,ai.get(i)); |
191 |
|
} |
192 |
|
} |
193 |
|
|
194 |
+ |
/** |
195 |
+ |
* decrementAndGet decrements and returns current value |
196 |
+ |
*/ |
197 |
|
public void testDecrementAndGet(){ |
198 |
< |
AtomicLongArray ai = new AtomicLongArray(N); |
199 |
< |
for (int i = 0; i < N; ++i) { |
198 |
> |
AtomicLongArray ai = new AtomicLongArray(SIZE); |
199 |
> |
for (int i = 0; i < SIZE; ++i) { |
200 |
|
ai.set(i, 1); |
201 |
|
assertEquals(0,ai.decrementAndGet(i)); |
202 |
|
assertEquals(-1,ai.decrementAndGet(i)); |
205 |
|
} |
206 |
|
} |
207 |
|
|
208 |
+ |
/** |
209 |
+ |
* incrementAndGet increments and returns current value |
210 |
+ |
*/ |
211 |
|
public void testIncrementAndGet() { |
212 |
< |
AtomicLongArray ai = new AtomicLongArray(N); |
213 |
< |
for (int i = 0; i < N; ++i) { |
212 |
> |
AtomicLongArray ai = new AtomicLongArray(SIZE); |
213 |
> |
for (int i = 0; i < SIZE; ++i) { |
214 |
|
ai.set(i, 1); |
215 |
|
assertEquals(2,ai.incrementAndGet(i)); |
216 |
|
assertEquals(2,ai.get(i)); |
222 |
|
} |
223 |
|
} |
224 |
|
|
225 |
+ |
static final long COUNTDOWN = 100000; |
226 |
+ |
|
227 |
+ |
class Counter implements Runnable { |
228 |
+ |
final AtomicLongArray ai; |
229 |
+ |
volatile long counts; |
230 |
+ |
Counter(AtomicLongArray a) { ai = a; } |
231 |
+ |
public void run() { |
232 |
+ |
for (;;) { |
233 |
+ |
boolean done = true; |
234 |
+ |
for (int i = 0; i < ai.length(); ++i) { |
235 |
+ |
long v = ai.get(i); |
236 |
+ |
threadAssertTrue(v >= 0); |
237 |
+ |
if (v != 0) { |
238 |
+ |
done = false; |
239 |
+ |
if (ai.compareAndSet(i, v, v-1)) |
240 |
+ |
++counts; |
241 |
+ |
} |
242 |
+ |
} |
243 |
+ |
if (done) |
244 |
+ |
break; |
245 |
+ |
} |
246 |
+ |
} |
247 |
+ |
} |
248 |
+ |
|
249 |
+ |
/** |
250 |
+ |
* Multiple threads using same array of counters successfully |
251 |
+ |
* update a number of times equal to total count |
252 |
+ |
*/ |
253 |
+ |
public void testCountingInMultipleThreads() { |
254 |
+ |
try { |
255 |
+ |
final AtomicLongArray ai = new AtomicLongArray(SIZE); |
256 |
+ |
for (int i = 0; i < SIZE; ++i) |
257 |
+ |
ai.set(i, COUNTDOWN); |
258 |
+ |
Counter c1 = new Counter(ai); |
259 |
+ |
Counter c2 = new Counter(ai); |
260 |
+ |
Thread t1 = new Thread(c1); |
261 |
+ |
Thread t2 = new Thread(c2); |
262 |
+ |
t1.start(); |
263 |
+ |
t2.start(); |
264 |
+ |
t1.join(); |
265 |
+ |
t2.join(); |
266 |
+ |
assertEquals(c1.counts+c2.counts, SIZE * COUNTDOWN); |
267 |
+ |
} |
268 |
+ |
catch(InterruptedException ie) { |
269 |
+ |
unexpectedException(); |
270 |
+ |
} |
271 |
+ |
} |
272 |
+ |
|
273 |
+ |
/** |
274 |
+ |
* a deserialized serialized array holds same values |
275 |
+ |
*/ |
276 |
+ |
public void testSerialization() { |
277 |
+ |
AtomicLongArray l = new AtomicLongArray(SIZE); |
278 |
+ |
for (int i = 0; i < SIZE; ++i) |
279 |
+ |
l.set(i, -i); |
280 |
+ |
|
281 |
+ |
try { |
282 |
+ |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
283 |
+ |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
284 |
+ |
out.writeObject(l); |
285 |
+ |
out.close(); |
286 |
+ |
|
287 |
+ |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
288 |
+ |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
289 |
+ |
AtomicLongArray r = (AtomicLongArray) in.readObject(); |
290 |
+ |
for (int i = 0; i < SIZE; ++i) { |
291 |
+ |
assertEquals(l.get(i), r.get(i)); |
292 |
+ |
} |
293 |
+ |
} catch(Exception e){ |
294 |
+ |
unexpectedException(); |
295 |
+ |
} |
296 |
+ |
} |
297 |
+ |
|
298 |
+ |
|
299 |
|
} |