ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck-jsr166e/AtomicDoubleArrayTest.java
Revision: 1.6
Committed: Wed Aug 10 07:23:43 2011 UTC (12 years, 10 months ago) by jsr166
Branch: MAIN
Changes since 1.5: +0 -1 lines
Log Message:
remove unused variable

File Contents

# Content
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 };
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 assertBitEquals(0.0, aa.get(i));
57 }
58
59 /**
60 * constructor with null array throws NPE
61 */
62 public void testConstructor2NPE() {
63 try {
64 double[] a = null;
65 AtomicDoubleArray aa = new AtomicDoubleArray(a);
66 shouldThrow();
67 } catch (NullPointerException success) {}
68 }
69
70 /**
71 * constructor with array is of same size and has all elements
72 */
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 assertBitEquals(VALUES[i], aa.get(i));
78 }
79
80 /**
81 * constructor with empty array has size 0 and contains no elements
82 */
83 public void testConstructorEmptyArray() {
84 AtomicDoubleArray aa = new AtomicDoubleArray(new double[0]);
85 assertEquals(0, aa.length());
86 try {
87 aa.get(0);
88 shouldThrow();
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.get(0);
100 shouldThrow();
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.lazySet(index, 1.0);
120 shouldThrow();
121 } catch (IndexOutOfBoundsException success) {}
122 try {
123 aa.compareAndSet(index, 1.0, 2.0);
124 shouldThrow();
125 } catch (IndexOutOfBoundsException success) {}
126 try {
127 aa.weakCompareAndSet(index, 1.0, 2.0);
128 shouldThrow();
129 } catch (IndexOutOfBoundsException success) {}
130 try {
131 aa.getAndAdd(index, 1.0);
132 shouldThrow();
133 } catch (IndexOutOfBoundsException success) {}
134 try {
135 aa.addAndGet(index, 1.0);
136 shouldThrow();
137 } catch (IndexOutOfBoundsException success) {}
138 }
139 }
140
141 /**
142 * get returns the last value set at index
143 */
144 public void testGetSet() {
145 AtomicDoubleArray aa = new AtomicDoubleArray(VALUES.length);
146 for (int i = 0; i < VALUES.length; i++) {
147 assertBitEquals(0.0, aa.get(i));
148 aa.set(i, VALUES[i]);
149 assertBitEquals(VALUES[i], aa.get(i));
150 aa.set(i, -3.0);
151 assertBitEquals(-3.0, aa.get(i));
152 }
153 }
154
155 /**
156 * get returns the last value lazySet at index by same thread
157 */
158 public void testGetLazySet() {
159 AtomicDoubleArray aa = new AtomicDoubleArray(VALUES.length);
160 for (int i = 0; i < VALUES.length; i++) {
161 assertBitEquals(0.0, aa.get(i));
162 aa.lazySet(i, VALUES[i]);
163 assertBitEquals(VALUES[i], aa.get(i));
164 aa.lazySet(i, -3.0);
165 assertBitEquals(-3.0, aa.get(i));
166 }
167 }
168
169 /**
170 * compareAndSet succeeds in changing value if equal to expected else fails
171 */
172 public void testCompareAndSet() {
173 AtomicDoubleArray aa = new AtomicDoubleArray(SIZE);
174 for (int i : new int[] { 0, SIZE - 1}) {
175 double prev = 0.0;
176 double unused = Math.E + Math.PI;
177 for (double x : VALUES) {
178 assertBitEquals(prev, aa.get(i));
179 assertFalse(aa.compareAndSet(i, unused, x));
180 assertBitEquals(prev, aa.get(i));
181 assertTrue(aa.compareAndSet(i, prev, x));
182 assertBitEquals(x, aa.get(i));
183 prev = x;
184 }
185 }
186 }
187
188 /**
189 * compareAndSet in one thread enables another waiting for value
190 * to succeed
191 */
192 public void testCompareAndSetInMultipleThreads() throws InterruptedException {
193 final AtomicDoubleArray a = new AtomicDoubleArray(1);
194 a.set(0, 1.0);
195 Thread t = newStartedThread(new CheckedRunnable() {
196 public void realRun() {
197 while (!a.compareAndSet(0, 2.0, 3.0))
198 Thread.yield();
199 }});
200
201 assertTrue(a.compareAndSet(0, 1.0, 2.0));
202 t.join(LONG_DELAY_MS);
203 assertFalse(t.isAlive());
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 = new Thread(c1);
311 Thread t2 = new Thread(c2);
312 t1.start();
313 t2.start();
314 t1.join();
315 t2.join();
316 assertEquals(c1.counts+c2.counts, SIZE * COUNTDOWN);
317 }
318
319 /**
320 * a deserialized serialized array holds same values
321 */
322 public void testSerialization() throws Exception {
323 AtomicDoubleArray x = new AtomicDoubleArray(SIZE);
324 for (int i = 0; i < SIZE; i++)
325 x.set(i, (double)-i);
326 AtomicDoubleArray y = serialClone(x);
327 assertTrue(x != y);
328 assertEquals(x.length(), y.length());
329 for (int i = 0; i < SIZE; i++)
330 assertBitEquals(x.get(i), y.get(i));
331
332 AtomicDoubleArray a = new AtomicDoubleArray(VALUES);
333 AtomicDoubleArray b = serialClone(a);
334 assertFalse(a.equals(b));
335 assertFalse(b.equals(a));
336 for (int i = 0; i < VALUES.length; i++)
337 assertBitEquals(a.get(i), b.get(i));
338 }
339
340 /**
341 * toString returns current value.
342 */
343 public void testToString() {
344 AtomicDoubleArray aa = new AtomicDoubleArray(VALUES);
345 assertEquals(Arrays.toString(VALUES), aa.toString());
346 }
347
348 /**
349 * compareAndSet treats +0.0 and -0.0 as distinct values
350 */
351 public void testDistinctZeros() {
352 AtomicDoubleArray aa = new AtomicDoubleArray(SIZE);
353 for (int i : new int[] { 0, SIZE - 1}) {
354 assertFalse(aa.compareAndSet(i, -0.0, 7.0));
355 assertFalse(aa.weakCompareAndSet(i, -0.0, 7.0));
356 assertBitEquals(+0.0, aa.get(i));
357 assertTrue(aa.compareAndSet(i, +0.0, -0.0));
358 assertBitEquals(-0.0, aa.get(i));
359 assertFalse(aa.compareAndSet(i, +0.0, 7.0));
360 assertFalse(aa.weakCompareAndSet(i, +0.0, 7.0));
361 assertBitEquals(-0.0, aa.get(i));
362 }
363 }
364
365 }