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 |
|
} |
18 |
|
} |
19 |
|
|
20 |
|
public void testConstructor(){ |
21 |
< |
AtomicLongArray ai = new AtomicLongArray(N); |
22 |
< |
for (int i = 0; i < N; ++i) |
21 |
> |
AtomicLongArray ai = new AtomicLongArray(SIZE); |
22 |
> |
for (int i = 0; i < SIZE; ++i) |
23 |
|
assertEquals(0,ai.get(i)); |
24 |
|
} |
25 |
|
|
26 |
|
public void testGetSet(){ |
27 |
< |
AtomicLongArray ai = new AtomicLongArray(N); |
28 |
< |
for (int i = 0; i < N; ++i) { |
27 |
> |
AtomicLongArray ai = new AtomicLongArray(SIZE); |
28 |
> |
for (int i = 0; i < SIZE; ++i) { |
29 |
|
ai.set(i, 1); |
30 |
|
assertEquals(1,ai.get(i)); |
31 |
|
ai.set(i, 2); |
36 |
|
} |
37 |
|
|
38 |
|
public void testCompareAndSet(){ |
39 |
< |
AtomicLongArray ai = new AtomicLongArray(N); |
40 |
< |
for (int i = 0; i < N; ++i) { |
39 |
> |
AtomicLongArray ai = new AtomicLongArray(SIZE); |
40 |
> |
for (int i = 0; i < SIZE; ++i) { |
41 |
|
ai.set(i, 1); |
42 |
|
assertTrue(ai.compareAndSet(i, 1,2)); |
43 |
|
assertTrue(ai.compareAndSet(i, 2,-4)); |
50 |
|
} |
51 |
|
|
52 |
|
public void testWeakCompareAndSet(){ |
53 |
< |
AtomicLongArray ai = new AtomicLongArray(N); |
54 |
< |
for (int i = 0; i < N; ++i) { |
53 |
> |
AtomicLongArray ai = new AtomicLongArray(SIZE); |
54 |
> |
for (int i = 0; i < SIZE; ++i) { |
55 |
|
ai.set(i, 1); |
56 |
|
while(!ai.weakCompareAndSet(i, 1,2)); |
57 |
|
while(!ai.weakCompareAndSet(i, 2,-4)); |
62 |
|
} |
63 |
|
|
64 |
|
public void testGetAndSet(){ |
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 |
|
assertEquals(1,ai.getAndSet(i,0)); |
69 |
|
assertEquals(0,ai.getAndSet(i,-10)); |
72 |
|
} |
73 |
|
|
74 |
|
public void testGetAndAdd(){ |
75 |
< |
AtomicLongArray ai = new AtomicLongArray(N); |
76 |
< |
for (int i = 0; i < N; ++i) { |
75 |
> |
AtomicLongArray ai = new AtomicLongArray(SIZE); |
76 |
> |
for (int i = 0; i < SIZE; ++i) { |
77 |
|
ai.set(i, 1); |
78 |
|
assertEquals(1,ai.getAndAdd(i,2)); |
79 |
|
assertEquals(3,ai.get(i)); |
83 |
|
} |
84 |
|
|
85 |
|
public void testGetAndDecrement(){ |
86 |
< |
AtomicLongArray ai = new AtomicLongArray(N); |
87 |
< |
for (int i = 0; i < N; ++i) { |
86 |
> |
AtomicLongArray ai = new AtomicLongArray(SIZE); |
87 |
> |
for (int i = 0; i < SIZE; ++i) { |
88 |
|
ai.set(i, 1); |
89 |
|
assertEquals(1,ai.getAndDecrement(i)); |
90 |
|
assertEquals(0,ai.getAndDecrement(i)); |
93 |
|
} |
94 |
|
|
95 |
|
public void testGetAndIncrement(){ |
96 |
< |
AtomicLongArray ai = new AtomicLongArray(N); |
97 |
< |
for (int i = 0; i < N; ++i) { |
96 |
> |
AtomicLongArray ai = new AtomicLongArray(SIZE); |
97 |
> |
for (int i = 0; i < SIZE; ++i) { |
98 |
|
ai.set(i, 1); |
99 |
|
assertEquals(1,ai.getAndIncrement(i)); |
100 |
|
assertEquals(2,ai.get(i)); |
107 |
|
} |
108 |
|
|
109 |
|
public void testAddAndGet() { |
110 |
< |
AtomicLongArray ai = new AtomicLongArray(N); |
111 |
< |
for (int i = 0; i < N; ++i) { |
110 |
> |
AtomicLongArray ai = new AtomicLongArray(SIZE); |
111 |
> |
for (int i = 0; i < SIZE; ++i) { |
112 |
|
ai.set(i, 1); |
113 |
|
assertEquals(3,ai.addAndGet(i,2)); |
114 |
|
assertEquals(3,ai.get(i)); |
118 |
|
} |
119 |
|
|
120 |
|
public void testDecrementAndGet(){ |
121 |
< |
AtomicLongArray ai = new AtomicLongArray(N); |
122 |
< |
for (int i = 0; i < N; ++i) { |
121 |
> |
AtomicLongArray ai = new AtomicLongArray(SIZE); |
122 |
> |
for (int i = 0; i < SIZE; ++i) { |
123 |
|
ai.set(i, 1); |
124 |
|
assertEquals(0,ai.decrementAndGet(i)); |
125 |
|
assertEquals(-1,ai.decrementAndGet(i)); |
129 |
|
} |
130 |
|
|
131 |
|
public void testIncrementAndGet() { |
132 |
< |
AtomicLongArray ai = new AtomicLongArray(N); |
133 |
< |
for (int i = 0; i < N; ++i) { |
132 |
> |
AtomicLongArray ai = new AtomicLongArray(SIZE); |
133 |
> |
for (int i = 0; i < SIZE; ++i) { |
134 |
|
ai.set(i, 1); |
135 |
|
assertEquals(2,ai.incrementAndGet(i)); |
136 |
|
assertEquals(2,ai.get(i)); |
142 |
|
} |
143 |
|
} |
144 |
|
|
145 |
+ |
static final long COUNTDOWN = 100000; |
146 |
+ |
|
147 |
+ |
class Counter implements Runnable { |
148 |
+ |
final AtomicLongArray ai; |
149 |
+ |
volatile long counts; |
150 |
+ |
Counter(AtomicLongArray a) { ai = a; } |
151 |
+ |
public void run() { |
152 |
+ |
for (;;) { |
153 |
+ |
boolean done = true; |
154 |
+ |
for (int i = 0; i < ai.length(); ++i) { |
155 |
+ |
long v = ai.get(i); |
156 |
+ |
threadAssertTrue(v >= 0); |
157 |
+ |
if (v != 0) { |
158 |
+ |
done = false; |
159 |
+ |
if (ai.compareAndSet(i, v, v-1)) |
160 |
+ |
++counts; |
161 |
+ |
} |
162 |
+ |
} |
163 |
+ |
if (done) |
164 |
+ |
break; |
165 |
+ |
} |
166 |
+ |
} |
167 |
+ |
} |
168 |
+ |
|
169 |
+ |
public void testCountingInMultipleThreads() { |
170 |
+ |
try { |
171 |
+ |
final AtomicLongArray ai = new AtomicLongArray(SIZE); |
172 |
+ |
for (int i = 0; i < SIZE; ++i) |
173 |
+ |
ai.set(i, COUNTDOWN); |
174 |
+ |
Counter c1 = new Counter(ai); |
175 |
+ |
Counter c2 = new Counter(ai); |
176 |
+ |
Thread t1 = new Thread(c1); |
177 |
+ |
Thread t2 = new Thread(c2); |
178 |
+ |
t1.start(); |
179 |
+ |
t2.start(); |
180 |
+ |
t1.join(); |
181 |
+ |
t2.join(); |
182 |
+ |
assertEquals(c1.counts+c2.counts, SIZE * COUNTDOWN); |
183 |
+ |
} |
184 |
+ |
catch(InterruptedException ie) { |
185 |
+ |
fail("unexpected exception"); |
186 |
+ |
} |
187 |
+ |
} |
188 |
+ |
|
189 |
+ |
public void testSerialization() { |
190 |
+ |
AtomicLongArray l = new AtomicLongArray(SIZE); |
191 |
+ |
for (int i = 0; i < SIZE; ++i) |
192 |
+ |
l.set(i, -i); |
193 |
+ |
|
194 |
+ |
try { |
195 |
+ |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
196 |
+ |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
197 |
+ |
out.writeObject(l); |
198 |
+ |
out.close(); |
199 |
+ |
|
200 |
+ |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
201 |
+ |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
202 |
+ |
AtomicLongArray r = (AtomicLongArray) in.readObject(); |
203 |
+ |
for (int i = 0; i < SIZE; ++i) { |
204 |
+ |
assertEquals(l.get(i), r.get(i)); |
205 |
+ |
} |
206 |
+ |
} catch(Exception e){ |
207 |
+ |
e.printStackTrace(); |
208 |
+ |
fail("unexpected exception"); |
209 |
+ |
} |
210 |
+ |
} |
211 |
+ |
|
212 |
+ |
|
213 |
|
} |