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