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.*; |
10 |
|
import java.util.concurrent.atomic.*; |
11 |
|
import java.io.*; |
12 |
+ |
import java.util.*; |
13 |
|
|
14 |
|
public class AtomicLongArrayTest extends JSR166TestCase { |
15 |
|
public static void main (String[] args) { |
20 |
|
} |
21 |
|
|
22 |
|
/** |
23 |
< |
* |
23 |
> |
* constructor creates array of given size with all elements zero |
24 |
|
*/ |
25 |
|
public void testConstructor(){ |
26 |
|
AtomicLongArray ai = new AtomicLongArray(SIZE); |
29 |
|
} |
30 |
|
|
31 |
|
/** |
32 |
< |
* |
32 |
> |
* constructor with null array throws NPE |
33 |
> |
*/ |
34 |
> |
public void testConstructor2NPE() { |
35 |
> |
try { |
36 |
> |
long[] a = null; |
37 |
> |
AtomicLongArray ai = new AtomicLongArray(a); |
38 |
> |
} catch (NullPointerException success) { |
39 |
> |
} catch (Exception ex) { |
40 |
> |
unexpectedException(); |
41 |
> |
} |
42 |
> |
} |
43 |
> |
|
44 |
> |
/** |
45 |
> |
* constructor with array is of same size and has all elements |
46 |
> |
*/ |
47 |
> |
public void testConstructor2() { |
48 |
> |
long[] a = { 17L, 3L, -42L, 99L, -7L}; |
49 |
> |
AtomicLongArray ai = new AtomicLongArray(a); |
50 |
> |
assertEquals(a.length, ai.length()); |
51 |
> |
for (int i = 0; i < a.length; ++i) |
52 |
> |
assertEquals(a[i], ai.get(i)); |
53 |
> |
} |
54 |
> |
|
55 |
> |
/** |
56 |
> |
* get and set for out of bound indices throw IndexOutOfBoundsException |
57 |
> |
*/ |
58 |
> |
public void testIndexing(){ |
59 |
> |
AtomicLongArray ai = new AtomicLongArray(SIZE); |
60 |
> |
try { |
61 |
> |
ai.get(SIZE); |
62 |
> |
} catch(IndexOutOfBoundsException success){ |
63 |
> |
} |
64 |
> |
try { |
65 |
> |
ai.get(-1); |
66 |
> |
} catch(IndexOutOfBoundsException success){ |
67 |
> |
} |
68 |
> |
try { |
69 |
> |
ai.set(SIZE, 0); |
70 |
> |
} catch(IndexOutOfBoundsException success){ |
71 |
> |
} |
72 |
> |
try { |
73 |
> |
ai.set(-1, 0); |
74 |
> |
} catch(IndexOutOfBoundsException success){ |
75 |
> |
} |
76 |
> |
} |
77 |
> |
|
78 |
> |
/** |
79 |
> |
* get returns the last value set at index |
80 |
|
*/ |
81 |
|
public void testGetSet(){ |
82 |
|
AtomicLongArray ai = new AtomicLongArray(SIZE); |
91 |
|
} |
92 |
|
|
93 |
|
/** |
94 |
< |
* |
94 |
> |
* compareAndSet succeeds in changing value if equal to expected else fails |
95 |
|
*/ |
96 |
|
public void testCompareAndSet(){ |
97 |
|
AtomicLongArray ai = new AtomicLongArray(SIZE); |
108 |
|
} |
109 |
|
|
110 |
|
/** |
111 |
< |
* |
111 |
> |
* compareAndSet in one thread enables another waiting for value |
112 |
> |
* to succeed |
113 |
> |
*/ |
114 |
> |
public void testCompareAndSetInMultipleThreads() { |
115 |
> |
final AtomicLongArray a = new AtomicLongArray(1); |
116 |
> |
a.set(0, 1); |
117 |
> |
Thread t = new Thread(new Runnable() { |
118 |
> |
public void run() { |
119 |
> |
while(!a.compareAndSet(0, 2, 3)) Thread.yield(); |
120 |
> |
}}); |
121 |
> |
try { |
122 |
> |
t.start(); |
123 |
> |
assertTrue(a.compareAndSet(0, 1, 2)); |
124 |
> |
t.join(LONG_DELAY_MS); |
125 |
> |
assertFalse(t.isAlive()); |
126 |
> |
assertEquals(a.get(0), 3); |
127 |
> |
} |
128 |
> |
catch(Exception e) { |
129 |
> |
unexpectedException(); |
130 |
> |
} |
131 |
> |
} |
132 |
> |
|
133 |
> |
/** |
134 |
> |
* repeated weakCompareAndSet succeeds in changing value when equal |
135 |
> |
* to expected |
136 |
|
*/ |
137 |
|
public void testWeakCompareAndSet(){ |
138 |
|
AtomicLongArray ai = new AtomicLongArray(SIZE); |
147 |
|
} |
148 |
|
|
149 |
|
/** |
150 |
< |
* |
150 |
> |
* getAndSet returns previous value and sets to given value at given index |
151 |
|
*/ |
152 |
|
public void testGetAndSet(){ |
153 |
|
AtomicLongArray ai = new AtomicLongArray(SIZE); |
160 |
|
} |
161 |
|
|
162 |
|
/** |
163 |
< |
* |
163 |
> |
* getAndAdd returns previous value and adds given value |
164 |
|
*/ |
165 |
|
public void testGetAndAdd(){ |
166 |
|
AtomicLongArray ai = new AtomicLongArray(SIZE); |
174 |
|
} |
175 |
|
|
176 |
|
/** |
177 |
< |
* |
177 |
> |
* getAndDecrement returns previous value and decrements |
178 |
|
*/ |
179 |
|
public void testGetAndDecrement(){ |
180 |
|
AtomicLongArray ai = new AtomicLongArray(SIZE); |
187 |
|
} |
188 |
|
|
189 |
|
/** |
190 |
< |
* |
190 |
> |
* getAndIncrement returns previous value and increments |
191 |
|
*/ |
192 |
|
public void testGetAndIncrement(){ |
193 |
|
AtomicLongArray ai = new AtomicLongArray(SIZE); |
204 |
|
} |
205 |
|
|
206 |
|
/** |
207 |
< |
* |
207 |
> |
* addAndGet adds given value to current, and returns current value |
208 |
|
*/ |
209 |
|
public void testAddAndGet() { |
210 |
|
AtomicLongArray ai = new AtomicLongArray(SIZE); |
218 |
|
} |
219 |
|
|
220 |
|
/** |
221 |
< |
* |
221 |
> |
* decrementAndGet decrements and returns current value |
222 |
|
*/ |
223 |
|
public void testDecrementAndGet(){ |
224 |
|
AtomicLongArray ai = new AtomicLongArray(SIZE); |
232 |
|
} |
233 |
|
|
234 |
|
/** |
235 |
< |
* |
235 |
> |
* incrementAndGet increments and returns current value |
236 |
|
*/ |
237 |
|
public void testIncrementAndGet() { |
238 |
|
AtomicLongArray ai = new AtomicLongArray(SIZE); |
273 |
|
} |
274 |
|
|
275 |
|
/** |
276 |
< |
* |
276 |
> |
* Multiple threads using same array of counters successfully |
277 |
> |
* update a number of times equal to total count |
278 |
|
*/ |
279 |
|
public void testCountingInMultipleThreads() { |
280 |
|
try { |
297 |
|
} |
298 |
|
|
299 |
|
/** |
300 |
< |
* |
300 |
> |
* a deserialized serialized array holds same values |
301 |
|
*/ |
302 |
|
public void testSerialization() { |
303 |
|
AtomicLongArray l = new AtomicLongArray(SIZE); |
321 |
|
} |
322 |
|
} |
323 |
|
|
324 |
+ |
/** |
325 |
+ |
* toString returns current value. |
326 |
+ |
*/ |
327 |
+ |
public void testToString() { |
328 |
+ |
long[] a = { 17, 3, -42, 99, -7}; |
329 |
+ |
AtomicLongArray ai = new AtomicLongArray(a); |
330 |
+ |
assertEquals(Arrays.toString(a), ai.toString()); |
331 |
+ |
} |
332 |
+ |
|
333 |
|
|
334 |
|
} |