21 |
|
return new TestSuite(AtomicLongFieldUpdaterTest.class); |
22 |
|
} |
23 |
|
|
24 |
+ |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> updaterFor(String fieldName) { |
25 |
+ |
return AtomicLongFieldUpdater.newUpdater |
26 |
+ |
(AtomicLongFieldUpdaterTest.class, fieldName); |
27 |
+ |
} |
28 |
+ |
|
29 |
|
/** |
30 |
|
* Construction with non-existent field throws RuntimeException |
31 |
|
*/ |
32 |
|
public void testConstructor() { |
33 |
|
try { |
34 |
< |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> |
30 |
< |
a = AtomicLongFieldUpdater.newUpdater |
31 |
< |
(AtomicLongFieldUpdaterTest.class, "y"); |
34 |
> |
updaterFor("y"); |
35 |
|
shouldThrow(); |
36 |
|
} catch (RuntimeException success) {} |
37 |
|
} |
41 |
|
*/ |
42 |
|
public void testConstructor2() { |
43 |
|
try { |
44 |
< |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> |
42 |
< |
a = AtomicLongFieldUpdater.newUpdater |
43 |
< |
(AtomicLongFieldUpdaterTest.class, "z"); |
44 |
> |
updaterFor("z"); |
45 |
|
shouldThrow(); |
46 |
|
} catch (RuntimeException success) {} |
47 |
|
} |
51 |
|
*/ |
52 |
|
public void testConstructor3() { |
53 |
|
try { |
54 |
< |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> |
54 |
< |
a = AtomicLongFieldUpdater.newUpdater |
55 |
< |
(AtomicLongFieldUpdaterTest.class, "w"); |
54 |
> |
updaterFor("w"); |
55 |
|
shouldThrow(); |
56 |
|
} catch (RuntimeException success) {} |
57 |
|
} |
61 |
|
*/ |
62 |
|
public void testGetSet() { |
63 |
|
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a; |
64 |
< |
try { |
66 |
< |
a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
67 |
< |
} catch (RuntimeException ok) { |
68 |
< |
return; |
69 |
< |
} |
64 |
> |
a = updaterFor("x"); |
65 |
|
x = 1; |
66 |
|
assertEquals(1, a.get(this)); |
67 |
|
a.set(this, 2); |
75 |
|
*/ |
76 |
|
public void testGetLazySet() { |
77 |
|
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a; |
78 |
< |
try { |
84 |
< |
a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
85 |
< |
} catch (RuntimeException ok) { |
86 |
< |
return; |
87 |
< |
} |
78 |
> |
a = updaterFor("x"); |
79 |
|
x = 1; |
80 |
|
assertEquals(1, a.get(this)); |
81 |
|
a.lazySet(this, 2); |
89 |
|
*/ |
90 |
|
public void testCompareAndSet() { |
91 |
|
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a; |
92 |
< |
try { |
102 |
< |
a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
103 |
< |
} catch (RuntimeException ok) { |
104 |
< |
return; |
105 |
< |
} |
92 |
> |
a = updaterFor("x"); |
93 |
|
x = 1; |
94 |
|
assertTrue(a.compareAndSet(this, 1, 2)); |
95 |
|
assertTrue(a.compareAndSet(this, 2, -4)); |
106 |
|
*/ |
107 |
|
public void testCompareAndSetInMultipleThreads() throws Exception { |
108 |
|
x = 1; |
109 |
< |
final AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest>a; |
110 |
< |
try { |
124 |
< |
a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
125 |
< |
} catch (RuntimeException ok) { |
126 |
< |
return; |
127 |
< |
} |
109 |
> |
final AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a; |
110 |
> |
a = updaterFor("x"); |
111 |
|
|
112 |
|
Thread t = new Thread(new CheckedRunnable() { |
113 |
|
public void realRun() { |
128 |
|
*/ |
129 |
|
public void testWeakCompareAndSet() { |
130 |
|
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a; |
131 |
< |
try { |
149 |
< |
a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
150 |
< |
} catch (RuntimeException ok) { |
151 |
< |
return; |
152 |
< |
} |
131 |
> |
a = updaterFor("x"); |
132 |
|
x = 1; |
133 |
|
while (!a.weakCompareAndSet(this, 1, 2)); |
134 |
|
while (!a.weakCompareAndSet(this, 2, -4)); |
142 |
|
*/ |
143 |
|
public void testGetAndSet() { |
144 |
|
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a; |
145 |
< |
try { |
167 |
< |
a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
168 |
< |
} catch (RuntimeException ok) { |
169 |
< |
return; |
170 |
< |
} |
145 |
> |
a = updaterFor("x"); |
146 |
|
x = 1; |
147 |
|
assertEquals(1, a.getAndSet(this, 0)); |
148 |
|
assertEquals(0, a.getAndSet(this, -10)); |
154 |
|
*/ |
155 |
|
public void testGetAndAdd() { |
156 |
|
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a; |
157 |
< |
try { |
183 |
< |
a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
184 |
< |
} catch (RuntimeException ok) { |
185 |
< |
return; |
186 |
< |
} |
157 |
> |
a = updaterFor("x"); |
158 |
|
x = 1; |
159 |
|
assertEquals(1, a.getAndAdd(this, 2)); |
160 |
|
assertEquals(3, a.get(this)); |
167 |
|
*/ |
168 |
|
public void testGetAndDecrement() { |
169 |
|
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a; |
170 |
< |
try { |
200 |
< |
a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
201 |
< |
} catch (RuntimeException ok) { |
202 |
< |
return; |
203 |
< |
} |
170 |
> |
a = updaterFor("x"); |
171 |
|
x = 1; |
172 |
|
assertEquals(1, a.getAndDecrement(this)); |
173 |
|
assertEquals(0, a.getAndDecrement(this)); |
179 |
|
*/ |
180 |
|
public void testGetAndIncrement() { |
181 |
|
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a; |
182 |
< |
try { |
216 |
< |
a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
217 |
< |
} catch (RuntimeException ok) { |
218 |
< |
return; |
219 |
< |
} |
182 |
> |
a = updaterFor("x"); |
183 |
|
x = 1; |
184 |
|
assertEquals(1, a.getAndIncrement(this)); |
185 |
|
assertEquals(2, a.get(this)); |
195 |
|
*/ |
196 |
|
public void testAddAndGet() { |
197 |
|
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a; |
198 |
< |
try { |
236 |
< |
a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
237 |
< |
} catch (RuntimeException ok) { |
238 |
< |
return; |
239 |
< |
} |
198 |
> |
a = updaterFor("x"); |
199 |
|
x = 1; |
200 |
|
assertEquals(3, a.addAndGet(this, 2)); |
201 |
|
assertEquals(3, a.get(this)); |
208 |
|
*/ |
209 |
|
public void testDecrementAndGet() { |
210 |
|
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a; |
211 |
< |
try { |
253 |
< |
a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
254 |
< |
} catch (RuntimeException ok) { |
255 |
< |
return; |
256 |
< |
} |
211 |
> |
a = updaterFor("x"); |
212 |
|
x = 1; |
213 |
|
assertEquals(0, a.decrementAndGet(this)); |
214 |
|
assertEquals(-1, a.decrementAndGet(this)); |
221 |
|
*/ |
222 |
|
public void testIncrementAndGet() { |
223 |
|
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a; |
224 |
< |
try { |
270 |
< |
a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
271 |
< |
} catch (RuntimeException ok) { |
272 |
< |
return; |
273 |
< |
} |
224 |
> |
a = updaterFor("x"); |
225 |
|
x = 1; |
226 |
|
assertEquals(2, a.incrementAndGet(this)); |
227 |
|
assertEquals(2, a.get(this)); |