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.3 by dl, Sun Sep 14 20:42:40 2003 UTC vs.
Revision 1.9 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 AtomicIntegerArrayTest extends JSR166TestCase {
15  
# Line 19 | Line 21 | public class AtomicIntegerArrayTest exte
21      }
22  
23  
24 <    public void testConstructor(){
24 >    /**
25 >     * constructor creates array of given size with all elements zero
26 >     */
27 >    public void testConstructor() {
28          AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
29          for (int i = 0; i < SIZE; ++i)
30              assertEquals(0,ai.get(i));
31      }
32  
33 <    public void testGetSet(){
33 >    /**
34 >     * constructor with null array throws NPE
35 >     */
36 >    public void testConstructor2NPE() {
37 >        try {
38 >            int[] a = null;
39 >            AtomicIntegerArray ai = new AtomicIntegerArray(a);
40 >        } catch (NullPointerException success) {
41 >        } catch (Exception ex) {
42 >            unexpectedException();
43 >        }
44 >    }
45 >
46 >    /**
47 >     * constructor with array is of same size and has all elements
48 >     */
49 >    public void testConstructor2() {
50 >        int[] a = { 17, 3, -42, 99, -7};
51 >        AtomicIntegerArray ai = new AtomicIntegerArray(a);
52 >        assertEquals(a.length, ai.length());
53 >        for (int i = 0; i < a.length; ++i)
54 >            assertEquals(a[i], ai.get(i));
55 >    }
56 >
57 >    /**
58 >     * get and set for out of bound indices throw IndexOutOfBoundsException
59 >     */
60 >    public void testIndexing(){
61 >        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
62 >        try {
63 >            ai.get(SIZE);
64 >        } catch(IndexOutOfBoundsException success){
65 >        }
66 >        try {
67 >            ai.get(-1);
68 >        } catch(IndexOutOfBoundsException success){
69 >        }
70 >        try {
71 >            ai.set(SIZE, 0);
72 >        } catch(IndexOutOfBoundsException success){
73 >        }
74 >        try {
75 >            ai.set(-1, 0);
76 >        } catch(IndexOutOfBoundsException success){
77 >        }
78 >    }
79 >
80 >    /**
81 >     * get returns the last value set at index
82 >     */
83 >    public void testGetSet() {
84          AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
85          for (int i = 0; i < SIZE; ++i) {
86              ai.set(i, 1);
# Line 37 | Line 92 | public class AtomicIntegerArrayTest exte
92          }
93      }
94  
95 <    public void testCompareAndSet(){
95 >    /**
96 >     * compareAndSet succeeds in changing value if equal to expected else fails
97 >     */
98 >    public void testCompareAndSet() {
99          AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
100          for (int i = 0; i < SIZE; ++i) {
101              ai.set(i, 1);
# Line 51 | Line 109 | public class AtomicIntegerArrayTest exte
109          }
110      }
111  
112 <    public void testWeakCompareAndSet(){
112 >    /**
113 >     * compareAndSet in one thread enables another waiting for value
114 >     * to succeed
115 >     */
116 >    public void testCompareAndSetInMultipleThreads() {
117 >        final AtomicIntegerArray a = new AtomicIntegerArray(1);
118 >        a.set(0, 1);
119 >        Thread t = new Thread(new Runnable() {
120 >                public void run() {
121 >                    while(!a.compareAndSet(0, 2, 3)) Thread.yield();
122 >                }});
123 >        try {
124 >            t.start();
125 >            assertTrue(a.compareAndSet(0, 1, 2));
126 >            t.join(LONG_DELAY_MS);
127 >            assertFalse(t.isAlive());
128 >            assertEquals(a.get(0), 3);
129 >        }
130 >        catch(Exception e) {
131 >            unexpectedException();
132 >        }
133 >    }
134 >
135 >    /**
136 >     * repeated weakCompareAndSet succeeds in changing value when equal
137 >     * to expected
138 >     */
139 >    public void testWeakCompareAndSet() {
140          AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
141          for (int i = 0; i < SIZE; ++i) {
142              ai.set(i, 1);
# Line 63 | Line 148 | public class AtomicIntegerArrayTest exte
148          }
149      }
150  
151 <    public void testGetAndSet(){
151 >    /**
152 >     *  getAndSet returns previous value and sets to given value at given index
153 >     */
154 >    public void testGetAndSet() {
155          AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
156          for (int i = 0; i < SIZE; ++i) {
157              ai.set(i, 1);
# Line 73 | Line 161 | public class AtomicIntegerArrayTest exte
161          }
162      }
163  
164 <    public void testGetAndAdd(){
164 >    /**
165 >     *  getAndAdd returns previous value and adds given value
166 >     */
167 >    public void testGetAndAdd() {
168          AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
169          for (int i = 0; i < SIZE; ++i) {
170              ai.set(i, 1);
# Line 84 | Line 175 | public class AtomicIntegerArrayTest exte
175          }
176      }
177  
178 <    public void testGetAndDecrement(){
178 >    /**
179 >     * getAndDecrement returns previous value and decrements
180 >     */
181 >    public void testGetAndDecrement() {
182          AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
183          for (int i = 0; i < SIZE; ++i) {
184              ai.set(i, 1);
# Line 94 | Line 188 | public class AtomicIntegerArrayTest exte
188          }
189      }
190  
191 <    public void testGetAndIncrement(){
191 >    /**
192 >     * getAndIncrement returns previous value and increments
193 >     */
194 >    public void testGetAndIncrement() {
195          AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
196          for (int i = 0; i < SIZE; ++i) {
197              ai.set(i, 1);
# Line 108 | Line 205 | public class AtomicIntegerArrayTest exte
205          }
206      }
207  
208 +    /**
209 +     *  addAndGet adds given value to current, and returns current value
210 +     */
211      public void testAddAndGet() {
212          AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
213          for (int i = 0; i < SIZE; ++i) {
# Line 119 | Line 219 | public class AtomicIntegerArrayTest exte
219          }
220      }
221  
222 <    public void testDecrementAndGet(){
222 >    /**
223 >     * decrementAndGet decrements and returns current value
224 >     */
225 >    public void testDecrementAndGet() {
226          AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
227          for (int i = 0; i < SIZE; ++i) {
228              ai.set(i, 1);
# Line 130 | Line 233 | public class AtomicIntegerArrayTest exte
233          }
234      }
235  
236 +    /**
237 +     *  incrementAndGet increments and returns current value
238 +     */
239      public void testIncrementAndGet() {
240          AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
241          for (int i = 0; i < SIZE; ++i) {
# Line 168 | Line 274 | public class AtomicIntegerArrayTest exte
274          }
275      }
276  
277 +    /**
278 +     * Multiple threads using same array of counters successfully
279 +     * update a number of times equal to total count
280 +     */
281      public void testCountingInMultipleThreads() {
282          try {
283              final AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
# Line 184 | Line 294 | public class AtomicIntegerArrayTest exte
294              assertEquals(c1.counts+c2.counts, SIZE * COUNTDOWN);
295          }
296          catch(InterruptedException ie) {
297 <            fail("unexpected exception");
297 >            unexpectedException();
298          }
299      }
300  
301  
302 +    /**
303 +     * a deserialized serialized array holds same values
304 +     */
305      public void testSerialization() {
306          AtomicIntegerArray l = new AtomicIntegerArray(SIZE);
307          for (int i = 0; i < SIZE; ++i)
# Line 208 | Line 321 | public class AtomicIntegerArrayTest exte
321              }
322          } catch(Exception e){
323              e.printStackTrace();
324 <            fail("unexpected exception");
324 >            unexpectedException();
325          }
326      }
327  
328 +
329 +    /**
330 +     * toString returns current value.
331 +     */
332 +    public void testToString() {
333 +        int[] a = { 17, 3, -42, 99, -7};
334 +        AtomicIntegerArray ai = new AtomicIntegerArray(a);
335 +        assertEquals(Arrays.toString(a), ai.toString());
336 +    }
337 +
338   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines