1 |
/* |
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/publicdomain/zero/1.0/ |
5 |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
* Pat Fisher, Mike Judd. |
7 |
*/ |
8 |
|
9 |
import junit.framework.*; |
10 |
import java.util.Arrays; |
11 |
import java.util.concurrent.atomic.AtomicIntegerArray; |
12 |
|
13 |
public class AtomicIntegerArrayTest extends JSR166TestCase { |
14 |
|
15 |
public static void main(String[] args) { |
16 |
junit.textui.TestRunner.run(suite()); |
17 |
} |
18 |
public static Test suite() { |
19 |
return new TestSuite(AtomicIntegerArrayTest.class); |
20 |
} |
21 |
|
22 |
/** |
23 |
* constructor creates array of given size with all elements zero |
24 |
*/ |
25 |
public void testConstructor() { |
26 |
AtomicIntegerArray aa = new AtomicIntegerArray(SIZE); |
27 |
for (int i = 0; i < SIZE; i++) |
28 |
assertEquals(0, aa.get(i)); |
29 |
} |
30 |
|
31 |
/** |
32 |
* constructor with null array throws NPE |
33 |
*/ |
34 |
public void testConstructor2NPE() { |
35 |
try { |
36 |
int[] a = null; |
37 |
AtomicIntegerArray aa = new AtomicIntegerArray(a); |
38 |
shouldThrow(); |
39 |
} catch (NullPointerException success) {} |
40 |
} |
41 |
|
42 |
/** |
43 |
* constructor with array is of same size and has all elements |
44 |
*/ |
45 |
public void testConstructor2() { |
46 |
int[] a = { 17, 3, -42, 99, -7 }; |
47 |
AtomicIntegerArray aa = new AtomicIntegerArray(a); |
48 |
assertEquals(a.length, aa.length()); |
49 |
for (int i = 0; i < a.length; i++) |
50 |
assertEquals(a[i], aa.get(i)); |
51 |
} |
52 |
|
53 |
/** |
54 |
* get and set for out of bound indices throw IndexOutOfBoundsException |
55 |
*/ |
56 |
public void testIndexing() { |
57 |
AtomicIntegerArray aa = new AtomicIntegerArray(SIZE); |
58 |
for (int index : new int[] { -1, SIZE }) { |
59 |
try { |
60 |
aa.get(index); |
61 |
shouldThrow(); |
62 |
} catch (IndexOutOfBoundsException success) {} |
63 |
try { |
64 |
aa.set(index, 1); |
65 |
shouldThrow(); |
66 |
} catch (IndexOutOfBoundsException success) {} |
67 |
try { |
68 |
aa.lazySet(index, 1); |
69 |
shouldThrow(); |
70 |
} catch (IndexOutOfBoundsException success) {} |
71 |
try { |
72 |
aa.compareAndSet(index, 1, 2); |
73 |
shouldThrow(); |
74 |
} catch (IndexOutOfBoundsException success) {} |
75 |
try { |
76 |
aa.weakCompareAndSet(index, 1, 2); |
77 |
shouldThrow(); |
78 |
} catch (IndexOutOfBoundsException success) {} |
79 |
try { |
80 |
aa.getAndAdd(index, 1); |
81 |
shouldThrow(); |
82 |
} catch (IndexOutOfBoundsException success) {} |
83 |
try { |
84 |
aa.addAndGet(index, 1); |
85 |
shouldThrow(); |
86 |
} catch (IndexOutOfBoundsException success) {} |
87 |
} |
88 |
} |
89 |
|
90 |
/** |
91 |
* get returns the last value set at index |
92 |
*/ |
93 |
public void testGetSet() { |
94 |
AtomicIntegerArray aa = new AtomicIntegerArray(SIZE); |
95 |
for (int i = 0; i < SIZE; i++) { |
96 |
aa.set(i, 1); |
97 |
assertEquals(1, aa.get(i)); |
98 |
aa.set(i, 2); |
99 |
assertEquals(2, aa.get(i)); |
100 |
aa.set(i, -3); |
101 |
assertEquals(-3, aa.get(i)); |
102 |
} |
103 |
} |
104 |
|
105 |
/** |
106 |
* get returns the last value lazySet at index by same thread |
107 |
*/ |
108 |
public void testGetLazySet() { |
109 |
AtomicIntegerArray aa = new AtomicIntegerArray(SIZE); |
110 |
for (int i = 0; i < SIZE; i++) { |
111 |
aa.lazySet(i, 1); |
112 |
assertEquals(1, aa.get(i)); |
113 |
aa.lazySet(i, 2); |
114 |
assertEquals(2, aa.get(i)); |
115 |
aa.lazySet(i, -3); |
116 |
assertEquals(-3, aa.get(i)); |
117 |
} |
118 |
} |
119 |
|
120 |
/** |
121 |
* compareAndSet succeeds in changing value if equal to expected else fails |
122 |
*/ |
123 |
public void testCompareAndSet() { |
124 |
AtomicIntegerArray aa = new AtomicIntegerArray(SIZE); |
125 |
for (int i = 0; i < SIZE; i++) { |
126 |
aa.set(i, 1); |
127 |
assertTrue(aa.compareAndSet(i, 1, 2)); |
128 |
assertTrue(aa.compareAndSet(i, 2, -4)); |
129 |
assertEquals(-4, aa.get(i)); |
130 |
assertFalse(aa.compareAndSet(i, -5, 7)); |
131 |
assertEquals(-4, aa.get(i)); |
132 |
assertTrue(aa.compareAndSet(i, -4, 7)); |
133 |
assertEquals(7, aa.get(i)); |
134 |
} |
135 |
} |
136 |
|
137 |
/** |
138 |
* compareAndSet in one thread enables another waiting for value |
139 |
* to succeed |
140 |
*/ |
141 |
public void testCompareAndSetInMultipleThreads() throws Exception { |
142 |
final AtomicIntegerArray a = new AtomicIntegerArray(1); |
143 |
a.set(0, 1); |
144 |
Thread t = new Thread(new CheckedRunnable() { |
145 |
public void realRun() { |
146 |
while (!a.compareAndSet(0, 2, 3)) |
147 |
Thread.yield(); |
148 |
}}); |
149 |
|
150 |
t.start(); |
151 |
assertTrue(a.compareAndSet(0, 1, 2)); |
152 |
t.join(LONG_DELAY_MS); |
153 |
assertFalse(t.isAlive()); |
154 |
assertEquals(3, a.get(0)); |
155 |
} |
156 |
|
157 |
/** |
158 |
* repeated weakCompareAndSet succeeds in changing value when equal |
159 |
* to expected |
160 |
*/ |
161 |
public void testWeakCompareAndSet() { |
162 |
AtomicIntegerArray aa = new AtomicIntegerArray(SIZE); |
163 |
for (int i = 0; i < SIZE; i++) { |
164 |
aa.set(i, 1); |
165 |
while (!aa.weakCompareAndSet(i, 1, 2)); |
166 |
while (!aa.weakCompareAndSet(i, 2, -4)); |
167 |
assertEquals(-4, aa.get(i)); |
168 |
while (!aa.weakCompareAndSet(i, -4, 7)); |
169 |
assertEquals(7, aa.get(i)); |
170 |
} |
171 |
} |
172 |
|
173 |
/** |
174 |
* getAndSet returns previous value and sets to given value at given index |
175 |
*/ |
176 |
public void testGetAndSet() { |
177 |
AtomicIntegerArray aa = new AtomicIntegerArray(SIZE); |
178 |
for (int i = 0; i < SIZE; i++) { |
179 |
aa.set(i, 1); |
180 |
assertEquals(1, aa.getAndSet(i, 0)); |
181 |
assertEquals(0, aa.getAndSet(i, -10)); |
182 |
assertEquals(-10, aa.getAndSet(i, 1)); |
183 |
} |
184 |
} |
185 |
|
186 |
/** |
187 |
* getAndAdd returns previous value and adds given value |
188 |
*/ |
189 |
public void testGetAndAdd() { |
190 |
AtomicIntegerArray aa = new AtomicIntegerArray(SIZE); |
191 |
for (int i = 0; i < SIZE; i++) { |
192 |
aa.set(i, 1); |
193 |
assertEquals(1, aa.getAndAdd(i, 2)); |
194 |
assertEquals(3, aa.get(i)); |
195 |
assertEquals(3, aa.getAndAdd(i, -4)); |
196 |
assertEquals(-1, aa.get(i)); |
197 |
} |
198 |
} |
199 |
|
200 |
/** |
201 |
* getAndDecrement returns previous value and decrements |
202 |
*/ |
203 |
public void testGetAndDecrement() { |
204 |
AtomicIntegerArray aa = new AtomicIntegerArray(SIZE); |
205 |
for (int i = 0; i < SIZE; i++) { |
206 |
aa.set(i, 1); |
207 |
assertEquals(1, aa.getAndDecrement(i)); |
208 |
assertEquals(0, aa.getAndDecrement(i)); |
209 |
assertEquals(-1, aa.getAndDecrement(i)); |
210 |
} |
211 |
} |
212 |
|
213 |
/** |
214 |
* getAndIncrement returns previous value and increments |
215 |
*/ |
216 |
public void testGetAndIncrement() { |
217 |
AtomicIntegerArray aa = new AtomicIntegerArray(SIZE); |
218 |
for (int i = 0; i < SIZE; i++) { |
219 |
aa.set(i, 1); |
220 |
assertEquals(1, aa.getAndIncrement(i)); |
221 |
assertEquals(2, aa.get(i)); |
222 |
aa.set(i, -2); |
223 |
assertEquals(-2, aa.getAndIncrement(i)); |
224 |
assertEquals(-1, aa.getAndIncrement(i)); |
225 |
assertEquals(0, aa.getAndIncrement(i)); |
226 |
assertEquals(1, aa.get(i)); |
227 |
} |
228 |
} |
229 |
|
230 |
/** |
231 |
* addAndGet adds given value to current, and returns current value |
232 |
*/ |
233 |
public void testAddAndGet() { |
234 |
AtomicIntegerArray aa = new AtomicIntegerArray(SIZE); |
235 |
for (int i = 0; i < SIZE; i++) { |
236 |
aa.set(i, 1); |
237 |
assertEquals(3, aa.addAndGet(i, 2)); |
238 |
assertEquals(3, aa.get(i)); |
239 |
assertEquals(-1, aa.addAndGet(i, -4)); |
240 |
assertEquals(-1, aa.get(i)); |
241 |
} |
242 |
} |
243 |
|
244 |
/** |
245 |
* decrementAndGet decrements and returns current value |
246 |
*/ |
247 |
public void testDecrementAndGet() { |
248 |
AtomicIntegerArray aa = new AtomicIntegerArray(SIZE); |
249 |
for (int i = 0; i < SIZE; i++) { |
250 |
aa.set(i, 1); |
251 |
assertEquals(0, aa.decrementAndGet(i)); |
252 |
assertEquals(-1, aa.decrementAndGet(i)); |
253 |
assertEquals(-2, aa.decrementAndGet(i)); |
254 |
assertEquals(-2, aa.get(i)); |
255 |
} |
256 |
} |
257 |
|
258 |
/** |
259 |
* incrementAndGet increments and returns current value |
260 |
*/ |
261 |
public void testIncrementAndGet() { |
262 |
AtomicIntegerArray aa = new AtomicIntegerArray(SIZE); |
263 |
for (int i = 0; i < SIZE; i++) { |
264 |
aa.set(i, 1); |
265 |
assertEquals(2, aa.incrementAndGet(i)); |
266 |
assertEquals(2, aa.get(i)); |
267 |
aa.set(i, -2); |
268 |
assertEquals(-1, aa.incrementAndGet(i)); |
269 |
assertEquals(0, aa.incrementAndGet(i)); |
270 |
assertEquals(1, aa.incrementAndGet(i)); |
271 |
assertEquals(1, aa.get(i)); |
272 |
} |
273 |
} |
274 |
|
275 |
static final int COUNTDOWN = 100000; |
276 |
|
277 |
class Counter extends CheckedRunnable { |
278 |
final AtomicIntegerArray aa; |
279 |
volatile int counts; |
280 |
Counter(AtomicIntegerArray a) { aa = a; } |
281 |
public void realRun() { |
282 |
for (;;) { |
283 |
boolean done = true; |
284 |
for (int i = 0; i < aa.length(); i++) { |
285 |
int v = aa.get(i); |
286 |
assertTrue(v >= 0); |
287 |
if (v != 0) { |
288 |
done = false; |
289 |
if (aa.compareAndSet(i, v, v-1)) |
290 |
++counts; |
291 |
} |
292 |
} |
293 |
if (done) |
294 |
break; |
295 |
} |
296 |
} |
297 |
} |
298 |
|
299 |
/** |
300 |
* Multiple threads using same array of counters successfully |
301 |
* update a number of times equal to total count |
302 |
*/ |
303 |
public void testCountingInMultipleThreads() throws InterruptedException { |
304 |
final AtomicIntegerArray aa = new AtomicIntegerArray(SIZE); |
305 |
for (int i = 0; i < SIZE; i++) |
306 |
aa.set(i, COUNTDOWN); |
307 |
Counter c1 = new Counter(aa); |
308 |
Counter c2 = new Counter(aa); |
309 |
Thread t1 = new Thread(c1); |
310 |
Thread t2 = new Thread(c2); |
311 |
t1.start(); |
312 |
t2.start(); |
313 |
t1.join(); |
314 |
t2.join(); |
315 |
assertEquals(c1.counts+c2.counts, SIZE * COUNTDOWN); |
316 |
} |
317 |
|
318 |
/** |
319 |
* a deserialized serialized array holds same values |
320 |
*/ |
321 |
public void testSerialization() throws Exception { |
322 |
AtomicIntegerArray x = new AtomicIntegerArray(SIZE); |
323 |
for (int i = 0; i < SIZE; i++) |
324 |
x.set(i, -i); |
325 |
AtomicIntegerArray y = serialClone(x); |
326 |
assertNotSame(x, y); |
327 |
assertEquals(x.length(), y.length()); |
328 |
for (int i = 0; i < SIZE; i++) { |
329 |
assertEquals(x.get(i), y.get(i)); |
330 |
} |
331 |
} |
332 |
|
333 |
/** |
334 |
* toString returns current value. |
335 |
*/ |
336 |
public void testToString() { |
337 |
int[] a = { 17, 3, -42, 99, -7 }; |
338 |
AtomicIntegerArray aa = new AtomicIntegerArray(a); |
339 |
assertEquals(Arrays.toString(a), aa.toString()); |
340 |
} |
341 |
|
342 |
} |