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.3 by dl, Sun Sep 14 20:42:40 2003 UTC vs.
Revision 1.13 by jsr166, Tue Nov 17 02:48:16 2009 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 JSR166TestCase {
15      public static void main (String[] args) {
# Line 17 | Line 19 | public class AtomicLongArrayTest extends
19          return new TestSuite(AtomicLongArrayTest.class);
20      }
21  
22 <    public void testConstructor(){
22 >    /**
23 >     * constructor creates array of given size with all elements zero
24 >     */
25 >    public void testConstructor() {
26          AtomicLongArray ai = new AtomicLongArray(SIZE);
27 <        for (int i = 0; i < SIZE; ++i)
27 >        for (int i = 0; i < SIZE; ++i)
28              assertEquals(0,ai.get(i));
29      }
30  
31 <    public void testGetSet(){
32 <        AtomicLongArray ai = new AtomicLongArray(SIZE);
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 >            shouldThrow();
39 >        } catch (NullPointerException success) {}
40 >    }
41 >
42 >    /**
43 >     * constructor with array is of same size and has all elements
44 >     */
45 >    public void testConstructor2() {
46 >        long[] a = { 17L, 3L, -42L, 99L, -7L};
47 >        AtomicLongArray ai = new AtomicLongArray(a);
48 >        assertEquals(a.length, ai.length());
49 >        for (int i = 0; i < a.length; ++i)
50 >            assertEquals(a[i], ai.get(i));
51 >    }
52 >
53 >    /**
54 >     * get and set for out of bound indices throw IndexOutOfBoundsException
55 >     */
56 >    public void testIndexing() {
57 >        AtomicLongArray ai = new AtomicLongArray(SIZE);
58 >        try {
59 >            ai.get(SIZE);
60 >            shouldThrow();
61 >        } catch (IndexOutOfBoundsException success) {
62 >        }
63 >        try {
64 >            ai.get(-1);
65 >            shouldThrow();
66 >        } catch (IndexOutOfBoundsException success) {
67 >        }
68 >        try {
69 >            ai.set(SIZE, 0);
70 >            shouldThrow();
71 >        } catch (IndexOutOfBoundsException success) {
72 >        }
73 >        try {
74 >            ai.set(-1, 0);
75 >            shouldThrow();
76 >        } catch (IndexOutOfBoundsException success) {
77 >        }
78 >    }
79 >
80 >    /**
81 >     * get returns the last value set at index
82 >     */
83 >    public void testGetSet() {
84 >        AtomicLongArray ai = new AtomicLongArray(SIZE);
85          for (int i = 0; i < SIZE; ++i) {
86              ai.set(i, 1);
87              assertEquals(1,ai.get(i));
# Line 35 | Line 92 | public class AtomicLongArrayTest extends
92          }
93      }
94  
95 <    public void testCompareAndSet(){
96 <        AtomicLongArray ai = new AtomicLongArray(SIZE);
95 >    /**
96 >     * get returns the last value lazySet at index by same thread
97 >     */
98 >    public void testGetLazySet() {
99 >        AtomicLongArray ai = new AtomicLongArray(SIZE);
100 >        for (int i = 0; i < SIZE; ++i) {
101 >            ai.lazySet(i, 1);
102 >            assertEquals(1,ai.get(i));
103 >            ai.lazySet(i, 2);
104 >            assertEquals(2,ai.get(i));
105 >            ai.lazySet(i, -3);
106 >            assertEquals(-3,ai.get(i));
107 >        }
108 >    }
109 >
110 >    /**
111 >     * compareAndSet succeeds in changing value if equal to expected else fails
112 >     */
113 >    public void testCompareAndSet() {
114 >        AtomicLongArray ai = new AtomicLongArray(SIZE);
115          for (int i = 0; i < SIZE; ++i) {
116              ai.set(i, 1);
117              assertTrue(ai.compareAndSet(i, 1,2));
# Line 49 | Line 124 | public class AtomicLongArrayTest extends
124          }
125      }
126  
127 <    public void testWeakCompareAndSet(){
128 <        AtomicLongArray ai = new AtomicLongArray(SIZE);
127 >    /**
128 >     * compareAndSet in one thread enables another waiting for value
129 >     * to succeed
130 >     */
131 >    public void testCompareAndSetInMultipleThreads() throws InterruptedException {
132 >        final AtomicLongArray a = new AtomicLongArray(1);
133 >        a.set(0, 1);
134 >        Thread t = new Thread(new Runnable() {
135 >                public void run() {
136 >                    while (!a.compareAndSet(0, 2, 3)) 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));
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));
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(SIZE);
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));
# Line 71 | Line 172 | public class AtomicLongArrayTest extends
172          }
173      }
174  
175 <    public void testGetAndAdd(){
176 <        AtomicLongArray ai = new AtomicLongArray(SIZE);
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));
# Line 82 | Line 186 | public class AtomicLongArrayTest extends
186          }
187      }
188  
189 <    public void testGetAndDecrement(){
190 <        AtomicLongArray ai = new AtomicLongArray(SIZE);
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));
# Line 92 | Line 199 | public class AtomicLongArrayTest extends
199          }
200      }
201  
202 <    public void testGetAndIncrement(){
203 <        AtomicLongArray ai = new AtomicLongArray(SIZE);
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));
# Line 106 | Line 216 | public class AtomicLongArrayTest extends
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(SIZE);
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));
# Line 117 | Line 230 | public class AtomicLongArrayTest extends
230          }
231      }
232  
233 <    public void testDecrementAndGet(){
234 <        AtomicLongArray ai = new AtomicLongArray(SIZE);
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));
# Line 128 | Line 244 | public class AtomicLongArrayTest extends
244          }
245      }
246  
247 +    /**
248 +     * incrementAndGet increments and returns current value
249 +     */
250      public void testIncrementAndGet() {
251 <        AtomicLongArray ai = new AtomicLongArray(SIZE);
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));
# Line 143 | Line 262 | public class AtomicLongArrayTest extends
262      }
263  
264      static final long COUNTDOWN = 100000;
265 <    
265 >
266      class Counter implements Runnable {
267          final AtomicLongArray ai;
268          volatile long counts;
# Line 166 | Line 285 | public class AtomicLongArrayTest extends
285          }
286      }
287  
288 <    public void testCountingInMultipleThreads() {
289 <        try {
290 <            final AtomicLongArray ai = new AtomicLongArray(SIZE);
291 <            for (int i = 0; i < SIZE; ++i)
292 <                ai.set(i, COUNTDOWN);
293 <            Counter c1 = new Counter(ai);
294 <            Counter c2 = new Counter(ai);
295 <            Thread t1 = new Thread(c1);
296 <            Thread t2 = new Thread(c2);
297 <            t1.start();
298 <            t2.start();
299 <            t1.join();
300 <            t2.join();
301 <            assertEquals(c1.counts+c2.counts, SIZE * COUNTDOWN);
302 <        }
303 <        catch(InterruptedException ie) {
304 <            fail("unexpected exception");
305 <        }
306 <    }
307 <
308 <    public void testSerialization() {
309 <        AtomicLongArray l = new AtomicLongArray(SIZE);
310 <        for (int i = 0; i < SIZE; ++i)
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 l = new AtomicLongArray(SIZE);
312 >        for (int i = 0; i < SIZE; ++i)
313              l.set(i, -i);
314  
315 <        try {
316 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
317 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
318 <            out.writeObject(l);
319 <            out.close();
320 <
321 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
322 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
323 <            AtomicLongArray r = (AtomicLongArray) in.readObject();
324 <            for (int i = 0; i < SIZE; ++i) {
204 <                assertEquals(l.get(i), r.get(i));
205 <            }
206 <        } catch(Exception e){
207 <            e.printStackTrace();
208 <            fail("unexpected exception");
315 >        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
316 >        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
317 >        out.writeObject(l);
318 >        out.close();
319 >
320 >        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
321 >        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
322 >        AtomicLongArray r = (AtomicLongArray) in.readObject();
323 >        for (int i = 0; i < SIZE; ++i) {
324 >            assertEquals(l.get(i), r.get(i));
325          }
326      }
327  
328 +    /**
329 +     * toString returns current value.
330 +     */
331 +    public void testToString() {
332 +        long[] a = { 17, 3, -42, 99, -7};
333 +        AtomicLongArray ai = new AtomicLongArray(a);
334 +        assertEquals(Arrays.toString(a), ai.toString());
335 +    }
336 +
337  
338   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines