18 |
|
import junit.framework.Test; |
19 |
|
import junit.framework.TestSuite; |
20 |
|
|
21 |
+ |
/** |
22 |
+ |
* Tests of atomic class methods accepting lambdas introduced in JDK8. |
23 |
+ |
*/ |
24 |
|
public class Atomic8Test extends JSR166TestCase { |
25 |
|
|
26 |
|
public static void main(String[] args) { |
30 |
|
return new TestSuite(Atomic8Test.class); |
31 |
|
} |
32 |
|
|
30 |
– |
/* |
31 |
– |
* Tests of atomic class methods accepting lambdas |
32 |
– |
* introduced in JDK8. |
33 |
– |
*/ |
34 |
– |
|
33 |
|
static long addLong17(long x) { return x + 17; } |
34 |
|
static int addInt17(int x) { return x + 17; } |
35 |
< |
static Integer addInteger17(Integer x) { |
36 |
< |
return new Integer(x.intValue() + 17); |
35 |
> |
static Item addItem17(Item x) { |
36 |
> |
return new Item(x.intValue() + 17); |
37 |
|
} |
38 |
< |
static Integer sumInteger(Integer x, Integer y) { |
39 |
< |
return new Integer(x.intValue() + y.intValue()); |
38 |
> |
static Item sumItem(Item x, Item y) { |
39 |
> |
return new Item(x.intValue() + y.intValue()); |
40 |
|
} |
41 |
|
|
42 |
|
volatile long aLongField; |
43 |
|
volatile int anIntField; |
44 |
< |
volatile Integer anIntegerField; |
44 |
> |
volatile Item anItemField; |
45 |
|
|
46 |
< |
AtomicLongFieldUpdater aLongFieldUpdater() { |
46 |
> |
AtomicLongFieldUpdater<Atomic8Test> aLongFieldUpdater() { |
47 |
|
return AtomicLongFieldUpdater.newUpdater |
48 |
|
(Atomic8Test.class, "aLongField"); |
49 |
|
} |
50 |
|
|
51 |
< |
AtomicIntegerFieldUpdater anIntFieldUpdater() { |
51 |
> |
AtomicIntegerFieldUpdater<Atomic8Test> anIntFieldUpdater() { |
52 |
|
return AtomicIntegerFieldUpdater.newUpdater |
53 |
|
(Atomic8Test.class, "anIntField"); |
54 |
|
} |
55 |
|
|
56 |
< |
AtomicReferenceFieldUpdater<Atomic8Test,Integer> anIntegerFieldUpdater() { |
56 |
> |
AtomicReferenceFieldUpdater<Atomic8Test,Item> anItemFieldUpdater() { |
57 |
|
return AtomicReferenceFieldUpdater.newUpdater |
58 |
< |
(Atomic8Test.class, Integer.class, "anIntegerField"); |
58 |
> |
(Atomic8Test.class, Item.class, "anItemField"); |
59 |
|
} |
60 |
|
|
61 |
|
/** |
64 |
|
*/ |
65 |
|
public void testLongGetAndUpdate() { |
66 |
|
AtomicLong a = new AtomicLong(1L); |
67 |
< |
assertEquals(1L, a.getAndUpdate(Atomic8Test::addLong17)); |
68 |
< |
assertEquals(18L, a.getAndUpdate(Atomic8Test::addLong17)); |
69 |
< |
assertEquals(35L, a.get()); |
67 |
> |
mustEqual(1L, a.getAndUpdate(Atomic8Test::addLong17)); |
68 |
> |
mustEqual(18L, a.getAndUpdate(Atomic8Test::addLong17)); |
69 |
> |
mustEqual(35L, a.get()); |
70 |
|
} |
71 |
|
|
72 |
|
/** |
75 |
|
*/ |
76 |
|
public void testLongUpdateAndGet() { |
77 |
|
AtomicLong a = new AtomicLong(1L); |
78 |
< |
assertEquals(18L, a.updateAndGet(Atomic8Test::addLong17)); |
79 |
< |
assertEquals(35L, a.updateAndGet(Atomic8Test::addLong17)); |
78 |
> |
mustEqual(18L, a.updateAndGet(Atomic8Test::addLong17)); |
79 |
> |
mustEqual(35L, a.updateAndGet(Atomic8Test::addLong17)); |
80 |
|
} |
81 |
|
|
82 |
|
/** |
85 |
|
*/ |
86 |
|
public void testLongGetAndAccumulate() { |
87 |
|
AtomicLong a = new AtomicLong(1L); |
88 |
< |
assertEquals(1L, a.getAndAccumulate(2L, Long::sum)); |
89 |
< |
assertEquals(3L, a.getAndAccumulate(3L, Long::sum)); |
90 |
< |
assertEquals(6L, a.get()); |
88 |
> |
mustEqual(1L, a.getAndAccumulate(2L, Long::sum)); |
89 |
> |
mustEqual(3L, a.getAndAccumulate(3L, Long::sum)); |
90 |
> |
mustEqual(6L, a.get()); |
91 |
|
} |
92 |
|
|
93 |
|
/** |
96 |
|
*/ |
97 |
|
public void testLongAccumulateAndGet() { |
98 |
|
AtomicLong a = new AtomicLong(1L); |
99 |
< |
assertEquals(7L, a.accumulateAndGet(6L, Long::sum)); |
100 |
< |
assertEquals(10L, a.accumulateAndGet(3L, Long::sum)); |
101 |
< |
assertEquals(10L, a.get()); |
99 |
> |
mustEqual(7L, a.accumulateAndGet(6L, Long::sum)); |
100 |
> |
mustEqual(10L, a.accumulateAndGet(3L, Long::sum)); |
101 |
> |
mustEqual(10L, a.get()); |
102 |
|
} |
103 |
|
|
104 |
|
/** |
107 |
|
*/ |
108 |
|
public void testIntGetAndUpdate() { |
109 |
|
AtomicInteger a = new AtomicInteger(1); |
110 |
< |
assertEquals(1, a.getAndUpdate(Atomic8Test::addInt17)); |
111 |
< |
assertEquals(18, a.getAndUpdate(Atomic8Test::addInt17)); |
112 |
< |
assertEquals(35, a.get()); |
110 |
> |
mustEqual(1, a.getAndUpdate(Atomic8Test::addInt17)); |
111 |
> |
mustEqual(18, a.getAndUpdate(Atomic8Test::addInt17)); |
112 |
> |
mustEqual(35, a.get()); |
113 |
|
} |
114 |
|
|
115 |
|
/** |
118 |
|
*/ |
119 |
|
public void testIntUpdateAndGet() { |
120 |
|
AtomicInteger a = new AtomicInteger(1); |
121 |
< |
assertEquals(18, a.updateAndGet(Atomic8Test::addInt17)); |
122 |
< |
assertEquals(35, a.updateAndGet(Atomic8Test::addInt17)); |
123 |
< |
assertEquals(35, a.get()); |
121 |
> |
mustEqual(18, a.updateAndGet(Atomic8Test::addInt17)); |
122 |
> |
mustEqual(35, a.updateAndGet(Atomic8Test::addInt17)); |
123 |
> |
mustEqual(35, a.get()); |
124 |
|
} |
125 |
|
|
126 |
|
/** |
129 |
|
*/ |
130 |
|
public void testIntGetAndAccumulate() { |
131 |
|
AtomicInteger a = new AtomicInteger(1); |
132 |
< |
assertEquals(1, a.getAndAccumulate(2, Integer::sum)); |
133 |
< |
assertEquals(3, a.getAndAccumulate(3, Integer::sum)); |
134 |
< |
assertEquals(6, a.get()); |
132 |
> |
mustEqual(1, a.getAndAccumulate(2, Integer::sum)); |
133 |
> |
mustEqual(3, a.getAndAccumulate(3, Integer::sum)); |
134 |
> |
mustEqual(6, a.get()); |
135 |
|
} |
136 |
|
|
137 |
|
/** |
140 |
|
*/ |
141 |
|
public void testIntAccumulateAndGet() { |
142 |
|
AtomicInteger a = new AtomicInteger(1); |
143 |
< |
assertEquals(7, a.accumulateAndGet(6, Integer::sum)); |
144 |
< |
assertEquals(10, a.accumulateAndGet(3, Integer::sum)); |
145 |
< |
assertEquals(10, a.get()); |
143 |
> |
mustEqual(7, a.accumulateAndGet(6, Integer::sum)); |
144 |
> |
mustEqual(10, a.accumulateAndGet(3, Integer::sum)); |
145 |
> |
mustEqual(10, a.get()); |
146 |
|
} |
147 |
|
|
148 |
|
/** |
150 |
|
* result of supplied function |
151 |
|
*/ |
152 |
|
public void testReferenceGetAndUpdate() { |
153 |
< |
AtomicReference<Integer> a = new AtomicReference<Integer>(one); |
154 |
< |
assertEquals(new Integer(1), a.getAndUpdate(Atomic8Test::addInteger17)); |
155 |
< |
assertEquals(new Integer(18), a.getAndUpdate(Atomic8Test::addInteger17)); |
156 |
< |
assertEquals(new Integer(35), a.get()); |
153 |
> |
AtomicReference<Item> a = new AtomicReference<>(one); |
154 |
> |
mustEqual(1, a.getAndUpdate(Atomic8Test::addItem17)); |
155 |
> |
mustEqual(18, a.getAndUpdate(Atomic8Test::addItem17)); |
156 |
> |
mustEqual(35, a.get()); |
157 |
|
} |
158 |
|
|
159 |
|
/** |
161 |
|
* returns result. |
162 |
|
*/ |
163 |
|
public void testReferenceUpdateAndGet() { |
164 |
< |
AtomicReference<Integer> a = new AtomicReference<Integer>(one); |
165 |
< |
assertEquals(new Integer(18), a.updateAndGet(Atomic8Test::addInteger17)); |
166 |
< |
assertEquals(new Integer(35), a.updateAndGet(Atomic8Test::addInteger17)); |
167 |
< |
assertEquals(new Integer(35), a.get()); |
164 |
> |
AtomicReference<Item> a = new AtomicReference<>(one); |
165 |
> |
mustEqual(18, a.updateAndGet(Atomic8Test::addItem17)); |
166 |
> |
mustEqual(35, a.updateAndGet(Atomic8Test::addItem17)); |
167 |
> |
mustEqual(35, a.get()); |
168 |
|
} |
169 |
|
|
170 |
|
/** |
172 |
|
* with supplied function. |
173 |
|
*/ |
174 |
|
public void testReferenceGetAndAccumulate() { |
175 |
< |
AtomicReference<Integer> a = new AtomicReference<Integer>(one); |
176 |
< |
assertEquals(new Integer(1), a.getAndAccumulate(2, Atomic8Test::sumInteger)); |
177 |
< |
assertEquals(new Integer(3), a.getAndAccumulate(3, Atomic8Test::sumInteger)); |
178 |
< |
assertEquals(new Integer(6), a.get()); |
175 |
> |
AtomicReference<Item> a = new AtomicReference<>(one); |
176 |
> |
mustEqual( 1, a.getAndAccumulate(two, Atomic8Test::sumItem)); |
177 |
> |
mustEqual( 3, a.getAndAccumulate(three, Atomic8Test::sumItem)); |
178 |
> |
mustEqual( 6, a.get()); |
179 |
|
} |
180 |
|
|
181 |
|
/** |
183 |
|
* returns result. |
184 |
|
*/ |
185 |
|
public void testReferenceAccumulateAndGet() { |
186 |
< |
AtomicReference<Integer> a = new AtomicReference<Integer>(one); |
187 |
< |
assertEquals(new Integer(7), a.accumulateAndGet(6, Atomic8Test::sumInteger)); |
188 |
< |
assertEquals(new Integer(10), a.accumulateAndGet(3, Atomic8Test::sumInteger)); |
189 |
< |
assertEquals(new Integer(10), a.get()); |
186 |
> |
AtomicReference<Item> a = new AtomicReference<>(one); |
187 |
> |
mustEqual( 7, a.accumulateAndGet(six, Atomic8Test::sumItem)); |
188 |
> |
mustEqual( 10, a.accumulateAndGet(three, Atomic8Test::sumItem)); |
189 |
> |
mustEqual( 10, a.get()); |
190 |
|
} |
191 |
|
|
192 |
|
/** |
196 |
|
public void testLongArrayGetAndUpdate() { |
197 |
|
AtomicLongArray a = new AtomicLongArray(1); |
198 |
|
a.set(0, 1); |
199 |
< |
assertEquals(1L, a.getAndUpdate(0, Atomic8Test::addLong17)); |
200 |
< |
assertEquals(18L, a.getAndUpdate(0, Atomic8Test::addLong17)); |
201 |
< |
assertEquals(35L, a.get(0)); |
199 |
> |
mustEqual(1L, a.getAndUpdate(0, Atomic8Test::addLong17)); |
200 |
> |
mustEqual(18L, a.getAndUpdate(0, Atomic8Test::addLong17)); |
201 |
> |
mustEqual(35L, a.get(0)); |
202 |
|
} |
203 |
|
|
204 |
|
/** |
208 |
|
public void testLongArrayUpdateAndGet() { |
209 |
|
AtomicLongArray a = new AtomicLongArray(1); |
210 |
|
a.set(0, 1); |
211 |
< |
assertEquals(18L, a.updateAndGet(0, Atomic8Test::addLong17)); |
212 |
< |
assertEquals(35L, a.updateAndGet(0, Atomic8Test::addLong17)); |
213 |
< |
assertEquals(35L, a.get(0)); |
211 |
> |
mustEqual(18L, a.updateAndGet(0, Atomic8Test::addLong17)); |
212 |
> |
mustEqual(35L, a.updateAndGet(0, Atomic8Test::addLong17)); |
213 |
> |
mustEqual(35L, a.get(0)); |
214 |
|
} |
215 |
|
|
216 |
|
/** |
220 |
|
public void testLongArrayGetAndAccumulate() { |
221 |
|
AtomicLongArray a = new AtomicLongArray(1); |
222 |
|
a.set(0, 1); |
223 |
< |
assertEquals(1L, a.getAndAccumulate(0, 2L, Long::sum)); |
224 |
< |
assertEquals(3L, a.getAndAccumulate(0, 3L, Long::sum)); |
225 |
< |
assertEquals(6L, a.get(0)); |
223 |
> |
mustEqual(1L, a.getAndAccumulate(0, 2L, Long::sum)); |
224 |
> |
mustEqual(3L, a.getAndAccumulate(0, 3L, Long::sum)); |
225 |
> |
mustEqual(6L, a.get(0)); |
226 |
|
} |
227 |
|
|
228 |
|
/** |
232 |
|
public void testLongArrayAccumulateAndGet() { |
233 |
|
AtomicLongArray a = new AtomicLongArray(1); |
234 |
|
a.set(0, 1); |
235 |
< |
assertEquals(7L, a.accumulateAndGet(0, 6L, Long::sum)); |
236 |
< |
assertEquals(10L, a.accumulateAndGet(0, 3L, Long::sum)); |
237 |
< |
assertEquals(10L, a.get(0)); |
235 |
> |
mustEqual(7L, a.accumulateAndGet(0, 6L, Long::sum)); |
236 |
> |
mustEqual(10L, a.accumulateAndGet(0, 3L, Long::sum)); |
237 |
> |
mustEqual(10L, a.get(0)); |
238 |
|
} |
239 |
|
|
240 |
|
/** |
244 |
|
public void testIntArrayGetAndUpdate() { |
245 |
|
AtomicIntegerArray a = new AtomicIntegerArray(1); |
246 |
|
a.set(0, 1); |
247 |
< |
assertEquals(1, a.getAndUpdate(0, Atomic8Test::addInt17)); |
248 |
< |
assertEquals(18, a.getAndUpdate(0, Atomic8Test::addInt17)); |
249 |
< |
assertEquals(35, a.get(0)); |
247 |
> |
mustEqual(1, a.getAndUpdate(0, Atomic8Test::addInt17)); |
248 |
> |
mustEqual(18, a.getAndUpdate(0, Atomic8Test::addInt17)); |
249 |
> |
mustEqual(35, a.get(0)); |
250 |
|
} |
251 |
|
|
252 |
|
/** |
256 |
|
public void testIntArrayUpdateAndGet() { |
257 |
|
AtomicIntegerArray a = new AtomicIntegerArray(1); |
258 |
|
a.set(0, 1); |
259 |
< |
assertEquals(18, a.updateAndGet(0, Atomic8Test::addInt17)); |
260 |
< |
assertEquals(35, a.updateAndGet(0, Atomic8Test::addInt17)); |
261 |
< |
assertEquals(35, a.get(0)); |
259 |
> |
mustEqual(18, a.updateAndGet(0, Atomic8Test::addInt17)); |
260 |
> |
mustEqual(35, a.updateAndGet(0, Atomic8Test::addInt17)); |
261 |
> |
mustEqual(35, a.get(0)); |
262 |
|
} |
263 |
|
|
264 |
|
/** |
268 |
|
public void testIntArrayGetAndAccumulate() { |
269 |
|
AtomicIntegerArray a = new AtomicIntegerArray(1); |
270 |
|
a.set(0, 1); |
271 |
< |
assertEquals(1, a.getAndAccumulate(0, 2, Integer::sum)); |
272 |
< |
assertEquals(3, a.getAndAccumulate(0, 3, Integer::sum)); |
273 |
< |
assertEquals(6, a.get(0)); |
271 |
> |
mustEqual(1, a.getAndAccumulate(0, 2, Integer::sum)); |
272 |
> |
mustEqual(3, a.getAndAccumulate(0, 3, Integer::sum)); |
273 |
> |
mustEqual(6, a.get(0)); |
274 |
|
} |
275 |
|
|
276 |
|
/** |
280 |
|
public void testIntArrayAccumulateAndGet() { |
281 |
|
AtomicIntegerArray a = new AtomicIntegerArray(1); |
282 |
|
a.set(0, 1); |
283 |
< |
assertEquals(7, a.accumulateAndGet(0, 6, Integer::sum)); |
284 |
< |
assertEquals(10, a.accumulateAndGet(0, 3, Integer::sum)); |
283 |
> |
mustEqual(7, a.accumulateAndGet(0, 6, Integer::sum)); |
284 |
> |
mustEqual(10, a.accumulateAndGet(0, 3, Integer::sum)); |
285 |
|
} |
286 |
|
|
287 |
|
/** |
289 |
|
* result of supplied function |
290 |
|
*/ |
291 |
|
public void testReferenceArrayGetAndUpdate() { |
292 |
< |
AtomicReferenceArray<Integer> a = new AtomicReferenceArray<Integer>(1); |
292 |
> |
AtomicReferenceArray<Item> a = new AtomicReferenceArray<>(1); |
293 |
|
a.set(0, one); |
294 |
< |
assertEquals(new Integer(1), a.getAndUpdate(0, Atomic8Test::addInteger17)); |
295 |
< |
assertEquals(new Integer(18), a.getAndUpdate(0, Atomic8Test::addInteger17)); |
296 |
< |
assertEquals(new Integer(35), a.get(0)); |
294 |
> |
mustEqual( 1, a.getAndUpdate(0, Atomic8Test::addItem17)); |
295 |
> |
mustEqual( 18, a.getAndUpdate(0, Atomic8Test::addItem17)); |
296 |
> |
mustEqual( 35, a.get(0)); |
297 |
|
} |
298 |
|
|
299 |
|
/** |
301 |
|
* returns result. |
302 |
|
*/ |
303 |
|
public void testReferenceArrayUpdateAndGet() { |
304 |
< |
AtomicReferenceArray<Integer> a = new AtomicReferenceArray<Integer>(1); |
304 |
> |
AtomicReferenceArray<Item> a = new AtomicReferenceArray<>(1); |
305 |
|
a.set(0, one); |
306 |
< |
assertEquals(new Integer(18), a.updateAndGet(0, Atomic8Test::addInteger17)); |
307 |
< |
assertEquals(new Integer(35), a.updateAndGet(0, Atomic8Test::addInteger17)); |
306 |
> |
mustEqual( 18, a.updateAndGet(0, Atomic8Test::addItem17)); |
307 |
> |
mustEqual( 35, a.updateAndGet(0, Atomic8Test::addItem17)); |
308 |
|
} |
309 |
|
|
310 |
|
/** |
312 |
|
* with supplied function. |
313 |
|
*/ |
314 |
|
public void testReferenceArrayGetAndAccumulate() { |
315 |
< |
AtomicReferenceArray<Integer> a = new AtomicReferenceArray<Integer>(1); |
315 |
> |
AtomicReferenceArray<Item> a = new AtomicReferenceArray<>(1); |
316 |
|
a.set(0, one); |
317 |
< |
assertEquals(new Integer(1), a.getAndAccumulate(0, 2, Atomic8Test::sumInteger)); |
318 |
< |
assertEquals(new Integer(3), a.getAndAccumulate(0, 3, Atomic8Test::sumInteger)); |
319 |
< |
assertEquals(new Integer(6), a.get(0)); |
317 |
> |
mustEqual( 1, a.getAndAccumulate(0, two, Atomic8Test::sumItem)); |
318 |
> |
mustEqual( 3, a.getAndAccumulate(0, three, Atomic8Test::sumItem)); |
319 |
> |
mustEqual( 6, a.get(0)); |
320 |
|
} |
321 |
|
|
322 |
|
/** |
324 |
|
* returns result. |
325 |
|
*/ |
326 |
|
public void testReferenceArrayAccumulateAndGet() { |
327 |
< |
AtomicReferenceArray<Integer> a = new AtomicReferenceArray<Integer>(1); |
327 |
> |
AtomicReferenceArray<Item> a = new AtomicReferenceArray<>(1); |
328 |
|
a.set(0, one); |
329 |
< |
assertEquals(new Integer(7), a.accumulateAndGet(0, 6, Atomic8Test::sumInteger)); |
330 |
< |
assertEquals(new Integer(10), a.accumulateAndGet(0, 3, Atomic8Test::sumInteger)); |
329 |
> |
mustEqual( 7, a.accumulateAndGet(0, six, Atomic8Test::sumItem)); |
330 |
> |
mustEqual( 10, a.accumulateAndGet(0, three, Atomic8Test::sumItem)); |
331 |
|
} |
332 |
|
|
333 |
|
/** |
335 |
|
* result of supplied function |
336 |
|
*/ |
337 |
|
public void testLongFieldUpdaterGetAndUpdate() { |
338 |
< |
AtomicLongFieldUpdater a = aLongFieldUpdater(); |
339 |
< |
a.set(this, 1); |
340 |
< |
assertEquals(1L, a.getAndUpdate(this, Atomic8Test::addLong17)); |
341 |
< |
assertEquals(18L, a.getAndUpdate(this, Atomic8Test::addLong17)); |
342 |
< |
assertEquals(35L, a.get(this)); |
343 |
< |
assertEquals(35L, aLongField); |
338 |
> |
AtomicLongFieldUpdater<Atomic8Test> a = aLongFieldUpdater(); |
339 |
> |
a.set(this, 1L); |
340 |
> |
mustEqual(1L, a.getAndUpdate(this, Atomic8Test::addLong17)); |
341 |
> |
mustEqual(18L, a.getAndUpdate(this, Atomic8Test::addLong17)); |
342 |
> |
mustEqual(35L, a.get(this)); |
343 |
> |
mustEqual(35L, aLongField); |
344 |
|
} |
345 |
|
|
346 |
|
/** |
348 |
|
* returns result. |
349 |
|
*/ |
350 |
|
public void testLongFieldUpdaterUpdateAndGet() { |
351 |
< |
AtomicLongFieldUpdater a = aLongFieldUpdater(); |
352 |
< |
a.set(this, 1); |
353 |
< |
assertEquals(18L, a.updateAndGet(this, Atomic8Test::addLong17)); |
354 |
< |
assertEquals(35L, a.updateAndGet(this, Atomic8Test::addLong17)); |
355 |
< |
assertEquals(35L, a.get(this)); |
356 |
< |
assertEquals(35L, aLongField); |
351 |
> |
AtomicLongFieldUpdater<Atomic8Test> a = aLongFieldUpdater(); |
352 |
> |
a.set(this, 1L); |
353 |
> |
mustEqual(18L, a.updateAndGet(this, Atomic8Test::addLong17)); |
354 |
> |
mustEqual(35L, a.updateAndGet(this, Atomic8Test::addLong17)); |
355 |
> |
mustEqual(35L, a.get(this)); |
356 |
> |
mustEqual(35L, aLongField); |
357 |
|
} |
358 |
|
|
359 |
|
/** |
361 |
|
* and updates with supplied function. |
362 |
|
*/ |
363 |
|
public void testLongFieldUpdaterGetAndAccumulate() { |
364 |
< |
AtomicLongFieldUpdater a = aLongFieldUpdater(); |
365 |
< |
a.set(this, 1); |
366 |
< |
assertEquals(1L, a.getAndAccumulate(this, 2L, Long::sum)); |
367 |
< |
assertEquals(3L, a.getAndAccumulate(this, 3L, Long::sum)); |
368 |
< |
assertEquals(6L, a.get(this)); |
369 |
< |
assertEquals(6L, aLongField); |
364 |
> |
AtomicLongFieldUpdater<Atomic8Test> a = aLongFieldUpdater(); |
365 |
> |
a.set(this, 1L); |
366 |
> |
mustEqual(1L, a.getAndAccumulate(this, 2L, Long::sum)); |
367 |
> |
mustEqual(3L, a.getAndAccumulate(this, 3L, Long::sum)); |
368 |
> |
mustEqual(6L, a.get(this)); |
369 |
> |
mustEqual(6L, aLongField); |
370 |
|
} |
371 |
|
|
372 |
|
/** |
374 |
|
* function and returns result. |
375 |
|
*/ |
376 |
|
public void testLongFieldUpdaterAccumulateAndGet() { |
377 |
< |
AtomicLongFieldUpdater a = aLongFieldUpdater(); |
378 |
< |
a.set(this, 1); |
379 |
< |
assertEquals(7L, a.accumulateAndGet(this, 6L, Long::sum)); |
380 |
< |
assertEquals(10L, a.accumulateAndGet(this, 3L, Long::sum)); |
381 |
< |
assertEquals(10L, a.get(this)); |
382 |
< |
assertEquals(10L, aLongField); |
377 |
> |
AtomicLongFieldUpdater<Atomic8Test> a = aLongFieldUpdater(); |
378 |
> |
a.set(this, 1L); |
379 |
> |
mustEqual(7L, a.accumulateAndGet(this, 6L, Long::sum)); |
380 |
> |
mustEqual(10L, a.accumulateAndGet(this, 3L, Long::sum)); |
381 |
> |
mustEqual(10L, a.get(this)); |
382 |
> |
mustEqual(10L, aLongField); |
383 |
|
} |
384 |
|
|
385 |
|
/** |
387 |
|
* result of supplied function |
388 |
|
*/ |
389 |
|
public void testIntegerFieldUpdaterGetAndUpdate() { |
390 |
< |
AtomicIntegerFieldUpdater a = anIntFieldUpdater(); |
390 |
> |
AtomicIntegerFieldUpdater<Atomic8Test> a = anIntFieldUpdater(); |
391 |
|
a.set(this, 1); |
392 |
< |
assertEquals(1, a.getAndUpdate(this, Atomic8Test::addInt17)); |
393 |
< |
assertEquals(18, a.getAndUpdate(this, Atomic8Test::addInt17)); |
394 |
< |
assertEquals(35, a.get(this)); |
395 |
< |
assertEquals(35, anIntField); |
392 |
> |
mustEqual(1, a.getAndUpdate(this, Atomic8Test::addInt17)); |
393 |
> |
mustEqual(18, a.getAndUpdate(this, Atomic8Test::addInt17)); |
394 |
> |
mustEqual(35, a.get(this)); |
395 |
> |
mustEqual(35, anIntField); |
396 |
|
} |
397 |
|
|
398 |
|
/** |
400 |
|
* returns result. |
401 |
|
*/ |
402 |
|
public void testIntegerFieldUpdaterUpdateAndGet() { |
403 |
< |
AtomicIntegerFieldUpdater a = anIntFieldUpdater(); |
403 |
> |
AtomicIntegerFieldUpdater<Atomic8Test> a = anIntFieldUpdater(); |
404 |
|
a.set(this, 1); |
405 |
< |
assertEquals(18, a.updateAndGet(this, Atomic8Test::addInt17)); |
406 |
< |
assertEquals(35, a.updateAndGet(this, Atomic8Test::addInt17)); |
407 |
< |
assertEquals(35, a.get(this)); |
408 |
< |
assertEquals(35, anIntField); |
405 |
> |
mustEqual(18, a.updateAndGet(this, Atomic8Test::addInt17)); |
406 |
> |
mustEqual(35, a.updateAndGet(this, Atomic8Test::addInt17)); |
407 |
> |
mustEqual(35, a.get(this)); |
408 |
> |
mustEqual(35, anIntField); |
409 |
|
} |
410 |
|
|
411 |
|
/** |
413 |
|
* and updates with supplied function. |
414 |
|
*/ |
415 |
|
public void testIntegerFieldUpdaterGetAndAccumulate() { |
416 |
< |
AtomicIntegerFieldUpdater a = anIntFieldUpdater(); |
416 |
> |
AtomicIntegerFieldUpdater<Atomic8Test> a = anIntFieldUpdater(); |
417 |
|
a.set(this, 1); |
418 |
< |
assertEquals(1, a.getAndAccumulate(this, 2, Integer::sum)); |
419 |
< |
assertEquals(3, a.getAndAccumulate(this, 3, Integer::sum)); |
420 |
< |
assertEquals(6, a.get(this)); |
421 |
< |
assertEquals(6, anIntField); |
418 |
> |
mustEqual(1, a.getAndAccumulate(this, 2, Integer::sum)); |
419 |
> |
mustEqual(3, a.getAndAccumulate(this, 3, Integer::sum)); |
420 |
> |
mustEqual(6, a.get(this)); |
421 |
> |
mustEqual(6, anIntField); |
422 |
|
} |
423 |
|
|
424 |
|
/** |
426 |
|
* function and returns result. |
427 |
|
*/ |
428 |
|
public void testIntegerFieldUpdaterAccumulateAndGet() { |
429 |
< |
AtomicIntegerFieldUpdater a = anIntFieldUpdater(); |
429 |
> |
AtomicIntegerFieldUpdater<Atomic8Test> a = anIntFieldUpdater(); |
430 |
|
a.set(this, 1); |
431 |
< |
assertEquals(7, a.accumulateAndGet(this, 6, Integer::sum)); |
432 |
< |
assertEquals(10, a.accumulateAndGet(this, 3, Integer::sum)); |
433 |
< |
assertEquals(10, a.get(this)); |
434 |
< |
assertEquals(10, anIntField); |
431 |
> |
mustEqual(7, a.accumulateAndGet(this, 6, Integer::sum)); |
432 |
> |
mustEqual(10, a.accumulateAndGet(this, 3, Integer::sum)); |
433 |
> |
mustEqual(10, a.get(this)); |
434 |
> |
mustEqual(10, anIntField); |
435 |
|
} |
436 |
|
|
437 |
|
/** |
439 |
|
* and updates result of supplied function |
440 |
|
*/ |
441 |
|
public void testReferenceFieldUpdaterGetAndUpdate() { |
442 |
< |
AtomicReferenceFieldUpdater<Atomic8Test,Integer> a = anIntegerFieldUpdater(); |
442 |
> |
AtomicReferenceFieldUpdater<Atomic8Test,Item> a = anItemFieldUpdater(); |
443 |
|
a.set(this, one); |
444 |
< |
assertEquals(new Integer(1), a.getAndUpdate(this, Atomic8Test::addInteger17)); |
445 |
< |
assertEquals(new Integer(18), a.getAndUpdate(this, Atomic8Test::addInteger17)); |
446 |
< |
assertEquals(new Integer(35), a.get(this)); |
447 |
< |
assertEquals(new Integer(35), anIntegerField); |
444 |
> |
mustEqual( 1, a.getAndUpdate(this, Atomic8Test::addItem17)); |
445 |
> |
mustEqual( 18, a.getAndUpdate(this, Atomic8Test::addItem17)); |
446 |
> |
mustEqual( 35, a.get(this)); |
447 |
> |
mustEqual( 35, anItemField); |
448 |
|
} |
449 |
|
|
450 |
|
/** |
452 |
|
* function and returns result. |
453 |
|
*/ |
454 |
|
public void testReferenceFieldUpdaterUpdateAndGet() { |
455 |
< |
AtomicReferenceFieldUpdater<Atomic8Test,Integer> a = anIntegerFieldUpdater(); |
455 |
> |
AtomicReferenceFieldUpdater<Atomic8Test,Item> a = anItemFieldUpdater(); |
456 |
|
a.set(this, one); |
457 |
< |
assertEquals(new Integer(18), a.updateAndGet(this, Atomic8Test::addInteger17)); |
458 |
< |
assertEquals(new Integer(35), a.updateAndGet(this, Atomic8Test::addInteger17)); |
459 |
< |
assertEquals(new Integer(35), a.get(this)); |
460 |
< |
assertEquals(new Integer(35), anIntegerField); |
457 |
> |
mustEqual( 18, a.updateAndGet(this, Atomic8Test::addItem17)); |
458 |
> |
mustEqual( 35, a.updateAndGet(this, Atomic8Test::addItem17)); |
459 |
> |
mustEqual( 35, a.get(this)); |
460 |
> |
mustEqual( 35, anItemField); |
461 |
|
} |
462 |
|
|
463 |
|
/** |
465 |
|
* with supplied function. |
466 |
|
*/ |
467 |
|
public void testReferenceFieldUpdaterGetAndAccumulate() { |
468 |
< |
AtomicReferenceFieldUpdater<Atomic8Test,Integer> a = anIntegerFieldUpdater(); |
468 |
> |
AtomicReferenceFieldUpdater<Atomic8Test,Item> a = anItemFieldUpdater(); |
469 |
|
a.set(this, one); |
470 |
< |
assertEquals(new Integer(1), a.getAndAccumulate(this, 2, Atomic8Test::sumInteger)); |
471 |
< |
assertEquals(new Integer(3), a.getAndAccumulate(this, 3, Atomic8Test::sumInteger)); |
472 |
< |
assertEquals(new Integer(6), a.get(this)); |
473 |
< |
assertEquals(new Integer(6), anIntegerField); |
470 |
> |
mustEqual( 1, a.getAndAccumulate(this, two, Atomic8Test::sumItem)); |
471 |
> |
mustEqual( 3, a.getAndAccumulate(this, three, Atomic8Test::sumItem)); |
472 |
> |
mustEqual( 6, a.get(this)); |
473 |
> |
mustEqual( 6, anItemField); |
474 |
|
} |
475 |
|
|
476 |
|
/** |
478 |
|
* supplied function and returns result. |
479 |
|
*/ |
480 |
|
public void testReferenceFieldUpdaterAccumulateAndGet() { |
481 |
< |
AtomicReferenceFieldUpdater<Atomic8Test,Integer> a = anIntegerFieldUpdater(); |
481 |
> |
AtomicReferenceFieldUpdater<Atomic8Test,Item> a = anItemFieldUpdater(); |
482 |
|
a.set(this, one); |
483 |
< |
assertEquals(new Integer(7), a.accumulateAndGet(this, 6, Atomic8Test::sumInteger)); |
484 |
< |
assertEquals(new Integer(10), a.accumulateAndGet(this, 3, Atomic8Test::sumInteger)); |
485 |
< |
assertEquals(new Integer(10), a.get(this)); |
486 |
< |
assertEquals(new Integer(10), anIntegerField); |
483 |
> |
mustEqual( 7, a.accumulateAndGet(this, six, Atomic8Test::sumItem)); |
484 |
> |
mustEqual( 10, a.accumulateAndGet(this, three, Atomic8Test::sumItem)); |
485 |
> |
mustEqual( 10, a.get(this)); |
486 |
> |
mustEqual( 10, anItemField); |
487 |
|
} |
488 |
|
|
489 |
|
/** |
490 |
|
* All Atomic getAndUpdate methods throw NullPointerException on |
491 |
|
* null function argument |
492 |
|
*/ |
493 |
+ |
@SuppressWarnings("unchecked") |
494 |
|
public void testGetAndUpdateNPE() { |
495 |
< |
Runnable[] throwingActions = { |
495 |
> |
assertThrows( |
496 |
> |
NullPointerException.class, |
497 |
|
() -> new AtomicLong().getAndUpdate(null), |
498 |
|
() -> new AtomicInteger().getAndUpdate(null), |
499 |
|
() -> new AtomicReference().getAndUpdate(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); |
505 |
> |
() -> anItemFieldUpdater().getAndUpdate(this, null)); |
506 |
|
} |
507 |
|
|
508 |
|
/** |
509 |
|
* All Atomic updateAndGet methods throw NullPointerException on null function argument |
510 |
|
*/ |
511 |
+ |
@SuppressWarnings("unchecked") |
512 |
|
public void testUpdateAndGetNPE() { |
513 |
< |
Runnable[] throwingActions = { |
513 |
> |
assertThrows( |
514 |
> |
NullPointerException.class, |
515 |
|
() -> new AtomicLong().updateAndGet(null), |
516 |
|
() -> new AtomicInteger().updateAndGet(null), |
517 |
|
() -> new AtomicReference().updateAndGet(null), |
520 |
|
() -> new AtomicReferenceArray(1).updateAndGet(0, null), |
521 |
|
() -> aLongFieldUpdater().updateAndGet(this, null), |
522 |
|
() -> anIntFieldUpdater().updateAndGet(this, null), |
523 |
< |
() -> anIntegerFieldUpdater().updateAndGet(this, null), |
527 |
< |
}; |
528 |
< |
assertThrows(NullPointerException.class, throwingActions); |
523 |
> |
() -> anItemFieldUpdater().updateAndGet(this, null)); |
524 |
|
} |
525 |
|
|
526 |
|
/** |
527 |
|
* All Atomic getAndAccumulate methods throw NullPointerException |
528 |
|
* on null function argument |
529 |
|
*/ |
530 |
+ |
@SuppressWarnings("unchecked") |
531 |
|
public void testGetAndAccumulateNPE() { |
532 |
< |
Runnable[] throwingActions = { |
532 |
> |
assertThrows( |
533 |
> |
NullPointerException.class, |
534 |
|
() -> new AtomicLong().getAndAccumulate(1L, null), |
535 |
|
() -> new AtomicInteger().getAndAccumulate(1, null), |
536 |
|
() -> new AtomicReference().getAndAccumulate(one, null), |
539 |
|
() -> new AtomicReferenceArray(1).getAndAccumulate(0, one, null), |
540 |
|
() -> aLongFieldUpdater().getAndAccumulate(this, 1L, null), |
541 |
|
() -> anIntFieldUpdater().getAndAccumulate(this, 1, null), |
542 |
< |
() -> anIntegerFieldUpdater().getAndAccumulate(this, one, null), |
546 |
< |
}; |
547 |
< |
assertThrows(NullPointerException.class, throwingActions); |
542 |
> |
() -> anItemFieldUpdater().getAndAccumulate(this, one, null)); |
543 |
|
} |
544 |
|
|
545 |
|
/** |
546 |
|
* All Atomic accumulateAndGet methods throw NullPointerException |
547 |
|
* on null function argument |
548 |
|
*/ |
549 |
+ |
@SuppressWarnings("unchecked") |
550 |
|
public void testAccumulateAndGetNPE() { |
551 |
< |
Runnable[] throwingActions = { |
551 |
> |
assertThrows( |
552 |
> |
NullPointerException.class, |
553 |
|
() -> new AtomicLong().accumulateAndGet(1L, null), |
554 |
|
() -> new AtomicInteger().accumulateAndGet(1, null), |
555 |
|
() -> new AtomicReference().accumulateAndGet(one, null), |
558 |
|
() -> new AtomicReferenceArray(1).accumulateAndGet(0, one, null), |
559 |
|
() -> aLongFieldUpdater().accumulateAndGet(this, 1L, null), |
560 |
|
() -> anIntFieldUpdater().accumulateAndGet(this, 1, null), |
561 |
< |
() -> anIntegerFieldUpdater().accumulateAndGet(this, one, null), |
562 |
< |
}; |
563 |
< |
assertThrows(NullPointerException.class, throwingActions); |
561 |
> |
() -> anItemFieldUpdater().accumulateAndGet(this, one, null)); |
562 |
> |
} |
563 |
> |
|
564 |
> |
/** |
565 |
> |
* Object arguments for parameters of type T that are not |
566 |
> |
* instances of the class passed to the newUpdater call will |
567 |
> |
* result in a ClassCastException being thrown. |
568 |
> |
*/ |
569 |
> |
@SuppressWarnings("unchecked") |
570 |
> |
public void testFieldUpdaters_ClassCastException() { |
571 |
> |
// Use raw types to allow passing wrong object type, provoking CCE |
572 |
> |
final AtomicLongFieldUpdater longUpdater = aLongFieldUpdater(); |
573 |
> |
final AtomicIntegerFieldUpdater intUpdater = anIntFieldUpdater(); |
574 |
> |
final AtomicReferenceFieldUpdater refUpdater = anItemFieldUpdater(); |
575 |
> |
for (Object x : new Object[]{ new Object(), null }) { |
576 |
> |
assertThrows( |
577 |
> |
ClassCastException.class, |
578 |
> |
() -> longUpdater.get(x), |
579 |
> |
() -> intUpdater.get(x), |
580 |
> |
() -> refUpdater.get(x), |
581 |
> |
|
582 |
> |
() -> longUpdater.set(x, 17L), |
583 |
> |
() -> intUpdater.set(x, 17), |
584 |
> |
() -> refUpdater.set(x, new Item(17)), |
585 |
> |
|
586 |
> |
() -> longUpdater.addAndGet(x, 17L), |
587 |
> |
() -> intUpdater.addAndGet(x, 17), |
588 |
> |
|
589 |
> |
() -> longUpdater.getAndUpdate(x, y -> y), |
590 |
> |
() -> intUpdater.getAndUpdate(x, y -> y), |
591 |
> |
() -> refUpdater.getAndUpdate(x, y -> y), |
592 |
> |
|
593 |
> |
() -> longUpdater.compareAndSet(x, 17L, 42L), |
594 |
> |
() -> intUpdater.compareAndSet(x, 17, 42), |
595 |
> |
() -> refUpdater.compareAndSet(x, 17, fortytwo)); |
596 |
> |
} |
597 |
|
} |
598 |
|
|
599 |
|
} |