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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines