ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AtomicLongTest.java
Revision: 1.14
Committed: Tue Nov 17 03:12:51 2009 UTC (14 years, 5 months ago) by jsr166
Branch: MAIN
Changes since 1.13: +18 -26 lines
Log Message:
nicer exception handling

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