ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AtomicLongFieldUpdaterTest.java
Revision: 1.29
Committed: Sat Apr 25 04:55:30 2015 UTC (9 years ago) by jsr166
Branch: MAIN
Changes since 1.28: +1 -1 lines
Log Message:
improve main methods; respect system properties; actually fail if a test fails

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