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.6 by dl, Sat Dec 27 19:26:43 2003 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  
13 < public class AtomicIntegerArrayTest extends TestCase
12 < {
13 <    static final int N = 10;
13 > public class AtomicIntegerArrayTest extends JSR166TestCase {
14  
15      public static void main (String[] args) {
16          junit.textui.TestRunner.run (suite());
# Line 19 | Line 19 | public class AtomicIntegerArrayTest exte
19          return new TestSuite(AtomicIntegerArrayTest.class);
20      }
21  
22 <    public void testConstructor(){
23 <        AtomicIntegerArray ai = new AtomicIntegerArray(N);
24 <        for (int i = 0; i < N; ++i)
22 >
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 >     * get and set for out of bound indices throw IndexOutOfBoundsException
34 >     */
35 >    public void testIndexing(){
36 >        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
37 >        try {
38 >            ai.get(SIZE);
39 >        } catch(IndexOutOfBoundsException success){
40 >        }
41 >        try {
42 >            ai.get(-1);
43 >        } catch(IndexOutOfBoundsException success){
44 >        }
45 >        try {
46 >            ai.set(SIZE, 0);
47 >        } catch(IndexOutOfBoundsException success){
48 >        }
49 >        try {
50 >            ai.set(-1, 0);
51 >        } catch(IndexOutOfBoundsException success){
52 >        }
53 >    }
54 >
55 >    /**
56 >     * get returns the last value set at index
57 >     */
58 >    public void testGetSet() {
59 >        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
60 >        for (int i = 0; i < SIZE; ++i) {
61              ai.set(i, 1);
62              assertEquals(1,ai.get(i));
63              ai.set(i, 2);
# Line 37 | Line 67 | public class AtomicIntegerArrayTest exte
67          }
68      }
69  
70 <    public void testCompareAndSet(){
71 <        AtomicIntegerArray ai = new AtomicIntegerArray(N);
72 <        for (int i = 0; i < N; ++i) {
70 >    /**
71 >     * compareAndSet succeeds in changing value if equal to expected else fails
72 >     */
73 >    public void testCompareAndSet() {
74 >        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
75 >        for (int i = 0; i < SIZE; ++i) {
76              ai.set(i, 1);
77              assertTrue(ai.compareAndSet(i, 1,2));
78              assertTrue(ai.compareAndSet(i, 2,-4));
# Line 51 | Line 84 | public class AtomicIntegerArrayTest exte
84          }
85      }
86  
87 <    public void testWeakCompareAndSet(){
88 <        AtomicIntegerArray ai = new AtomicIntegerArray(N);
89 <        for (int i = 0; i < N; ++i) {
87 >    /**
88 >     * compareAndSet in one thread enables another waiting for value
89 >     * to succeed
90 >     */
91 >    public void testCompareAndSetInMultipleThreads() {
92 >        final AtomicIntegerArray a = new AtomicIntegerArray(1);
93 >        a.set(0, 1);
94 >        Thread t = new Thread(new Runnable() {
95 >                public void run() {
96 >                    while(!a.compareAndSet(0, 2, 3)) Thread.yield();
97 >                }});
98 >        try {
99 >            t.start();
100 >            assertTrue(a.compareAndSet(0, 1, 2));
101 >            t.join(LONG_DELAY_MS);
102 >            assertFalse(t.isAlive());
103 >            assertEquals(a.get(0), 3);
104 >        }
105 >        catch(Exception e) {
106 >            unexpectedException();
107 >        }
108 >    }
109 >
110 >    /**
111 >     * repeated weakCompareAndSet succeeds in changing value when equal
112 >     * to expected
113 >     */
114 >    public void testWeakCompareAndSet() {
115 >        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
116 >        for (int i = 0; i < SIZE; ++i) {
117              ai.set(i, 1);
118              while(!ai.weakCompareAndSet(i, 1,2));
119              while(!ai.weakCompareAndSet(i, 2,-4));
# Line 63 | Line 123 | public class AtomicIntegerArrayTest exte
123          }
124      }
125  
126 <    public void testGetAndSet(){
127 <        AtomicIntegerArray ai = new AtomicIntegerArray(N);
128 <        for (int i = 0; i < N; ++i) {
126 >    /**
127 >     *  getAndSet returns previous value and sets to given value at given index
128 >     */
129 >    public void testGetAndSet() {
130 >        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
131 >        for (int i = 0; i < SIZE; ++i) {
132              ai.set(i, 1);
133              assertEquals(1,ai.getAndSet(i,0));
134              assertEquals(0,ai.getAndSet(i,-10));
# Line 73 | Line 136 | public class AtomicIntegerArrayTest exte
136          }
137      }
138  
139 <    public void testGetAndAdd(){
140 <        AtomicIntegerArray ai = new AtomicIntegerArray(N);
141 <        for (int i = 0; i < N; ++i) {
139 >    /**
140 >     *  getAndAdd returns previous value and adds given value
141 >     */
142 >    public void testGetAndAdd() {
143 >        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
144 >        for (int i = 0; i < SIZE; ++i) {
145              ai.set(i, 1);
146              assertEquals(1,ai.getAndAdd(i,2));
147              assertEquals(3,ai.get(i));
# Line 84 | Line 150 | public class AtomicIntegerArrayTest exte
150          }
151      }
152  
153 <    public void testGetAndDecrement(){
154 <        AtomicIntegerArray ai = new AtomicIntegerArray(N);
155 <        for (int i = 0; i < N; ++i) {
153 >    /**
154 >     * getAndDecrement returns previous value and decrements
155 >     */
156 >    public void testGetAndDecrement() {
157 >        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
158 >        for (int i = 0; i < SIZE; ++i) {
159              ai.set(i, 1);
160              assertEquals(1,ai.getAndDecrement(i));
161              assertEquals(0,ai.getAndDecrement(i));
# Line 94 | Line 163 | public class AtomicIntegerArrayTest exte
163          }
164      }
165  
166 <    public void testGetAndIncrement(){
167 <        AtomicIntegerArray ai = new AtomicIntegerArray(N);
168 <        for (int i = 0; i < N; ++i) {
166 >    /**
167 >     * getAndIncrement returns previous value and increments
168 >     */
169 >    public void testGetAndIncrement() {
170 >        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
171 >        for (int i = 0; i < SIZE; ++i) {
172              ai.set(i, 1);
173              assertEquals(1,ai.getAndIncrement(i));
174              assertEquals(2,ai.get(i));
# Line 108 | Line 180 | public class AtomicIntegerArrayTest exte
180          }
181      }
182  
183 +    /**
184 +     *  addAndGet adds given value to current, and returns current value
185 +     */
186      public void testAddAndGet() {
187 <        AtomicIntegerArray ai = new AtomicIntegerArray(N);
188 <        for (int i = 0; i < N; ++i) {
187 >        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
188 >        for (int i = 0; i < SIZE; ++i) {
189              ai.set(i, 1);
190              assertEquals(3,ai.addAndGet(i,2));
191              assertEquals(3,ai.get(i));
# Line 119 | Line 194 | public class AtomicIntegerArrayTest exte
194          }
195      }
196  
197 <    public void testDecrementAndGet(){
198 <        AtomicIntegerArray ai = new AtomicIntegerArray(N);
199 <        for (int i = 0; i < N; ++i) {
197 >    /**
198 >     * decrementAndGet decrements and returns current value
199 >     */
200 >    public void testDecrementAndGet() {
201 >        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
202 >        for (int i = 0; i < SIZE; ++i) {
203              ai.set(i, 1);
204              assertEquals(0,ai.decrementAndGet(i));
205              assertEquals(-1,ai.decrementAndGet(i));
# Line 130 | Line 208 | public class AtomicIntegerArrayTest exte
208          }
209      }
210  
211 +    /**
212 +     *  incrementAndGet increments and returns current value
213 +     */
214      public void testIncrementAndGet() {
215 <        AtomicIntegerArray ai = new AtomicIntegerArray(N);
216 <        for (int i = 0; i < N; ++i) {
215 >        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
216 >        for (int i = 0; i < SIZE; ++i) {
217              ai.set(i, 1);
218              assertEquals(2,ai.incrementAndGet(i));
219              assertEquals(2,ai.get(i));
# Line 144 | Line 225 | public class AtomicIntegerArrayTest exte
225          }
226      }
227  
228 +    static final int COUNTDOWN = 100000;
229 +    
230 +    class Counter implements Runnable {
231 +        final AtomicIntegerArray ai;
232 +        volatile int counts;
233 +        Counter(AtomicIntegerArray a) { ai = a; }
234 +        public void run() {
235 +            for (;;) {
236 +                boolean done = true;
237 +                for (int i = 0; i < ai.length(); ++i) {
238 +                    int v = ai.get(i);
239 +                    threadAssertTrue(v >= 0);
240 +                    if (v != 0) {
241 +                        done = false;
242 +                        if (ai.compareAndSet(i, v, v-1))
243 +                            ++counts;
244 +                    }
245 +                }
246 +                if (done)
247 +                    break;
248 +            }
249 +        }
250 +    }
251 +
252 +    /**
253 +     * Multiple threads using same array of counters successfully
254 +     * update a number of times equal to total count
255 +     */
256 +    public void testCountingInMultipleThreads() {
257 +        try {
258 +            final AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
259 +            for (int i = 0; i < SIZE; ++i)
260 +                ai.set(i, COUNTDOWN);
261 +            Counter c1 = new Counter(ai);
262 +            Counter c2 = new Counter(ai);
263 +            Thread t1 = new Thread(c1);
264 +            Thread t2 = new Thread(c2);
265 +            t1.start();
266 +            t2.start();
267 +            t1.join();
268 +            t2.join();
269 +            assertEquals(c1.counts+c2.counts, SIZE * COUNTDOWN);
270 +        }
271 +        catch(InterruptedException ie) {
272 +            unexpectedException();
273 +        }
274 +    }
275 +
276 +
277 +    /**
278 +     * a deserialized serialized array holds same values
279 +     */
280 +    public void testSerialization() {
281 +        AtomicIntegerArray l = new AtomicIntegerArray(SIZE);
282 +        for (int i = 0; i < SIZE; ++i)
283 +            l.set(i, -i);
284 +
285 +        try {
286 +            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
287 +            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
288 +            out.writeObject(l);
289 +            out.close();
290 +
291 +            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
292 +            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
293 +            AtomicIntegerArray r = (AtomicIntegerArray) in.readObject();
294 +            for (int i = 0; i < SIZE; ++i) {
295 +                assertEquals(l.get(i), r.get(i));
296 +            }
297 +        } catch(Exception e){
298 +            e.printStackTrace();
299 +            unexpectedException();
300 +        }
301 +    }
302 +
303   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines