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