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.8 by dl, Fri Jan 9 20:07:36 2004 UTC vs.
Revision 1.14 by jsr166, Tue Nov 17 02:37:16 2009 UTC

# Line 2 | Line 2
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.
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 AtomicIntegerArrayTest extends JSR166TestCase {
15  
# Line 25 | Line 26 | public class AtomicIntegerArrayTest exte
26       */
27      public void testConstructor() {
28          AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
29 <        for (int i = 0; i < SIZE; ++i)
29 >        for (int i = 0; i < SIZE; ++i)
30              assertEquals(0,ai.get(i));
31      }
32  
# Line 36 | Line 37 | public class AtomicIntegerArrayTest exte
37          try {
38              int[] a = null;
39              AtomicIntegerArray ai = new AtomicIntegerArray(a);
40 <        } catch (NullPointerException success) {
41 <        } catch (Exception ex) {
41 <            unexpectedException();
42 <        }
40 >            shouldThrow();
41 >        } catch (NullPointerException success) {}
42      }
43  
44      /**
# Line 49 | Line 48 | public class AtomicIntegerArrayTest exte
48          int[] a = { 17, 3, -42, 99, -7};
49          AtomicIntegerArray ai = new AtomicIntegerArray(a);
50          assertEquals(a.length, ai.length());
51 <        for (int i = 0; i < a.length; ++i)
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(){
58 >    public void testIndexing() {
59          AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
60          try {
61              ai.get(SIZE);
62 <        } catch(IndexOutOfBoundsException success){
62 >            shouldThrow();
63 >        } catch (IndexOutOfBoundsException success) {
64          }
65          try {
66              ai.get(-1);
67 <        } catch(IndexOutOfBoundsException success){
67 >            shouldThrow();
68 >        } catch (IndexOutOfBoundsException success) {
69          }
70          try {
71              ai.set(SIZE, 0);
72 <        } catch(IndexOutOfBoundsException success){
72 >            shouldThrow();
73 >        } catch (IndexOutOfBoundsException success) {
74          }
75          try {
76              ai.set(-1, 0);
77 <        } catch(IndexOutOfBoundsException success){
77 >            shouldThrow();
78 >        } catch (IndexOutOfBoundsException success) {
79          }
80      }
81  
# Line 80 | Line 83 | public class AtomicIntegerArrayTest exte
83       * get returns the last value set at index
84       */
85      public void testGetSet() {
86 <        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
86 >        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
87          for (int i = 0; i < SIZE; ++i) {
88              ai.set(i, 1);
89              assertEquals(1,ai.get(i));
# Line 92 | Line 95 | public class AtomicIntegerArrayTest exte
95      }
96  
97      /**
98 +     * get returns the last value lazySet at index by same thread
99 +     */
100 +    public void testGetLazySet() {
101 +        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
102 +        for (int i = 0; i < SIZE; ++i) {
103 +            ai.lazySet(i, 1);
104 +            assertEquals(1,ai.get(i));
105 +            ai.lazySet(i, 2);
106 +            assertEquals(2,ai.get(i));
107 +            ai.lazySet(i, -3);
108 +            assertEquals(-3,ai.get(i));
109 +        }
110 +    }
111 +
112 +    /**
113       * compareAndSet succeeds in changing value if equal to expected else fails
114       */
115      public void testCompareAndSet() {
116 <        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
116 >        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
117          for (int i = 0; i < SIZE; ++i) {
118              ai.set(i, 1);
119              assertTrue(ai.compareAndSet(i, 1,2));
# Line 112 | Line 130 | public class AtomicIntegerArrayTest exte
130       * compareAndSet in one thread enables another waiting for value
131       * to succeed
132       */
133 <    public void testCompareAndSetInMultipleThreads() {
133 >    public void testCompareAndSetInMultipleThreads() throws Exception {
134          final AtomicIntegerArray a = new AtomicIntegerArray(1);
135          a.set(0, 1);
136          Thread t = new Thread(new Runnable() {
137                  public void run() {
138 <                    while(!a.compareAndSet(0, 2, 3)) Thread.yield();
138 >                    while (!a.compareAndSet(0, 2, 3)) Thread.yield();
139                  }});
140 <        try {
141 <            t.start();
142 <            assertTrue(a.compareAndSet(0, 1, 2));
143 <            t.join(LONG_DELAY_MS);
144 <            assertFalse(t.isAlive());
145 <            assertEquals(a.get(0), 3);
128 <        }
129 <        catch(Exception e) {
130 <            unexpectedException();
131 <        }
140 >
141 >        t.start();
142 >        assertTrue(a.compareAndSet(0, 1, 2));
143 >        t.join(LONG_DELAY_MS);
144 >        assertFalse(t.isAlive());
145 >        assertEquals(a.get(0), 3);
146      }
147  
148      /**
149       * repeated weakCompareAndSet succeeds in changing value when equal
150 <     * to expected
150 >     * to expected
151       */
152      public void testWeakCompareAndSet() {
153 <        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
153 >        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
154          for (int i = 0; i < SIZE; ++i) {
155              ai.set(i, 1);
156 <            while(!ai.weakCompareAndSet(i, 1,2));
157 <            while(!ai.weakCompareAndSet(i, 2,-4));
156 >            while (!ai.weakCompareAndSet(i, 1,2));
157 >            while (!ai.weakCompareAndSet(i, 2,-4));
158              assertEquals(-4,ai.get(i));
159 <            while(!ai.weakCompareAndSet(i, -4,7));
159 >            while (!ai.weakCompareAndSet(i, -4,7));
160              assertEquals(7,ai.get(i));
161          }
162      }
# Line 151 | Line 165 | public class AtomicIntegerArrayTest exte
165       *  getAndSet returns previous value and sets to given value at given index
166       */
167      public void testGetAndSet() {
168 <        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
168 >        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
169          for (int i = 0; i < SIZE; ++i) {
170              ai.set(i, 1);
171              assertEquals(1,ai.getAndSet(i,0));
# Line 164 | Line 178 | public class AtomicIntegerArrayTest exte
178       *  getAndAdd returns previous value and adds given value
179       */
180      public void testGetAndAdd() {
181 <        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
181 >        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
182          for (int i = 0; i < SIZE; ++i) {
183              ai.set(i, 1);
184              assertEquals(1,ai.getAndAdd(i,2));
# Line 178 | Line 192 | public class AtomicIntegerArrayTest exte
192       * getAndDecrement returns previous value and decrements
193       */
194      public void testGetAndDecrement() {
195 <        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
195 >        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
196          for (int i = 0; i < SIZE; ++i) {
197              ai.set(i, 1);
198              assertEquals(1,ai.getAndDecrement(i));
# Line 191 | Line 205 | public class AtomicIntegerArrayTest exte
205       * getAndIncrement returns previous value and increments
206       */
207      public void testGetAndIncrement() {
208 <        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
208 >        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
209          for (int i = 0; i < SIZE; ++i) {
210              ai.set(i, 1);
211              assertEquals(1,ai.getAndIncrement(i));
# Line 208 | Line 222 | public class AtomicIntegerArrayTest exte
222       *  addAndGet adds given value to current, and returns current value
223       */
224      public void testAddAndGet() {
225 <        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
225 >        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
226          for (int i = 0; i < SIZE; ++i) {
227              ai.set(i, 1);
228              assertEquals(3,ai.addAndGet(i,2));
# Line 222 | Line 236 | public class AtomicIntegerArrayTest exte
236       * decrementAndGet decrements and returns current value
237       */
238      public void testDecrementAndGet() {
239 <        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
239 >        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
240          for (int i = 0; i < SIZE; ++i) {
241              ai.set(i, 1);
242              assertEquals(0,ai.decrementAndGet(i));
# Line 236 | Line 250 | public class AtomicIntegerArrayTest exte
250       *  incrementAndGet increments and returns current value
251       */
252      public void testIncrementAndGet() {
253 <        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
253 >        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
254          for (int i = 0; i < SIZE; ++i) {
255              ai.set(i, 1);
256              assertEquals(2,ai.incrementAndGet(i));
# Line 250 | Line 264 | public class AtomicIntegerArrayTest exte
264      }
265  
266      static final int COUNTDOWN = 100000;
267 <    
267 >
268      class Counter implements Runnable {
269          final AtomicIntegerArray ai;
270          volatile int counts;
# Line 277 | Line 291 | public class AtomicIntegerArrayTest exte
291       * Multiple threads using same array of counters successfully
292       * update a number of times equal to total count
293       */
294 <    public void testCountingInMultipleThreads() {
295 <        try {
296 <            final AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
297 <            for (int i = 0; i < SIZE; ++i)
298 <                ai.set(i, COUNTDOWN);
299 <            Counter c1 = new Counter(ai);
300 <            Counter c2 = new Counter(ai);
301 <            Thread t1 = new Thread(c1);
302 <            Thread t2 = new Thread(c2);
303 <            t1.start();
304 <            t2.start();
305 <            t1.join();
306 <            t2.join();
293 <            assertEquals(c1.counts+c2.counts, SIZE * COUNTDOWN);
294 <        }
295 <        catch(InterruptedException ie) {
296 <            unexpectedException();
297 <        }
294 >    public void testCountingInMultipleThreads() throws InterruptedException {
295 >        final AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
296 >        for (int i = 0; i < SIZE; ++i)
297 >            ai.set(i, COUNTDOWN);
298 >        Counter c1 = new Counter(ai);
299 >        Counter c2 = new Counter(ai);
300 >        Thread t1 = new Thread(c1);
301 >        Thread t2 = new Thread(c2);
302 >        t1.start();
303 >        t2.start();
304 >        t1.join();
305 >        t2.join();
306 >        assertEquals(c1.counts+c2.counts, SIZE * COUNTDOWN);
307      }
308  
309  
310      /**
311       * a deserialized serialized array holds same values
312       */
313 <    public void testSerialization() {
314 <        AtomicIntegerArray l = new AtomicIntegerArray(SIZE);
315 <        for (int i = 0; i < SIZE; ++i)
313 >    public void testSerialization() throws Exception {
314 >        AtomicIntegerArray l = new AtomicIntegerArray(SIZE);
315 >        for (int i = 0; i < SIZE; ++i)
316              l.set(i, -i);
317  
318 <        try {
319 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
320 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
321 <            out.writeObject(l);
322 <            out.close();
323 <
324 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
325 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
326 <            AtomicIntegerArray r = (AtomicIntegerArray) in.readObject();
327 <            for (int i = 0; i < SIZE; ++i) {
319 <                assertEquals(l.get(i), r.get(i));
320 <            }
321 <        } catch(Exception e){
322 <            e.printStackTrace();
323 <            unexpectedException();
318 >        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
319 >        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
320 >        out.writeObject(l);
321 >        out.close();
322 >
323 >        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
324 >        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
325 >        AtomicIntegerArray r = (AtomicIntegerArray) in.readObject();
326 >        for (int i = 0; i < SIZE; ++i) {
327 >            assertEquals(l.get(i), r.get(i));
328          }
329      }
330  
331  
332 +    /**
333 +     * toString returns current value.
334 +     */
335 +    public void testToString() {
336 +        int[] a = { 17, 3, -42, 99, -7};
337 +        AtomicIntegerArray ai = new AtomicIntegerArray(a);
338 +        assertEquals(Arrays.toString(a), ai.toString());
339 +    }
340  
341   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines