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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines