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