6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
– |
import junit.framework.*; |
9 |
|
import java.util.concurrent.atomic.AtomicLongFieldUpdater; |
10 |
|
|
11 |
+ |
import junit.framework.Test; |
12 |
+ |
import junit.framework.TestSuite; |
13 |
+ |
|
14 |
|
public class AtomicLongFieldUpdaterTest extends JSR166TestCase { |
15 |
|
volatile long x = 0; |
16 |
|
int z; |
17 |
|
long w; |
18 |
|
|
19 |
|
public static void main(String[] args) { |
20 |
< |
junit.textui.TestRunner.run(suite()); |
20 |
> |
main(suite(), args); |
21 |
|
} |
22 |
|
public static Test suite() { |
23 |
|
return new TestSuite(AtomicLongFieldUpdaterTest.class); |
24 |
|
} |
25 |
|
|
26 |
+ |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> updaterFor(String fieldName) { |
27 |
+ |
return AtomicLongFieldUpdater.newUpdater |
28 |
+ |
(AtomicLongFieldUpdaterTest.class, fieldName); |
29 |
+ |
} |
30 |
+ |
|
31 |
|
/** |
32 |
|
* Construction with non-existent field throws RuntimeException |
33 |
|
*/ |
34 |
|
public void testConstructor() { |
35 |
|
try { |
36 |
< |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> |
30 |
< |
a = AtomicLongFieldUpdater.newUpdater |
31 |
< |
(AtomicLongFieldUpdaterTest.class, "y"); |
36 |
> |
updaterFor("y"); |
37 |
|
shouldThrow(); |
38 |
< |
} catch (RuntimeException success) {} |
38 |
> |
} catch (RuntimeException success) { |
39 |
> |
assertNotNull(success.getCause()); |
40 |
> |
} |
41 |
|
} |
42 |
|
|
43 |
|
/** |
44 |
< |
* construction with field not of given type throws RuntimeException |
44 |
> |
* construction with field not of given type throws IllegalArgumentException |
45 |
|
*/ |
46 |
|
public void testConstructor2() { |
47 |
|
try { |
48 |
< |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> |
42 |
< |
a = AtomicLongFieldUpdater.newUpdater |
43 |
< |
(AtomicLongFieldUpdaterTest.class, "z"); |
48 |
> |
updaterFor("z"); |
49 |
|
shouldThrow(); |
50 |
< |
} catch (RuntimeException success) {} |
50 |
> |
} catch (IllegalArgumentException success) {} |
51 |
|
} |
52 |
|
|
53 |
|
/** |
54 |
< |
* construction with non-volatile field throws RuntimeException |
54 |
> |
* construction with non-volatile field throws IllegalArgumentException |
55 |
|
*/ |
56 |
|
public void testConstructor3() { |
57 |
|
try { |
58 |
< |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> |
54 |
< |
a = AtomicLongFieldUpdater.newUpdater |
55 |
< |
(AtomicLongFieldUpdaterTest.class, "w"); |
58 |
> |
updaterFor("w"); |
59 |
|
shouldThrow(); |
60 |
< |
} catch (RuntimeException success) {} |
60 |
> |
} catch (IllegalArgumentException success) {} |
61 |
|
} |
62 |
|
|
63 |
|
/** |
65 |
|
*/ |
66 |
|
public void testGetSet() { |
67 |
|
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a; |
68 |
< |
try { |
66 |
< |
a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
67 |
< |
} catch (RuntimeException ok) { |
68 |
< |
return; |
69 |
< |
} |
68 |
> |
a = updaterFor("x"); |
69 |
|
x = 1; |
70 |
|
assertEquals(1, a.get(this)); |
71 |
|
a.set(this, 2); |
79 |
|
*/ |
80 |
|
public void testGetLazySet() { |
81 |
|
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a; |
82 |
< |
try { |
84 |
< |
a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
85 |
< |
} catch (RuntimeException ok) { |
86 |
< |
return; |
87 |
< |
} |
82 |
> |
a = updaterFor("x"); |
83 |
|
x = 1; |
84 |
|
assertEquals(1, a.get(this)); |
85 |
|
a.lazySet(this, 2); |
93 |
|
*/ |
94 |
|
public void testCompareAndSet() { |
95 |
|
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a; |
96 |
< |
try { |
102 |
< |
a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
103 |
< |
} catch (RuntimeException ok) { |
104 |
< |
return; |
105 |
< |
} |
96 |
> |
a = updaterFor("x"); |
97 |
|
x = 1; |
98 |
|
assertTrue(a.compareAndSet(this, 1, 2)); |
99 |
|
assertTrue(a.compareAndSet(this, 2, -4)); |
110 |
|
*/ |
111 |
|
public void testCompareAndSetInMultipleThreads() throws Exception { |
112 |
|
x = 1; |
113 |
< |
final AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest>a; |
114 |
< |
try { |
124 |
< |
a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
125 |
< |
} catch (RuntimeException ok) { |
126 |
< |
return; |
127 |
< |
} |
113 |
> |
final AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a; |
114 |
> |
a = updaterFor("x"); |
115 |
|
|
116 |
|
Thread t = new Thread(new CheckedRunnable() { |
117 |
|
public void realRun() { |
132 |
|
*/ |
133 |
|
public void testWeakCompareAndSet() { |
134 |
|
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a; |
135 |
< |
try { |
149 |
< |
a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
150 |
< |
} catch (RuntimeException ok) { |
151 |
< |
return; |
152 |
< |
} |
135 |
> |
a = updaterFor("x"); |
136 |
|
x = 1; |
137 |
< |
while (!a.weakCompareAndSet(this, 1, 2)); |
138 |
< |
while (!a.weakCompareAndSet(this, 2, -4)); |
137 |
> |
do {} while (!a.weakCompareAndSet(this, 1, 2)); |
138 |
> |
do {} while (!a.weakCompareAndSet(this, 2, -4)); |
139 |
|
assertEquals(-4, a.get(this)); |
140 |
< |
while (!a.weakCompareAndSet(this, -4, 7)); |
140 |
> |
do {} while (!a.weakCompareAndSet(this, -4, 7)); |
141 |
|
assertEquals(7, a.get(this)); |
142 |
|
} |
143 |
|
|
146 |
|
*/ |
147 |
|
public void testGetAndSet() { |
148 |
|
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a; |
149 |
< |
try { |
167 |
< |
a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
168 |
< |
} catch (RuntimeException ok) { |
169 |
< |
return; |
170 |
< |
} |
149 |
> |
a = updaterFor("x"); |
150 |
|
x = 1; |
151 |
|
assertEquals(1, a.getAndSet(this, 0)); |
152 |
|
assertEquals(0, a.getAndSet(this, -10)); |
158 |
|
*/ |
159 |
|
public void testGetAndAdd() { |
160 |
|
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a; |
161 |
< |
try { |
183 |
< |
a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
184 |
< |
} catch (RuntimeException ok) { |
185 |
< |
return; |
186 |
< |
} |
161 |
> |
a = updaterFor("x"); |
162 |
|
x = 1; |
163 |
|
assertEquals(1, a.getAndAdd(this, 2)); |
164 |
|
assertEquals(3, a.get(this)); |
171 |
|
*/ |
172 |
|
public void testGetAndDecrement() { |
173 |
|
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a; |
174 |
< |
try { |
200 |
< |
a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
201 |
< |
} catch (RuntimeException ok) { |
202 |
< |
return; |
203 |
< |
} |
174 |
> |
a = updaterFor("x"); |
175 |
|
x = 1; |
176 |
|
assertEquals(1, a.getAndDecrement(this)); |
177 |
|
assertEquals(0, a.getAndDecrement(this)); |
183 |
|
*/ |
184 |
|
public void testGetAndIncrement() { |
185 |
|
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a; |
186 |
< |
try { |
216 |
< |
a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
217 |
< |
} catch (RuntimeException ok) { |
218 |
< |
return; |
219 |
< |
} |
186 |
> |
a = updaterFor("x"); |
187 |
|
x = 1; |
188 |
|
assertEquals(1, a.getAndIncrement(this)); |
189 |
|
assertEquals(2, a.get(this)); |
199 |
|
*/ |
200 |
|
public void testAddAndGet() { |
201 |
|
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a; |
202 |
< |
try { |
236 |
< |
a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
237 |
< |
} catch (RuntimeException ok) { |
238 |
< |
return; |
239 |
< |
} |
202 |
> |
a = updaterFor("x"); |
203 |
|
x = 1; |
204 |
|
assertEquals(3, a.addAndGet(this, 2)); |
205 |
|
assertEquals(3, a.get(this)); |
212 |
|
*/ |
213 |
|
public void testDecrementAndGet() { |
214 |
|
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a; |
215 |
< |
try { |
253 |
< |
a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
254 |
< |
} catch (RuntimeException ok) { |
255 |
< |
return; |
256 |
< |
} |
215 |
> |
a = updaterFor("x"); |
216 |
|
x = 1; |
217 |
|
assertEquals(0, a.decrementAndGet(this)); |
218 |
|
assertEquals(-1, a.decrementAndGet(this)); |
225 |
|
*/ |
226 |
|
public void testIncrementAndGet() { |
227 |
|
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a; |
228 |
< |
try { |
270 |
< |
a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
271 |
< |
} catch (RuntimeException ok) { |
272 |
< |
return; |
273 |
< |
} |
228 |
> |
a = updaterFor("x"); |
229 |
|
x = 1; |
230 |
|
assertEquals(2, a.incrementAndGet(this)); |
231 |
|
assertEquals(2, a.get(this)); |