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.8 by dl, Sat Jan 10 01:41:59 2004 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 +     * compareAndSet succeeds in changing value if equal to expected else fails
95 +     */
96      public void testCompareAndSet(){
97          AtomicLongArray ai = new AtomicLongArray(SIZE);
98          for (int i = 0; i < SIZE; ++i) {
# Line 49 | Line 107 | public class AtomicLongArrayTest extends
107          }
108      }
109  
110 +    /**
111 +     * compareAndSet in one thread enables another waiting for value
112 +     * to succeed
113 +     */
114 +    public void testCompareAndSetInMultipleThreads() {
115 +        final AtomicLongArray a = new AtomicLongArray(1);
116 +        a.set(0, 1);
117 +        Thread t = new Thread(new Runnable() {
118 +                public void run() {
119 +                    while(!a.compareAndSet(0, 2, 3)) Thread.yield();
120 +                }});
121 +        try {
122 +            t.start();
123 +            assertTrue(a.compareAndSet(0, 1, 2));
124 +            t.join(LONG_DELAY_MS);
125 +            assertFalse(t.isAlive());
126 +            assertEquals(a.get(0), 3);
127 +        }
128 +        catch(Exception e) {
129 +            unexpectedException();
130 +        }
131 +    }
132 +
133 +    /**
134 +     * repeated weakCompareAndSet succeeds in changing value when equal
135 +     * to expected
136 +     */
137      public void testWeakCompareAndSet(){
138          AtomicLongArray ai = new AtomicLongArray(SIZE);
139          for (int i = 0; i < SIZE; ++i) {
# Line 61 | Line 146 | public class AtomicLongArrayTest extends
146          }
147      }
148  
149 +    /**
150 +     *  getAndSet returns previous value and sets to given value at given index
151 +     */
152      public void testGetAndSet(){
153          AtomicLongArray ai = new AtomicLongArray(SIZE);
154          for (int i = 0; i < SIZE; ++i) {
# Line 71 | Line 159 | public class AtomicLongArrayTest extends
159          }
160      }
161  
162 +    /**
163 +     *  getAndAdd returns previous value and adds given value
164 +     */
165      public void testGetAndAdd(){
166          AtomicLongArray ai = new AtomicLongArray(SIZE);
167          for (int i = 0; i < SIZE; ++i) {
# Line 82 | Line 173 | public class AtomicLongArrayTest extends
173          }
174      }
175  
176 +    /**
177 +     * getAndDecrement returns previous value and decrements
178 +     */
179      public void testGetAndDecrement(){
180          AtomicLongArray ai = new AtomicLongArray(SIZE);
181          for (int i = 0; i < SIZE; ++i) {
# Line 92 | Line 186 | public class AtomicLongArrayTest extends
186          }
187      }
188  
189 +    /**
190 +     * getAndIncrement returns previous value and increments
191 +     */
192      public void testGetAndIncrement(){
193          AtomicLongArray ai = new AtomicLongArray(SIZE);
194          for (int i = 0; i < SIZE; ++i) {
# Line 106 | Line 203 | public class AtomicLongArrayTest extends
203          }
204      }
205  
206 +    /**
207 +     *  addAndGet adds given value to current, and returns current value
208 +     */
209      public void testAddAndGet() {
210          AtomicLongArray ai = new AtomicLongArray(SIZE);
211          for (int i = 0; i < SIZE; ++i) {
# Line 117 | Line 217 | public class AtomicLongArrayTest extends
217          }
218      }
219  
220 +    /**
221 +     * decrementAndGet decrements and returns current value
222 +     */
223      public void testDecrementAndGet(){
224          AtomicLongArray ai = new AtomicLongArray(SIZE);
225          for (int i = 0; i < SIZE; ++i) {
# Line 128 | Line 231 | public class AtomicLongArrayTest extends
231          }
232      }
233  
234 +    /**
235 +     * incrementAndGet increments and returns current value
236 +     */
237      public void testIncrementAndGet() {
238          AtomicLongArray ai = new AtomicLongArray(SIZE);
239          for (int i = 0; i < SIZE; ++i) {
# Line 166 | Line 272 | public class AtomicLongArrayTest extends
272          }
273      }
274  
275 +    /**
276 +     * Multiple threads using same array of counters successfully
277 +     * update a number of times equal to total count
278 +     */
279      public void testCountingInMultipleThreads() {
280          try {
281              final AtomicLongArray ai = new AtomicLongArray(SIZE);
# Line 182 | Line 292 | public class AtomicLongArrayTest extends
292              assertEquals(c1.counts+c2.counts, SIZE * COUNTDOWN);
293          }
294          catch(InterruptedException ie) {
295 <            fail("unexpected exception");
295 >            unexpectedException();
296          }
297      }
298  
299 +    /**
300 +     * a deserialized serialized array holds same values
301 +     */
302      public void testSerialization() {
303          AtomicLongArray l = new AtomicLongArray(SIZE);
304          for (int i = 0; i < SIZE; ++i)
# Line 204 | Line 317 | public class AtomicLongArrayTest extends
317                  assertEquals(l.get(i), r.get(i));
318              }
319          } catch(Exception e){
320 <            e.printStackTrace();
208 <            fail("unexpected exception");
320 >            unexpectedException();
321          }
322      }
323  
324 +    /**
325 +     * toString returns current value.
326 +     */
327 +    public void testToString() {
328 +        long[] a = { 17, 3, -42, 99, -7};
329 +        AtomicLongArray ai = new AtomicLongArray(a);
330 +        assertEquals(Arrays.toString(a), ai.toString());
331 +    }
332 +
333  
334   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines