ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AtomicIntegerArrayTest.java
Revision: 1.9
Committed: Sat Jan 10 01:41:59 2004 UTC (20 years, 4 months ago) by dl
Branch: MAIN
CVS Tags: JSR166_PFD
Changes since 1.8: +9 -0 lines
Log Message:
Test toString

File Contents

# User Rev Content
1 dl 1.1 /*
2 dl 1.6 * 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 dl 1.1 */
8    
9     import junit.framework.*;
10     import java.util.concurrent.atomic.*;
11 dl 1.2 import java.io.*;
12 dl 1.9 import java.util.*;
13 dl 1.1
14 dl 1.3 public class AtomicIntegerArrayTest extends JSR166TestCase {
15 dl 1.1
16     public static void main (String[] args) {
17     junit.textui.TestRunner.run (suite());
18     }
19     public static Test suite() {
20     return new TestSuite(AtomicIntegerArrayTest.class);
21     }
22    
23 dl 1.3
24 dl 1.4 /**
25 dl 1.5 * constructor creates array of given size with all elements zero
26 dl 1.4 */
27     public void testConstructor() {
28 dl 1.3 AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
29     for (int i = 0; i < SIZE; ++i)
30 dl 1.1 assertEquals(0,ai.get(i));
31     }
32    
33 dl 1.4 /**
34 dl 1.7 * 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 dl 1.5 * 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 dl 1.4 */
83     public void testGetSet() {
84 dl 1.3 AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
85     for (int i = 0; i < SIZE; ++i) {
86 dl 1.1 ai.set(i, 1);
87     assertEquals(1,ai.get(i));
88     ai.set(i, 2);
89     assertEquals(2,ai.get(i));
90     ai.set(i, -3);
91     assertEquals(-3,ai.get(i));
92     }
93     }
94    
95 dl 1.4 /**
96 dl 1.5 * compareAndSet succeeds in changing value if equal to expected else fails
97 dl 1.4 */
98     public void testCompareAndSet() {
99 dl 1.3 AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
100     for (int i = 0; i < SIZE; ++i) {
101 dl 1.1 ai.set(i, 1);
102     assertTrue(ai.compareAndSet(i, 1,2));
103     assertTrue(ai.compareAndSet(i, 2,-4));
104     assertEquals(-4,ai.get(i));
105     assertFalse(ai.compareAndSet(i, -5,7));
106     assertFalse((7 == ai.get(i)));
107     assertTrue(ai.compareAndSet(i, -4,7));
108     assertEquals(7,ai.get(i));
109     }
110     }
111    
112 dl 1.4 /**
113 dl 1.5 * 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 dl 1.4 */
139     public void testWeakCompareAndSet() {
140 dl 1.3 AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
141     for (int i = 0; i < SIZE; ++i) {
142 dl 1.1 ai.set(i, 1);
143     while(!ai.weakCompareAndSet(i, 1,2));
144     while(!ai.weakCompareAndSet(i, 2,-4));
145     assertEquals(-4,ai.get(i));
146     while(!ai.weakCompareAndSet(i, -4,7));
147     assertEquals(7,ai.get(i));
148     }
149     }
150    
151 dl 1.4 /**
152 dl 1.5 * getAndSet returns previous value and sets to given value at given index
153 dl 1.4 */
154     public void testGetAndSet() {
155 dl 1.3 AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
156     for (int i = 0; i < SIZE; ++i) {
157 dl 1.1 ai.set(i, 1);
158     assertEquals(1,ai.getAndSet(i,0));
159     assertEquals(0,ai.getAndSet(i,-10));
160     assertEquals(-10,ai.getAndSet(i,1));
161     }
162     }
163    
164 dl 1.4 /**
165 dl 1.5 * getAndAdd returns previous value and adds given value
166 dl 1.4 */
167     public void testGetAndAdd() {
168 dl 1.3 AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
169     for (int i = 0; i < SIZE; ++i) {
170 dl 1.1 ai.set(i, 1);
171     assertEquals(1,ai.getAndAdd(i,2));
172     assertEquals(3,ai.get(i));
173     assertEquals(3,ai.getAndAdd(i,-4));
174     assertEquals(-1,ai.get(i));
175     }
176     }
177    
178 dl 1.4 /**
179 dl 1.5 * getAndDecrement returns previous value and decrements
180 dl 1.4 */
181     public void testGetAndDecrement() {
182 dl 1.3 AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
183     for (int i = 0; i < SIZE; ++i) {
184 dl 1.1 ai.set(i, 1);
185     assertEquals(1,ai.getAndDecrement(i));
186     assertEquals(0,ai.getAndDecrement(i));
187     assertEquals(-1,ai.getAndDecrement(i));
188     }
189     }
190    
191 dl 1.4 /**
192 dl 1.5 * getAndIncrement returns previous value and increments
193 dl 1.4 */
194     public void testGetAndIncrement() {
195 dl 1.3 AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
196     for (int i = 0; i < SIZE; ++i) {
197 dl 1.1 ai.set(i, 1);
198     assertEquals(1,ai.getAndIncrement(i));
199     assertEquals(2,ai.get(i));
200     ai.set(i,-2);
201     assertEquals(-2,ai.getAndIncrement(i));
202     assertEquals(-1,ai.getAndIncrement(i));
203     assertEquals(0,ai.getAndIncrement(i));
204     assertEquals(1,ai.get(i));
205     }
206     }
207    
208 dl 1.4 /**
209 dl 1.5 * addAndGet adds given value to current, and returns current value
210 dl 1.4 */
211 dl 1.1 public void testAddAndGet() {
212 dl 1.3 AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
213     for (int i = 0; i < SIZE; ++i) {
214 dl 1.1 ai.set(i, 1);
215     assertEquals(3,ai.addAndGet(i,2));
216     assertEquals(3,ai.get(i));
217     assertEquals(-1,ai.addAndGet(i,-4));
218     assertEquals(-1,ai.get(i));
219     }
220     }
221    
222 dl 1.4 /**
223 dl 1.5 * decrementAndGet decrements and returns current value
224 dl 1.4 */
225     public void testDecrementAndGet() {
226 dl 1.3 AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
227     for (int i = 0; i < SIZE; ++i) {
228 dl 1.1 ai.set(i, 1);
229     assertEquals(0,ai.decrementAndGet(i));
230     assertEquals(-1,ai.decrementAndGet(i));
231     assertEquals(-2,ai.decrementAndGet(i));
232     assertEquals(-2,ai.get(i));
233     }
234     }
235    
236 dl 1.4 /**
237 dl 1.5 * incrementAndGet increments and returns current value
238 dl 1.4 */
239 dl 1.1 public void testIncrementAndGet() {
240 dl 1.3 AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
241     for (int i = 0; i < SIZE; ++i) {
242 dl 1.1 ai.set(i, 1);
243     assertEquals(2,ai.incrementAndGet(i));
244     assertEquals(2,ai.get(i));
245     ai.set(i, -2);
246     assertEquals(-1,ai.incrementAndGet(i));
247     assertEquals(0,ai.incrementAndGet(i));
248     assertEquals(1,ai.incrementAndGet(i));
249     assertEquals(1,ai.get(i));
250 dl 1.2 }
251     }
252    
253 dl 1.3 static final int COUNTDOWN = 100000;
254    
255     class Counter implements Runnable {
256     final AtomicIntegerArray ai;
257     volatile int counts;
258     Counter(AtomicIntegerArray a) { ai = a; }
259     public void run() {
260     for (;;) {
261     boolean done = true;
262     for (int i = 0; i < ai.length(); ++i) {
263     int v = ai.get(i);
264     threadAssertTrue(v >= 0);
265     if (v != 0) {
266     done = false;
267     if (ai.compareAndSet(i, v, v-1))
268     ++counts;
269     }
270     }
271     if (done)
272     break;
273     }
274     }
275     }
276    
277 dl 1.4 /**
278 dl 1.5 * Multiple threads using same array of counters successfully
279     * update a number of times equal to total count
280 dl 1.4 */
281 dl 1.3 public void testCountingInMultipleThreads() {
282     try {
283     final AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
284     for (int i = 0; i < SIZE; ++i)
285     ai.set(i, COUNTDOWN);
286     Counter c1 = new Counter(ai);
287     Counter c2 = new Counter(ai);
288     Thread t1 = new Thread(c1);
289     Thread t2 = new Thread(c2);
290     t1.start();
291     t2.start();
292     t1.join();
293     t2.join();
294     assertEquals(c1.counts+c2.counts, SIZE * COUNTDOWN);
295     }
296     catch(InterruptedException ie) {
297 dl 1.4 unexpectedException();
298 dl 1.3 }
299     }
300    
301    
302 dl 1.4 /**
303 dl 1.5 * a deserialized serialized array holds same values
304 dl 1.4 */
305 dl 1.2 public void testSerialization() {
306 dl 1.3 AtomicIntegerArray l = new AtomicIntegerArray(SIZE);
307     for (int i = 0; i < SIZE; ++i)
308 dl 1.2 l.set(i, -i);
309    
310     try {
311     ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
312     ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
313     out.writeObject(l);
314     out.close();
315    
316     ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
317     ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
318     AtomicIntegerArray r = (AtomicIntegerArray) in.readObject();
319 dl 1.3 for (int i = 0; i < SIZE; ++i) {
320 dl 1.2 assertEquals(l.get(i), r.get(i));
321     }
322     } catch(Exception e){
323     e.printStackTrace();
324 dl 1.4 unexpectedException();
325 dl 1.1 }
326     }
327    
328 dl 1.8
329 dl 1.9 /**
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 dl 1.8
338 dl 1.1 }