9 |
|
import java.util.concurrent.atomic.*; |
10 |
|
import java.io.*; |
11 |
|
|
12 |
< |
public class AtomicBooleanTest extends TestCase { |
12 |
> |
public class AtomicBooleanTest extends JSR166TestCase { |
13 |
|
public static void main (String[] args) { |
14 |
|
junit.textui.TestRunner.run (suite()); |
15 |
|
} |
17 |
|
return new TestSuite(AtomicBooleanTest.class); |
18 |
|
} |
19 |
|
|
20 |
< |
public void testConstructor(){ |
20 |
> |
/** |
21 |
> |
* constructor initializes to given value |
22 |
> |
*/ |
23 |
> |
public void testConstructor() { |
24 |
|
AtomicBoolean ai = new AtomicBoolean(true); |
25 |
|
assertEquals(true,ai.get()); |
26 |
|
} |
27 |
|
|
28 |
< |
public void testConstructor2(){ |
28 |
> |
/** |
29 |
> |
* default constructed intializes to false |
30 |
> |
*/ |
31 |
> |
public void testConstructor2() { |
32 |
|
AtomicBoolean ai = new AtomicBoolean(); |
33 |
|
assertEquals(false,ai.get()); |
34 |
|
} |
35 |
|
|
36 |
< |
public void testGetSet(){ |
36 |
> |
/** |
37 |
> |
* get returns the last value set |
38 |
> |
*/ |
39 |
> |
public void testGetSet() { |
40 |
|
AtomicBoolean ai = new AtomicBoolean(true); |
41 |
|
assertEquals(true,ai.get()); |
42 |
|
ai.set(false); |
45 |
|
assertEquals(true,ai.get()); |
46 |
|
|
47 |
|
} |
48 |
< |
public void testCompareAndSet(){ |
48 |
> |
|
49 |
> |
/** |
50 |
> |
* compareAndSet succeeds in changing value if equal to expected else fails |
51 |
> |
*/ |
52 |
> |
public void testCompareAndSet() { |
53 |
|
AtomicBoolean ai = new AtomicBoolean(true); |
54 |
|
assertTrue(ai.compareAndSet(true,false)); |
55 |
|
assertEquals(false,ai.get()); |
61 |
|
assertEquals(true,ai.get()); |
62 |
|
} |
63 |
|
|
64 |
< |
public void testWeakCompareAndSet(){ |
64 |
> |
/** |
65 |
> |
* 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 |
> |
*/ |
89 |
> |
public void testWeakCompareAndSet() { |
90 |
|
AtomicBoolean ai = new AtomicBoolean(true); |
91 |
|
while(!ai.weakCompareAndSet(true,false)); |
92 |
|
assertEquals(false,ai.get()); |
96 |
|
assertEquals(true,ai.get()); |
97 |
|
} |
98 |
|
|
99 |
< |
public void testGetAndSet(){ |
99 |
> |
/** |
100 |
> |
* getAndSet returns previous value and sets to given value |
101 |
> |
*/ |
102 |
> |
public void testGetAndSet() { |
103 |
|
AtomicBoolean ai = new AtomicBoolean(true); |
104 |
|
assertEquals(true,ai.getAndSet(false)); |
105 |
|
assertEquals(false,ai.getAndSet(false)); |
107 |
|
assertEquals(true,ai.get()); |
108 |
|
} |
109 |
|
|
110 |
+ |
/** |
111 |
+ |
* a deserialized serialized atomic holds same value |
112 |
+ |
*/ |
113 |
|
public void testSerialization() { |
114 |
|
AtomicBoolean l = new AtomicBoolean(); |
115 |
|
|
126 |
|
assertEquals(l.get(), r.get()); |
127 |
|
} catch(Exception e){ |
128 |
|
e.printStackTrace(); |
129 |
< |
fail("unexpected exception"); |
129 |
> |
unexpectedException(); |
130 |
|
} |
131 |
|
} |
132 |
|
|