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