35394
|
1 |
/*
|
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
3 |
*
|
|
4 |
* This code is free software; you can redistribute it and/or modify it
|
|
5 |
* under the terms of the GNU General Public License version 2 only, as
|
|
6 |
* published by the Free Software Foundation.
|
|
7 |
*
|
|
8 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
9 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
10 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
11 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
12 |
* accompanied this code).
|
|
13 |
*
|
|
14 |
* You should have received a copy of the GNU General Public License version
|
|
15 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
16 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
17 |
*
|
|
18 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
19 |
* or visit www.oracle.com if you need additional information or have any
|
|
20 |
* questions.
|
|
21 |
*/
|
|
22 |
|
|
23 |
/*
|
|
24 |
* This file is available under and governed by the GNU General Public
|
|
25 |
* License version 2 only, as published by the Free Software Foundation.
|
|
26 |
* However, the following notice accompanied the original version of this
|
|
27 |
* file:
|
|
28 |
*
|
|
29 |
* Written by Doug Lea with assistance from members of JCP JSR-166
|
|
30 |
* Expert Group and released to the public domain, as explained at
|
|
31 |
* http://creativecommons.org/publicdomain/zero/1.0/
|
|
32 |
* Other contributors include Andrew Wright, Jeffrey Hayes,
|
|
33 |
* Pat Fisher, Mike Judd.
|
|
34 |
*/
|
|
35 |
|
|
36 |
import java.util.concurrent.atomic.AtomicInteger;
|
|
37 |
|
|
38 |
import junit.framework.Test;
|
|
39 |
import junit.framework.TestSuite;
|
|
40 |
|
|
41 |
public class AtomicIntegerTest extends JSR166TestCase {
|
|
42 |
public static void main(String[] args) {
|
|
43 |
main(suite(), args);
|
|
44 |
}
|
|
45 |
public static Test suite() {
|
|
46 |
return new TestSuite(AtomicIntegerTest.class);
|
|
47 |
}
|
|
48 |
|
|
49 |
final int[] VALUES = {
|
|
50 |
Integer.MIN_VALUE, -1, 0, 1, 42, Integer.MAX_VALUE,
|
|
51 |
};
|
|
52 |
|
|
53 |
/**
|
|
54 |
* constructor initializes to given value
|
|
55 |
*/
|
|
56 |
public void testConstructor() {
|
|
57 |
AtomicInteger ai = new AtomicInteger(1);
|
|
58 |
assertEquals(1, ai.get());
|
|
59 |
}
|
|
60 |
|
|
61 |
/**
|
|
62 |
* default constructed initializes to zero
|
|
63 |
*/
|
|
64 |
public void testConstructor2() {
|
|
65 |
AtomicInteger ai = new AtomicInteger();
|
|
66 |
assertEquals(0, ai.get());
|
|
67 |
}
|
|
68 |
|
|
69 |
/**
|
|
70 |
* get returns the last value set
|
|
71 |
*/
|
|
72 |
public void testGetSet() {
|
|
73 |
AtomicInteger ai = new AtomicInteger(1);
|
|
74 |
assertEquals(1, ai.get());
|
|
75 |
ai.set(2);
|
|
76 |
assertEquals(2, ai.get());
|
|
77 |
ai.set(-3);
|
|
78 |
assertEquals(-3, ai.get());
|
|
79 |
}
|
|
80 |
|
|
81 |
/**
|
|
82 |
* get returns the last value lazySet in same thread
|
|
83 |
*/
|
|
84 |
public void testGetLazySet() {
|
|
85 |
AtomicInteger ai = new AtomicInteger(1);
|
|
86 |
assertEquals(1, ai.get());
|
|
87 |
ai.lazySet(2);
|
|
88 |
assertEquals(2, ai.get());
|
|
89 |
ai.lazySet(-3);
|
|
90 |
assertEquals(-3, ai.get());
|
|
91 |
}
|
|
92 |
|
|
93 |
/**
|
|
94 |
* compareAndSet succeeds in changing value if equal to expected else fails
|
|
95 |
*/
|
|
96 |
public void testCompareAndSet() {
|
|
97 |
AtomicInteger ai = new AtomicInteger(1);
|
|
98 |
assertTrue(ai.compareAndSet(1, 2));
|
|
99 |
assertTrue(ai.compareAndSet(2, -4));
|
|
100 |
assertEquals(-4, ai.get());
|
|
101 |
assertFalse(ai.compareAndSet(-5, 7));
|
|
102 |
assertEquals(-4, ai.get());
|
|
103 |
assertTrue(ai.compareAndSet(-4, 7));
|
|
104 |
assertEquals(7, ai.get());
|
|
105 |
}
|
|
106 |
|
|
107 |
/**
|
|
108 |
* compareAndSet in one thread enables another waiting for value
|
|
109 |
* to succeed
|
|
110 |
*/
|
|
111 |
public void testCompareAndSetInMultipleThreads() throws Exception {
|
|
112 |
final AtomicInteger ai = new AtomicInteger(1);
|
|
113 |
Thread t = new Thread(new CheckedRunnable() {
|
|
114 |
public void realRun() {
|
|
115 |
while (!ai.compareAndSet(2, 3))
|
|
116 |
Thread.yield();
|
|
117 |
}});
|
|
118 |
|
|
119 |
t.start();
|
|
120 |
assertTrue(ai.compareAndSet(1, 2));
|
|
121 |
t.join(LONG_DELAY_MS);
|
|
122 |
assertFalse(t.isAlive());
|
|
123 |
assertEquals(3, ai.get());
|
|
124 |
}
|
|
125 |
|
|
126 |
/**
|
|
127 |
* repeated weakCompareAndSet succeeds in changing value when equal
|
|
128 |
* to expected
|
|
129 |
*/
|
|
130 |
public void testWeakCompareAndSet() {
|
|
131 |
AtomicInteger ai = new AtomicInteger(1);
|
|
132 |
do {} while (!ai.weakCompareAndSet(1, 2));
|
|
133 |
do {} while (!ai.weakCompareAndSet(2, -4));
|
|
134 |
assertEquals(-4, ai.get());
|
|
135 |
do {} while (!ai.weakCompareAndSet(-4, 7));
|
|
136 |
assertEquals(7, ai.get());
|
|
137 |
}
|
|
138 |
|
|
139 |
/**
|
|
140 |
* getAndSet returns previous value and sets to given value
|
|
141 |
*/
|
|
142 |
public void testGetAndSet() {
|
|
143 |
AtomicInteger ai = new AtomicInteger(1);
|
|
144 |
assertEquals(1, ai.getAndSet(0));
|
|
145 |
assertEquals(0, ai.getAndSet(-10));
|
|
146 |
assertEquals(-10, ai.getAndSet(1));
|
|
147 |
}
|
|
148 |
|
|
149 |
/**
|
|
150 |
* getAndAdd returns previous value and adds given value
|
|
151 |
*/
|
|
152 |
public void testGetAndAdd() {
|
|
153 |
AtomicInteger ai = new AtomicInteger(1);
|
|
154 |
assertEquals(1, ai.getAndAdd(2));
|
|
155 |
assertEquals(3, ai.get());
|
|
156 |
assertEquals(3, ai.getAndAdd(-4));
|
|
157 |
assertEquals(-1, ai.get());
|
|
158 |
}
|
|
159 |
|
|
160 |
/**
|
|
161 |
* getAndDecrement returns previous value and decrements
|
|
162 |
*/
|
|
163 |
public void testGetAndDecrement() {
|
|
164 |
AtomicInteger ai = new AtomicInteger(1);
|
|
165 |
assertEquals(1, ai.getAndDecrement());
|
|
166 |
assertEquals(0, ai.getAndDecrement());
|
|
167 |
assertEquals(-1, ai.getAndDecrement());
|
|
168 |
}
|
|
169 |
|
|
170 |
/**
|
|
171 |
* getAndIncrement returns previous value and increments
|
|
172 |
*/
|
|
173 |
public void testGetAndIncrement() {
|
|
174 |
AtomicInteger ai = new AtomicInteger(1);
|
|
175 |
assertEquals(1, ai.getAndIncrement());
|
|
176 |
assertEquals(2, ai.get());
|
|
177 |
ai.set(-2);
|
|
178 |
assertEquals(-2, ai.getAndIncrement());
|
|
179 |
assertEquals(-1, ai.getAndIncrement());
|
|
180 |
assertEquals(0, ai.getAndIncrement());
|
|
181 |
assertEquals(1, ai.get());
|
|
182 |
}
|
|
183 |
|
|
184 |
/**
|
|
185 |
* addAndGet adds given value to current, and returns current value
|
|
186 |
*/
|
|
187 |
public void testAddAndGet() {
|
|
188 |
AtomicInteger ai = new AtomicInteger(1);
|
|
189 |
assertEquals(3, ai.addAndGet(2));
|
|
190 |
assertEquals(3, ai.get());
|
|
191 |
assertEquals(-1, ai.addAndGet(-4));
|
|
192 |
assertEquals(-1, ai.get());
|
|
193 |
}
|
|
194 |
|
|
195 |
/**
|
|
196 |
* decrementAndGet decrements and returns current value
|
|
197 |
*/
|
|
198 |
public void testDecrementAndGet() {
|
|
199 |
AtomicInteger ai = new AtomicInteger(1);
|
|
200 |
assertEquals(0, ai.decrementAndGet());
|
|
201 |
assertEquals(-1, ai.decrementAndGet());
|
|
202 |
assertEquals(-2, ai.decrementAndGet());
|
|
203 |
assertEquals(-2, ai.get());
|
|
204 |
}
|
|
205 |
|
|
206 |
/**
|
|
207 |
* incrementAndGet increments and returns current value
|
|
208 |
*/
|
|
209 |
public void testIncrementAndGet() {
|
|
210 |
AtomicInteger ai = new AtomicInteger(1);
|
|
211 |
assertEquals(2, ai.incrementAndGet());
|
|
212 |
assertEquals(2, ai.get());
|
|
213 |
ai.set(-2);
|
|
214 |
assertEquals(-1, ai.incrementAndGet());
|
|
215 |
assertEquals(0, ai.incrementAndGet());
|
|
216 |
assertEquals(1, ai.incrementAndGet());
|
|
217 |
assertEquals(1, ai.get());
|
|
218 |
}
|
|
219 |
|
|
220 |
/**
|
|
221 |
* a deserialized serialized atomic holds same value
|
|
222 |
*/
|
|
223 |
public void testSerialization() throws Exception {
|
|
224 |
AtomicInteger x = new AtomicInteger();
|
|
225 |
AtomicInteger y = serialClone(x);
|
|
226 |
assertNotSame(x, y);
|
|
227 |
x.set(22);
|
|
228 |
AtomicInteger z = serialClone(x);
|
|
229 |
assertEquals(22, x.get());
|
|
230 |
assertEquals(0, y.get());
|
|
231 |
assertEquals(22, z.get());
|
|
232 |
}
|
|
233 |
|
|
234 |
/**
|
|
235 |
* toString returns current value.
|
|
236 |
*/
|
|
237 |
public void testToString() {
|
|
238 |
AtomicInteger ai = new AtomicInteger();
|
|
239 |
assertEquals("0", ai.toString());
|
|
240 |
for (int x : VALUES) {
|
|
241 |
ai.set(x);
|
|
242 |
assertEquals(Integer.toString(x), ai.toString());
|
|
243 |
}
|
|
244 |
}
|
|
245 |
|
|
246 |
/**
|
|
247 |
* intValue returns current value.
|
|
248 |
*/
|
|
249 |
public void testIntValue() {
|
|
250 |
AtomicInteger ai = new AtomicInteger();
|
|
251 |
assertEquals(0, ai.intValue());
|
|
252 |
for (int x : VALUES) {
|
|
253 |
ai.set(x);
|
|
254 |
assertEquals(x, ai.intValue());
|
|
255 |
}
|
|
256 |
}
|
|
257 |
|
|
258 |
/**
|
|
259 |
* longValue returns current value.
|
|
260 |
*/
|
|
261 |
public void testLongValue() {
|
|
262 |
AtomicInteger ai = new AtomicInteger();
|
|
263 |
assertEquals(0L, ai.longValue());
|
|
264 |
for (int x : VALUES) {
|
|
265 |
ai.set(x);
|
|
266 |
assertEquals((long)x, ai.longValue());
|
|
267 |
}
|
|
268 |
}
|
|
269 |
|
|
270 |
/**
|
|
271 |
* floatValue returns current value.
|
|
272 |
*/
|
|
273 |
public void testFloatValue() {
|
|
274 |
AtomicInteger ai = new AtomicInteger();
|
|
275 |
assertEquals(0.0f, ai.floatValue());
|
|
276 |
for (int x : VALUES) {
|
|
277 |
ai.set(x);
|
|
278 |
assertEquals((float)x, ai.floatValue());
|
|
279 |
}
|
|
280 |
}
|
|
281 |
|
|
282 |
/**
|
|
283 |
* doubleValue returns current value.
|
|
284 |
*/
|
|
285 |
public void testDoubleValue() {
|
|
286 |
AtomicInteger ai = new AtomicInteger();
|
|
287 |
assertEquals(0.0d, ai.doubleValue());
|
|
288 |
for (int x : VALUES) {
|
|
289 |
ai.set(x);
|
|
290 |
assertEquals((double)x, ai.doubleValue());
|
|
291 |
}
|
|
292 |
}
|
|
293 |
|
|
294 |
}
|