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)); |
26 |
> |
AtomicIntegerArray aa = new AtomicIntegerArray(SIZE); |
27 |
> |
for (int i = 0; i < SIZE; i++) |
28 |
> |
assertEquals(0, aa.get(i)); |
29 |
|
} |
30 |
|
|
31 |
|
/** |
34 |
|
public void testConstructor2NPE() { |
35 |
|
try { |
36 |
|
int[] a = null; |
37 |
< |
AtomicIntegerArray ai = new AtomicIntegerArray(a); |
37 |
> |
AtomicIntegerArray aa = new AtomicIntegerArray(a); |
38 |
|
shouldThrow(); |
39 |
|
} catch (NullPointerException success) {} |
40 |
|
} |
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)); |
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 ai = new AtomicIntegerArray(SIZE); |
58 |
< |
try { |
59 |
< |
ai.get(SIZE); |
60 |
< |
shouldThrow(); |
61 |
< |
} catch (IndexOutOfBoundsException success) { |
62 |
< |
} |
63 |
< |
try { |
64 |
< |
ai.get(-1); |
65 |
< |
shouldThrow(); |
66 |
< |
} catch (IndexOutOfBoundsException success) { |
67 |
< |
} |
68 |
< |
try { |
69 |
< |
ai.set(SIZE, 0); |
70 |
< |
shouldThrow(); |
71 |
< |
} catch (IndexOutOfBoundsException success) { |
72 |
< |
} |
73 |
< |
try { |
74 |
< |
ai.set(-1, 0); |
75 |
< |
shouldThrow(); |
76 |
< |
} catch (IndexOutOfBoundsException success) { |
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 |
|
|
91 |
|
* get returns the last value set at index |
92 |
|
*/ |
93 |
|
public void testGetSet() { |
94 |
< |
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
95 |
< |
for (int i = 0; i < SIZE; ++i) { |
96 |
< |
ai.set(i, 1); |
97 |
< |
assertEquals(1,ai.get(i)); |
98 |
< |
ai.set(i, 2); |
99 |
< |
assertEquals(2,ai.get(i)); |
100 |
< |
ai.set(i, -3); |
101 |
< |
assertEquals(-3,ai.get(i)); |
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 |
|
|
106 |
|
* get returns the last value lazySet at index by same thread |
107 |
|
*/ |
108 |
|
public void testGetLazySet() { |
109 |
< |
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
110 |
< |
for (int i = 0; i < SIZE; ++i) { |
111 |
< |
ai.lazySet(i, 1); |
112 |
< |
assertEquals(1,ai.get(i)); |
113 |
< |
ai.lazySet(i, 2); |
114 |
< |
assertEquals(2,ai.get(i)); |
115 |
< |
ai.lazySet(i, -3); |
116 |
< |
assertEquals(-3,ai.get(i)); |
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 |
|
|
121 |
|
* compareAndSet succeeds in changing value if equal to expected else fails |
122 |
|
*/ |
123 |
|
public void testCompareAndSet() { |
124 |
< |
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
125 |
< |
for (int i = 0; i < SIZE; ++i) { |
126 |
< |
ai.set(i, 1); |
127 |
< |
assertTrue(ai.compareAndSet(i, 1,2)); |
128 |
< |
assertTrue(ai.compareAndSet(i, 2,-4)); |
129 |
< |
assertEquals(-4,ai.get(i)); |
130 |
< |
assertFalse(ai.compareAndSet(i, -5,7)); |
131 |
< |
assertEquals(-4,ai.get(i)); |
132 |
< |
assertTrue(ai.compareAndSet(i, -4,7)); |
133 |
< |
assertEquals(7,ai.get(i)); |
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 |
|
|
151 |
|
assertTrue(a.compareAndSet(0, 1, 2)); |
152 |
|
t.join(LONG_DELAY_MS); |
153 |
|
assertFalse(t.isAlive()); |
154 |
< |
assertEquals(a.get(0), 3); |
154 |
> |
assertEquals(3, a.get(0)); |
155 |
|
} |
156 |
|
|
157 |
|
/** |
159 |
|
* to expected |
160 |
|
*/ |
161 |
|
public void testWeakCompareAndSet() { |
162 |
< |
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
163 |
< |
for (int i = 0; i < SIZE; ++i) { |
164 |
< |
ai.set(i, 1); |
165 |
< |
while (!ai.weakCompareAndSet(i, 1,2)); |
166 |
< |
while (!ai.weakCompareAndSet(i, 2,-4)); |
167 |
< |
assertEquals(-4,ai.get(i)); |
168 |
< |
while (!ai.weakCompareAndSet(i, -4,7)); |
169 |
< |
assertEquals(7,ai.get(i)); |
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 |
|
|
174 |
|
* getAndSet returns previous value and sets to given value at given index |
175 |
|
*/ |
176 |
|
public void testGetAndSet() { |
177 |
< |
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
178 |
< |
for (int i = 0; i < SIZE; ++i) { |
179 |
< |
ai.set(i, 1); |
180 |
< |
assertEquals(1,ai.getAndSet(i,0)); |
181 |
< |
assertEquals(0,ai.getAndSet(i,-10)); |
182 |
< |
assertEquals(-10,ai.getAndSet(i,1)); |
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 |
|
|
187 |
|
* getAndAdd returns previous value and adds given value |
188 |
|
*/ |
189 |
|
public void testGetAndAdd() { |
190 |
< |
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
191 |
< |
for (int i = 0; i < SIZE; ++i) { |
192 |
< |
ai.set(i, 1); |
193 |
< |
assertEquals(1,ai.getAndAdd(i,2)); |
194 |
< |
assertEquals(3,ai.get(i)); |
195 |
< |
assertEquals(3,ai.getAndAdd(i,-4)); |
196 |
< |
assertEquals(-1,ai.get(i)); |
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 |
|
|
201 |
|
* getAndDecrement returns previous value and decrements |
202 |
|
*/ |
203 |
|
public void testGetAndDecrement() { |
204 |
< |
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
205 |
< |
for (int i = 0; i < SIZE; ++i) { |
206 |
< |
ai.set(i, 1); |
207 |
< |
assertEquals(1,ai.getAndDecrement(i)); |
208 |
< |
assertEquals(0,ai.getAndDecrement(i)); |
209 |
< |
assertEquals(-1,ai.getAndDecrement(i)); |
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 |
|
|
214 |
|
* getAndIncrement returns previous value and increments |
215 |
|
*/ |
216 |
|
public void testGetAndIncrement() { |
217 |
< |
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
218 |
< |
for (int i = 0; i < SIZE; ++i) { |
219 |
< |
ai.set(i, 1); |
220 |
< |
assertEquals(1,ai.getAndIncrement(i)); |
221 |
< |
assertEquals(2,ai.get(i)); |
222 |
< |
ai.set(i,-2); |
223 |
< |
assertEquals(-2,ai.getAndIncrement(i)); |
224 |
< |
assertEquals(-1,ai.getAndIncrement(i)); |
225 |
< |
assertEquals(0,ai.getAndIncrement(i)); |
226 |
< |
assertEquals(1,ai.get(i)); |
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 |
|
|
231 |
|
* addAndGet adds given value to current, and returns current value |
232 |
|
*/ |
233 |
|
public void testAddAndGet() { |
234 |
< |
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
235 |
< |
for (int i = 0; i < SIZE; ++i) { |
236 |
< |
ai.set(i, 1); |
237 |
< |
assertEquals(3,ai.addAndGet(i,2)); |
238 |
< |
assertEquals(3,ai.get(i)); |
239 |
< |
assertEquals(-1,ai.addAndGet(i,-4)); |
240 |
< |
assertEquals(-1,ai.get(i)); |
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 |
|
|
245 |
|
* decrementAndGet decrements and returns current value |
246 |
|
*/ |
247 |
|
public void testDecrementAndGet() { |
248 |
< |
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
249 |
< |
for (int i = 0; i < SIZE; ++i) { |
250 |
< |
ai.set(i, 1); |
251 |
< |
assertEquals(0,ai.decrementAndGet(i)); |
252 |
< |
assertEquals(-1,ai.decrementAndGet(i)); |
253 |
< |
assertEquals(-2,ai.decrementAndGet(i)); |
254 |
< |
assertEquals(-2,ai.get(i)); |
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 |
|
|
259 |
|
* incrementAndGet increments and returns current value |
260 |
|
*/ |
261 |
|
public void testIncrementAndGet() { |
262 |
< |
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
263 |
< |
for (int i = 0; i < SIZE; ++i) { |
264 |
< |
ai.set(i, 1); |
265 |
< |
assertEquals(2,ai.incrementAndGet(i)); |
266 |
< |
assertEquals(2,ai.get(i)); |
267 |
< |
ai.set(i, -2); |
268 |
< |
assertEquals(-1,ai.incrementAndGet(i)); |
269 |
< |
assertEquals(0,ai.incrementAndGet(i)); |
270 |
< |
assertEquals(1,ai.incrementAndGet(i)); |
271 |
< |
assertEquals(1,ai.get(i)); |
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 ai; |
278 |
> |
final AtomicIntegerArray aa; |
279 |
|
volatile int counts; |
280 |
< |
Counter(AtomicIntegerArray a) { ai = a; } |
280 |
> |
Counter(AtomicIntegerArray a) { aa = a; } |
281 |
|
public void realRun() { |
282 |
|
for (;;) { |
283 |
|
boolean done = true; |
284 |
< |
for (int i = 0; i < ai.length(); ++i) { |
285 |
< |
int v = ai.get(i); |
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 (ai.compareAndSet(i, v, v-1)) |
289 |
> |
if (aa.compareAndSet(i, v, v-1)) |
290 |
|
++counts; |
291 |
|
} |
292 |
|
} |
301 |
|
* update a number of times equal to total count |
302 |
|
*/ |
303 |
|
public void testCountingInMultipleThreads() throws InterruptedException { |
304 |
< |
final AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
305 |
< |
for (int i = 0; i < SIZE; ++i) |
306 |
< |
ai.set(i, COUNTDOWN); |
307 |
< |
Counter c1 = new Counter(ai); |
308 |
< |
Counter c2 = new Counter(ai); |
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(); |
323 |
|
for (int i = 0; i < SIZE; i++) |
324 |
|
x.set(i, -i); |
325 |
|
AtomicIntegerArray y = serialClone(x); |
326 |
< |
assertTrue(x != y); |
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)); |
335 |
|
*/ |
336 |
|
public void testToString() { |
337 |
|
int[] a = { 17, 3, -42, 99, -7 }; |
338 |
< |
AtomicIntegerArray ai = new AtomicIntegerArray(a); |
339 |
< |
assertEquals(Arrays.toString(a), ai.toString()); |
338 |
> |
AtomicIntegerArray aa = new AtomicIntegerArray(a); |
339 |
> |
assertEquals(Arrays.toString(a), aa.toString()); |
340 |
|
} |
341 |
|
|
342 |
|
} |