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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines