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.AtomicInteger; |
11 |
|
|
12 |
|
public class AtomicIntegerTest 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(AtomicIntegerTest.class); |
18 |
|
} |
19 |
|
|
20 |
+ |
final int[] VALUES = { |
21 |
+ |
Integer.MIN_VALUE, -1, 0, 1, 42, Integer.MAX_VALUE, |
22 |
+ |
}; |
23 |
+ |
|
24 |
|
/** |
25 |
|
* constructor initializes to given value |
26 |
|
*/ |
47 |
|
assertEquals(2,ai.get()); |
48 |
|
ai.set(-3); |
49 |
|
assertEquals(-3,ai.get()); |
47 |
– |
|
50 |
|
} |
51 |
|
|
52 |
|
/** |
59 |
|
assertEquals(2,ai.get()); |
60 |
|
ai.lazySet(-3); |
61 |
|
assertEquals(-3,ai.get()); |
60 |
– |
|
62 |
|
} |
63 |
+ |
|
64 |
|
/** |
65 |
|
* compareAndSet succeeds in changing value if equal to expected else fails |
66 |
|
*/ |
70 |
|
assertTrue(ai.compareAndSet(2,-4)); |
71 |
|
assertEquals(-4,ai.get()); |
72 |
|
assertFalse(ai.compareAndSet(-5,7)); |
73 |
< |
assertFalse((7 == ai.get())); |
73 |
> |
assertEquals(-4,ai.get()); |
74 |
|
assertTrue(ai.compareAndSet(-4,7)); |
75 |
|
assertEquals(7,ai.get()); |
76 |
|
} |
192 |
|
* a deserialized serialized atomic holds same value |
193 |
|
*/ |
194 |
|
public void testSerialization() throws Exception { |
195 |
< |
AtomicInteger l = new AtomicInteger(); |
196 |
< |
|
197 |
< |
l.set(22); |
198 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
199 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
200 |
< |
out.writeObject(l); |
201 |
< |
out.close(); |
202 |
< |
|
201 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
202 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
203 |
< |
AtomicInteger r = (AtomicInteger) in.readObject(); |
204 |
< |
assertEquals(l.get(), r.get()); |
195 |
> |
AtomicInteger x = new AtomicInteger(); |
196 |
> |
AtomicInteger y = serialClone(x); |
197 |
> |
assertTrue(x != y); |
198 |
> |
x.set(22); |
199 |
> |
AtomicInteger z = serialClone(x); |
200 |
> |
assertEquals(22, x.get()); |
201 |
> |
assertEquals(0, y.get()); |
202 |
> |
assertEquals(22, z.get()); |
203 |
|
} |
204 |
|
|
205 |
|
/** |
207 |
|
*/ |
208 |
|
public void testToString() { |
209 |
|
AtomicInteger ai = new AtomicInteger(); |
210 |
< |
for (int i = -12; i < 6; ++i) { |
211 |
< |
ai.set(i); |
212 |
< |
assertEquals(ai.toString(), Integer.toString(i)); |
210 |
> |
assertEquals("0", ai.toString()); |
211 |
> |
for (int x : VALUES) { |
212 |
> |
ai.set(x); |
213 |
> |
assertEquals(ai.toString(), Integer.toString(x)); |
214 |
|
} |
215 |
|
} |
216 |
|
|
219 |
|
*/ |
220 |
|
public void testIntValue() { |
221 |
|
AtomicInteger ai = new AtomicInteger(); |
222 |
< |
for (int i = -12; i < 6; ++i) { |
223 |
< |
ai.set(i); |
224 |
< |
assertEquals(i, ai.intValue()); |
222 |
> |
assertEquals(0, ai.intValue()); |
223 |
> |
for (int x : VALUES) { |
224 |
> |
ai.set(x); |
225 |
> |
assertEquals(x, ai.intValue()); |
226 |
|
} |
227 |
|
} |
228 |
|
|
229 |
– |
|
229 |
|
/** |
230 |
|
* longValue returns current value. |
231 |
|
*/ |
232 |
|
public void testLongValue() { |
233 |
|
AtomicInteger ai = new AtomicInteger(); |
234 |
< |
for (int i = -12; i < 6; ++i) { |
235 |
< |
ai.set(i); |
236 |
< |
assertEquals((long)i, ai.longValue()); |
234 |
> |
assertEquals(0L, ai.longValue()); |
235 |
> |
for (int x : VALUES) { |
236 |
> |
ai.set(x); |
237 |
> |
assertEquals((long)x, ai.longValue()); |
238 |
|
} |
239 |
|
} |
240 |
|
|
243 |
|
*/ |
244 |
|
public void testFloatValue() { |
245 |
|
AtomicInteger ai = new AtomicInteger(); |
246 |
< |
for (int i = -12; i < 6; ++i) { |
247 |
< |
ai.set(i); |
248 |
< |
assertEquals((float)i, ai.floatValue()); |
246 |
> |
assertEquals(0.0f, ai.floatValue()); |
247 |
> |
for (int x : VALUES) { |
248 |
> |
ai.set(x); |
249 |
> |
assertEquals((float)x, ai.floatValue()); |
250 |
|
} |
251 |
|
} |
252 |
|
|
255 |
|
*/ |
256 |
|
public void testDoubleValue() { |
257 |
|
AtomicInteger ai = new AtomicInteger(); |
258 |
< |
for (int i = -12; i < 6; ++i) { |
259 |
< |
ai.set(i); |
260 |
< |
assertEquals((double)i, ai.doubleValue()); |
258 |
> |
assertEquals(0.0d, ai.doubleValue()); |
259 |
> |
for (int x : VALUES) { |
260 |
> |
ai.set(x); |
261 |
> |
assertEquals((double)x, ai.doubleValue()); |
262 |
|
} |
263 |
|
} |
264 |
|
|
263 |
– |
|
264 |
– |
|
265 |
|
} |