ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AtomicLongArrayTest.java
Revision: 1.6
Committed: Sat Dec 27 19:26:43 2003 UTC (20 years, 4 months ago) by dl
Branch: MAIN
Changes since 1.5: +5 -4 lines
Log Message:
Headers reference Creative Commons

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