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