1 |
jsr166 |
1.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 |
jsr166 |
1.7 |
Float.MAX_VALUE, |
39 |
jsr166 |
1.1 |
}; |
40 |
|
|
|
41 |
jsr166 |
1.3 |
/** 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 |
jsr166 |
1.1 |
/** |
52 |
|
|
* constructor creates array of given size with all elements zero |
53 |
|
|
*/ |
54 |
|
|
public void testConstructor() { |
55 |
|
|
AtomicDoubleArray aa = new AtomicDoubleArray(SIZE); |
56 |
jsr166 |
1.3 |
for (int i = 0; i < SIZE; i++) |
57 |
|
|
assertBitEquals(0.0, aa.get(i)); |
58 |
jsr166 |
1.1 |
} |
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 |
jsr166 |
1.2 |
AtomicDoubleArray aa = new AtomicDoubleArray(VALUES); |
76 |
|
|
assertEquals(VALUES.length, aa.length()); |
77 |
jsr166 |
1.3 |
for (int i = 0; i < VALUES.length; i++) |
78 |
|
|
assertBitEquals(VALUES[i], aa.get(i)); |
79 |
jsr166 |
1.1 |
} |
80 |
|
|
|
81 |
|
|
/** |
82 |
jsr166 |
1.3 |
* constructor with empty array has size 0 and contains no elements |
83 |
jsr166 |
1.1 |
*/ |
84 |
jsr166 |
1.3 |
public void testConstructorEmptyArray() { |
85 |
|
|
AtomicDoubleArray aa = new AtomicDoubleArray(new double[0]); |
86 |
|
|
assertEquals(0, aa.length()); |
87 |
jsr166 |
1.1 |
try { |
88 |
jsr166 |
1.3 |
aa.get(0); |
89 |
jsr166 |
1.1 |
shouldThrow(); |
90 |
jsr166 |
1.3 |
} 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 |
jsr166 |
1.1 |
try { |
100 |
jsr166 |
1.3 |
aa.get(0); |
101 |
jsr166 |
1.1 |
shouldThrow(); |
102 |
jsr166 |
1.3 |
} 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 |
jsr166 |
1.4 |
aa.lazySet(index, 1.0); |
121 |
|
|
shouldThrow(); |
122 |
|
|
} catch (IndexOutOfBoundsException success) {} |
123 |
|
|
try { |
124 |
jsr166 |
1.3 |
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 |
jsr166 |
1.1 |
} |
140 |
|
|
} |
141 |
|
|
|
142 |
|
|
/** |
143 |
|
|
* get returns the last value set at index |
144 |
|
|
*/ |
145 |
|
|
public void testGetSet() { |
146 |
jsr166 |
1.3 |
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 |
jsr166 |
1.1 |
aa.set(i, -3.0); |
152 |
jsr166 |
1.3 |
assertBitEquals(-3.0, aa.get(i)); |
153 |
jsr166 |
1.1 |
} |
154 |
|
|
} |
155 |
|
|
|
156 |
|
|
/** |
157 |
|
|
* get returns the last value lazySet at index by same thread |
158 |
|
|
*/ |
159 |
|
|
public void testGetLazySet() { |
160 |
jsr166 |
1.3 |
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 |
jsr166 |
1.1 |
aa.lazySet(i, -3.0); |
166 |
jsr166 |
1.3 |
assertBitEquals(-3.0, aa.get(i)); |
167 |
jsr166 |
1.1 |
} |
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 |
jsr166 |
1.3 |
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 |
jsr166 |
1.1 |
} |
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 |
jsr166 |
1.5 |
a.set(0, 1.0); |
196 |
jsr166 |
1.3 |
Thread t = newStartedThread(new CheckedRunnable() { |
197 |
jsr166 |
1.1 |
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 |
jsr166 |
1.8 |
awaitTermination(t); |
204 |
jsr166 |
1.3 |
assertBitEquals(3.0, a.get(0)); |
205 |
jsr166 |
1.1 |
} |
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 |
jsr166 |
1.3 |
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 |
jsr166 |
1.1 |
} |
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 |
jsr166 |
1.3 |
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 |
jsr166 |
1.1 |
} |
240 |
|
|
} |
241 |
|
|
|
242 |
|
|
/** |
243 |
|
|
* getAndAdd returns previous value and adds given value |
244 |
|
|
*/ |
245 |
|
|
public void testGetAndAdd() { |
246 |
|
|
AtomicDoubleArray aa = new AtomicDoubleArray(SIZE); |
247 |
jsr166 |
1.3 |
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 |
jsr166 |
1.1 |
} |
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 |
jsr166 |
1.3 |
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 |
jsr166 |
1.1 |
} |
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 |
jsr166 |
1.3 |
for (int i = 0; i < aa.length(); i++) { |
286 |
jsr166 |
1.1 |
double v = aa.get(i); |
287 |
|
|
assertTrue(v >= 0); |
288 |
|
|
if (v != 0) { |
289 |
|
|
done = false; |
290 |
jsr166 |
1.3 |
if (aa.compareAndSet(i, v, v - 1.0)) |
291 |
jsr166 |
1.1 |
++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 |
jsr166 |
1.3 |
for (int i = 0; i < SIZE; i++) |
307 |
|
|
aa.set(i, (double)COUNTDOWN); |
308 |
jsr166 |
1.1 |
Counter c1 = new Counter(aa); |
309 |
|
|
Counter c2 = new Counter(aa); |
310 |
jsr166 |
1.9 |
Thread t1 = newStartedThread(c1); |
311 |
|
|
Thread t2 = newStartedThread(c2); |
312 |
|
|
awaitTermination(t1); |
313 |
|
|
awaitTermination(t2); |
314 |
jsr166 |
1.1 |
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 |
jsr166 |
1.3 |
for (int i = 0; i < SIZE; i++) |
323 |
|
|
x.set(i, (double)-i); |
324 |
jsr166 |
1.1 |
AtomicDoubleArray y = serialClone(x); |
325 |
|
|
assertTrue(x != y); |
326 |
|
|
assertEquals(x.length(), y.length()); |
327 |
jsr166 |
1.3 |
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 |
jsr166 |
1.10 |
assertEquals(a.length(), b.length()); |
335 |
jsr166 |
1.3 |
for (int i = 0; i < VALUES.length; i++) |
336 |
|
|
assertBitEquals(a.get(i), b.get(i)); |
337 |
jsr166 |
1.1 |
} |
338 |
|
|
|
339 |
|
|
/** |
340 |
jsr166 |
1.12 |
* toString returns current value |
341 |
jsr166 |
1.1 |
*/ |
342 |
|
|
public void testToString() { |
343 |
|
|
AtomicDoubleArray aa = new AtomicDoubleArray(VALUES); |
344 |
|
|
assertEquals(Arrays.toString(VALUES), aa.toString()); |
345 |
jsr166 |
1.11 |
assertEquals("[]", new AtomicDoubleArray(0).toString()); |
346 |
|
|
assertEquals("[]", new AtomicDoubleArray(new double[0]).toString()); |
347 |
jsr166 |
1.1 |
} |
348 |
|
|
|
349 |
jsr166 |
1.3 |
/** |
350 |
|
|
* compareAndSet treats +0.0 and -0.0 as distinct values |
351 |
|
|
*/ |
352 |
|
|
public void testDistinctZeros() { |
353 |
|
|
AtomicDoubleArray aa = new AtomicDoubleArray(SIZE); |
354 |
|
|
for (int i : new int[] { 0, SIZE - 1}) { |
355 |
|
|
assertFalse(aa.compareAndSet(i, -0.0, 7.0)); |
356 |
|
|
assertFalse(aa.weakCompareAndSet(i, -0.0, 7.0)); |
357 |
|
|
assertBitEquals(+0.0, aa.get(i)); |
358 |
|
|
assertTrue(aa.compareAndSet(i, +0.0, -0.0)); |
359 |
|
|
assertBitEquals(-0.0, aa.get(i)); |
360 |
|
|
assertFalse(aa.compareAndSet(i, +0.0, 7.0)); |
361 |
|
|
assertFalse(aa.weakCompareAndSet(i, +0.0, 7.0)); |
362 |
|
|
assertBitEquals(-0.0, aa.get(i)); |
363 |
|
|
} |
364 |
|
|
} |
365 |
|
|
|
366 |
jsr166 |
1.1 |
} |