ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AtomicIntegerFieldUpdaterTest.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

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