ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AtomicLongArrayTest.java
(Generate patch)

Comparing jsr166/src/test/tck/AtomicLongArrayTest.java (file contents):
Revision 1.4 by dl, Sat Sep 20 18:20:07 2003 UTC vs.
Revision 1.38 by dl, Tue Jan 26 13:33:05 2021 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.*;
9 > import java.util.Arrays;
10 > import java.util.concurrent.atomic.AtomicLongArray;
11 >
12 > import junit.framework.Test;
13 > import junit.framework.TestSuite;
14  
15   public class AtomicLongArrayTest extends JSR166TestCase {
16 <    public static void main (String[] args) {
17 <        junit.textui.TestRunner.run (suite());
16 >    public static void main(String[] args) {
17 >        main(suite(), args);
18      }
19      public static Test suite() {
20          return new TestSuite(AtomicLongArrayTest.class);
21      }
22  
23      /**
24 <     *
24 >     * constructor creates array of given size with all elements zero
25 >     */
26 >    public void testConstructor() {
27 >        AtomicLongArray aa = new AtomicLongArray(SIZE);
28 >        for (int i = 0; i < SIZE; i++)
29 >            assertEquals(0, aa.get(i));
30 >    }
31 >
32 >    /**
33 >     * constructor with null array throws NPE
34 >     */
35 >    public void testConstructor2NPE() {
36 >        try {
37 >            long[] a = null;
38 >            new AtomicLongArray(a);
39 >            shouldThrow();
40 >        } catch (NullPointerException success) {}
41 >    }
42 >
43 >    /**
44 >     * constructor with array is of same size and has all elements
45 >     */
46 >    public void testConstructor2() {
47 >        long[] a = { 17L, 3L, -42L, 99L, -7L };
48 >        AtomicLongArray aa = new AtomicLongArray(a);
49 >        assertEquals(a.length, aa.length());
50 >        for (int i = 0; i < a.length; i++)
51 >            assertEquals(a[i], aa.get(i));
52 >    }
53 >
54 >    /**
55 >     * get and set for out of bound indices throw IndexOutOfBoundsException
56 >     */
57 >    @SuppressWarnings("deprecation")
58 >    public void testIndexing() {
59 >        AtomicLongArray aa = new AtomicLongArray(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 >
92 >    /**
93 >     * get returns the last value set at index
94       */
95 <    public void testConstructor(){
96 <        AtomicLongArray ai = new AtomicLongArray(SIZE);
97 <        for (int i = 0; i < SIZE; ++i)
98 <            assertEquals(0,ai.get(i));
95 >    public void testGetSet() {
96 >        AtomicLongArray aa = new AtomicLongArray(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  
107      /**
108 <     *
108 >     * get returns the last value lazySet at index by same thread
109       */
110 <    public void testGetSet(){
111 <        AtomicLongArray ai = new AtomicLongArray(SIZE);
112 <        for (int i = 0; i < SIZE; ++i) {
113 <            ai.set(i, 1);
114 <            assertEquals(1,ai.get(i));
115 <            ai.set(i, 2);
116 <            assertEquals(2,ai.get(i));
117 <            ai.set(i, -3);
118 <            assertEquals(-3,ai.get(i));
110 >    public void testGetLazySet() {
111 >        AtomicLongArray aa = new AtomicLongArray(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  
122      /**
123 <     *
123 >     * compareAndSet succeeds in changing value if equal to expected else fails
124       */
125 <    public void testCompareAndSet(){
126 <        AtomicLongArray ai = new AtomicLongArray(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));
125 >    public void testCompareAndSet() {
126 >        AtomicLongArray aa = new AtomicLongArray(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  
139      /**
140 <     *
140 >     * compareAndSet in one thread enables another waiting for value
141 >     * to succeed
142       */
143 <    public void testWeakCompareAndSet(){
144 <        AtomicLongArray ai = new AtomicLongArray(SIZE);
145 <        for (int i = 0; i < SIZE; ++i) {
146 <            ai.set(i, 1);
147 <            while(!ai.weakCompareAndSet(i, 1,2));
148 <            while(!ai.weakCompareAndSet(i, 2,-4));
149 <            assertEquals(-4,ai.get(i));
150 <            while(!ai.weakCompareAndSet(i, -4,7));
151 <            assertEquals(7,ai.get(i));
143 >    public void testCompareAndSetInMultipleThreads() throws InterruptedException {
144 >        final AtomicLongArray a = new AtomicLongArray(1);
145 >        a.set(0, 1);
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
162 >     */
163 >    @SuppressWarnings("deprecation")
164 >    public void testWeakCompareAndSet() {
165 >        AtomicLongArray aa = new AtomicLongArray(SIZE);
166 >        for (int i = 0; i < SIZE; i++) {
167 >            aa.set(i, 1);
168 >            do {} while (!aa.weakCompareAndSet(i, 1, 2));
169 >            do {} while (!aa.weakCompareAndSet(i, 2, -4));
170 >            assertEquals(-4, aa.get(i));
171 >            do {} while (!aa.weakCompareAndSet(i, -4, 7));
172 >            assertEquals(7, aa.get(i));
173          }
174      }
175  
176      /**
177 <     *
177 >     * getAndSet returns previous value and sets to given value at given index
178       */
179 <    public void testGetAndSet(){
180 <        AtomicLongArray ai = new AtomicLongArray(SIZE);
181 <        for (int i = 0; i < SIZE; ++i) {
182 <            ai.set(i, 1);
183 <            assertEquals(1,ai.getAndSet(i,0));
184 <            assertEquals(0,ai.getAndSet(i,-10));
185 <            assertEquals(-10,ai.getAndSet(i,1));
179 >    public void testGetAndSet() {
180 >        AtomicLongArray aa = new AtomicLongArray(SIZE);
181 >        for (int i = 0; i < SIZE; i++) {
182 >            aa.set(i, 1);
183 >            assertEquals(1, aa.getAndSet(i, 0));
184 >            assertEquals(0, aa.getAndSet(i, -10));
185 >            assertEquals(-10, aa.getAndSet(i, 1));
186          }
187      }
188  
189      /**
190 <     *
190 >     * getAndAdd returns previous value and adds given value
191       */
192 <    public void testGetAndAdd(){
193 <        AtomicLongArray ai = new AtomicLongArray(SIZE);
194 <        for (int i = 0; i < SIZE; ++i) {
195 <            ai.set(i, 1);
196 <            assertEquals(1,ai.getAndAdd(i,2));
197 <            assertEquals(3,ai.get(i));
198 <            assertEquals(3,ai.getAndAdd(i,-4));
199 <            assertEquals(-1,ai.get(i));
192 >    public void testGetAndAdd() {
193 >        AtomicLongArray aa = new AtomicLongArray(SIZE);
194 >        for (int i = 0; i < SIZE; i++) {
195 >            aa.set(i, 1);
196 >            assertEquals(1, aa.getAndAdd(i, 2));
197 >            assertEquals(3, aa.get(i));
198 >            assertEquals(3, aa.getAndAdd(i, -4));
199 >            assertEquals(-1, aa.get(i));
200          }
201      }
202  
203      /**
204 <     *
204 >     * getAndDecrement returns previous value and decrements
205       */
206 <    public void testGetAndDecrement(){
207 <        AtomicLongArray ai = new AtomicLongArray(SIZE);
208 <        for (int i = 0; i < SIZE; ++i) {
209 <            ai.set(i, 1);
210 <            assertEquals(1,ai.getAndDecrement(i));
211 <            assertEquals(0,ai.getAndDecrement(i));
212 <            assertEquals(-1,ai.getAndDecrement(i));
206 >    public void testGetAndDecrement() {
207 >        AtomicLongArray aa = new AtomicLongArray(SIZE);
208 >        for (int i = 0; i < SIZE; i++) {
209 >            aa.set(i, 1);
210 >            assertEquals(1, aa.getAndDecrement(i));
211 >            assertEquals(0, aa.getAndDecrement(i));
212 >            assertEquals(-1, aa.getAndDecrement(i));
213          }
214      }
215  
216      /**
217 <     *
217 >     * getAndIncrement returns previous value and increments
218       */
219 <    public void testGetAndIncrement(){
220 <        AtomicLongArray ai = new AtomicLongArray(SIZE);
221 <        for (int i = 0; i < SIZE; ++i) {
222 <            ai.set(i, 1);
223 <            assertEquals(1,ai.getAndIncrement(i));
224 <            assertEquals(2,ai.get(i));
225 <            ai.set(i,-2);
226 <            assertEquals(-2,ai.getAndIncrement(i));
227 <            assertEquals(-1,ai.getAndIncrement(i));
228 <            assertEquals(0,ai.getAndIncrement(i));
229 <            assertEquals(1,ai.get(i));
219 >    public void testGetAndIncrement() {
220 >        AtomicLongArray aa = new AtomicLongArray(SIZE);
221 >        for (int i = 0; i < SIZE; i++) {
222 >            aa.set(i, 1);
223 >            assertEquals(1, aa.getAndIncrement(i));
224 >            assertEquals(2, aa.get(i));
225 >            aa.set(i, -2);
226 >            assertEquals(-2, aa.getAndIncrement(i));
227 >            assertEquals(-1, aa.getAndIncrement(i));
228 >            assertEquals(0, aa.getAndIncrement(i));
229 >            assertEquals(1, aa.get(i));
230          }
231      }
232  
233      /**
234 <     *
234 >     * addAndGet adds given value to current, and returns current value
235       */
236      public void testAddAndGet() {
237 <        AtomicLongArray ai = new AtomicLongArray(SIZE);
238 <        for (int i = 0; i < SIZE; ++i) {
239 <            ai.set(i, 1);
240 <            assertEquals(3,ai.addAndGet(i,2));
241 <            assertEquals(3,ai.get(i));
242 <            assertEquals(-1,ai.addAndGet(i,-4));
243 <            assertEquals(-1,ai.get(i));
237 >        AtomicLongArray aa = new AtomicLongArray(SIZE);
238 >        for (int i = 0; i < SIZE; i++) {
239 >            aa.set(i, 1);
240 >            assertEquals(3, aa.addAndGet(i, 2));
241 >            assertEquals(3, aa.get(i));
242 >            assertEquals(-1, aa.addAndGet(i, -4));
243 >            assertEquals(-1, aa.get(i));
244          }
245      }
246  
247      /**
248 <     *
248 >     * decrementAndGet decrements and returns current value
249       */
250 <    public void testDecrementAndGet(){
251 <        AtomicLongArray ai = new AtomicLongArray(SIZE);
252 <        for (int i = 0; i < SIZE; ++i) {
253 <            ai.set(i, 1);
254 <            assertEquals(0,ai.decrementAndGet(i));
255 <            assertEquals(-1,ai.decrementAndGet(i));
256 <            assertEquals(-2,ai.decrementAndGet(i));
257 <            assertEquals(-2,ai.get(i));
250 >    public void testDecrementAndGet() {
251 >        AtomicLongArray aa = new AtomicLongArray(SIZE);
252 >        for (int i = 0; i < SIZE; i++) {
253 >            aa.set(i, 1);
254 >            assertEquals(0, aa.decrementAndGet(i));
255 >            assertEquals(-1, aa.decrementAndGet(i));
256 >            assertEquals(-2, aa.decrementAndGet(i));
257 >            assertEquals(-2, aa.get(i));
258          }
259      }
260  
261      /**
262 <     *
262 >     * incrementAndGet increments and returns current value
263       */
264      public void testIncrementAndGet() {
265 <        AtomicLongArray ai = new AtomicLongArray(SIZE);
266 <        for (int i = 0; i < SIZE; ++i) {
267 <            ai.set(i, 1);
268 <            assertEquals(2,ai.incrementAndGet(i));
269 <            assertEquals(2,ai.get(i));
270 <            ai.set(i, -2);
271 <            assertEquals(-1,ai.incrementAndGet(i));
272 <            assertEquals(0,ai.incrementAndGet(i));
273 <            assertEquals(1,ai.incrementAndGet(i));
274 <            assertEquals(1,ai.get(i));
265 >        AtomicLongArray aa = new AtomicLongArray(SIZE);
266 >        for (int i = 0; i < SIZE; i++) {
267 >            aa.set(i, 1);
268 >            assertEquals(2, aa.incrementAndGet(i));
269 >            assertEquals(2, aa.get(i));
270 >            aa.set(i, -2);
271 >            assertEquals(-1, aa.incrementAndGet(i));
272 >            assertEquals(0, aa.incrementAndGet(i));
273 >            assertEquals(1, aa.incrementAndGet(i));
274 >            assertEquals(1, aa.get(i));
275          }
276      }
277  
278 <    static final long COUNTDOWN = 100000;
279 <    
280 <    class Counter implements Runnable {
281 <        final AtomicLongArray ai;
282 <        volatile long counts;
183 <        Counter(AtomicLongArray a) { ai = a; }
184 <        public void run() {
278 >    class Counter extends CheckedRunnable {
279 >        final AtomicLongArray aa;
280 >        int decs;
281 >        Counter(AtomicLongArray a) { aa = a; }
282 >        public void realRun() {
283              for (;;) {
284                  boolean done = true;
285 <                for (int i = 0; i < ai.length(); ++i) {
286 <                    long v = ai.get(i);
287 <                    threadAssertTrue(v >= 0);
285 >                for (int i = 0; i < aa.length(); i++) {
286 >                    long v = aa.get(i);
287 >                    assertTrue(v >= 0);
288                      if (v != 0) {
289                          done = false;
290 <                        if (ai.compareAndSet(i, v, v-1))
291 <                            ++counts;
290 >                        if (aa.compareAndSet(i, v, v - 1))
291 >                            decs++;
292                      }
293                  }
294                  if (done)
# Line 200 | Line 298 | public class AtomicLongArrayTest extends
298      }
299  
300      /**
301 <     *
301 >     * Multiple threads using same array of counters successfully
302 >     * update a number of times equal to total count
303       */
304 <    public void testCountingInMultipleThreads() {
305 <        try {
306 <            final AtomicLongArray ai = new AtomicLongArray(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();
314 <            t2.start();
315 <            t1.join();
217 <            t2.join();
218 <            assertEquals(c1.counts+c2.counts, SIZE * COUNTDOWN);
219 <        }
220 <        catch(InterruptedException ie) {
221 <            unexpectedException();
222 <        }
304 >    public void testCountingInMultipleThreads() throws InterruptedException {
305 >        final AtomicLongArray aa = new AtomicLongArray(SIZE);
306 >        long countdown = 10000;
307 >        for (int i = 0; i < SIZE; i++)
308 >            aa.set(i, countdown);
309 >        Counter c1 = new Counter(aa);
310 >        Counter c2 = new Counter(aa);
311 >        Thread t1 = newStartedThread(c1);
312 >        Thread t2 = newStartedThread(c2);
313 >        t1.join();
314 >        t2.join();
315 >        assertEquals(c1.decs + c2.decs, SIZE * countdown);
316      }
317  
318      /**
319 <     *
320 <     */
321 <    public void testSerialization() {
322 <        AtomicLongArray l = new AtomicLongArray(SIZE);
323 <        for (int i = 0; i < SIZE; ++i)
324 <            l.set(i, -i);
325 <
326 <        try {
327 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
328 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
329 <            out.writeObject(l);
237 <            out.close();
238 <
239 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
240 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
241 <            AtomicLongArray r = (AtomicLongArray) in.readObject();
242 <            for (int i = 0; i < SIZE; ++i) {
243 <                assertEquals(l.get(i), r.get(i));
244 <            }
245 <        } catch(Exception e){
246 <            unexpectedException();
319 >     * a deserialized/reserialized array holds same values in same order
320 >     */
321 >    public void testSerialization() throws Exception {
322 >        AtomicLongArray x = new AtomicLongArray(SIZE);
323 >        for (int i = 0; i < SIZE; i++)
324 >            x.set(i, -i);
325 >        AtomicLongArray y = serialClone(x);
326 >        assertNotSame(x, y);
327 >        assertEquals(x.length(), y.length());
328 >        for (int i = 0; i < SIZE; i++) {
329 >            assertEquals(x.get(i), y.get(i));
330          }
331      }
332  
333 +    /**
334 +     * toString returns current value.
335 +     */
336 +    public void testToString() {
337 +        long[] a = { 17, 3, -42, 99, -7 };
338 +        AtomicLongArray aa = new AtomicLongArray(a);
339 +        assertEquals(Arrays.toString(a), aa.toString());
340 +    }
341  
342   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines