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. |
5 |
> |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
> |
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
|
import junit.framework.*; |
44 |
|
assertEquals(2,ai.get()); |
45 |
|
ai.set(-3); |
46 |
|
assertEquals(-3,ai.get()); |
47 |
< |
|
47 |
> |
|
48 |
> |
} |
49 |
> |
|
50 |
> |
/** |
51 |
> |
* get returns the last value lazySet in same thread |
52 |
> |
*/ |
53 |
> |
public void testGetLazySet(){ |
54 |
> |
AtomicInteger ai = new AtomicInteger(1); |
55 |
> |
assertEquals(1,ai.get()); |
56 |
> |
ai.lazySet(2); |
57 |
> |
assertEquals(2,ai.get()); |
58 |
> |
ai.lazySet(-3); |
59 |
> |
assertEquals(-3,ai.get()); |
60 |
> |
|
61 |
|
} |
62 |
|
/** |
63 |
|
* compareAndSet succeeds in changing value if equal to expected else fails |
81 |
|
final AtomicInteger ai = new AtomicInteger(1); |
82 |
|
Thread t = new Thread(new Runnable() { |
83 |
|
public void run() { |
84 |
< |
while(!ai.compareAndSet(2, 3)) Thread.yield(); |
84 |
> |
while (!ai.compareAndSet(2, 3)) Thread.yield(); |
85 |
|
}}); |
86 |
|
try { |
87 |
|
t.start(); |
90 |
|
assertFalse(t.isAlive()); |
91 |
|
assertEquals(ai.get(), 3); |
92 |
|
} |
93 |
< |
catch(Exception e) { |
93 |
> |
catch (Exception e) { |
94 |
|
unexpectedException(); |
95 |
|
} |
96 |
|
} |
97 |
|
|
98 |
|
/** |
99 |
|
* repeated weakCompareAndSet succeeds in changing value when equal |
100 |
< |
* to expected |
100 |
> |
* to expected |
101 |
|
*/ |
102 |
|
public void testWeakCompareAndSet(){ |
103 |
|
AtomicInteger ai = new AtomicInteger(1); |
104 |
< |
while(!ai.weakCompareAndSet(1,2)); |
105 |
< |
while(!ai.weakCompareAndSet(2,-4)); |
104 |
> |
while (!ai.weakCompareAndSet(1,2)); |
105 |
> |
while (!ai.weakCompareAndSet(2,-4)); |
106 |
|
assertEquals(-4,ai.get()); |
107 |
< |
while(!ai.weakCompareAndSet(-4,7)); |
107 |
> |
while (!ai.weakCompareAndSet(-4,7)); |
108 |
|
assertEquals(7,ai.get()); |
109 |
|
} |
110 |
|
|
206 |
|
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
207 |
|
AtomicInteger r = (AtomicInteger) in.readObject(); |
208 |
|
assertEquals(l.get(), r.get()); |
209 |
< |
} catch(Exception e){ |
209 |
> |
} catch (Exception e){ |
210 |
|
unexpectedException(); |
211 |
|
} |
212 |
|
} |
213 |
|
|
214 |
+ |
/** |
215 |
+ |
* toString returns current value. |
216 |
+ |
*/ |
217 |
+ |
public void testToString() { |
218 |
+ |
AtomicInteger ai = new AtomicInteger(); |
219 |
+ |
for (int i = -12; i < 6; ++i) { |
220 |
+ |
ai.set(i); |
221 |
+ |
assertEquals(ai.toString(), Integer.toString(i)); |
222 |
+ |
} |
223 |
+ |
} |
224 |
+ |
|
225 |
+ |
/** |
226 |
+ |
* intValue returns current value. |
227 |
+ |
*/ |
228 |
+ |
public void testIntValue() { |
229 |
+ |
AtomicInteger ai = new AtomicInteger(); |
230 |
+ |
for (int i = -12; i < 6; ++i) { |
231 |
+ |
ai.set(i); |
232 |
+ |
assertEquals(i, ai.intValue()); |
233 |
+ |
} |
234 |
+ |
} |
235 |
+ |
|
236 |
+ |
|
237 |
+ |
/** |
238 |
+ |
* longValue returns current value. |
239 |
+ |
*/ |
240 |
+ |
public void testLongValue() { |
241 |
+ |
AtomicInteger ai = new AtomicInteger(); |
242 |
+ |
for (int i = -12; i < 6; ++i) { |
243 |
+ |
ai.set(i); |
244 |
+ |
assertEquals((long)i, ai.longValue()); |
245 |
+ |
} |
246 |
+ |
} |
247 |
+ |
|
248 |
+ |
/** |
249 |
+ |
* floatValue returns current value. |
250 |
+ |
*/ |
251 |
+ |
public void testFloatValue() { |
252 |
+ |
AtomicInteger ai = new AtomicInteger(); |
253 |
+ |
for (int i = -12; i < 6; ++i) { |
254 |
+ |
ai.set(i); |
255 |
+ |
assertEquals((float)i, ai.floatValue()); |
256 |
+ |
} |
257 |
+ |
} |
258 |
+ |
|
259 |
+ |
/** |
260 |
+ |
* doubleValue returns current value. |
261 |
+ |
*/ |
262 |
+ |
public void testDoubleValue() { |
263 |
+ |
AtomicInteger ai = new AtomicInteger(); |
264 |
+ |
for (int i = -12; i < 6; ++i) { |
265 |
+ |
ai.set(i); |
266 |
+ |
assertEquals((double)i, ai.doubleValue()); |
267 |
+ |
} |
268 |
+ |
} |
269 |
+ |
|
270 |
+ |
|
271 |
|
|
272 |
|
} |