ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AtomicLongTest.java
Revision: 1.15
Committed: Tue Nov 17 06:58:50 2009 UTC (14 years, 5 months ago) by jsr166
Branch: MAIN
Changes since 1.14: +5 -4 lines
Log Message:
Runnable => CheckedRunnable

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/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 /**
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());
43 ai.set(2);
44 assertEquals(2,ai.get());
45 ai.set(-3);
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));
69 assertTrue(ai.compareAndSet(2,-4));
70 assertEquals(-4,ai.get());
71 assertFalse(ai.compareAndSet(-5,7));
72 assertFalse((7 == ai.get()));
73 assertTrue(ai.compareAndSet(-4,7));
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() throws Exception {
82 final AtomicLong ai = new AtomicLong(1);
83 Thread t = new Thread(new CheckedRunnable() {
84 public void realRun() {
85 while (!ai.compareAndSet(2, 3))
86 Thread.yield();
87 }});
88
89 t.start();
90 assertTrue(ai.compareAndSet(1, 2));
91 t.join(LONG_DELAY_MS);
92 assertFalse(t.isAlive());
93 assertEquals(ai.get(), 3);
94 }
95
96 /**
97 * repeated weakCompareAndSet succeeds in changing value when equal
98 * to expected
99 */
100 public void testWeakCompareAndSet() {
101 AtomicLong ai = new AtomicLong(1);
102 while (!ai.weakCompareAndSet(1,2));
103 while (!ai.weakCompareAndSet(2,-4));
104 assertEquals(-4,ai.get());
105 while (!ai.weakCompareAndSet(-4,7));
106 assertEquals(7,ai.get());
107 }
108
109 /**
110 * getAndSet returns previous value and sets to given value
111 */
112 public void testGetAndSet() {
113 AtomicLong ai = new AtomicLong(1);
114 assertEquals(1,ai.getAndSet(0));
115 assertEquals(0,ai.getAndSet(-10));
116 assertEquals(-10,ai.getAndSet(1));
117 }
118
119 /**
120 * getAndAdd returns previous value and adds given value
121 */
122 public void testGetAndAdd() {
123 AtomicLong ai = new AtomicLong(1);
124 assertEquals(1,ai.getAndAdd(2));
125 assertEquals(3,ai.get());
126 assertEquals(3,ai.getAndAdd(-4));
127 assertEquals(-1,ai.get());
128 }
129
130 /**
131 * getAndDecrement returns previous value and decrements
132 */
133 public void testGetAndDecrement() {
134 AtomicLong ai = new AtomicLong(1);
135 assertEquals(1,ai.getAndDecrement());
136 assertEquals(0,ai.getAndDecrement());
137 assertEquals(-1,ai.getAndDecrement());
138 }
139
140 /**
141 * getAndIncrement returns previous value and increments
142 */
143 public void testGetAndIncrement() {
144 AtomicLong ai = new AtomicLong(1);
145 assertEquals(1,ai.getAndIncrement());
146 assertEquals(2,ai.get());
147 ai.set(-2);
148 assertEquals(-2,ai.getAndIncrement());
149 assertEquals(-1,ai.getAndIncrement());
150 assertEquals(0,ai.getAndIncrement());
151 assertEquals(1,ai.get());
152 }
153
154 /**
155 * addAndGet adds given value to current, and returns current value
156 */
157 public void testAddAndGet() {
158 AtomicLong ai = new AtomicLong(1);
159 assertEquals(3,ai.addAndGet(2));
160 assertEquals(3,ai.get());
161 assertEquals(-1,ai.addAndGet(-4));
162 assertEquals(-1,ai.get());
163 }
164
165 /**
166 * decrementAndGet decrements and returns current value
167 */
168 public void testDecrementAndGet() {
169 AtomicLong ai = new AtomicLong(1);
170 assertEquals(0,ai.decrementAndGet());
171 assertEquals(-1,ai.decrementAndGet());
172 assertEquals(-2,ai.decrementAndGet());
173 assertEquals(-2,ai.get());
174 }
175
176 /**
177 * incrementAndGet increments and returns current value
178 */
179 public void testIncrementAndGet() {
180 AtomicLong ai = new AtomicLong(1);
181 assertEquals(2,ai.incrementAndGet());
182 assertEquals(2,ai.get());
183 ai.set(-2);
184 assertEquals(-1,ai.incrementAndGet());
185 assertEquals(0,ai.incrementAndGet());
186 assertEquals(1,ai.incrementAndGet());
187 assertEquals(1,ai.get());
188 }
189
190 /**
191 * a deserialized serialized atomic holds same value
192 */
193 public void testSerialization() throws Exception {
194 AtomicLong l = new AtomicLong();
195
196 l.set(-22);
197 ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
198 ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
199 out.writeObject(l);
200 out.close();
201
202 ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
203 ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
204 AtomicLong r = (AtomicLong) in.readObject();
205 assertEquals(l.get(), r.get());
206 }
207
208 /**
209 * toString returns current value.
210 */
211 public void testToString() {
212 AtomicLong ai = new AtomicLong();
213 for (long i = -12; i < 6; ++i) {
214 ai.set(i);
215 assertEquals(ai.toString(), Long.toString(i));
216 }
217 }
218
219 /**
220 * longValue returns current value.
221 */
222 public void testLongValue() {
223 AtomicLong ai = new AtomicLong();
224 for (int i = -12; i < 6; ++i) {
225 ai.set(i);
226 assertEquals((long)i, ai.longValue());
227 }
228 }
229
230 /**
231 * floatValue returns current value.
232 */
233 public void testFloatValue() {
234 AtomicLong ai = new AtomicLong();
235 for (int i = -12; i < 6; ++i) {
236 ai.set(i);
237 assertEquals((float)i, ai.floatValue());
238 }
239 }
240
241 /**
242 * doubleValue returns current value.
243 */
244 public void testDoubleValue() {
245 AtomicLong ai = new AtomicLong();
246 for (int i = -12; i < 6; ++i) {
247 ai.set(i);
248 assertEquals((double)i, ai.doubleValue());
249 }
250 }
251
252
253 }