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

# Content
1 /*
2 * 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 */
8
9 import java.util.concurrent.atomic.*;
10 import junit.framework.*;
11 import java.util.*;
12
13 public class AtomicIntegerFieldUpdaterTest extends JSR166TestCase {
14 volatile int x = 0;
15 int w;
16 long z;
17 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 /**
25 * Construction with non-existent field throws RuntimeException
26 */
27 public void testConstructor() {
28 try{
29 AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest>
30 a = AtomicIntegerFieldUpdater.newUpdater
31 (AtomicIntegerFieldUpdaterTest.class, "y");
32 shouldThrow();
33 }
34 catch (RuntimeException rt) {}
35 }
36
37 /**
38 * construction with field not of given type throws RuntimeException
39 */
40 public void testConstructor2() {
41 try{
42 AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest>
43 a = AtomicIntegerFieldUpdater.newUpdater
44 (AtomicIntegerFieldUpdaterTest.class, "z");
45 shouldThrow();
46 }
47 catch (RuntimeException rt) {}
48 }
49
50 /**
51 * construction with non-volatile field throws RuntimeException
52 */
53 public void testConstructor3() {
54 try{
55 AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest>
56 a = AtomicIntegerFieldUpdater.newUpdater
57 (AtomicIntegerFieldUpdaterTest.class, "w");
58 shouldThrow();
59 }
60 catch (RuntimeException rt) {}
61 }
62
63 /**
64 * get returns the last value set or assigned
65 */
66 public void testGetSet() {
67 AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
68 try {
69 a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
70 } catch (RuntimeException ok) {
71 return;
72 }
73 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 /**
82 * compareAndSet succeeds in changing value if equal to expected else fails
83 */
84 public void testCompareAndSet() {
85 AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
86 try {
87 a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
88 } catch (RuntimeException ok) {
89 return;
90 }
91 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
102 /**
103 * compareAndSet in one thread enables another waiting for value
104 * to succeed
105 */
106 public void testCompareAndSetInMultipleThreads() {
107 x = 1;
108 final AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest>a;
109 try {
110 a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
111 } catch (RuntimeException ok) {
112 return;
113 }
114
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 /**
132 * repeated weakCompareAndSet succeeds in changing value when equal
133 * to expected
134 */
135 public void testWeakCompareAndSet() {
136 AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
137 try {
138 a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
139 } catch (RuntimeException ok) {
140 return;
141 }
142 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 /**
151 * getAndSet returns previous value and sets to given value
152 */
153 public void testGetAndSet() {
154 AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
155 try {
156 a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
157 } catch (RuntimeException ok) {
158 return;
159 }
160 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 /**
167 * getAndAdd returns previous value and adds given value
168 */
169 public void testGetAndAdd() {
170 AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
171 try {
172 a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
173 } catch (RuntimeException ok) {
174 return;
175 }
176 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 /**
184 * getAndDecrement returns previous value and decrements
185 */
186 public void testGetAndDecrement() {
187 AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
188 try {
189 a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
190 } catch (RuntimeException ok) {
191 return;
192 }
193 x = 1;
194 assertEquals(1,a.getAndDecrement(this));
195 assertEquals(0,a.getAndDecrement(this));
196 assertEquals(-1,a.getAndDecrement(this));
197 }
198
199 /**
200 * getAndIncrement returns previous value and increments
201 */
202 public void testGetAndIncrement() {
203 AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
204 try {
205 a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
206 } catch (RuntimeException ok) {
207 return;
208 }
209 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 /**
220 * addAndGet adds given value to current, and returns current value
221 */
222 public void testAddAndGet() {
223 AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
224 try {
225 a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
226 } catch (RuntimeException ok) {
227 return;
228 }
229 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 /**
237 * decrementAndGet decrements and returns current value
238 */
239 public void testDecrementAndGet() {
240 AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
241 try {
242 a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
243 } catch (RuntimeException ok) {
244 return;
245 }
246 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 /**
254 * incrementAndGet increments and returns current value
255 */
256 public void testIncrementAndGet() {
257 AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
258 try {
259 a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
260 } catch (RuntimeException ok) {
261 return;
262 }
263 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 }