ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AtomicReferenceArrayTest.java
(Generate patch)

Comparing jsr166/src/test/tck/AtomicReferenceArrayTest.java (file contents):
Revision 1.16 by jsr166, Tue Nov 17 06:58:50 2009 UTC vs.
Revision 1.34 by jsr166, Fri Jun 17 01:38:28 2016 UTC

# Line 1 | Line 1
1   /*
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
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   * Other contributors include Andrew Wright, Jeffrey Hayes,
6   * Pat Fisher, Mike Judd.
7   */
8  
9 < import junit.framework.*;
10 < import java.util.concurrent.atomic.*;
11 < import java.io.*;
12 < import java.util.*;
9 > import java.util.Arrays;
10 > import java.util.concurrent.atomic.AtomicReferenceArray;
11 >
12 > import junit.framework.Test;
13 > import junit.framework.TestSuite;
14  
15   public class AtomicReferenceArrayTest extends JSR166TestCase {
16 <    public static void main (String[] args) {
17 <        junit.textui.TestRunner.run (suite());
16 >    public static void main(String[] args) {
17 >        main(suite(), args);
18      }
19      public static Test suite() {
20          return new TestSuite(AtomicReferenceArrayTest.class);
# Line 23 | Line 24 | public class AtomicReferenceArrayTest ex
24       * constructor creates array of given size with all elements null
25       */
26      public void testConstructor() {
27 <        AtomicReferenceArray<Integer> ai = new AtomicReferenceArray<Integer>(SIZE);
28 <        for (int i = 0; i < SIZE; ++i) {
29 <            assertNull(ai.get(i));
27 >        AtomicReferenceArray<Integer> aa = new AtomicReferenceArray<Integer>(SIZE);
28 >        for (int i = 0; i < SIZE; i++) {
29 >            assertNull(aa.get(i));
30          }
31      }
32  
# Line 35 | Line 36 | public class AtomicReferenceArrayTest ex
36      public void testConstructor2NPE() {
37          try {
38              Integer[] a = null;
39 <            AtomicReferenceArray<Integer> ai = new AtomicReferenceArray<Integer>(a);
39 >            new AtomicReferenceArray<Integer>(a);
40              shouldThrow();
41          } catch (NullPointerException success) {}
42      }
# Line 44 | Line 45 | public class AtomicReferenceArrayTest ex
45       * constructor with array is of same size and has all elements
46       */
47      public void testConstructor2() {
48 <        Integer[] a = { two, one, three, four, seven};
49 <        AtomicReferenceArray<Integer> ai = new AtomicReferenceArray<Integer>(a);
50 <        assertEquals(a.length, ai.length());
51 <        for (int i = 0; i < a.length; ++i)
52 <            assertEquals(a[i], ai.get(i));
48 >        Integer[] a = { two, one, three, four, seven };
49 >        AtomicReferenceArray<Integer> aa = new AtomicReferenceArray<Integer>(a);
50 >        assertEquals(a.length, aa.length());
51 >        for (int i = 0; i < a.length; i++)
52 >            assertEquals(a[i], aa.get(i));
53      }
54  
55 +    /**
56 +     * Initialize AtomicReferenceArray<Class> with SubClass[]
57 +     */
58 +    public void testConstructorSubClassArray() {
59 +        Integer[] a = { two, one, three, four, seven };
60 +        AtomicReferenceArray<Number> aa = new AtomicReferenceArray<Number>(a);
61 +        assertEquals(a.length, aa.length());
62 +        for (int i = 0; i < a.length; i++) {
63 +            assertSame(a[i], aa.get(i));
64 +            Long x = Long.valueOf(i);
65 +            aa.set(i, x);
66 +            assertSame(x, aa.get(i));
67 +        }
68 +    }
69  
70      /**
71       * get and set for out of bound indices throw IndexOutOfBoundsException
72       */
73      public void testIndexing() {
74 <        AtomicReferenceArray<Integer> ai = new AtomicReferenceArray<Integer>(SIZE);
75 <        try {
76 <            ai.get(SIZE);
77 <            shouldThrow();
78 <        } catch (IndexOutOfBoundsException success) {
79 <        }
80 <        try {
81 <            ai.get(-1);
82 <            shouldThrow();
83 <        } catch (IndexOutOfBoundsException success) {
84 <        }
85 <        try {
86 <            ai.set(SIZE, null);
87 <            shouldThrow();
88 <        } catch (IndexOutOfBoundsException success) {
89 <        }
90 <        try {
91 <            ai.set(-1, null);
92 <            shouldThrow();
93 <        } catch (IndexOutOfBoundsException success) {
74 >        AtomicReferenceArray<Integer> aa = new AtomicReferenceArray<Integer>(SIZE);
75 >        for (int index : new int[] { -1, SIZE }) {
76 >            try {
77 >                aa.get(index);
78 >                shouldThrow();
79 >            } catch (IndexOutOfBoundsException success) {}
80 >            try {
81 >                aa.set(index, null);
82 >                shouldThrow();
83 >            } catch (IndexOutOfBoundsException success) {}
84 >            try {
85 >                aa.lazySet(index, null);
86 >                shouldThrow();
87 >            } catch (IndexOutOfBoundsException success) {}
88 >            try {
89 >                aa.compareAndSet(index, null, null);
90 >                shouldThrow();
91 >            } catch (IndexOutOfBoundsException success) {}
92 >            try {
93 >                aa.weakCompareAndSet(index, null, null);
94 >                shouldThrow();
95 >            } catch (IndexOutOfBoundsException success) {}
96 >            try {
97 >                aa.getPlain(index);
98 >                shouldThrow();
99 >            } catch (IndexOutOfBoundsException success) {}
100 >            try {
101 >                aa.getOpaque(index);
102 >                shouldThrow();
103 >            } catch (IndexOutOfBoundsException success) {}
104 >            try {
105 >                aa.getAcquire(index);
106 >                shouldThrow();
107 >            } catch (IndexOutOfBoundsException success) {}
108 >            try {
109 >                aa.setPlain(index, null);
110 >                shouldThrow();
111 >            } catch (IndexOutOfBoundsException success) {}
112 >            try {
113 >                aa.setOpaque(index, null);
114 >                shouldThrow();
115 >            } catch (IndexOutOfBoundsException success) {}
116 >            try {
117 >                aa.setRelease(index, null);
118 >                shouldThrow();
119 >            } catch (IndexOutOfBoundsException success) {}
120 >            try {
121 >                aa.compareAndExchange(index, null, null);
122 >                shouldThrow();
123 >            } catch (IndexOutOfBoundsException success) {}
124 >            try {
125 >                aa.compareAndExchangeAcquire(index, null, null);
126 >                shouldThrow();
127 >            } catch (IndexOutOfBoundsException success) {}
128 >            try {
129 >                aa.compareAndExchangeRelease(index, null, null);
130 >                shouldThrow();
131 >            } catch (IndexOutOfBoundsException success) {}
132 >            try {
133 >                aa.weakCompareAndSetVolatile(index, null, null);
134 >                shouldThrow();
135 >            } catch (IndexOutOfBoundsException success) {}
136 >            try {
137 >                aa.weakCompareAndSetAcquire(index, null, null);
138 >                shouldThrow();
139 >            } catch (IndexOutOfBoundsException success) {}
140 >            try {
141 >                aa.weakCompareAndSetRelease(index, null, null);
142 >                shouldThrow();
143 >            } catch (IndexOutOfBoundsException success) {}
144          }
145      }
146  
# Line 83 | Line 148 | public class AtomicReferenceArrayTest ex
148       * get returns the last value set at index
149       */
150      public void testGetSet() {
151 <        AtomicReferenceArray ai = new AtomicReferenceArray(SIZE);
152 <        for (int i = 0; i < SIZE; ++i) {
153 <            ai.set(i, one);
154 <            assertEquals(one,ai.get(i));
155 <            ai.set(i, two);
156 <            assertEquals(two,ai.get(i));
157 <            ai.set(i, m3);
158 <            assertEquals(m3,ai.get(i));
151 >        AtomicReferenceArray aa = new AtomicReferenceArray(SIZE);
152 >        for (int i = 0; i < SIZE; i++) {
153 >            aa.set(i, one);
154 >            assertSame(one, aa.get(i));
155 >            aa.set(i, two);
156 >            assertSame(two, aa.get(i));
157 >            aa.set(i, m3);
158 >            assertSame(m3, aa.get(i));
159          }
160      }
161  
# Line 98 | Line 163 | public class AtomicReferenceArrayTest ex
163       * get returns the last value lazySet at index by same thread
164       */
165      public void testGetLazySet() {
166 <        AtomicReferenceArray ai = new AtomicReferenceArray(SIZE);
167 <        for (int i = 0; i < SIZE; ++i) {
168 <            ai.lazySet(i, one);
169 <            assertEquals(one,ai.get(i));
170 <            ai.lazySet(i, two);
171 <            assertEquals(two,ai.get(i));
172 <            ai.lazySet(i, m3);
173 <            assertEquals(m3,ai.get(i));
166 >        AtomicReferenceArray aa = new AtomicReferenceArray(SIZE);
167 >        for (int i = 0; i < SIZE; i++) {
168 >            aa.lazySet(i, one);
169 >            assertSame(one, aa.get(i));
170 >            aa.lazySet(i, two);
171 >            assertSame(two, aa.get(i));
172 >            aa.lazySet(i, m3);
173 >            assertSame(m3, aa.get(i));
174          }
175      }
176  
# Line 113 | Line 178 | public class AtomicReferenceArrayTest ex
178       * compareAndSet succeeds in changing value if equal to expected else fails
179       */
180      public void testCompareAndSet() {
181 <        AtomicReferenceArray ai = new AtomicReferenceArray(SIZE);
182 <        for (int i = 0; i < SIZE; ++i) {
183 <            ai.set(i, one);
184 <            assertTrue(ai.compareAndSet(i, one,two));
185 <            assertTrue(ai.compareAndSet(i, two,m4));
186 <            assertEquals(m4,ai.get(i));
187 <            assertFalse(ai.compareAndSet(i, m5,seven));
188 <            assertFalse((seven.equals(ai.get(i))));
189 <            assertTrue(ai.compareAndSet(i, m4,seven));
190 <            assertEquals(seven,ai.get(i));
181 >        AtomicReferenceArray aa = new AtomicReferenceArray(SIZE);
182 >        for (int i = 0; i < SIZE; i++) {
183 >            aa.set(i, one);
184 >            assertTrue(aa.compareAndSet(i, one, two));
185 >            assertTrue(aa.compareAndSet(i, two, m4));
186 >            assertSame(m4, aa.get(i));
187 >            assertFalse(aa.compareAndSet(i, m5, seven));
188 >            assertSame(m4, aa.get(i));
189 >            assertTrue(aa.compareAndSet(i, m4, seven));
190 >            assertSame(seven, aa.get(i));
191          }
192      }
193  
# Line 143 | Line 208 | public class AtomicReferenceArrayTest ex
208          assertTrue(a.compareAndSet(0, one, two));
209          t.join(LONG_DELAY_MS);
210          assertFalse(t.isAlive());
211 <        assertEquals(a.get(0), three);
211 >        assertSame(three, a.get(0));
212      }
213  
214      /**
# Line 151 | Line 216 | public class AtomicReferenceArrayTest ex
216       * to expected
217       */
218      public void testWeakCompareAndSet() {
219 <        AtomicReferenceArray ai = new AtomicReferenceArray(SIZE);
220 <        for (int i = 0; i < SIZE; ++i) {
221 <            ai.set(i, one);
222 <            while (!ai.weakCompareAndSet(i, one,two));
223 <            while (!ai.weakCompareAndSet(i, two,m4));
224 <            assertEquals(m4,ai.get(i));
225 <            while (!ai.weakCompareAndSet(i, m4,seven));
226 <            assertEquals(seven,ai.get(i));
219 >        AtomicReferenceArray aa = new AtomicReferenceArray(SIZE);
220 >        for (int i = 0; i < SIZE; i++) {
221 >            aa.set(i, one);
222 >            do {} while (!aa.weakCompareAndSet(i, one, two));
223 >            do {} while (!aa.weakCompareAndSet(i, two, m4));
224 >            assertSame(m4, aa.get(i));
225 >            do {} while (!aa.weakCompareAndSet(i, m4, seven));
226 >            assertSame(seven, aa.get(i));
227          }
228      }
229  
# Line 166 | Line 231 | public class AtomicReferenceArrayTest ex
231       * getAndSet returns previous value and sets to given value at given index
232       */
233      public void testGetAndSet() {
234 <        AtomicReferenceArray ai = new AtomicReferenceArray(SIZE);
235 <        for (int i = 0; i < SIZE; ++i) {
236 <            ai.set(i, one);
237 <            assertEquals(one,ai.getAndSet(i,zero));
238 <            assertEquals(0,ai.getAndSet(i,m10));
239 <            assertEquals(m10,ai.getAndSet(i,one));
234 >        AtomicReferenceArray aa = new AtomicReferenceArray(SIZE);
235 >        for (int i = 0; i < SIZE; i++) {
236 >            aa.set(i, one);
237 >            assertSame(one, aa.getAndSet(i, zero));
238 >            assertSame(zero, aa.getAndSet(i, m10));
239 >            assertSame(m10, aa.getAndSet(i, one));
240          }
241      }
242  
# Line 179 | Line 244 | public class AtomicReferenceArrayTest ex
244       * a deserialized serialized array holds same values
245       */
246      public void testSerialization() throws Exception {
247 <        AtomicReferenceArray l = new AtomicReferenceArray(SIZE);
248 <        for (int i = 0; i < SIZE; ++i) {
249 <            l.set(i, new Integer(-i));
250 <        }
251 <
252 <        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
253 <        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
254 <        out.writeObject(l);
255 <        out.close();
191 <
192 <        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
193 <        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
194 <        AtomicReferenceArray r = (AtomicReferenceArray) in.readObject();
195 <        assertEquals(l.length(), r.length());
196 <        for (int i = 0; i < SIZE; ++i) {
197 <            assertEquals(r.get(i), l.get(i));
247 >        AtomicReferenceArray x = new AtomicReferenceArray(SIZE);
248 >        for (int i = 0; i < SIZE; i++) {
249 >            x.set(i, new Integer(-i));
250 >        }
251 >        AtomicReferenceArray y = serialClone(x);
252 >        assertNotSame(x, y);
253 >        assertEquals(x.length(), y.length());
254 >        for (int i = 0; i < SIZE; i++) {
255 >            assertEquals(x.get(i), y.get(i));
256          }
257      }
258  
201
259      /**
260       * toString returns current value.
261       */
262      public void testToString() {
263 <        Integer[] a = { two, one, three, four, seven};
264 <        AtomicReferenceArray<Integer> ai = new AtomicReferenceArray<Integer>(a);
265 <        assertEquals(Arrays.toString(a), ai.toString());
263 >        Integer[] a = { two, one, three, four, seven };
264 >        AtomicReferenceArray<Integer> aa = new AtomicReferenceArray<Integer>(a);
265 >        assertEquals(Arrays.toString(a), aa.toString());
266      }
267 +
268 +    // jdk9
269 +
270 +    /**
271 +     * getPlain returns the last value set
272 +     */
273 +    public void testGetPlainSet() {
274 +        AtomicReferenceArray<Integer> aa = new AtomicReferenceArray<Integer>(SIZE);
275 +        for (int i = 0; i < SIZE; i++) {
276 +            aa.set(i, one);
277 +            assertEquals(one, aa.getPlain(i));
278 +            aa.set(i, two);
279 +            assertEquals(two, aa.getPlain(i));
280 +            aa.set(i, m3);
281 +            assertEquals(m3, aa.getPlain(i));
282 +        }
283 +    }
284 +
285 +    /**
286 +     * getOpaque returns the last value set
287 +     */
288 +    public void testGetOpaqueSet() {
289 +        AtomicReferenceArray<Integer> aa = new AtomicReferenceArray<Integer>(SIZE);
290 +        for (int i = 0; i < SIZE; i++) {
291 +            aa.set(i, one);
292 +            assertEquals(one, aa.getOpaque(i));
293 +            aa.set(i, two);
294 +            assertEquals(two, aa.getOpaque(i));
295 +            aa.set(i, m3);
296 +            assertEquals(m3, aa.getOpaque(i));
297 +        }
298 +    }
299 +
300 +    /**
301 +     * getAcquire returns the last value set
302 +     */
303 +    public void testGetAcquireSet() {
304 +        AtomicReferenceArray<Integer> aa = new AtomicReferenceArray<Integer>(SIZE);
305 +        for (int i = 0; i < SIZE; i++) {
306 +            aa.set(i, one);
307 +            assertEquals(one, aa.getAcquire(i));
308 +            aa.set(i, two);
309 +            assertEquals(two, aa.getAcquire(i));
310 +            aa.set(i, m3);
311 +            assertEquals(m3, aa.getAcquire(i));
312 +        }
313 +    }
314 +
315 +    /**
316 +     * get returns the last value setPlain
317 +     */
318 +    public void testGetSetPlain() {
319 +        AtomicReferenceArray<Integer> aa = new AtomicReferenceArray<Integer>(SIZE);
320 +        for (int i = 0; i < SIZE; i++) {
321 +            aa.setPlain(i, one);
322 +            assertEquals(one, aa.get(i));
323 +            aa.setPlain(i, two);
324 +            assertEquals(two, aa.get(i));
325 +            aa.setPlain(i, m3);
326 +            assertEquals(m3, aa.get(i));
327 +        }
328 +    }
329 +
330 +    /**
331 +     * get returns the last value setOpaque
332 +     */
333 +    public void testGetSetOpaque() {
334 +        AtomicReferenceArray<Integer> aa = new AtomicReferenceArray<Integer>(SIZE);
335 +        for (int i = 0; i < SIZE; i++) {
336 +            aa.setOpaque(i, one);
337 +            assertEquals(one, aa.get(i));
338 +            aa.setOpaque(i, two);
339 +            assertEquals(two, aa.get(i));
340 +            aa.setOpaque(i, m3);
341 +            assertEquals(m3, aa.get(i));
342 +        }
343 +    }
344 +
345 +    /**
346 +     * get returns the last value setRelease
347 +     */
348 +    public void testGetSetRelease() {
349 +        AtomicReferenceArray<Integer> aa = new AtomicReferenceArray<Integer>(SIZE);
350 +        for (int i = 0; i < SIZE; i++) {
351 +            aa.setRelease(i, one);
352 +            assertEquals(one, aa.get(i));
353 +            aa.setRelease(i, two);
354 +            assertEquals(two, aa.get(i));
355 +            aa.setRelease(i, m3);
356 +            assertEquals(m3, aa.get(i));
357 +        }
358 +    }
359 +
360 +    /**
361 +     * compareAndExchange succeeds in changing value if equal to
362 +     * expected else fails
363 +     */
364 +    public void testCompareAndExchange() {
365 +        AtomicReferenceArray<Integer> aa = new AtomicReferenceArray<Integer>(SIZE);
366 +        for (int i = 0; i < SIZE; i++) {
367 +            aa.set(i, one);
368 +            assertEquals(one, aa.compareAndExchange(i, one, two));
369 +            assertEquals(two, aa.compareAndExchange(i, two, m4));
370 +            assertEquals(m4, aa.get(i));
371 +            assertEquals(m4, aa.compareAndExchange(i,m5, seven));
372 +            assertEquals(m4, aa.get(i));
373 +            assertEquals(m4, aa.compareAndExchange(i, m4, seven));
374 +            assertEquals(seven, aa.get(i));
375 +        }
376 +    }
377 +
378 +    /**
379 +     * compareAndExchangeAcquire succeeds in changing value if equal to
380 +     * expected else fails
381 +     */
382 +    public void testCompareAndExchangeAcquire() {
383 +        AtomicReferenceArray<Integer> aa = new AtomicReferenceArray<Integer>(SIZE);
384 +        for (int i = 0; i < SIZE; i++) {
385 +            aa.set(i, one);
386 +            assertEquals(one, aa.compareAndExchangeAcquire(i, one, two));
387 +            assertEquals(two, aa.compareAndExchangeAcquire(i, two, m4));
388 +            assertEquals(m4, aa.get(i));
389 +            assertEquals(m4, aa.compareAndExchangeAcquire(i,m5, seven));
390 +            assertEquals(m4, aa.get(i));
391 +            assertEquals(m4, aa.compareAndExchangeAcquire(i, m4, seven));
392 +            assertEquals(seven, aa.get(i));
393 +        }
394 +    }
395 +
396 +    /**
397 +     * compareAndExchangeRelease succeeds in changing value if equal to
398 +     * expected else fails
399 +     */
400 +    public void testCompareAndExchangeRelease() {
401 +        AtomicReferenceArray<Integer> aa = new AtomicReferenceArray<Integer>(SIZE);
402 +        for (int i = 0; i < SIZE; i++) {
403 +            aa.set(i, one);
404 +            assertEquals(one, aa.compareAndExchangeRelease(i, one, two));
405 +            assertEquals(two, aa.compareAndExchangeRelease(i, two, m4));
406 +            assertEquals(m4, aa.get(i));
407 +            assertEquals(m4, aa.compareAndExchangeRelease(i,m5, seven));
408 +            assertEquals(m4, aa.get(i));
409 +            assertEquals(m4, aa.compareAndExchangeRelease(i, m4, seven));
410 +            assertEquals(seven, aa.get(i));
411 +        }
412 +    }
413 +
414 +    /**
415 +     * repeated weakCompareAndSetVolatile succeeds in changing value when equal
416 +     * to expected
417 +     */
418 +    public void testWeakCompareAndSetVolatile() {
419 +        AtomicReferenceArray<Integer> aa = new AtomicReferenceArray<Integer>(SIZE);
420 +        for (int i = 0; i < SIZE; i++) {
421 +            aa.set(i, one);
422 +            do {} while (!aa.weakCompareAndSetVolatile(i, one, two));
423 +            do {} while (!aa.weakCompareAndSetVolatile(i, two, m4));
424 +            assertEquals(m4, aa.get(i));
425 +            do {} while (!aa.weakCompareAndSetVolatile(i, m4, seven));
426 +            assertEquals(seven, aa.get(i));
427 +        }
428 +    }
429 +
430 +    /**
431 +     * repeated weakCompareAndSetAcquire succeeds in changing value when equal
432 +     * to expected
433 +     */
434 +    public void testWeakCompareAndSetAcquire() {
435 +        AtomicReferenceArray<Integer> aa = new AtomicReferenceArray<Integer>(SIZE);
436 +        for (int i = 0; i < SIZE; i++) {
437 +            aa.set(i, one);
438 +            do {} while (!aa.weakCompareAndSetAcquire(i, one, two));
439 +            do {} while (!aa.weakCompareAndSetAcquire(i, two, m4));
440 +            assertEquals(m4, aa.get(i));
441 +            do {} while (!aa.weakCompareAndSetAcquire(i, m4, seven));
442 +            assertEquals(seven, aa.get(i));
443 +        }
444 +    }
445 +
446 +    /**
447 +     * repeated weakCompareAndSetRelease succeeds in changing value when equal
448 +     * to expected
449 +     */
450 +    public void testWeakCompareAndSetRelease() {
451 +        AtomicReferenceArray<Integer> aa = new AtomicReferenceArray<Integer>(SIZE);
452 +        for (int i = 0; i < SIZE; i++) {
453 +            aa.set(i, one);
454 +            do {} while (!aa.weakCompareAndSetRelease(i, one, two));
455 +            do {} while (!aa.weakCompareAndSetRelease(i, two, m4));
456 +            assertEquals(m4, aa.get(i));
457 +            do {} while (!aa.weakCompareAndSetRelease(i, m4, seven));
458 +            assertEquals(seven, aa.get(i));
459 +        }
460 +    }
461 +
462   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines