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