ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AtomicLongFieldUpdaterTest.java
Revision: 1.21
Committed: Tue May 31 16:16:23 2011 UTC (12 years, 11 months ago) by jsr166
Branch: MAIN
Changes since 1.20: +1 -2 lines
Log Message:
use serialClone in serialization tests; update imports

File Contents

# Content
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/publicdomain/zero/1.0/
5 * Other contributors include Andrew Wright, Jeffrey Hayes,
6 * Pat Fisher, Mike Judd.
7 */
8
9 import junit.framework.*;
10 import java.util.concurrent.atomic.AtomicLongFieldUpdater;
11
12 public class AtomicLongFieldUpdaterTest extends JSR166TestCase {
13 volatile long x = 0;
14 int z;
15 long w;
16
17 public static void main(String[] args) {
18 junit.textui.TestRunner.run(suite());
19 }
20 public static Test suite() {
21 return new TestSuite(AtomicLongFieldUpdaterTest.class);
22 }
23
24 /**
25 * Construction with non-existent field throws RuntimeException
26 */
27 public void testConstructor() {
28 try {
29 AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest>
30 a = AtomicLongFieldUpdater.newUpdater
31 (AtomicLongFieldUpdaterTest.class, "y");
32 shouldThrow();
33 } catch (RuntimeException success) {}
34 }
35
36 /**
37 * construction with field not of given type throws RuntimeException
38 */
39 public void testConstructor2() {
40 try {
41 AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest>
42 a = AtomicLongFieldUpdater.newUpdater
43 (AtomicLongFieldUpdaterTest.class, "z");
44 shouldThrow();
45 } catch (RuntimeException success) {}
46 }
47
48 /**
49 * construction with non-volatile field throws RuntimeException
50 */
51 public void testConstructor3() {
52 try {
53 AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest>
54 a = AtomicLongFieldUpdater.newUpdater
55 (AtomicLongFieldUpdaterTest.class, "w");
56 shouldThrow();
57 } catch (RuntimeException success) {}
58 }
59
60 /**
61 * get returns the last value set or assigned
62 */
63 public void testGetSet() {
64 AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
65 try {
66 a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
67 } catch (RuntimeException ok) {
68 return;
69 }
70 x = 1;
71 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));
76 }
77
78 /**
79 * get returns the last value lazySet by same thread
80 */
81 public void testGetLazySet() {
82 AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
83 try {
84 a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
85 } catch (RuntimeException ok) {
86 return;
87 }
88 x = 1;
89 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));
94 }
95
96 /**
97 * compareAndSet succeeds in changing value if equal to expected else fails
98 */
99 public void testCompareAndSet() {
100 AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
101 try {
102 a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
103 } catch (RuntimeException ok) {
104 return;
105 }
106 x = 1;
107 assertTrue(a.compareAndSet(this,1,2));
108 assertTrue(a.compareAndSet(this,2,-4));
109 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));
114 }
115
116 /**
117 * compareAndSet in one thread enables another waiting for value
118 * to succeed
119 */
120 public void testCompareAndSetInMultipleThreads() throws Exception {
121 x = 1;
122 final AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest>a;
123 try {
124 a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
125 } catch (RuntimeException ok) {
126 return;
127 }
128
129 Thread t = new Thread(new CheckedRunnable() {
130 public void realRun() {
131 while (!a.compareAndSet(AtomicLongFieldUpdaterTest.this, 2, 3))
132 Thread.yield();
133 }});
134
135 t.start();
136 assertTrue(a.compareAndSet(this, 1, 2));
137 t.join(LONG_DELAY_MS);
138 assertFalse(t.isAlive());
139 assertEquals(a.get(this), 3);
140 }
141
142 /**
143 * repeated weakCompareAndSet succeeds in changing value when equal
144 * to expected
145 */
146 public void testWeakCompareAndSet() {
147 AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
148 try {
149 a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
150 } catch (RuntimeException ok) {
151 return;
152 }
153 x = 1;
154 while (!a.weakCompareAndSet(this,1,2));
155 while (!a.weakCompareAndSet(this,2,-4));
156 assertEquals(-4,a.get(this));
157 while (!a.weakCompareAndSet(this,-4,7));
158 assertEquals(7,a.get(this));
159 }
160
161 /**
162 * getAndSet returns previous value and sets to given value
163 */
164 public void testGetAndSet() {
165 AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
166 try {
167 a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
168 } catch (RuntimeException ok) {
169 return;
170 }
171 x = 1;
172 assertEquals(1,a.getAndSet(this, 0));
173 assertEquals(0,a.getAndSet(this,-10));
174 assertEquals(-10,a.getAndSet(this,1));
175 }
176
177 /**
178 * getAndAdd returns previous value and adds given value
179 */
180 public void testGetAndAdd() {
181 AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
182 try {
183 a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
184 } catch (RuntimeException ok) {
185 return;
186 }
187 x = 1;
188 assertEquals(1,a.getAndAdd(this,2));
189 assertEquals(3,a.get(this));
190 assertEquals(3,a.getAndAdd(this,-4));
191 assertEquals(-1,a.get(this));
192 }
193
194 /**
195 * getAndDecrement returns previous value and decrements
196 */
197 public void testGetAndDecrement() {
198 AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
199 try {
200 a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
201 } catch (RuntimeException ok) {
202 return;
203 }
204 x = 1;
205 assertEquals(1,a.getAndDecrement(this));
206 assertEquals(0,a.getAndDecrement(this));
207 assertEquals(-1,a.getAndDecrement(this));
208 }
209
210 /**
211 * getAndIncrement returns previous value and increments
212 */
213 public void testGetAndIncrement() {
214 AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
215 try {
216 a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
217 } catch (RuntimeException ok) {
218 return;
219 }
220 x = 1;
221 assertEquals(1,a.getAndIncrement(this));
222 assertEquals(2,a.get(this));
223 a.set(this,-2);
224 assertEquals(-2,a.getAndIncrement(this));
225 assertEquals(-1,a.getAndIncrement(this));
226 assertEquals(0,a.getAndIncrement(this));
227 assertEquals(1,a.get(this));
228 }
229
230 /**
231 * addAndGet adds given value to current, and returns current value
232 */
233 public void testAddAndGet() {
234 AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
235 try {
236 a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
237 } catch (RuntimeException ok) {
238 return;
239 }
240 x = 1;
241 assertEquals(3,a.addAndGet(this,2));
242 assertEquals(3,a.get(this));
243 assertEquals(-1,a.addAndGet(this,-4));
244 assertEquals(-1,a.get(this));
245 }
246
247 /**
248 * decrementAndGet decrements and returns current value
249 */
250 public void testDecrementAndGet() {
251 AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
252 try {
253 a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
254 } catch (RuntimeException ok) {
255 return;
256 }
257 x = 1;
258 assertEquals(0,a.decrementAndGet(this));
259 assertEquals(-1,a.decrementAndGet(this));
260 assertEquals(-2,a.decrementAndGet(this));
261 assertEquals(-2,a.get(this));
262 }
263
264 /**
265 * incrementAndGet increments and returns current value
266 */
267 public void testIncrementAndGet() {
268 AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
269 try {
270 a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x");
271 } catch (RuntimeException ok) {
272 return;
273 }
274 x = 1;
275 assertEquals(2,a.incrementAndGet(this));
276 assertEquals(2,a.get(this));
277 a.set(this,-2);
278 assertEquals(-1,a.incrementAndGet(this));
279 assertEquals(0,a.incrementAndGet(this));
280 assertEquals(1,a.incrementAndGet(this));
281 assertEquals(1,a.get(this));
282 }
283
284 }