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 |
|
|
|
11 |
|
|
public class AtomicBooleanTest extends TestCase { |
12 |
|
|
public static void main (String[] args) { |
13 |
|
|
junit.textui.TestRunner.run (suite()); |
14 |
|
|
} |
15 |
|
|
public static Test suite() { |
16 |
|
|
return new TestSuite(AtomicBooleanTest.class); |
17 |
|
|
} |
18 |
|
|
|
19 |
|
|
public void testConstructor(){ |
20 |
|
|
AtomicBoolean ai = new AtomicBoolean(true); |
21 |
|
|
assertEquals(true,ai.get()); |
22 |
|
|
} |
23 |
|
|
|
24 |
|
|
public void testConstructor2(){ |
25 |
|
|
AtomicBoolean ai = new AtomicBoolean(); |
26 |
|
|
assertEquals(false,ai.get()); |
27 |
|
|
} |
28 |
|
|
|
29 |
|
|
public void testGetSet(){ |
30 |
|
|
AtomicBoolean ai = new AtomicBoolean(true); |
31 |
|
|
assertEquals(true,ai.get()); |
32 |
|
|
ai.set(false); |
33 |
|
|
assertEquals(false,ai.get()); |
34 |
|
|
ai.set(true); |
35 |
|
|
assertEquals(true,ai.get()); |
36 |
|
|
|
37 |
|
|
} |
38 |
|
|
public void testCompareAndSet(){ |
39 |
|
|
AtomicBoolean ai = new AtomicBoolean(true); |
40 |
|
|
assertTrue(ai.compareAndSet(true,false)); |
41 |
|
|
assertEquals(false,ai.get()); |
42 |
|
|
assertTrue(ai.compareAndSet(false,false)); |
43 |
|
|
assertEquals(false,ai.get()); |
44 |
|
|
assertFalse(ai.compareAndSet(true,false)); |
45 |
|
|
assertFalse((ai.get())); |
46 |
|
|
assertTrue(ai.compareAndSet(false,true)); |
47 |
|
|
assertEquals(true,ai.get()); |
48 |
|
|
} |
49 |
|
|
|
50 |
|
|
public void testWeakCompareAndSet(){ |
51 |
|
|
AtomicBoolean ai = new AtomicBoolean(true); |
52 |
|
|
while(!ai.weakCompareAndSet(true,false)); |
53 |
|
|
assertEquals(false,ai.get()); |
54 |
|
|
while(!ai.weakCompareAndSet(false,false)); |
55 |
|
|
assertEquals(false,ai.get()); |
56 |
|
|
while(!ai.weakCompareAndSet(false,true)); |
57 |
|
|
assertEquals(true,ai.get()); |
58 |
|
|
} |
59 |
|
|
|
60 |
|
|
public void testGetAndSet(){ |
61 |
|
|
AtomicBoolean ai = new AtomicBoolean(true); |
62 |
|
|
assertEquals(true,ai.getAndSet(false)); |
63 |
|
|
assertEquals(false,ai.getAndSet(false)); |
64 |
|
|
assertEquals(false,ai.getAndSet(true)); |
65 |
|
|
assertEquals(true,ai.get()); |
66 |
|
|
} |
67 |
|
|
|
68 |
|
|
|
69 |
|
|
} |