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