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.15 by jsr166, Tue Nov 17 06:58:50 2009 UTC vs.
Revision 1.39 by dl, Tue Jan 26 13:33:05 2021 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
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);
39 >            new AtomicIntegerArray(a);
40              shouldThrow();
41          } catch (NullPointerException success) {}
42      }
# Line 45 | Line 45 | public class AtomicIntegerArrayTest exte
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 +    @SuppressWarnings("deprecation")
59      public void testIndexing() {
60 <        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
61 <        try {
62 <            ai.get(SIZE);
63 <            shouldThrow();
64 <        } catch (IndexOutOfBoundsException success) {
65 <        }
66 <        try {
67 <            ai.get(-1);
68 <            shouldThrow();
69 <        } catch (IndexOutOfBoundsException success) {
70 <        }
71 <        try {
72 <            ai.set(SIZE, 0);
73 <            shouldThrow();
74 <        } catch (IndexOutOfBoundsException success) {
75 <        }
76 <        try {
77 <            ai.set(-1, 0);
78 <            shouldThrow();
79 <        } catch (IndexOutOfBoundsException success) {
60 >        AtomicIntegerArray aa = new AtomicIntegerArray(SIZE);
61 >        for (int index : new int[] { -1, SIZE }) {
62 >            try {
63 >                aa.get(index);
64 >                shouldThrow();
65 >            } catch (IndexOutOfBoundsException success) {}
66 >            try {
67 >                aa.set(index, 1);
68 >                shouldThrow();
69 >            } catch (IndexOutOfBoundsException success) {}
70 >            try {
71 >                aa.lazySet(index, 1);
72 >                shouldThrow();
73 >            } catch (IndexOutOfBoundsException success) {}
74 >            try {
75 >                aa.compareAndSet(index, 1, 2);
76 >                shouldThrow();
77 >            } catch (IndexOutOfBoundsException success) {}
78 >            try {
79 >                aa.weakCompareAndSet(index, 1, 2);
80 >                shouldThrow();
81 >            } catch (IndexOutOfBoundsException success) {}
82 >            try {
83 >                aa.getAndAdd(index, 1);
84 >                shouldThrow();
85 >            } catch (IndexOutOfBoundsException success) {}
86 >            try {
87 >                aa.addAndGet(index, 1);
88 >                shouldThrow();
89 >            } catch (IndexOutOfBoundsException success) {}
90          }
91      }
92  
# Line 83 | Line 94 | public class AtomicIntegerArrayTest exte
94       * get returns the last value set at index
95       */
96      public void testGetSet() {
97 <        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
98 <        for (int i = 0; i < SIZE; ++i) {
99 <            ai.set(i, 1);
100 <            assertEquals(1,ai.get(i));
101 <            ai.set(i, 2);
102 <            assertEquals(2,ai.get(i));
103 <            ai.set(i, -3);
104 <            assertEquals(-3,ai.get(i));
97 >        AtomicIntegerArray aa = new AtomicIntegerArray(SIZE);
98 >        for (int i = 0; i < SIZE; i++) {
99 >            aa.set(i, 1);
100 >            assertEquals(1, aa.get(i));
101 >            aa.set(i, 2);
102 >            assertEquals(2, aa.get(i));
103 >            aa.set(i, -3);
104 >            assertEquals(-3, aa.get(i));
105          }
106      }
107  
# Line 98 | Line 109 | public class AtomicIntegerArrayTest exte
109       * get returns the last value lazySet at index by same thread
110       */
111      public void testGetLazySet() {
112 <        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
113 <        for (int i = 0; i < SIZE; ++i) {
114 <            ai.lazySet(i, 1);
115 <            assertEquals(1,ai.get(i));
116 <            ai.lazySet(i, 2);
117 <            assertEquals(2,ai.get(i));
118 <            ai.lazySet(i, -3);
119 <            assertEquals(-3,ai.get(i));
112 >        AtomicIntegerArray aa = new AtomicIntegerArray(SIZE);
113 >        for (int i = 0; i < SIZE; i++) {
114 >            aa.lazySet(i, 1);
115 >            assertEquals(1, aa.get(i));
116 >            aa.lazySet(i, 2);
117 >            assertEquals(2, aa.get(i));
118 >            aa.lazySet(i, -3);
119 >            assertEquals(-3, aa.get(i));
120          }
121      }
122  
# Line 113 | Line 124 | public class AtomicIntegerArrayTest exte
124       * compareAndSet succeeds in changing value if equal to expected else fails
125       */
126      public void testCompareAndSet() {
127 <        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
128 <        for (int i = 0; i < SIZE; ++i) {
129 <            ai.set(i, 1);
130 <            assertTrue(ai.compareAndSet(i, 1,2));
131 <            assertTrue(ai.compareAndSet(i, 2,-4));
132 <            assertEquals(-4,ai.get(i));
133 <            assertFalse(ai.compareAndSet(i, -5,7));
134 <            assertFalse((7 == ai.get(i)));
135 <            assertTrue(ai.compareAndSet(i, -4,7));
136 <            assertEquals(7,ai.get(i));
127 >        AtomicIntegerArray aa = new AtomicIntegerArray(SIZE);
128 >        for (int i = 0; i < SIZE; i++) {
129 >            aa.set(i, 1);
130 >            assertTrue(aa.compareAndSet(i, 1, 2));
131 >            assertTrue(aa.compareAndSet(i, 2, -4));
132 >            assertEquals(-4, aa.get(i));
133 >            assertFalse(aa.compareAndSet(i, -5, 7));
134 >            assertEquals(-4, aa.get(i));
135 >            assertTrue(aa.compareAndSet(i, -4, 7));
136 >            assertEquals(7, aa.get(i));
137          }
138      }
139  
# Line 143 | Line 154 | public class AtomicIntegerArrayTest exte
154          assertTrue(a.compareAndSet(0, 1, 2));
155          t.join(LONG_DELAY_MS);
156          assertFalse(t.isAlive());
157 <        assertEquals(a.get(0), 3);
157 >        assertEquals(3, a.get(0));
158      }
159  
160      /**
161       * repeated weakCompareAndSet succeeds in changing value when equal
162       * to expected
163       */
164 +    @SuppressWarnings("deprecation")
165      public void testWeakCompareAndSet() {
166 <        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
167 <        for (int i = 0; i < SIZE; ++i) {
168 <            ai.set(i, 1);
169 <            while (!ai.weakCompareAndSet(i, 1,2));
170 <            while (!ai.weakCompareAndSet(i, 2,-4));
171 <            assertEquals(-4,ai.get(i));
172 <            while (!ai.weakCompareAndSet(i, -4,7));
173 <            assertEquals(7,ai.get(i));
166 >        AtomicIntegerArray aa = new AtomicIntegerArray(SIZE);
167 >        for (int i = 0; i < SIZE; i++) {
168 >            aa.set(i, 1);
169 >            do {} while (!aa.weakCompareAndSet(i, 1, 2));
170 >            do {} while (!aa.weakCompareAndSet(i, 2, -4));
171 >            assertEquals(-4, aa.get(i));
172 >            do {} while (!aa.weakCompareAndSet(i, -4, 7));
173 >            assertEquals(7, aa.get(i));
174          }
175      }
176  
177      /**
178 <     *  getAndSet returns previous value and sets to given value at given index
178 >     * getAndSet returns previous value and sets to given value at given index
179       */
180      public void testGetAndSet() {
181 <        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
182 <        for (int i = 0; i < SIZE; ++i) {
183 <            ai.set(i, 1);
184 <            assertEquals(1,ai.getAndSet(i,0));
185 <            assertEquals(0,ai.getAndSet(i,-10));
186 <            assertEquals(-10,ai.getAndSet(i,1));
181 >        AtomicIntegerArray aa = new AtomicIntegerArray(SIZE);
182 >        for (int i = 0; i < SIZE; i++) {
183 >            aa.set(i, 1);
184 >            assertEquals(1, aa.getAndSet(i, 0));
185 >            assertEquals(0, aa.getAndSet(i, -10));
186 >            assertEquals(-10, aa.getAndSet(i, 1));
187          }
188      }
189  
190      /**
191 <     *  getAndAdd returns previous value and adds given value
191 >     * getAndAdd returns previous value and adds given value
192       */
193      public void testGetAndAdd() {
194 <        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
195 <        for (int i = 0; i < SIZE; ++i) {
196 <            ai.set(i, 1);
197 <            assertEquals(1,ai.getAndAdd(i,2));
198 <            assertEquals(3,ai.get(i));
199 <            assertEquals(3,ai.getAndAdd(i,-4));
200 <            assertEquals(-1,ai.get(i));
194 >        AtomicIntegerArray aa = new AtomicIntegerArray(SIZE);
195 >        for (int i = 0; i < SIZE; i++) {
196 >            aa.set(i, 1);
197 >            assertEquals(1, aa.getAndAdd(i, 2));
198 >            assertEquals(3, aa.get(i));
199 >            assertEquals(3, aa.getAndAdd(i, -4));
200 >            assertEquals(-1, aa.get(i));
201          }
202      }
203  
# Line 193 | Line 205 | public class AtomicIntegerArrayTest exte
205       * getAndDecrement returns previous value and decrements
206       */
207      public void testGetAndDecrement() {
208 <        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
209 <        for (int i = 0; i < SIZE; ++i) {
210 <            ai.set(i, 1);
211 <            assertEquals(1,ai.getAndDecrement(i));
212 <            assertEquals(0,ai.getAndDecrement(i));
213 <            assertEquals(-1,ai.getAndDecrement(i));
208 >        AtomicIntegerArray aa = new AtomicIntegerArray(SIZE);
209 >        for (int i = 0; i < SIZE; i++) {
210 >            aa.set(i, 1);
211 >            assertEquals(1, aa.getAndDecrement(i));
212 >            assertEquals(0, aa.getAndDecrement(i));
213 >            assertEquals(-1, aa.getAndDecrement(i));
214          }
215      }
216  
# Line 206 | Line 218 | public class AtomicIntegerArrayTest exte
218       * getAndIncrement returns previous value and increments
219       */
220      public void testGetAndIncrement() {
221 <        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
222 <        for (int i = 0; i < SIZE; ++i) {
223 <            ai.set(i, 1);
224 <            assertEquals(1,ai.getAndIncrement(i));
225 <            assertEquals(2,ai.get(i));
226 <            ai.set(i,-2);
227 <            assertEquals(-2,ai.getAndIncrement(i));
228 <            assertEquals(-1,ai.getAndIncrement(i));
229 <            assertEquals(0,ai.getAndIncrement(i));
230 <            assertEquals(1,ai.get(i));
221 >        AtomicIntegerArray aa = new AtomicIntegerArray(SIZE);
222 >        for (int i = 0; i < SIZE; i++) {
223 >            aa.set(i, 1);
224 >            assertEquals(1, aa.getAndIncrement(i));
225 >            assertEquals(2, aa.get(i));
226 >            aa.set(i, -2);
227 >            assertEquals(-2, aa.getAndIncrement(i));
228 >            assertEquals(-1, aa.getAndIncrement(i));
229 >            assertEquals(0, aa.getAndIncrement(i));
230 >            assertEquals(1, aa.get(i));
231          }
232      }
233  
234      /**
235 <     *  addAndGet adds given value to current, and returns current value
235 >     * addAndGet adds given value to current, and returns current value
236       */
237      public void testAddAndGet() {
238 <        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
239 <        for (int i = 0; i < SIZE; ++i) {
240 <            ai.set(i, 1);
241 <            assertEquals(3,ai.addAndGet(i,2));
242 <            assertEquals(3,ai.get(i));
243 <            assertEquals(-1,ai.addAndGet(i,-4));
244 <            assertEquals(-1,ai.get(i));
238 >        AtomicIntegerArray aa = new AtomicIntegerArray(SIZE);
239 >        for (int i = 0; i < SIZE; i++) {
240 >            aa.set(i, 1);
241 >            assertEquals(3, aa.addAndGet(i, 2));
242 >            assertEquals(3, aa.get(i));
243 >            assertEquals(-1, aa.addAndGet(i, -4));
244 >            assertEquals(-1, aa.get(i));
245          }
246      }
247  
# Line 237 | Line 249 | public class AtomicIntegerArrayTest exte
249       * decrementAndGet decrements and returns current value
250       */
251      public void testDecrementAndGet() {
252 <        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
253 <        for (int i = 0; i < SIZE; ++i) {
254 <            ai.set(i, 1);
255 <            assertEquals(0,ai.decrementAndGet(i));
256 <            assertEquals(-1,ai.decrementAndGet(i));
257 <            assertEquals(-2,ai.decrementAndGet(i));
258 <            assertEquals(-2,ai.get(i));
252 >        AtomicIntegerArray aa = new AtomicIntegerArray(SIZE);
253 >        for (int i = 0; i < SIZE; i++) {
254 >            aa.set(i, 1);
255 >            assertEquals(0, aa.decrementAndGet(i));
256 >            assertEquals(-1, aa.decrementAndGet(i));
257 >            assertEquals(-2, aa.decrementAndGet(i));
258 >            assertEquals(-2, aa.get(i));
259          }
260      }
261  
262      /**
263 <     *  incrementAndGet increments and returns current value
263 >     * incrementAndGet increments and returns current value
264       */
265      public void testIncrementAndGet() {
266 <        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
267 <        for (int i = 0; i < SIZE; ++i) {
268 <            ai.set(i, 1);
269 <            assertEquals(2,ai.incrementAndGet(i));
270 <            assertEquals(2,ai.get(i));
271 <            ai.set(i, -2);
272 <            assertEquals(-1,ai.incrementAndGet(i));
273 <            assertEquals(0,ai.incrementAndGet(i));
274 <            assertEquals(1,ai.incrementAndGet(i));
275 <            assertEquals(1,ai.get(i));
266 >        AtomicIntegerArray aa = new AtomicIntegerArray(SIZE);
267 >        for (int i = 0; i < SIZE; i++) {
268 >            aa.set(i, 1);
269 >            assertEquals(2, aa.incrementAndGet(i));
270 >            assertEquals(2, aa.get(i));
271 >            aa.set(i, -2);
272 >            assertEquals(-1, aa.incrementAndGet(i));
273 >            assertEquals(0, aa.incrementAndGet(i));
274 >            assertEquals(1, aa.incrementAndGet(i));
275 >            assertEquals(1, aa.get(i));
276          }
277      }
278  
279 <    static final int COUNTDOWN = 100000;
280 <
281 <    class Counter implements Runnable {
282 <        final AtomicIntegerArray ai;
283 <        volatile int counts;
272 <        Counter(AtomicIntegerArray a) { ai = a; }
273 <        public void run() {
279 >    class Counter extends CheckedRunnable {
280 >        final AtomicIntegerArray aa;
281 >        int decs;
282 >        Counter(AtomicIntegerArray a) { aa = a; }
283 >        public void realRun() {
284              for (;;) {
285                  boolean done = true;
286 <                for (int i = 0; i < ai.length(); ++i) {
287 <                    int v = ai.get(i);
288 <                    threadAssertTrue(v >= 0);
286 >                for (int i = 0; i < aa.length(); i++) {
287 >                    int v = aa.get(i);
288 >                    assertTrue(v >= 0);
289                      if (v != 0) {
290                          done = false;
291 <                        if (ai.compareAndSet(i, v, v-1))
292 <                            ++counts;
291 >                        if (aa.compareAndSet(i, v, v - 1))
292 >                            decs++;
293                      }
294                  }
295                  if (done)
# Line 293 | Line 303 | public class AtomicIntegerArrayTest exte
303       * update a number of times equal to total count
304       */
305      public void testCountingInMultipleThreads() throws InterruptedException {
306 <        final AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
307 <        for (int i = 0; i < SIZE; ++i)
308 <            ai.set(i, COUNTDOWN);
309 <        Counter c1 = new Counter(ai);
310 <        Counter c2 = new Counter(ai);
311 <        Thread t1 = new Thread(c1);
312 <        Thread t2 = new Thread(c2);
313 <        t1.start();
304 <        t2.start();
306 >        final AtomicIntegerArray aa = new AtomicIntegerArray(SIZE);
307 >        int countdown = 10000;
308 >        for (int i = 0; i < SIZE; i++)
309 >            aa.set(i, countdown);
310 >        Counter c1 = new Counter(aa);
311 >        Counter c2 = new Counter(aa);
312 >        Thread t1 = newStartedThread(c1);
313 >        Thread t2 = newStartedThread(c2);
314          t1.join();
315          t2.join();
316 <        assertEquals(c1.counts+c2.counts, SIZE * COUNTDOWN);
316 >        assertEquals(c1.decs + c2.decs, SIZE * countdown);
317      }
318  
310
319      /**
320 <     * a deserialized serialized array holds same values
320 >     * a deserialized/reserialized array holds same values in same order
321       */
322      public void testSerialization() throws Exception {
323 <        AtomicIntegerArray l = new AtomicIntegerArray(SIZE);
324 <        for (int i = 0; i < SIZE; ++i)
325 <            l.set(i, -i);
326 <
327 <        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
328 <        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
329 <        out.writeObject(l);
330 <        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) {
328 <            assertEquals(l.get(i), r.get(i));
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  
332
334      /**
335       * toString returns current value.
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