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/licenses/publicdomain |
5 |
> |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
> |
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
|
import junit.framework.*; |
20 |
|
} |
21 |
|
|
22 |
|
|
23 |
< |
public void testConstructor(){ |
23 |
> |
/** |
24 |
> |
* constructor creates array of given size with all elements zero |
25 |
> |
*/ |
26 |
> |
public void testConstructor() { |
27 |
|
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
28 |
|
for (int i = 0; i < SIZE; ++i) |
29 |
|
assertEquals(0,ai.get(i)); |
30 |
|
} |
31 |
|
|
32 |
< |
public void testGetSet(){ |
32 |
> |
/** |
33 |
> |
* 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 |
> |
* 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 |
> |
*/ |
82 |
> |
public void testGetSet() { |
83 |
|
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
84 |
|
for (int i = 0; i < SIZE; ++i) { |
85 |
|
ai.set(i, 1); |
91 |
|
} |
92 |
|
} |
93 |
|
|
94 |
< |
public void testCompareAndSet(){ |
94 |
> |
/** |
95 |
> |
* compareAndSet succeeds in changing value if equal to expected else fails |
96 |
> |
*/ |
97 |
> |
public void testCompareAndSet() { |
98 |
|
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
99 |
|
for (int i = 0; i < SIZE; ++i) { |
100 |
|
ai.set(i, 1); |
108 |
|
} |
109 |
|
} |
110 |
|
|
111 |
< |
public void testWeakCompareAndSet(){ |
111 |
> |
/** |
112 |
> |
* 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 |
> |
*/ |
138 |
> |
public void testWeakCompareAndSet() { |
139 |
|
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
140 |
|
for (int i = 0; i < SIZE; ++i) { |
141 |
|
ai.set(i, 1); |
147 |
|
} |
148 |
|
} |
149 |
|
|
150 |
< |
public void testGetAndSet(){ |
150 |
> |
/** |
151 |
> |
* getAndSet returns previous value and sets to given value at given index |
152 |
> |
*/ |
153 |
> |
public void testGetAndSet() { |
154 |
|
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
155 |
|
for (int i = 0; i < SIZE; ++i) { |
156 |
|
ai.set(i, 1); |
160 |
|
} |
161 |
|
} |
162 |
|
|
163 |
< |
public void testGetAndAdd(){ |
163 |
> |
/** |
164 |
> |
* getAndAdd returns previous value and adds given value |
165 |
> |
*/ |
166 |
> |
public void testGetAndAdd() { |
167 |
|
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
168 |
|
for (int i = 0; i < SIZE; ++i) { |
169 |
|
ai.set(i, 1); |
174 |
|
} |
175 |
|
} |
176 |
|
|
177 |
< |
public void testGetAndDecrement(){ |
177 |
> |
/** |
178 |
> |
* getAndDecrement returns previous value and decrements |
179 |
> |
*/ |
180 |
> |
public void testGetAndDecrement() { |
181 |
|
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
182 |
|
for (int i = 0; i < SIZE; ++i) { |
183 |
|
ai.set(i, 1); |
187 |
|
} |
188 |
|
} |
189 |
|
|
190 |
< |
public void testGetAndIncrement(){ |
190 |
> |
/** |
191 |
> |
* getAndIncrement returns previous value and increments |
192 |
> |
*/ |
193 |
> |
public void testGetAndIncrement() { |
194 |
|
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
195 |
|
for (int i = 0; i < SIZE; ++i) { |
196 |
|
ai.set(i, 1); |
204 |
|
} |
205 |
|
} |
206 |
|
|
207 |
+ |
/** |
208 |
+ |
* addAndGet adds given value to current, and returns current value |
209 |
+ |
*/ |
210 |
|
public void testAddAndGet() { |
211 |
|
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
212 |
|
for (int i = 0; i < SIZE; ++i) { |
218 |
|
} |
219 |
|
} |
220 |
|
|
221 |
< |
public void testDecrementAndGet(){ |
221 |
> |
/** |
222 |
> |
* decrementAndGet decrements and returns current value |
223 |
> |
*/ |
224 |
> |
public void testDecrementAndGet() { |
225 |
|
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
226 |
|
for (int i = 0; i < SIZE; ++i) { |
227 |
|
ai.set(i, 1); |
232 |
|
} |
233 |
|
} |
234 |
|
|
235 |
+ |
/** |
236 |
+ |
* incrementAndGet increments and returns current value |
237 |
+ |
*/ |
238 |
|
public void testIncrementAndGet() { |
239 |
|
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
240 |
|
for (int i = 0; i < SIZE; ++i) { |
273 |
|
} |
274 |
|
} |
275 |
|
|
276 |
+ |
/** |
277 |
+ |
* Multiple threads using same array of counters successfully |
278 |
+ |
* update a number of times equal to total count |
279 |
+ |
*/ |
280 |
|
public void testCountingInMultipleThreads() { |
281 |
|
try { |
282 |
|
final AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
293 |
|
assertEquals(c1.counts+c2.counts, SIZE * COUNTDOWN); |
294 |
|
} |
295 |
|
catch(InterruptedException ie) { |
296 |
< |
fail("unexpected exception"); |
296 |
> |
unexpectedException(); |
297 |
|
} |
298 |
|
} |
299 |
|
|
300 |
|
|
301 |
+ |
/** |
302 |
+ |
* a deserialized serialized array holds same values |
303 |
+ |
*/ |
304 |
|
public void testSerialization() { |
305 |
|
AtomicIntegerArray l = new AtomicIntegerArray(SIZE); |
306 |
|
for (int i = 0; i < SIZE; ++i) |
320 |
|
} |
321 |
|
} catch(Exception e){ |
322 |
|
e.printStackTrace(); |
323 |
< |
fail("unexpected exception"); |
323 |
> |
unexpectedException(); |
324 |
|
} |
325 |
|
} |
326 |
|
|
327 |
+ |
|
328 |
+ |
|
329 |
|
} |