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

Comparing jsr166/src/test/tck/AtomicIntegerArrayTest.java (file contents):
Revision 1.2 by dl, Sun Sep 7 20:39:11 2003 UTC vs.
Revision 1.11 by jsr166, Mon Nov 2 20:28:31 2009 UTC

# Line 1 | Line 1
1   /*
2 < * Written by members of JCP JSR-166 Expert Group and released to the
3 < * public domain. Use, modify, and redistribute this code in any way
4 < * without acknowledgement. Other contributors include Andrew Wright,
5 < * Jeffrey Hayes, Pat Fischer, Mike Judd.
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.
7   */
8  
9   import junit.framework.*;
10   import java.util.concurrent.atomic.*;
11   import java.io.*;
12 + import java.util.*;
13  
14 < public class AtomicIntegerArrayTest extends TestCase
13 < {
14 <    static final int N = 10;
14 > public class AtomicIntegerArrayTest extends JSR166TestCase {
15  
16      public static void main (String[] args) {
17          junit.textui.TestRunner.run (suite());
# Line 20 | Line 20 | public class AtomicIntegerArrayTest exte
20          return new TestSuite(AtomicIntegerArrayTest.class);
21      }
22  
23 <    public void testConstructor(){
24 <        AtomicIntegerArray ai = new AtomicIntegerArray(N);
25 <        for (int i = 0; i < N; ++i)
23 >
24 >    /**
25 >     * constructor creates array of given size with all elements zero
26 >     */
27 >    public void testConstructor() {
28 >        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
29 >        for (int i = 0; i < SIZE; ++i)
30              assertEquals(0,ai.get(i));
31      }
32  
33 <    public void testGetSet(){
34 <        AtomicIntegerArray ai = new AtomicIntegerArray(N);
35 <        for (int i = 0; i < N; ++i) {
33 >    /**
34 >     * constructor with null array throws NPE
35 >     */
36 >    public void testConstructor2NPE() {
37 >        try {
38 >            int[] a = null;
39 >            AtomicIntegerArray ai = new AtomicIntegerArray(a);
40 >        } catch (NullPointerException success) {
41 >        } catch (Exception ex) {
42 >            unexpectedException();
43 >        }
44 >    }
45 >
46 >    /**
47 >     * constructor with array is of same size and has all elements
48 >     */
49 >    public void testConstructor2() {
50 >        int[] a = { 17, 3, -42, 99, -7};
51 >        AtomicIntegerArray ai = new AtomicIntegerArray(a);
52 >        assertEquals(a.length, ai.length());
53 >        for (int i = 0; i < a.length; ++i)
54 >            assertEquals(a[i], ai.get(i));
55 >    }
56 >
57 >    /**
58 >     * get and set for out of bound indices throw IndexOutOfBoundsException
59 >     */
60 >    public void testIndexing(){
61 >        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
62 >        try {
63 >            ai.get(SIZE);
64 >        } catch(IndexOutOfBoundsException success){
65 >        }
66 >        try {
67 >            ai.get(-1);
68 >        } catch(IndexOutOfBoundsException success){
69 >        }
70 >        try {
71 >            ai.set(SIZE, 0);
72 >        } catch(IndexOutOfBoundsException success){
73 >        }
74 >        try {
75 >            ai.set(-1, 0);
76 >        } catch(IndexOutOfBoundsException success){
77 >        }
78 >    }
79 >
80 >    /**
81 >     * get returns the last value set at index
82 >     */
83 >    public void testGetSet() {
84 >        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
85 >        for (int i = 0; i < SIZE; ++i) {
86              ai.set(i, 1);
87              assertEquals(1,ai.get(i));
88              ai.set(i, 2);
# Line 38 | Line 92 | public class AtomicIntegerArrayTest exte
92          }
93      }
94  
95 <    public void testCompareAndSet(){
96 <        AtomicIntegerArray ai = new AtomicIntegerArray(N);
97 <        for (int i = 0; i < N; ++i) {
95 >    /**
96 >     * get returns the last value lazySet at index by same thread
97 >     */
98 >    public void testGetLazySet() {
99 >        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
100 >        for (int i = 0; i < SIZE; ++i) {
101 >            ai.lazySet(i, 1);
102 >            assertEquals(1,ai.get(i));
103 >            ai.lazySet(i, 2);
104 >            assertEquals(2,ai.get(i));
105 >            ai.lazySet(i, -3);
106 >            assertEquals(-3,ai.get(i));
107 >        }
108 >    }
109 >
110 >    /**
111 >     * compareAndSet succeeds in changing value if equal to expected else fails
112 >     */
113 >    public void testCompareAndSet() {
114 >        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
115 >        for (int i = 0; i < SIZE; ++i) {
116              ai.set(i, 1);
117              assertTrue(ai.compareAndSet(i, 1,2));
118              assertTrue(ai.compareAndSet(i, 2,-4));
# Line 52 | Line 124 | public class AtomicIntegerArrayTest exte
124          }
125      }
126  
127 <    public void testWeakCompareAndSet(){
128 <        AtomicIntegerArray ai = new AtomicIntegerArray(N);
129 <        for (int i = 0; i < N; ++i) {
127 >    /**
128 >     * compareAndSet in one thread enables another waiting for value
129 >     * to succeed
130 >     */
131 >    public void testCompareAndSetInMultipleThreads() {
132 >        final AtomicIntegerArray a = new AtomicIntegerArray(1);
133 >        a.set(0, 1);
134 >        Thread t = new Thread(new Runnable() {
135 >                public void run() {
136 >                    while(!a.compareAndSet(0, 2, 3)) Thread.yield();
137 >                }});
138 >        try {
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 >        catch(Exception e) {
146 >            unexpectedException();
147 >        }
148 >    }
149 >
150 >    /**
151 >     * repeated weakCompareAndSet succeeds in changing value when equal
152 >     * to expected
153 >     */
154 >    public void testWeakCompareAndSet() {
155 >        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
156 >        for (int i = 0; i < SIZE; ++i) {
157              ai.set(i, 1);
158              while(!ai.weakCompareAndSet(i, 1,2));
159              while(!ai.weakCompareAndSet(i, 2,-4));
# Line 64 | Line 163 | public class AtomicIntegerArrayTest exte
163          }
164      }
165  
166 <    public void testGetAndSet(){
167 <        AtomicIntegerArray ai = new AtomicIntegerArray(N);
168 <        for (int i = 0; i < N; ++i) {
166 >    /**
167 >     *  getAndSet returns previous value and sets to given value at given index
168 >     */
169 >    public void testGetAndSet() {
170 >        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
171 >        for (int i = 0; i < SIZE; ++i) {
172              ai.set(i, 1);
173              assertEquals(1,ai.getAndSet(i,0));
174              assertEquals(0,ai.getAndSet(i,-10));
# Line 74 | Line 176 | public class AtomicIntegerArrayTest exte
176          }
177      }
178  
179 <    public void testGetAndAdd(){
180 <        AtomicIntegerArray ai = new AtomicIntegerArray(N);
181 <        for (int i = 0; i < N; ++i) {
179 >    /**
180 >     *  getAndAdd returns previous value and adds given value
181 >     */
182 >    public void testGetAndAdd() {
183 >        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
184 >        for (int i = 0; i < SIZE; ++i) {
185              ai.set(i, 1);
186              assertEquals(1,ai.getAndAdd(i,2));
187              assertEquals(3,ai.get(i));
# Line 85 | Line 190 | public class AtomicIntegerArrayTest exte
190          }
191      }
192  
193 <    public void testGetAndDecrement(){
194 <        AtomicIntegerArray ai = new AtomicIntegerArray(N);
195 <        for (int i = 0; i < N; ++i) {
193 >    /**
194 >     * getAndDecrement returns previous value and decrements
195 >     */
196 >    public void testGetAndDecrement() {
197 >        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
198 >        for (int i = 0; i < SIZE; ++i) {
199              ai.set(i, 1);
200              assertEquals(1,ai.getAndDecrement(i));
201              assertEquals(0,ai.getAndDecrement(i));
# Line 95 | Line 203 | public class AtomicIntegerArrayTest exte
203          }
204      }
205  
206 <    public void testGetAndIncrement(){
207 <        AtomicIntegerArray ai = new AtomicIntegerArray(N);
208 <        for (int i = 0; i < N; ++i) {
206 >    /**
207 >     * getAndIncrement returns previous value and increments
208 >     */
209 >    public void testGetAndIncrement() {
210 >        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
211 >        for (int i = 0; i < SIZE; ++i) {
212              ai.set(i, 1);
213              assertEquals(1,ai.getAndIncrement(i));
214              assertEquals(2,ai.get(i));
# Line 109 | Line 220 | public class AtomicIntegerArrayTest exte
220          }
221      }
222  
223 +    /**
224 +     *  addAndGet adds given value to current, and returns current value
225 +     */
226      public void testAddAndGet() {
227 <        AtomicIntegerArray ai = new AtomicIntegerArray(N);
228 <        for (int i = 0; i < N; ++i) {
227 >        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
228 >        for (int i = 0; i < SIZE; ++i) {
229              ai.set(i, 1);
230              assertEquals(3,ai.addAndGet(i,2));
231              assertEquals(3,ai.get(i));
# Line 120 | Line 234 | public class AtomicIntegerArrayTest exte
234          }
235      }
236  
237 <    public void testDecrementAndGet(){
238 <        AtomicIntegerArray ai = new AtomicIntegerArray(N);
239 <        for (int i = 0; i < N; ++i) {
237 >    /**
238 >     * decrementAndGet decrements and returns current value
239 >     */
240 >    public void testDecrementAndGet() {
241 >        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
242 >        for (int i = 0; i < SIZE; ++i) {
243              ai.set(i, 1);
244              assertEquals(0,ai.decrementAndGet(i));
245              assertEquals(-1,ai.decrementAndGet(i));
# Line 131 | Line 248 | public class AtomicIntegerArrayTest exte
248          }
249      }
250  
251 +    /**
252 +     *  incrementAndGet increments and returns current value
253 +     */
254      public void testIncrementAndGet() {
255 <        AtomicIntegerArray ai = new AtomicIntegerArray(N);
256 <        for (int i = 0; i < N; ++i) {
255 >        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
256 >        for (int i = 0; i < SIZE; ++i) {
257              ai.set(i, 1);
258              assertEquals(2,ai.incrementAndGet(i));
259              assertEquals(2,ai.get(i));
# Line 145 | Line 265 | public class AtomicIntegerArrayTest exte
265          }
266      }
267  
268 +    static final int COUNTDOWN = 100000;
269 +
270 +    class Counter implements Runnable {
271 +        final AtomicIntegerArray ai;
272 +        volatile int counts;
273 +        Counter(AtomicIntegerArray a) { ai = a; }
274 +        public void run() {
275 +            for (;;) {
276 +                boolean done = true;
277 +                for (int i = 0; i < ai.length(); ++i) {
278 +                    int v = ai.get(i);
279 +                    threadAssertTrue(v >= 0);
280 +                    if (v != 0) {
281 +                        done = false;
282 +                        if (ai.compareAndSet(i, v, v-1))
283 +                            ++counts;
284 +                    }
285 +                }
286 +                if (done)
287 +                    break;
288 +            }
289 +        }
290 +    }
291 +
292 +    /**
293 +     * Multiple threads using same array of counters successfully
294 +     * update a number of times equal to total count
295 +     */
296 +    public void testCountingInMultipleThreads() {
297 +        try {
298 +            final AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
299 +            for (int i = 0; i < SIZE; ++i)
300 +                ai.set(i, COUNTDOWN);
301 +            Counter c1 = new Counter(ai);
302 +            Counter c2 = new Counter(ai);
303 +            Thread t1 = new Thread(c1);
304 +            Thread t2 = new Thread(c2);
305 +            t1.start();
306 +            t2.start();
307 +            t1.join();
308 +            t2.join();
309 +            assertEquals(c1.counts+c2.counts, SIZE * COUNTDOWN);
310 +        }
311 +        catch(InterruptedException ie) {
312 +            unexpectedException();
313 +        }
314 +    }
315 +
316 +
317 +    /**
318 +     * a deserialized serialized array holds same values
319 +     */
320      public void testSerialization() {
321 <        AtomicIntegerArray l = new AtomicIntegerArray(N);
322 <        for (int i = 0; i < N; ++i)
321 >        AtomicIntegerArray l = new AtomicIntegerArray(SIZE);
322 >        for (int i = 0; i < SIZE; ++i)
323              l.set(i, -i);
324  
325          try {
# Line 159 | Line 331 | public class AtomicIntegerArrayTest exte
331              ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
332              ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
333              AtomicIntegerArray r = (AtomicIntegerArray) in.readObject();
334 <            for (int i = 0; i < N; ++i) {
334 >            for (int i = 0; i < SIZE; ++i) {
335                  assertEquals(l.get(i), r.get(i));
336              }
337          } catch(Exception e){
338              e.printStackTrace();
339 <            fail("unexpected exception");
339 >            unexpectedException();
340          }
341      }
342  
343 +
344 +    /**
345 +     * toString returns current value.
346 +     */
347 +    public void testToString() {
348 +        int[] a = { 17, 3, -42, 99, -7};
349 +        AtomicIntegerArray ai = new AtomicIntegerArray(a);
350 +        assertEquals(Arrays.toString(a), ai.toString());
351 +    }
352 +
353   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines