7 |
|
|
8 |
|
import junit.framework.*; |
9 |
|
import java.util.concurrent.atomic.*; |
10 |
+ |
import java.io.*; |
11 |
|
|
12 |
< |
public class AtomicIntegerArrayTest extends TestCase |
12 |
< |
{ |
13 |
< |
static final int N = 10; |
12 |
> |
public class AtomicIntegerArrayTest extends JSR166TestCase { |
13 |
|
|
14 |
|
public static void main (String[] args) { |
15 |
|
junit.textui.TestRunner.run (suite()); |
18 |
|
return new TestSuite(AtomicIntegerArrayTest.class); |
19 |
|
} |
20 |
|
|
21 |
+ |
|
22 |
|
public void testConstructor(){ |
23 |
< |
AtomicIntegerArray ai = new AtomicIntegerArray(N); |
24 |
< |
for (int i = 0; i < N; ++i) |
23 |
> |
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
24 |
> |
for (int i = 0; i < SIZE; ++i) |
25 |
|
assertEquals(0,ai.get(i)); |
26 |
|
} |
27 |
|
|
28 |
|
public void testGetSet(){ |
29 |
< |
AtomicIntegerArray ai = new AtomicIntegerArray(N); |
30 |
< |
for (int i = 0; i < N; ++i) { |
29 |
> |
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
30 |
> |
for (int i = 0; i < SIZE; ++i) { |
31 |
|
ai.set(i, 1); |
32 |
|
assertEquals(1,ai.get(i)); |
33 |
|
ai.set(i, 2); |
38 |
|
} |
39 |
|
|
40 |
|
public void testCompareAndSet(){ |
41 |
< |
AtomicIntegerArray ai = new AtomicIntegerArray(N); |
42 |
< |
for (int i = 0; i < N; ++i) { |
41 |
> |
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
42 |
> |
for (int i = 0; i < SIZE; ++i) { |
43 |
|
ai.set(i, 1); |
44 |
|
assertTrue(ai.compareAndSet(i, 1,2)); |
45 |
|
assertTrue(ai.compareAndSet(i, 2,-4)); |
52 |
|
} |
53 |
|
|
54 |
|
public void testWeakCompareAndSet(){ |
55 |
< |
AtomicIntegerArray ai = new AtomicIntegerArray(N); |
56 |
< |
for (int i = 0; i < N; ++i) { |
55 |
> |
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
56 |
> |
for (int i = 0; i < SIZE; ++i) { |
57 |
|
ai.set(i, 1); |
58 |
|
while(!ai.weakCompareAndSet(i, 1,2)); |
59 |
|
while(!ai.weakCompareAndSet(i, 2,-4)); |
64 |
|
} |
65 |
|
|
66 |
|
public void testGetAndSet(){ |
67 |
< |
AtomicIntegerArray ai = new AtomicIntegerArray(N); |
68 |
< |
for (int i = 0; i < N; ++i) { |
67 |
> |
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
68 |
> |
for (int i = 0; i < SIZE; ++i) { |
69 |
|
ai.set(i, 1); |
70 |
|
assertEquals(1,ai.getAndSet(i,0)); |
71 |
|
assertEquals(0,ai.getAndSet(i,-10)); |
74 |
|
} |
75 |
|
|
76 |
|
public void testGetAndAdd(){ |
77 |
< |
AtomicIntegerArray ai = new AtomicIntegerArray(N); |
78 |
< |
for (int i = 0; i < N; ++i) { |
77 |
> |
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
78 |
> |
for (int i = 0; i < SIZE; ++i) { |
79 |
|
ai.set(i, 1); |
80 |
|
assertEquals(1,ai.getAndAdd(i,2)); |
81 |
|
assertEquals(3,ai.get(i)); |
85 |
|
} |
86 |
|
|
87 |
|
public void testGetAndDecrement(){ |
88 |
< |
AtomicIntegerArray ai = new AtomicIntegerArray(N); |
89 |
< |
for (int i = 0; i < N; ++i) { |
88 |
> |
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
89 |
> |
for (int i = 0; i < SIZE; ++i) { |
90 |
|
ai.set(i, 1); |
91 |
|
assertEquals(1,ai.getAndDecrement(i)); |
92 |
|
assertEquals(0,ai.getAndDecrement(i)); |
95 |
|
} |
96 |
|
|
97 |
|
public void testGetAndIncrement(){ |
98 |
< |
AtomicIntegerArray ai = new AtomicIntegerArray(N); |
99 |
< |
for (int i = 0; i < N; ++i) { |
98 |
> |
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
99 |
> |
for (int i = 0; i < SIZE; ++i) { |
100 |
|
ai.set(i, 1); |
101 |
|
assertEquals(1,ai.getAndIncrement(i)); |
102 |
|
assertEquals(2,ai.get(i)); |
109 |
|
} |
110 |
|
|
111 |
|
public void testAddAndGet() { |
112 |
< |
AtomicIntegerArray ai = new AtomicIntegerArray(N); |
113 |
< |
for (int i = 0; i < N; ++i) { |
112 |
> |
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
113 |
> |
for (int i = 0; i < SIZE; ++i) { |
114 |
|
ai.set(i, 1); |
115 |
|
assertEquals(3,ai.addAndGet(i,2)); |
116 |
|
assertEquals(3,ai.get(i)); |
120 |
|
} |
121 |
|
|
122 |
|
public void testDecrementAndGet(){ |
123 |
< |
AtomicIntegerArray ai = new AtomicIntegerArray(N); |
124 |
< |
for (int i = 0; i < N; ++i) { |
123 |
> |
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
124 |
> |
for (int i = 0; i < SIZE; ++i) { |
125 |
|
ai.set(i, 1); |
126 |
|
assertEquals(0,ai.decrementAndGet(i)); |
127 |
|
assertEquals(-1,ai.decrementAndGet(i)); |
131 |
|
} |
132 |
|
|
133 |
|
public void testIncrementAndGet() { |
134 |
< |
AtomicIntegerArray ai = new AtomicIntegerArray(N); |
135 |
< |
for (int i = 0; i < N; ++i) { |
134 |
> |
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
135 |
> |
for (int i = 0; i < SIZE; ++i) { |
136 |
|
ai.set(i, 1); |
137 |
|
assertEquals(2,ai.incrementAndGet(i)); |
138 |
|
assertEquals(2,ai.get(i)); |
144 |
|
} |
145 |
|
} |
146 |
|
|
147 |
+ |
static final int COUNTDOWN = 100000; |
148 |
+ |
|
149 |
+ |
class Counter implements Runnable { |
150 |
+ |
final AtomicIntegerArray ai; |
151 |
+ |
volatile int counts; |
152 |
+ |
Counter(AtomicIntegerArray a) { ai = a; } |
153 |
+ |
public void run() { |
154 |
+ |
for (;;) { |
155 |
+ |
boolean done = true; |
156 |
+ |
for (int i = 0; i < ai.length(); ++i) { |
157 |
+ |
int v = ai.get(i); |
158 |
+ |
threadAssertTrue(v >= 0); |
159 |
+ |
if (v != 0) { |
160 |
+ |
done = false; |
161 |
+ |
if (ai.compareAndSet(i, v, v-1)) |
162 |
+ |
++counts; |
163 |
+ |
} |
164 |
+ |
} |
165 |
+ |
if (done) |
166 |
+ |
break; |
167 |
+ |
} |
168 |
+ |
} |
169 |
+ |
} |
170 |
+ |
|
171 |
+ |
public void testCountingInMultipleThreads() { |
172 |
+ |
try { |
173 |
+ |
final AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
174 |
+ |
for (int i = 0; i < SIZE; ++i) |
175 |
+ |
ai.set(i, COUNTDOWN); |
176 |
+ |
Counter c1 = new Counter(ai); |
177 |
+ |
Counter c2 = new Counter(ai); |
178 |
+ |
Thread t1 = new Thread(c1); |
179 |
+ |
Thread t2 = new Thread(c2); |
180 |
+ |
t1.start(); |
181 |
+ |
t2.start(); |
182 |
+ |
t1.join(); |
183 |
+ |
t2.join(); |
184 |
+ |
assertEquals(c1.counts+c2.counts, SIZE * COUNTDOWN); |
185 |
+ |
} |
186 |
+ |
catch(InterruptedException ie) { |
187 |
+ |
fail("unexpected exception"); |
188 |
+ |
} |
189 |
+ |
} |
190 |
+ |
|
191 |
+ |
|
192 |
+ |
public void testSerialization() { |
193 |
+ |
AtomicIntegerArray l = new AtomicIntegerArray(SIZE); |
194 |
+ |
for (int i = 0; i < SIZE; ++i) |
195 |
+ |
l.set(i, -i); |
196 |
+ |
|
197 |
+ |
try { |
198 |
+ |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
199 |
+ |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
200 |
+ |
out.writeObject(l); |
201 |
+ |
out.close(); |
202 |
+ |
|
203 |
+ |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
204 |
+ |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
205 |
+ |
AtomicIntegerArray r = (AtomicIntegerArray) in.readObject(); |
206 |
+ |
for (int i = 0; i < SIZE; ++i) { |
207 |
+ |
assertEquals(l.get(i), r.get(i)); |
208 |
+ |
} |
209 |
+ |
} catch(Exception e){ |
210 |
+ |
e.printStackTrace(); |
211 |
+ |
fail("unexpected exception"); |
212 |
+ |
} |
213 |
+ |
} |
214 |
+ |
|
215 |
|
} |