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. |
5 |
> |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
> |
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
|
import junit.framework.*; |
26 |
|
*/ |
27 |
|
public void testConstructor() { |
28 |
|
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
29 |
< |
for (int i = 0; i < SIZE; ++i) |
29 |
> |
for (int i = 0; i < SIZE; ++i) |
30 |
|
assertEquals(0,ai.get(i)); |
31 |
|
} |
32 |
|
|
50 |
|
int[] a = { 17, 3, -42, 99, -7}; |
51 |
|
AtomicIntegerArray ai = new AtomicIntegerArray(a); |
52 |
|
assertEquals(a.length, ai.length()); |
53 |
< |
for (int i = 0; i < a.length; ++i) |
53 |
> |
for (int i = 0; i < a.length; ++i) |
54 |
|
assertEquals(a[i], ai.get(i)); |
55 |
|
} |
56 |
|
|
57 |
|
/** |
58 |
|
* get and set for out of bound indices throw IndexOutOfBoundsException |
59 |
|
*/ |
60 |
< |
public void testIndexing(){ |
60 |
> |
public void testIndexing() { |
61 |
|
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
62 |
|
try { |
63 |
|
ai.get(SIZE); |
64 |
< |
} catch(IndexOutOfBoundsException success){ |
64 |
> |
} catch (IndexOutOfBoundsException success) { |
65 |
|
} |
66 |
|
try { |
67 |
|
ai.get(-1); |
68 |
< |
} catch(IndexOutOfBoundsException success){ |
68 |
> |
} catch (IndexOutOfBoundsException success) { |
69 |
|
} |
70 |
|
try { |
71 |
|
ai.set(SIZE, 0); |
72 |
< |
} catch(IndexOutOfBoundsException success){ |
72 |
> |
} catch (IndexOutOfBoundsException success) { |
73 |
|
} |
74 |
|
try { |
75 |
|
ai.set(-1, 0); |
76 |
< |
} catch(IndexOutOfBoundsException success){ |
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)); |
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 |
+ |
|
110 |
+ |
/** |
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)); |
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(); |
136 |
> |
while (!a.compareAndSet(0, 2, 3)) Thread.yield(); |
137 |
|
}}); |
138 |
|
try { |
139 |
|
t.start(); |
142 |
|
assertFalse(t.isAlive()); |
143 |
|
assertEquals(a.get(0), 3); |
144 |
|
} |
145 |
< |
catch(Exception e) { |
145 |
> |
catch (Exception e) { |
146 |
|
unexpectedException(); |
147 |
|
} |
148 |
|
} |
149 |
|
|
150 |
|
/** |
151 |
|
* repeated weakCompareAndSet succeeds in changing value when equal |
152 |
< |
* to expected |
152 |
> |
* to expected |
153 |
|
*/ |
154 |
|
public void testWeakCompareAndSet() { |
155 |
< |
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
155 |
> |
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
156 |
|
for (int i = 0; i < SIZE; ++i) { |
157 |
|
ai.set(i, 1); |
158 |
< |
while(!ai.weakCompareAndSet(i, 1,2)); |
159 |
< |
while(!ai.weakCompareAndSet(i, 2,-4)); |
158 |
> |
while (!ai.weakCompareAndSet(i, 1,2)); |
159 |
> |
while (!ai.weakCompareAndSet(i, 2,-4)); |
160 |
|
assertEquals(-4,ai.get(i)); |
161 |
< |
while(!ai.weakCompareAndSet(i, -4,7)); |
161 |
> |
while (!ai.weakCompareAndSet(i, -4,7)); |
162 |
|
assertEquals(7,ai.get(i)); |
163 |
|
} |
164 |
|
} |
167 |
|
* getAndSet returns previous value and sets to given value at given index |
168 |
|
*/ |
169 |
|
public void testGetAndSet() { |
170 |
< |
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
170 |
> |
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
171 |
|
for (int i = 0; i < SIZE; ++i) { |
172 |
|
ai.set(i, 1); |
173 |
|
assertEquals(1,ai.getAndSet(i,0)); |
180 |
|
* getAndAdd returns previous value and adds given value |
181 |
|
*/ |
182 |
|
public void testGetAndAdd() { |
183 |
< |
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
183 |
> |
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
184 |
|
for (int i = 0; i < SIZE; ++i) { |
185 |
|
ai.set(i, 1); |
186 |
|
assertEquals(1,ai.getAndAdd(i,2)); |
194 |
|
* getAndDecrement returns previous value and decrements |
195 |
|
*/ |
196 |
|
public void testGetAndDecrement() { |
197 |
< |
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
197 |
> |
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
198 |
|
for (int i = 0; i < SIZE; ++i) { |
199 |
|
ai.set(i, 1); |
200 |
|
assertEquals(1,ai.getAndDecrement(i)); |
207 |
|
* getAndIncrement returns previous value and increments |
208 |
|
*/ |
209 |
|
public void testGetAndIncrement() { |
210 |
< |
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
210 |
> |
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
211 |
|
for (int i = 0; i < SIZE; ++i) { |
212 |
|
ai.set(i, 1); |
213 |
|
assertEquals(1,ai.getAndIncrement(i)); |
224 |
|
* addAndGet adds given value to current, and returns current value |
225 |
|
*/ |
226 |
|
public void testAddAndGet() { |
227 |
< |
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
227 |
> |
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
228 |
|
for (int i = 0; i < SIZE; ++i) { |
229 |
|
ai.set(i, 1); |
230 |
|
assertEquals(3,ai.addAndGet(i,2)); |
238 |
|
* decrementAndGet decrements and returns current value |
239 |
|
*/ |
240 |
|
public void testDecrementAndGet() { |
241 |
< |
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
241 |
> |
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
242 |
|
for (int i = 0; i < SIZE; ++i) { |
243 |
|
ai.set(i, 1); |
244 |
|
assertEquals(0,ai.decrementAndGet(i)); |
252 |
|
* incrementAndGet increments and returns current value |
253 |
|
*/ |
254 |
|
public void testIncrementAndGet() { |
255 |
< |
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
255 |
> |
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
256 |
|
for (int i = 0; i < SIZE; ++i) { |
257 |
|
ai.set(i, 1); |
258 |
|
assertEquals(2,ai.incrementAndGet(i)); |
266 |
|
} |
267 |
|
|
268 |
|
static final int COUNTDOWN = 100000; |
269 |
< |
|
269 |
> |
|
270 |
|
class Counter implements Runnable { |
271 |
|
final AtomicIntegerArray ai; |
272 |
|
volatile int counts; |
295 |
|
*/ |
296 |
|
public void testCountingInMultipleThreads() { |
297 |
|
try { |
298 |
< |
final AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
299 |
< |
for (int i = 0; i < SIZE; ++i) |
298 |
> |
final AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
299 |
> |
for (int i = 0; i < SIZE; ++i) |
300 |
|
ai.set(i, COUNTDOWN); |
301 |
|
Counter c1 = new Counter(ai); |
302 |
|
Counter c2 = new Counter(ai); |
308 |
|
t2.join(); |
309 |
|
assertEquals(c1.counts+c2.counts, SIZE * COUNTDOWN); |
310 |
|
} |
311 |
< |
catch(InterruptedException ie) { |
311 |
> |
catch (InterruptedException ie) { |
312 |
|
unexpectedException(); |
313 |
|
} |
314 |
|
} |
318 |
|
* a deserialized serialized array holds same values |
319 |
|
*/ |
320 |
|
public void testSerialization() { |
321 |
< |
AtomicIntegerArray l = new AtomicIntegerArray(SIZE); |
322 |
< |
for (int i = 0; i < SIZE; ++i) |
321 |
> |
AtomicIntegerArray l = new AtomicIntegerArray(SIZE); |
322 |
> |
for (int i = 0; i < SIZE; ++i) |
323 |
|
l.set(i, -i); |
324 |
|
|
325 |
|
try { |
334 |
|
for (int i = 0; i < SIZE; ++i) { |
335 |
|
assertEquals(l.get(i), r.get(i)); |
336 |
|
} |
337 |
< |
} catch(Exception e){ |
337 |
> |
} catch (Exception e) { |
338 |
|
e.printStackTrace(); |
339 |
|
unexpectedException(); |
340 |
|
} |
343 |
|
|
344 |
|
/** |
345 |
|
* toString returns current value. |
346 |
< |
*/ |
346 |
> |
*/ |
347 |
|
public void testToString() { |
348 |
|
int[] a = { 17, 3, -42, 99, -7}; |
349 |
|
AtomicIntegerArray ai = new AtomicIntegerArray(a); |