22 |
|
/** |
23 |
|
* constructor creates array of given size with all elements zero |
24 |
|
*/ |
25 |
< |
public void testConstructor(){ |
25 |
> |
public void testConstructor() { |
26 |
|
AtomicLongArray ai = new AtomicLongArray(SIZE); |
27 |
|
for (int i = 0; i < SIZE; ++i) |
28 |
|
assertEquals(0,ai.get(i)); |
35 |
|
try { |
36 |
|
long[] a = null; |
37 |
|
AtomicLongArray ai = new AtomicLongArray(a); |
38 |
< |
} catch (NullPointerException success) { |
39 |
< |
} catch (Exception ex) { |
40 |
< |
unexpectedException(); |
41 |
< |
} |
38 |
> |
shouldThrow(); |
39 |
> |
} catch (NullPointerException success) {} |
40 |
|
} |
41 |
|
|
42 |
|
/** |
53 |
|
/** |
54 |
|
* get and set for out of bound indices throw IndexOutOfBoundsException |
55 |
|
*/ |
56 |
< |
public void testIndexing(){ |
56 |
> |
public void testIndexing() { |
57 |
|
AtomicLongArray ai = new AtomicLongArray(SIZE); |
58 |
|
try { |
59 |
|
ai.get(SIZE); |
60 |
< |
} catch (IndexOutOfBoundsException success){ |
60 |
> |
shouldThrow(); |
61 |
> |
} catch (IndexOutOfBoundsException success) { |
62 |
|
} |
63 |
|
try { |
64 |
|
ai.get(-1); |
65 |
< |
} catch (IndexOutOfBoundsException success){ |
65 |
> |
shouldThrow(); |
66 |
> |
} catch (IndexOutOfBoundsException success) { |
67 |
|
} |
68 |
|
try { |
69 |
|
ai.set(SIZE, 0); |
70 |
< |
} catch (IndexOutOfBoundsException success){ |
70 |
> |
shouldThrow(); |
71 |
> |
} catch (IndexOutOfBoundsException success) { |
72 |
|
} |
73 |
|
try { |
74 |
|
ai.set(-1, 0); |
75 |
< |
} catch (IndexOutOfBoundsException success){ |
75 |
> |
shouldThrow(); |
76 |
> |
} catch (IndexOutOfBoundsException success) { |
77 |
|
} |
78 |
|
} |
79 |
|
|
80 |
|
/** |
81 |
|
* get returns the last value set at index |
82 |
|
*/ |
83 |
< |
public void testGetSet(){ |
83 |
> |
public void testGetSet() { |
84 |
|
AtomicLongArray ai = new AtomicLongArray(SIZE); |
85 |
|
for (int i = 0; i < SIZE; ++i) { |
86 |
|
ai.set(i, 1); |
95 |
|
/** |
96 |
|
* get returns the last value lazySet at index by same thread |
97 |
|
*/ |
98 |
< |
public void testGetLazySet(){ |
98 |
> |
public void testGetLazySet() { |
99 |
|
AtomicLongArray ai = new AtomicLongArray(SIZE); |
100 |
|
for (int i = 0; i < SIZE; ++i) { |
101 |
|
ai.lazySet(i, 1); |
110 |
|
/** |
111 |
|
* compareAndSet succeeds in changing value if equal to expected else fails |
112 |
|
*/ |
113 |
< |
public void testCompareAndSet(){ |
113 |
> |
public void testCompareAndSet() { |
114 |
|
AtomicLongArray ai = new AtomicLongArray(SIZE); |
115 |
|
for (int i = 0; i < SIZE; ++i) { |
116 |
|
ai.set(i, 1); |
128 |
|
* compareAndSet in one thread enables another waiting for value |
129 |
|
* to succeed |
130 |
|
*/ |
131 |
< |
public void testCompareAndSetInMultipleThreads() { |
131 |
> |
public void testCompareAndSetInMultipleThreads() throws InterruptedException { |
132 |
|
final AtomicLongArray a = new AtomicLongArray(1); |
133 |
|
a.set(0, 1); |
134 |
|
Thread t = new Thread(new Runnable() { |
135 |
|
public void run() { |
136 |
|
while (!a.compareAndSet(0, 2, 3)) Thread.yield(); |
137 |
|
}}); |
138 |
< |
try { |
139 |
< |
t.start(); |
140 |
< |
assertTrue(a.compareAndSet(0, 1, 2)); |
141 |
< |
t.join(LONG_DELAY_MS); |
142 |
< |
assertFalse(t.isAlive()); |
143 |
< |
assertEquals(a.get(0), 3); |
142 |
< |
} |
143 |
< |
catch (Exception e) { |
144 |
< |
unexpectedException(); |
145 |
< |
} |
138 |
> |
|
139 |
> |
t.start(); |
140 |
> |
assertTrue(a.compareAndSet(0, 1, 2)); |
141 |
> |
t.join(LONG_DELAY_MS); |
142 |
> |
assertFalse(t.isAlive()); |
143 |
> |
assertEquals(a.get(0), 3); |
144 |
|
} |
145 |
|
|
146 |
|
/** |
147 |
|
* repeated weakCompareAndSet succeeds in changing value when equal |
148 |
|
* to expected |
149 |
|
*/ |
150 |
< |
public void testWeakCompareAndSet(){ |
150 |
> |
public void testWeakCompareAndSet() { |
151 |
|
AtomicLongArray ai = new AtomicLongArray(SIZE); |
152 |
|
for (int i = 0; i < SIZE; ++i) { |
153 |
|
ai.set(i, 1); |
162 |
|
/** |
163 |
|
* getAndSet returns previous value and sets to given value at given index |
164 |
|
*/ |
165 |
< |
public void testGetAndSet(){ |
165 |
> |
public void testGetAndSet() { |
166 |
|
AtomicLongArray ai = new AtomicLongArray(SIZE); |
167 |
|
for (int i = 0; i < SIZE; ++i) { |
168 |
|
ai.set(i, 1); |
175 |
|
/** |
176 |
|
* getAndAdd returns previous value and adds given value |
177 |
|
*/ |
178 |
< |
public void testGetAndAdd(){ |
178 |
> |
public void testGetAndAdd() { |
179 |
|
AtomicLongArray ai = new AtomicLongArray(SIZE); |
180 |
|
for (int i = 0; i < SIZE; ++i) { |
181 |
|
ai.set(i, 1); |
189 |
|
/** |
190 |
|
* getAndDecrement returns previous value and decrements |
191 |
|
*/ |
192 |
< |
public void testGetAndDecrement(){ |
192 |
> |
public void testGetAndDecrement() { |
193 |
|
AtomicLongArray ai = new AtomicLongArray(SIZE); |
194 |
|
for (int i = 0; i < SIZE; ++i) { |
195 |
|
ai.set(i, 1); |
202 |
|
/** |
203 |
|
* getAndIncrement returns previous value and increments |
204 |
|
*/ |
205 |
< |
public void testGetAndIncrement(){ |
205 |
> |
public void testGetAndIncrement() { |
206 |
|
AtomicLongArray ai = new AtomicLongArray(SIZE); |
207 |
|
for (int i = 0; i < SIZE; ++i) { |
208 |
|
ai.set(i, 1); |
233 |
|
/** |
234 |
|
* decrementAndGet decrements and returns current value |
235 |
|
*/ |
236 |
< |
public void testDecrementAndGet(){ |
236 |
> |
public void testDecrementAndGet() { |
237 |
|
AtomicLongArray ai = new AtomicLongArray(SIZE); |
238 |
|
for (int i = 0; i < SIZE; ++i) { |
239 |
|
ai.set(i, 1); |
289 |
|
* Multiple threads using same array of counters successfully |
290 |
|
* update a number of times equal to total count |
291 |
|
*/ |
292 |
< |
public void testCountingInMultipleThreads() { |
293 |
< |
try { |
294 |
< |
final AtomicLongArray ai = new AtomicLongArray(SIZE); |
295 |
< |
for (int i = 0; i < SIZE; ++i) |
296 |
< |
ai.set(i, COUNTDOWN); |
297 |
< |
Counter c1 = new Counter(ai); |
298 |
< |
Counter c2 = new Counter(ai); |
299 |
< |
Thread t1 = new Thread(c1); |
300 |
< |
Thread t2 = new Thread(c2); |
301 |
< |
t1.start(); |
302 |
< |
t2.start(); |
303 |
< |
t1.join(); |
304 |
< |
t2.join(); |
307 |
< |
assertEquals(c1.counts+c2.counts, SIZE * COUNTDOWN); |
308 |
< |
} |
309 |
< |
catch (InterruptedException ie) { |
310 |
< |
unexpectedException(); |
311 |
< |
} |
292 |
> |
public void testCountingInMultipleThreads() throws InterruptedException { |
293 |
> |
final AtomicLongArray ai = new AtomicLongArray(SIZE); |
294 |
> |
for (int i = 0; i < SIZE; ++i) |
295 |
> |
ai.set(i, COUNTDOWN); |
296 |
> |
Counter c1 = new Counter(ai); |
297 |
> |
Counter c2 = new Counter(ai); |
298 |
> |
Thread t1 = new Thread(c1); |
299 |
> |
Thread t2 = new Thread(c2); |
300 |
> |
t1.start(); |
301 |
> |
t2.start(); |
302 |
> |
t1.join(); |
303 |
> |
t2.join(); |
304 |
> |
assertEquals(c1.counts+c2.counts, SIZE * COUNTDOWN); |
305 |
|
} |
306 |
|
|
307 |
|
/** |
308 |
|
* a deserialized serialized array holds same values |
309 |
|
*/ |
310 |
< |
public void testSerialization() { |
310 |
> |
public void testSerialization() throws Exception { |
311 |
|
AtomicLongArray l = new AtomicLongArray(SIZE); |
312 |
|
for (int i = 0; i < SIZE; ++i) |
313 |
|
l.set(i, -i); |
314 |
|
|
315 |
< |
try { |
316 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
317 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
318 |
< |
out.writeObject(l); |
319 |
< |
out.close(); |
320 |
< |
|
321 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
322 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
323 |
< |
AtomicLongArray r = (AtomicLongArray) in.readObject(); |
324 |
< |
for (int i = 0; i < SIZE; ++i) { |
332 |
< |
assertEquals(l.get(i), r.get(i)); |
333 |
< |
} |
334 |
< |
} catch (Exception e){ |
335 |
< |
unexpectedException(); |
315 |
> |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
316 |
> |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
317 |
> |
out.writeObject(l); |
318 |
> |
out.close(); |
319 |
> |
|
320 |
> |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
321 |
> |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
322 |
> |
AtomicLongArray r = (AtomicLongArray) in.readObject(); |
323 |
> |
for (int i = 0; i < SIZE; ++i) { |
324 |
> |
assertEquals(l.get(i), r.get(i)); |
325 |
|
} |
326 |
|
} |
327 |
|
|