1 |
/* |
2 |
* Written by Doug Lea and Martin Buchholz with assistance from |
3 |
* members of JCP JSR-166 Expert Group and released to the public |
4 |
* domain, as explained at |
5 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
6 |
*/ |
7 |
|
8 |
import junit.framework.*; |
9 |
import java.util.Arrays; |
10 |
import jsr166e.extra.AtomicDoubleArray; |
11 |
|
12 |
public class AtomicDoubleArrayTest extends JSR166TestCase { |
13 |
public static void main(String[] args) { |
14 |
junit.textui.TestRunner.run(suite()); |
15 |
} |
16 |
public static Test suite() { |
17 |
return new TestSuite(AtomicDoubleArrayTest.class); |
18 |
} |
19 |
|
20 |
final double[] VALUES = { |
21 |
Double.NEGATIVE_INFINITY, |
22 |
-Double.MAX_VALUE, |
23 |
(double)Long.MIN_VALUE, |
24 |
(double)Integer.MIN_VALUE, |
25 |
-Math.PI, |
26 |
-1.0, |
27 |
-Double.MIN_VALUE, |
28 |
-0.0, |
29 |
+0.0, |
30 |
Double.MIN_VALUE, |
31 |
1.0, |
32 |
Math.PI, |
33 |
(double)Integer.MAX_VALUE, |
34 |
(double)Long.MAX_VALUE, |
35 |
Double.MAX_VALUE, |
36 |
Double.POSITIVE_INFINITY, |
37 |
Double.NaN, |
38 |
Float.MAX_VALUE, |
39 |
}; |
40 |
|
41 |
/** The notion of equality used by AtomicDoubleArray */ |
42 |
boolean bitEquals(double x, double y) { |
43 |
return Double.doubleToRawLongBits(x) == Double.doubleToRawLongBits(y); |
44 |
} |
45 |
|
46 |
void assertBitEquals(double x, double y) { |
47 |
assertEquals(Double.doubleToRawLongBits(x), |
48 |
Double.doubleToRawLongBits(y)); |
49 |
} |
50 |
|
51 |
/** |
52 |
* constructor creates array of given size with all elements zero |
53 |
*/ |
54 |
public void testConstructor() { |
55 |
AtomicDoubleArray aa = new AtomicDoubleArray(SIZE); |
56 |
for (int i = 0; i < SIZE; i++) |
57 |
assertBitEquals(0.0, aa.get(i)); |
58 |
} |
59 |
|
60 |
/** |
61 |
* constructor with null array throws NPE |
62 |
*/ |
63 |
public void testConstructor2NPE() { |
64 |
try { |
65 |
double[] a = null; |
66 |
AtomicDoubleArray aa = new AtomicDoubleArray(a); |
67 |
shouldThrow(); |
68 |
} catch (NullPointerException success) {} |
69 |
} |
70 |
|
71 |
/** |
72 |
* constructor with array is of same size and has all elements |
73 |
*/ |
74 |
public void testConstructor2() { |
75 |
AtomicDoubleArray aa = new AtomicDoubleArray(VALUES); |
76 |
assertEquals(VALUES.length, aa.length()); |
77 |
for (int i = 0; i < VALUES.length; i++) |
78 |
assertBitEquals(VALUES[i], aa.get(i)); |
79 |
} |
80 |
|
81 |
/** |
82 |
* constructor with empty array has size 0 and contains no elements |
83 |
*/ |
84 |
public void testConstructorEmptyArray() { |
85 |
AtomicDoubleArray aa = new AtomicDoubleArray(new double[0]); |
86 |
assertEquals(0, aa.length()); |
87 |
try { |
88 |
aa.get(0); |
89 |
shouldThrow(); |
90 |
} catch (IndexOutOfBoundsException success) {} |
91 |
} |
92 |
|
93 |
/** |
94 |
* constructor with length zero has size 0 and contains no elements |
95 |
*/ |
96 |
public void testConstructorZeroLength() { |
97 |
AtomicDoubleArray aa = new AtomicDoubleArray(0); |
98 |
assertEquals(0, aa.length()); |
99 |
try { |
100 |
aa.get(0); |
101 |
shouldThrow(); |
102 |
} catch (IndexOutOfBoundsException success) {} |
103 |
} |
104 |
|
105 |
/** |
106 |
* get and set for out of bound indices throw IndexOutOfBoundsException |
107 |
*/ |
108 |
public void testIndexing() { |
109 |
AtomicDoubleArray aa = new AtomicDoubleArray(SIZE); |
110 |
for (int index : new int[] { -1, SIZE }) { |
111 |
try { |
112 |
aa.get(index); |
113 |
shouldThrow(); |
114 |
} catch (IndexOutOfBoundsException success) {} |
115 |
try { |
116 |
aa.set(index, 1.0); |
117 |
shouldThrow(); |
118 |
} catch (IndexOutOfBoundsException success) {} |
119 |
try { |
120 |
aa.lazySet(index, 1.0); |
121 |
shouldThrow(); |
122 |
} catch (IndexOutOfBoundsException success) {} |
123 |
try { |
124 |
aa.compareAndSet(index, 1.0, 2.0); |
125 |
shouldThrow(); |
126 |
} catch (IndexOutOfBoundsException success) {} |
127 |
try { |
128 |
aa.weakCompareAndSet(index, 1.0, 2.0); |
129 |
shouldThrow(); |
130 |
} catch (IndexOutOfBoundsException success) {} |
131 |
try { |
132 |
aa.getAndAdd(index, 1.0); |
133 |
shouldThrow(); |
134 |
} catch (IndexOutOfBoundsException success) {} |
135 |
try { |
136 |
aa.addAndGet(index, 1.0); |
137 |
shouldThrow(); |
138 |
} catch (IndexOutOfBoundsException success) {} |
139 |
} |
140 |
} |
141 |
|
142 |
/** |
143 |
* get returns the last value set at index |
144 |
*/ |
145 |
public void testGetSet() { |
146 |
AtomicDoubleArray aa = new AtomicDoubleArray(VALUES.length); |
147 |
for (int i = 0; i < VALUES.length; i++) { |
148 |
assertBitEquals(0.0, aa.get(i)); |
149 |
aa.set(i, VALUES[i]); |
150 |
assertBitEquals(VALUES[i], aa.get(i)); |
151 |
aa.set(i, -3.0); |
152 |
assertBitEquals(-3.0, aa.get(i)); |
153 |
} |
154 |
} |
155 |
|
156 |
/** |
157 |
* get returns the last value lazySet at index by same thread |
158 |
*/ |
159 |
public void testGetLazySet() { |
160 |
AtomicDoubleArray aa = new AtomicDoubleArray(VALUES.length); |
161 |
for (int i = 0; i < VALUES.length; i++) { |
162 |
assertBitEquals(0.0, aa.get(i)); |
163 |
aa.lazySet(i, VALUES[i]); |
164 |
assertBitEquals(VALUES[i], aa.get(i)); |
165 |
aa.lazySet(i, -3.0); |
166 |
assertBitEquals(-3.0, aa.get(i)); |
167 |
} |
168 |
} |
169 |
|
170 |
/** |
171 |
* compareAndSet succeeds in changing value if equal to expected else fails |
172 |
*/ |
173 |
public void testCompareAndSet() { |
174 |
AtomicDoubleArray aa = new AtomicDoubleArray(SIZE); |
175 |
for (int i : new int[] { 0, SIZE - 1}) { |
176 |
double prev = 0.0; |
177 |
double unused = Math.E + Math.PI; |
178 |
for (double x : VALUES) { |
179 |
assertBitEquals(prev, aa.get(i)); |
180 |
assertFalse(aa.compareAndSet(i, unused, x)); |
181 |
assertBitEquals(prev, aa.get(i)); |
182 |
assertTrue(aa.compareAndSet(i, prev, x)); |
183 |
assertBitEquals(x, aa.get(i)); |
184 |
prev = x; |
185 |
} |
186 |
} |
187 |
} |
188 |
|
189 |
/** |
190 |
* compareAndSet in one thread enables another waiting for value |
191 |
* to succeed |
192 |
*/ |
193 |
public void testCompareAndSetInMultipleThreads() throws InterruptedException { |
194 |
final AtomicDoubleArray a = new AtomicDoubleArray(1); |
195 |
a.set(0, 1.0); |
196 |
Thread t = newStartedThread(new CheckedRunnable() { |
197 |
public void realRun() { |
198 |
while (!a.compareAndSet(0, 2.0, 3.0)) |
199 |
Thread.yield(); |
200 |
}}); |
201 |
|
202 |
assertTrue(a.compareAndSet(0, 1.0, 2.0)); |
203 |
awaitTermination(t); |
204 |
assertBitEquals(3.0, a.get(0)); |
205 |
} |
206 |
|
207 |
/** |
208 |
* repeated weakCompareAndSet succeeds in changing value when equal |
209 |
* to expected |
210 |
*/ |
211 |
public void testWeakCompareAndSet() { |
212 |
AtomicDoubleArray aa = new AtomicDoubleArray(SIZE); |
213 |
for (int i : new int[] { 0, SIZE - 1}) { |
214 |
double prev = 0.0; |
215 |
double unused = Math.E + Math.PI; |
216 |
for (double x : VALUES) { |
217 |
assertBitEquals(prev, aa.get(i)); |
218 |
assertFalse(aa.weakCompareAndSet(i, unused, x)); |
219 |
assertBitEquals(prev, aa.get(i)); |
220 |
while (!aa.weakCompareAndSet(i, prev, x)) |
221 |
; |
222 |
assertBitEquals(x, aa.get(i)); |
223 |
prev = x; |
224 |
} |
225 |
} |
226 |
} |
227 |
|
228 |
/** |
229 |
* getAndSet returns previous value and sets to given value at given index |
230 |
*/ |
231 |
public void testGetAndSet() { |
232 |
AtomicDoubleArray aa = new AtomicDoubleArray(SIZE); |
233 |
for (int i : new int[] { 0, SIZE - 1}) { |
234 |
double prev = 0.0; |
235 |
for (double x : VALUES) { |
236 |
assertBitEquals(prev, aa.getAndSet(i, x)); |
237 |
prev = x; |
238 |
} |
239 |
} |
240 |
} |
241 |
|
242 |
/** |
243 |
* getAndAdd returns previous value and adds given value |
244 |
*/ |
245 |
public void testGetAndAdd() { |
246 |
AtomicDoubleArray aa = new AtomicDoubleArray(SIZE); |
247 |
for (int i : new int[] { 0, SIZE - 1}) { |
248 |
for (double x : VALUES) { |
249 |
for (double y : VALUES) { |
250 |
aa.set(i, x); |
251 |
double z = aa.getAndAdd(i, y); |
252 |
assertBitEquals(x, z); |
253 |
assertBitEquals(x + y, aa.get(i)); |
254 |
} |
255 |
} |
256 |
} |
257 |
} |
258 |
|
259 |
/** |
260 |
* addAndGet adds given value to current, and returns current value |
261 |
*/ |
262 |
public void testAddAndGet() { |
263 |
AtomicDoubleArray aa = new AtomicDoubleArray(SIZE); |
264 |
for (int i : new int[] { 0, SIZE - 1}) { |
265 |
for (double x : VALUES) { |
266 |
for (double y : VALUES) { |
267 |
aa.set(i, x); |
268 |
double z = aa.addAndGet(i, y); |
269 |
assertBitEquals(x + y, z); |
270 |
assertBitEquals(x + y, aa.get(i)); |
271 |
} |
272 |
} |
273 |
} |
274 |
} |
275 |
|
276 |
static final long COUNTDOWN = 100000; |
277 |
|
278 |
class Counter extends CheckedRunnable { |
279 |
final AtomicDoubleArray aa; |
280 |
volatile long counts; |
281 |
Counter(AtomicDoubleArray a) { aa = a; } |
282 |
public void realRun() { |
283 |
for (;;) { |
284 |
boolean done = true; |
285 |
for (int i = 0; i < aa.length(); i++) { |
286 |
double v = aa.get(i); |
287 |
assertTrue(v >= 0); |
288 |
if (v != 0) { |
289 |
done = false; |
290 |
if (aa.compareAndSet(i, v, v - 1.0)) |
291 |
++counts; |
292 |
} |
293 |
} |
294 |
if (done) |
295 |
break; |
296 |
} |
297 |
} |
298 |
} |
299 |
|
300 |
/** |
301 |
* Multiple threads using same array of counters successfully |
302 |
* update a number of times equal to total count |
303 |
*/ |
304 |
public void testCountingInMultipleThreads() throws InterruptedException { |
305 |
final AtomicDoubleArray aa = new AtomicDoubleArray(SIZE); |
306 |
for (int i = 0; i < SIZE; i++) |
307 |
aa.set(i, (double)COUNTDOWN); |
308 |
Counter c1 = new Counter(aa); |
309 |
Counter c2 = new Counter(aa); |
310 |
Thread t1 = newStartedThread(c1); |
311 |
Thread t2 = newStartedThread(c2); |
312 |
awaitTermination(t1); |
313 |
awaitTermination(t2); |
314 |
assertEquals(c1.counts+c2.counts, SIZE * COUNTDOWN); |
315 |
} |
316 |
|
317 |
/** |
318 |
* a deserialized serialized array holds same values |
319 |
*/ |
320 |
public void testSerialization() throws Exception { |
321 |
AtomicDoubleArray x = new AtomicDoubleArray(SIZE); |
322 |
for (int i = 0; i < SIZE; i++) |
323 |
x.set(i, (double)-i); |
324 |
AtomicDoubleArray y = serialClone(x); |
325 |
assertTrue(x != y); |
326 |
assertEquals(x.length(), y.length()); |
327 |
for (int i = 0; i < SIZE; i++) |
328 |
assertBitEquals(x.get(i), y.get(i)); |
329 |
|
330 |
AtomicDoubleArray a = new AtomicDoubleArray(VALUES); |
331 |
AtomicDoubleArray b = serialClone(a); |
332 |
assertFalse(a.equals(b)); |
333 |
assertFalse(b.equals(a)); |
334 |
for (int i = 0; i < VALUES.length; i++) |
335 |
assertBitEquals(a.get(i), b.get(i)); |
336 |
} |
337 |
|
338 |
/** |
339 |
* toString returns current value. |
340 |
*/ |
341 |
public void testToString() { |
342 |
AtomicDoubleArray aa = new AtomicDoubleArray(VALUES); |
343 |
assertEquals(Arrays.toString(VALUES), aa.toString()); |
344 |
} |
345 |
|
346 |
/** |
347 |
* compareAndSet treats +0.0 and -0.0 as distinct values |
348 |
*/ |
349 |
public void testDistinctZeros() { |
350 |
AtomicDoubleArray aa = new AtomicDoubleArray(SIZE); |
351 |
for (int i : new int[] { 0, SIZE - 1}) { |
352 |
assertFalse(aa.compareAndSet(i, -0.0, 7.0)); |
353 |
assertFalse(aa.weakCompareAndSet(i, -0.0, 7.0)); |
354 |
assertBitEquals(+0.0, aa.get(i)); |
355 |
assertTrue(aa.compareAndSet(i, +0.0, -0.0)); |
356 |
assertBitEquals(-0.0, aa.get(i)); |
357 |
assertFalse(aa.compareAndSet(i, +0.0, 7.0)); |
358 |
assertFalse(aa.weakCompareAndSet(i, +0.0, 7.0)); |
359 |
assertBitEquals(-0.0, aa.get(i)); |
360 |
} |
361 |
} |
362 |
|
363 |
} |