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