ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AtomicLongArrayTest.java
Revision: 1.9
Committed: Wed May 25 14:27:37 2005 UTC (18 years, 11 months ago) by dl
Branch: MAIN
Changes since 1.8: +15 -0 lines
Log Message:
Add tests for AtomicX.lazySet

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.8 import java.util.*;
13 dl 1.1
14 dl 1.3 public class AtomicLongArrayTest extends JSR166TestCase {
15 dl 1.1 public static void main (String[] args) {
16     junit.textui.TestRunner.run (suite());
17     }
18     public static Test suite() {
19     return new TestSuite(AtomicLongArrayTest.class);
20     }
21    
22 dl 1.4 /**
23 dl 1.5 * constructor creates array of given size with all elements zero
24 dl 1.4 */
25 dl 1.1 public void testConstructor(){
26 dl 1.3 AtomicLongArray ai = new AtomicLongArray(SIZE);
27     for (int i = 0; i < SIZE; ++i)
28 dl 1.1 assertEquals(0,ai.get(i));
29     }
30    
31 dl 1.4 /**
32 dl 1.7 * 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 dl 1.5 * 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 dl 1.4 */
81 dl 1.1 public void testGetSet(){
82 dl 1.3 AtomicLongArray ai = new AtomicLongArray(SIZE);
83     for (int i = 0; i < SIZE; ++i) {
84 dl 1.1 ai.set(i, 1);
85     assertEquals(1,ai.get(i));
86     ai.set(i, 2);
87     assertEquals(2,ai.get(i));
88     ai.set(i, -3);
89     assertEquals(-3,ai.get(i));
90     }
91     }
92    
93 dl 1.4 /**
94 dl 1.9 * 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 dl 1.5 * compareAndSet succeeds in changing value if equal to expected else fails
110 dl 1.4 */
111 dl 1.1 public void testCompareAndSet(){
112 dl 1.3 AtomicLongArray ai = new AtomicLongArray(SIZE);
113     for (int i = 0; i < SIZE; ++i) {
114 dl 1.1 ai.set(i, 1);
115     assertTrue(ai.compareAndSet(i, 1,2));
116     assertTrue(ai.compareAndSet(i, 2,-4));
117     assertEquals(-4,ai.get(i));
118     assertFalse(ai.compareAndSet(i, -5,7));
119     assertFalse((7 == ai.get(i)));
120     assertTrue(ai.compareAndSet(i, -4,7));
121     assertEquals(7,ai.get(i));
122     }
123     }
124    
125 dl 1.4 /**
126 dl 1.5 * 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 dl 1.4 */
152 dl 1.1 public void testWeakCompareAndSet(){
153 dl 1.3 AtomicLongArray ai = new AtomicLongArray(SIZE);
154     for (int i = 0; i < SIZE; ++i) {
155 dl 1.1 ai.set(i, 1);
156     while(!ai.weakCompareAndSet(i, 1,2));
157     while(!ai.weakCompareAndSet(i, 2,-4));
158     assertEquals(-4,ai.get(i));
159     while(!ai.weakCompareAndSet(i, -4,7));
160     assertEquals(7,ai.get(i));
161     }
162     }
163    
164 dl 1.4 /**
165 dl 1.5 * getAndSet returns previous value and sets to given value at given index
166 dl 1.4 */
167 dl 1.1 public void testGetAndSet(){
168 dl 1.3 AtomicLongArray ai = new AtomicLongArray(SIZE);
169     for (int i = 0; i < SIZE; ++i) {
170 dl 1.1 ai.set(i, 1);
171     assertEquals(1,ai.getAndSet(i,0));
172     assertEquals(0,ai.getAndSet(i,-10));
173     assertEquals(-10,ai.getAndSet(i,1));
174     }
175     }
176    
177 dl 1.4 /**
178 dl 1.5 * getAndAdd returns previous value and adds given value
179 dl 1.4 */
180 dl 1.1 public void testGetAndAdd(){
181 dl 1.3 AtomicLongArray ai = new AtomicLongArray(SIZE);
182     for (int i = 0; i < SIZE; ++i) {
183 dl 1.1 ai.set(i, 1);
184     assertEquals(1,ai.getAndAdd(i,2));
185     assertEquals(3,ai.get(i));
186     assertEquals(3,ai.getAndAdd(i,-4));
187     assertEquals(-1,ai.get(i));
188     }
189     }
190    
191 dl 1.4 /**
192 dl 1.5 * getAndDecrement returns previous value and decrements
193 dl 1.4 */
194 dl 1.1 public void testGetAndDecrement(){
195 dl 1.3 AtomicLongArray ai = new AtomicLongArray(SIZE);
196     for (int i = 0; i < SIZE; ++i) {
197 dl 1.1 ai.set(i, 1);
198     assertEquals(1,ai.getAndDecrement(i));
199     assertEquals(0,ai.getAndDecrement(i));
200     assertEquals(-1,ai.getAndDecrement(i));
201     }
202     }
203    
204 dl 1.4 /**
205 dl 1.5 * getAndIncrement returns previous value and increments
206 dl 1.4 */
207 dl 1.1 public void testGetAndIncrement(){
208 dl 1.3 AtomicLongArray ai = new AtomicLongArray(SIZE);
209     for (int i = 0; i < SIZE; ++i) {
210 dl 1.1 ai.set(i, 1);
211     assertEquals(1,ai.getAndIncrement(i));
212     assertEquals(2,ai.get(i));
213     ai.set(i,-2);
214     assertEquals(-2,ai.getAndIncrement(i));
215     assertEquals(-1,ai.getAndIncrement(i));
216     assertEquals(0,ai.getAndIncrement(i));
217     assertEquals(1,ai.get(i));
218     }
219     }
220    
221 dl 1.4 /**
222 dl 1.5 * addAndGet adds given value to current, and returns current value
223 dl 1.4 */
224 dl 1.1 public void testAddAndGet() {
225 dl 1.3 AtomicLongArray ai = new AtomicLongArray(SIZE);
226     for (int i = 0; i < SIZE; ++i) {
227 dl 1.1 ai.set(i, 1);
228     assertEquals(3,ai.addAndGet(i,2));
229     assertEquals(3,ai.get(i));
230     assertEquals(-1,ai.addAndGet(i,-4));
231     assertEquals(-1,ai.get(i));
232     }
233     }
234    
235 dl 1.4 /**
236 dl 1.5 * decrementAndGet decrements and returns current value
237 dl 1.4 */
238 dl 1.1 public void testDecrementAndGet(){
239 dl 1.3 AtomicLongArray ai = new AtomicLongArray(SIZE);
240     for (int i = 0; i < SIZE; ++i) {
241 dl 1.1 ai.set(i, 1);
242     assertEquals(0,ai.decrementAndGet(i));
243     assertEquals(-1,ai.decrementAndGet(i));
244     assertEquals(-2,ai.decrementAndGet(i));
245     assertEquals(-2,ai.get(i));
246     }
247     }
248    
249 dl 1.4 /**
250 dl 1.5 * incrementAndGet increments and returns current value
251 dl 1.4 */
252 dl 1.1 public void testIncrementAndGet() {
253 dl 1.3 AtomicLongArray ai = new AtomicLongArray(SIZE);
254     for (int i = 0; i < SIZE; ++i) {
255 dl 1.1 ai.set(i, 1);
256     assertEquals(2,ai.incrementAndGet(i));
257     assertEquals(2,ai.get(i));
258     ai.set(i, -2);
259     assertEquals(-1,ai.incrementAndGet(i));
260     assertEquals(0,ai.incrementAndGet(i));
261     assertEquals(1,ai.incrementAndGet(i));
262     assertEquals(1,ai.get(i));
263     }
264     }
265 dl 1.2
266 dl 1.3 static final long COUNTDOWN = 100000;
267    
268     class Counter implements Runnable {
269     final AtomicLongArray ai;
270     volatile long counts;
271     Counter(AtomicLongArray a) { ai = a; }
272     public void run() {
273     for (;;) {
274     boolean done = true;
275     for (int i = 0; i < ai.length(); ++i) {
276     long v = ai.get(i);
277     threadAssertTrue(v >= 0);
278     if (v != 0) {
279     done = false;
280     if (ai.compareAndSet(i, v, v-1))
281     ++counts;
282     }
283     }
284     if (done)
285     break;
286     }
287     }
288     }
289    
290 dl 1.4 /**
291 dl 1.5 * Multiple threads using same array of counters successfully
292     * update a number of times equal to total count
293 dl 1.4 */
294 dl 1.3 public void testCountingInMultipleThreads() {
295     try {
296     final AtomicLongArray ai = new AtomicLongArray(SIZE);
297     for (int i = 0; i < SIZE; ++i)
298     ai.set(i, COUNTDOWN);
299     Counter c1 = new Counter(ai);
300     Counter c2 = new Counter(ai);
301     Thread t1 = new Thread(c1);
302     Thread t2 = new Thread(c2);
303     t1.start();
304     t2.start();
305     t1.join();
306     t2.join();
307     assertEquals(c1.counts+c2.counts, SIZE * COUNTDOWN);
308     }
309     catch(InterruptedException ie) {
310 dl 1.4 unexpectedException();
311 dl 1.3 }
312     }
313    
314 dl 1.4 /**
315 dl 1.5 * a deserialized serialized array holds same values
316 dl 1.4 */
317 dl 1.2 public void testSerialization() {
318 dl 1.3 AtomicLongArray l = new AtomicLongArray(SIZE);
319     for (int i = 0; i < SIZE; ++i)
320 dl 1.2 l.set(i, -i);
321    
322     try {
323     ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
324     ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
325     out.writeObject(l);
326     out.close();
327    
328     ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
329     ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
330     AtomicLongArray r = (AtomicLongArray) in.readObject();
331 dl 1.3 for (int i = 0; i < SIZE; ++i) {
332 dl 1.2 assertEquals(l.get(i), r.get(i));
333     }
334     } catch(Exception e){
335 dl 1.4 unexpectedException();
336 dl 1.2 }
337 dl 1.8 }
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 dl 1.2 }
347    
348 dl 1.1
349     }