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

Comparing jsr166/src/test/tck/AtomicIntegerArrayTest.java (file contents):
Revision 1.4 by dl, Sat Sep 20 18:20:07 2003 UTC vs.
Revision 1.7 by dl, Thu Jan 8 01:29:46 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.*;
# Line 20 | Line 21 | public class AtomicIntegerArrayTest exte
21  
22  
23      /**
24 <     *
24 >     * constructor creates array of given size with all elements zero
25       */
26      public void testConstructor() {
27          AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
# Line 29 | Line 30 | public class AtomicIntegerArrayTest exte
30      }
31  
32      /**
33 <     *
33 >     * constructor with null array throws NPE
34 >     */
35 >    public void testConstructor2NPE() {
36 >        try {
37 >            int[] a = null;
38 >            AtomicIntegerArray ai = new AtomicIntegerArray(a);
39 >        } catch (NullPointerException success) {
40 >        } catch (Exception ex) {
41 >            unexpectedException();
42 >        }
43 >    }
44 >
45 >    /**
46 >     * constructor with array is of same size and has all elements
47 >     */
48 >    public void testConstructor2() {
49 >        int[] a = { 17, 3, -42, 99, -7};
50 >        AtomicIntegerArray ai = new AtomicIntegerArray(a);
51 >        assertEquals(a.length, ai.length());
52 >        for (int i = 0; i < a.length; ++i)
53 >            assertEquals(a[i], ai.get(i));
54 >    }
55 >
56 >    /**
57 >     * get and set for out of bound indices throw IndexOutOfBoundsException
58 >     */
59 >    public void testIndexing(){
60 >        AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
61 >        try {
62 >            ai.get(SIZE);
63 >        } catch(IndexOutOfBoundsException success){
64 >        }
65 >        try {
66 >            ai.get(-1);
67 >        } catch(IndexOutOfBoundsException success){
68 >        }
69 >        try {
70 >            ai.set(SIZE, 0);
71 >        } catch(IndexOutOfBoundsException success){
72 >        }
73 >        try {
74 >            ai.set(-1, 0);
75 >        } catch(IndexOutOfBoundsException success){
76 >        }
77 >    }
78 >
79 >    /**
80 >     * get returns the last value set at index
81       */
82      public void testGetSet() {
83          AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
# Line 44 | Line 92 | public class AtomicIntegerArrayTest exte
92      }
93  
94      /**
95 <     *
95 >     * compareAndSet succeeds in changing value if equal to expected else fails
96       */
97      public void testCompareAndSet() {
98          AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
# Line 61 | Line 109 | public class AtomicIntegerArrayTest exte
109      }
110  
111      /**
112 <     *
112 >     * compareAndSet in one thread enables another waiting for value
113 >     * to succeed
114 >     */
115 >    public void testCompareAndSetInMultipleThreads() {
116 >        final AtomicIntegerArray a = new AtomicIntegerArray(1);
117 >        a.set(0, 1);
118 >        Thread t = new Thread(new Runnable() {
119 >                public void run() {
120 >                    while(!a.compareAndSet(0, 2, 3)) Thread.yield();
121 >                }});
122 >        try {
123 >            t.start();
124 >            assertTrue(a.compareAndSet(0, 1, 2));
125 >            t.join(LONG_DELAY_MS);
126 >            assertFalse(t.isAlive());
127 >            assertEquals(a.get(0), 3);
128 >        }
129 >        catch(Exception e) {
130 >            unexpectedException();
131 >        }
132 >    }
133 >
134 >    /**
135 >     * repeated weakCompareAndSet succeeds in changing value when equal
136 >     * to expected
137       */
138      public void testWeakCompareAndSet() {
139          AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
# Line 76 | Line 148 | public class AtomicIntegerArrayTest exte
148      }
149  
150      /**
151 <     *
151 >     *  getAndSet returns previous value and sets to given value at given index
152       */
153      public void testGetAndSet() {
154          AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
# Line 89 | Line 161 | public class AtomicIntegerArrayTest exte
161      }
162  
163      /**
164 <     *
164 >     *  getAndAdd returns previous value and adds given value
165       */
166      public void testGetAndAdd() {
167          AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
# Line 103 | Line 175 | public class AtomicIntegerArrayTest exte
175      }
176  
177      /**
178 <     *
178 >     * getAndDecrement returns previous value and decrements
179       */
180      public void testGetAndDecrement() {
181          AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
# Line 116 | Line 188 | public class AtomicIntegerArrayTest exte
188      }
189  
190      /**
191 <     *
191 >     * getAndIncrement returns previous value and increments
192       */
193      public void testGetAndIncrement() {
194          AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
# Line 133 | Line 205 | public class AtomicIntegerArrayTest exte
205      }
206  
207      /**
208 <     *
208 >     *  addAndGet adds given value to current, and returns current value
209       */
210      public void testAddAndGet() {
211          AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
# Line 147 | Line 219 | public class AtomicIntegerArrayTest exte
219      }
220  
221      /**
222 <     *
222 >     * decrementAndGet decrements and returns current value
223       */
224      public void testDecrementAndGet() {
225          AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
# Line 161 | Line 233 | public class AtomicIntegerArrayTest exte
233      }
234  
235      /**
236 <     *
236 >     *  incrementAndGet increments and returns current value
237       */
238      public void testIncrementAndGet() {
239          AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
# Line 202 | Line 274 | public class AtomicIntegerArrayTest exte
274      }
275  
276      /**
277 <     *
277 >     * Multiple threads using same array of counters successfully
278 >     * update a number of times equal to total count
279       */
280      public void testCountingInMultipleThreads() {
281          try {
# Line 226 | Line 299 | public class AtomicIntegerArrayTest exte
299  
300  
301      /**
302 <     *
302 >     * a deserialized serialized array holds same values
303       */
304      public void testSerialization() {
305          AtomicIntegerArray l = new AtomicIntegerArray(SIZE);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines