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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines