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.9 by dl, Wed May 25 14:27:37 2005 UTC vs.
Revision 1.23 by jsr166, Fri Jun 10 20:01:21 2011 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
5 < * Other contributors include Andrew Wright, Jeffrey Hayes,
6 < * Pat Fisher, Mike Judd.
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.*;
10 > import java.util.Arrays;
11 > import java.util.concurrent.atomic.AtomicLongArray;
12  
13   public class AtomicLongArrayTest extends JSR166TestCase {
14 <    public static void main (String[] args) {
15 <        junit.textui.TestRunner.run (suite());
14 >    public static void main(String[] args) {
15 >        junit.textui.TestRunner.run(suite());
16      }
17      public static Test suite() {
18          return new TestSuite(AtomicLongArrayTest.class);
# Line 22 | Line 21 | public class AtomicLongArrayTest extends
21      /**
22       * constructor creates array of given size with all elements zero
23       */
24 <    public void testConstructor(){
24 >    public void testConstructor() {
25          AtomicLongArray ai = new AtomicLongArray(SIZE);
26 <        for (int i = 0; i < SIZE; ++i)
27 <            assertEquals(0,ai.get(i));
26 >        for (int i = 0; i < SIZE; ++i)
27 >            assertEquals(0, ai.get(i));
28      }
29  
30      /**
# Line 35 | Line 34 | public class AtomicLongArrayTest extends
34          try {
35              long[] a = null;
36              AtomicLongArray ai = new AtomicLongArray(a);
37 <        } catch (NullPointerException success) {
38 <        } catch (Exception ex) {
40 <            unexpectedException();
41 <        }
37 >            shouldThrow();
38 >        } catch (NullPointerException success) {}
39      }
40  
41      /**
42       * constructor with array is of same size and has all elements
43       */
44      public void testConstructor2() {
45 <        long[] a = { 17L, 3L, -42L, 99L, -7L};
45 >        long[] a = { 17L, 3L, -42L, 99L, -7L };
46          AtomicLongArray ai = new AtomicLongArray(a);
47          assertEquals(a.length, ai.length());
48 <        for (int i = 0; i < a.length; ++i)
48 >        for (int i = 0; i < a.length; ++i)
49              assertEquals(a[i], ai.get(i));
50      }
51  
52      /**
53       * get and set for out of bound indices throw IndexOutOfBoundsException
54       */
55 <    public void testIndexing(){
55 >    public void testIndexing() {
56          AtomicLongArray ai = new AtomicLongArray(SIZE);
57          try {
58              ai.get(SIZE);
59 <        } catch(IndexOutOfBoundsException success){
59 >            shouldThrow();
60 >        } catch (IndexOutOfBoundsException success) {
61          }
62          try {
63              ai.get(-1);
64 <        } catch(IndexOutOfBoundsException success){
64 >            shouldThrow();
65 >        } catch (IndexOutOfBoundsException success) {
66          }
67          try {
68              ai.set(SIZE, 0);
69 <        } catch(IndexOutOfBoundsException success){
69 >            shouldThrow();
70 >        } catch (IndexOutOfBoundsException success) {
71          }
72          try {
73              ai.set(-1, 0);
74 <        } catch(IndexOutOfBoundsException success){
74 >            shouldThrow();
75 >        } catch (IndexOutOfBoundsException success) {
76          }
77      }
78  
79      /**
80       * get returns the last value set at index
81       */
82 <    public void testGetSet(){
83 <        AtomicLongArray ai = new AtomicLongArray(SIZE);
82 >    public void testGetSet() {
83 >        AtomicLongArray ai = new AtomicLongArray(SIZE);
84          for (int i = 0; i < SIZE; ++i) {
85              ai.set(i, 1);
86 <            assertEquals(1,ai.get(i));
86 >            assertEquals(1, ai.get(i));
87              ai.set(i, 2);
88 <            assertEquals(2,ai.get(i));
88 >            assertEquals(2, ai.get(i));
89              ai.set(i, -3);
90 <            assertEquals(-3,ai.get(i));
90 >            assertEquals(-3, ai.get(i));
91          }
92      }
93  
94      /**
95       * get returns the last value lazySet at index by same thread
96       */
97 <    public void testGetLazySet(){
98 <        AtomicLongArray ai = new AtomicLongArray(SIZE);
97 >    public void testGetLazySet() {
98 >        AtomicLongArray ai = new AtomicLongArray(SIZE);
99          for (int i = 0; i < SIZE; ++i) {
100              ai.lazySet(i, 1);
101 <            assertEquals(1,ai.get(i));
101 >            assertEquals(1, ai.get(i));
102              ai.lazySet(i, 2);
103 <            assertEquals(2,ai.get(i));
103 >            assertEquals(2, ai.get(i));
104              ai.lazySet(i, -3);
105 <            assertEquals(-3,ai.get(i));
105 >            assertEquals(-3, ai.get(i));
106          }
107      }
108  
109      /**
110       * compareAndSet succeeds in changing value if equal to expected else fails
111       */
112 <    public void testCompareAndSet(){
113 <        AtomicLongArray ai = new AtomicLongArray(SIZE);
112 >    public void testCompareAndSet() {
113 >        AtomicLongArray ai = new AtomicLongArray(SIZE);
114          for (int i = 0; i < SIZE; ++i) {
115              ai.set(i, 1);
116 <            assertTrue(ai.compareAndSet(i, 1,2));
117 <            assertTrue(ai.compareAndSet(i, 2,-4));
118 <            assertEquals(-4,ai.get(i));
119 <            assertFalse(ai.compareAndSet(i, -5,7));
120 <            assertFalse((7 == ai.get(i)));
121 <            assertTrue(ai.compareAndSet(i, -4,7));
122 <            assertEquals(7,ai.get(i));
116 >            assertTrue(ai.compareAndSet(i, 1, 2));
117 >            assertTrue(ai.compareAndSet(i, 2, -4));
118 >            assertEquals(-4, ai.get(i));
119 >            assertFalse(ai.compareAndSet(i, -5, 7));
120 >            assertEquals(-4, ai.get(i));
121 >            assertTrue(ai.compareAndSet(i, -4, 7));
122 >            assertEquals(7, ai.get(i));
123          }
124      }
125  
# Line 126 | Line 127 | public class AtomicLongArrayTest extends
127       * compareAndSet in one thread enables another waiting for value
128       * to succeed
129       */
130 <    public void testCompareAndSetInMultipleThreads() {
130 >    public void testCompareAndSetInMultipleThreads() throws InterruptedException {
131          final AtomicLongArray a = new AtomicLongArray(1);
132          a.set(0, 1);
133 <        Thread t = new Thread(new Runnable() {
134 <                public void run() {
135 <                    while(!a.compareAndSet(0, 2, 3)) Thread.yield();
136 <                }});
137 <        try {
138 <            t.start();
139 <            assertTrue(a.compareAndSet(0, 1, 2));
140 <            t.join(LONG_DELAY_MS);
141 <            assertFalse(t.isAlive());
142 <            assertEquals(a.get(0), 3);
143 <        }
143 <        catch(Exception e) {
144 <            unexpectedException();
145 <        }
133 >        Thread t = new Thread(new CheckedRunnable() {
134 >            public void realRun() {
135 >                while (!a.compareAndSet(0, 2, 3))
136 >                    Thread.yield();
137 >            }});
138 >
139 >        t.start();
140 >        assertTrue(a.compareAndSet(0, 1, 2));
141 >        t.join(LONG_DELAY_MS);
142 >        assertFalse(t.isAlive());
143 >        assertEquals(a.get(0), 3);
144      }
145  
146      /**
147       * repeated weakCompareAndSet succeeds in changing value when equal
148 <     * to expected
148 >     * to expected
149       */
150 <    public void testWeakCompareAndSet(){
151 <        AtomicLongArray ai = new AtomicLongArray(SIZE);
150 >    public void testWeakCompareAndSet() {
151 >        AtomicLongArray ai = new AtomicLongArray(SIZE);
152          for (int i = 0; i < SIZE; ++i) {
153              ai.set(i, 1);
154 <            while(!ai.weakCompareAndSet(i, 1,2));
155 <            while(!ai.weakCompareAndSet(i, 2,-4));
156 <            assertEquals(-4,ai.get(i));
157 <            while(!ai.weakCompareAndSet(i, -4,7));
158 <            assertEquals(7,ai.get(i));
154 >            while (!ai.weakCompareAndSet(i, 1, 2));
155 >            while (!ai.weakCompareAndSet(i, 2, -4));
156 >            assertEquals(-4, ai.get(i));
157 >            while (!ai.weakCompareAndSet(i, -4, 7));
158 >            assertEquals(7, ai.get(i));
159          }
160      }
161  
162      /**
163 <     *  getAndSet returns previous value and sets to given value at given index
163 >     * getAndSet returns previous value and sets to given value at given index
164       */
165 <    public void testGetAndSet(){
166 <        AtomicLongArray ai = new AtomicLongArray(SIZE);
165 >    public void testGetAndSet() {
166 >        AtomicLongArray ai = new AtomicLongArray(SIZE);
167          for (int i = 0; i < SIZE; ++i) {
168              ai.set(i, 1);
169 <            assertEquals(1,ai.getAndSet(i,0));
170 <            assertEquals(0,ai.getAndSet(i,-10));
171 <            assertEquals(-10,ai.getAndSet(i,1));
169 >            assertEquals(1, ai.getAndSet(i, 0));
170 >            assertEquals(0, ai.getAndSet(i, -10));
171 >            assertEquals(-10, ai.getAndSet(i, 1));
172          }
173      }
174  
175      /**
176 <     *  getAndAdd returns previous value and adds given value
176 >     * getAndAdd returns previous value and adds given value
177       */
178 <    public void testGetAndAdd(){
179 <        AtomicLongArray ai = new AtomicLongArray(SIZE);
178 >    public void testGetAndAdd() {
179 >        AtomicLongArray ai = new AtomicLongArray(SIZE);
180          for (int i = 0; i < SIZE; ++i) {
181              ai.set(i, 1);
182 <            assertEquals(1,ai.getAndAdd(i,2));
183 <            assertEquals(3,ai.get(i));
184 <            assertEquals(3,ai.getAndAdd(i,-4));
185 <            assertEquals(-1,ai.get(i));
182 >            assertEquals(1, ai.getAndAdd(i, 2));
183 >            assertEquals(3, ai.get(i));
184 >            assertEquals(3, ai.getAndAdd(i, -4));
185 >            assertEquals(-1, ai.get(i));
186          }
187      }
188  
189      /**
190       * getAndDecrement returns previous value and decrements
191       */
192 <    public void testGetAndDecrement(){
193 <        AtomicLongArray ai = new AtomicLongArray(SIZE);
192 >    public void testGetAndDecrement() {
193 >        AtomicLongArray ai = new AtomicLongArray(SIZE);
194          for (int i = 0; i < SIZE; ++i) {
195              ai.set(i, 1);
196 <            assertEquals(1,ai.getAndDecrement(i));
197 <            assertEquals(0,ai.getAndDecrement(i));
198 <            assertEquals(-1,ai.getAndDecrement(i));
196 >            assertEquals(1, ai.getAndDecrement(i));
197 >            assertEquals(0, ai.getAndDecrement(i));
198 >            assertEquals(-1, ai.getAndDecrement(i));
199          }
200      }
201  
202      /**
203       * getAndIncrement returns previous value and increments
204       */
205 <    public void testGetAndIncrement(){
206 <        AtomicLongArray ai = new AtomicLongArray(SIZE);
205 >    public void testGetAndIncrement() {
206 >        AtomicLongArray ai = new AtomicLongArray(SIZE);
207          for (int i = 0; i < SIZE; ++i) {
208              ai.set(i, 1);
209 <            assertEquals(1,ai.getAndIncrement(i));
210 <            assertEquals(2,ai.get(i));
211 <            ai.set(i,-2);
212 <            assertEquals(-2,ai.getAndIncrement(i));
213 <            assertEquals(-1,ai.getAndIncrement(i));
214 <            assertEquals(0,ai.getAndIncrement(i));
215 <            assertEquals(1,ai.get(i));
209 >            assertEquals(1, ai.getAndIncrement(i));
210 >            assertEquals(2, ai.get(i));
211 >            ai.set(i, -2);
212 >            assertEquals(-2, ai.getAndIncrement(i));
213 >            assertEquals(-1, ai.getAndIncrement(i));
214 >            assertEquals(0, ai.getAndIncrement(i));
215 >            assertEquals(1, ai.get(i));
216          }
217      }
218  
219      /**
220 <     *  addAndGet adds given value to current, and returns current value
220 >     * addAndGet adds given value to current, and returns current value
221       */
222      public void testAddAndGet() {
223 <        AtomicLongArray ai = new AtomicLongArray(SIZE);
223 >        AtomicLongArray ai = new AtomicLongArray(SIZE);
224          for (int i = 0; i < SIZE; ++i) {
225              ai.set(i, 1);
226 <            assertEquals(3,ai.addAndGet(i,2));
227 <            assertEquals(3,ai.get(i));
228 <            assertEquals(-1,ai.addAndGet(i,-4));
229 <            assertEquals(-1,ai.get(i));
226 >            assertEquals(3, ai.addAndGet(i, 2));
227 >            assertEquals(3, ai.get(i));
228 >            assertEquals(-1, ai.addAndGet(i, -4));
229 >            assertEquals(-1, ai.get(i));
230          }
231      }
232  
233      /**
234       * decrementAndGet decrements and returns current value
235       */
236 <    public void testDecrementAndGet(){
237 <        AtomicLongArray ai = new AtomicLongArray(SIZE);
236 >    public void testDecrementAndGet() {
237 >        AtomicLongArray ai = new AtomicLongArray(SIZE);
238          for (int i = 0; i < SIZE; ++i) {
239              ai.set(i, 1);
240 <            assertEquals(0,ai.decrementAndGet(i));
241 <            assertEquals(-1,ai.decrementAndGet(i));
242 <            assertEquals(-2,ai.decrementAndGet(i));
243 <            assertEquals(-2,ai.get(i));
240 >            assertEquals(0, ai.decrementAndGet(i));
241 >            assertEquals(-1, ai.decrementAndGet(i));
242 >            assertEquals(-2, ai.decrementAndGet(i));
243 >            assertEquals(-2, ai.get(i));
244          }
245      }
246  
# Line 250 | Line 248 | public class AtomicLongArrayTest extends
248       * incrementAndGet increments and returns current value
249       */
250      public void testIncrementAndGet() {
251 <        AtomicLongArray ai = new AtomicLongArray(SIZE);
251 >        AtomicLongArray ai = new AtomicLongArray(SIZE);
252          for (int i = 0; i < SIZE; ++i) {
253              ai.set(i, 1);
254 <            assertEquals(2,ai.incrementAndGet(i));
255 <            assertEquals(2,ai.get(i));
254 >            assertEquals(2, ai.incrementAndGet(i));
255 >            assertEquals(2, ai.get(i));
256              ai.set(i, -2);
257 <            assertEquals(-1,ai.incrementAndGet(i));
258 <            assertEquals(0,ai.incrementAndGet(i));
259 <            assertEquals(1,ai.incrementAndGet(i));
260 <            assertEquals(1,ai.get(i));
257 >            assertEquals(-1, ai.incrementAndGet(i));
258 >            assertEquals(0, ai.incrementAndGet(i));
259 >            assertEquals(1, ai.incrementAndGet(i));
260 >            assertEquals(1, ai.get(i));
261          }
262      }
263  
264      static final long COUNTDOWN = 100000;
265 <    
266 <    class Counter implements Runnable {
265 >
266 >    class Counter extends CheckedRunnable {
267          final AtomicLongArray ai;
268          volatile long counts;
269          Counter(AtomicLongArray a) { ai = a; }
270 <        public void run() {
270 >        public void realRun() {
271              for (;;) {
272                  boolean done = true;
273                  for (int i = 0; i < ai.length(); ++i) {
274                      long v = ai.get(i);
275 <                    threadAssertTrue(v >= 0);
275 >                    assertTrue(v >= 0);
276                      if (v != 0) {
277                          done = false;
278                          if (ai.compareAndSet(i, v, v-1))
# Line 291 | Line 289 | public class AtomicLongArrayTest extends
289       * Multiple threads using same array of counters successfully
290       * update a number of times equal to total count
291       */
292 <    public void testCountingInMultipleThreads() {
293 <        try {
294 <            final AtomicLongArray ai = new AtomicLongArray(SIZE);
295 <            for (int i = 0; i < SIZE; ++i)
296 <                ai.set(i, COUNTDOWN);
297 <            Counter c1 = new Counter(ai);
298 <            Counter c2 = new Counter(ai);
299 <            Thread t1 = new Thread(c1);
300 <            Thread t2 = new Thread(c2);
301 <            t1.start();
302 <            t2.start();
303 <            t1.join();
304 <            t2.join();
307 <            assertEquals(c1.counts+c2.counts, SIZE * COUNTDOWN);
308 <        }
309 <        catch(InterruptedException ie) {
310 <            unexpectedException();
311 <        }
292 >    public void testCountingInMultipleThreads() throws InterruptedException {
293 >        final AtomicLongArray ai = new AtomicLongArray(SIZE);
294 >        for (int i = 0; i < SIZE; ++i)
295 >            ai.set(i, COUNTDOWN);
296 >        Counter c1 = new Counter(ai);
297 >        Counter c2 = new Counter(ai);
298 >        Thread t1 = new Thread(c1);
299 >        Thread t2 = new Thread(c2);
300 >        t1.start();
301 >        t2.start();
302 >        t1.join();
303 >        t2.join();
304 >        assertEquals(c1.counts+c2.counts, SIZE * COUNTDOWN);
305      }
306  
307      /**
308       * a deserialized serialized array holds same values
309       */
310 <    public void testSerialization() {
311 <        AtomicLongArray l = new AtomicLongArray(SIZE);
312 <        for (int i = 0; i < SIZE; ++i)
313 <            l.set(i, -i);
314 <
315 <        try {
316 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
317 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
318 <            out.writeObject(l);
326 <            out.close();
327 <
328 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
329 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
330 <            AtomicLongArray r = (AtomicLongArray) in.readObject();
331 <            for (int i = 0; i < SIZE; ++i) {
332 <                assertEquals(l.get(i), r.get(i));
333 <            }
334 <        } catch(Exception e){
335 <            unexpectedException();
310 >    public void testSerialization() throws Exception {
311 >        AtomicLongArray x = new AtomicLongArray(SIZE);
312 >        for (int i = 0; i < SIZE; ++i)
313 >            x.set(i, -i);
314 >        AtomicLongArray y = serialClone(x);
315 >        assertTrue(x != y);
316 >        assertEquals(x.length(), y.length());
317 >        for (int i = 0; i < SIZE; ++i) {
318 >            assertEquals(x.get(i), y.get(i));
319          }
320      }
321  
322      /**
323       * toString returns current value.
324 <     */
324 >     */
325      public void testToString() {
326 <        long[] a = { 17, 3, -42, 99, -7};
326 >        long[] a = { 17, 3, -42, 99, -7 };
327          AtomicLongArray ai = new AtomicLongArray(a);
328          assertEquals(Arrays.toString(a), ai.toString());
329      }
330  
348
331   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines