1 |
dl |
1.1 |
/* |
2 |
|
|
* Written by Doug Lea and Martin Buchholz with assistance from |
3 |
|
|
* members of JCP JSR-166 Expert Group and released to the public |
4 |
|
|
* domain, as explained at |
5 |
|
|
* http://creativecommons.org/publicdomain/zero/1.0/ |
6 |
|
|
*/ |
7 |
|
|
|
8 |
jsr166 |
1.3 |
import java.util.concurrent.atomic.AtomicInteger; |
9 |
|
|
import java.util.concurrent.atomic.AtomicIntegerArray; |
10 |
|
|
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; |
11 |
|
|
import java.util.concurrent.atomic.AtomicLong; |
12 |
|
|
import java.util.concurrent.atomic.AtomicLongArray; |
13 |
|
|
import java.util.concurrent.atomic.AtomicLongFieldUpdater; |
14 |
|
|
import java.util.concurrent.atomic.AtomicReference; |
15 |
|
|
import java.util.concurrent.atomic.AtomicReferenceArray; |
16 |
|
|
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; |
17 |
|
|
|
18 |
|
|
import junit.framework.Test; |
19 |
|
|
import junit.framework.TestSuite; |
20 |
dl |
1.1 |
|
21 |
|
|
public class Atomic8Test extends JSR166TestCase { |
22 |
|
|
|
23 |
|
|
public static void main(String[] args) { |
24 |
|
|
junit.textui.TestRunner.run(suite()); |
25 |
|
|
} |
26 |
|
|
public static Test suite() { |
27 |
|
|
return new TestSuite(Atomic8Test.class); |
28 |
|
|
} |
29 |
|
|
|
30 |
|
|
/* |
31 |
|
|
* Tests of atomic class methods accepting lambdas |
32 |
|
|
* introduced in JDK8. |
33 |
|
|
*/ |
34 |
|
|
|
35 |
|
|
static long addLong17(long x) { return x + 17; } |
36 |
|
|
static int addInt17(int x) { return x + 17; } |
37 |
|
|
static Integer addInteger17(Integer x) { |
38 |
|
|
return new Integer(x.intValue() + 17); |
39 |
|
|
} |
40 |
|
|
static Integer sumInteger(Integer x, Integer y) { |
41 |
|
|
return new Integer(x.intValue() + y.intValue()); |
42 |
|
|
} |
43 |
|
|
|
44 |
|
|
volatile long aLongField; |
45 |
|
|
volatile int anIntField; |
46 |
|
|
volatile Integer anIntegerField; |
47 |
|
|
|
48 |
jsr166 |
1.2 |
AtomicLongFieldUpdater aLongFieldUpdater() { |
49 |
|
|
return AtomicLongFieldUpdater.newUpdater |
50 |
|
|
(Atomic8Test.class, "aLongField"); |
51 |
|
|
} |
52 |
|
|
|
53 |
|
|
AtomicIntegerFieldUpdater anIntFieldUpdater() { |
54 |
|
|
return AtomicIntegerFieldUpdater.newUpdater |
55 |
|
|
(Atomic8Test.class, "anIntField"); |
56 |
|
|
} |
57 |
|
|
|
58 |
|
|
AtomicReferenceFieldUpdater<Atomic8Test,Integer> anIntegerFieldUpdater() { |
59 |
|
|
return AtomicReferenceFieldUpdater.newUpdater |
60 |
|
|
(Atomic8Test.class, Integer.class, "anIntegerField"); |
61 |
|
|
} |
62 |
|
|
|
63 |
dl |
1.1 |
/** |
64 |
|
|
* AtomicLong getAndUpdate returns previous value and updates |
65 |
|
|
* result of supplied function |
66 |
|
|
*/ |
67 |
|
|
public void testLongGetAndUpdate() { |
68 |
|
|
AtomicLong a = new AtomicLong(1L); |
69 |
|
|
assertEquals(1L, a.getAndUpdate(Atomic8Test::addLong17)); |
70 |
|
|
assertEquals(18L, a.getAndUpdate(Atomic8Test::addLong17)); |
71 |
|
|
assertEquals(35L, a.get()); |
72 |
|
|
} |
73 |
|
|
|
74 |
|
|
/** |
75 |
|
|
* AtomicLong updateAndGet updates with supplied function and |
76 |
|
|
* returns result. |
77 |
|
|
*/ |
78 |
|
|
public void testLongUpdateAndGet() { |
79 |
|
|
AtomicLong a = new AtomicLong(1L); |
80 |
|
|
assertEquals(18L, a.updateAndGet(Atomic8Test::addLong17)); |
81 |
|
|
assertEquals(35L, a.updateAndGet(Atomic8Test::addLong17)); |
82 |
|
|
} |
83 |
|
|
|
84 |
|
|
/** |
85 |
|
|
* AtomicLong getAndAccumulate returns previous value and updates |
86 |
|
|
* with supplied function. |
87 |
|
|
*/ |
88 |
|
|
public void testLongGetAndAccumulate() { |
89 |
|
|
AtomicLong a = new AtomicLong(1L); |
90 |
|
|
assertEquals(1L, a.getAndAccumulate(2L, Long::sum)); |
91 |
|
|
assertEquals(3L, a.getAndAccumulate(3L, Long::sum)); |
92 |
|
|
assertEquals(6L, a.get()); |
93 |
|
|
} |
94 |
|
|
|
95 |
|
|
/** |
96 |
|
|
* AtomicLong accumulateAndGet updates with supplied function and |
97 |
|
|
* returns result. |
98 |
|
|
*/ |
99 |
|
|
public void testLongAccumulateAndGet() { |
100 |
|
|
AtomicLong a = new AtomicLong(1L); |
101 |
|
|
assertEquals(7L, a.accumulateAndGet(6L, Long::sum)); |
102 |
|
|
assertEquals(10L, a.accumulateAndGet(3L, Long::sum)); |
103 |
jsr166 |
1.2 |
assertEquals(10L, a.get()); |
104 |
dl |
1.1 |
} |
105 |
|
|
|
106 |
|
|
/** |
107 |
|
|
* AtomicInteger getAndUpdate returns previous value and updates |
108 |
|
|
* result of supplied function |
109 |
|
|
*/ |
110 |
|
|
public void testIntGetAndUpdate() { |
111 |
|
|
AtomicInteger a = new AtomicInteger(1); |
112 |
|
|
assertEquals(1, a.getAndUpdate(Atomic8Test::addInt17)); |
113 |
|
|
assertEquals(18, a.getAndUpdate(Atomic8Test::addInt17)); |
114 |
|
|
assertEquals(35, a.get()); |
115 |
|
|
} |
116 |
|
|
|
117 |
|
|
/** |
118 |
|
|
* AtomicInteger updateAndGet updates with supplied function and |
119 |
|
|
* returns result. |
120 |
|
|
*/ |
121 |
|
|
public void testIntUpdateAndGet() { |
122 |
|
|
AtomicInteger a = new AtomicInteger(1); |
123 |
|
|
assertEquals(18, a.updateAndGet(Atomic8Test::addInt17)); |
124 |
|
|
assertEquals(35, a.updateAndGet(Atomic8Test::addInt17)); |
125 |
jsr166 |
1.2 |
assertEquals(35, a.get()); |
126 |
dl |
1.1 |
} |
127 |
|
|
|
128 |
|
|
/** |
129 |
|
|
* AtomicInteger getAndAccumulate returns previous value and updates |
130 |
|
|
* with supplied function. |
131 |
|
|
*/ |
132 |
|
|
public void testIntGetAndAccumulate() { |
133 |
|
|
AtomicInteger a = new AtomicInteger(1); |
134 |
|
|
assertEquals(1, a.getAndAccumulate(2, Integer::sum)); |
135 |
|
|
assertEquals(3, a.getAndAccumulate(3, Integer::sum)); |
136 |
|
|
assertEquals(6, a.get()); |
137 |
|
|
} |
138 |
|
|
|
139 |
|
|
/** |
140 |
|
|
* AtomicInteger accumulateAndGet updates with supplied function and |
141 |
|
|
* returns result. |
142 |
|
|
*/ |
143 |
|
|
public void testIntAccumulateAndGet() { |
144 |
|
|
AtomicInteger a = new AtomicInteger(1); |
145 |
|
|
assertEquals(7, a.accumulateAndGet(6, Integer::sum)); |
146 |
|
|
assertEquals(10, a.accumulateAndGet(3, Integer::sum)); |
147 |
jsr166 |
1.2 |
assertEquals(10, a.get()); |
148 |
dl |
1.1 |
} |
149 |
|
|
|
150 |
|
|
/** |
151 |
|
|
* AtomicReference getAndUpdate returns previous value and updates |
152 |
|
|
* result of supplied function |
153 |
|
|
*/ |
154 |
|
|
public void testReferenceGetAndUpdate() { |
155 |
|
|
AtomicReference<Integer> a = new AtomicReference<Integer>(one); |
156 |
|
|
assertEquals(new Integer(1), a.getAndUpdate(Atomic8Test::addInteger17)); |
157 |
|
|
assertEquals(new Integer(18), a.getAndUpdate(Atomic8Test::addInteger17)); |
158 |
|
|
assertEquals(new Integer(35), a.get()); |
159 |
|
|
} |
160 |
|
|
|
161 |
|
|
/** |
162 |
|
|
* AtomicReference updateAndGet updates with supplied function and |
163 |
|
|
* returns result. |
164 |
|
|
*/ |
165 |
|
|
public void testReferenceUpdateAndGet() { |
166 |
|
|
AtomicReference<Integer> a = new AtomicReference<Integer>(one); |
167 |
|
|
assertEquals(new Integer(18), a.updateAndGet(Atomic8Test::addInteger17)); |
168 |
|
|
assertEquals(new Integer(35), a.updateAndGet(Atomic8Test::addInteger17)); |
169 |
jsr166 |
1.2 |
assertEquals(new Integer(35), a.get()); |
170 |
dl |
1.1 |
} |
171 |
|
|
|
172 |
|
|
/** |
173 |
|
|
* AtomicReference getAndAccumulate returns previous value and updates |
174 |
|
|
* with supplied function. |
175 |
|
|
*/ |
176 |
|
|
public void testReferenceGetAndAccumulate() { |
177 |
|
|
AtomicReference<Integer> a = new AtomicReference<Integer>(one); |
178 |
|
|
assertEquals(new Integer(1), a.getAndAccumulate(2, Atomic8Test::sumInteger)); |
179 |
|
|
assertEquals(new Integer(3), a.getAndAccumulate(3, Atomic8Test::sumInteger)); |
180 |
|
|
assertEquals(new Integer(6), a.get()); |
181 |
|
|
} |
182 |
|
|
|
183 |
|
|
/** |
184 |
|
|
* AtomicReference accumulateAndGet updates with supplied function and |
185 |
|
|
* returns result. |
186 |
|
|
*/ |
187 |
|
|
public void testReferenceAccumulateAndGet() { |
188 |
|
|
AtomicReference<Integer> a = new AtomicReference<Integer>(one); |
189 |
|
|
assertEquals(new Integer(7), a.accumulateAndGet(6, Atomic8Test::sumInteger)); |
190 |
|
|
assertEquals(new Integer(10), a.accumulateAndGet(3, Atomic8Test::sumInteger)); |
191 |
jsr166 |
1.2 |
assertEquals(new Integer(10), a.get()); |
192 |
dl |
1.1 |
} |
193 |
|
|
|
194 |
|
|
/** |
195 |
|
|
* AtomicLongArray getAndUpdate returns previous value and updates |
196 |
|
|
* result of supplied function |
197 |
|
|
*/ |
198 |
|
|
public void testLongArrayGetAndUpdate() { |
199 |
|
|
AtomicLongArray a = new AtomicLongArray(1); |
200 |
|
|
a.set(0, 1); |
201 |
|
|
assertEquals(1L, a.getAndUpdate(0, Atomic8Test::addLong17)); |
202 |
|
|
assertEquals(18L, a.getAndUpdate(0, Atomic8Test::addLong17)); |
203 |
|
|
assertEquals(35L, a.get(0)); |
204 |
|
|
} |
205 |
|
|
|
206 |
|
|
/** |
207 |
|
|
* AtomicLongArray updateAndGet updates with supplied function and |
208 |
|
|
* returns result. |
209 |
|
|
*/ |
210 |
|
|
public void testLongArrayUpdateAndGet() { |
211 |
|
|
AtomicLongArray a = new AtomicLongArray(1); |
212 |
|
|
a.set(0, 1); |
213 |
|
|
assertEquals(18L, a.updateAndGet(0, Atomic8Test::addLong17)); |
214 |
|
|
assertEquals(35L, a.updateAndGet(0, Atomic8Test::addLong17)); |
215 |
jsr166 |
1.2 |
assertEquals(35L, a.get(0)); |
216 |
dl |
1.1 |
} |
217 |
|
|
|
218 |
|
|
/** |
219 |
|
|
* AtomicLongArray getAndAccumulate returns previous value and updates |
220 |
|
|
* with supplied function. |
221 |
|
|
*/ |
222 |
|
|
public void testLongArrayGetAndAccumulate() { |
223 |
|
|
AtomicLongArray a = new AtomicLongArray(1); |
224 |
|
|
a.set(0, 1); |
225 |
|
|
assertEquals(1L, a.getAndAccumulate(0, 2L, Long::sum)); |
226 |
|
|
assertEquals(3L, a.getAndAccumulate(0, 3L, Long::sum)); |
227 |
|
|
assertEquals(6L, a.get(0)); |
228 |
|
|
} |
229 |
|
|
|
230 |
|
|
/** |
231 |
|
|
* AtomicLongArray accumulateAndGet updates with supplied function and |
232 |
|
|
* returns result. |
233 |
|
|
*/ |
234 |
|
|
public void testLongArrayAccumulateAndGet() { |
235 |
|
|
AtomicLongArray a = new AtomicLongArray(1); |
236 |
|
|
a.set(0, 1); |
237 |
|
|
assertEquals(7L, a.accumulateAndGet(0, 6L, Long::sum)); |
238 |
|
|
assertEquals(10L, a.accumulateAndGet(0, 3L, Long::sum)); |
239 |
jsr166 |
1.2 |
assertEquals(10L, a.get(0)); |
240 |
dl |
1.1 |
} |
241 |
|
|
|
242 |
|
|
/** |
243 |
|
|
* AtomicIntegerArray getAndUpdate returns previous value and updates |
244 |
|
|
* result of supplied function |
245 |
|
|
*/ |
246 |
|
|
public void testIntArrayGetAndUpdate() { |
247 |
|
|
AtomicIntegerArray a = new AtomicIntegerArray(1); |
248 |
|
|
a.set(0, 1); |
249 |
|
|
assertEquals(1, a.getAndUpdate(0, Atomic8Test::addInt17)); |
250 |
|
|
assertEquals(18, a.getAndUpdate(0, Atomic8Test::addInt17)); |
251 |
|
|
assertEquals(35, a.get(0)); |
252 |
|
|
} |
253 |
|
|
|
254 |
|
|
/** |
255 |
|
|
* AtomicIntegerArray updateAndGet updates with supplied function and |
256 |
|
|
* returns result. |
257 |
|
|
*/ |
258 |
|
|
public void testIntArrayUpdateAndGet() { |
259 |
|
|
AtomicIntegerArray a = new AtomicIntegerArray(1); |
260 |
|
|
a.set(0, 1); |
261 |
|
|
assertEquals(18, a.updateAndGet(0, Atomic8Test::addInt17)); |
262 |
|
|
assertEquals(35, a.updateAndGet(0, Atomic8Test::addInt17)); |
263 |
jsr166 |
1.2 |
assertEquals(35, a.get(0)); |
264 |
dl |
1.1 |
} |
265 |
|
|
|
266 |
|
|
/** |
267 |
|
|
* AtomicIntegerArray getAndAccumulate returns previous value and updates |
268 |
|
|
* with supplied function. |
269 |
|
|
*/ |
270 |
|
|
public void testIntArrayGetAndAccumulate() { |
271 |
|
|
AtomicIntegerArray a = new AtomicIntegerArray(1); |
272 |
|
|
a.set(0, 1); |
273 |
|
|
assertEquals(1, a.getAndAccumulate(0, 2, Integer::sum)); |
274 |
|
|
assertEquals(3, a.getAndAccumulate(0, 3, Integer::sum)); |
275 |
|
|
assertEquals(6, a.get(0)); |
276 |
|
|
} |
277 |
|
|
|
278 |
|
|
/** |
279 |
|
|
* AtomicIntegerArray accumulateAndGet updates with supplied function and |
280 |
|
|
* returns result. |
281 |
|
|
*/ |
282 |
|
|
public void testIntArrayAccumulateAndGet() { |
283 |
|
|
AtomicIntegerArray a = new AtomicIntegerArray(1); |
284 |
|
|
a.set(0, 1); |
285 |
|
|
assertEquals(7, a.accumulateAndGet(0, 6, Integer::sum)); |
286 |
|
|
assertEquals(10, a.accumulateAndGet(0, 3, Integer::sum)); |
287 |
|
|
} |
288 |
|
|
|
289 |
|
|
/** |
290 |
|
|
* AtomicReferenceArray getAndUpdate returns previous value and updates |
291 |
|
|
* result of supplied function |
292 |
|
|
*/ |
293 |
|
|
public void testReferenceArrayGetAndUpdate() { |
294 |
|
|
AtomicReferenceArray<Integer> a = new AtomicReferenceArray<Integer>(1); |
295 |
|
|
a.set(0, one); |
296 |
|
|
assertEquals(new Integer(1), a.getAndUpdate(0, Atomic8Test::addInteger17)); |
297 |
|
|
assertEquals(new Integer(18), a.getAndUpdate(0, Atomic8Test::addInteger17)); |
298 |
|
|
assertEquals(new Integer(35), a.get(0)); |
299 |
|
|
} |
300 |
|
|
|
301 |
|
|
/** |
302 |
|
|
* AtomicReferenceArray updateAndGet updates with supplied function and |
303 |
|
|
* returns result. |
304 |
|
|
*/ |
305 |
|
|
public void testReferenceArrayUpdateAndGet() { |
306 |
|
|
AtomicReferenceArray<Integer> a = new AtomicReferenceArray<Integer>(1); |
307 |
|
|
a.set(0, one); |
308 |
|
|
assertEquals(new Integer(18), a.updateAndGet(0, Atomic8Test::addInteger17)); |
309 |
|
|
assertEquals(new Integer(35), a.updateAndGet(0, Atomic8Test::addInteger17)); |
310 |
|
|
} |
311 |
|
|
|
312 |
|
|
/** |
313 |
|
|
* AtomicReferenceArray getAndAccumulate returns previous value and updates |
314 |
|
|
* with supplied function. |
315 |
|
|
*/ |
316 |
|
|
public void testReferenceArrayGetAndAccumulate() { |
317 |
|
|
AtomicReferenceArray<Integer> a = new AtomicReferenceArray<Integer>(1); |
318 |
|
|
a.set(0, one); |
319 |
|
|
assertEquals(new Integer(1), a.getAndAccumulate(0, 2, Atomic8Test::sumInteger)); |
320 |
|
|
assertEquals(new Integer(3), a.getAndAccumulate(0, 3, Atomic8Test::sumInteger)); |
321 |
|
|
assertEquals(new Integer(6), a.get(0)); |
322 |
|
|
} |
323 |
|
|
|
324 |
|
|
/** |
325 |
|
|
* AtomicReferenceArray accumulateAndGet updates with supplied function and |
326 |
|
|
* returns result. |
327 |
|
|
*/ |
328 |
|
|
public void testReferenceArrayAccumulateAndGet() { |
329 |
|
|
AtomicReferenceArray<Integer> a = new AtomicReferenceArray<Integer>(1); |
330 |
|
|
a.set(0, one); |
331 |
|
|
assertEquals(new Integer(7), a.accumulateAndGet(0, 6, Atomic8Test::sumInteger)); |
332 |
|
|
assertEquals(new Integer(10), a.accumulateAndGet(0, 3, Atomic8Test::sumInteger)); |
333 |
|
|
} |
334 |
|
|
|
335 |
|
|
/** |
336 |
|
|
* AtomicLongFieldUpdater getAndUpdate returns previous value and updates |
337 |
|
|
* result of supplied function |
338 |
|
|
*/ |
339 |
|
|
public void testLongFieldUpdaterGetAndUpdate() { |
340 |
jsr166 |
1.2 |
AtomicLongFieldUpdater a = aLongFieldUpdater(); |
341 |
dl |
1.1 |
a.set(this, 1); |
342 |
|
|
assertEquals(1L, a.getAndUpdate(this, Atomic8Test::addLong17)); |
343 |
|
|
assertEquals(18L, a.getAndUpdate(this, Atomic8Test::addLong17)); |
344 |
|
|
assertEquals(35L, a.get(this)); |
345 |
jsr166 |
1.2 |
assertEquals(35L, aLongField); |
346 |
dl |
1.1 |
} |
347 |
|
|
|
348 |
|
|
/** |
349 |
|
|
* AtomicLongFieldUpdater updateAndGet updates with supplied function and |
350 |
|
|
* returns result. |
351 |
|
|
*/ |
352 |
|
|
public void testLongFieldUpdaterUpdateAndGet() { |
353 |
jsr166 |
1.2 |
AtomicLongFieldUpdater a = aLongFieldUpdater(); |
354 |
dl |
1.1 |
a.set(this, 1); |
355 |
|
|
assertEquals(18L, a.updateAndGet(this, Atomic8Test::addLong17)); |
356 |
|
|
assertEquals(35L, a.updateAndGet(this, Atomic8Test::addLong17)); |
357 |
jsr166 |
1.2 |
assertEquals(35L, a.get(this)); |
358 |
|
|
assertEquals(35L, aLongField); |
359 |
dl |
1.1 |
} |
360 |
|
|
|
361 |
|
|
/** |
362 |
|
|
* AtomicLongFieldUpdater getAndAccumulate returns previous value |
363 |
|
|
* and updates with supplied function. |
364 |
|
|
*/ |
365 |
|
|
public void testLongFieldUpdaterGetAndAccumulate() { |
366 |
jsr166 |
1.2 |
AtomicLongFieldUpdater a = aLongFieldUpdater(); |
367 |
dl |
1.1 |
a.set(this, 1); |
368 |
|
|
assertEquals(1L, a.getAndAccumulate(this, 2L, Long::sum)); |
369 |
|
|
assertEquals(3L, a.getAndAccumulate(this, 3L, Long::sum)); |
370 |
|
|
assertEquals(6L, a.get(this)); |
371 |
jsr166 |
1.2 |
assertEquals(6L, aLongField); |
372 |
dl |
1.1 |
} |
373 |
|
|
|
374 |
|
|
/** |
375 |
|
|
* AtomicLongFieldUpdater accumulateAndGet updates with supplied |
376 |
|
|
* function and returns result. |
377 |
|
|
*/ |
378 |
|
|
public void testLongFieldUpdaterAccumulateAndGet() { |
379 |
jsr166 |
1.2 |
AtomicLongFieldUpdater a = aLongFieldUpdater(); |
380 |
dl |
1.1 |
a.set(this, 1); |
381 |
|
|
assertEquals(7L, a.accumulateAndGet(this, 6L, Long::sum)); |
382 |
|
|
assertEquals(10L, a.accumulateAndGet(this, 3L, Long::sum)); |
383 |
jsr166 |
1.2 |
assertEquals(10L, a.get(this)); |
384 |
|
|
assertEquals(10L, aLongField); |
385 |
dl |
1.1 |
} |
386 |
|
|
|
387 |
|
|
/** |
388 |
|
|
* AtomicIntegerFieldUpdater getAndUpdate returns previous value and updates |
389 |
|
|
* result of supplied function |
390 |
|
|
*/ |
391 |
|
|
public void testIntegerFieldUpdaterGetAndUpdate() { |
392 |
jsr166 |
1.2 |
AtomicIntegerFieldUpdater a = anIntFieldUpdater(); |
393 |
dl |
1.1 |
a.set(this, 1); |
394 |
|
|
assertEquals(1, a.getAndUpdate(this, Atomic8Test::addInt17)); |
395 |
|
|
assertEquals(18, a.getAndUpdate(this, Atomic8Test::addInt17)); |
396 |
|
|
assertEquals(35, a.get(this)); |
397 |
jsr166 |
1.2 |
assertEquals(35, anIntField); |
398 |
dl |
1.1 |
} |
399 |
|
|
|
400 |
|
|
/** |
401 |
|
|
* AtomicIntegerFieldUpdater updateAndGet updates with supplied function and |
402 |
|
|
* returns result. |
403 |
|
|
*/ |
404 |
|
|
public void testIntegerFieldUpdaterUpdateAndGet() { |
405 |
jsr166 |
1.2 |
AtomicIntegerFieldUpdater a = anIntFieldUpdater(); |
406 |
dl |
1.1 |
a.set(this, 1); |
407 |
|
|
assertEquals(18, a.updateAndGet(this, Atomic8Test::addInt17)); |
408 |
|
|
assertEquals(35, a.updateAndGet(this, Atomic8Test::addInt17)); |
409 |
jsr166 |
1.2 |
assertEquals(35, a.get(this)); |
410 |
|
|
assertEquals(35, anIntField); |
411 |
dl |
1.1 |
} |
412 |
|
|
|
413 |
|
|
/** |
414 |
|
|
* AtomicIntegerFieldUpdater getAndAccumulate returns previous value |
415 |
|
|
* and updates with supplied function. |
416 |
|
|
*/ |
417 |
|
|
public void testIntegerFieldUpdaterGetAndAccumulate() { |
418 |
jsr166 |
1.2 |
AtomicIntegerFieldUpdater a = anIntFieldUpdater(); |
419 |
dl |
1.1 |
a.set(this, 1); |
420 |
|
|
assertEquals(1, a.getAndAccumulate(this, 2, Integer::sum)); |
421 |
|
|
assertEquals(3, a.getAndAccumulate(this, 3, Integer::sum)); |
422 |
|
|
assertEquals(6, a.get(this)); |
423 |
jsr166 |
1.2 |
assertEquals(6, anIntField); |
424 |
dl |
1.1 |
} |
425 |
|
|
|
426 |
|
|
/** |
427 |
|
|
* AtomicIntegerFieldUpdater accumulateAndGet updates with supplied |
428 |
|
|
* function and returns result. |
429 |
|
|
*/ |
430 |
|
|
public void testIntegerFieldUpdaterAccumulateAndGet() { |
431 |
jsr166 |
1.2 |
AtomicIntegerFieldUpdater a = anIntFieldUpdater(); |
432 |
dl |
1.1 |
a.set(this, 1); |
433 |
|
|
assertEquals(7, a.accumulateAndGet(this, 6, Integer::sum)); |
434 |
|
|
assertEquals(10, a.accumulateAndGet(this, 3, Integer::sum)); |
435 |
jsr166 |
1.2 |
assertEquals(10, a.get(this)); |
436 |
|
|
assertEquals(10, anIntField); |
437 |
dl |
1.1 |
} |
438 |
|
|
|
439 |
|
|
/** |
440 |
|
|
* AtomicReferenceFieldUpdater getAndUpdate returns previous value |
441 |
|
|
* and updates result of supplied function |
442 |
|
|
*/ |
443 |
|
|
public void testReferenceFieldUpdaterGetAndUpdate() { |
444 |
jsr166 |
1.2 |
AtomicReferenceFieldUpdater<Atomic8Test,Integer> a = anIntegerFieldUpdater(); |
445 |
dl |
1.1 |
a.set(this, one); |
446 |
|
|
assertEquals(new Integer(1), a.getAndUpdate(this, Atomic8Test::addInteger17)); |
447 |
|
|
assertEquals(new Integer(18), a.getAndUpdate(this, Atomic8Test::addInteger17)); |
448 |
|
|
assertEquals(new Integer(35), a.get(this)); |
449 |
jsr166 |
1.2 |
assertEquals(new Integer(35), anIntegerField); |
450 |
dl |
1.1 |
} |
451 |
|
|
|
452 |
|
|
/** |
453 |
|
|
* AtomicReferenceFieldUpdater updateAndGet updates with supplied |
454 |
|
|
* function and returns result. |
455 |
|
|
*/ |
456 |
|
|
public void testReferenceFieldUpdaterUpdateAndGet() { |
457 |
jsr166 |
1.2 |
AtomicReferenceFieldUpdater<Atomic8Test,Integer> a = anIntegerFieldUpdater(); |
458 |
dl |
1.1 |
a.set(this, one); |
459 |
|
|
assertEquals(new Integer(18), a.updateAndGet(this, Atomic8Test::addInteger17)); |
460 |
|
|
assertEquals(new Integer(35), a.updateAndGet(this, Atomic8Test::addInteger17)); |
461 |
jsr166 |
1.2 |
assertEquals(new Integer(35), a.get(this)); |
462 |
|
|
assertEquals(new Integer(35), anIntegerField); |
463 |
dl |
1.1 |
} |
464 |
|
|
|
465 |
|
|
/** |
466 |
|
|
* AtomicReferenceFieldUpdater returns previous value and updates |
467 |
|
|
* with supplied function. |
468 |
|
|
*/ |
469 |
|
|
public void testReferenceFieldUpdaterGetAndAccumulate() { |
470 |
jsr166 |
1.2 |
AtomicReferenceFieldUpdater<Atomic8Test,Integer> a = anIntegerFieldUpdater(); |
471 |
dl |
1.1 |
a.set(this, one); |
472 |
|
|
assertEquals(new Integer(1), a.getAndAccumulate(this, 2, Atomic8Test::sumInteger)); |
473 |
|
|
assertEquals(new Integer(3), a.getAndAccumulate(this, 3, Atomic8Test::sumInteger)); |
474 |
|
|
assertEquals(new Integer(6), a.get(this)); |
475 |
jsr166 |
1.2 |
assertEquals(new Integer(6), anIntegerField); |
476 |
dl |
1.1 |
} |
477 |
|
|
|
478 |
|
|
/** |
479 |
|
|
* AtomicReferenceFieldUpdater accumulateAndGet updates with |
480 |
|
|
* supplied function and returns result. |
481 |
|
|
*/ |
482 |
|
|
public void testReferenceFieldUpdaterAccumulateAndGet() { |
483 |
jsr166 |
1.2 |
AtomicReferenceFieldUpdater<Atomic8Test,Integer> a = anIntegerFieldUpdater(); |
484 |
dl |
1.1 |
a.set(this, one); |
485 |
|
|
assertEquals(new Integer(7), a.accumulateAndGet(this, 6, Atomic8Test::sumInteger)); |
486 |
|
|
assertEquals(new Integer(10), a.accumulateAndGet(this, 3, Atomic8Test::sumInteger)); |
487 |
jsr166 |
1.2 |
assertEquals(new Integer(10), a.get(this)); |
488 |
|
|
assertEquals(new Integer(10), anIntegerField); |
489 |
dl |
1.1 |
} |
490 |
|
|
|
491 |
|
|
/** |
492 |
jsr166 |
1.2 |
* All Atomic getAndUpdate methods throw NullPointerException on |
493 |
|
|
* null function argument |
494 |
dl |
1.1 |
*/ |
495 |
|
|
public void testGetAndUpdateNPE() { |
496 |
jsr166 |
1.2 |
Runnable[] throwingActions = { |
497 |
|
|
() -> new AtomicLong().getAndUpdate(null), |
498 |
|
|
() -> new AtomicInteger().getAndUpdate(null), |
499 |
|
|
() -> new AtomicReference().getAndUpdate(null), |
500 |
|
|
() -> new AtomicLongArray(1).getAndUpdate(0, null), |
501 |
|
|
() -> new AtomicIntegerArray(1).getAndUpdate(0, null), |
502 |
|
|
() -> new AtomicReferenceArray(1).getAndUpdate(0, null), |
503 |
|
|
() -> aLongFieldUpdater().getAndUpdate(this, null), |
504 |
|
|
() -> anIntFieldUpdater().getAndUpdate(this, null), |
505 |
|
|
() -> anIntegerFieldUpdater().getAndUpdate(this, null), |
506 |
|
|
////() -> aLongFieldUpdater().getAndUpdate(null, Atomic8Test::addLong17), |
507 |
|
|
////() -> anIntFieldUpdater().getAndUpdate(null, Atomic8Test::addInt17), |
508 |
|
|
////() -> anIntegerFieldUpdater().getAndUpdate(null, Atomic8Test::addInteger17), |
509 |
|
|
}; |
510 |
|
|
assertThrows(NullPointerException.class, throwingActions); |
511 |
dl |
1.1 |
} |
512 |
|
|
|
513 |
|
|
/** |
514 |
jsr166 |
1.2 |
* All Atomic updateAndGet methods throw NullPointerException on null function argument |
515 |
|
|
*/ |
516 |
|
|
public void testUpdateAndGetNPE() { |
517 |
|
|
Runnable[] throwingActions = { |
518 |
|
|
() -> new AtomicLong().updateAndGet(null), |
519 |
|
|
() -> new AtomicInteger().updateAndGet(null), |
520 |
|
|
() -> new AtomicReference().updateAndGet(null), |
521 |
|
|
() -> new AtomicLongArray(1).updateAndGet(0, null), |
522 |
|
|
() -> new AtomicIntegerArray(1).updateAndGet(0, null), |
523 |
|
|
() -> new AtomicReferenceArray(1).updateAndGet(0, null), |
524 |
|
|
() -> aLongFieldUpdater().updateAndGet(this, null), |
525 |
|
|
() -> anIntFieldUpdater().updateAndGet(this, null), |
526 |
|
|
() -> anIntegerFieldUpdater().updateAndGet(this, null), |
527 |
|
|
}; |
528 |
|
|
assertThrows(NullPointerException.class, throwingActions); |
529 |
|
|
} |
530 |
|
|
|
531 |
|
|
/** |
532 |
|
|
* All Atomic getAndAccumulate methods throw NullPointerException |
533 |
|
|
* on null function argument |
534 |
dl |
1.1 |
*/ |
535 |
|
|
public void testGetAndAccumulateNPE() { |
536 |
jsr166 |
1.2 |
Runnable[] throwingActions = { |
537 |
|
|
() -> new AtomicLong().getAndAccumulate(1L, null), |
538 |
|
|
() -> new AtomicInteger().getAndAccumulate(1, null), |
539 |
|
|
() -> new AtomicReference().getAndAccumulate(one, null), |
540 |
|
|
() -> new AtomicLongArray(1).getAndAccumulate(0, 1L, null), |
541 |
|
|
() -> new AtomicIntegerArray(1).getAndAccumulate(0, 1, null), |
542 |
|
|
() -> new AtomicReferenceArray(1).getAndAccumulate(0, one, null), |
543 |
|
|
() -> aLongFieldUpdater().getAndAccumulate(this, 1L, null), |
544 |
|
|
() -> anIntFieldUpdater().getAndAccumulate(this, 1, null), |
545 |
|
|
() -> anIntegerFieldUpdater().getAndAccumulate(this, one, null), |
546 |
|
|
}; |
547 |
|
|
assertThrows(NullPointerException.class, throwingActions); |
548 |
dl |
1.1 |
} |
549 |
|
|
|
550 |
|
|
/** |
551 |
jsr166 |
1.2 |
* All Atomic accumulateAndGet methods throw NullPointerException |
552 |
|
|
* on null function argument |
553 |
dl |
1.1 |
*/ |
554 |
|
|
public void testAccumulateAndGetNPE() { |
555 |
jsr166 |
1.2 |
Runnable[] throwingActions = { |
556 |
|
|
() -> new AtomicLong().accumulateAndGet(1L, null), |
557 |
|
|
() -> new AtomicInteger().accumulateAndGet(1, null), |
558 |
|
|
() -> new AtomicReference().accumulateAndGet(one, null), |
559 |
|
|
() -> new AtomicLongArray(1).accumulateAndGet(0, 1L, null), |
560 |
|
|
() -> new AtomicIntegerArray(1).accumulateAndGet(0, 1, null), |
561 |
|
|
() -> new AtomicReferenceArray(1).accumulateAndGet(0, one, null), |
562 |
|
|
() -> aLongFieldUpdater().accumulateAndGet(this, 1L, null), |
563 |
|
|
() -> anIntFieldUpdater().accumulateAndGet(this, 1, null), |
564 |
|
|
() -> anIntegerFieldUpdater().accumulateAndGet(this, one, null), |
565 |
|
|
}; |
566 |
|
|
assertThrows(NullPointerException.class, throwingActions); |
567 |
dl |
1.1 |
} |
568 |
|
|
|
569 |
|
|
} |