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. |
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 |
5 |
> |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
> |
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
|
import junit.framework.*; |
19 |
|
} |
20 |
|
|
21 |
|
/** |
22 |
< |
* |
22 |
> |
* constructor initializes to given value |
23 |
|
*/ |
24 |
|
public void testConstructor() { |
25 |
|
AtomicBoolean ai = new AtomicBoolean(true); |
27 |
|
} |
28 |
|
|
29 |
|
/** |
30 |
< |
* |
30 |
> |
* default constructed initializes to false |
31 |
|
*/ |
32 |
|
public void testConstructor2() { |
33 |
|
AtomicBoolean ai = new AtomicBoolean(); |
35 |
|
} |
36 |
|
|
37 |
|
/** |
38 |
< |
* |
38 |
> |
* get returns the last value set |
39 |
|
*/ |
40 |
|
public void testGetSet() { |
41 |
|
AtomicBoolean ai = new AtomicBoolean(true); |
46 |
|
assertEquals(true,ai.get()); |
47 |
|
|
48 |
|
} |
49 |
+ |
|
50 |
|
/** |
51 |
< |
* |
51 |
> |
* compareAndSet succeeds in changing value if equal to expected else fails |
52 |
|
*/ |
53 |
|
public void testCompareAndSet() { |
54 |
|
AtomicBoolean ai = new AtomicBoolean(true); |
63 |
|
} |
64 |
|
|
65 |
|
/** |
66 |
< |
* |
66 |
> |
* compareAndSet in one thread enables another waiting for value |
67 |
> |
* to succeed |
68 |
> |
*/ |
69 |
> |
public void testCompareAndSetInMultipleThreads() { |
70 |
> |
final AtomicBoolean ai = new AtomicBoolean(true); |
71 |
> |
Thread t = new Thread(new Runnable() { |
72 |
> |
public void run() { |
73 |
> |
while(!ai.compareAndSet(false, true)) Thread.yield(); |
74 |
> |
}}); |
75 |
> |
try { |
76 |
> |
t.start(); |
77 |
> |
assertTrue(ai.compareAndSet(true, false)); |
78 |
> |
t.join(LONG_DELAY_MS); |
79 |
> |
assertFalse(t.isAlive()); |
80 |
> |
} |
81 |
> |
catch(Exception e) { |
82 |
> |
unexpectedException(); |
83 |
> |
} |
84 |
> |
} |
85 |
> |
|
86 |
> |
/** |
87 |
> |
* repeated weakCompareAndSet succeeds in changing value when equal |
88 |
> |
* to expected |
89 |
|
*/ |
90 |
|
public void testWeakCompareAndSet() { |
91 |
|
AtomicBoolean ai = new AtomicBoolean(true); |
98 |
|
} |
99 |
|
|
100 |
|
/** |
101 |
< |
* |
101 |
> |
* getAndSet returns previous value and sets to given value |
102 |
|
*/ |
103 |
|
public void testGetAndSet() { |
104 |
|
AtomicBoolean ai = new AtomicBoolean(true); |
109 |
|
} |
110 |
|
|
111 |
|
/** |
112 |
< |
* |
112 |
> |
* a deserialized serialized atomic holds same value |
113 |
|
*/ |
114 |
|
public void testSerialization() { |
115 |
|
AtomicBoolean l = new AtomicBoolean(); |
131 |
|
} |
132 |
|
} |
133 |
|
|
134 |
+ |
/** |
135 |
+ |
* toString returns current value. |
136 |
+ |
*/ |
137 |
+ |
public void testToString() { |
138 |
+ |
AtomicBoolean ai = new AtomicBoolean(); |
139 |
+ |
assertEquals(ai.toString(), Boolean.toString(false)); |
140 |
+ |
ai.set(true); |
141 |
+ |
assertEquals(ai.toString(), Boolean.toString(true)); |
142 |
+ |
} |
143 |
|
|
144 |
|
} |