ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AtomicIntegerFieldUpdaterTest.java
Revision: 1.24
Committed: Mon Apr 1 20:58:58 2013 UTC (11 years, 1 month ago) by jsr166
Branch: MAIN
Changes since 1.23: +21 -70 lines
Log Message:
RuntimeException is *not* ok for valid fields; refactor a little

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