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