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. |
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. |
7 |
|
*/ |
8 |
|
|
9 |
|
import junit.framework.*; |
18 |
|
return new TestSuite(AtomicLongArrayTest.class); |
19 |
|
} |
20 |
|
|
21 |
+ |
/** |
22 |
+ |
* constructor creates array of given size with all elements zero |
23 |
+ |
*/ |
24 |
|
public void testConstructor(){ |
25 |
|
AtomicLongArray ai = new AtomicLongArray(SIZE); |
26 |
|
for (int i = 0; i < SIZE; ++i) |
27 |
|
assertEquals(0,ai.get(i)); |
28 |
|
} |
29 |
|
|
30 |
+ |
/** |
31 |
+ |
* get and set for out of bound indices throw IndexOutOfBoundsException |
32 |
+ |
*/ |
33 |
+ |
public void testIndexing(){ |
34 |
+ |
AtomicLongArray ai = new AtomicLongArray(SIZE); |
35 |
+ |
try { |
36 |
+ |
ai.get(SIZE); |
37 |
+ |
} catch(IndexOutOfBoundsException success){ |
38 |
+ |
} |
39 |
+ |
try { |
40 |
+ |
ai.get(-1); |
41 |
+ |
} catch(IndexOutOfBoundsException success){ |
42 |
+ |
} |
43 |
+ |
try { |
44 |
+ |
ai.set(SIZE, 0); |
45 |
+ |
} catch(IndexOutOfBoundsException success){ |
46 |
+ |
} |
47 |
+ |
try { |
48 |
+ |
ai.set(-1, 0); |
49 |
+ |
} catch(IndexOutOfBoundsException success){ |
50 |
+ |
} |
51 |
+ |
} |
52 |
+ |
|
53 |
+ |
/** |
54 |
+ |
* get returns the last value set at index |
55 |
+ |
*/ |
56 |
|
public void testGetSet(){ |
57 |
|
AtomicLongArray ai = new AtomicLongArray(SIZE); |
58 |
|
for (int i = 0; i < SIZE; ++i) { |
65 |
|
} |
66 |
|
} |
67 |
|
|
68 |
+ |
/** |
69 |
+ |
* compareAndSet succeeds in changing value if equal to expected else fails |
70 |
+ |
*/ |
71 |
|
public void testCompareAndSet(){ |
72 |
|
AtomicLongArray ai = new AtomicLongArray(SIZE); |
73 |
|
for (int i = 0; i < SIZE; ++i) { |
82 |
|
} |
83 |
|
} |
84 |
|
|
85 |
+ |
/** |
86 |
+ |
* compareAndSet in one thread enables another waiting for value |
87 |
+ |
* to succeed |
88 |
+ |
*/ |
89 |
+ |
public void testCompareAndSetInMultipleThreads() { |
90 |
+ |
final AtomicLongArray a = new AtomicLongArray(1); |
91 |
+ |
a.set(0, 1); |
92 |
+ |
Thread t = new Thread(new Runnable() { |
93 |
+ |
public void run() { |
94 |
+ |
while(!a.compareAndSet(0, 2, 3)) Thread.yield(); |
95 |
+ |
}}); |
96 |
+ |
try { |
97 |
+ |
t.start(); |
98 |
+ |
assertTrue(a.compareAndSet(0, 1, 2)); |
99 |
+ |
t.join(LONG_DELAY_MS); |
100 |
+ |
assertFalse(t.isAlive()); |
101 |
+ |
assertEquals(a.get(0), 3); |
102 |
+ |
} |
103 |
+ |
catch(Exception e) { |
104 |
+ |
unexpectedException(); |
105 |
+ |
} |
106 |
+ |
} |
107 |
+ |
|
108 |
+ |
/** |
109 |
+ |
* repeated weakCompareAndSet succeeds in changing value when equal |
110 |
+ |
* to expected |
111 |
+ |
*/ |
112 |
|
public void testWeakCompareAndSet(){ |
113 |
|
AtomicLongArray ai = new AtomicLongArray(SIZE); |
114 |
|
for (int i = 0; i < SIZE; ++i) { |
121 |
|
} |
122 |
|
} |
123 |
|
|
124 |
+ |
/** |
125 |
+ |
* getAndSet returns previous value and sets to given value at given index |
126 |
+ |
*/ |
127 |
|
public void testGetAndSet(){ |
128 |
|
AtomicLongArray ai = new AtomicLongArray(SIZE); |
129 |
|
for (int i = 0; i < SIZE; ++i) { |
134 |
|
} |
135 |
|
} |
136 |
|
|
137 |
+ |
/** |
138 |
+ |
* getAndAdd returns previous value and adds given value |
139 |
+ |
*/ |
140 |
|
public void testGetAndAdd(){ |
141 |
|
AtomicLongArray ai = new AtomicLongArray(SIZE); |
142 |
|
for (int i = 0; i < SIZE; ++i) { |
148 |
|
} |
149 |
|
} |
150 |
|
|
151 |
+ |
/** |
152 |
+ |
* getAndDecrement returns previous value and decrements |
153 |
+ |
*/ |
154 |
|
public void testGetAndDecrement(){ |
155 |
|
AtomicLongArray ai = new AtomicLongArray(SIZE); |
156 |
|
for (int i = 0; i < SIZE; ++i) { |
161 |
|
} |
162 |
|
} |
163 |
|
|
164 |
+ |
/** |
165 |
+ |
* getAndIncrement returns previous value and increments |
166 |
+ |
*/ |
167 |
|
public void testGetAndIncrement(){ |
168 |
|
AtomicLongArray ai = new AtomicLongArray(SIZE); |
169 |
|
for (int i = 0; i < SIZE; ++i) { |
178 |
|
} |
179 |
|
} |
180 |
|
|
181 |
+ |
/** |
182 |
+ |
* addAndGet adds given value to current, and returns current value |
183 |
+ |
*/ |
184 |
|
public void testAddAndGet() { |
185 |
|
AtomicLongArray ai = new AtomicLongArray(SIZE); |
186 |
|
for (int i = 0; i < SIZE; ++i) { |
192 |
|
} |
193 |
|
} |
194 |
|
|
195 |
+ |
/** |
196 |
+ |
* decrementAndGet decrements and returns current value |
197 |
+ |
*/ |
198 |
|
public void testDecrementAndGet(){ |
199 |
|
AtomicLongArray ai = new AtomicLongArray(SIZE); |
200 |
|
for (int i = 0; i < SIZE; ++i) { |
206 |
|
} |
207 |
|
} |
208 |
|
|
209 |
+ |
/** |
210 |
+ |
* incrementAndGet increments and returns current value |
211 |
+ |
*/ |
212 |
|
public void testIncrementAndGet() { |
213 |
|
AtomicLongArray ai = new AtomicLongArray(SIZE); |
214 |
|
for (int i = 0; i < SIZE; ++i) { |
247 |
|
} |
248 |
|
} |
249 |
|
|
250 |
+ |
/** |
251 |
+ |
* Multiple threads using same array of counters successfully |
252 |
+ |
* update a number of times equal to total count |
253 |
+ |
*/ |
254 |
|
public void testCountingInMultipleThreads() { |
255 |
|
try { |
256 |
|
final AtomicLongArray ai = new AtomicLongArray(SIZE); |
267 |
|
assertEquals(c1.counts+c2.counts, SIZE * COUNTDOWN); |
268 |
|
} |
269 |
|
catch(InterruptedException ie) { |
270 |
< |
fail("unexpected exception"); |
270 |
> |
unexpectedException(); |
271 |
|
} |
272 |
|
} |
273 |
|
|
274 |
+ |
/** |
275 |
+ |
* a deserialized serialized array holds same values |
276 |
+ |
*/ |
277 |
|
public void testSerialization() { |
278 |
|
AtomicLongArray l = new AtomicLongArray(SIZE); |
279 |
|
for (int i = 0; i < SIZE; ++i) |
292 |
|
assertEquals(l.get(i), r.get(i)); |
293 |
|
} |
294 |
|
} catch(Exception e){ |
295 |
< |
e.printStackTrace(); |
208 |
< |
fail("unexpected exception"); |
295 |
> |
unexpectedException(); |
296 |
|
} |
297 |
|
} |
298 |
|
|