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