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. |
6 |
*/ |
7 |
|
8 |
import junit.framework.*; |
9 |
import java.util.concurrent.atomic.*; |
10 |
import java.io.*; |
11 |
|
12 |
public class AtomicBooleanTest extends JSR166TestCase { |
13 |
public static void main (String[] args) { |
14 |
junit.textui.TestRunner.run (suite()); |
15 |
} |
16 |
public static Test suite() { |
17 |
return new TestSuite(AtomicBooleanTest.class); |
18 |
} |
19 |
|
20 |
/** |
21 |
* |
22 |
*/ |
23 |
public void testConstructor() { |
24 |
AtomicBoolean ai = new AtomicBoolean(true); |
25 |
assertEquals(true,ai.get()); |
26 |
} |
27 |
|
28 |
/** |
29 |
* |
30 |
*/ |
31 |
public void testConstructor2() { |
32 |
AtomicBoolean ai = new AtomicBoolean(); |
33 |
assertEquals(false,ai.get()); |
34 |
} |
35 |
|
36 |
/** |
37 |
* |
38 |
*/ |
39 |
public void testGetSet() { |
40 |
AtomicBoolean ai = new AtomicBoolean(true); |
41 |
assertEquals(true,ai.get()); |
42 |
ai.set(false); |
43 |
assertEquals(false,ai.get()); |
44 |
ai.set(true); |
45 |
assertEquals(true,ai.get()); |
46 |
|
47 |
} |
48 |
/** |
49 |
* |
50 |
*/ |
51 |
public void testCompareAndSet() { |
52 |
AtomicBoolean ai = new AtomicBoolean(true); |
53 |
assertTrue(ai.compareAndSet(true,false)); |
54 |
assertEquals(false,ai.get()); |
55 |
assertTrue(ai.compareAndSet(false,false)); |
56 |
assertEquals(false,ai.get()); |
57 |
assertFalse(ai.compareAndSet(true,false)); |
58 |
assertFalse((ai.get())); |
59 |
assertTrue(ai.compareAndSet(false,true)); |
60 |
assertEquals(true,ai.get()); |
61 |
} |
62 |
|
63 |
/** |
64 |
* |
65 |
*/ |
66 |
public void testWeakCompareAndSet() { |
67 |
AtomicBoolean ai = new AtomicBoolean(true); |
68 |
while(!ai.weakCompareAndSet(true,false)); |
69 |
assertEquals(false,ai.get()); |
70 |
while(!ai.weakCompareAndSet(false,false)); |
71 |
assertEquals(false,ai.get()); |
72 |
while(!ai.weakCompareAndSet(false,true)); |
73 |
assertEquals(true,ai.get()); |
74 |
} |
75 |
|
76 |
/** |
77 |
* |
78 |
*/ |
79 |
public void testGetAndSet() { |
80 |
AtomicBoolean ai = new AtomicBoolean(true); |
81 |
assertEquals(true,ai.getAndSet(false)); |
82 |
assertEquals(false,ai.getAndSet(false)); |
83 |
assertEquals(false,ai.getAndSet(true)); |
84 |
assertEquals(true,ai.get()); |
85 |
} |
86 |
|
87 |
/** |
88 |
* |
89 |
*/ |
90 |
public void testSerialization() { |
91 |
AtomicBoolean l = new AtomicBoolean(); |
92 |
|
93 |
try { |
94 |
l.set(true); |
95 |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
96 |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
97 |
out.writeObject(l); |
98 |
out.close(); |
99 |
|
100 |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
101 |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
102 |
AtomicBoolean r = (AtomicBoolean) in.readObject(); |
103 |
assertEquals(l.get(), r.get()); |
104 |
} catch(Exception e){ |
105 |
e.printStackTrace(); |
106 |
unexpectedException(); |
107 |
} |
108 |
} |
109 |
|
110 |
|
111 |
} |