1 |
dl |
1.1 |
/* |
2 |
dl |
1.6 |
* 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 |
jsr166 |
1.10 |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
|
* Pat Fisher, Mike Judd. |
7 |
dl |
1.1 |
*/ |
8 |
|
|
|
9 |
|
|
import java.util.concurrent.atomic.*; |
10 |
|
|
import junit.framework.*; |
11 |
|
|
import java.util.*; |
12 |
|
|
|
13 |
dl |
1.2 |
public class AtomicLongFieldUpdaterTest extends JSR166TestCase { |
14 |
dl |
1.1 |
volatile long x = 0; |
15 |
dl |
1.4 |
int z; |
16 |
|
|
long w; |
17 |
dl |
1.1 |
|
18 |
|
|
public static void main(String[] args){ |
19 |
|
|
junit.textui.TestRunner.run(suite()); |
20 |
|
|
} |
21 |
|
|
public static Test suite() { |
22 |
|
|
return new TestSuite(AtomicLongFieldUpdaterTest.class); |
23 |
|
|
} |
24 |
|
|
|
25 |
dl |
1.3 |
/** |
26 |
dl |
1.7 |
* Construction with non-existent field throws RuntimeException |
27 |
dl |
1.3 |
*/ |
28 |
dl |
1.1 |
public void testConstructor(){ |
29 |
|
|
try{ |
30 |
jsr166 |
1.10 |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> |
31 |
dl |
1.1 |
a = AtomicLongFieldUpdater.newUpdater |
32 |
dl |
1.5 |
(AtomicLongFieldUpdaterTest.class, "y"); |
33 |
dl |
1.3 |
shouldThrow(); |
34 |
dl |
1.1 |
} |
35 |
|
|
catch (RuntimeException rt) {} |
36 |
|
|
} |
37 |
|
|
|
38 |
dl |
1.3 |
/** |
39 |
dl |
1.4 |
* construction with field not of given type throws RuntimeException |
40 |
dl |
1.3 |
*/ |
41 |
dl |
1.1 |
public void testConstructor2(){ |
42 |
|
|
try{ |
43 |
jsr166 |
1.10 |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> |
44 |
dl |
1.1 |
a = AtomicLongFieldUpdater.newUpdater |
45 |
dl |
1.5 |
(AtomicLongFieldUpdaterTest.class, "z"); |
46 |
dl |
1.3 |
shouldThrow(); |
47 |
dl |
1.1 |
} |
48 |
|
|
catch (RuntimeException rt) {} |
49 |
|
|
} |
50 |
|
|
|
51 |
dl |
1.3 |
/** |
52 |
dl |
1.4 |
* construction with non-volatile field throws RuntimeException |
53 |
dl |
1.3 |
*/ |
54 |
dl |
1.1 |
public void testConstructor3(){ |
55 |
|
|
try{ |
56 |
jsr166 |
1.10 |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> |
57 |
dl |
1.1 |
a = AtomicLongFieldUpdater.newUpdater |
58 |
dl |
1.5 |
(AtomicLongFieldUpdaterTest.class, "w"); |
59 |
dl |
1.3 |
shouldThrow(); |
60 |
dl |
1.1 |
} |
61 |
|
|
|
62 |
|
|
catch (RuntimeException rt) {} |
63 |
|
|
} |
64 |
|
|
|
65 |
dl |
1.3 |
/** |
66 |
dl |
1.4 |
* get returns the last value set or assigned |
67 |
dl |
1.3 |
*/ |
68 |
dl |
1.1 |
public void testGetSet(){ |
69 |
dl |
1.8 |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a; |
70 |
|
|
try { |
71 |
|
|
a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
72 |
|
|
} catch (RuntimeException ok) { |
73 |
|
|
return; |
74 |
|
|
} |
75 |
dl |
1.1 |
x = 1; |
76 |
|
|
assertEquals(1,a.get(this)); |
77 |
|
|
a.set(this,2); |
78 |
|
|
assertEquals(2,a.get(this)); |
79 |
|
|
a.set(this,-3); |
80 |
|
|
assertEquals(-3,a.get(this)); |
81 |
|
|
} |
82 |
dl |
1.9 |
|
83 |
|
|
/** |
84 |
|
|
* get returns the last value lazySet by same thread |
85 |
|
|
*/ |
86 |
|
|
public void testGetLazySet(){ |
87 |
|
|
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a; |
88 |
|
|
try { |
89 |
|
|
a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
90 |
|
|
} catch (RuntimeException ok) { |
91 |
|
|
return; |
92 |
|
|
} |
93 |
|
|
x = 1; |
94 |
|
|
assertEquals(1,a.get(this)); |
95 |
|
|
a.lazySet(this,2); |
96 |
|
|
assertEquals(2,a.get(this)); |
97 |
|
|
a.lazySet(this,-3); |
98 |
|
|
assertEquals(-3,a.get(this)); |
99 |
|
|
} |
100 |
|
|
|
101 |
|
|
|
102 |
dl |
1.3 |
/** |
103 |
dl |
1.4 |
* compareAndSet succeeds in changing value if equal to expected else fails |
104 |
dl |
1.3 |
*/ |
105 |
dl |
1.1 |
public void testCompareAndSet(){ |
106 |
dl |
1.8 |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a; |
107 |
|
|
try { |
108 |
|
|
a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
109 |
|
|
} catch (RuntimeException ok) { |
110 |
|
|
return; |
111 |
|
|
} |
112 |
dl |
1.1 |
x = 1; |
113 |
|
|
assertTrue(a.compareAndSet(this,1,2)); |
114 |
|
|
assertTrue(a.compareAndSet(this,2,-4)); |
115 |
|
|
assertEquals(-4,a.get(this)); |
116 |
|
|
assertFalse(a.compareAndSet(this,-5,7)); |
117 |
|
|
assertFalse((7 == a.get(this))); |
118 |
|
|
assertTrue(a.compareAndSet(this,-4,7)); |
119 |
|
|
assertEquals(7,a.get(this)); |
120 |
|
|
} |
121 |
|
|
|
122 |
dl |
1.4 |
|
123 |
|
|
/** |
124 |
|
|
* compareAndSet in one thread enables another waiting for value |
125 |
|
|
* to succeed |
126 |
|
|
*/ |
127 |
|
|
public void testCompareAndSetInMultipleThreads() { |
128 |
|
|
x = 1; |
129 |
dl |
1.8 |
final AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest>a; |
130 |
|
|
try { |
131 |
|
|
a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
132 |
|
|
} catch (RuntimeException ok) { |
133 |
|
|
return; |
134 |
|
|
} |
135 |
dl |
1.4 |
|
136 |
|
|
Thread t = new Thread(new Runnable() { |
137 |
|
|
public void run() { |
138 |
|
|
while(!a.compareAndSet(AtomicLongFieldUpdaterTest.this, 2, 3)) Thread.yield(); |
139 |
|
|
}}); |
140 |
|
|
try { |
141 |
|
|
t.start(); |
142 |
|
|
assertTrue(a.compareAndSet(this, 1, 2)); |
143 |
|
|
t.join(LONG_DELAY_MS); |
144 |
|
|
assertFalse(t.isAlive()); |
145 |
|
|
assertEquals(a.get(this), 3); |
146 |
|
|
} |
147 |
|
|
catch(Exception e) { |
148 |
|
|
unexpectedException(); |
149 |
|
|
} |
150 |
|
|
} |
151 |
|
|
|
152 |
dl |
1.3 |
/** |
153 |
dl |
1.4 |
* repeated weakCompareAndSet succeeds in changing value when equal |
154 |
jsr166 |
1.10 |
* to expected |
155 |
dl |
1.3 |
*/ |
156 |
dl |
1.1 |
public void testWeakCompareAndSet(){ |
157 |
dl |
1.8 |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a; |
158 |
|
|
try { |
159 |
|
|
a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
160 |
|
|
} catch (RuntimeException ok) { |
161 |
|
|
return; |
162 |
|
|
} |
163 |
dl |
1.1 |
x = 1; |
164 |
|
|
while(!a.weakCompareAndSet(this,1,2)); |
165 |
|
|
while(!a.weakCompareAndSet(this,2,-4)); |
166 |
|
|
assertEquals(-4,a.get(this)); |
167 |
|
|
while(!a.weakCompareAndSet(this,-4,7)); |
168 |
|
|
assertEquals(7,a.get(this)); |
169 |
|
|
} |
170 |
|
|
|
171 |
dl |
1.3 |
/** |
172 |
dl |
1.4 |
* getAndSet returns previous value and sets to given value |
173 |
dl |
1.3 |
*/ |
174 |
dl |
1.1 |
public void testGetAndSet(){ |
175 |
dl |
1.8 |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a; |
176 |
|
|
try { |
177 |
|
|
a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
178 |
|
|
} catch (RuntimeException ok) { |
179 |
|
|
return; |
180 |
|
|
} |
181 |
dl |
1.1 |
x = 1; |
182 |
|
|
assertEquals(1,a.getAndSet(this, 0)); |
183 |
|
|
assertEquals(0,a.getAndSet(this,-10)); |
184 |
|
|
assertEquals(-10,a.getAndSet(this,1)); |
185 |
|
|
} |
186 |
|
|
|
187 |
dl |
1.3 |
/** |
188 |
dl |
1.4 |
* getAndAdd returns previous value and adds given value |
189 |
dl |
1.3 |
*/ |
190 |
dl |
1.1 |
public void testGetAndAdd(){ |
191 |
dl |
1.8 |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a; |
192 |
|
|
try { |
193 |
|
|
a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
194 |
|
|
} catch (RuntimeException ok) { |
195 |
|
|
return; |
196 |
|
|
} |
197 |
dl |
1.1 |
x = 1; |
198 |
|
|
assertEquals(1,a.getAndAdd(this,2)); |
199 |
|
|
assertEquals(3,a.get(this)); |
200 |
|
|
assertEquals(3,a.getAndAdd(this,-4)); |
201 |
|
|
assertEquals(-1,a.get(this)); |
202 |
|
|
} |
203 |
|
|
|
204 |
dl |
1.3 |
/** |
205 |
dl |
1.4 |
* getAndDecrement returns previous value and decrements |
206 |
dl |
1.3 |
*/ |
207 |
dl |
1.1 |
public void testGetAndDecrement(){ |
208 |
dl |
1.8 |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a; |
209 |
|
|
try { |
210 |
|
|
a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
211 |
|
|
} catch (RuntimeException ok) { |
212 |
|
|
return; |
213 |
|
|
} |
214 |
dl |
1.1 |
x = 1; |
215 |
|
|
assertEquals(1,a.getAndDecrement(this)); |
216 |
|
|
assertEquals(0,a.getAndDecrement(this)); |
217 |
|
|
assertEquals(-1,a.getAndDecrement(this)); |
218 |
|
|
} |
219 |
|
|
|
220 |
dl |
1.3 |
/** |
221 |
dl |
1.4 |
* getAndIncrement returns previous value and increments |
222 |
dl |
1.3 |
*/ |
223 |
dl |
1.1 |
public void testGetAndIncrement(){ |
224 |
dl |
1.8 |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a; |
225 |
|
|
try { |
226 |
|
|
a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
227 |
|
|
} catch (RuntimeException ok) { |
228 |
|
|
return; |
229 |
|
|
} |
230 |
dl |
1.1 |
x = 1; |
231 |
|
|
assertEquals(1,a.getAndIncrement(this)); |
232 |
|
|
assertEquals(2,a.get(this)); |
233 |
|
|
a.set(this,-2); |
234 |
|
|
assertEquals(-2,a.getAndIncrement(this)); |
235 |
|
|
assertEquals(-1,a.getAndIncrement(this)); |
236 |
|
|
assertEquals(0,a.getAndIncrement(this)); |
237 |
|
|
assertEquals(1,a.get(this)); |
238 |
|
|
} |
239 |
|
|
|
240 |
dl |
1.3 |
/** |
241 |
dl |
1.4 |
* addAndGet adds given value to current, and returns current value |
242 |
dl |
1.3 |
*/ |
243 |
dl |
1.1 |
public void testAddAndGet(){ |
244 |
dl |
1.8 |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a; |
245 |
|
|
try { |
246 |
|
|
a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
247 |
|
|
} catch (RuntimeException ok) { |
248 |
|
|
return; |
249 |
|
|
} |
250 |
dl |
1.1 |
x = 1; |
251 |
|
|
assertEquals(3,a.addAndGet(this,2)); |
252 |
|
|
assertEquals(3,a.get(this)); |
253 |
|
|
assertEquals(-1,a.addAndGet(this,-4)); |
254 |
|
|
assertEquals(-1,a.get(this)); |
255 |
|
|
} |
256 |
|
|
|
257 |
dl |
1.3 |
/** |
258 |
dl |
1.4 |
* decrementAndGet decrements and returns current value |
259 |
dl |
1.3 |
*/ |
260 |
dl |
1.1 |
public void testDecrementAndGet(){ |
261 |
dl |
1.8 |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a; |
262 |
|
|
try { |
263 |
|
|
a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
264 |
|
|
} catch (RuntimeException ok) { |
265 |
|
|
return; |
266 |
|
|
} |
267 |
dl |
1.1 |
x = 1; |
268 |
|
|
assertEquals(0,a.decrementAndGet(this)); |
269 |
|
|
assertEquals(-1,a.decrementAndGet(this)); |
270 |
|
|
assertEquals(-2,a.decrementAndGet(this)); |
271 |
|
|
assertEquals(-2,a.get(this)); |
272 |
|
|
} |
273 |
|
|
|
274 |
dl |
1.3 |
/** |
275 |
dl |
1.4 |
* incrementAndGet increments and returns current value |
276 |
dl |
1.3 |
*/ |
277 |
dl |
1.1 |
public void testIncrementAndGet(){ |
278 |
dl |
1.8 |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a; |
279 |
|
|
try { |
280 |
|
|
a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
281 |
|
|
} catch (RuntimeException ok) { |
282 |
|
|
return; |
283 |
|
|
} |
284 |
dl |
1.1 |
x = 1; |
285 |
|
|
assertEquals(2,a.incrementAndGet(this)); |
286 |
|
|
assertEquals(2,a.get(this)); |
287 |
|
|
a.set(this,-2); |
288 |
|
|
assertEquals(-1,a.incrementAndGet(this)); |
289 |
|
|
assertEquals(0,a.incrementAndGet(this)); |
290 |
|
|
assertEquals(1,a.incrementAndGet(this)); |
291 |
|
|
assertEquals(1,a.get(this)); |
292 |
|
|
} |
293 |
|
|
|
294 |
|
|
} |