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. |
6 |
*/ |
7 |
|
8 |
import junit.framework.*; |
9 |
import java.util.concurrent.atomic.*; |
10 |
import java.io.*; |
11 |
|
12 |
public class AtomicLongArrayTest extends JSR166TestCase { |
13 |
public static void main (String[] args) { |
14 |
junit.textui.TestRunner.run (suite()); |
15 |
} |
16 |
public static Test suite() { |
17 |
return new TestSuite(AtomicLongArrayTest.class); |
18 |
} |
19 |
|
20 |
/** |
21 |
* |
22 |
*/ |
23 |
public void testConstructor(){ |
24 |
AtomicLongArray ai = new AtomicLongArray(SIZE); |
25 |
for (int i = 0; i < SIZE; ++i) |
26 |
assertEquals(0,ai.get(i)); |
27 |
} |
28 |
|
29 |
/** |
30 |
* |
31 |
*/ |
32 |
public void testGetSet(){ |
33 |
AtomicLongArray ai = new AtomicLongArray(SIZE); |
34 |
for (int i = 0; i < SIZE; ++i) { |
35 |
ai.set(i, 1); |
36 |
assertEquals(1,ai.get(i)); |
37 |
ai.set(i, 2); |
38 |
assertEquals(2,ai.get(i)); |
39 |
ai.set(i, -3); |
40 |
assertEquals(-3,ai.get(i)); |
41 |
} |
42 |
} |
43 |
|
44 |
/** |
45 |
* |
46 |
*/ |
47 |
public void testCompareAndSet(){ |
48 |
AtomicLongArray ai = new AtomicLongArray(SIZE); |
49 |
for (int i = 0; i < SIZE; ++i) { |
50 |
ai.set(i, 1); |
51 |
assertTrue(ai.compareAndSet(i, 1,2)); |
52 |
assertTrue(ai.compareAndSet(i, 2,-4)); |
53 |
assertEquals(-4,ai.get(i)); |
54 |
assertFalse(ai.compareAndSet(i, -5,7)); |
55 |
assertFalse((7 == ai.get(i))); |
56 |
assertTrue(ai.compareAndSet(i, -4,7)); |
57 |
assertEquals(7,ai.get(i)); |
58 |
} |
59 |
} |
60 |
|
61 |
/** |
62 |
* |
63 |
*/ |
64 |
public void testWeakCompareAndSet(){ |
65 |
AtomicLongArray ai = new AtomicLongArray(SIZE); |
66 |
for (int i = 0; i < SIZE; ++i) { |
67 |
ai.set(i, 1); |
68 |
while(!ai.weakCompareAndSet(i, 1,2)); |
69 |
while(!ai.weakCompareAndSet(i, 2,-4)); |
70 |
assertEquals(-4,ai.get(i)); |
71 |
while(!ai.weakCompareAndSet(i, -4,7)); |
72 |
assertEquals(7,ai.get(i)); |
73 |
} |
74 |
} |
75 |
|
76 |
/** |
77 |
* |
78 |
*/ |
79 |
public void testGetAndSet(){ |
80 |
AtomicLongArray ai = new AtomicLongArray(SIZE); |
81 |
for (int i = 0; i < SIZE; ++i) { |
82 |
ai.set(i, 1); |
83 |
assertEquals(1,ai.getAndSet(i,0)); |
84 |
assertEquals(0,ai.getAndSet(i,-10)); |
85 |
assertEquals(-10,ai.getAndSet(i,1)); |
86 |
} |
87 |
} |
88 |
|
89 |
/** |
90 |
* |
91 |
*/ |
92 |
public void testGetAndAdd(){ |
93 |
AtomicLongArray ai = new AtomicLongArray(SIZE); |
94 |
for (int i = 0; i < SIZE; ++i) { |
95 |
ai.set(i, 1); |
96 |
assertEquals(1,ai.getAndAdd(i,2)); |
97 |
assertEquals(3,ai.get(i)); |
98 |
assertEquals(3,ai.getAndAdd(i,-4)); |
99 |
assertEquals(-1,ai.get(i)); |
100 |
} |
101 |
} |
102 |
|
103 |
/** |
104 |
* |
105 |
*/ |
106 |
public void testGetAndDecrement(){ |
107 |
AtomicLongArray ai = new AtomicLongArray(SIZE); |
108 |
for (int i = 0; i < SIZE; ++i) { |
109 |
ai.set(i, 1); |
110 |
assertEquals(1,ai.getAndDecrement(i)); |
111 |
assertEquals(0,ai.getAndDecrement(i)); |
112 |
assertEquals(-1,ai.getAndDecrement(i)); |
113 |
} |
114 |
} |
115 |
|
116 |
/** |
117 |
* |
118 |
*/ |
119 |
public void testGetAndIncrement(){ |
120 |
AtomicLongArray ai = new AtomicLongArray(SIZE); |
121 |
for (int i = 0; i < SIZE; ++i) { |
122 |
ai.set(i, 1); |
123 |
assertEquals(1,ai.getAndIncrement(i)); |
124 |
assertEquals(2,ai.get(i)); |
125 |
ai.set(i,-2); |
126 |
assertEquals(-2,ai.getAndIncrement(i)); |
127 |
assertEquals(-1,ai.getAndIncrement(i)); |
128 |
assertEquals(0,ai.getAndIncrement(i)); |
129 |
assertEquals(1,ai.get(i)); |
130 |
} |
131 |
} |
132 |
|
133 |
/** |
134 |
* |
135 |
*/ |
136 |
public void testAddAndGet() { |
137 |
AtomicLongArray ai = new AtomicLongArray(SIZE); |
138 |
for (int i = 0; i < SIZE; ++i) { |
139 |
ai.set(i, 1); |
140 |
assertEquals(3,ai.addAndGet(i,2)); |
141 |
assertEquals(3,ai.get(i)); |
142 |
assertEquals(-1,ai.addAndGet(i,-4)); |
143 |
assertEquals(-1,ai.get(i)); |
144 |
} |
145 |
} |
146 |
|
147 |
/** |
148 |
* |
149 |
*/ |
150 |
public void testDecrementAndGet(){ |
151 |
AtomicLongArray ai = new AtomicLongArray(SIZE); |
152 |
for (int i = 0; i < SIZE; ++i) { |
153 |
ai.set(i, 1); |
154 |
assertEquals(0,ai.decrementAndGet(i)); |
155 |
assertEquals(-1,ai.decrementAndGet(i)); |
156 |
assertEquals(-2,ai.decrementAndGet(i)); |
157 |
assertEquals(-2,ai.get(i)); |
158 |
} |
159 |
} |
160 |
|
161 |
/** |
162 |
* |
163 |
*/ |
164 |
public void testIncrementAndGet() { |
165 |
AtomicLongArray ai = new AtomicLongArray(SIZE); |
166 |
for (int i = 0; i < SIZE; ++i) { |
167 |
ai.set(i, 1); |
168 |
assertEquals(2,ai.incrementAndGet(i)); |
169 |
assertEquals(2,ai.get(i)); |
170 |
ai.set(i, -2); |
171 |
assertEquals(-1,ai.incrementAndGet(i)); |
172 |
assertEquals(0,ai.incrementAndGet(i)); |
173 |
assertEquals(1,ai.incrementAndGet(i)); |
174 |
assertEquals(1,ai.get(i)); |
175 |
} |
176 |
} |
177 |
|
178 |
static final long COUNTDOWN = 100000; |
179 |
|
180 |
class Counter implements Runnable { |
181 |
final AtomicLongArray ai; |
182 |
volatile long counts; |
183 |
Counter(AtomicLongArray a) { ai = a; } |
184 |
public void run() { |
185 |
for (;;) { |
186 |
boolean done = true; |
187 |
for (int i = 0; i < ai.length(); ++i) { |
188 |
long v = ai.get(i); |
189 |
threadAssertTrue(v >= 0); |
190 |
if (v != 0) { |
191 |
done = false; |
192 |
if (ai.compareAndSet(i, v, v-1)) |
193 |
++counts; |
194 |
} |
195 |
} |
196 |
if (done) |
197 |
break; |
198 |
} |
199 |
} |
200 |
} |
201 |
|
202 |
/** |
203 |
* |
204 |
*/ |
205 |
public void testCountingInMultipleThreads() { |
206 |
try { |
207 |
final AtomicLongArray ai = new AtomicLongArray(SIZE); |
208 |
for (int i = 0; i < SIZE; ++i) |
209 |
ai.set(i, COUNTDOWN); |
210 |
Counter c1 = new Counter(ai); |
211 |
Counter c2 = new Counter(ai); |
212 |
Thread t1 = new Thread(c1); |
213 |
Thread t2 = new Thread(c2); |
214 |
t1.start(); |
215 |
t2.start(); |
216 |
t1.join(); |
217 |
t2.join(); |
218 |
assertEquals(c1.counts+c2.counts, SIZE * COUNTDOWN); |
219 |
} |
220 |
catch(InterruptedException ie) { |
221 |
unexpectedException(); |
222 |
} |
223 |
} |
224 |
|
225 |
/** |
226 |
* |
227 |
*/ |
228 |
public void testSerialization() { |
229 |
AtomicLongArray l = new AtomicLongArray(SIZE); |
230 |
for (int i = 0; i < SIZE; ++i) |
231 |
l.set(i, -i); |
232 |
|
233 |
try { |
234 |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
235 |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
236 |
out.writeObject(l); |
237 |
out.close(); |
238 |
|
239 |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
240 |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
241 |
AtomicLongArray r = (AtomicLongArray) in.readObject(); |
242 |
for (int i = 0; i < SIZE; ++i) { |
243 |
assertEquals(l.get(i), r.get(i)); |
244 |
} |
245 |
} catch(Exception e){ |
246 |
unexpectedException(); |
247 |
} |
248 |
} |
249 |
|
250 |
|
251 |
} |