ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AtomicIntegerFieldUpdaterTest.java
Revision: 1.18
Committed: Sat Oct 9 19:30:34 2010 UTC (13 years, 7 months ago) by jsr166
Branch: MAIN
Changes since 1.17: +3 -3 lines
Log Message:
whitespace

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 jsr166 1.18 * 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 dl 1.1 }
77 dl 1.9
78     /**
79 jsr166 1.18 * get returns the last value lazySet by same thread
80 dl 1.9 */
81     public void testGetLazySet() {
82     AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
83     try {
84     a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
85     } catch (RuntimeException ok) {
86     return;
87     }
88     x = 1;
89 jsr166 1.15 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 dl 1.9 }
95    
96 dl 1.3 /**
97 dl 1.4 * compareAndSet succeeds in changing value if equal to expected else fails
98 dl 1.3 */
99     public void testCompareAndSet() {
100 dl 1.8 AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
101     try {
102     a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
103     } catch (RuntimeException ok) {
104     return;
105     }
106 dl 1.1 x = 1;
107 jsr166 1.15 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 jsr166 1.17 assertEquals(-4,a.get(this));
112 jsr166 1.15 assertTrue(a.compareAndSet(this,-4,7));
113     assertEquals(7,a.get(this));
114 dl 1.1 }
115    
116 dl 1.4
117     /**
118     * compareAndSet in one thread enables another waiting for value
119     * to succeed
120     */
121 jsr166 1.13 public void testCompareAndSetInMultipleThreads() throws Exception {
122 dl 1.4 x = 1;
123 dl 1.8 final AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest>a;
124     try {
125     a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
126     } catch (RuntimeException ok) {
127     return;
128     }
129 dl 1.4
130 jsr166 1.14 Thread t = new Thread(new CheckedRunnable() {
131     public void realRun() {
132     while (!a.compareAndSet(AtomicIntegerFieldUpdaterTest.this, 2, 3))
133     Thread.yield();
134     }});
135 jsr166 1.13
136     t.start();
137     assertTrue(a.compareAndSet(this, 1, 2));
138     t.join(LONG_DELAY_MS);
139     assertFalse(t.isAlive());
140     assertEquals(a.get(this), 3);
141 dl 1.4 }
142    
143 dl 1.3 /**
144 dl 1.4 * repeated weakCompareAndSet succeeds in changing value when equal
145 jsr166 1.10 * to expected
146 dl 1.3 */
147     public void testWeakCompareAndSet() {
148 dl 1.8 AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
149     try {
150     a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
151     } catch (RuntimeException ok) {
152     return;
153     }
154 dl 1.1 x = 1;
155 jsr166 1.15 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));
160 dl 1.1 }
161    
162 dl 1.3 /**
163 jsr166 1.18 * getAndSet returns previous value and sets to given value
164 dl 1.3 */
165     public void testGetAndSet() {
166 dl 1.8 AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
167     try {
168     a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
169     } catch (RuntimeException ok) {
170     return;
171     }
172 dl 1.1 x = 1;
173 jsr166 1.15 assertEquals(1,a.getAndSet(this, 0));
174     assertEquals(0,a.getAndSet(this,-10));
175     assertEquals(-10,a.getAndSet(this,1));
176 dl 1.1 }
177    
178 dl 1.3 /**
179 dl 1.4 * getAndAdd returns previous value and adds given value
180 dl 1.3 */
181     public void testGetAndAdd() {
182 dl 1.8 AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
183     try {
184     a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
185     } catch (RuntimeException ok) {
186     return;
187     }
188 dl 1.1 x = 1;
189 jsr166 1.15 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));
193 dl 1.1 }
194    
195 dl 1.3 /**
196 dl 1.4 * getAndDecrement returns previous value and decrements
197 dl 1.3 */
198     public void testGetAndDecrement() {
199 dl 1.8 AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
200     try {
201     a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
202     } catch (RuntimeException ok) {
203     return;
204     }
205 dl 1.1 x = 1;
206 jsr166 1.15 assertEquals(1,a.getAndDecrement(this));
207     assertEquals(0,a.getAndDecrement(this));
208     assertEquals(-1,a.getAndDecrement(this));
209 dl 1.1 }
210    
211 dl 1.3 /**
212 dl 1.4 * getAndIncrement returns previous value and increments
213 dl 1.3 */
214     public void testGetAndIncrement() {
215 dl 1.8 AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
216     try {
217     a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
218     } catch (RuntimeException ok) {
219     return;
220     }
221 dl 1.1 x = 1;
222 jsr166 1.15 assertEquals(1,a.getAndIncrement(this));
223     assertEquals(2,a.get(this));
224     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));
229 dl 1.1 }
230    
231 dl 1.3 /**
232 dl 1.4 * addAndGet adds given value to current, and returns current value
233 dl 1.3 */
234     public void testAddAndGet() {
235 dl 1.8 AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
236     try {
237     a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
238     } catch (RuntimeException ok) {
239     return;
240     }
241 dl 1.1 x = 1;
242 jsr166 1.15 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));
246 dl 1.1 }
247    
248 dl 1.3 /**
249 dl 1.4 * decrementAndGet decrements and returns current value
250 dl 1.3 */
251     public void testDecrementAndGet() {
252 dl 1.8 AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
253     try {
254     a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
255     } catch (RuntimeException ok) {
256     return;
257     }
258 dl 1.1 x = 1;
259 jsr166 1.15 assertEquals(0,a.decrementAndGet(this));
260     assertEquals(-1,a.decrementAndGet(this));
261     assertEquals(-2,a.decrementAndGet(this));
262     assertEquals(-2,a.get(this));
263 dl 1.1 }
264    
265 dl 1.3 /**
266 dl 1.4 * incrementAndGet increments and returns current value
267 dl 1.3 */
268     public void testIncrementAndGet() {
269 dl 1.8 AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
270     try {
271     a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
272     } catch (RuntimeException ok) {
273     return;
274     }
275 dl 1.1 x = 1;
276 jsr166 1.15 assertEquals(2,a.incrementAndGet(this));
277     assertEquals(2,a.get(this));
278     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));
283 dl 1.1 }
284    
285     }