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.1 by dl, Sun Aug 31 19:24:53 2003 UTC vs.
Revision 1.7 by dl, Thu Jan 8 01:29:46 2004 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 AtomicLongArrayTest extends TestCase
12 < {
13 <    static final int N = 10;
14 <
13 > public class AtomicLongArrayTest extends JSR166TestCase {
14      public static void main (String[] args) {
15          junit.textui.TestRunner.run (suite());
16      }
# Line 19 | Line 18 | public class AtomicLongArrayTest extends
18          return new TestSuite(AtomicLongArrayTest.class);
19      }
20  
21 +    /**
22 +     * constructor creates array of given size with all elements zero
23 +     */
24      public void testConstructor(){
25 <        AtomicLongArray ai = new AtomicLongArray(N);
26 <        for (int i = 0; i < N; ++i)
25 >        AtomicLongArray ai = new AtomicLongArray(SIZE);
26 >        for (int i = 0; i < SIZE; ++i)
27              assertEquals(0,ai.get(i));
28      }
29  
30 +    /**
31 +     * constructor with null array throws NPE
32 +     */
33 +    public void testConstructor2NPE() {
34 +        try {
35 +            long[] a = null;
36 +            AtomicLongArray ai = new AtomicLongArray(a);
37 +        } catch (NullPointerException success) {
38 +        } catch (Exception ex) {
39 +            unexpectedException();
40 +        }
41 +    }
42 +
43 +    /**
44 +     * constructor with array is of same size and has all elements
45 +     */
46 +    public void testConstructor2() {
47 +        long[] a = { 17L, 3L, -42L, 99L, -7L};
48 +        AtomicLongArray ai = new AtomicLongArray(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 +        AtomicLongArray ai = new AtomicLongArray(SIZE);
59 +        try {
60 +            ai.get(SIZE);
61 +        } catch(IndexOutOfBoundsException success){
62 +        }
63 +        try {
64 +            ai.get(-1);
65 +        } catch(IndexOutOfBoundsException success){
66 +        }
67 +        try {
68 +            ai.set(SIZE, 0);
69 +        } catch(IndexOutOfBoundsException success){
70 +        }
71 +        try {
72 +            ai.set(-1, 0);
73 +        } catch(IndexOutOfBoundsException success){
74 +        }
75 +    }
76 +
77 +    /**
78 +     * get returns the last value set at index
79 +     */
80      public void testGetSet(){
81 <        AtomicLongArray ai = new AtomicLongArray(N);
82 <        for (int i = 0; i < N; ++i) {
81 >        AtomicLongArray ai = new AtomicLongArray(SIZE);
82 >        for (int i = 0; i < SIZE; ++i) {
83              ai.set(i, 1);
84              assertEquals(1,ai.get(i));
85              ai.set(i, 2);
# Line 37 | Line 89 | public class AtomicLongArrayTest extends
89          }
90      }
91  
92 +    /**
93 +     * compareAndSet succeeds in changing value if equal to expected else fails
94 +     */
95      public void testCompareAndSet(){
96 <        AtomicLongArray ai = new AtomicLongArray(N);
97 <        for (int i = 0; i < N; ++i) {
96 >        AtomicLongArray ai = new AtomicLongArray(SIZE);
97 >        for (int i = 0; i < SIZE; ++i) {
98              ai.set(i, 1);
99              assertTrue(ai.compareAndSet(i, 1,2));
100              assertTrue(ai.compareAndSet(i, 2,-4));
# Line 51 | Line 106 | public class AtomicLongArrayTest extends
106          }
107      }
108  
109 +    /**
110 +     * compareAndSet in one thread enables another waiting for value
111 +     * to succeed
112 +     */
113 +    public void testCompareAndSetInMultipleThreads() {
114 +        final AtomicLongArray a = new AtomicLongArray(1);
115 +        a.set(0, 1);
116 +        Thread t = new Thread(new Runnable() {
117 +                public void run() {
118 +                    while(!a.compareAndSet(0, 2, 3)) Thread.yield();
119 +                }});
120 +        try {
121 +            t.start();
122 +            assertTrue(a.compareAndSet(0, 1, 2));
123 +            t.join(LONG_DELAY_MS);
124 +            assertFalse(t.isAlive());
125 +            assertEquals(a.get(0), 3);
126 +        }
127 +        catch(Exception e) {
128 +            unexpectedException();
129 +        }
130 +    }
131 +
132 +    /**
133 +     * repeated weakCompareAndSet succeeds in changing value when equal
134 +     * to expected
135 +     */
136      public void testWeakCompareAndSet(){
137 <        AtomicLongArray ai = new AtomicLongArray(N);
138 <        for (int i = 0; i < N; ++i) {
137 >        AtomicLongArray ai = new AtomicLongArray(SIZE);
138 >        for (int i = 0; i < SIZE; ++i) {
139              ai.set(i, 1);
140              while(!ai.weakCompareAndSet(i, 1,2));
141              while(!ai.weakCompareAndSet(i, 2,-4));
# Line 63 | Line 145 | public class AtomicLongArrayTest extends
145          }
146      }
147  
148 +    /**
149 +     *  getAndSet returns previous value and sets to given value at given index
150 +     */
151      public void testGetAndSet(){
152 <        AtomicLongArray ai = new AtomicLongArray(N);
153 <        for (int i = 0; i < N; ++i) {
152 >        AtomicLongArray ai = new AtomicLongArray(SIZE);
153 >        for (int i = 0; i < SIZE; ++i) {
154              ai.set(i, 1);
155              assertEquals(1,ai.getAndSet(i,0));
156              assertEquals(0,ai.getAndSet(i,-10));
# Line 73 | Line 158 | public class AtomicLongArrayTest extends
158          }
159      }
160  
161 +    /**
162 +     *  getAndAdd returns previous value and adds given value
163 +     */
164      public void testGetAndAdd(){
165 <        AtomicLongArray ai = new AtomicLongArray(N);
166 <        for (int i = 0; i < N; ++i) {
165 >        AtomicLongArray ai = new AtomicLongArray(SIZE);
166 >        for (int i = 0; i < SIZE; ++i) {
167              ai.set(i, 1);
168              assertEquals(1,ai.getAndAdd(i,2));
169              assertEquals(3,ai.get(i));
# Line 84 | Line 172 | public class AtomicLongArrayTest extends
172          }
173      }
174  
175 +    /**
176 +     * getAndDecrement returns previous value and decrements
177 +     */
178      public void testGetAndDecrement(){
179 <        AtomicLongArray ai = new AtomicLongArray(N);
180 <        for (int i = 0; i < N; ++i) {
179 >        AtomicLongArray ai = new AtomicLongArray(SIZE);
180 >        for (int i = 0; i < SIZE; ++i) {
181              ai.set(i, 1);
182              assertEquals(1,ai.getAndDecrement(i));
183              assertEquals(0,ai.getAndDecrement(i));
# Line 94 | Line 185 | public class AtomicLongArrayTest extends
185          }
186      }
187  
188 +    /**
189 +     * getAndIncrement returns previous value and increments
190 +     */
191      public void testGetAndIncrement(){
192 <        AtomicLongArray ai = new AtomicLongArray(N);
193 <        for (int i = 0; i < N; ++i) {
192 >        AtomicLongArray ai = new AtomicLongArray(SIZE);
193 >        for (int i = 0; i < SIZE; ++i) {
194              ai.set(i, 1);
195              assertEquals(1,ai.getAndIncrement(i));
196              assertEquals(2,ai.get(i));
# Line 108 | Line 202 | public class AtomicLongArrayTest extends
202          }
203      }
204  
205 +    /**
206 +     *  addAndGet adds given value to current, and returns current value
207 +     */
208      public void testAddAndGet() {
209 <        AtomicLongArray ai = new AtomicLongArray(N);
210 <        for (int i = 0; i < N; ++i) {
209 >        AtomicLongArray ai = new AtomicLongArray(SIZE);
210 >        for (int i = 0; i < SIZE; ++i) {
211              ai.set(i, 1);
212              assertEquals(3,ai.addAndGet(i,2));
213              assertEquals(3,ai.get(i));
# Line 119 | Line 216 | public class AtomicLongArrayTest extends
216          }
217      }
218  
219 +    /**
220 +     * decrementAndGet decrements and returns current value
221 +     */
222      public void testDecrementAndGet(){
223 <        AtomicLongArray ai = new AtomicLongArray(N);
224 <        for (int i = 0; i < N; ++i) {
223 >        AtomicLongArray ai = new AtomicLongArray(SIZE);
224 >        for (int i = 0; i < SIZE; ++i) {
225              ai.set(i, 1);
226              assertEquals(0,ai.decrementAndGet(i));
227              assertEquals(-1,ai.decrementAndGet(i));
# Line 130 | Line 230 | public class AtomicLongArrayTest extends
230          }
231      }
232  
233 +    /**
234 +     * incrementAndGet increments and returns current value
235 +     */
236      public void testIncrementAndGet() {
237 <        AtomicLongArray ai = new AtomicLongArray(N);
238 <        for (int i = 0; i < N; ++i) {
237 >        AtomicLongArray ai = new AtomicLongArray(SIZE);
238 >        for (int i = 0; i < SIZE; ++i) {
239              ai.set(i, 1);
240              assertEquals(2,ai.incrementAndGet(i));
241              assertEquals(2,ai.get(i));
# Line 144 | Line 247 | public class AtomicLongArrayTest extends
247          }
248      }
249  
250 +    static final long COUNTDOWN = 100000;
251 +    
252 +    class Counter implements Runnable {
253 +        final AtomicLongArray ai;
254 +        volatile long counts;
255 +        Counter(AtomicLongArray a) { ai = a; }
256 +        public void run() {
257 +            for (;;) {
258 +                boolean done = true;
259 +                for (int i = 0; i < ai.length(); ++i) {
260 +                    long v = ai.get(i);
261 +                    threadAssertTrue(v >= 0);
262 +                    if (v != 0) {
263 +                        done = false;
264 +                        if (ai.compareAndSet(i, v, v-1))
265 +                            ++counts;
266 +                    }
267 +                }
268 +                if (done)
269 +                    break;
270 +            }
271 +        }
272 +    }
273 +
274 +    /**
275 +     * Multiple threads using same array of counters successfully
276 +     * update a number of times equal to total count
277 +     */
278 +    public void testCountingInMultipleThreads() {
279 +        try {
280 +            final AtomicLongArray ai = new AtomicLongArray(SIZE);
281 +            for (int i = 0; i < SIZE; ++i)
282 +                ai.set(i, COUNTDOWN);
283 +            Counter c1 = new Counter(ai);
284 +            Counter c2 = new Counter(ai);
285 +            Thread t1 = new Thread(c1);
286 +            Thread t2 = new Thread(c2);
287 +            t1.start();
288 +            t2.start();
289 +            t1.join();
290 +            t2.join();
291 +            assertEquals(c1.counts+c2.counts, SIZE * COUNTDOWN);
292 +        }
293 +        catch(InterruptedException ie) {
294 +            unexpectedException();
295 +        }
296 +    }
297 +
298 +    /**
299 +     * a deserialized serialized array holds same values
300 +     */
301 +    public void testSerialization() {
302 +        AtomicLongArray l = new AtomicLongArray(SIZE);
303 +        for (int i = 0; i < SIZE; ++i)
304 +            l.set(i, -i);
305 +
306 +        try {
307 +            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
308 +            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
309 +            out.writeObject(l);
310 +            out.close();
311 +
312 +            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
313 +            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
314 +            AtomicLongArray r = (AtomicLongArray) in.readObject();
315 +            for (int i = 0; i < SIZE; ++i) {
316 +                assertEquals(l.get(i), r.get(i));
317 +            }
318 +        } catch(Exception e){
319 +            unexpectedException();
320 +        }
321 +    }
322 +
323 +
324   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines