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.10 by dl, Wed May 25 14:27:37 2005 UTC vs.
Revision 1.36 by jsr166, Fri Jun 17 19:00:48 2016 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines