ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AtomicLongFieldUpdaterTest.java
Revision: 1.25
Committed: Tue Apr 2 04:11:28 2013 UTC (11 years, 1 month ago) by jsr166
Branch: MAIN
Changes since 1.24: +7 -5 lines
Log Message:
tighten tests to match specs

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