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.6 by dl, Sat Dec 27 19:26:43 2003 UTC vs.
Revision 1.25 by jsr166, Wed Aug 10 07:14:48 2011 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.*;
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);
# Line 21 | Line 21 | public class AtomicLongArrayTest extends
21      /**
22       * constructor creates array of given size with all elements zero
23       */
24 <    public void testConstructor(){
25 <        AtomicLongArray ai = new AtomicLongArray(SIZE);
26 <        for (int i = 0; i < SIZE; ++i)
27 <            assertEquals(0,ai.get(i));
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 <     * get and set for out of bound indices throw IndexOutOfBoundsException
31 >     * constructor with null array throws NPE
32       */
33 <    public void testIndexing(){
34 <        AtomicLongArray ai = new AtomicLongArray(SIZE);
35 <        try {
36 <            ai.get(SIZE);
37 <        } catch(IndexOutOfBoundsException success){
38 <        }
39 <        try {
40 <            ai.get(-1);
41 <        } catch(IndexOutOfBoundsException success){
42 <        }
33 >    public void testConstructor2NPE() {
34          try {
35 <            ai.set(SIZE, 0);
36 <        } catch(IndexOutOfBoundsException success){
37 <        }
38 <        try {
39 <            ai.set(-1, 0);
40 <        } catch(IndexOutOfBoundsException success){
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 ai = new AtomicLongArray(SIZE);
94 <        for (int i = 0; i < SIZE; ++i) {
95 <            ai.set(i, 1);
96 <            assertEquals(1,ai.get(i));
97 <            ai.set(i, 2);
98 <            assertEquals(2,ai.get(i));
99 <            ai.set(i, -3);
100 <            assertEquals(-3,ai.get(i));
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 ai = new AtomicLongArray(SIZE);
124 <        for (int i = 0; i < SIZE; ++i) {
125 <            ai.set(i, 1);
126 <            assertTrue(ai.compareAndSet(i, 1,2));
127 <            assertTrue(ai.compareAndSet(i, 2,-4));
128 <            assertEquals(-4,ai.get(i));
129 <            assertFalse(ai.compareAndSet(i, -5,7));
130 <            assertFalse((7 == ai.get(i)));
131 <            assertTrue(ai.compareAndSet(i, -4,7));
132 <            assertEquals(7,ai.get(i));
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  
# Line 86 | Line 137 | public class AtomicLongArrayTest extends
137       * compareAndSet in one thread enables another waiting for value
138       * to succeed
139       */
140 <    public void testCompareAndSetInMultipleThreads() {
140 >    public void testCompareAndSetInMultipleThreads() throws InterruptedException {
141          final AtomicLongArray a = new AtomicLongArray(1);
142          a.set(0, 1);
143 <        Thread t = new Thread(new Runnable() {
144 <                public void run() {
145 <                    while(!a.compareAndSet(0, 2, 3)) Thread.yield();
146 <                }});
147 <        try {
148 <            t.start();
149 <            assertTrue(a.compareAndSet(0, 1, 2));
150 <            t.join(LONG_DELAY_MS);
151 <            assertFalse(t.isAlive());
152 <            assertEquals(a.get(0), 3);
153 <        }
103 <        catch(Exception e) {
104 <            unexpectedException();
105 <        }
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
158 >     * to expected
159       */
160 <    public void testWeakCompareAndSet(){
161 <        AtomicLongArray ai = new AtomicLongArray(SIZE);
162 <        for (int i = 0; i < SIZE; ++i) {
163 <            ai.set(i, 1);
164 <            while(!ai.weakCompareAndSet(i, 1,2));
165 <            while(!ai.weakCompareAndSet(i, 2,-4));
166 <            assertEquals(-4,ai.get(i));
167 <            while(!ai.weakCompareAndSet(i, -4,7));
168 <            assertEquals(7,ai.get(i));
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
173 >     * getAndSet returns previous value and sets to given value at given index
174       */
175 <    public void testGetAndSet(){
176 <        AtomicLongArray ai = new AtomicLongArray(SIZE);
177 <        for (int i = 0; i < SIZE; ++i) {
178 <            ai.set(i, 1);
179 <            assertEquals(1,ai.getAndSet(i,0));
180 <            assertEquals(0,ai.getAndSet(i,-10));
181 <            assertEquals(-10,ai.getAndSet(i,1));
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
186 >     * getAndAdd returns previous value and adds given value
187       */
188 <    public void testGetAndAdd(){
189 <        AtomicLongArray ai = new AtomicLongArray(SIZE);
190 <        for (int i = 0; i < SIZE; ++i) {
191 <            ai.set(i, 1);
192 <            assertEquals(1,ai.getAndAdd(i,2));
193 <            assertEquals(3,ai.get(i));
194 <            assertEquals(3,ai.getAndAdd(i,-4));
195 <            assertEquals(-1,ai.get(i));
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 ai = new AtomicLongArray(SIZE);
204 <        for (int i = 0; i < SIZE; ++i) {
205 <            ai.set(i, 1);
206 <            assertEquals(1,ai.getAndDecrement(i));
207 <            assertEquals(0,ai.getAndDecrement(i));
208 <            assertEquals(-1,ai.getAndDecrement(i));
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 ai = new AtomicLongArray(SIZE);
217 <        for (int i = 0; i < SIZE; ++i) {
218 <            ai.set(i, 1);
219 <            assertEquals(1,ai.getAndIncrement(i));
220 <            assertEquals(2,ai.get(i));
221 <            ai.set(i,-2);
222 <            assertEquals(-2,ai.getAndIncrement(i));
223 <            assertEquals(-1,ai.getAndIncrement(i));
224 <            assertEquals(0,ai.getAndIncrement(i));
225 <            assertEquals(1,ai.get(i));
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
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      /**
244       * decrementAndGet decrements and returns current value
245       */
246 <    public void testDecrementAndGet(){
247 <        AtomicLongArray ai = new AtomicLongArray(SIZE);
248 <        for (int i = 0; i < SIZE; ++i) {
249 <            ai.set(i, 1);
250 <            assertEquals(0,ai.decrementAndGet(i));
251 <            assertEquals(-1,ai.decrementAndGet(i));
252 <            assertEquals(-2,ai.decrementAndGet(i));
253 <            assertEquals(-2,ai.get(i));
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  
# Line 210 | Line 258 | public class AtomicLongArrayTest extends
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 <    
276 <    class Counter implements Runnable {
277 <        final AtomicLongArray ai;
275 >
276 >    class Counter extends CheckedRunnable {
277 >        final AtomicLongArray aa;
278          volatile long counts;
279 <        Counter(AtomicLongArray a) { ai = a; }
280 <        public void run() {
279 >        Counter(AtomicLongArray a) { aa = a; }
280 >        public void realRun() {
281              for (;;) {
282                  boolean done = true;
283 <                for (int i = 0; i < ai.length(); ++i) {
284 <                    long v = ai.get(i);
285 <                    threadAssertTrue(v >= 0);
283 >                for (int i = 0; i < aa.length(); i++) {
284 >                    long v = aa.get(i);
285 >                    assertTrue(v >= 0);
286                      if (v != 0) {
287                          done = false;
288 <                        if (ai.compareAndSet(i, v, v-1))
288 >                        if (aa.compareAndSet(i, v, v-1))
289                              ++counts;
290                      }
291                  }
# Line 251 | Line 299 | public class AtomicLongArrayTest extends
299       * Multiple threads using same array of counters successfully
300       * update a number of times equal to total count
301       */
302 <    public void testCountingInMultipleThreads() {
303 <        try {
304 <            final AtomicLongArray ai = new AtomicLongArray(SIZE);
305 <            for (int i = 0; i < SIZE; ++i)
306 <                ai.set(i, COUNTDOWN);
307 <            Counter c1 = new Counter(ai);
308 <            Counter c2 = new Counter(ai);
309 <            Thread t1 = new Thread(c1);
310 <            Thread t2 = new Thread(c2);
311 <            t1.start();
312 <            t2.start();
313 <            t1.join();
314 <            t2.join();
267 <            assertEquals(c1.counts+c2.counts, SIZE * COUNTDOWN);
268 <        }
269 <        catch(InterruptedException ie) {
270 <            unexpectedException();
271 <        }
302 >    public void testCountingInMultipleThreads() throws InterruptedException {
303 >        final AtomicLongArray aa = new AtomicLongArray(SIZE);
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() {
321 <        AtomicLongArray l = new AtomicLongArray(SIZE);
322 <        for (int i = 0; i < SIZE; ++i)
323 <            l.set(i, -i);
324 <
325 <        try {
326 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
327 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
328 <            out.writeObject(l);
286 <            out.close();
287 <
288 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
289 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
290 <            AtomicLongArray r = (AtomicLongArray) in.readObject();
291 <            for (int i = 0; i < SIZE; ++i) {
292 <                assertEquals(l.get(i), r.get(i));
293 <            }
294 <        } catch(Exception e){
295 <            unexpectedException();
320 >    public void testSerialization() throws Exception {
321 >        AtomicLongArray x = new AtomicLongArray(SIZE);
322 >        for (int i = 0; i < SIZE; i++)
323 >            x.set(i, -i);
324 >        AtomicLongArray y = serialClone(x);
325 >        assertTrue(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 +        long[] a = { 17, 3, -42, 99, -7 };
337 +        AtomicLongArray aa = new AtomicLongArray(a);
338 +        assertEquals(Arrays.toString(a), aa.toString());
339 +    }
340  
341   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines