1 |
|
/* |
2 |
< |
* Written by members of JCP JSR-166 Expert Group and released to the |
3 |
< |
* public domain. Use, modify, and redistribute this code in any way |
4 |
< |
* without acknowledgement. Other contributors include Andrew Wright, |
5 |
< |
* Jeffrey Hayes, Pat Fischer, Mike Judd. |
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.*; |
18 |
|
return new TestSuite(AtomicLongTest.class); |
19 |
|
} |
20 |
|
|
21 |
+ |
/** |
22 |
+ |
* constructor initializes to given value |
23 |
+ |
*/ |
24 |
|
public void testConstructor(){ |
25 |
|
AtomicLong ai = new AtomicLong(1); |
26 |
|
assertEquals(1,ai.get()); |
27 |
|
} |
28 |
|
|
29 |
+ |
/** |
30 |
+ |
* default constructed initializes to zero |
31 |
+ |
*/ |
32 |
|
public void testConstructor2(){ |
33 |
|
AtomicLong ai = new AtomicLong(); |
34 |
|
assertEquals(0,ai.get()); |
35 |
|
} |
36 |
|
|
37 |
+ |
/** |
38 |
+ |
* get returns the last value set |
39 |
+ |
*/ |
40 |
|
public void testGetSet(){ |
41 |
|
AtomicLong ai = new AtomicLong(1); |
42 |
|
assertEquals(1,ai.get()); |
46 |
|
assertEquals(-3,ai.get()); |
47 |
|
|
48 |
|
} |
49 |
+ |
|
50 |
+ |
/** |
51 |
+ |
* get returns the last value lazySet in same thread |
52 |
+ |
*/ |
53 |
+ |
public void testGetLazySet(){ |
54 |
+ |
AtomicLong ai = new AtomicLong(1); |
55 |
+ |
assertEquals(1,ai.get()); |
56 |
+ |
ai.lazySet(2); |
57 |
+ |
assertEquals(2,ai.get()); |
58 |
+ |
ai.lazySet(-3); |
59 |
+ |
assertEquals(-3,ai.get()); |
60 |
+ |
|
61 |
+ |
} |
62 |
+ |
|
63 |
+ |
/** |
64 |
+ |
* compareAndSet succeeds in changing value if equal to expected else fails |
65 |
+ |
*/ |
66 |
|
public void testCompareAndSet(){ |
67 |
|
AtomicLong ai = new AtomicLong(1); |
68 |
|
assertTrue(ai.compareAndSet(1,2)); |
74 |
|
assertEquals(7,ai.get()); |
75 |
|
} |
76 |
|
|
77 |
+ |
/** |
78 |
+ |
* compareAndSet in one thread enables another waiting for value |
79 |
+ |
* to succeed |
80 |
+ |
*/ |
81 |
+ |
public void testCompareAndSetInMultipleThreads() { |
82 |
+ |
final AtomicLong ai = new AtomicLong(1); |
83 |
+ |
Thread t = new Thread(new Runnable() { |
84 |
+ |
public void run() { |
85 |
+ |
while(!ai.compareAndSet(2, 3)) Thread.yield(); |
86 |
+ |
}}); |
87 |
+ |
try { |
88 |
+ |
t.start(); |
89 |
+ |
assertTrue(ai.compareAndSet(1, 2)); |
90 |
+ |
t.join(LONG_DELAY_MS); |
91 |
+ |
assertFalse(t.isAlive()); |
92 |
+ |
assertEquals(ai.get(), 3); |
93 |
+ |
} |
94 |
+ |
catch(Exception e) { |
95 |
+ |
unexpectedException(); |
96 |
+ |
} |
97 |
+ |
} |
98 |
+ |
|
99 |
+ |
/** |
100 |
+ |
* repeated weakCompareAndSet succeeds in changing value when equal |
101 |
+ |
* to expected |
102 |
+ |
*/ |
103 |
|
public void testWeakCompareAndSet(){ |
104 |
|
AtomicLong ai = new AtomicLong(1); |
105 |
|
while(!ai.weakCompareAndSet(1,2)); |
109 |
|
assertEquals(7,ai.get()); |
110 |
|
} |
111 |
|
|
112 |
+ |
/** |
113 |
+ |
* getAndSet returns previous value and sets to given value |
114 |
+ |
*/ |
115 |
|
public void testGetAndSet(){ |
116 |
|
AtomicLong ai = new AtomicLong(1); |
117 |
|
assertEquals(1,ai.getAndSet(0)); |
119 |
|
assertEquals(-10,ai.getAndSet(1)); |
120 |
|
} |
121 |
|
|
122 |
+ |
/** |
123 |
+ |
* getAndAdd returns previous value and adds given value |
124 |
+ |
*/ |
125 |
|
public void testGetAndAdd(){ |
126 |
|
AtomicLong ai = new AtomicLong(1); |
127 |
|
assertEquals(1,ai.getAndAdd(2)); |
130 |
|
assertEquals(-1,ai.get()); |
131 |
|
} |
132 |
|
|
133 |
+ |
/** |
134 |
+ |
* getAndDecrement returns previous value and decrements |
135 |
+ |
*/ |
136 |
|
public void testGetAndDecrement(){ |
137 |
|
AtomicLong ai = new AtomicLong(1); |
138 |
|
assertEquals(1,ai.getAndDecrement()); |
140 |
|
assertEquals(-1,ai.getAndDecrement()); |
141 |
|
} |
142 |
|
|
143 |
+ |
/** |
144 |
+ |
* getAndIncrement returns previous value and increments |
145 |
+ |
*/ |
146 |
|
public void testGetAndIncrement(){ |
147 |
|
AtomicLong ai = new AtomicLong(1); |
148 |
|
assertEquals(1,ai.getAndIncrement()); |
154 |
|
assertEquals(1,ai.get()); |
155 |
|
} |
156 |
|
|
157 |
+ |
/** |
158 |
+ |
* addAndGet adds given value to current, and returns current value |
159 |
+ |
*/ |
160 |
|
public void testAddAndGet(){ |
161 |
|
AtomicLong ai = new AtomicLong(1); |
162 |
|
assertEquals(3,ai.addAndGet(2)); |
165 |
|
assertEquals(-1,ai.get()); |
166 |
|
} |
167 |
|
|
168 |
+ |
/** |
169 |
+ |
* decrementAndGet decrements and returns current value |
170 |
+ |
*/ |
171 |
|
public void testDecrementAndGet(){ |
172 |
|
AtomicLong ai = new AtomicLong(1); |
173 |
|
assertEquals(0,ai.decrementAndGet()); |
176 |
|
assertEquals(-2,ai.get()); |
177 |
|
} |
178 |
|
|
179 |
+ |
/** |
180 |
+ |
* incrementAndGet increments and returns current value |
181 |
+ |
*/ |
182 |
|
public void testIncrementAndGet(){ |
183 |
|
AtomicLong ai = new AtomicLong(1); |
184 |
|
assertEquals(2,ai.incrementAndGet()); |
190 |
|
assertEquals(1,ai.get()); |
191 |
|
} |
192 |
|
|
193 |
+ |
/** |
194 |
+ |
* a deserialized serialized atomic holds same value |
195 |
+ |
*/ |
196 |
|
public void testSerialization() { |
197 |
|
AtomicLong l = new AtomicLong(); |
198 |
|
|
208 |
|
AtomicLong r = (AtomicLong) in.readObject(); |
209 |
|
assertEquals(l.get(), r.get()); |
210 |
|
} catch(Exception e){ |
211 |
< |
e.printStackTrace(); |
212 |
< |
fail("unexpected exception"); |
211 |
> |
unexpectedException(); |
212 |
> |
} |
213 |
> |
} |
214 |
> |
|
215 |
> |
/** |
216 |
> |
* toString returns current value. |
217 |
> |
*/ |
218 |
> |
public void testToString() { |
219 |
> |
AtomicLong ai = new AtomicLong(); |
220 |
> |
for (long i = -12; i < 6; ++i) { |
221 |
> |
ai.set(i); |
222 |
> |
assertEquals(ai.toString(), Long.toString(i)); |
223 |
> |
} |
224 |
> |
} |
225 |
> |
|
226 |
> |
/** |
227 |
> |
* longValue returns current value. |
228 |
> |
*/ |
229 |
> |
public void testLongValue() { |
230 |
> |
AtomicLong ai = new AtomicLong(); |
231 |
> |
for (int i = -12; i < 6; ++i) { |
232 |
> |
ai.set(i); |
233 |
> |
assertEquals((long)i, ai.longValue()); |
234 |
|
} |
235 |
|
} |
236 |
|
|
237 |
+ |
/** |
238 |
+ |
* floatValue returns current value. |
239 |
+ |
*/ |
240 |
+ |
public void testFloatValue() { |
241 |
+ |
AtomicLong ai = new AtomicLong(); |
242 |
+ |
for (int i = -12; i < 6; ++i) { |
243 |
+ |
ai.set(i); |
244 |
+ |
assertEquals((float)i, ai.floatValue()); |
245 |
+ |
} |
246 |
+ |
} |
247 |
+ |
|
248 |
+ |
/** |
249 |
+ |
* doubleValue returns current value. |
250 |
+ |
*/ |
251 |
+ |
public void testDoubleValue() { |
252 |
+ |
AtomicLong ai = new AtomicLong(); |
253 |
+ |
for (int i = -12; i < 6; ++i) { |
254 |
+ |
ai.set(i); |
255 |
+ |
assertEquals((double)i, ai.doubleValue()); |
256 |
+ |
} |
257 |
+ |
} |
258 |
+ |
|
259 |
+ |
|
260 |
|
} |