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

Comparing jsr166/src/test/tck/AtomicLongArrayTest.java (file contents):
Revision 1.17 by jsr166, Thu Sep 16 00:52:49 2010 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.AtomicLongArray;
11 >
12 > import junit.framework.Test;
13 > import junit.framework.TestSuite;
14  
15   public class AtomicLongArrayTest extends JSR166TestCase {
16      public static void main(String[] args) {
17 <        junit.textui.TestRunner.run(suite());
17 >        main(suite(), args);
18      }
19      public static Test suite() {
20          return new TestSuite(AtomicLongArrayTest.class);
# Line 23 | Line 24 | public class AtomicLongArrayTest extends
24       * constructor creates array of given size with all elements zero
25       */
26      public void testConstructor() {
27 <        AtomicLongArray ai = new AtomicLongArray(SIZE);
28 <        for (int i = 0; i < SIZE; ++i)
29 <            assertEquals(0,ai.get(i));
27 >        AtomicLongArray aa = new AtomicLongArray(SIZE);
28 >        for (int i = 0; i < SIZE; i++)
29 >            assertEquals(0, aa.get(i));
30      }
31  
32      /**
# Line 34 | Line 35 | public class AtomicLongArrayTest extends
35      public void testConstructor2NPE() {
36          try {
37              long[] a = null;
38 <            AtomicLongArray ai = new AtomicLongArray(a);
38 >            new AtomicLongArray(a);
39              shouldThrow();
40          } catch (NullPointerException success) {}
41      }
# Line 43 | Line 44 | public class AtomicLongArrayTest extends
44       * constructor with array is of same size and has all elements
45       */
46      public void testConstructor2() {
47 <        long[] a = { 17L, 3L, -42L, 99L, -7L};
48 <        AtomicLongArray ai = new AtomicLongArray(a);
49 <        assertEquals(a.length, ai.length());
50 <        for (int i = 0; i < a.length; ++i)
51 <            assertEquals(a[i], ai.get(i));
47 >        long[] a = { 17L, 3L, -42L, 99L, -7L };
48 >        AtomicLongArray aa = new AtomicLongArray(a);
49 >        assertEquals(a.length, aa.length());
50 >        for (int i = 0; i < a.length; i++)
51 >            assertEquals(a[i], aa.get(i));
52      }
53  
54      /**
55       * get and set for out of bound indices throw IndexOutOfBoundsException
56       */
57      public void testIndexing() {
58 <        AtomicLongArray ai = new AtomicLongArray(SIZE);
59 <        try {
60 <            ai.get(SIZE);
61 <            shouldThrow();
62 <        } catch (IndexOutOfBoundsException success) {
63 <        }
64 <        try {
65 <            ai.get(-1);
66 <            shouldThrow();
67 <        } catch (IndexOutOfBoundsException success) {
68 <        }
69 <        try {
70 <            ai.set(SIZE, 0);
71 <            shouldThrow();
72 <        } catch (IndexOutOfBoundsException success) {
73 <        }
74 <        try {
75 <            ai.set(-1, 0);
76 <            shouldThrow();
77 <        } catch (IndexOutOfBoundsException success) {
58 >        AtomicLongArray aa = new AtomicLongArray(SIZE);
59 >        for (int index : new int[] { -1, SIZE }) {
60 >            try {
61 >                aa.get(index);
62 >                shouldThrow();
63 >            } catch (IndexOutOfBoundsException success) {}
64 >            try {
65 >                aa.set(index, 1);
66 >                shouldThrow();
67 >            } catch (IndexOutOfBoundsException success) {}
68 >            try {
69 >                aa.lazySet(index, 1);
70 >                shouldThrow();
71 >            } catch (IndexOutOfBoundsException success) {}
72 >            try {
73 >                aa.compareAndSet(index, 1, 2);
74 >                shouldThrow();
75 >            } catch (IndexOutOfBoundsException success) {}
76 >            try {
77 >                aa.weakCompareAndSet(index, 1, 2);
78 >                shouldThrow();
79 >            } catch (IndexOutOfBoundsException success) {}
80 >            try {
81 >                aa.getAndAdd(index, 1);
82 >                shouldThrow();
83 >            } catch (IndexOutOfBoundsException success) {}
84 >            try {
85 >                aa.addAndGet(index, 1);
86 >                shouldThrow();
87 >            } catch (IndexOutOfBoundsException success) {}
88 >            try {
89 >                aa.getPlain(index);
90 >                shouldThrow();
91 >            } catch (IndexOutOfBoundsException success) {}
92 >            try {
93 >                aa.getOpaque(index);
94 >                shouldThrow();
95 >            } catch (IndexOutOfBoundsException success) {}
96 >            try {
97 >                aa.getAcquire(index);
98 >                shouldThrow();
99 >            } catch (IndexOutOfBoundsException success) {}
100 >            try {
101 >                aa.setPlain(index, 1);
102 >                shouldThrow();
103 >            } catch (IndexOutOfBoundsException success) {}
104 >            try {
105 >                aa.setOpaque(index, 1);
106 >                shouldThrow();
107 >            } catch (IndexOutOfBoundsException success) {}
108 >            try {
109 >                aa.setRelease(index, 1);
110 >                shouldThrow();
111 >            } catch (IndexOutOfBoundsException success) {}
112 >            try {
113 >                aa.compareAndExchange(index, 1, 2);
114 >                shouldThrow();
115 >            } catch (IndexOutOfBoundsException success) {}
116 >            try {
117 >                aa.compareAndExchangeAcquire(index, 1, 2);
118 >                shouldThrow();
119 >            } catch (IndexOutOfBoundsException success) {}
120 >            try {
121 >                aa.compareAndExchangeRelease(index, 1, 2);
122 >                shouldThrow();
123 >            } catch (IndexOutOfBoundsException success) {}
124 >            try {
125 >                aa.weakCompareAndSetVolatile(index, 1, 2);
126 >                shouldThrow();
127 >            } catch (IndexOutOfBoundsException success) {}
128 >            try {
129 >                aa.weakCompareAndSetAcquire(index, 1, 2);
130 >                shouldThrow();
131 >            } catch (IndexOutOfBoundsException success) {}
132 >            try {
133 >                aa.weakCompareAndSetRelease(index, 1, 2);
134 >                shouldThrow();
135 >            } catch (IndexOutOfBoundsException success) {}
136          }
137      }
138  
# Line 81 | Line 140 | public class AtomicLongArrayTest extends
140       * get returns the last value set at index
141       */
142      public void testGetSet() {
143 <        AtomicLongArray ai = new AtomicLongArray(SIZE);
144 <        for (int i = 0; i < SIZE; ++i) {
145 <            ai.set(i, 1);
146 <            assertEquals(1,ai.get(i));
147 <            ai.set(i, 2);
148 <            assertEquals(2,ai.get(i));
149 <            ai.set(i, -3);
150 <            assertEquals(-3,ai.get(i));
143 >        AtomicLongArray aa = new AtomicLongArray(SIZE);
144 >        for (int i = 0; i < SIZE; i++) {
145 >            aa.set(i, 1);
146 >            assertEquals(1, aa.get(i));
147 >            aa.set(i, 2);
148 >            assertEquals(2, aa.get(i));
149 >            aa.set(i, -3);
150 >            assertEquals(-3, aa.get(i));
151          }
152      }
153  
# Line 96 | Line 155 | public class AtomicLongArrayTest extends
155       * get returns the last value lazySet at index by same thread
156       */
157      public void testGetLazySet() {
158 <        AtomicLongArray ai = new AtomicLongArray(SIZE);
159 <        for (int i = 0; i < SIZE; ++i) {
160 <            ai.lazySet(i, 1);
161 <            assertEquals(1,ai.get(i));
162 <            ai.lazySet(i, 2);
163 <            assertEquals(2,ai.get(i));
164 <            ai.lazySet(i, -3);
165 <            assertEquals(-3,ai.get(i));
158 >        AtomicLongArray aa = new AtomicLongArray(SIZE);
159 >        for (int i = 0; i < SIZE; i++) {
160 >            aa.lazySet(i, 1);
161 >            assertEquals(1, aa.get(i));
162 >            aa.lazySet(i, 2);
163 >            assertEquals(2, aa.get(i));
164 >            aa.lazySet(i, -3);
165 >            assertEquals(-3, aa.get(i));
166          }
167      }
168  
# Line 111 | Line 170 | public class AtomicLongArrayTest extends
170       * compareAndSet succeeds in changing value if equal to expected else fails
171       */
172      public void testCompareAndSet() {
173 <        AtomicLongArray ai = new AtomicLongArray(SIZE);
174 <        for (int i = 0; i < SIZE; ++i) {
175 <            ai.set(i, 1);
176 <            assertTrue(ai.compareAndSet(i, 1,2));
177 <            assertTrue(ai.compareAndSet(i, 2,-4));
178 <            assertEquals(-4,ai.get(i));
179 <            assertFalse(ai.compareAndSet(i, -5,7));
180 <            assertEquals(-4,ai.get(i));
181 <            assertTrue(ai.compareAndSet(i, -4,7));
182 <            assertEquals(7,ai.get(i));
173 >        AtomicLongArray aa = new AtomicLongArray(SIZE);
174 >        for (int i = 0; i < SIZE; i++) {
175 >            aa.set(i, 1);
176 >            assertTrue(aa.compareAndSet(i, 1, 2));
177 >            assertTrue(aa.compareAndSet(i, 2, -4));
178 >            assertEquals(-4, aa.get(i));
179 >            assertFalse(aa.compareAndSet(i, -5, 7));
180 >            assertEquals(-4, aa.get(i));
181 >            assertTrue(aa.compareAndSet(i, -4, 7));
182 >            assertEquals(7, aa.get(i));
183          }
184      }
185  
# Line 141 | Line 200 | public class AtomicLongArrayTest extends
200          assertTrue(a.compareAndSet(0, 1, 2));
201          t.join(LONG_DELAY_MS);
202          assertFalse(t.isAlive());
203 <        assertEquals(a.get(0), 3);
203 >        assertEquals(3, a.get(0));
204      }
205  
206      /**
# Line 149 | Line 208 | public class AtomicLongArrayTest extends
208       * to expected
209       */
210      public void testWeakCompareAndSet() {
211 <        AtomicLongArray ai = new AtomicLongArray(SIZE);
212 <        for (int i = 0; i < SIZE; ++i) {
213 <            ai.set(i, 1);
214 <            while (!ai.weakCompareAndSet(i, 1,2));
215 <            while (!ai.weakCompareAndSet(i, 2,-4));
216 <            assertEquals(-4,ai.get(i));
217 <            while (!ai.weakCompareAndSet(i, -4,7));
218 <            assertEquals(7,ai.get(i));
211 >        AtomicLongArray aa = new AtomicLongArray(SIZE);
212 >        for (int i = 0; i < SIZE; i++) {
213 >            aa.set(i, 1);
214 >            do {} while (!aa.weakCompareAndSet(i, 1, 2));
215 >            do {} while (!aa.weakCompareAndSet(i, 2, -4));
216 >            assertEquals(-4, aa.get(i));
217 >            do {} while (!aa.weakCompareAndSet(i, -4, 7));
218 >            assertEquals(7, aa.get(i));
219          }
220      }
221  
222      /**
223 <     *  getAndSet returns previous value and sets to given value at given index
223 >     * getAndSet returns previous value and sets to given value at given index
224       */
225      public void testGetAndSet() {
226 <        AtomicLongArray ai = new AtomicLongArray(SIZE);
227 <        for (int i = 0; i < SIZE; ++i) {
228 <            ai.set(i, 1);
229 <            assertEquals(1,ai.getAndSet(i,0));
230 <            assertEquals(0,ai.getAndSet(i,-10));
231 <            assertEquals(-10,ai.getAndSet(i,1));
226 >        AtomicLongArray aa = new AtomicLongArray(SIZE);
227 >        for (int i = 0; i < SIZE; i++) {
228 >            aa.set(i, 1);
229 >            assertEquals(1, aa.getAndSet(i, 0));
230 >            assertEquals(0, aa.getAndSet(i, -10));
231 >            assertEquals(-10, aa.getAndSet(i, 1));
232          }
233      }
234  
235      /**
236 <     *  getAndAdd returns previous value and adds given value
236 >     * getAndAdd returns previous value and adds given value
237       */
238      public void testGetAndAdd() {
239 <        AtomicLongArray ai = new AtomicLongArray(SIZE);
240 <        for (int i = 0; i < SIZE; ++i) {
241 <            ai.set(i, 1);
242 <            assertEquals(1,ai.getAndAdd(i,2));
243 <            assertEquals(3,ai.get(i));
244 <            assertEquals(3,ai.getAndAdd(i,-4));
245 <            assertEquals(-1,ai.get(i));
239 >        AtomicLongArray aa = new AtomicLongArray(SIZE);
240 >        for (int i = 0; i < SIZE; i++) {
241 >            aa.set(i, 1);
242 >            assertEquals(1, aa.getAndAdd(i, 2));
243 >            assertEquals(3, aa.get(i));
244 >            assertEquals(3, aa.getAndAdd(i, -4));
245 >            assertEquals(-1, aa.get(i));
246          }
247      }
248  
# Line 191 | Line 250 | public class AtomicLongArrayTest extends
250       * getAndDecrement returns previous value and decrements
251       */
252      public void testGetAndDecrement() {
253 <        AtomicLongArray ai = new AtomicLongArray(SIZE);
254 <        for (int i = 0; i < SIZE; ++i) {
255 <            ai.set(i, 1);
256 <            assertEquals(1,ai.getAndDecrement(i));
257 <            assertEquals(0,ai.getAndDecrement(i));
258 <            assertEquals(-1,ai.getAndDecrement(i));
253 >        AtomicLongArray aa = new AtomicLongArray(SIZE);
254 >        for (int i = 0; i < SIZE; i++) {
255 >            aa.set(i, 1);
256 >            assertEquals(1, aa.getAndDecrement(i));
257 >            assertEquals(0, aa.getAndDecrement(i));
258 >            assertEquals(-1, aa.getAndDecrement(i));
259          }
260      }
261  
# Line 204 | Line 263 | public class AtomicLongArrayTest extends
263       * getAndIncrement returns previous value and increments
264       */
265      public void testGetAndIncrement() {
266 <        AtomicLongArray ai = new AtomicLongArray(SIZE);
267 <        for (int i = 0; i < SIZE; ++i) {
268 <            ai.set(i, 1);
269 <            assertEquals(1,ai.getAndIncrement(i));
270 <            assertEquals(2,ai.get(i));
271 <            ai.set(i,-2);
272 <            assertEquals(-2,ai.getAndIncrement(i));
273 <            assertEquals(-1,ai.getAndIncrement(i));
274 <            assertEquals(0,ai.getAndIncrement(i));
275 <            assertEquals(1,ai.get(i));
266 >        AtomicLongArray aa = new AtomicLongArray(SIZE);
267 >        for (int i = 0; i < SIZE; i++) {
268 >            aa.set(i, 1);
269 >            assertEquals(1, aa.getAndIncrement(i));
270 >            assertEquals(2, aa.get(i));
271 >            aa.set(i, -2);
272 >            assertEquals(-2, aa.getAndIncrement(i));
273 >            assertEquals(-1, aa.getAndIncrement(i));
274 >            assertEquals(0, aa.getAndIncrement(i));
275 >            assertEquals(1, aa.get(i));
276          }
277      }
278  
279      /**
280 <     *  addAndGet adds given value to current, and returns current value
280 >     * addAndGet adds given value to current, and returns current value
281       */
282      public void testAddAndGet() {
283 <        AtomicLongArray ai = new AtomicLongArray(SIZE);
284 <        for (int i = 0; i < SIZE; ++i) {
285 <            ai.set(i, 1);
286 <            assertEquals(3,ai.addAndGet(i,2));
287 <            assertEquals(3,ai.get(i));
288 <            assertEquals(-1,ai.addAndGet(i,-4));
289 <            assertEquals(-1,ai.get(i));
283 >        AtomicLongArray aa = new AtomicLongArray(SIZE);
284 >        for (int i = 0; i < SIZE; i++) {
285 >            aa.set(i, 1);
286 >            assertEquals(3, aa.addAndGet(i, 2));
287 >            assertEquals(3, aa.get(i));
288 >            assertEquals(-1, aa.addAndGet(i, -4));
289 >            assertEquals(-1, aa.get(i));
290          }
291      }
292  
# Line 235 | Line 294 | public class AtomicLongArrayTest extends
294       * decrementAndGet decrements and returns current value
295       */
296      public void testDecrementAndGet() {
297 <        AtomicLongArray ai = new AtomicLongArray(SIZE);
298 <        for (int i = 0; i < SIZE; ++i) {
299 <            ai.set(i, 1);
300 <            assertEquals(0,ai.decrementAndGet(i));
301 <            assertEquals(-1,ai.decrementAndGet(i));
302 <            assertEquals(-2,ai.decrementAndGet(i));
303 <            assertEquals(-2,ai.get(i));
297 >        AtomicLongArray aa = new AtomicLongArray(SIZE);
298 >        for (int i = 0; i < SIZE; i++) {
299 >            aa.set(i, 1);
300 >            assertEquals(0, aa.decrementAndGet(i));
301 >            assertEquals(-1, aa.decrementAndGet(i));
302 >            assertEquals(-2, aa.decrementAndGet(i));
303 >            assertEquals(-2, aa.get(i));
304          }
305      }
306  
# Line 249 | Line 308 | public class AtomicLongArrayTest extends
308       * incrementAndGet increments and returns current value
309       */
310      public void testIncrementAndGet() {
311 <        AtomicLongArray ai = new AtomicLongArray(SIZE);
312 <        for (int i = 0; i < SIZE; ++i) {
313 <            ai.set(i, 1);
314 <            assertEquals(2,ai.incrementAndGet(i));
315 <            assertEquals(2,ai.get(i));
316 <            ai.set(i, -2);
317 <            assertEquals(-1,ai.incrementAndGet(i));
318 <            assertEquals(0,ai.incrementAndGet(i));
319 <            assertEquals(1,ai.incrementAndGet(i));
320 <            assertEquals(1,ai.get(i));
311 >        AtomicLongArray aa = new AtomicLongArray(SIZE);
312 >        for (int i = 0; i < SIZE; i++) {
313 >            aa.set(i, 1);
314 >            assertEquals(2, aa.incrementAndGet(i));
315 >            assertEquals(2, aa.get(i));
316 >            aa.set(i, -2);
317 >            assertEquals(-1, aa.incrementAndGet(i));
318 >            assertEquals(0, aa.incrementAndGet(i));
319 >            assertEquals(1, aa.incrementAndGet(i));
320 >            assertEquals(1, aa.get(i));
321          }
322      }
323  
265    static final long COUNTDOWN = 100000;
266
324      class Counter extends CheckedRunnable {
325 <        final AtomicLongArray ai;
325 >        final AtomicLongArray aa;
326          volatile long counts;
327 <        Counter(AtomicLongArray a) { ai = a; }
327 >        Counter(AtomicLongArray a) { aa = a; }
328          public void realRun() {
329              for (;;) {
330                  boolean done = true;
331 <                for (int i = 0; i < ai.length(); ++i) {
332 <                    long v = ai.get(i);
331 >                for (int i = 0; i < aa.length(); i++) {
332 >                    long v = aa.get(i);
333                      assertTrue(v >= 0);
334                      if (v != 0) {
335                          done = false;
336 <                        if (ai.compareAndSet(i, v, v-1))
336 >                        if (aa.compareAndSet(i, v, v - 1))
337                              ++counts;
338                      }
339                  }
# Line 291 | Line 348 | public class AtomicLongArrayTest extends
348       * update a number of times equal to total count
349       */
350      public void testCountingInMultipleThreads() throws InterruptedException {
351 <        final AtomicLongArray ai = new AtomicLongArray(SIZE);
352 <        for (int i = 0; i < SIZE; ++i)
353 <            ai.set(i, COUNTDOWN);
354 <        Counter c1 = new Counter(ai);
355 <        Counter c2 = new Counter(ai);
351 >        final AtomicLongArray aa = new AtomicLongArray(SIZE);
352 >        long countdown = 10000;
353 >        for (int i = 0; i < SIZE; i++)
354 >            aa.set(i, countdown);
355 >        Counter c1 = new Counter(aa);
356 >        Counter c2 = new Counter(aa);
357          Thread t1 = new Thread(c1);
358          Thread t2 = new Thread(c2);
359          t1.start();
360          t2.start();
361          t1.join();
362          t2.join();
363 <        assertEquals(c1.counts+c2.counts, SIZE * COUNTDOWN);
363 >        assertEquals(c1.counts+c2.counts, SIZE * countdown);
364      }
365  
366      /**
367       * a deserialized serialized array holds same values
368       */
369      public void testSerialization() throws Exception {
370 <        AtomicLongArray l = new AtomicLongArray(SIZE);
371 <        for (int i = 0; i < SIZE; ++i)
372 <            l.set(i, -i);
373 <
374 <        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
375 <        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
376 <        out.writeObject(l);
377 <        out.close();
320 <
321 <        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
322 <        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
323 <        AtomicLongArray r = (AtomicLongArray) in.readObject();
324 <        for (int i = 0; i < SIZE; ++i) {
325 <            assertEquals(l.get(i), r.get(i));
370 >        AtomicLongArray x = new AtomicLongArray(SIZE);
371 >        for (int i = 0; i < SIZE; i++)
372 >            x.set(i, -i);
373 >        AtomicLongArray y = serialClone(x);
374 >        assertNotSame(x, y);
375 >        assertEquals(x.length(), y.length());
376 >        for (int i = 0; i < SIZE; i++) {
377 >            assertEquals(x.get(i), y.get(i));
378          }
379      }
380  
# Line 330 | Line 382 | public class AtomicLongArrayTest extends
382       * toString returns current value.
383       */
384      public void testToString() {
385 <        long[] a = { 17, 3, -42, 99, -7};
386 <        AtomicLongArray ai = new AtomicLongArray(a);
387 <        assertEquals(Arrays.toString(a), ai.toString());
385 >        long[] a = { 17, 3, -42, 99, -7 };
386 >        AtomicLongArray aa = new AtomicLongArray(a);
387 >        assertEquals(Arrays.toString(a), aa.toString());
388 >    }
389 >
390 >    // jdk9
391 >
392 >    /**
393 >     * getPlain returns the last value set
394 >     */
395 >    public void testGetPlainSet() {
396 >        AtomicLongArray aa = new AtomicLongArray(SIZE);
397 >        for (int i = 0; i < SIZE; i++) {
398 >            aa.set(i, 1);
399 >            assertEquals(1, aa.getPlain(i));
400 >            aa.set(i, 2);
401 >            assertEquals(2, aa.getPlain(i));
402 >            aa.set(i, -3);
403 >            assertEquals(-3, aa.getPlain(i));
404 >        }
405 >    }
406 >
407 >    /**
408 >     * getOpaque returns the last value set
409 >     */
410 >    public void testGetOpaqueSet() {
411 >        AtomicLongArray aa = new AtomicLongArray(SIZE);
412 >        for (int i = 0; i < SIZE; i++) {
413 >            aa.set(i, 1);
414 >            assertEquals(1, aa.getOpaque(i));
415 >            aa.set(i, 2);
416 >            assertEquals(2, aa.getOpaque(i));
417 >            aa.set(i, -3);
418 >            assertEquals(-3, aa.getOpaque(i));
419 >        }
420 >    }
421 >
422 >    /**
423 >     * getAcquire returns the last value set
424 >     */
425 >    public void testGetAcquireSet() {
426 >        AtomicLongArray aa = new AtomicLongArray(SIZE);
427 >        for (int i = 0; i < SIZE; i++) {
428 >            aa.set(i, 1);
429 >            assertEquals(1, aa.getAcquire(i));
430 >            aa.set(i, 2);
431 >            assertEquals(2, aa.getAcquire(i));
432 >            aa.set(i, -3);
433 >            assertEquals(-3, aa.getAcquire(i));
434 >        }
435 >    }
436 >
437 >    /**
438 >     * get returns the last value setPlain
439 >     */
440 >    public void testGetSetPlain() {
441 >        AtomicLongArray aa = new AtomicLongArray(SIZE);
442 >        for (int i = 0; i < SIZE; i++) {
443 >            aa.setPlain(i, 1);
444 >            assertEquals(1, aa.get(i));
445 >            aa.setPlain(i, 2);
446 >            assertEquals(2, aa.get(i));
447 >            aa.setPlain(i, -3);
448 >            assertEquals(-3, aa.get(i));
449 >        }
450 >    }
451 >
452 >    /**
453 >     * get returns the last value setOpaque
454 >     */
455 >    public void testGetSetOpaque() {
456 >        AtomicLongArray aa = new AtomicLongArray(SIZE);
457 >        for (int i = 0; i < SIZE; i++) {
458 >            aa.setOpaque(i, 1);
459 >            assertEquals(1, aa.get(i));
460 >            aa.setOpaque(i, 2);
461 >            assertEquals(2, aa.get(i));
462 >            aa.setOpaque(i, -3);
463 >            assertEquals(-3, aa.get(i));
464 >        }
465 >    }
466 >
467 >    /**
468 >     * get returns the last value setRelease
469 >     */
470 >    public void testGetSetRelease() {
471 >        AtomicLongArray aa = new AtomicLongArray(SIZE);
472 >        for (int i = 0; i < SIZE; i++) {
473 >            aa.setRelease(i, 1);
474 >            assertEquals(1, aa.get(i));
475 >            aa.setRelease(i, 2);
476 >            assertEquals(2, aa.get(i));
477 >            aa.setRelease(i, -3);
478 >            assertEquals(-3, aa.get(i));
479 >        }
480 >    }
481 >
482 >    /**
483 >     * compareAndExchange succeeds in changing value if equal to
484 >     * expected else fails
485 >     */
486 >    public void testCompareAndExchange() {
487 >        AtomicLongArray aa = new AtomicLongArray(SIZE);
488 >        for (int i = 0; i < SIZE; i++) {
489 >            aa.set(i, 1);
490 >            assertEquals(1, aa.compareAndExchange(i, 1, 2));
491 >            assertEquals(2, aa.compareAndExchange(i, 2, -4));
492 >            assertEquals(-4, aa.get(i));
493 >            assertEquals(-4, aa.compareAndExchange(i,-5, 7));
494 >            assertEquals(-4, aa.get(i));
495 >            assertEquals(-4, aa.compareAndExchange(i, -4, 7));
496 >            assertEquals(7, aa.get(i));
497 >        }
498 >    }
499 >
500 >    /**
501 >     * compareAndExchangeAcquire succeeds in changing value if equal to
502 >     * expected else fails
503 >     */
504 >    public void testCompareAndExchangeAcquire() {
505 >        AtomicLongArray aa = new AtomicLongArray(SIZE);
506 >        for (int i = 0; i < SIZE; i++) {
507 >            aa.set(i, 1);
508 >            assertEquals(1, aa.compareAndExchangeAcquire(i, 1, 2));
509 >            assertEquals(2, aa.compareAndExchangeAcquire(i, 2, -4));
510 >            assertEquals(-4, aa.get(i));
511 >            assertEquals(-4, aa.compareAndExchangeAcquire(i,-5, 7));
512 >            assertEquals(-4, aa.get(i));
513 >            assertEquals(-4, aa.compareAndExchangeAcquire(i, -4, 7));
514 >            assertEquals(7, aa.get(i));
515 >        }
516 >    }
517 >
518 >    /**
519 >     * compareAndExchangeRelease succeeds in changing value if equal to
520 >     * expected else fails
521 >     */
522 >    public void testCompareAndExchangeRelease() {
523 >        AtomicLongArray aa = new AtomicLongArray(SIZE);
524 >        for (int i = 0; i < SIZE; i++) {
525 >            aa.set(i, 1);
526 >            assertEquals(1, aa.compareAndExchangeRelease(i, 1, 2));
527 >            assertEquals(2, aa.compareAndExchangeRelease(i, 2, -4));
528 >            assertEquals(-4, aa.get(i));
529 >            assertEquals(-4, aa.compareAndExchangeRelease(i,-5, 7));
530 >            assertEquals(-4, aa.get(i));
531 >            assertEquals(-4, aa.compareAndExchangeRelease(i, -4, 7));
532 >            assertEquals(7, aa.get(i));
533 >        }
534 >    }
535 >
536 >    /**
537 >     * repeated weakCompareAndSetVolatile succeeds in changing value when equal
538 >     * to expected
539 >     */
540 >    public void testWeakCompareAndSetVolatile() {
541 >        AtomicLongArray aa = new AtomicLongArray(SIZE);
542 >        for (int i = 0; i < SIZE; i++) {
543 >            aa.set(i, 1);
544 >            do {} while (!aa.weakCompareAndSetVolatile(i, 1, 2));
545 >            do {} while (!aa.weakCompareAndSetVolatile(i, 2, -4));
546 >            assertEquals(-4, aa.get(i));
547 >            do {} while (!aa.weakCompareAndSetVolatile(i, -4, 7));
548 >            assertEquals(7, aa.get(i));
549 >        }
550      }
551  
552 +    /**
553 +     * repeated weakCompareAndSetAcquire succeeds in changing value when equal
554 +     * to expected
555 +     */
556 +    public void testWeakCompareAndSetAcquire() {
557 +        AtomicLongArray aa = new AtomicLongArray(SIZE);
558 +        for (int i = 0; i < SIZE; i++) {
559 +            aa.set(i, 1);
560 +            do {} while (!aa.weakCompareAndSetAcquire(i, 1, 2));
561 +            do {} while (!aa.weakCompareAndSetAcquire(i, 2, -4));
562 +            assertEquals(-4, aa.get(i));
563 +            do {} while (!aa.weakCompareAndSetAcquire(i, -4, 7));
564 +            assertEquals(7, aa.get(i));
565 +        }
566 +    }
567 +
568 +    /**
569 +     * repeated weakCompareAndSetRelease succeeds in changing value when equal
570 +     * to expected
571 +     */
572 +    public void testWeakCompareAndSetRelease() {
573 +        AtomicLongArray aa = new AtomicLongArray(SIZE);
574 +        for (int i = 0; i < SIZE; i++) {
575 +            aa.set(i, 1);
576 +            do {} while (!aa.weakCompareAndSetRelease(i, 1, 2));
577 +            do {} while (!aa.weakCompareAndSetRelease(i, 2, -4));
578 +            assertEquals(-4, aa.get(i));
579 +            do {} while (!aa.weakCompareAndSetRelease(i, -4, 7));
580 +            assertEquals(7, aa.get(i));
581 +        }
582 +    }
583  
584   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines