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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines