ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AtomicIntegerFieldUpdaterTest.java
Revision: 1.16
Committed: Sat Nov 21 17:38:05 2009 UTC (14 years, 5 months ago) by jsr166
Branch: MAIN
Changes since 1.15: +3 -6 lines
Log Message:
improve exception handling

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