ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AtomicLongArrayTest.java
Revision: 1.8
Committed: Sat Jan 10 01:41:59 2004 UTC (20 years, 4 months ago) by dl
Branch: MAIN
CVS Tags: JSR166_PFD
Changes since 1.7: +10 -0 lines
Log Message:
Test toString

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.5 * compareAndSet succeeds in changing value if equal to expected else fails
95 dl 1.4 */
96 dl 1.1 public void testCompareAndSet(){
97 dl 1.3 AtomicLongArray ai = new AtomicLongArray(SIZE);
98     for (int i = 0; i < SIZE; ++i) {
99 dl 1.1 ai.set(i, 1);
100     assertTrue(ai.compareAndSet(i, 1,2));
101     assertTrue(ai.compareAndSet(i, 2,-4));
102     assertEquals(-4,ai.get(i));
103     assertFalse(ai.compareAndSet(i, -5,7));
104     assertFalse((7 == ai.get(i)));
105     assertTrue(ai.compareAndSet(i, -4,7));
106     assertEquals(7,ai.get(i));
107     }
108     }
109    
110 dl 1.4 /**
111 dl 1.5 * 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 dl 1.4 */
137 dl 1.1 public void testWeakCompareAndSet(){
138 dl 1.3 AtomicLongArray ai = new AtomicLongArray(SIZE);
139     for (int i = 0; i < SIZE; ++i) {
140 dl 1.1 ai.set(i, 1);
141     while(!ai.weakCompareAndSet(i, 1,2));
142     while(!ai.weakCompareAndSet(i, 2,-4));
143     assertEquals(-4,ai.get(i));
144     while(!ai.weakCompareAndSet(i, -4,7));
145     assertEquals(7,ai.get(i));
146     }
147     }
148    
149 dl 1.4 /**
150 dl 1.5 * getAndSet returns previous value and sets to given value at given index
151 dl 1.4 */
152 dl 1.1 public void testGetAndSet(){
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     assertEquals(1,ai.getAndSet(i,0));
157     assertEquals(0,ai.getAndSet(i,-10));
158     assertEquals(-10,ai.getAndSet(i,1));
159     }
160     }
161    
162 dl 1.4 /**
163 dl 1.5 * getAndAdd returns previous value and adds given value
164 dl 1.4 */
165 dl 1.1 public void testGetAndAdd(){
166 dl 1.3 AtomicLongArray ai = new AtomicLongArray(SIZE);
167     for (int i = 0; i < SIZE; ++i) {
168 dl 1.1 ai.set(i, 1);
169     assertEquals(1,ai.getAndAdd(i,2));
170     assertEquals(3,ai.get(i));
171     assertEquals(3,ai.getAndAdd(i,-4));
172     assertEquals(-1,ai.get(i));
173     }
174     }
175    
176 dl 1.4 /**
177 dl 1.5 * getAndDecrement returns previous value and decrements
178 dl 1.4 */
179 dl 1.1 public void testGetAndDecrement(){
180 dl 1.3 AtomicLongArray ai = new AtomicLongArray(SIZE);
181     for (int i = 0; i < SIZE; ++i) {
182 dl 1.1 ai.set(i, 1);
183     assertEquals(1,ai.getAndDecrement(i));
184     assertEquals(0,ai.getAndDecrement(i));
185     assertEquals(-1,ai.getAndDecrement(i));
186     }
187     }
188    
189 dl 1.4 /**
190 dl 1.5 * getAndIncrement returns previous value and increments
191 dl 1.4 */
192 dl 1.1 public void testGetAndIncrement(){
193 dl 1.3 AtomicLongArray ai = new AtomicLongArray(SIZE);
194     for (int i = 0; i < SIZE; ++i) {
195 dl 1.1 ai.set(i, 1);
196     assertEquals(1,ai.getAndIncrement(i));
197     assertEquals(2,ai.get(i));
198     ai.set(i,-2);
199     assertEquals(-2,ai.getAndIncrement(i));
200     assertEquals(-1,ai.getAndIncrement(i));
201     assertEquals(0,ai.getAndIncrement(i));
202     assertEquals(1,ai.get(i));
203     }
204     }
205    
206 dl 1.4 /**
207 dl 1.5 * addAndGet adds given value to current, and returns current value
208 dl 1.4 */
209 dl 1.1 public void testAddAndGet() {
210 dl 1.3 AtomicLongArray ai = new AtomicLongArray(SIZE);
211     for (int i = 0; i < SIZE; ++i) {
212 dl 1.1 ai.set(i, 1);
213     assertEquals(3,ai.addAndGet(i,2));
214     assertEquals(3,ai.get(i));
215     assertEquals(-1,ai.addAndGet(i,-4));
216     assertEquals(-1,ai.get(i));
217     }
218     }
219    
220 dl 1.4 /**
221 dl 1.5 * decrementAndGet decrements and returns current value
222 dl 1.4 */
223 dl 1.1 public void testDecrementAndGet(){
224 dl 1.3 AtomicLongArray ai = new AtomicLongArray(SIZE);
225     for (int i = 0; i < SIZE; ++i) {
226 dl 1.1 ai.set(i, 1);
227     assertEquals(0,ai.decrementAndGet(i));
228     assertEquals(-1,ai.decrementAndGet(i));
229     assertEquals(-2,ai.decrementAndGet(i));
230     assertEquals(-2,ai.get(i));
231     }
232     }
233    
234 dl 1.4 /**
235 dl 1.5 * incrementAndGet increments and returns current value
236 dl 1.4 */
237 dl 1.1 public void testIncrementAndGet() {
238 dl 1.3 AtomicLongArray ai = new AtomicLongArray(SIZE);
239     for (int i = 0; i < SIZE; ++i) {
240 dl 1.1 ai.set(i, 1);
241     assertEquals(2,ai.incrementAndGet(i));
242     assertEquals(2,ai.get(i));
243     ai.set(i, -2);
244     assertEquals(-1,ai.incrementAndGet(i));
245     assertEquals(0,ai.incrementAndGet(i));
246     assertEquals(1,ai.incrementAndGet(i));
247     assertEquals(1,ai.get(i));
248     }
249     }
250 dl 1.2
251 dl 1.3 static final long COUNTDOWN = 100000;
252    
253     class Counter implements Runnable {
254     final AtomicLongArray ai;
255     volatile long counts;
256     Counter(AtomicLongArray a) { ai = a; }
257     public void run() {
258     for (;;) {
259     boolean done = true;
260     for (int i = 0; i < ai.length(); ++i) {
261     long v = ai.get(i);
262     threadAssertTrue(v >= 0);
263     if (v != 0) {
264     done = false;
265     if (ai.compareAndSet(i, v, v-1))
266     ++counts;
267     }
268     }
269     if (done)
270     break;
271     }
272     }
273     }
274    
275 dl 1.4 /**
276 dl 1.5 * Multiple threads using same array of counters successfully
277     * update a number of times equal to total count
278 dl 1.4 */
279 dl 1.3 public void testCountingInMultipleThreads() {
280     try {
281     final AtomicLongArray ai = new AtomicLongArray(SIZE);
282     for (int i = 0; i < SIZE; ++i)
283     ai.set(i, COUNTDOWN);
284     Counter c1 = new Counter(ai);
285     Counter c2 = new Counter(ai);
286     Thread t1 = new Thread(c1);
287     Thread t2 = new Thread(c2);
288     t1.start();
289     t2.start();
290     t1.join();
291     t2.join();
292     assertEquals(c1.counts+c2.counts, SIZE * COUNTDOWN);
293     }
294     catch(InterruptedException ie) {
295 dl 1.4 unexpectedException();
296 dl 1.3 }
297     }
298    
299 dl 1.4 /**
300 dl 1.5 * a deserialized serialized array holds same values
301 dl 1.4 */
302 dl 1.2 public void testSerialization() {
303 dl 1.3 AtomicLongArray l = new AtomicLongArray(SIZE);
304     for (int i = 0; i < SIZE; ++i)
305 dl 1.2 l.set(i, -i);
306    
307     try {
308     ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
309     ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
310     out.writeObject(l);
311     out.close();
312    
313     ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
314     ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
315     AtomicLongArray r = (AtomicLongArray) in.readObject();
316 dl 1.3 for (int i = 0; i < SIZE; ++i) {
317 dl 1.2 assertEquals(l.get(i), r.get(i));
318     }
319     } catch(Exception e){
320 dl 1.4 unexpectedException();
321 dl 1.2 }
322 dl 1.8 }
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 dl 1.2 }
332    
333 dl 1.1
334     }