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.19 by jsr166, Sat Oct 9 19:30:34 2010 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());
16 >    public static void main(String[] args) {
17 >        junit.textui.TestRunner.run(suite());
18      }
19      public static Test suite() {
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 >            shouldThrow();
41 >        } catch (NullPointerException success) {}
42 >    }
43 >
44 >    /**
45 >     * constructor with array is of same size and has all elements
46 >     */
47 >    public void testConstructor2() {
48 >        int[] a = { 17, 3, -42, 99, -7};
49 >        AtomicIntegerArray ai = new AtomicIntegerArray(a);
50 >        assertEquals(a.length, ai.length());
51 >        for (int i = 0; i < a.length; ++i)
52 >            assertEquals(a[i], ai.get(i));
53 >    }
54 >
55 >    /**
56 >     * get and set for out of bound indices throw IndexOutOfBoundsException
57 >     */
58 >    public void testIndexing() {
59 >        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
60 >        try {
61 >            ai.get(SIZE);
62 >            shouldThrow();
63 >        } catch (IndexOutOfBoundsException success) {
64 >        }
65 >        try {
66 >            ai.get(-1);
67 >            shouldThrow();
68 >        } catch (IndexOutOfBoundsException success) {
69 >        }
70 >        try {
71 >            ai.set(SIZE, 0);
72 >            shouldThrow();
73 >        } catch (IndexOutOfBoundsException success) {
74 >        }
75 >        try {
76 >            ai.set(-1, 0);
77 >            shouldThrow();
78 >        } catch (IndexOutOfBoundsException success) {
79 >        }
80 >    }
81 >
82 >    /**
83 >     * get returns the last value set at index
84 >     */
85 >    public void testGetSet() {
86 >        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
87 >        for (int i = 0; i < SIZE; ++i) {
88              ai.set(i, 1);
89              assertEquals(1,ai.get(i));
90              ai.set(i, 2);
# Line 38 | Line 94 | public class AtomicIntegerArrayTest exte
94          }
95      }
96  
97 <    public void testCompareAndSet(){
98 <        AtomicIntegerArray ai = new AtomicIntegerArray(N);
99 <        for (int i = 0; i < N; ++i) {
97 >    /**
98 >     * get returns the last value lazySet at index by same thread
99 >     */
100 >    public void testGetLazySet() {
101 >        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
102 >        for (int i = 0; i < SIZE; ++i) {
103 >            ai.lazySet(i, 1);
104 >            assertEquals(1,ai.get(i));
105 >            ai.lazySet(i, 2);
106 >            assertEquals(2,ai.get(i));
107 >            ai.lazySet(i, -3);
108 >            assertEquals(-3,ai.get(i));
109 >        }
110 >    }
111 >
112 >    /**
113 >     * compareAndSet succeeds in changing value if equal to expected else fails
114 >     */
115 >    public void testCompareAndSet() {
116 >        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
117 >        for (int i = 0; i < SIZE; ++i) {
118              ai.set(i, 1);
119              assertTrue(ai.compareAndSet(i, 1,2));
120              assertTrue(ai.compareAndSet(i, 2,-4));
121              assertEquals(-4,ai.get(i));
122              assertFalse(ai.compareAndSet(i, -5,7));
123 <            assertFalse((7 == ai.get(i)));
123 >            assertEquals(-4,ai.get(i));
124              assertTrue(ai.compareAndSet(i, -4,7));
125              assertEquals(7,ai.get(i));
126          }
127      }
128  
129 <    public void testWeakCompareAndSet(){
130 <        AtomicIntegerArray ai = new AtomicIntegerArray(N);
131 <        for (int i = 0; i < N; ++i) {
129 >    /**
130 >     * compareAndSet in one thread enables another waiting for value
131 >     * to succeed
132 >     */
133 >    public void testCompareAndSetInMultipleThreads() throws Exception {
134 >        final AtomicIntegerArray a = new AtomicIntegerArray(1);
135 >        a.set(0, 1);
136 >        Thread t = new Thread(new CheckedRunnable() {
137 >            public void realRun() {
138 >                while (!a.compareAndSet(0, 2, 3))
139 >                    Thread.yield();
140 >            }});
141 >
142 >        t.start();
143 >        assertTrue(a.compareAndSet(0, 1, 2));
144 >        t.join(LONG_DELAY_MS);
145 >        assertFalse(t.isAlive());
146 >        assertEquals(a.get(0), 3);
147 >    }
148 >
149 >    /**
150 >     * repeated weakCompareAndSet succeeds in changing value when equal
151 >     * to expected
152 >     */
153 >    public void testWeakCompareAndSet() {
154 >        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
155 >        for (int i = 0; i < SIZE; ++i) {
156              ai.set(i, 1);
157 <            while(!ai.weakCompareAndSet(i, 1,2));
158 <            while(!ai.weakCompareAndSet(i, 2,-4));
157 >            while (!ai.weakCompareAndSet(i, 1,2));
158 >            while (!ai.weakCompareAndSet(i, 2,-4));
159              assertEquals(-4,ai.get(i));
160 <            while(!ai.weakCompareAndSet(i, -4,7));
160 >            while (!ai.weakCompareAndSet(i, -4,7));
161              assertEquals(7,ai.get(i));
162          }
163      }
164  
165 <    public void testGetAndSet(){
166 <        AtomicIntegerArray ai = new AtomicIntegerArray(N);
167 <        for (int i = 0; i < N; ++i) {
165 >    /**
166 >     * getAndSet returns previous value and sets to given value at given index
167 >     */
168 >    public void testGetAndSet() {
169 >        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
170 >        for (int i = 0; i < SIZE; ++i) {
171              ai.set(i, 1);
172              assertEquals(1,ai.getAndSet(i,0));
173              assertEquals(0,ai.getAndSet(i,-10));
# Line 74 | Line 175 | public class AtomicIntegerArrayTest exte
175          }
176      }
177  
178 <    public void testGetAndAdd(){
179 <        AtomicIntegerArray ai = new AtomicIntegerArray(N);
180 <        for (int i = 0; i < N; ++i) {
178 >    /**
179 >     * getAndAdd returns previous value and adds given value
180 >     */
181 >    public void testGetAndAdd() {
182 >        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
183 >        for (int i = 0; i < SIZE; ++i) {
184              ai.set(i, 1);
185              assertEquals(1,ai.getAndAdd(i,2));
186              assertEquals(3,ai.get(i));
# Line 85 | Line 189 | public class AtomicIntegerArrayTest exte
189          }
190      }
191  
192 <    public void testGetAndDecrement(){
193 <        AtomicIntegerArray ai = new AtomicIntegerArray(N);
194 <        for (int i = 0; i < N; ++i) {
192 >    /**
193 >     * getAndDecrement returns previous value and decrements
194 >     */
195 >    public void testGetAndDecrement() {
196 >        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
197 >        for (int i = 0; i < SIZE; ++i) {
198              ai.set(i, 1);
199              assertEquals(1,ai.getAndDecrement(i));
200              assertEquals(0,ai.getAndDecrement(i));
# Line 95 | Line 202 | public class AtomicIntegerArrayTest exte
202          }
203      }
204  
205 <    public void testGetAndIncrement(){
206 <        AtomicIntegerArray ai = new AtomicIntegerArray(N);
207 <        for (int i = 0; i < N; ++i) {
205 >    /**
206 >     * getAndIncrement returns previous value and increments
207 >     */
208 >    public void testGetAndIncrement() {
209 >        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
210 >        for (int i = 0; i < SIZE; ++i) {
211              ai.set(i, 1);
212              assertEquals(1,ai.getAndIncrement(i));
213              assertEquals(2,ai.get(i));
# Line 109 | Line 219 | public class AtomicIntegerArrayTest exte
219          }
220      }
221  
222 +    /**
223 +     * addAndGet adds given value to current, and returns current value
224 +     */
225      public void testAddAndGet() {
226 <        AtomicIntegerArray ai = new AtomicIntegerArray(N);
227 <        for (int i = 0; i < N; ++i) {
226 >        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
227 >        for (int i = 0; i < SIZE; ++i) {
228              ai.set(i, 1);
229              assertEquals(3,ai.addAndGet(i,2));
230              assertEquals(3,ai.get(i));
# Line 120 | Line 233 | public class AtomicIntegerArrayTest exte
233          }
234      }
235  
236 <    public void testDecrementAndGet(){
237 <        AtomicIntegerArray ai = new AtomicIntegerArray(N);
238 <        for (int i = 0; i < N; ++i) {
236 >    /**
237 >     * decrementAndGet decrements and returns current value
238 >     */
239 >    public void testDecrementAndGet() {
240 >        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
241 >        for (int i = 0; i < SIZE; ++i) {
242              ai.set(i, 1);
243              assertEquals(0,ai.decrementAndGet(i));
244              assertEquals(-1,ai.decrementAndGet(i));
# Line 131 | Line 247 | public class AtomicIntegerArrayTest exte
247          }
248      }
249  
250 +    /**
251 +     * incrementAndGet increments and returns current value
252 +     */
253      public void testIncrementAndGet() {
254 <        AtomicIntegerArray ai = new AtomicIntegerArray(N);
255 <        for (int i = 0; i < N; ++i) {
254 >        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
255 >        for (int i = 0; i < SIZE; ++i) {
256              ai.set(i, 1);
257              assertEquals(2,ai.incrementAndGet(i));
258              assertEquals(2,ai.get(i));
# Line 145 | Line 264 | public class AtomicIntegerArrayTest exte
264          }
265      }
266  
267 <    public void testSerialization() {
149 <        AtomicIntegerArray l = new AtomicIntegerArray(N);
150 <        for (int i = 0; i < N; ++i)
151 <            l.set(i, -i);
267 >    static final int COUNTDOWN = 100000;
268  
269 <        try {
270 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
271 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
272 <            out.writeObject(l);
273 <            out.close();
274 <
275 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
276 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
277 <            AtomicIntegerArray r = (AtomicIntegerArray) in.readObject();
278 <            for (int i = 0; i < N; ++i) {
279 <                assertEquals(l.get(i), r.get(i));
269 >    class Counter extends CheckedRunnable {
270 >        final AtomicIntegerArray ai;
271 >        volatile int counts;
272 >        Counter(AtomicIntegerArray a) { ai = a; }
273 >        public void realRun() {
274 >            for (;;) {
275 >                boolean done = true;
276 >                for (int i = 0; i < ai.length(); ++i) {
277 >                    int v = ai.get(i);
278 >                    assertTrue(v >= 0);
279 >                    if (v != 0) {
280 >                        done = false;
281 >                        if (ai.compareAndSet(i, v, v-1))
282 >                            ++counts;
283 >                    }
284 >                }
285 >                if (done)
286 >                    break;
287              }
165        } catch(Exception e){
166            e.printStackTrace();
167            fail("unexpected exception");
288          }
289      }
290  
291 +    /**
292 +     * Multiple threads using same array of counters successfully
293 +     * update a number of times equal to total count
294 +     */
295 +    public void testCountingInMultipleThreads() throws InterruptedException {
296 +        final AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
297 +        for (int i = 0; i < SIZE; ++i)
298 +            ai.set(i, COUNTDOWN);
299 +        Counter c1 = new Counter(ai);
300 +        Counter c2 = new Counter(ai);
301 +        Thread t1 = new Thread(c1);
302 +        Thread t2 = new Thread(c2);
303 +        t1.start();
304 +        t2.start();
305 +        t1.join();
306 +        t2.join();
307 +        assertEquals(c1.counts+c2.counts, SIZE * COUNTDOWN);
308 +    }
309 +
310 +
311 +    /**
312 +     * a deserialized serialized array holds same values
313 +     */
314 +    public void testSerialization() throws Exception {
315 +        AtomicIntegerArray l = new AtomicIntegerArray(SIZE);
316 +        for (int i = 0; i < SIZE; ++i)
317 +            l.set(i, -i);
318 +
319 +        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
320 +        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
321 +        out.writeObject(l);
322 +        out.close();
323 +
324 +        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
325 +        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
326 +        AtomicIntegerArray r = (AtomicIntegerArray) in.readObject();
327 +        for (int i = 0; i < SIZE; ++i) {
328 +            assertEquals(l.get(i), r.get(i));
329 +        }
330 +    }
331 +
332 +
333 +    /**
334 +     * toString returns current value.
335 +     */
336 +    public void testToString() {
337 +        int[] a = { 17, 3, -42, 99, -7};
338 +        AtomicIntegerArray ai = new AtomicIntegerArray(a);
339 +        assertEquals(Arrays.toString(a), ai.toString());
340 +    }
341 +
342   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines