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.*; |
19 |
|
} |
20 |
|
|
21 |
|
/** |
22 |
< |
* |
22 |
> |
* constructor creates array of given size with all elements zero |
23 |
|
*/ |
24 |
|
public void testConstructor(){ |
25 |
|
AtomicLongArray ai = new AtomicLongArray(SIZE); |
28 |
|
} |
29 |
|
|
30 |
|
/** |
31 |
< |
* |
31 |
> |
* constructor with null array throws NPE |
32 |
> |
*/ |
33 |
> |
public void testConstructor2NPE() { |
34 |
> |
try { |
35 |
> |
long[] a = null; |
36 |
> |
AtomicLongArray ai = new AtomicLongArray(a); |
37 |
> |
} catch (NullPointerException success) { |
38 |
> |
} catch (Exception ex) { |
39 |
> |
unexpectedException(); |
40 |
> |
} |
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)); |
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){ |
74 |
> |
} |
75 |
> |
} |
76 |
> |
|
77 |
> |
/** |
78 |
> |
* get returns the last value set at index |
79 |
|
*/ |
80 |
|
public void testGetSet(){ |
81 |
|
AtomicLongArray ai = new AtomicLongArray(SIZE); |
90 |
|
} |
91 |
|
|
92 |
|
/** |
93 |
< |
* |
93 |
> |
* compareAndSet succeeds in changing value if equal to expected else fails |
94 |
|
*/ |
95 |
|
public void testCompareAndSet(){ |
96 |
|
AtomicLongArray ai = new AtomicLongArray(SIZE); |
107 |
|
} |
108 |
|
|
109 |
|
/** |
110 |
< |
* |
110 |
> |
* compareAndSet in one thread enables another waiting for value |
111 |
> |
* to succeed |
112 |
> |
*/ |
113 |
> |
public void testCompareAndSetInMultipleThreads() { |
114 |
> |
final AtomicLongArray a = new AtomicLongArray(1); |
115 |
> |
a.set(0, 1); |
116 |
> |
Thread t = new Thread(new Runnable() { |
117 |
> |
public void run() { |
118 |
> |
while(!a.compareAndSet(0, 2, 3)) Thread.yield(); |
119 |
> |
}}); |
120 |
> |
try { |
121 |
> |
t.start(); |
122 |
> |
assertTrue(a.compareAndSet(0, 1, 2)); |
123 |
> |
t.join(LONG_DELAY_MS); |
124 |
> |
assertFalse(t.isAlive()); |
125 |
> |
assertEquals(a.get(0), 3); |
126 |
> |
} |
127 |
> |
catch(Exception e) { |
128 |
> |
unexpectedException(); |
129 |
> |
} |
130 |
> |
} |
131 |
> |
|
132 |
> |
/** |
133 |
> |
* repeated weakCompareAndSet succeeds in changing value when equal |
134 |
> |
* to expected |
135 |
|
*/ |
136 |
|
public void testWeakCompareAndSet(){ |
137 |
|
AtomicLongArray ai = new AtomicLongArray(SIZE); |
146 |
|
} |
147 |
|
|
148 |
|
/** |
149 |
< |
* |
149 |
> |
* getAndSet returns previous value and sets to given value at given index |
150 |
|
*/ |
151 |
|
public void testGetAndSet(){ |
152 |
|
AtomicLongArray ai = new AtomicLongArray(SIZE); |
159 |
|
} |
160 |
|
|
161 |
|
/** |
162 |
< |
* |
162 |
> |
* getAndAdd returns previous value and adds given value |
163 |
|
*/ |
164 |
|
public void testGetAndAdd(){ |
165 |
|
AtomicLongArray ai = new AtomicLongArray(SIZE); |
173 |
|
} |
174 |
|
|
175 |
|
/** |
176 |
< |
* |
176 |
> |
* getAndDecrement returns previous value and decrements |
177 |
|
*/ |
178 |
|
public void testGetAndDecrement(){ |
179 |
|
AtomicLongArray ai = new AtomicLongArray(SIZE); |
186 |
|
} |
187 |
|
|
188 |
|
/** |
189 |
< |
* |
189 |
> |
* getAndIncrement returns previous value and increments |
190 |
|
*/ |
191 |
|
public void testGetAndIncrement(){ |
192 |
|
AtomicLongArray ai = new AtomicLongArray(SIZE); |
203 |
|
} |
204 |
|
|
205 |
|
/** |
206 |
< |
* |
206 |
> |
* addAndGet adds given value to current, and returns current value |
207 |
|
*/ |
208 |
|
public void testAddAndGet() { |
209 |
|
AtomicLongArray ai = new AtomicLongArray(SIZE); |
217 |
|
} |
218 |
|
|
219 |
|
/** |
220 |
< |
* |
220 |
> |
* decrementAndGet decrements and returns current value |
221 |
|
*/ |
222 |
|
public void testDecrementAndGet(){ |
223 |
|
AtomicLongArray ai = new AtomicLongArray(SIZE); |
231 |
|
} |
232 |
|
|
233 |
|
/** |
234 |
< |
* |
234 |
> |
* incrementAndGet increments and returns current value |
235 |
|
*/ |
236 |
|
public void testIncrementAndGet() { |
237 |
|
AtomicLongArray ai = new AtomicLongArray(SIZE); |
272 |
|
} |
273 |
|
|
274 |
|
/** |
275 |
< |
* |
275 |
> |
* Multiple threads using same array of counters successfully |
276 |
> |
* update a number of times equal to total count |
277 |
|
*/ |
278 |
|
public void testCountingInMultipleThreads() { |
279 |
|
try { |
296 |
|
} |
297 |
|
|
298 |
|
/** |
299 |
< |
* |
299 |
> |
* a deserialized serialized array holds same values |
300 |
|
*/ |
301 |
|
public void testSerialization() { |
302 |
|
AtomicLongArray l = new AtomicLongArray(SIZE); |