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.9 by dl, Wed May 25 14:27:37 2005 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/licenses/publicdomain
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.*;
13  
14   public class AtomicLongArrayTest extends JSR166TestCase {
15      public static void main (String[] args) {
# Line 17 | Line 19 | public class AtomicLongArrayTest extends
19          return new TestSuite(AtomicLongArrayTest.class);
20      }
21  
22 +    /**
23 +     * constructor creates array of given size with all elements zero
24 +     */
25      public void testConstructor(){
26          AtomicLongArray ai = new AtomicLongArray(SIZE);
27          for (int i = 0; i < SIZE; ++i)
28              assertEquals(0,ai.get(i));
29      }
30  
31 +    /**
32 +     * constructor with null array throws NPE
33 +     */
34 +    public void testConstructor2NPE() {
35 +        try {
36 +            long[] a = null;
37 +            AtomicLongArray ai = new AtomicLongArray(a);
38 +        } catch (NullPointerException success) {
39 +        } catch (Exception ex) {
40 +            unexpectedException();
41 +        }
42 +    }
43 +
44 +    /**
45 +     * constructor with array is of same size and has all elements
46 +     */
47 +    public void testConstructor2() {
48 +        long[] a = { 17L, 3L, -42L, 99L, -7L};
49 +        AtomicLongArray ai = new AtomicLongArray(a);
50 +        assertEquals(a.length, ai.length());
51 +        for (int i = 0; i < a.length; ++i)
52 +            assertEquals(a[i], ai.get(i));
53 +    }
54 +
55 +    /**
56 +     * get and set for out of bound indices throw IndexOutOfBoundsException
57 +     */
58 +    public void testIndexing(){
59 +        AtomicLongArray ai = new AtomicLongArray(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){
75 +        }
76 +    }
77 +
78 +    /**
79 +     * get returns the last value set at index
80 +     */
81      public void testGetSet(){
82          AtomicLongArray ai = new AtomicLongArray(SIZE);
83          for (int i = 0; i < SIZE; ++i) {
# Line 35 | Line 90 | public class AtomicLongArrayTest extends
90          }
91      }
92  
93 +    /**
94 +     * get returns the last value lazySet at index by same thread
95 +     */
96 +    public void testGetLazySet(){
97 +        AtomicLongArray ai = new AtomicLongArray(SIZE);
98 +        for (int i = 0; i < SIZE; ++i) {
99 +            ai.lazySet(i, 1);
100 +            assertEquals(1,ai.get(i));
101 +            ai.lazySet(i, 2);
102 +            assertEquals(2,ai.get(i));
103 +            ai.lazySet(i, -3);
104 +            assertEquals(-3,ai.get(i));
105 +        }
106 +    }
107 +
108 +    /**
109 +     * compareAndSet succeeds in changing value if equal to expected else fails
110 +     */
111      public void testCompareAndSet(){
112          AtomicLongArray ai = new AtomicLongArray(SIZE);
113          for (int i = 0; i < SIZE; ++i) {
# Line 49 | Line 122 | public class AtomicLongArrayTest extends
122          }
123      }
124  
125 +    /**
126 +     * compareAndSet in one thread enables another waiting for value
127 +     * to succeed
128 +     */
129 +    public void testCompareAndSetInMultipleThreads() {
130 +        final AtomicLongArray a = new AtomicLongArray(1);
131 +        a.set(0, 1);
132 +        Thread t = new Thread(new Runnable() {
133 +                public void run() {
134 +                    while(!a.compareAndSet(0, 2, 3)) Thread.yield();
135 +                }});
136 +        try {
137 +            t.start();
138 +            assertTrue(a.compareAndSet(0, 1, 2));
139 +            t.join(LONG_DELAY_MS);
140 +            assertFalse(t.isAlive());
141 +            assertEquals(a.get(0), 3);
142 +        }
143 +        catch(Exception e) {
144 +            unexpectedException();
145 +        }
146 +    }
147 +
148 +    /**
149 +     * repeated weakCompareAndSet succeeds in changing value when equal
150 +     * to expected
151 +     */
152      public void testWeakCompareAndSet(){
153          AtomicLongArray ai = new AtomicLongArray(SIZE);
154          for (int i = 0; i < SIZE; ++i) {
# Line 61 | Line 161 | public class AtomicLongArrayTest extends
161          }
162      }
163  
164 +    /**
165 +     *  getAndSet returns previous value and sets to given value at given index
166 +     */
167      public void testGetAndSet(){
168          AtomicLongArray ai = new AtomicLongArray(SIZE);
169          for (int i = 0; i < SIZE; ++i) {
# Line 71 | Line 174 | public class AtomicLongArrayTest extends
174          }
175      }
176  
177 +    /**
178 +     *  getAndAdd returns previous value and adds given value
179 +     */
180      public void testGetAndAdd(){
181          AtomicLongArray ai = new AtomicLongArray(SIZE);
182          for (int i = 0; i < SIZE; ++i) {
# Line 82 | Line 188 | public class AtomicLongArrayTest extends
188          }
189      }
190  
191 +    /**
192 +     * getAndDecrement returns previous value and decrements
193 +     */
194      public void testGetAndDecrement(){
195          AtomicLongArray ai = new AtomicLongArray(SIZE);
196          for (int i = 0; i < SIZE; ++i) {
# Line 92 | Line 201 | public class AtomicLongArrayTest extends
201          }
202      }
203  
204 +    /**
205 +     * getAndIncrement returns previous value and increments
206 +     */
207      public void testGetAndIncrement(){
208          AtomicLongArray ai = new AtomicLongArray(SIZE);
209          for (int i = 0; i < SIZE; ++i) {
# Line 106 | Line 218 | public class AtomicLongArrayTest extends
218          }
219      }
220  
221 +    /**
222 +     *  addAndGet adds given value to current, and returns current value
223 +     */
224      public void testAddAndGet() {
225          AtomicLongArray ai = new AtomicLongArray(SIZE);
226          for (int i = 0; i < SIZE; ++i) {
# Line 117 | Line 232 | public class AtomicLongArrayTest extends
232          }
233      }
234  
235 +    /**
236 +     * decrementAndGet decrements and returns current value
237 +     */
238      public void testDecrementAndGet(){
239          AtomicLongArray ai = new AtomicLongArray(SIZE);
240          for (int i = 0; i < SIZE; ++i) {
# Line 128 | Line 246 | public class AtomicLongArrayTest extends
246          }
247      }
248  
249 +    /**
250 +     * incrementAndGet increments and returns current value
251 +     */
252      public void testIncrementAndGet() {
253          AtomicLongArray ai = new AtomicLongArray(SIZE);
254          for (int i = 0; i < SIZE; ++i) {
# Line 166 | Line 287 | public class AtomicLongArrayTest extends
287          }
288      }
289  
290 +    /**
291 +     * Multiple threads using same array of counters successfully
292 +     * update a number of times equal to total count
293 +     */
294      public void testCountingInMultipleThreads() {
295          try {
296              final AtomicLongArray ai = new AtomicLongArray(SIZE);
# Line 182 | Line 307 | public class AtomicLongArrayTest extends
307              assertEquals(c1.counts+c2.counts, SIZE * COUNTDOWN);
308          }
309          catch(InterruptedException ie) {
310 <            fail("unexpected exception");
310 >            unexpectedException();
311          }
312      }
313  
314 +    /**
315 +     * a deserialized serialized array holds same values
316 +     */
317      public void testSerialization() {
318          AtomicLongArray l = new AtomicLongArray(SIZE);
319          for (int i = 0; i < SIZE; ++i)
# Line 204 | Line 332 | public class AtomicLongArrayTest extends
332                  assertEquals(l.get(i), r.get(i));
333              }
334          } catch(Exception e){
335 <            e.printStackTrace();
208 <            fail("unexpected exception");
335 >            unexpectedException();
336          }
337      }
338  
339 +    /**
340 +     * toString returns current value.
341 +     */
342 +    public void testToString() {
343 +        long[] a = { 17, 3, -42, 99, -7};
344 +        AtomicLongArray ai = new AtomicLongArray(a);
345 +        assertEquals(Arrays.toString(a), ai.toString());
346 +    }
347 +
348  
349   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines