7 |
|
|
8 |
|
import junit.framework.*; |
9 |
|
import java.util.concurrent.atomic.*; |
10 |
+ |
import java.io.*; |
11 |
|
|
12 |
< |
public class AtomicLongArrayTest extends TestCase |
12 |
< |
{ |
13 |
< |
static final int N = 10; |
14 |
< |
|
12 |
> |
public class AtomicLongArrayTest extends JSR166TestCase { |
13 |
|
public static void main (String[] args) { |
14 |
|
junit.textui.TestRunner.run (suite()); |
15 |
|
} |
17 |
|
return new TestSuite(AtomicLongArrayTest.class); |
18 |
|
} |
19 |
|
|
20 |
+ |
/** |
21 |
+ |
* |
22 |
+ |
*/ |
23 |
|
public void testConstructor(){ |
24 |
< |
AtomicLongArray ai = new AtomicLongArray(N); |
25 |
< |
for (int i = 0; i < N; ++i) |
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(N); |
34 |
< |
for (int i = 0; i < N; ++i) { |
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); |
41 |
|
} |
42 |
|
} |
43 |
|
|
44 |
+ |
/** |
45 |
+ |
* |
46 |
+ |
*/ |
47 |
|
public void testCompareAndSet(){ |
48 |
< |
AtomicLongArray ai = new AtomicLongArray(N); |
49 |
< |
for (int i = 0; i < N; ++i) { |
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)); |
58 |
|
} |
59 |
|
} |
60 |
|
|
61 |
+ |
/** |
62 |
+ |
* |
63 |
+ |
*/ |
64 |
|
public void testWeakCompareAndSet(){ |
65 |
< |
AtomicLongArray ai = new AtomicLongArray(N); |
66 |
< |
for (int i = 0; i < N; ++i) { |
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)); |
73 |
|
} |
74 |
|
} |
75 |
|
|
76 |
+ |
/** |
77 |
+ |
* |
78 |
+ |
*/ |
79 |
|
public void testGetAndSet(){ |
80 |
< |
AtomicLongArray ai = new AtomicLongArray(N); |
81 |
< |
for (int i = 0; i < N; ++i) { |
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)); |
86 |
|
} |
87 |
|
} |
88 |
|
|
89 |
+ |
/** |
90 |
+ |
* |
91 |
+ |
*/ |
92 |
|
public void testGetAndAdd(){ |
93 |
< |
AtomicLongArray ai = new AtomicLongArray(N); |
94 |
< |
for (int i = 0; i < N; ++i) { |
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)); |
100 |
|
} |
101 |
|
} |
102 |
|
|
103 |
+ |
/** |
104 |
+ |
* |
105 |
+ |
*/ |
106 |
|
public void testGetAndDecrement(){ |
107 |
< |
AtomicLongArray ai = new AtomicLongArray(N); |
108 |
< |
for (int i = 0; i < N; ++i) { |
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)); |
113 |
|
} |
114 |
|
} |
115 |
|
|
116 |
+ |
/** |
117 |
+ |
* |
118 |
+ |
*/ |
119 |
|
public void testGetAndIncrement(){ |
120 |
< |
AtomicLongArray ai = new AtomicLongArray(N); |
121 |
< |
for (int i = 0; i < N; ++i) { |
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)); |
130 |
|
} |
131 |
|
} |
132 |
|
|
133 |
+ |
/** |
134 |
+ |
* |
135 |
+ |
*/ |
136 |
|
public void testAddAndGet() { |
137 |
< |
AtomicLongArray ai = new AtomicLongArray(N); |
138 |
< |
for (int i = 0; i < N; ++i) { |
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)); |
144 |
|
} |
145 |
|
} |
146 |
|
|
147 |
+ |
/** |
148 |
+ |
* |
149 |
+ |
*/ |
150 |
|
public void testDecrementAndGet(){ |
151 |
< |
AtomicLongArray ai = new AtomicLongArray(N); |
152 |
< |
for (int i = 0; i < N; ++i) { |
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)); |
158 |
|
} |
159 |
|
} |
160 |
|
|
161 |
+ |
/** |
162 |
+ |
* |
163 |
+ |
*/ |
164 |
|
public void testIncrementAndGet() { |
165 |
< |
AtomicLongArray ai = new AtomicLongArray(N); |
166 |
< |
for (int i = 0; i < N; ++i) { |
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)); |
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 |
|
} |