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/licenses/publicdomain |
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.*; |
11 |
import java.io.*; |
12 |
|
13 |
public class AtomicLongTest extends JSR166TestCase { |
14 |
public static void main (String[] args) { |
15 |
junit.textui.TestRunner.run (suite()); |
16 |
} |
17 |
public static Test suite() { |
18 |
return new TestSuite(AtomicLongTest.class); |
19 |
} |
20 |
|
21 |
final long[] VALUES = { |
22 |
Long.MIN_VALUE, |
23 |
Integer.MIN_VALUE, -1, 0, 1, 42, Integer.MAX_VALUE, |
24 |
Long.MAX_VALUE, |
25 |
}; |
26 |
|
27 |
/** |
28 |
* constructor initializes to given value |
29 |
*/ |
30 |
public void testConstructor() { |
31 |
AtomicLong ai = new AtomicLong(1); |
32 |
assertEquals(1,ai.get()); |
33 |
} |
34 |
|
35 |
/** |
36 |
* default constructed initializes to zero |
37 |
*/ |
38 |
public void testConstructor2() { |
39 |
AtomicLong ai = new AtomicLong(); |
40 |
assertEquals(0,ai.get()); |
41 |
} |
42 |
|
43 |
/** |
44 |
* get returns the last value set |
45 |
*/ |
46 |
public void testGetSet() { |
47 |
AtomicLong ai = new AtomicLong(1); |
48 |
assertEquals(1,ai.get()); |
49 |
ai.set(2); |
50 |
assertEquals(2,ai.get()); |
51 |
ai.set(-3); |
52 |
assertEquals(-3,ai.get()); |
53 |
} |
54 |
|
55 |
/** |
56 |
* get returns the last value lazySet in same thread |
57 |
*/ |
58 |
public void testGetLazySet() { |
59 |
AtomicLong ai = new AtomicLong(1); |
60 |
assertEquals(1,ai.get()); |
61 |
ai.lazySet(2); |
62 |
assertEquals(2,ai.get()); |
63 |
ai.lazySet(-3); |
64 |
assertEquals(-3,ai.get()); |
65 |
} |
66 |
|
67 |
/** |
68 |
* compareAndSet succeeds in changing value if equal to expected else fails |
69 |
*/ |
70 |
public void testCompareAndSet() { |
71 |
AtomicLong ai = new AtomicLong(1); |
72 |
assertTrue(ai.compareAndSet(1,2)); |
73 |
assertTrue(ai.compareAndSet(2,-4)); |
74 |
assertEquals(-4,ai.get()); |
75 |
assertFalse(ai.compareAndSet(-5,7)); |
76 |
assertEquals(-4,ai.get()); |
77 |
assertTrue(ai.compareAndSet(-4,7)); |
78 |
assertEquals(7,ai.get()); |
79 |
} |
80 |
|
81 |
/** |
82 |
* compareAndSet in one thread enables another waiting for value |
83 |
* to succeed |
84 |
*/ |
85 |
public void testCompareAndSetInMultipleThreads() throws Exception { |
86 |
final AtomicLong ai = new AtomicLong(1); |
87 |
Thread t = new Thread(new CheckedRunnable() { |
88 |
public void realRun() { |
89 |
while (!ai.compareAndSet(2, 3)) |
90 |
Thread.yield(); |
91 |
}}); |
92 |
|
93 |
t.start(); |
94 |
assertTrue(ai.compareAndSet(1, 2)); |
95 |
t.join(LONG_DELAY_MS); |
96 |
assertFalse(t.isAlive()); |
97 |
assertEquals(ai.get(), 3); |
98 |
} |
99 |
|
100 |
/** |
101 |
* repeated weakCompareAndSet succeeds in changing value when equal |
102 |
* to expected |
103 |
*/ |
104 |
public void testWeakCompareAndSet() { |
105 |
AtomicLong ai = new AtomicLong(1); |
106 |
while (!ai.weakCompareAndSet(1,2)); |
107 |
while (!ai.weakCompareAndSet(2,-4)); |
108 |
assertEquals(-4,ai.get()); |
109 |
while (!ai.weakCompareAndSet(-4,7)); |
110 |
assertEquals(7,ai.get()); |
111 |
} |
112 |
|
113 |
/** |
114 |
* getAndSet returns previous value and sets to given value |
115 |
*/ |
116 |
public void testGetAndSet() { |
117 |
AtomicLong ai = new AtomicLong(1); |
118 |
assertEquals(1,ai.getAndSet(0)); |
119 |
assertEquals(0,ai.getAndSet(-10)); |
120 |
assertEquals(-10,ai.getAndSet(1)); |
121 |
} |
122 |
|
123 |
/** |
124 |
* getAndAdd returns previous value and adds given value |
125 |
*/ |
126 |
public void testGetAndAdd() { |
127 |
AtomicLong ai = new AtomicLong(1); |
128 |
assertEquals(1,ai.getAndAdd(2)); |
129 |
assertEquals(3,ai.get()); |
130 |
assertEquals(3,ai.getAndAdd(-4)); |
131 |
assertEquals(-1,ai.get()); |
132 |
} |
133 |
|
134 |
/** |
135 |
* getAndDecrement returns previous value and decrements |
136 |
*/ |
137 |
public void testGetAndDecrement() { |
138 |
AtomicLong ai = new AtomicLong(1); |
139 |
assertEquals(1,ai.getAndDecrement()); |
140 |
assertEquals(0,ai.getAndDecrement()); |
141 |
assertEquals(-1,ai.getAndDecrement()); |
142 |
} |
143 |
|
144 |
/** |
145 |
* getAndIncrement returns previous value and increments |
146 |
*/ |
147 |
public void testGetAndIncrement() { |
148 |
AtomicLong ai = new AtomicLong(1); |
149 |
assertEquals(1,ai.getAndIncrement()); |
150 |
assertEquals(2,ai.get()); |
151 |
ai.set(-2); |
152 |
assertEquals(-2,ai.getAndIncrement()); |
153 |
assertEquals(-1,ai.getAndIncrement()); |
154 |
assertEquals(0,ai.getAndIncrement()); |
155 |
assertEquals(1,ai.get()); |
156 |
} |
157 |
|
158 |
/** |
159 |
* addAndGet adds given value to current, and returns current value |
160 |
*/ |
161 |
public void testAddAndGet() { |
162 |
AtomicLong ai = new AtomicLong(1); |
163 |
assertEquals(3,ai.addAndGet(2)); |
164 |
assertEquals(3,ai.get()); |
165 |
assertEquals(-1,ai.addAndGet(-4)); |
166 |
assertEquals(-1,ai.get()); |
167 |
} |
168 |
|
169 |
/** |
170 |
* decrementAndGet decrements and returns current value |
171 |
*/ |
172 |
public void testDecrementAndGet() { |
173 |
AtomicLong ai = new AtomicLong(1); |
174 |
assertEquals(0,ai.decrementAndGet()); |
175 |
assertEquals(-1,ai.decrementAndGet()); |
176 |
assertEquals(-2,ai.decrementAndGet()); |
177 |
assertEquals(-2,ai.get()); |
178 |
} |
179 |
|
180 |
/** |
181 |
* incrementAndGet increments and returns current value |
182 |
*/ |
183 |
public void testIncrementAndGet() { |
184 |
AtomicLong ai = new AtomicLong(1); |
185 |
assertEquals(2,ai.incrementAndGet()); |
186 |
assertEquals(2,ai.get()); |
187 |
ai.set(-2); |
188 |
assertEquals(-1,ai.incrementAndGet()); |
189 |
assertEquals(0,ai.incrementAndGet()); |
190 |
assertEquals(1,ai.incrementAndGet()); |
191 |
assertEquals(1,ai.get()); |
192 |
} |
193 |
|
194 |
/** |
195 |
* a deserialized serialized atomic holds same value |
196 |
*/ |
197 |
public void testSerialization() throws Exception { |
198 |
AtomicLong l = new AtomicLong(); |
199 |
|
200 |
l.set(-22); |
201 |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
202 |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
203 |
out.writeObject(l); |
204 |
out.close(); |
205 |
|
206 |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
207 |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
208 |
AtomicLong r = (AtomicLong) in.readObject(); |
209 |
assertEquals(l.get(), r.get()); |
210 |
} |
211 |
|
212 |
/** |
213 |
* toString returns current value. |
214 |
*/ |
215 |
public void testToString() { |
216 |
AtomicLong ai = new AtomicLong(); |
217 |
assertEquals("0", ai.toString()); |
218 |
for (long i : VALUES) { |
219 |
ai.set(i); |
220 |
assertEquals(ai.toString(), Long.toString(i)); |
221 |
} |
222 |
} |
223 |
|
224 |
/** |
225 |
* intValue returns current value. |
226 |
*/ |
227 |
public void testIntValue() { |
228 |
AtomicLong ai = new AtomicLong(); |
229 |
assertEquals(0, ai.intValue()); |
230 |
for (long x : VALUES) { |
231 |
ai.set(x); |
232 |
assertEquals((int)x, ai.intValue()); |
233 |
} |
234 |
} |
235 |
|
236 |
/** |
237 |
* longValue returns current value. |
238 |
*/ |
239 |
public void testLongValue() { |
240 |
AtomicLong ai = new AtomicLong(); |
241 |
assertEquals(0L, ai.longValue()); |
242 |
for (long x : VALUES) { |
243 |
ai.set(x); |
244 |
assertEquals((long)x, ai.longValue()); |
245 |
} |
246 |
} |
247 |
|
248 |
/** |
249 |
* floatValue returns current value. |
250 |
*/ |
251 |
public void testFloatValue() { |
252 |
AtomicLong ai = new AtomicLong(); |
253 |
assertEquals(0.0f, ai.floatValue()); |
254 |
for (long x : VALUES) { |
255 |
ai.set(x); |
256 |
assertEquals((float)x, ai.floatValue()); |
257 |
} |
258 |
} |
259 |
|
260 |
/** |
261 |
* doubleValue returns current value. |
262 |
*/ |
263 |
public void testDoubleValue() { |
264 |
AtomicLong ai = new AtomicLong(); |
265 |
assertEquals(0.0d, ai.doubleValue()); |
266 |
for (long x : VALUES) { |
267 |
ai.set(x); |
268 |
assertEquals((double)x, ai.doubleValue()); |
269 |
} |
270 |
} |
271 |
|
272 |
} |