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