37 |
|
Double.NaN, |
38 |
|
}; |
39 |
|
|
40 |
+ |
/** The notion of equality used by AtomicDoubleArray */ |
41 |
+ |
boolean bitEquals(double x, double y) { |
42 |
+ |
return Double.doubleToRawLongBits(x) == Double.doubleToRawLongBits(y); |
43 |
+ |
} |
44 |
+ |
|
45 |
+ |
void assertBitEquals(double x, double y) { |
46 |
+ |
assertEquals(Double.doubleToRawLongBits(x), |
47 |
+ |
Double.doubleToRawLongBits(y)); |
48 |
+ |
} |
49 |
+ |
|
50 |
|
/** |
51 |
|
* constructor creates array of given size with all elements zero |
52 |
|
*/ |
53 |
|
public void testConstructor() { |
54 |
|
AtomicDoubleArray aa = new AtomicDoubleArray(SIZE); |
55 |
< |
for (int i = 0; i < SIZE; ++i) |
56 |
< |
assertEquals(0.0, aa.get(i)); |
55 |
> |
for (int i = 0; i < SIZE; i++) |
56 |
> |
assertBitEquals(0.0, aa.get(i)); |
57 |
|
} |
58 |
|
|
59 |
|
/** |
73 |
|
public void testConstructor2() { |
74 |
|
AtomicDoubleArray aa = new AtomicDoubleArray(VALUES); |
75 |
|
assertEquals(VALUES.length, aa.length()); |
76 |
< |
for (int i = 0; i < VALUES.length; ++i) |
77 |
< |
assertEquals(a[i], aa.get(i)); |
76 |
> |
for (int i = 0; i < VALUES.length; i++) |
77 |
> |
assertBitEquals(VALUES[i], aa.get(i)); |
78 |
|
} |
79 |
|
|
80 |
|
/** |
81 |
< |
* get and set for out of bound indices throw IndexOutOfBoundsException |
81 |
> |
* constructor with empty array has size 0 and contains no elements |
82 |
|
*/ |
83 |
< |
public void testIndexing() { |
84 |
< |
AtomicDoubleArray aa = new AtomicDoubleArray(SIZE); |
83 |
> |
public void testConstructorEmptyArray() { |
84 |
> |
AtomicDoubleArray aa = new AtomicDoubleArray(new double[0]); |
85 |
> |
assertEquals(0, aa.length()); |
86 |
|
try { |
87 |
< |
aa.get(SIZE); |
87 |
> |
aa.get(0); |
88 |
|
shouldThrow(); |
89 |
< |
} catch (IndexOutOfBoundsException success) { |
90 |
< |
} |
91 |
< |
try { |
92 |
< |
aa.get(-1); |
93 |
< |
shouldThrow(); |
94 |
< |
} catch (IndexOutOfBoundsException success) { |
95 |
< |
} |
96 |
< |
try { |
97 |
< |
aa.set(SIZE, 0); |
87 |
< |
shouldThrow(); |
88 |
< |
} catch (IndexOutOfBoundsException success) { |
89 |
< |
} |
89 |
> |
} catch (IndexOutOfBoundsException success) {} |
90 |
> |
} |
91 |
> |
|
92 |
> |
/** |
93 |
> |
* constructor with length zero has size 0 and contains no elements |
94 |
> |
*/ |
95 |
> |
public void testConstructorZeroLength() { |
96 |
> |
AtomicDoubleArray aa = new AtomicDoubleArray(0); |
97 |
> |
assertEquals(0, aa.length()); |
98 |
|
try { |
99 |
< |
aa.set(-1, 0.0); |
99 |
> |
aa.get(0); |
100 |
|
shouldThrow(); |
101 |
< |
} catch (IndexOutOfBoundsException success) { |
101 |
> |
} catch (IndexOutOfBoundsException success) {} |
102 |
> |
} |
103 |
> |
|
104 |
> |
/** |
105 |
> |
* get and set for out of bound indices throw IndexOutOfBoundsException |
106 |
> |
*/ |
107 |
> |
public void testIndexing() { |
108 |
> |
AtomicDoubleArray aa = new AtomicDoubleArray(SIZE); |
109 |
> |
for (int index : new int[] { -1, SIZE }) { |
110 |
> |
try { |
111 |
> |
aa.get(index); |
112 |
> |
shouldThrow(); |
113 |
> |
} catch (IndexOutOfBoundsException success) {} |
114 |
> |
try { |
115 |
> |
aa.set(index, 1.0); |
116 |
> |
shouldThrow(); |
117 |
> |
} catch (IndexOutOfBoundsException success) {} |
118 |
> |
try { |
119 |
> |
aa.compareAndSet(index, 1.0, 2.0); |
120 |
> |
shouldThrow(); |
121 |
> |
} catch (IndexOutOfBoundsException success) {} |
122 |
> |
try { |
123 |
> |
aa.weakCompareAndSet(index, 1.0, 2.0); |
124 |
> |
shouldThrow(); |
125 |
> |
} catch (IndexOutOfBoundsException success) {} |
126 |
> |
try { |
127 |
> |
aa.getAndAdd(index, 1.0); |
128 |
> |
shouldThrow(); |
129 |
> |
} catch (IndexOutOfBoundsException success) {} |
130 |
> |
try { |
131 |
> |
aa.addAndGet(index, 1.0); |
132 |
> |
shouldThrow(); |
133 |
> |
} catch (IndexOutOfBoundsException success) {} |
134 |
|
} |
135 |
|
} |
136 |
|
|
138 |
|
* get returns the last value set at index |
139 |
|
*/ |
140 |
|
public void testGetSet() { |
141 |
< |
AtomicDoubleArray aa = new AtomicDoubleArray(SIZE); |
142 |
< |
for (int i = 0; i < SIZE; ++i) { |
143 |
< |
aa.set(i, 1.0); |
144 |
< |
assertEquals(1.0, aa.get(i)); |
145 |
< |
aa.set(i, 2.0); |
106 |
< |
assertEquals(2.0, aa.get(i)); |
141 |
> |
AtomicDoubleArray aa = new AtomicDoubleArray(VALUES.length); |
142 |
> |
for (int i = 0; i < VALUES.length; i++) { |
143 |
> |
assertBitEquals(0.0, aa.get(i)); |
144 |
> |
aa.set(i, VALUES[i]); |
145 |
> |
assertBitEquals(VALUES[i], aa.get(i)); |
146 |
|
aa.set(i, -3.0); |
147 |
< |
assertEquals(-3.0, aa.get(i)); |
147 |
> |
assertBitEquals(-3.0, aa.get(i)); |
148 |
|
} |
149 |
|
} |
150 |
|
|
152 |
|
* get returns the last value lazySet at index by same thread |
153 |
|
*/ |
154 |
|
public void testGetLazySet() { |
155 |
< |
AtomicDoubleArray aa = new AtomicDoubleArray(SIZE); |
156 |
< |
for (int i = 0; i < SIZE; ++i) { |
157 |
< |
aa.lazySet(i, 1.0); |
158 |
< |
assertEquals(1.0, aa.get(i)); |
159 |
< |
aa.lazySet(i, 2.0); |
121 |
< |
assertEquals(2.0, aa.get(i)); |
155 |
> |
AtomicDoubleArray aa = new AtomicDoubleArray(VALUES.length); |
156 |
> |
for (int i = 0; i < VALUES.length; i++) { |
157 |
> |
assertBitEquals(0.0, aa.get(i)); |
158 |
> |
aa.lazySet(i, VALUES[i]); |
159 |
> |
assertBitEquals(VALUES[i], aa.get(i)); |
160 |
|
aa.lazySet(i, -3.0); |
161 |
< |
assertEquals(-3.0, aa.get(i)); |
161 |
> |
assertBitEquals(-3.0, aa.get(i)); |
162 |
|
} |
163 |
|
} |
164 |
|
|
167 |
|
*/ |
168 |
|
public void testCompareAndSet() { |
169 |
|
AtomicDoubleArray aa = new AtomicDoubleArray(SIZE); |
170 |
< |
for (int i = 0; i < SIZE; ++i) { |
171 |
< |
aa.set(i, 1); |
172 |
< |
assertTrue(aa.compareAndSet(i, 1.0, 2.0)); |
173 |
< |
assertTrue(aa.compareAndSet(i, 2.0, -4.0)); |
174 |
< |
assertEquals(-4.0, aa.get(i)); |
175 |
< |
assertFalse(aa.compareAndSet(i, -5.0, 7.0)); |
176 |
< |
assertEquals(-4.0, aa.get(i)); |
177 |
< |
assertTrue(aa.compareAndSet(i, -4.0, 7.0)); |
178 |
< |
assertEquals(7.0, aa.get(i)); |
170 |
> |
for (int i : new int[] { 0, SIZE - 1}) { |
171 |
> |
double prev = 0.0; |
172 |
> |
double unused = Math.E + Math.PI; |
173 |
> |
for (double x : VALUES) { |
174 |
> |
assertBitEquals(prev, aa.get(i)); |
175 |
> |
assertFalse(aa.compareAndSet(i, unused, x)); |
176 |
> |
assertBitEquals(prev, aa.get(i)); |
177 |
> |
assertTrue(aa.compareAndSet(i, prev, x)); |
178 |
> |
assertBitEquals(x, aa.get(i)); |
179 |
> |
prev = x; |
180 |
> |
} |
181 |
|
} |
182 |
|
} |
183 |
|
|
188 |
|
public void testCompareAndSetInMultipleThreads() throws InterruptedException { |
189 |
|
final AtomicDoubleArray a = new AtomicDoubleArray(1); |
190 |
|
a.set(0, 1); |
191 |
< |
Thread t = new Thread(new CheckedRunnable() { |
191 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
192 |
|
public void realRun() { |
193 |
|
while (!a.compareAndSet(0, 2.0, 3.0)) |
194 |
|
Thread.yield(); |
195 |
|
}}); |
196 |
|
|
157 |
– |
t.start(); |
197 |
|
assertTrue(a.compareAndSet(0, 1.0, 2.0)); |
198 |
|
t.join(LONG_DELAY_MS); |
199 |
|
assertFalse(t.isAlive()); |
200 |
< |
assertEquals(3.0, a.get(0)); |
200 |
> |
assertBitEquals(3.0, a.get(0)); |
201 |
|
} |
202 |
|
|
203 |
|
/** |
206 |
|
*/ |
207 |
|
public void testWeakCompareAndSet() { |
208 |
|
AtomicDoubleArray aa = new AtomicDoubleArray(SIZE); |
209 |
< |
for (int i = 0; i < SIZE; ++i) { |
210 |
< |
aa.set(i, 1.0); |
211 |
< |
while (!aa.weakCompareAndSet(i, 1.0, 2.0)); |
212 |
< |
while (!aa.weakCompareAndSet(i, 2.0, -4.0)); |
213 |
< |
assertEquals(-4.0, aa.get(i)); |
214 |
< |
while (!aa.weakCompareAndSet(i, -4.0, 7.0)); |
215 |
< |
assertEquals(7.0, aa.get(i)); |
209 |
> |
for (int i : new int[] { 0, SIZE - 1}) { |
210 |
> |
double prev = 0.0; |
211 |
> |
double unused = Math.E + Math.PI; |
212 |
> |
for (double x : VALUES) { |
213 |
> |
assertBitEquals(prev, aa.get(i)); |
214 |
> |
assertFalse(aa.weakCompareAndSet(i, unused, x)); |
215 |
> |
assertBitEquals(prev, aa.get(i)); |
216 |
> |
while (!aa.weakCompareAndSet(i, prev, x)) |
217 |
> |
; |
218 |
> |
assertBitEquals(x, aa.get(i)); |
219 |
> |
prev = x; |
220 |
> |
} |
221 |
|
} |
222 |
|
} |
223 |
|
|
226 |
|
*/ |
227 |
|
public void testGetAndSet() { |
228 |
|
AtomicDoubleArray aa = new AtomicDoubleArray(SIZE); |
229 |
< |
for (int i = 0; i < SIZE; ++i) { |
230 |
< |
aa.set(i, 1.0); |
231 |
< |
assertEquals(1.0, aa.getAndSet(i, 0.0)); |
232 |
< |
assertEquals(0.0, aa.getAndSet(i, -10.0)); |
233 |
< |
assertEquals(-10.0, aa.getAndSet(i, 1.0)); |
229 |
> |
for (int i : new int[] { 0, SIZE - 1}) { |
230 |
> |
double prev = 0.0; |
231 |
> |
double unused = Math.E + Math.PI; |
232 |
> |
for (double x : VALUES) { |
233 |
> |
assertBitEquals(prev, aa.getAndSet(i, x)); |
234 |
> |
prev = x; |
235 |
> |
} |
236 |
|
} |
237 |
|
} |
238 |
|
|
241 |
|
*/ |
242 |
|
public void testGetAndAdd() { |
243 |
|
AtomicDoubleArray aa = new AtomicDoubleArray(SIZE); |
244 |
< |
for (int i = 0; i < SIZE; ++i) { |
245 |
< |
aa.set(i, 1.0); |
246 |
< |
assertEquals(1.0, aa.getAndAdd(i, 2.0)); |
247 |
< |
assertEquals(3.0, aa.get(i)); |
248 |
< |
assertEquals(3.0, aa.getAndAdd(i, -4.0)); |
249 |
< |
assertEquals(-1.0, aa.get(i)); |
244 |
> |
for (int i : new int[] { 0, SIZE - 1}) { |
245 |
> |
for (double x : VALUES) { |
246 |
> |
for (double y : VALUES) { |
247 |
> |
aa.set(i, x); |
248 |
> |
double z = aa.getAndAdd(i, y); |
249 |
> |
assertBitEquals(x, z); |
250 |
> |
assertBitEquals(x + y, aa.get(i)); |
251 |
> |
} |
252 |
> |
} |
253 |
|
} |
254 |
|
} |
255 |
|
|
258 |
|
*/ |
259 |
|
public void testAddAndGet() { |
260 |
|
AtomicDoubleArray aa = new AtomicDoubleArray(SIZE); |
261 |
< |
for (int i = 0; i < SIZE; ++i) { |
262 |
< |
aa.set(i, 1.0); |
263 |
< |
assertEquals(3.0, aa.addAndGet(i, 2.0)); |
264 |
< |
assertEquals(3.0, aa.get(i)); |
265 |
< |
assertEquals(-1.0, aa.addAndGet(i, -4.0)); |
266 |
< |
assertEquals(-1.0, aa.get(i)); |
261 |
> |
for (int i : new int[] { 0, SIZE - 1}) { |
262 |
> |
for (double x : VALUES) { |
263 |
> |
for (double y : VALUES) { |
264 |
> |
aa.set(i, x); |
265 |
> |
double z = aa.addAndGet(i, y); |
266 |
> |
assertBitEquals(x + y, z); |
267 |
> |
assertBitEquals(x + y, aa.get(i)); |
268 |
> |
} |
269 |
> |
} |
270 |
|
} |
271 |
|
} |
272 |
|
|
279 |
|
public void realRun() { |
280 |
|
for (;;) { |
281 |
|
boolean done = true; |
282 |
< |
for (int i = 0; i < aa.length(); ++i) { |
282 |
> |
for (int i = 0; i < aa.length(); i++) { |
283 |
|
double v = aa.get(i); |
284 |
|
assertTrue(v >= 0); |
285 |
|
if (v != 0) { |
286 |
|
done = false; |
287 |
< |
if (aa.compareAndSet(i, v, v-1)) |
287 |
> |
if (aa.compareAndSet(i, v, v - 1.0)) |
288 |
|
++counts; |
289 |
|
} |
290 |
|
} |
300 |
|
*/ |
301 |
|
public void testCountingInMultipleThreads() throws InterruptedException { |
302 |
|
final AtomicDoubleArray aa = new AtomicDoubleArray(SIZE); |
303 |
< |
for (int i = 0; i < SIZE; ++i) |
304 |
< |
aa.set(i, COUNTDOWN); |
303 |
> |
for (int i = 0; i < SIZE; i++) |
304 |
> |
aa.set(i, (double)COUNTDOWN); |
305 |
|
Counter c1 = new Counter(aa); |
306 |
|
Counter c2 = new Counter(aa); |
307 |
|
Thread t1 = new Thread(c1); |
318 |
|
*/ |
319 |
|
public void testSerialization() throws Exception { |
320 |
|
AtomicDoubleArray x = new AtomicDoubleArray(SIZE); |
321 |
< |
for (int i = 0; i < SIZE; ++i) |
322 |
< |
x.set(i, -i); |
321 |
> |
for (int i = 0; i < SIZE; i++) |
322 |
> |
x.set(i, (double)-i); |
323 |
|
AtomicDoubleArray y = serialClone(x); |
324 |
|
assertTrue(x != y); |
325 |
|
assertEquals(x.length(), y.length()); |
326 |
< |
for (int i = 0; i < SIZE; ++i) { |
327 |
< |
assertEquals(x.get(i), y.get(i)); |
328 |
< |
} |
326 |
> |
for (int i = 0; i < SIZE; i++) |
327 |
> |
assertBitEquals(x.get(i), y.get(i)); |
328 |
> |
|
329 |
> |
AtomicDoubleArray a = new AtomicDoubleArray(VALUES); |
330 |
> |
AtomicDoubleArray b = serialClone(a); |
331 |
> |
assertFalse(a.equals(b)); |
332 |
> |
assertFalse(b.equals(a)); |
333 |
> |
for (int i = 0; i < VALUES.length; i++) |
334 |
> |
assertBitEquals(a.get(i), b.get(i)); |
335 |
|
} |
336 |
|
|
337 |
|
/** |
342 |
|
assertEquals(Arrays.toString(VALUES), aa.toString()); |
343 |
|
} |
344 |
|
|
345 |
+ |
/** |
346 |
+ |
* compareAndSet treats +0.0 and -0.0 as distinct values |
347 |
+ |
*/ |
348 |
+ |
public void testDistinctZeros() { |
349 |
+ |
AtomicDoubleArray aa = new AtomicDoubleArray(SIZE); |
350 |
+ |
for (int i : new int[] { 0, SIZE - 1}) { |
351 |
+ |
assertFalse(aa.compareAndSet(i, -0.0, 7.0)); |
352 |
+ |
assertFalse(aa.weakCompareAndSet(i, -0.0, 7.0)); |
353 |
+ |
assertBitEquals(+0.0, aa.get(i)); |
354 |
+ |
assertTrue(aa.compareAndSet(i, +0.0, -0.0)); |
355 |
+ |
assertBitEquals(-0.0, aa.get(i)); |
356 |
+ |
assertFalse(aa.compareAndSet(i, +0.0, 7.0)); |
357 |
+ |
assertFalse(aa.weakCompareAndSet(i, +0.0, 7.0)); |
358 |
+ |
assertBitEquals(-0.0, aa.get(i)); |
359 |
+ |
} |
360 |
+ |
} |
361 |
+ |
|
362 |
|
} |