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