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