1 |
|
/* |
2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
< |
* http://creativecommons.org/licenses/publicdomain |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
< |
import java.util.concurrent.atomic.*; |
10 |
< |
import junit.framework.*; |
11 |
< |
import java.util.*; |
9 |
> |
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; |
10 |
> |
|
11 |
> |
import junit.framework.Test; |
12 |
> |
import junit.framework.TestSuite; |
13 |
|
|
14 |
|
public class AtomicIntegerFieldUpdaterTest extends JSR166TestCase { |
15 |
|
volatile int x = 0; |
22 |
|
return new TestSuite(AtomicIntegerFieldUpdaterTest.class); |
23 |
|
} |
24 |
|
|
25 |
+ |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> updaterFor(String fieldName) { |
26 |
+ |
return AtomicIntegerFieldUpdater.newUpdater |
27 |
+ |
(AtomicIntegerFieldUpdaterTest.class, fieldName); |
28 |
+ |
} |
29 |
+ |
|
30 |
|
/** |
31 |
|
* Construction with non-existent field throws RuntimeException |
32 |
|
*/ |
33 |
|
public void testConstructor() { |
34 |
|
try { |
35 |
< |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> |
30 |
< |
a = AtomicIntegerFieldUpdater.newUpdater |
31 |
< |
(AtomicIntegerFieldUpdaterTest.class, "y"); |
35 |
> |
updaterFor("y"); |
36 |
|
shouldThrow(); |
37 |
+ |
} catch (RuntimeException success) { |
38 |
+ |
assertNotNull(success.getCause()); |
39 |
|
} |
34 |
– |
catch (RuntimeException rt) {} |
40 |
|
} |
41 |
|
|
42 |
|
/** |
43 |
< |
* construction with field not of given type throws RuntimeException |
43 |
> |
* construction with field not of given type throws IllegalArgumentException |
44 |
|
*/ |
45 |
|
public void testConstructor2() { |
46 |
|
try { |
47 |
< |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> |
43 |
< |
a = AtomicIntegerFieldUpdater.newUpdater |
44 |
< |
(AtomicIntegerFieldUpdaterTest.class, "z"); |
47 |
> |
updaterFor("z"); |
48 |
|
shouldThrow(); |
49 |
< |
} |
47 |
< |
catch (RuntimeException rt) {} |
49 |
> |
} catch (IllegalArgumentException success) {} |
50 |
|
} |
51 |
|
|
52 |
|
/** |
53 |
< |
* construction with non-volatile field throws RuntimeException |
53 |
> |
* construction with non-volatile field throws IllegalArgumentException |
54 |
|
*/ |
55 |
|
public void testConstructor3() { |
56 |
|
try { |
57 |
< |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> |
56 |
< |
a = AtomicIntegerFieldUpdater.newUpdater |
57 |
< |
(AtomicIntegerFieldUpdaterTest.class, "w"); |
57 |
> |
updaterFor("w"); |
58 |
|
shouldThrow(); |
59 |
< |
} |
60 |
< |
catch (RuntimeException rt) {} |
59 |
> |
} catch (IllegalArgumentException success) {} |
60 |
|
} |
61 |
|
|
62 |
|
/** |
63 |
< |
* get returns the last value set or assigned |
63 |
> |
* get returns the last value set or assigned |
64 |
|
*/ |
65 |
|
public void testGetSet() { |
66 |
|
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a; |
67 |
< |
try { |
69 |
< |
a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
70 |
< |
} catch (RuntimeException ok) { |
71 |
< |
return; |
72 |
< |
} |
67 |
> |
a = updaterFor("x"); |
68 |
|
x = 1; |
69 |
< |
assertEquals(1,a.get(this)); |
70 |
< |
a.set(this,2); |
71 |
< |
assertEquals(2,a.get(this)); |
72 |
< |
a.set(this,-3); |
73 |
< |
assertEquals(-3,a.get(this)); |
79 |
< |
|
69 |
> |
assertEquals(1, a.get(this)); |
70 |
> |
a.set(this, 2); |
71 |
> |
assertEquals(2, a.get(this)); |
72 |
> |
a.set(this, -3); |
73 |
> |
assertEquals(-3, a.get(this)); |
74 |
|
} |
75 |
|
|
76 |
|
/** |
77 |
< |
* get returns the last value lazySet by same thread |
77 |
> |
* get returns the last value lazySet by same thread |
78 |
|
*/ |
79 |
|
public void testGetLazySet() { |
80 |
|
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a; |
81 |
< |
try { |
88 |
< |
a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
89 |
< |
} catch (RuntimeException ok) { |
90 |
< |
return; |
91 |
< |
} |
81 |
> |
a = updaterFor("x"); |
82 |
|
x = 1; |
83 |
< |
assertEquals(1,a.get(this)); |
84 |
< |
a.lazySet(this,2); |
85 |
< |
assertEquals(2,a.get(this)); |
86 |
< |
a.lazySet(this,-3); |
87 |
< |
assertEquals(-3,a.get(this)); |
98 |
< |
|
83 |
> |
assertEquals(1, a.get(this)); |
84 |
> |
a.lazySet(this, 2); |
85 |
> |
assertEquals(2, a.get(this)); |
86 |
> |
a.lazySet(this, -3); |
87 |
> |
assertEquals(-3, a.get(this)); |
88 |
|
} |
89 |
|
|
90 |
|
/** |
92 |
|
*/ |
93 |
|
public void testCompareAndSet() { |
94 |
|
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a; |
95 |
< |
try { |
107 |
< |
a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
108 |
< |
} catch (RuntimeException ok) { |
109 |
< |
return; |
110 |
< |
} |
95 |
> |
a = updaterFor("x"); |
96 |
|
x = 1; |
97 |
< |
assertTrue(a.compareAndSet(this,1,2)); |
98 |
< |
assertTrue(a.compareAndSet(this,2,-4)); |
99 |
< |
assertEquals(-4,a.get(this)); |
100 |
< |
assertFalse(a.compareAndSet(this,-5,7)); |
101 |
< |
assertFalse((7 == a.get(this))); |
102 |
< |
assertTrue(a.compareAndSet(this,-4,7)); |
103 |
< |
assertEquals(7,a.get(this)); |
97 |
> |
assertTrue(a.compareAndSet(this, 1, 2)); |
98 |
> |
assertTrue(a.compareAndSet(this, 2, -4)); |
99 |
> |
assertEquals(-4, a.get(this)); |
100 |
> |
assertFalse(a.compareAndSet(this, -5, 7)); |
101 |
> |
assertEquals(-4, a.get(this)); |
102 |
> |
assertTrue(a.compareAndSet(this, -4, 7)); |
103 |
> |
assertEquals(7, a.get(this)); |
104 |
|
} |
105 |
|
|
121 |
– |
|
106 |
|
/** |
107 |
|
* compareAndSet in one thread enables another waiting for value |
108 |
|
* to succeed |
109 |
|
*/ |
110 |
|
public void testCompareAndSetInMultipleThreads() throws Exception { |
111 |
|
x = 1; |
112 |
< |
final AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest>a; |
113 |
< |
try { |
130 |
< |
a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
131 |
< |
} catch (RuntimeException ok) { |
132 |
< |
return; |
133 |
< |
} |
112 |
> |
final AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a; |
113 |
> |
a = updaterFor("x"); |
114 |
|
|
115 |
|
Thread t = new Thread(new CheckedRunnable() { |
116 |
|
public void realRun() { |
122 |
|
assertTrue(a.compareAndSet(this, 1, 2)); |
123 |
|
t.join(LONG_DELAY_MS); |
124 |
|
assertFalse(t.isAlive()); |
125 |
< |
assertEquals(a.get(this), 3); |
125 |
> |
assertEquals(3, a.get(this)); |
126 |
|
} |
127 |
|
|
128 |
|
/** |
131 |
|
*/ |
132 |
|
public void testWeakCompareAndSet() { |
133 |
|
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a; |
134 |
< |
try { |
155 |
< |
a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
156 |
< |
} catch (RuntimeException ok) { |
157 |
< |
return; |
158 |
< |
} |
134 |
> |
a = updaterFor("x"); |
135 |
|
x = 1; |
136 |
< |
while (!a.weakCompareAndSet(this,1,2)); |
137 |
< |
while (!a.weakCompareAndSet(this,2,-4)); |
138 |
< |
assertEquals(-4,a.get(this)); |
139 |
< |
while (!a.weakCompareAndSet(this,-4,7)); |
140 |
< |
assertEquals(7,a.get(this)); |
136 |
> |
while (!a.weakCompareAndSet(this, 1, 2)); |
137 |
> |
while (!a.weakCompareAndSet(this, 2, -4)); |
138 |
> |
assertEquals(-4, a.get(this)); |
139 |
> |
while (!a.weakCompareAndSet(this, -4, 7)); |
140 |
> |
assertEquals(7, a.get(this)); |
141 |
|
} |
142 |
|
|
143 |
|
/** |
144 |
< |
* getAndSet returns previous value and sets to given value |
144 |
> |
* getAndSet returns previous value and sets to given value |
145 |
|
*/ |
146 |
|
public void testGetAndSet() { |
147 |
|
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a; |
148 |
< |
try { |
173 |
< |
a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
174 |
< |
} catch (RuntimeException ok) { |
175 |
< |
return; |
176 |
< |
} |
148 |
> |
a = updaterFor("x"); |
149 |
|
x = 1; |
150 |
< |
assertEquals(1,a.getAndSet(this, 0)); |
151 |
< |
assertEquals(0,a.getAndSet(this,-10)); |
152 |
< |
assertEquals(-10,a.getAndSet(this,1)); |
150 |
> |
assertEquals(1, a.getAndSet(this, 0)); |
151 |
> |
assertEquals(0, a.getAndSet(this, -10)); |
152 |
> |
assertEquals(-10, a.getAndSet(this, 1)); |
153 |
|
} |
154 |
|
|
155 |
|
/** |
157 |
|
*/ |
158 |
|
public void testGetAndAdd() { |
159 |
|
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a; |
160 |
< |
try { |
189 |
< |
a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
190 |
< |
} catch (RuntimeException ok) { |
191 |
< |
return; |
192 |
< |
} |
160 |
> |
a = updaterFor("x"); |
161 |
|
x = 1; |
162 |
< |
assertEquals(1,a.getAndAdd(this,2)); |
163 |
< |
assertEquals(3,a.get(this)); |
164 |
< |
assertEquals(3,a.getAndAdd(this,-4)); |
165 |
< |
assertEquals(-1,a.get(this)); |
162 |
> |
assertEquals(1, a.getAndAdd(this, 2)); |
163 |
> |
assertEquals(3, a.get(this)); |
164 |
> |
assertEquals(3, a.getAndAdd(this, -4)); |
165 |
> |
assertEquals(-1, a.get(this)); |
166 |
|
} |
167 |
|
|
168 |
|
/** |
170 |
|
*/ |
171 |
|
public void testGetAndDecrement() { |
172 |
|
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a; |
173 |
< |
try { |
206 |
< |
a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
207 |
< |
} catch (RuntimeException ok) { |
208 |
< |
return; |
209 |
< |
} |
173 |
> |
a = updaterFor("x"); |
174 |
|
x = 1; |
175 |
< |
assertEquals(1,a.getAndDecrement(this)); |
176 |
< |
assertEquals(0,a.getAndDecrement(this)); |
177 |
< |
assertEquals(-1,a.getAndDecrement(this)); |
175 |
> |
assertEquals(1, a.getAndDecrement(this)); |
176 |
> |
assertEquals(0, a.getAndDecrement(this)); |
177 |
> |
assertEquals(-1, a.getAndDecrement(this)); |
178 |
|
} |
179 |
|
|
180 |
|
/** |
182 |
|
*/ |
183 |
|
public void testGetAndIncrement() { |
184 |
|
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a; |
185 |
< |
try { |
222 |
< |
a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
223 |
< |
} catch (RuntimeException ok) { |
224 |
< |
return; |
225 |
< |
} |
185 |
> |
a = updaterFor("x"); |
186 |
|
x = 1; |
187 |
< |
assertEquals(1,a.getAndIncrement(this)); |
188 |
< |
assertEquals(2,a.get(this)); |
189 |
< |
a.set(this,-2); |
190 |
< |
assertEquals(-2,a.getAndIncrement(this)); |
191 |
< |
assertEquals(-1,a.getAndIncrement(this)); |
192 |
< |
assertEquals(0,a.getAndIncrement(this)); |
193 |
< |
assertEquals(1,a.get(this)); |
187 |
> |
assertEquals(1, a.getAndIncrement(this)); |
188 |
> |
assertEquals(2, a.get(this)); |
189 |
> |
a.set(this, -2); |
190 |
> |
assertEquals(-2, a.getAndIncrement(this)); |
191 |
> |
assertEquals(-1, a.getAndIncrement(this)); |
192 |
> |
assertEquals(0, a.getAndIncrement(this)); |
193 |
> |
assertEquals(1, a.get(this)); |
194 |
|
} |
195 |
|
|
196 |
|
/** |
198 |
|
*/ |
199 |
|
public void testAddAndGet() { |
200 |
|
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a; |
201 |
< |
try { |
242 |
< |
a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
243 |
< |
} catch (RuntimeException ok) { |
244 |
< |
return; |
245 |
< |
} |
201 |
> |
a = updaterFor("x"); |
202 |
|
x = 1; |
203 |
< |
assertEquals(3,a.addAndGet(this,2)); |
204 |
< |
assertEquals(3,a.get(this)); |
205 |
< |
assertEquals(-1,a.addAndGet(this,-4)); |
206 |
< |
assertEquals(-1,a.get(this)); |
203 |
> |
assertEquals(3, a.addAndGet(this, 2)); |
204 |
> |
assertEquals(3, a.get(this)); |
205 |
> |
assertEquals(-1, a.addAndGet(this, -4)); |
206 |
> |
assertEquals(-1, a.get(this)); |
207 |
|
} |
208 |
|
|
209 |
|
/** |
211 |
|
*/ |
212 |
|
public void testDecrementAndGet() { |
213 |
|
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a; |
214 |
< |
try { |
259 |
< |
a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
260 |
< |
} catch (RuntimeException ok) { |
261 |
< |
return; |
262 |
< |
} |
214 |
> |
a = updaterFor("x"); |
215 |
|
x = 1; |
216 |
< |
assertEquals(0,a.decrementAndGet(this)); |
217 |
< |
assertEquals(-1,a.decrementAndGet(this)); |
218 |
< |
assertEquals(-2,a.decrementAndGet(this)); |
219 |
< |
assertEquals(-2,a.get(this)); |
216 |
> |
assertEquals(0, a.decrementAndGet(this)); |
217 |
> |
assertEquals(-1, a.decrementAndGet(this)); |
218 |
> |
assertEquals(-2, a.decrementAndGet(this)); |
219 |
> |
assertEquals(-2, a.get(this)); |
220 |
|
} |
221 |
|
|
222 |
|
/** |
224 |
|
*/ |
225 |
|
public void testIncrementAndGet() { |
226 |
|
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a; |
227 |
< |
try { |
276 |
< |
a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
277 |
< |
} catch (RuntimeException ok) { |
278 |
< |
return; |
279 |
< |
} |
227 |
> |
a = updaterFor("x"); |
228 |
|
x = 1; |
229 |
< |
assertEquals(2,a.incrementAndGet(this)); |
230 |
< |
assertEquals(2,a.get(this)); |
231 |
< |
a.set(this,-2); |
232 |
< |
assertEquals(-1,a.incrementAndGet(this)); |
233 |
< |
assertEquals(0,a.incrementAndGet(this)); |
234 |
< |
assertEquals(1,a.incrementAndGet(this)); |
235 |
< |
assertEquals(1,a.get(this)); |
229 |
> |
assertEquals(2, a.incrementAndGet(this)); |
230 |
> |
assertEquals(2, a.get(this)); |
231 |
> |
a.set(this, -2); |
232 |
> |
assertEquals(-1, a.incrementAndGet(this)); |
233 |
> |
assertEquals(0, a.incrementAndGet(this)); |
234 |
> |
assertEquals(1, a.incrementAndGet(this)); |
235 |
> |
assertEquals(1, a.get(this)); |
236 |
|
} |
237 |
|
|
238 |
|
} |