ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AtomicIntegerFieldUpdaterTest.java
Revision: 1.8
Committed: Tue Jan 20 20:20:56 2004 UTC (20 years, 3 months ago) by dl
Branch: MAIN
CVS Tags: JSR166_PFD
Changes since 1.7: +66 -11 lines
Log Message:
Don't fail if test harness doesn't have sufficient permissions

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     * 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 dl 1.1 public static void main(String[] args){
18     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 dl 1.1 try{
29     AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest>
30     a = AtomicIntegerFieldUpdater.newUpdater
31 dl 1.5 (AtomicIntegerFieldUpdaterTest.class, "y");
32 dl 1.3 shouldThrow();
33 dl 1.2 }
34     catch (RuntimeException rt) {}
35     }
36    
37 dl 1.3 /**
38 dl 1.4 * construction with field not of given type throws RuntimeException
39 dl 1.3 */
40     public void testConstructor2() {
41 dl 1.2 try{
42     AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest>
43     a = AtomicIntegerFieldUpdater.newUpdater
44 dl 1.5 (AtomicIntegerFieldUpdaterTest.class, "z");
45 dl 1.3 shouldThrow();
46 dl 1.1 }
47     catch (RuntimeException rt) {}
48     }
49    
50 dl 1.3 /**
51 dl 1.4 * construction with non-volatile field throws RuntimeException
52     */
53     public void testConstructor3() {
54     try{
55     AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest>
56     a = AtomicIntegerFieldUpdater.newUpdater
57 dl 1.5 (AtomicIntegerFieldUpdaterTest.class, "w");
58 dl 1.4 shouldThrow();
59     }
60     catch (RuntimeException rt) {}
61     }
62    
63     /**
64     * get returns the last value set or assigned
65 dl 1.3 */
66     public void testGetSet() {
67 dl 1.8 AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
68     try {
69     a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
70     } catch (RuntimeException ok) {
71     return;
72     }
73 dl 1.1 x = 1;
74     assertEquals(1,a.get(this));
75     a.set(this,2);
76     assertEquals(2,a.get(this));
77     a.set(this,-3);
78     assertEquals(-3,a.get(this));
79    
80     }
81 dl 1.3 /**
82 dl 1.4 * compareAndSet succeeds in changing value if equal to expected else fails
83 dl 1.3 */
84     public void testCompareAndSet() {
85 dl 1.8 AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
86     try {
87     a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
88     } catch (RuntimeException ok) {
89     return;
90     }
91 dl 1.1 x = 1;
92     assertTrue(a.compareAndSet(this,1,2));
93     assertTrue(a.compareAndSet(this,2,-4));
94     assertEquals(-4,a.get(this));
95     assertFalse(a.compareAndSet(this,-5,7));
96     assertFalse((7 == a.get(this)));
97     assertTrue(a.compareAndSet(this,-4,7));
98     assertEquals(7,a.get(this));
99     }
100    
101 dl 1.4
102     /**
103     * compareAndSet in one thread enables another waiting for value
104     * to succeed
105     */
106     public void testCompareAndSetInMultipleThreads() {
107     x = 1;
108 dl 1.8 final AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest>a;
109     try {
110     a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
111     } catch (RuntimeException ok) {
112     return;
113     }
114 dl 1.4
115     Thread t = new Thread(new Runnable() {
116     public void run() {
117     while(!a.compareAndSet(AtomicIntegerFieldUpdaterTest.this, 2, 3)) Thread.yield();
118     }});
119     try {
120     t.start();
121     assertTrue(a.compareAndSet(this, 1, 2));
122     t.join(LONG_DELAY_MS);
123     assertFalse(t.isAlive());
124     assertEquals(a.get(this), 3);
125     }
126     catch(Exception e) {
127     unexpectedException();
128     }
129     }
130    
131 dl 1.3 /**
132 dl 1.4 * repeated weakCompareAndSet succeeds in changing value when equal
133     * to expected
134 dl 1.3 */
135     public void testWeakCompareAndSet() {
136 dl 1.8 AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
137     try {
138     a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
139     } catch (RuntimeException ok) {
140     return;
141     }
142 dl 1.1 x = 1;
143     while(!a.weakCompareAndSet(this,1,2));
144     while(!a.weakCompareAndSet(this,2,-4));
145     assertEquals(-4,a.get(this));
146     while(!a.weakCompareAndSet(this,-4,7));
147     assertEquals(7,a.get(this));
148     }
149    
150 dl 1.3 /**
151 dl 1.4 * getAndSet returns previous value and sets to given value
152 dl 1.3 */
153     public void testGetAndSet() {
154 dl 1.8 AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
155     try {
156     a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
157     } catch (RuntimeException ok) {
158     return;
159     }
160 dl 1.1 x = 1;
161     assertEquals(1,a.getAndSet(this, 0));
162     assertEquals(0,a.getAndSet(this,-10));
163     assertEquals(-10,a.getAndSet(this,1));
164     }
165    
166 dl 1.3 /**
167 dl 1.4 * getAndAdd returns previous value and adds given value
168 dl 1.3 */
169     public void testGetAndAdd() {
170 dl 1.8 AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
171     try {
172     a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
173     } catch (RuntimeException ok) {
174     return;
175     }
176 dl 1.1 x = 1;
177     assertEquals(1,a.getAndAdd(this,2));
178     assertEquals(3,a.get(this));
179     assertEquals(3,a.getAndAdd(this,-4));
180     assertEquals(-1,a.get(this));
181     }
182    
183 dl 1.3 /**
184 dl 1.4 * getAndDecrement returns previous value and decrements
185 dl 1.3 */
186     public void testGetAndDecrement() {
187 dl 1.8 AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
188     try {
189     a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
190     } catch (RuntimeException ok) {
191     return;
192     }
193 dl 1.1 x = 1;
194     assertEquals(1,a.getAndDecrement(this));
195     assertEquals(0,a.getAndDecrement(this));
196     assertEquals(-1,a.getAndDecrement(this));
197     }
198    
199 dl 1.3 /**
200 dl 1.4 * getAndIncrement returns previous value and increments
201 dl 1.3 */
202     public void testGetAndIncrement() {
203 dl 1.8 AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
204     try {
205     a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
206     } catch (RuntimeException ok) {
207     return;
208     }
209 dl 1.1 x = 1;
210     assertEquals(1,a.getAndIncrement(this));
211     assertEquals(2,a.get(this));
212     a.set(this,-2);
213     assertEquals(-2,a.getAndIncrement(this));
214     assertEquals(-1,a.getAndIncrement(this));
215     assertEquals(0,a.getAndIncrement(this));
216     assertEquals(1,a.get(this));
217     }
218    
219 dl 1.3 /**
220 dl 1.4 * addAndGet adds given value to current, and returns current value
221 dl 1.3 */
222     public void testAddAndGet() {
223 dl 1.8 AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
224     try {
225     a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
226     } catch (RuntimeException ok) {
227     return;
228     }
229 dl 1.1 x = 1;
230     assertEquals(3,a.addAndGet(this,2));
231     assertEquals(3,a.get(this));
232     assertEquals(-1,a.addAndGet(this,-4));
233     assertEquals(-1,a.get(this));
234     }
235    
236 dl 1.3 /**
237 dl 1.4 * decrementAndGet decrements and returns current value
238 dl 1.3 */
239     public void testDecrementAndGet() {
240 dl 1.8 AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
241     try {
242     a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
243     } catch (RuntimeException ok) {
244     return;
245     }
246 dl 1.1 x = 1;
247     assertEquals(0,a.decrementAndGet(this));
248     assertEquals(-1,a.decrementAndGet(this));
249     assertEquals(-2,a.decrementAndGet(this));
250     assertEquals(-2,a.get(this));
251     }
252    
253 dl 1.3 /**
254 dl 1.4 * incrementAndGet increments and returns current value
255 dl 1.3 */
256     public void testIncrementAndGet() {
257 dl 1.8 AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
258     try {
259     a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
260     } catch (RuntimeException ok) {
261     return;
262     }
263 dl 1.1 x = 1;
264     assertEquals(2,a.incrementAndGet(this));
265     assertEquals(2,a.get(this));
266     a.set(this,-2);
267     assertEquals(-1,a.incrementAndGet(this));
268     assertEquals(0,a.incrementAndGet(this));
269     assertEquals(1,a.incrementAndGet(this));
270     assertEquals(1,a.get(this));
271     }
272    
273     }