1 |
dl |
1.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 |
dl |
1.2 |
import java.io.*; |
11 |
dl |
1.1 |
|
12 |
dl |
1.3 |
public class AtomicLongArrayTest extends JSR166TestCase { |
13 |
dl |
1.1 |
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 |
dl |
1.4 |
/** |
21 |
|
|
* |
22 |
|
|
*/ |
23 |
dl |
1.1 |
public void testConstructor(){ |
24 |
dl |
1.3 |
AtomicLongArray ai = new AtomicLongArray(SIZE); |
25 |
|
|
for (int i = 0; i < SIZE; ++i) |
26 |
dl |
1.1 |
assertEquals(0,ai.get(i)); |
27 |
|
|
} |
28 |
|
|
|
29 |
dl |
1.4 |
/** |
30 |
|
|
* |
31 |
|
|
*/ |
32 |
dl |
1.1 |
public void testGetSet(){ |
33 |
dl |
1.3 |
AtomicLongArray ai = new AtomicLongArray(SIZE); |
34 |
|
|
for (int i = 0; i < SIZE; ++i) { |
35 |
dl |
1.1 |
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 |
dl |
1.4 |
/** |
45 |
|
|
* |
46 |
|
|
*/ |
47 |
dl |
1.1 |
public void testCompareAndSet(){ |
48 |
dl |
1.3 |
AtomicLongArray ai = new AtomicLongArray(SIZE); |
49 |
|
|
for (int i = 0; i < SIZE; ++i) { |
50 |
dl |
1.1 |
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 |
dl |
1.4 |
/** |
62 |
|
|
* |
63 |
|
|
*/ |
64 |
dl |
1.1 |
public void testWeakCompareAndSet(){ |
65 |
dl |
1.3 |
AtomicLongArray ai = new AtomicLongArray(SIZE); |
66 |
|
|
for (int i = 0; i < SIZE; ++i) { |
67 |
dl |
1.1 |
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 |
dl |
1.4 |
/** |
77 |
|
|
* |
78 |
|
|
*/ |
79 |
dl |
1.1 |
public void testGetAndSet(){ |
80 |
dl |
1.3 |
AtomicLongArray ai = new AtomicLongArray(SIZE); |
81 |
|
|
for (int i = 0; i < SIZE; ++i) { |
82 |
dl |
1.1 |
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 |
dl |
1.4 |
/** |
90 |
|
|
* |
91 |
|
|
*/ |
92 |
dl |
1.1 |
public void testGetAndAdd(){ |
93 |
dl |
1.3 |
AtomicLongArray ai = new AtomicLongArray(SIZE); |
94 |
|
|
for (int i = 0; i < SIZE; ++i) { |
95 |
dl |
1.1 |
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 |
dl |
1.4 |
/** |
104 |
|
|
* |
105 |
|
|
*/ |
106 |
dl |
1.1 |
public void testGetAndDecrement(){ |
107 |
dl |
1.3 |
AtomicLongArray ai = new AtomicLongArray(SIZE); |
108 |
|
|
for (int i = 0; i < SIZE; ++i) { |
109 |
dl |
1.1 |
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 |
dl |
1.4 |
/** |
117 |
|
|
* |
118 |
|
|
*/ |
119 |
dl |
1.1 |
public void testGetAndIncrement(){ |
120 |
dl |
1.3 |
AtomicLongArray ai = new AtomicLongArray(SIZE); |
121 |
|
|
for (int i = 0; i < SIZE; ++i) { |
122 |
dl |
1.1 |
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 |
dl |
1.4 |
/** |
134 |
|
|
* |
135 |
|
|
*/ |
136 |
dl |
1.1 |
public void testAddAndGet() { |
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 |
|
|
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 |
dl |
1.4 |
/** |
148 |
|
|
* |
149 |
|
|
*/ |
150 |
dl |
1.1 |
public void testDecrementAndGet(){ |
151 |
dl |
1.3 |
AtomicLongArray ai = new AtomicLongArray(SIZE); |
152 |
|
|
for (int i = 0; i < SIZE; ++i) { |
153 |
dl |
1.1 |
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 |
dl |
1.4 |
/** |
162 |
|
|
* |
163 |
|
|
*/ |
164 |
dl |
1.1 |
public void testIncrementAndGet() { |
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(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 |
dl |
1.2 |
|
178 |
dl |
1.3 |
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 |
dl |
1.4 |
/** |
203 |
|
|
* |
204 |
|
|
*/ |
205 |
dl |
1.3 |
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 |
dl |
1.4 |
unexpectedException(); |
222 |
dl |
1.3 |
} |
223 |
|
|
} |
224 |
|
|
|
225 |
dl |
1.4 |
/** |
226 |
|
|
* |
227 |
|
|
*/ |
228 |
dl |
1.2 |
public void testSerialization() { |
229 |
dl |
1.3 |
AtomicLongArray l = new AtomicLongArray(SIZE); |
230 |
|
|
for (int i = 0; i < SIZE; ++i) |
231 |
dl |
1.2 |
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 |
dl |
1.3 |
for (int i = 0; i < SIZE; ++i) { |
243 |
dl |
1.2 |
assertEquals(l.get(i), r.get(i)); |
244 |
|
|
} |
245 |
|
|
} catch(Exception e){ |
246 |
dl |
1.4 |
unexpectedException(); |
247 |
dl |
1.2 |
} |
248 |
|
|
} |
249 |
|
|
|
250 |
dl |
1.1 |
|
251 |
|
|
} |