1 |
|
/* |
2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
< |
* http://creativecommons.org/licenses/publicdomain |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
|
import junit.framework.*; |
10 |
< |
import java.util.concurrent.atomic.*; |
11 |
< |
import java.io.*; |
12 |
< |
import java.util.*; |
10 |
> |
import java.util.Arrays; |
11 |
> |
import java.util.concurrent.atomic.AtomicIntegerArray; |
12 |
|
|
13 |
|
public class AtomicIntegerArrayTest extends JSR166TestCase { |
14 |
|
|
15 |
< |
public static void main (String[] args) { |
16 |
< |
junit.textui.TestRunner.run (suite()); |
15 |
> |
public static void main(String[] args) { |
16 |
> |
junit.textui.TestRunner.run(suite()); |
17 |
|
} |
18 |
|
public static Test suite() { |
19 |
|
return new TestSuite(AtomicIntegerArrayTest.class); |
20 |
|
} |
21 |
|
|
23 |
– |
|
22 |
|
/** |
23 |
|
* constructor creates array of given size with all elements zero |
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)); |
26 |
> |
AtomicIntegerArray aa = new AtomicIntegerArray(SIZE); |
27 |
> |
for (int i = 0; i < SIZE; i++) |
28 |
> |
assertEquals(0, aa.get(i)); |
29 |
|
} |
30 |
|
|
31 |
|
/** |
34 |
|
public void testConstructor2NPE() { |
35 |
|
try { |
36 |
|
int[] a = null; |
37 |
< |
AtomicIntegerArray ai = new AtomicIntegerArray(a); |
38 |
< |
} catch (NullPointerException success) { |
39 |
< |
} catch (Exception ex) { |
42 |
< |
unexpectedException(); |
43 |
< |
} |
37 |
> |
AtomicIntegerArray aa = new AtomicIntegerArray(a); |
38 |
> |
shouldThrow(); |
39 |
> |
} catch (NullPointerException success) {} |
40 |
|
} |
41 |
|
|
42 |
|
/** |
43 |
|
* constructor with array is of same size and has all elements |
44 |
|
*/ |
45 |
|
public void testConstructor2() { |
46 |
< |
int[] a = { 17, 3, -42, 99, -7}; |
47 |
< |
AtomicIntegerArray ai = new AtomicIntegerArray(a); |
48 |
< |
assertEquals(a.length, ai.length()); |
49 |
< |
for (int i = 0; i < a.length; ++i) |
50 |
< |
assertEquals(a[i], ai.get(i)); |
46 |
> |
int[] a = { 17, 3, -42, 99, -7 }; |
47 |
> |
AtomicIntegerArray aa = new AtomicIntegerArray(a); |
48 |
> |
assertEquals(a.length, aa.length()); |
49 |
> |
for (int i = 0; i < a.length; i++) |
50 |
> |
assertEquals(a[i], aa.get(i)); |
51 |
|
} |
52 |
|
|
53 |
|
/** |
54 |
|
* get and set for out of bound indices throw IndexOutOfBoundsException |
55 |
|
*/ |
56 |
< |
public void testIndexing(){ |
57 |
< |
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
58 |
< |
try { |
59 |
< |
ai.get(SIZE); |
60 |
< |
} catch(IndexOutOfBoundsException success){ |
61 |
< |
} |
62 |
< |
try { |
63 |
< |
ai.get(-1); |
64 |
< |
} catch(IndexOutOfBoundsException success){ |
65 |
< |
} |
66 |
< |
try { |
67 |
< |
ai.set(SIZE, 0); |
68 |
< |
} catch(IndexOutOfBoundsException success){ |
69 |
< |
} |
70 |
< |
try { |
71 |
< |
ai.set(-1, 0); |
72 |
< |
} catch(IndexOutOfBoundsException success){ |
56 |
> |
public void testIndexing() { |
57 |
> |
AtomicIntegerArray aa = new AtomicIntegerArray(SIZE); |
58 |
> |
for (int index : new int[] { -1, SIZE }) { |
59 |
> |
try { |
60 |
> |
aa.get(index); |
61 |
> |
shouldThrow(); |
62 |
> |
} catch (IndexOutOfBoundsException success) {} |
63 |
> |
try { |
64 |
> |
aa.set(index, 1); |
65 |
> |
shouldThrow(); |
66 |
> |
} catch (IndexOutOfBoundsException success) {} |
67 |
> |
try { |
68 |
> |
aa.lazySet(index, 1); |
69 |
> |
shouldThrow(); |
70 |
> |
} catch (IndexOutOfBoundsException success) {} |
71 |
> |
try { |
72 |
> |
aa.compareAndSet(index, 1, 2); |
73 |
> |
shouldThrow(); |
74 |
> |
} catch (IndexOutOfBoundsException success) {} |
75 |
> |
try { |
76 |
> |
aa.weakCompareAndSet(index, 1, 2); |
77 |
> |
shouldThrow(); |
78 |
> |
} catch (IndexOutOfBoundsException success) {} |
79 |
> |
try { |
80 |
> |
aa.getAndAdd(index, 1); |
81 |
> |
shouldThrow(); |
82 |
> |
} catch (IndexOutOfBoundsException success) {} |
83 |
> |
try { |
84 |
> |
aa.addAndGet(index, 1); |
85 |
> |
shouldThrow(); |
86 |
> |
} catch (IndexOutOfBoundsException success) {} |
87 |
|
} |
88 |
|
} |
89 |
|
|
91 |
|
* get returns the last value set at index |
92 |
|
*/ |
93 |
|
public void testGetSet() { |
94 |
< |
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
95 |
< |
for (int i = 0; i < SIZE; ++i) { |
96 |
< |
ai.set(i, 1); |
97 |
< |
assertEquals(1,ai.get(i)); |
98 |
< |
ai.set(i, 2); |
99 |
< |
assertEquals(2,ai.get(i)); |
100 |
< |
ai.set(i, -3); |
101 |
< |
assertEquals(-3,ai.get(i)); |
94 |
> |
AtomicIntegerArray aa = new AtomicIntegerArray(SIZE); |
95 |
> |
for (int i = 0; i < SIZE; i++) { |
96 |
> |
aa.set(i, 1); |
97 |
> |
assertEquals(1, aa.get(i)); |
98 |
> |
aa.set(i, 2); |
99 |
> |
assertEquals(2, aa.get(i)); |
100 |
> |
aa.set(i, -3); |
101 |
> |
assertEquals(-3, aa.get(i)); |
102 |
|
} |
103 |
|
} |
104 |
|
|
106 |
|
* get returns the last value lazySet at index by same thread |
107 |
|
*/ |
108 |
|
public void testGetLazySet() { |
109 |
< |
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
110 |
< |
for (int i = 0; i < SIZE; ++i) { |
111 |
< |
ai.lazySet(i, 1); |
112 |
< |
assertEquals(1,ai.get(i)); |
113 |
< |
ai.lazySet(i, 2); |
114 |
< |
assertEquals(2,ai.get(i)); |
115 |
< |
ai.lazySet(i, -3); |
116 |
< |
assertEquals(-3,ai.get(i)); |
109 |
> |
AtomicIntegerArray aa = new AtomicIntegerArray(SIZE); |
110 |
> |
for (int i = 0; i < SIZE; i++) { |
111 |
> |
aa.lazySet(i, 1); |
112 |
> |
assertEquals(1, aa.get(i)); |
113 |
> |
aa.lazySet(i, 2); |
114 |
> |
assertEquals(2, aa.get(i)); |
115 |
> |
aa.lazySet(i, -3); |
116 |
> |
assertEquals(-3, aa.get(i)); |
117 |
|
} |
118 |
|
} |
119 |
|
|
121 |
|
* compareAndSet succeeds in changing value if equal to expected else fails |
122 |
|
*/ |
123 |
|
public void testCompareAndSet() { |
124 |
< |
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
125 |
< |
for (int i = 0; i < SIZE; ++i) { |
126 |
< |
ai.set(i, 1); |
127 |
< |
assertTrue(ai.compareAndSet(i, 1,2)); |
128 |
< |
assertTrue(ai.compareAndSet(i, 2,-4)); |
129 |
< |
assertEquals(-4,ai.get(i)); |
130 |
< |
assertFalse(ai.compareAndSet(i, -5,7)); |
131 |
< |
assertFalse((7 == ai.get(i))); |
132 |
< |
assertTrue(ai.compareAndSet(i, -4,7)); |
133 |
< |
assertEquals(7,ai.get(i)); |
124 |
> |
AtomicIntegerArray aa = new AtomicIntegerArray(SIZE); |
125 |
> |
for (int i = 0; i < SIZE; i++) { |
126 |
> |
aa.set(i, 1); |
127 |
> |
assertTrue(aa.compareAndSet(i, 1, 2)); |
128 |
> |
assertTrue(aa.compareAndSet(i, 2, -4)); |
129 |
> |
assertEquals(-4, aa.get(i)); |
130 |
> |
assertFalse(aa.compareAndSet(i, -5, 7)); |
131 |
> |
assertEquals(-4, aa.get(i)); |
132 |
> |
assertTrue(aa.compareAndSet(i, -4, 7)); |
133 |
> |
assertEquals(7, aa.get(i)); |
134 |
|
} |
135 |
|
} |
136 |
|
|
138 |
|
* compareAndSet in one thread enables another waiting for value |
139 |
|
* to succeed |
140 |
|
*/ |
141 |
< |
public void testCompareAndSetInMultipleThreads() { |
141 |
> |
public void testCompareAndSetInMultipleThreads() throws Exception { |
142 |
|
final AtomicIntegerArray a = new AtomicIntegerArray(1); |
143 |
|
a.set(0, 1); |
144 |
< |
Thread t = new Thread(new Runnable() { |
145 |
< |
public void run() { |
146 |
< |
while(!a.compareAndSet(0, 2, 3)) Thread.yield(); |
147 |
< |
}}); |
148 |
< |
try { |
149 |
< |
t.start(); |
150 |
< |
assertTrue(a.compareAndSet(0, 1, 2)); |
151 |
< |
t.join(LONG_DELAY_MS); |
152 |
< |
assertFalse(t.isAlive()); |
153 |
< |
assertEquals(a.get(0), 3); |
154 |
< |
} |
145 |
< |
catch(Exception e) { |
146 |
< |
unexpectedException(); |
147 |
< |
} |
144 |
> |
Thread t = new Thread(new CheckedRunnable() { |
145 |
> |
public void realRun() { |
146 |
> |
while (!a.compareAndSet(0, 2, 3)) |
147 |
> |
Thread.yield(); |
148 |
> |
}}); |
149 |
> |
|
150 |
> |
t.start(); |
151 |
> |
assertTrue(a.compareAndSet(0, 1, 2)); |
152 |
> |
t.join(LONG_DELAY_MS); |
153 |
> |
assertFalse(t.isAlive()); |
154 |
> |
assertEquals(3, a.get(0)); |
155 |
|
} |
156 |
|
|
157 |
|
/** |
159 |
|
* to expected |
160 |
|
*/ |
161 |
|
public void testWeakCompareAndSet() { |
162 |
< |
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
163 |
< |
for (int i = 0; i < SIZE; ++i) { |
164 |
< |
ai.set(i, 1); |
165 |
< |
while(!ai.weakCompareAndSet(i, 1,2)); |
166 |
< |
while(!ai.weakCompareAndSet(i, 2,-4)); |
167 |
< |
assertEquals(-4,ai.get(i)); |
168 |
< |
while(!ai.weakCompareAndSet(i, -4,7)); |
169 |
< |
assertEquals(7,ai.get(i)); |
162 |
> |
AtomicIntegerArray aa = new AtomicIntegerArray(SIZE); |
163 |
> |
for (int i = 0; i < SIZE; i++) { |
164 |
> |
aa.set(i, 1); |
165 |
> |
while (!aa.weakCompareAndSet(i, 1, 2)); |
166 |
> |
while (!aa.weakCompareAndSet(i, 2, -4)); |
167 |
> |
assertEquals(-4, aa.get(i)); |
168 |
> |
while (!aa.weakCompareAndSet(i, -4, 7)); |
169 |
> |
assertEquals(7, aa.get(i)); |
170 |
|
} |
171 |
|
} |
172 |
|
|
173 |
|
/** |
174 |
< |
* getAndSet returns previous value and sets to given value at given index |
174 |
> |
* getAndSet returns previous value and sets to given value at given index |
175 |
|
*/ |
176 |
|
public void testGetAndSet() { |
177 |
< |
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
178 |
< |
for (int i = 0; i < SIZE; ++i) { |
179 |
< |
ai.set(i, 1); |
180 |
< |
assertEquals(1,ai.getAndSet(i,0)); |
181 |
< |
assertEquals(0,ai.getAndSet(i,-10)); |
182 |
< |
assertEquals(-10,ai.getAndSet(i,1)); |
177 |
> |
AtomicIntegerArray aa = new AtomicIntegerArray(SIZE); |
178 |
> |
for (int i = 0; i < SIZE; i++) { |
179 |
> |
aa.set(i, 1); |
180 |
> |
assertEquals(1, aa.getAndSet(i, 0)); |
181 |
> |
assertEquals(0, aa.getAndSet(i, -10)); |
182 |
> |
assertEquals(-10, aa.getAndSet(i, 1)); |
183 |
|
} |
184 |
|
} |
185 |
|
|
186 |
|
/** |
187 |
< |
* getAndAdd returns previous value and adds given value |
187 |
> |
* getAndAdd returns previous value and adds given value |
188 |
|
*/ |
189 |
|
public void testGetAndAdd() { |
190 |
< |
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
191 |
< |
for (int i = 0; i < SIZE; ++i) { |
192 |
< |
ai.set(i, 1); |
193 |
< |
assertEquals(1,ai.getAndAdd(i,2)); |
194 |
< |
assertEquals(3,ai.get(i)); |
195 |
< |
assertEquals(3,ai.getAndAdd(i,-4)); |
196 |
< |
assertEquals(-1,ai.get(i)); |
190 |
> |
AtomicIntegerArray aa = new AtomicIntegerArray(SIZE); |
191 |
> |
for (int i = 0; i < SIZE; i++) { |
192 |
> |
aa.set(i, 1); |
193 |
> |
assertEquals(1, aa.getAndAdd(i, 2)); |
194 |
> |
assertEquals(3, aa.get(i)); |
195 |
> |
assertEquals(3, aa.getAndAdd(i, -4)); |
196 |
> |
assertEquals(-1, aa.get(i)); |
197 |
|
} |
198 |
|
} |
199 |
|
|
201 |
|
* getAndDecrement returns previous value and decrements |
202 |
|
*/ |
203 |
|
public void testGetAndDecrement() { |
204 |
< |
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
205 |
< |
for (int i = 0; i < SIZE; ++i) { |
206 |
< |
ai.set(i, 1); |
207 |
< |
assertEquals(1,ai.getAndDecrement(i)); |
208 |
< |
assertEquals(0,ai.getAndDecrement(i)); |
209 |
< |
assertEquals(-1,ai.getAndDecrement(i)); |
204 |
> |
AtomicIntegerArray aa = new AtomicIntegerArray(SIZE); |
205 |
> |
for (int i = 0; i < SIZE; i++) { |
206 |
> |
aa.set(i, 1); |
207 |
> |
assertEquals(1, aa.getAndDecrement(i)); |
208 |
> |
assertEquals(0, aa.getAndDecrement(i)); |
209 |
> |
assertEquals(-1, aa.getAndDecrement(i)); |
210 |
|
} |
211 |
|
} |
212 |
|
|
214 |
|
* getAndIncrement returns previous value and increments |
215 |
|
*/ |
216 |
|
public void testGetAndIncrement() { |
217 |
< |
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
218 |
< |
for (int i = 0; i < SIZE; ++i) { |
219 |
< |
ai.set(i, 1); |
220 |
< |
assertEquals(1,ai.getAndIncrement(i)); |
221 |
< |
assertEquals(2,ai.get(i)); |
222 |
< |
ai.set(i,-2); |
223 |
< |
assertEquals(-2,ai.getAndIncrement(i)); |
224 |
< |
assertEquals(-1,ai.getAndIncrement(i)); |
225 |
< |
assertEquals(0,ai.getAndIncrement(i)); |
226 |
< |
assertEquals(1,ai.get(i)); |
217 |
> |
AtomicIntegerArray aa = new AtomicIntegerArray(SIZE); |
218 |
> |
for (int i = 0; i < SIZE; i++) { |
219 |
> |
aa.set(i, 1); |
220 |
> |
assertEquals(1, aa.getAndIncrement(i)); |
221 |
> |
assertEquals(2, aa.get(i)); |
222 |
> |
aa.set(i, -2); |
223 |
> |
assertEquals(-2, aa.getAndIncrement(i)); |
224 |
> |
assertEquals(-1, aa.getAndIncrement(i)); |
225 |
> |
assertEquals(0, aa.getAndIncrement(i)); |
226 |
> |
assertEquals(1, aa.get(i)); |
227 |
|
} |
228 |
|
} |
229 |
|
|
230 |
|
/** |
231 |
< |
* addAndGet adds given value to current, and returns current value |
231 |
> |
* addAndGet adds given value to current, and returns current value |
232 |
|
*/ |
233 |
|
public void testAddAndGet() { |
234 |
< |
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
235 |
< |
for (int i = 0; i < SIZE; ++i) { |
236 |
< |
ai.set(i, 1); |
237 |
< |
assertEquals(3,ai.addAndGet(i,2)); |
238 |
< |
assertEquals(3,ai.get(i)); |
239 |
< |
assertEquals(-1,ai.addAndGet(i,-4)); |
240 |
< |
assertEquals(-1,ai.get(i)); |
234 |
> |
AtomicIntegerArray aa = new AtomicIntegerArray(SIZE); |
235 |
> |
for (int i = 0; i < SIZE; i++) { |
236 |
> |
aa.set(i, 1); |
237 |
> |
assertEquals(3, aa.addAndGet(i, 2)); |
238 |
> |
assertEquals(3, aa.get(i)); |
239 |
> |
assertEquals(-1, aa.addAndGet(i, -4)); |
240 |
> |
assertEquals(-1, aa.get(i)); |
241 |
|
} |
242 |
|
} |
243 |
|
|
245 |
|
* decrementAndGet decrements and returns current value |
246 |
|
*/ |
247 |
|
public void testDecrementAndGet() { |
248 |
< |
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
249 |
< |
for (int i = 0; i < SIZE; ++i) { |
250 |
< |
ai.set(i, 1); |
251 |
< |
assertEquals(0,ai.decrementAndGet(i)); |
252 |
< |
assertEquals(-1,ai.decrementAndGet(i)); |
253 |
< |
assertEquals(-2,ai.decrementAndGet(i)); |
254 |
< |
assertEquals(-2,ai.get(i)); |
248 |
> |
AtomicIntegerArray aa = new AtomicIntegerArray(SIZE); |
249 |
> |
for (int i = 0; i < SIZE; i++) { |
250 |
> |
aa.set(i, 1); |
251 |
> |
assertEquals(0, aa.decrementAndGet(i)); |
252 |
> |
assertEquals(-1, aa.decrementAndGet(i)); |
253 |
> |
assertEquals(-2, aa.decrementAndGet(i)); |
254 |
> |
assertEquals(-2, aa.get(i)); |
255 |
|
} |
256 |
|
} |
257 |
|
|
258 |
|
/** |
259 |
< |
* incrementAndGet increments and returns current value |
259 |
> |
* incrementAndGet increments and returns current value |
260 |
|
*/ |
261 |
|
public void testIncrementAndGet() { |
262 |
< |
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
263 |
< |
for (int i = 0; i < SIZE; ++i) { |
264 |
< |
ai.set(i, 1); |
265 |
< |
assertEquals(2,ai.incrementAndGet(i)); |
266 |
< |
assertEquals(2,ai.get(i)); |
267 |
< |
ai.set(i, -2); |
268 |
< |
assertEquals(-1,ai.incrementAndGet(i)); |
269 |
< |
assertEquals(0,ai.incrementAndGet(i)); |
270 |
< |
assertEquals(1,ai.incrementAndGet(i)); |
271 |
< |
assertEquals(1,ai.get(i)); |
262 |
> |
AtomicIntegerArray aa = new AtomicIntegerArray(SIZE); |
263 |
> |
for (int i = 0; i < SIZE; i++) { |
264 |
> |
aa.set(i, 1); |
265 |
> |
assertEquals(2, aa.incrementAndGet(i)); |
266 |
> |
assertEquals(2, aa.get(i)); |
267 |
> |
aa.set(i, -2); |
268 |
> |
assertEquals(-1, aa.incrementAndGet(i)); |
269 |
> |
assertEquals(0, aa.incrementAndGet(i)); |
270 |
> |
assertEquals(1, aa.incrementAndGet(i)); |
271 |
> |
assertEquals(1, aa.get(i)); |
272 |
|
} |
273 |
|
} |
274 |
|
|
275 |
|
static final int COUNTDOWN = 100000; |
276 |
|
|
277 |
< |
class Counter implements Runnable { |
278 |
< |
final AtomicIntegerArray ai; |
277 |
> |
class Counter extends CheckedRunnable { |
278 |
> |
final AtomicIntegerArray aa; |
279 |
|
volatile int counts; |
280 |
< |
Counter(AtomicIntegerArray a) { ai = a; } |
281 |
< |
public void run() { |
280 |
> |
Counter(AtomicIntegerArray a) { aa = a; } |
281 |
> |
public void realRun() { |
282 |
|
for (;;) { |
283 |
|
boolean done = true; |
284 |
< |
for (int i = 0; i < ai.length(); ++i) { |
285 |
< |
int v = ai.get(i); |
286 |
< |
threadAssertTrue(v >= 0); |
284 |
> |
for (int i = 0; i < aa.length(); i++) { |
285 |
> |
int v = aa.get(i); |
286 |
> |
assertTrue(v >= 0); |
287 |
|
if (v != 0) { |
288 |
|
done = false; |
289 |
< |
if (ai.compareAndSet(i, v, v-1)) |
289 |
> |
if (aa.compareAndSet(i, v, v-1)) |
290 |
|
++counts; |
291 |
|
} |
292 |
|
} |
300 |
|
* Multiple threads using same array of counters successfully |
301 |
|
* update a number of times equal to total count |
302 |
|
*/ |
303 |
< |
public void testCountingInMultipleThreads() { |
304 |
< |
try { |
305 |
< |
final AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
306 |
< |
for (int i = 0; i < SIZE; ++i) |
307 |
< |
ai.set(i, COUNTDOWN); |
308 |
< |
Counter c1 = new Counter(ai); |
309 |
< |
Counter c2 = new Counter(ai); |
310 |
< |
Thread t1 = new Thread(c1); |
311 |
< |
Thread t2 = new Thread(c2); |
312 |
< |
t1.start(); |
313 |
< |
t2.start(); |
314 |
< |
t1.join(); |
315 |
< |
t2.join(); |
309 |
< |
assertEquals(c1.counts+c2.counts, SIZE * COUNTDOWN); |
310 |
< |
} |
311 |
< |
catch(InterruptedException ie) { |
312 |
< |
unexpectedException(); |
313 |
< |
} |
303 |
> |
public void testCountingInMultipleThreads() throws InterruptedException { |
304 |
> |
final AtomicIntegerArray aa = new AtomicIntegerArray(SIZE); |
305 |
> |
for (int i = 0; i < SIZE; i++) |
306 |
> |
aa.set(i, COUNTDOWN); |
307 |
> |
Counter c1 = new Counter(aa); |
308 |
> |
Counter c2 = new Counter(aa); |
309 |
> |
Thread t1 = new Thread(c1); |
310 |
> |
Thread t2 = new Thread(c2); |
311 |
> |
t1.start(); |
312 |
> |
t2.start(); |
313 |
> |
t1.join(); |
314 |
> |
t2.join(); |
315 |
> |
assertEquals(c1.counts+c2.counts, SIZE * COUNTDOWN); |
316 |
|
} |
317 |
|
|
316 |
– |
|
318 |
|
/** |
319 |
|
* a deserialized serialized array holds same values |
320 |
|
*/ |
321 |
< |
public void testSerialization() { |
322 |
< |
AtomicIntegerArray l = new AtomicIntegerArray(SIZE); |
323 |
< |
for (int i = 0; i < SIZE; ++i) |
324 |
< |
l.set(i, -i); |
325 |
< |
|
326 |
< |
try { |
327 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
328 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
329 |
< |
out.writeObject(l); |
329 |
< |
out.close(); |
330 |
< |
|
331 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
332 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
333 |
< |
AtomicIntegerArray r = (AtomicIntegerArray) in.readObject(); |
334 |
< |
for (int i = 0; i < SIZE; ++i) { |
335 |
< |
assertEquals(l.get(i), r.get(i)); |
336 |
< |
} |
337 |
< |
} catch(Exception e){ |
338 |
< |
e.printStackTrace(); |
339 |
< |
unexpectedException(); |
321 |
> |
public void testSerialization() throws Exception { |
322 |
> |
AtomicIntegerArray x = new AtomicIntegerArray(SIZE); |
323 |
> |
for (int i = 0; i < SIZE; i++) |
324 |
> |
x.set(i, -i); |
325 |
> |
AtomicIntegerArray y = serialClone(x); |
326 |
> |
assertNotSame(x, y); |
327 |
> |
assertEquals(x.length(), y.length()); |
328 |
> |
for (int i = 0; i < SIZE; i++) { |
329 |
> |
assertEquals(x.get(i), y.get(i)); |
330 |
|
} |
331 |
|
} |
332 |
|
|
343 |
– |
|
333 |
|
/** |
334 |
|
* toString returns current value. |
335 |
|
*/ |
336 |
|
public void testToString() { |
337 |
< |
int[] a = { 17, 3, -42, 99, -7}; |
338 |
< |
AtomicIntegerArray ai = new AtomicIntegerArray(a); |
339 |
< |
assertEquals(Arrays.toString(a), ai.toString()); |
337 |
> |
int[] a = { 17, 3, -42, 99, -7 }; |
338 |
> |
AtomicIntegerArray aa = new AtomicIntegerArray(a); |
339 |
> |
assertEquals(Arrays.toString(a), aa.toString()); |
340 |
|
} |
341 |
|
|
342 |
|
} |