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.*; |
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 |
+ |
* constructor with null array throws NPE |
32 |
+ |
*/ |
33 |
+ |
public void testConstructor2NPE() { |
34 |
+ |
try { |
35 |
+ |
long[] a = null; |
36 |
+ |
AtomicLongArray ai = new AtomicLongArray(a); |
37 |
+ |
} catch (NullPointerException success) { |
38 |
+ |
} catch (Exception ex) { |
39 |
+ |
unexpectedException(); |
40 |
+ |
} |
41 |
+ |
} |
42 |
+ |
|
43 |
+ |
/** |
44 |
+ |
* constructor with array is of same size and has all elements |
45 |
+ |
*/ |
46 |
+ |
public void testConstructor2() { |
47 |
+ |
long[] a = { 17L, 3L, -42L, 99L, -7L}; |
48 |
+ |
AtomicLongArray ai = new AtomicLongArray(a); |
49 |
+ |
assertEquals(a.length, ai.length()); |
50 |
+ |
for (int i = 0; i < a.length; ++i) |
51 |
+ |
assertEquals(a[i], ai.get(i)); |
52 |
+ |
} |
53 |
+ |
|
54 |
+ |
/** |
55 |
+ |
* get and set for out of bound indices throw IndexOutOfBoundsException |
56 |
+ |
*/ |
57 |
+ |
public void testIndexing(){ |
58 |
+ |
AtomicLongArray ai = new AtomicLongArray(SIZE); |
59 |
+ |
try { |
60 |
+ |
ai.get(SIZE); |
61 |
+ |
} catch(IndexOutOfBoundsException success){ |
62 |
+ |
} |
63 |
+ |
try { |
64 |
+ |
ai.get(-1); |
65 |
+ |
} catch(IndexOutOfBoundsException success){ |
66 |
+ |
} |
67 |
+ |
try { |
68 |
+ |
ai.set(SIZE, 0); |
69 |
+ |
} catch(IndexOutOfBoundsException success){ |
70 |
+ |
} |
71 |
+ |
try { |
72 |
+ |
ai.set(-1, 0); |
73 |
+ |
} catch(IndexOutOfBoundsException success){ |
74 |
+ |
} |
75 |
+ |
} |
76 |
+ |
|
77 |
+ |
/** |
78 |
+ |
* get returns the last value set at index |
79 |
+ |
*/ |
80 |
|
public void testGetSet(){ |
81 |
|
AtomicLongArray ai = new AtomicLongArray(SIZE); |
82 |
|
for (int i = 0; i < SIZE; ++i) { |
89 |
|
} |
90 |
|
} |
91 |
|
|
92 |
+ |
/** |
93 |
+ |
* compareAndSet succeeds in changing value if equal to expected else fails |
94 |
+ |
*/ |
95 |
|
public void testCompareAndSet(){ |
96 |
|
AtomicLongArray ai = new AtomicLongArray(SIZE); |
97 |
|
for (int i = 0; i < SIZE; ++i) { |
106 |
|
} |
107 |
|
} |
108 |
|
|
109 |
+ |
/** |
110 |
+ |
* compareAndSet in one thread enables another waiting for value |
111 |
+ |
* to succeed |
112 |
+ |
*/ |
113 |
+ |
public void testCompareAndSetInMultipleThreads() { |
114 |
+ |
final AtomicLongArray a = new AtomicLongArray(1); |
115 |
+ |
a.set(0, 1); |
116 |
+ |
Thread t = new Thread(new Runnable() { |
117 |
+ |
public void run() { |
118 |
+ |
while(!a.compareAndSet(0, 2, 3)) Thread.yield(); |
119 |
+ |
}}); |
120 |
+ |
try { |
121 |
+ |
t.start(); |
122 |
+ |
assertTrue(a.compareAndSet(0, 1, 2)); |
123 |
+ |
t.join(LONG_DELAY_MS); |
124 |
+ |
assertFalse(t.isAlive()); |
125 |
+ |
assertEquals(a.get(0), 3); |
126 |
+ |
} |
127 |
+ |
catch(Exception e) { |
128 |
+ |
unexpectedException(); |
129 |
+ |
} |
130 |
+ |
} |
131 |
+ |
|
132 |
+ |
/** |
133 |
+ |
* repeated weakCompareAndSet succeeds in changing value when equal |
134 |
+ |
* to expected |
135 |
+ |
*/ |
136 |
|
public void testWeakCompareAndSet(){ |
137 |
|
AtomicLongArray ai = new AtomicLongArray(SIZE); |
138 |
|
for (int i = 0; i < SIZE; ++i) { |
145 |
|
} |
146 |
|
} |
147 |
|
|
148 |
+ |
/** |
149 |
+ |
* getAndSet returns previous value and sets to given value at given index |
150 |
+ |
*/ |
151 |
|
public void testGetAndSet(){ |
152 |
|
AtomicLongArray ai = new AtomicLongArray(SIZE); |
153 |
|
for (int i = 0; i < SIZE; ++i) { |
158 |
|
} |
159 |
|
} |
160 |
|
|
161 |
+ |
/** |
162 |
+ |
* getAndAdd returns previous value and adds given value |
163 |
+ |
*/ |
164 |
|
public void testGetAndAdd(){ |
165 |
|
AtomicLongArray ai = new AtomicLongArray(SIZE); |
166 |
|
for (int i = 0; i < SIZE; ++i) { |
172 |
|
} |
173 |
|
} |
174 |
|
|
175 |
+ |
/** |
176 |
+ |
* getAndDecrement returns previous value and decrements |
177 |
+ |
*/ |
178 |
|
public void testGetAndDecrement(){ |
179 |
|
AtomicLongArray ai = new AtomicLongArray(SIZE); |
180 |
|
for (int i = 0; i < SIZE; ++i) { |
185 |
|
} |
186 |
|
} |
187 |
|
|
188 |
+ |
/** |
189 |
+ |
* getAndIncrement returns previous value and increments |
190 |
+ |
*/ |
191 |
|
public void testGetAndIncrement(){ |
192 |
|
AtomicLongArray ai = new AtomicLongArray(SIZE); |
193 |
|
for (int i = 0; i < SIZE; ++i) { |
202 |
|
} |
203 |
|
} |
204 |
|
|
205 |
+ |
/** |
206 |
+ |
* addAndGet adds given value to current, and returns current value |
207 |
+ |
*/ |
208 |
|
public void testAddAndGet() { |
209 |
|
AtomicLongArray ai = new AtomicLongArray(SIZE); |
210 |
|
for (int i = 0; i < SIZE; ++i) { |
216 |
|
} |
217 |
|
} |
218 |
|
|
219 |
+ |
/** |
220 |
+ |
* decrementAndGet decrements and returns current value |
221 |
+ |
*/ |
222 |
|
public void testDecrementAndGet(){ |
223 |
|
AtomicLongArray ai = new AtomicLongArray(SIZE); |
224 |
|
for (int i = 0; i < SIZE; ++i) { |
230 |
|
} |
231 |
|
} |
232 |
|
|
233 |
+ |
/** |
234 |
+ |
* incrementAndGet increments and returns current value |
235 |
+ |
*/ |
236 |
|
public void testIncrementAndGet() { |
237 |
|
AtomicLongArray ai = new AtomicLongArray(SIZE); |
238 |
|
for (int i = 0; i < SIZE; ++i) { |
271 |
|
} |
272 |
|
} |
273 |
|
|
274 |
+ |
/** |
275 |
+ |
* Multiple threads using same array of counters successfully |
276 |
+ |
* update a number of times equal to total count |
277 |
+ |
*/ |
278 |
|
public void testCountingInMultipleThreads() { |
279 |
|
try { |
280 |
|
final AtomicLongArray ai = new AtomicLongArray(SIZE); |
291 |
|
assertEquals(c1.counts+c2.counts, SIZE * COUNTDOWN); |
292 |
|
} |
293 |
|
catch(InterruptedException ie) { |
294 |
< |
fail("unexpected exception"); |
294 |
> |
unexpectedException(); |
295 |
|
} |
296 |
|
} |
297 |
|
|
298 |
+ |
/** |
299 |
+ |
* a deserialized serialized array holds same values |
300 |
+ |
*/ |
301 |
|
public void testSerialization() { |
302 |
|
AtomicLongArray l = new AtomicLongArray(SIZE); |
303 |
|
for (int i = 0; i < SIZE; ++i) |
316 |
|
assertEquals(l.get(i), r.get(i)); |
317 |
|
} |
318 |
|
} catch(Exception e){ |
319 |
< |
e.printStackTrace(); |
208 |
< |
fail("unexpected exception"); |
319 |
> |
unexpectedException(); |
320 |
|
} |
321 |
|
} |
322 |
|
|