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