ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AtomicLongArrayTest.java
Revision: 1.7
Committed: Thu Jan 8 01:29:46 2004 UTC (20 years, 4 months ago) by dl
Branch: MAIN
Changes since 1.6: +24 -0 lines
Log Message:
Add Atomic array constructor tests; adjust timings on other tests

File Contents

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