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 |
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.*; |
10 |
> |
import java.util.concurrent.atomic.AtomicLong; |
11 |
|
|
12 |
|
public class AtomicLongTest extends JSR166TestCase { |
13 |
< |
public static void main (String[] args) { |
14 |
< |
junit.textui.TestRunner.run (suite()); |
13 |
> |
public static void main(String[] args) { |
14 |
> |
junit.textui.TestRunner.run(suite()); |
15 |
|
} |
16 |
|
public static Test suite() { |
17 |
|
return new TestSuite(AtomicLongTest.class); |
18 |
|
} |
19 |
|
|
20 |
+ |
final long[] VALUES = { |
21 |
+ |
Long.MIN_VALUE, |
22 |
+ |
Integer.MIN_VALUE, -1, 0, 1, 42, Integer.MAX_VALUE, |
23 |
+ |
Long.MAX_VALUE, |
24 |
+ |
}; |
25 |
+ |
|
26 |
|
/** |
27 |
|
* constructor initializes to given value |
28 |
|
*/ |
49 |
|
assertEquals(2,ai.get()); |
50 |
|
ai.set(-3); |
51 |
|
assertEquals(-3,ai.get()); |
47 |
– |
|
52 |
|
} |
53 |
|
|
54 |
|
/** |
61 |
|
assertEquals(2,ai.get()); |
62 |
|
ai.lazySet(-3); |
63 |
|
assertEquals(-3,ai.get()); |
60 |
– |
|
64 |
|
} |
65 |
|
|
66 |
|
/** |
72 |
|
assertTrue(ai.compareAndSet(2,-4)); |
73 |
|
assertEquals(-4,ai.get()); |
74 |
|
assertFalse(ai.compareAndSet(-5,7)); |
75 |
< |
assertFalse((7 == ai.get())); |
75 |
> |
assertEquals(-4,ai.get()); |
76 |
|
assertTrue(ai.compareAndSet(-4,7)); |
77 |
|
assertEquals(7,ai.get()); |
78 |
|
} |
194 |
|
* a deserialized serialized atomic holds same value |
195 |
|
*/ |
196 |
|
public void testSerialization() throws Exception { |
197 |
< |
AtomicLong l = new AtomicLong(); |
198 |
< |
|
199 |
< |
l.set(-22); |
200 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
201 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
202 |
< |
out.writeObject(l); |
203 |
< |
out.close(); |
204 |
< |
|
202 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
203 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
204 |
< |
AtomicLong r = (AtomicLong) in.readObject(); |
205 |
< |
assertEquals(l.get(), r.get()); |
197 |
> |
AtomicLong x = new AtomicLong(); |
198 |
> |
AtomicLong y = serialClone(x); |
199 |
> |
assertTrue(x != y); |
200 |
> |
x.set(-22); |
201 |
> |
AtomicLong z = serialClone(x); |
202 |
> |
assertEquals(-22, x.get()); |
203 |
> |
assertEquals(0, y.get()); |
204 |
> |
assertEquals(-22, z.get()); |
205 |
|
} |
206 |
|
|
207 |
|
/** |
209 |
|
*/ |
210 |
|
public void testToString() { |
211 |
|
AtomicLong ai = new AtomicLong(); |
212 |
< |
for (long i = -12; i < 6; ++i) { |
212 |
> |
assertEquals("0", ai.toString()); |
213 |
> |
for (long i : VALUES) { |
214 |
|
ai.set(i); |
215 |
|
assertEquals(ai.toString(), Long.toString(i)); |
216 |
|
} |
217 |
|
} |
218 |
|
|
219 |
|
/** |
220 |
+ |
* intValue returns current value. |
221 |
+ |
*/ |
222 |
+ |
public void testIntValue() { |
223 |
+ |
AtomicLong ai = new AtomicLong(); |
224 |
+ |
assertEquals(0, ai.intValue()); |
225 |
+ |
for (long x : VALUES) { |
226 |
+ |
ai.set(x); |
227 |
+ |
assertEquals((int)x, ai.intValue()); |
228 |
+ |
} |
229 |
+ |
} |
230 |
+ |
|
231 |
+ |
/** |
232 |
|
* longValue returns current value. |
233 |
|
*/ |
234 |
|
public void testLongValue() { |
235 |
|
AtomicLong ai = new AtomicLong(); |
236 |
< |
for (int i = -12; i < 6; ++i) { |
237 |
< |
ai.set(i); |
238 |
< |
assertEquals((long)i, ai.longValue()); |
236 |
> |
assertEquals(0L, ai.longValue()); |
237 |
> |
for (long x : VALUES) { |
238 |
> |
ai.set(x); |
239 |
> |
assertEquals(x, ai.longValue()); |
240 |
|
} |
241 |
|
} |
242 |
|
|
245 |
|
*/ |
246 |
|
public void testFloatValue() { |
247 |
|
AtomicLong ai = new AtomicLong(); |
248 |
< |
for (int i = -12; i < 6; ++i) { |
249 |
< |
ai.set(i); |
250 |
< |
assertEquals((float)i, ai.floatValue()); |
248 |
> |
assertEquals(0.0f, ai.floatValue()); |
249 |
> |
for (long x : VALUES) { |
250 |
> |
ai.set(x); |
251 |
> |
assertEquals((float)x, ai.floatValue()); |
252 |
|
} |
253 |
|
} |
254 |
|
|
257 |
|
*/ |
258 |
|
public void testDoubleValue() { |
259 |
|
AtomicLong ai = new AtomicLong(); |
260 |
< |
for (int i = -12; i < 6; ++i) { |
261 |
< |
ai.set(i); |
262 |
< |
assertEquals((double)i, ai.doubleValue()); |
260 |
> |
assertEquals(0.0d, ai.doubleValue()); |
261 |
> |
for (long x : VALUES) { |
262 |
> |
ai.set(x); |
263 |
> |
assertEquals((double)x, ai.doubleValue()); |
264 |
|
} |
265 |
|
} |
266 |
|
|
252 |
– |
|
267 |
|
} |