6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
– |
import java.util.concurrent.atomic.*; |
9 |
|
import junit.framework.*; |
10 |
< |
import java.util.*; |
10 |
> |
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; |
11 |
|
|
12 |
|
public class AtomicIntegerFieldUpdaterTest extends JSR166TestCase { |
13 |
|
volatile int x = 0; |
20 |
|
return new TestSuite(AtomicIntegerFieldUpdaterTest.class); |
21 |
|
} |
22 |
|
|
23 |
+ |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> updaterFor(String fieldName) { |
24 |
+ |
return AtomicIntegerFieldUpdater.newUpdater |
25 |
+ |
(AtomicIntegerFieldUpdaterTest.class, fieldName); |
26 |
+ |
} |
27 |
+ |
|
28 |
|
/** |
29 |
|
* Construction with non-existent field throws RuntimeException |
30 |
|
*/ |
31 |
|
public void testConstructor() { |
32 |
|
try { |
33 |
< |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> |
30 |
< |
a = AtomicIntegerFieldUpdater.newUpdater |
31 |
< |
(AtomicIntegerFieldUpdaterTest.class, "y"); |
33 |
> |
updaterFor("y"); |
34 |
|
shouldThrow(); |
35 |
|
} catch (RuntimeException success) {} |
36 |
|
} |
40 |
|
*/ |
41 |
|
public void testConstructor2() { |
42 |
|
try { |
43 |
< |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> |
42 |
< |
a = AtomicIntegerFieldUpdater.newUpdater |
43 |
< |
(AtomicIntegerFieldUpdaterTest.class, "z"); |
43 |
> |
updaterFor("z"); |
44 |
|
shouldThrow(); |
45 |
|
} catch (RuntimeException success) {} |
46 |
|
} |
50 |
|
*/ |
51 |
|
public void testConstructor3() { |
52 |
|
try { |
53 |
< |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> |
54 |
< |
a = AtomicIntegerFieldUpdater.newUpdater |
55 |
< |
(AtomicIntegerFieldUpdaterTest.class, "w"); |
53 |
> |
updaterFor("w"); |
54 |
|
shouldThrow(); |
55 |
|
} catch (RuntimeException success) {} |
56 |
|
} |
60 |
|
*/ |
61 |
|
public void testGetSet() { |
62 |
|
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a; |
63 |
< |
try { |
64 |
< |
a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
65 |
< |
} catch (RuntimeException ok) { |
66 |
< |
return; |
67 |
< |
} |
68 |
< |
x = 1; |
69 |
< |
assertEquals(1,a.get(this)); |
72 |
< |
a.set(this,2); |
73 |
< |
assertEquals(2,a.get(this)); |
74 |
< |
a.set(this,-3); |
75 |
< |
assertEquals(-3,a.get(this)); |
63 |
> |
a = updaterFor("x"); |
64 |
> |
x = 1; |
65 |
> |
assertEquals(1, a.get(this)); |
66 |
> |
a.set(this, 2); |
67 |
> |
assertEquals(2, a.get(this)); |
68 |
> |
a.set(this, -3); |
69 |
> |
assertEquals(-3, a.get(this)); |
70 |
|
} |
71 |
|
|
72 |
|
/** |
74 |
|
*/ |
75 |
|
public void testGetLazySet() { |
76 |
|
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a; |
77 |
< |
try { |
78 |
< |
a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
79 |
< |
} catch (RuntimeException ok) { |
80 |
< |
return; |
81 |
< |
} |
82 |
< |
x = 1; |
83 |
< |
assertEquals(1,a.get(this)); |
90 |
< |
a.lazySet(this,2); |
91 |
< |
assertEquals(2,a.get(this)); |
92 |
< |
a.lazySet(this,-3); |
93 |
< |
assertEquals(-3,a.get(this)); |
77 |
> |
a = updaterFor("x"); |
78 |
> |
x = 1; |
79 |
> |
assertEquals(1, a.get(this)); |
80 |
> |
a.lazySet(this, 2); |
81 |
> |
assertEquals(2, a.get(this)); |
82 |
> |
a.lazySet(this, -3); |
83 |
> |
assertEquals(-3, a.get(this)); |
84 |
|
} |
85 |
|
|
86 |
|
/** |
88 |
|
*/ |
89 |
|
public void testCompareAndSet() { |
90 |
|
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a; |
91 |
< |
try { |
92 |
< |
a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
93 |
< |
} catch (RuntimeException ok) { |
94 |
< |
return; |
95 |
< |
} |
96 |
< |
x = 1; |
97 |
< |
assertTrue(a.compareAndSet(this,1,2)); |
98 |
< |
assertTrue(a.compareAndSet(this,2,-4)); |
99 |
< |
assertEquals(-4,a.get(this)); |
110 |
< |
assertFalse(a.compareAndSet(this,-5,7)); |
111 |
< |
assertEquals(-4,a.get(this)); |
112 |
< |
assertTrue(a.compareAndSet(this,-4,7)); |
113 |
< |
assertEquals(7,a.get(this)); |
91 |
> |
a = updaterFor("x"); |
92 |
> |
x = 1; |
93 |
> |
assertTrue(a.compareAndSet(this, 1, 2)); |
94 |
> |
assertTrue(a.compareAndSet(this, 2, -4)); |
95 |
> |
assertEquals(-4, a.get(this)); |
96 |
> |
assertFalse(a.compareAndSet(this, -5, 7)); |
97 |
> |
assertEquals(-4, a.get(this)); |
98 |
> |
assertTrue(a.compareAndSet(this, -4, 7)); |
99 |
> |
assertEquals(7, a.get(this)); |
100 |
|
} |
101 |
|
|
116 |
– |
|
102 |
|
/** |
103 |
|
* compareAndSet in one thread enables another waiting for value |
104 |
|
* to succeed |
105 |
|
*/ |
106 |
|
public void testCompareAndSetInMultipleThreads() throws Exception { |
107 |
|
x = 1; |
108 |
< |
final AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest>a; |
109 |
< |
try { |
125 |
< |
a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
126 |
< |
} catch (RuntimeException ok) { |
127 |
< |
return; |
128 |
< |
} |
108 |
> |
final AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a; |
109 |
> |
a = updaterFor("x"); |
110 |
|
|
111 |
|
Thread t = new Thread(new CheckedRunnable() { |
112 |
|
public void realRun() { |
118 |
|
assertTrue(a.compareAndSet(this, 1, 2)); |
119 |
|
t.join(LONG_DELAY_MS); |
120 |
|
assertFalse(t.isAlive()); |
121 |
< |
assertEquals(a.get(this), 3); |
121 |
> |
assertEquals(3, a.get(this)); |
122 |
|
} |
123 |
|
|
124 |
|
/** |
127 |
|
*/ |
128 |
|
public void testWeakCompareAndSet() { |
129 |
|
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a; |
130 |
< |
try { |
131 |
< |
a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
132 |
< |
} catch (RuntimeException ok) { |
133 |
< |
return; |
134 |
< |
} |
135 |
< |
x = 1; |
136 |
< |
while (!a.weakCompareAndSet(this,1,2)); |
156 |
< |
while (!a.weakCompareAndSet(this,2,-4)); |
157 |
< |
assertEquals(-4,a.get(this)); |
158 |
< |
while (!a.weakCompareAndSet(this,-4,7)); |
159 |
< |
assertEquals(7,a.get(this)); |
130 |
> |
a = updaterFor("x"); |
131 |
> |
x = 1; |
132 |
> |
while (!a.weakCompareAndSet(this, 1, 2)); |
133 |
> |
while (!a.weakCompareAndSet(this, 2, -4)); |
134 |
> |
assertEquals(-4, a.get(this)); |
135 |
> |
while (!a.weakCompareAndSet(this, -4, 7)); |
136 |
> |
assertEquals(7, a.get(this)); |
137 |
|
} |
138 |
|
|
139 |
|
/** |
141 |
|
*/ |
142 |
|
public void testGetAndSet() { |
143 |
|
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a; |
144 |
< |
try { |
145 |
< |
a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
146 |
< |
} catch (RuntimeException ok) { |
147 |
< |
return; |
148 |
< |
} |
172 |
< |
x = 1; |
173 |
< |
assertEquals(1,a.getAndSet(this, 0)); |
174 |
< |
assertEquals(0,a.getAndSet(this,-10)); |
175 |
< |
assertEquals(-10,a.getAndSet(this,1)); |
144 |
> |
a = updaterFor("x"); |
145 |
> |
x = 1; |
146 |
> |
assertEquals(1, a.getAndSet(this, 0)); |
147 |
> |
assertEquals(0, a.getAndSet(this, -10)); |
148 |
> |
assertEquals(-10, a.getAndSet(this, 1)); |
149 |
|
} |
150 |
|
|
151 |
|
/** |
153 |
|
*/ |
154 |
|
public void testGetAndAdd() { |
155 |
|
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a; |
156 |
< |
try { |
157 |
< |
a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
158 |
< |
} catch (RuntimeException ok) { |
159 |
< |
return; |
160 |
< |
} |
161 |
< |
x = 1; |
189 |
< |
assertEquals(1,a.getAndAdd(this,2)); |
190 |
< |
assertEquals(3,a.get(this)); |
191 |
< |
assertEquals(3,a.getAndAdd(this,-4)); |
192 |
< |
assertEquals(-1,a.get(this)); |
156 |
> |
a = updaterFor("x"); |
157 |
> |
x = 1; |
158 |
> |
assertEquals(1, a.getAndAdd(this, 2)); |
159 |
> |
assertEquals(3, a.get(this)); |
160 |
> |
assertEquals(3, a.getAndAdd(this, -4)); |
161 |
> |
assertEquals(-1, a.get(this)); |
162 |
|
} |
163 |
|
|
164 |
|
/** |
166 |
|
*/ |
167 |
|
public void testGetAndDecrement() { |
168 |
|
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a; |
169 |
< |
try { |
170 |
< |
a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
171 |
< |
} catch (RuntimeException ok) { |
172 |
< |
return; |
173 |
< |
} |
205 |
< |
x = 1; |
206 |
< |
assertEquals(1,a.getAndDecrement(this)); |
207 |
< |
assertEquals(0,a.getAndDecrement(this)); |
208 |
< |
assertEquals(-1,a.getAndDecrement(this)); |
169 |
> |
a = updaterFor("x"); |
170 |
> |
x = 1; |
171 |
> |
assertEquals(1, a.getAndDecrement(this)); |
172 |
> |
assertEquals(0, a.getAndDecrement(this)); |
173 |
> |
assertEquals(-1, a.getAndDecrement(this)); |
174 |
|
} |
175 |
|
|
176 |
|
/** |
178 |
|
*/ |
179 |
|
public void testGetAndIncrement() { |
180 |
|
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a; |
181 |
< |
try { |
182 |
< |
a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
183 |
< |
} catch (RuntimeException ok) { |
184 |
< |
return; |
185 |
< |
} |
186 |
< |
x = 1; |
187 |
< |
assertEquals(1,a.getAndIncrement(this)); |
188 |
< |
assertEquals(2,a.get(this)); |
189 |
< |
a.set(this,-2); |
225 |
< |
assertEquals(-2,a.getAndIncrement(this)); |
226 |
< |
assertEquals(-1,a.getAndIncrement(this)); |
227 |
< |
assertEquals(0,a.getAndIncrement(this)); |
228 |
< |
assertEquals(1,a.get(this)); |
181 |
> |
a = updaterFor("x"); |
182 |
> |
x = 1; |
183 |
> |
assertEquals(1, a.getAndIncrement(this)); |
184 |
> |
assertEquals(2, a.get(this)); |
185 |
> |
a.set(this, -2); |
186 |
> |
assertEquals(-2, a.getAndIncrement(this)); |
187 |
> |
assertEquals(-1, a.getAndIncrement(this)); |
188 |
> |
assertEquals(0, a.getAndIncrement(this)); |
189 |
> |
assertEquals(1, a.get(this)); |
190 |
|
} |
191 |
|
|
192 |
|
/** |
194 |
|
*/ |
195 |
|
public void testAddAndGet() { |
196 |
|
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a; |
197 |
< |
try { |
198 |
< |
a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
199 |
< |
} catch (RuntimeException ok) { |
200 |
< |
return; |
201 |
< |
} |
202 |
< |
x = 1; |
242 |
< |
assertEquals(3,a.addAndGet(this,2)); |
243 |
< |
assertEquals(3,a.get(this)); |
244 |
< |
assertEquals(-1,a.addAndGet(this,-4)); |
245 |
< |
assertEquals(-1,a.get(this)); |
197 |
> |
a = updaterFor("x"); |
198 |
> |
x = 1; |
199 |
> |
assertEquals(3, a.addAndGet(this, 2)); |
200 |
> |
assertEquals(3, a.get(this)); |
201 |
> |
assertEquals(-1, a.addAndGet(this, -4)); |
202 |
> |
assertEquals(-1, a.get(this)); |
203 |
|
} |
204 |
|
|
205 |
|
/** |
207 |
|
*/ |
208 |
|
public void testDecrementAndGet() { |
209 |
|
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a; |
210 |
< |
try { |
211 |
< |
a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
212 |
< |
} catch (RuntimeException ok) { |
213 |
< |
return; |
214 |
< |
} |
215 |
< |
x = 1; |
259 |
< |
assertEquals(0,a.decrementAndGet(this)); |
260 |
< |
assertEquals(-1,a.decrementAndGet(this)); |
261 |
< |
assertEquals(-2,a.decrementAndGet(this)); |
262 |
< |
assertEquals(-2,a.get(this)); |
210 |
> |
a = updaterFor("x"); |
211 |
> |
x = 1; |
212 |
> |
assertEquals(0, a.decrementAndGet(this)); |
213 |
> |
assertEquals(-1, a.decrementAndGet(this)); |
214 |
> |
assertEquals(-2, a.decrementAndGet(this)); |
215 |
> |
assertEquals(-2, a.get(this)); |
216 |
|
} |
217 |
|
|
218 |
|
/** |
220 |
|
*/ |
221 |
|
public void testIncrementAndGet() { |
222 |
|
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a; |
223 |
< |
try { |
224 |
< |
a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
225 |
< |
} catch (RuntimeException ok) { |
226 |
< |
return; |
227 |
< |
} |
228 |
< |
x = 1; |
229 |
< |
assertEquals(2,a.incrementAndGet(this)); |
230 |
< |
assertEquals(2,a.get(this)); |
231 |
< |
a.set(this,-2); |
279 |
< |
assertEquals(-1,a.incrementAndGet(this)); |
280 |
< |
assertEquals(0,a.incrementAndGet(this)); |
281 |
< |
assertEquals(1,a.incrementAndGet(this)); |
282 |
< |
assertEquals(1,a.get(this)); |
223 |
> |
a = updaterFor("x"); |
224 |
> |
x = 1; |
225 |
> |
assertEquals(2, a.incrementAndGet(this)); |
226 |
> |
assertEquals(2, a.get(this)); |
227 |
> |
a.set(this, -2); |
228 |
> |
assertEquals(-1, a.incrementAndGet(this)); |
229 |
> |
assertEquals(0, a.incrementAndGet(this)); |
230 |
> |
assertEquals(1, a.incrementAndGet(this)); |
231 |
> |
assertEquals(1, a.get(this)); |
232 |
|
} |
233 |
|
|
234 |
|
} |