1 |
dl |
1.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 |
dl |
1.2 |
import java.io.*; |
11 |
dl |
1.1 |
|
12 |
dl |
1.3 |
public class AtomicBooleanTest extends JSR166TestCase { |
13 |
dl |
1.1 |
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 |
dl |
1.4 |
/** |
21 |
dl |
1.5 |
* constructor initializes to given value |
22 |
dl |
1.4 |
*/ |
23 |
|
|
public void testConstructor() { |
24 |
dl |
1.1 |
AtomicBoolean ai = new AtomicBoolean(true); |
25 |
|
|
assertEquals(true,ai.get()); |
26 |
|
|
} |
27 |
|
|
|
28 |
dl |
1.4 |
/** |
29 |
dl |
1.5 |
* default constructed intializes to false |
30 |
dl |
1.4 |
*/ |
31 |
|
|
public void testConstructor2() { |
32 |
dl |
1.1 |
AtomicBoolean ai = new AtomicBoolean(); |
33 |
|
|
assertEquals(false,ai.get()); |
34 |
|
|
} |
35 |
|
|
|
36 |
dl |
1.4 |
/** |
37 |
dl |
1.5 |
* get returns the last value set |
38 |
dl |
1.4 |
*/ |
39 |
|
|
public void testGetSet() { |
40 |
dl |
1.1 |
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 |
dl |
1.5 |
|
49 |
dl |
1.4 |
/** |
50 |
dl |
1.5 |
* compareAndSet succeeds in changing value if equal to expected else fails |
51 |
dl |
1.4 |
*/ |
52 |
|
|
public void testCompareAndSet() { |
53 |
dl |
1.1 |
AtomicBoolean ai = new AtomicBoolean(true); |
54 |
|
|
assertTrue(ai.compareAndSet(true,false)); |
55 |
|
|
assertEquals(false,ai.get()); |
56 |
|
|
assertTrue(ai.compareAndSet(false,false)); |
57 |
|
|
assertEquals(false,ai.get()); |
58 |
|
|
assertFalse(ai.compareAndSet(true,false)); |
59 |
|
|
assertFalse((ai.get())); |
60 |
|
|
assertTrue(ai.compareAndSet(false,true)); |
61 |
|
|
assertEquals(true,ai.get()); |
62 |
|
|
} |
63 |
|
|
|
64 |
dl |
1.4 |
/** |
65 |
dl |
1.5 |
* compareAndSet in one thread enables another waiting for value |
66 |
|
|
* to succeed |
67 |
|
|
*/ |
68 |
|
|
public void testCompareAndSetInMultipleThreads() { |
69 |
|
|
final AtomicBoolean ai = new AtomicBoolean(true); |
70 |
|
|
Thread t = new Thread(new Runnable() { |
71 |
|
|
public void run() { |
72 |
|
|
while(!ai.compareAndSet(false, true)) Thread.yield(); |
73 |
|
|
}}); |
74 |
|
|
try { |
75 |
|
|
t.start(); |
76 |
|
|
assertTrue(ai.compareAndSet(true, false)); |
77 |
|
|
t.join(LONG_DELAY_MS); |
78 |
|
|
assertFalse(t.isAlive()); |
79 |
|
|
} |
80 |
|
|
catch(Exception e) { |
81 |
|
|
unexpectedException(); |
82 |
|
|
} |
83 |
|
|
} |
84 |
|
|
|
85 |
|
|
/** |
86 |
|
|
* repeated weakCompareAndSet succeeds in changing value when equal |
87 |
|
|
* to expected |
88 |
dl |
1.4 |
*/ |
89 |
|
|
public void testWeakCompareAndSet() { |
90 |
dl |
1.1 |
AtomicBoolean ai = new AtomicBoolean(true); |
91 |
|
|
while(!ai.weakCompareAndSet(true,false)); |
92 |
|
|
assertEquals(false,ai.get()); |
93 |
|
|
while(!ai.weakCompareAndSet(false,false)); |
94 |
|
|
assertEquals(false,ai.get()); |
95 |
|
|
while(!ai.weakCompareAndSet(false,true)); |
96 |
|
|
assertEquals(true,ai.get()); |
97 |
|
|
} |
98 |
|
|
|
99 |
dl |
1.4 |
/** |
100 |
dl |
1.5 |
* getAndSet returns previous value and sets to given value |
101 |
dl |
1.4 |
*/ |
102 |
|
|
public void testGetAndSet() { |
103 |
dl |
1.1 |
AtomicBoolean ai = new AtomicBoolean(true); |
104 |
|
|
assertEquals(true,ai.getAndSet(false)); |
105 |
|
|
assertEquals(false,ai.getAndSet(false)); |
106 |
|
|
assertEquals(false,ai.getAndSet(true)); |
107 |
|
|
assertEquals(true,ai.get()); |
108 |
dl |
1.2 |
} |
109 |
|
|
|
110 |
dl |
1.4 |
/** |
111 |
dl |
1.5 |
* a deserialized serialized atomic holds same value |
112 |
dl |
1.4 |
*/ |
113 |
dl |
1.2 |
public void testSerialization() { |
114 |
|
|
AtomicBoolean l = new AtomicBoolean(); |
115 |
|
|
|
116 |
|
|
try { |
117 |
|
|
l.set(true); |
118 |
|
|
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
119 |
|
|
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
120 |
|
|
out.writeObject(l); |
121 |
|
|
out.close(); |
122 |
|
|
|
123 |
|
|
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
124 |
|
|
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
125 |
|
|
AtomicBoolean r = (AtomicBoolean) in.readObject(); |
126 |
|
|
assertEquals(l.get(), r.get()); |
127 |
|
|
} catch(Exception e){ |
128 |
|
|
e.printStackTrace(); |
129 |
dl |
1.4 |
unexpectedException(); |
130 |
dl |
1.2 |
} |
131 |
dl |
1.1 |
} |
132 |
|
|
|
133 |
|
|
|
134 |
|
|
} |