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 |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
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 |
< |
import java.util.*; |
10 |
> |
import java.util.Arrays; |
11 |
> |
import java.util.concurrent.atomic.AtomicLongArray; |
12 |
|
|
13 |
|
public class AtomicLongArrayTest extends JSR166TestCase { |
14 |
< |
public static void main (String[] args) { |
15 |
< |
junit.textui.TestRunner.run (suite()); |
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); |
42 |
|
* constructor with array is of same size and has all elements |
43 |
|
*/ |
44 |
|
public void testConstructor2() { |
45 |
< |
long[] a = { 17L, 3L, -42L, 99L, -7L}; |
45 |
> |
long[] a = { 17L, 3L, -42L, 99L, -7L }; |
46 |
|
AtomicLongArray ai = new AtomicLongArray(a); |
47 |
|
assertEquals(a.length, ai.length()); |
48 |
|
for (int i = 0; i < a.length; ++i) |
160 |
|
} |
161 |
|
|
162 |
|
/** |
163 |
< |
* getAndSet returns previous value and sets to given value at given index |
163 |
> |
* getAndSet returns previous value and sets to given value at given index |
164 |
|
*/ |
165 |
|
public void testGetAndSet() { |
166 |
|
AtomicLongArray ai = new AtomicLongArray(SIZE); |
173 |
|
} |
174 |
|
|
175 |
|
/** |
176 |
< |
* getAndAdd returns previous value and adds given value |
176 |
> |
* getAndAdd returns previous value and adds given value |
177 |
|
*/ |
178 |
|
public void testGetAndAdd() { |
179 |
|
AtomicLongArray ai = new AtomicLongArray(SIZE); |
217 |
|
} |
218 |
|
|
219 |
|
/** |
220 |
< |
* addAndGet adds given value to current, and returns current value |
220 |
> |
* addAndGet adds given value to current, and returns current value |
221 |
|
*/ |
222 |
|
public void testAddAndGet() { |
223 |
|
AtomicLongArray ai = new AtomicLongArray(SIZE); |
263 |
|
|
264 |
|
static final long COUNTDOWN = 100000; |
265 |
|
|
266 |
< |
class Counter implements Runnable { |
266 |
> |
class Counter extends CheckedRunnable { |
267 |
|
final AtomicLongArray ai; |
268 |
|
volatile long counts; |
269 |
|
Counter(AtomicLongArray a) { ai = a; } |
270 |
< |
public void run() { |
270 |
> |
public void realRun() { |
271 |
|
for (;;) { |
272 |
|
boolean done = true; |
273 |
|
for (int i = 0; i < ai.length(); ++i) { |
274 |
|
long v = ai.get(i); |
275 |
< |
threadAssertTrue(v >= 0); |
275 |
> |
assertTrue(v >= 0); |
276 |
|
if (v != 0) { |
277 |
|
done = false; |
278 |
|
if (ai.compareAndSet(i, v, v-1)) |
308 |
|
* a deserialized serialized array holds same values |
309 |
|
*/ |
310 |
|
public void testSerialization() throws Exception { |
311 |
< |
AtomicLongArray l = new AtomicLongArray(SIZE); |
311 |
> |
AtomicLongArray x = new AtomicLongArray(SIZE); |
312 |
|
for (int i = 0; i < SIZE; ++i) |
313 |
< |
l.set(i, -i); |
314 |
< |
|
315 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
316 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
318 |
< |
out.writeObject(l); |
319 |
< |
out.close(); |
320 |
< |
|
321 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
322 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
323 |
< |
AtomicLongArray r = (AtomicLongArray) in.readObject(); |
313 |
> |
x.set(i, -i); |
314 |
> |
AtomicLongArray y = serialClone(x); |
315 |
> |
assertTrue(x != y); |
316 |
> |
assertEquals(x.length(), y.length()); |
317 |
|
for (int i = 0; i < SIZE; ++i) { |
318 |
< |
assertEquals(l.get(i), r.get(i)); |
318 |
> |
assertEquals(x.get(i), y.get(i)); |
319 |
|
} |
320 |
|
} |
321 |
|
|
323 |
|
* toString returns current value. |
324 |
|
*/ |
325 |
|
public void testToString() { |
326 |
< |
long[] a = { 17, 3, -42, 99, -7}; |
326 |
> |
long[] a = { 17, 3, -42, 99, -7 }; |
327 |
|
AtomicLongArray ai = new AtomicLongArray(a); |
328 |
|
assertEquals(Arrays.toString(a), ai.toString()); |
329 |
|
} |
330 |
|
|
338 |
– |
|
331 |
|
} |