ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck-jsr166e/AtomicDoubleTest.java
Revision: 1.8
Committed: Thu Oct 20 16:33:25 2011 UTC (12 years, 7 months ago) by jsr166
Branch: MAIN
CVS Tags: HEAD
Changes since 1.7: +10 -11 lines
Log Message:
incorporate feedback from guava reviewers; migrate to slightly more guava-compatible coding style

File Contents

# Content
1 /*
2 * Written by Doug Lea and Martin Buchholz with assistance from
3 * members of JCP JSR-166 Expert Group and released to the public
4 * domain, as explained at
5 * http://creativecommons.org/publicdomain/zero/1.0/
6 */
7
8 import junit.framework.*;
9 import jsr166e.extra.AtomicDouble;
10
11 public class AtomicDoubleTest extends JSR166TestCase {
12 public static void main(String[] args) {
13 junit.textui.TestRunner.run(suite());
14 }
15 public static Test suite() {
16 return new TestSuite(AtomicDoubleTest.class);
17 }
18
19 private static final double[] VALUES = {
20 Double.NEGATIVE_INFINITY,
21 -Double.MAX_VALUE,
22 (double) Long.MIN_VALUE,
23 (double) Integer.MIN_VALUE,
24 -Math.PI,
25 -1.0,
26 -Double.MIN_VALUE,
27 -0.0,
28 +0.0,
29 Double.MIN_VALUE,
30 1.0,
31 Math.PI,
32 (double) Integer.MAX_VALUE,
33 (double) Long.MAX_VALUE,
34 Double.MAX_VALUE,
35 Double.POSITIVE_INFINITY,
36 Double.NaN,
37 Float.MAX_VALUE,
38 };
39
40 /** The notion of equality used by AtomicDouble */
41 static boolean bitEquals(double x, double y) {
42 return Double.doubleToRawLongBits(x) == Double.doubleToRawLongBits(y);
43 }
44
45 static void assertBitEquals(double x, double y) {
46 assertEquals(Double.doubleToRawLongBits(x),
47 Double.doubleToRawLongBits(y));
48 }
49
50 /**
51 * constructor initializes to given value
52 */
53 public void testConstructor() {
54 for (double x : VALUES) {
55 AtomicDouble a = new AtomicDouble(x);
56 assertBitEquals(x, a.get());
57 }
58 }
59
60 /**
61 * default constructed initializes to zero
62 */
63 public void testConstructor2() {
64 AtomicDouble a = new AtomicDouble();
65 assertBitEquals(0.0, a.get());
66 }
67
68 /**
69 * get returns the last value set
70 */
71 public void testGetSet() {
72 AtomicDouble at = new AtomicDouble(1.0);
73 assertBitEquals(1.0, at.get());
74 for (double x : VALUES) {
75 at.set(x);
76 assertBitEquals(x, at.get());
77 }
78 }
79
80 /**
81 * get returns the last value lazySet in same thread
82 */
83 public void testGetLazySet() {
84 AtomicDouble at = new AtomicDouble(1.0);
85 assertBitEquals(1.0, at.get());
86 for (double x : VALUES) {
87 at.lazySet(x);
88 assertBitEquals(x, at.get());
89 }
90 }
91
92 /**
93 * compareAndSet succeeds in changing value if equal to expected else fails
94 */
95 public void testCompareAndSet() {
96 double prev = Math.E;
97 double unused = Math.E + Math.PI;
98 AtomicDouble at = new AtomicDouble(prev);
99 for (double x : VALUES) {
100 assertBitEquals(prev, at.get());
101 assertFalse(at.compareAndSet(unused, x));
102 assertBitEquals(prev, at.get());
103 assertTrue(at.compareAndSet(prev, x));
104 assertBitEquals(x, at.get());
105 prev = x;
106 }
107 }
108
109 /**
110 * compareAndSet in one thread enables another waiting for value
111 * to succeed
112 */
113 public void testCompareAndSetInMultipleThreads() throws Exception {
114 final AtomicDouble at = new AtomicDouble(1.0);
115 Thread t = newStartedThread(new CheckedRunnable() {
116 public void realRun() {
117 while (!at.compareAndSet(2.0, 3.0))
118 Thread.yield();
119 }});
120
121 assertTrue(at.compareAndSet(1.0, 2.0));
122 awaitTermination(t);
123 assertBitEquals(3.0, at.get());
124 }
125
126 /**
127 * repeated weakCompareAndSet succeeds in changing value when equal
128 * to expected
129 */
130 public void testWeakCompareAndSet() {
131 double prev = Math.E;
132 double unused = Math.E + Math.PI;
133 AtomicDouble at = new AtomicDouble(prev);
134 for (double x : VALUES) {
135 assertBitEquals(prev, at.get());
136 assertFalse(at.weakCompareAndSet(unused, x));
137 assertBitEquals(prev, at.get());
138 while (!at.weakCompareAndSet(prev, x))
139 ;
140 assertBitEquals(x, at.get());
141 prev = x;
142 }
143 }
144
145 /**
146 * getAndSet returns previous value and sets to given value
147 */
148 public void testGetAndSet() {
149 double prev = Math.E;
150 AtomicDouble at = new AtomicDouble(prev);
151 for (double x : VALUES) {
152 assertBitEquals(prev, at.getAndSet(x));
153 prev = x;
154 }
155 }
156
157 /**
158 * getAndAdd returns previous value and adds given value
159 */
160 public void testGetAndAdd() {
161 for (double x : VALUES) {
162 for (double y : VALUES) {
163 AtomicDouble a = new AtomicDouble(x);
164 double z = a.getAndAdd(y);
165 assertBitEquals(x, z);
166 assertBitEquals(x + y, a.get());
167 }
168 }
169 }
170
171 /**
172 * addAndGet adds given value to current, and returns current value
173 */
174 public void testAddAndGet() {
175 for (double x : VALUES) {
176 for (double y : VALUES) {
177 AtomicDouble a = new AtomicDouble(x);
178 double z = a.addAndGet(y);
179 assertBitEquals(x + y, z);
180 assertBitEquals(x + y, a.get());
181 }
182 }
183 }
184
185 /**
186 * a deserialized serialized atomic holds same value
187 */
188 public void testSerialization() throws Exception {
189 AtomicDouble a = new AtomicDouble();
190 AtomicDouble b = serialClone(a);
191 assertTrue(a != b);
192 a.set(-22.0);
193 AtomicDouble c = serialClone(a);
194 assertBitEquals(-22.0, a.get());
195 assertBitEquals(0.0, b.get());
196 assertBitEquals(-22.0, c.get());
197 for (double x : VALUES) {
198 AtomicDouble d = new AtomicDouble(x);
199 assertBitEquals(serialClone(d).get(), d.get());
200 }
201 }
202
203 /**
204 * toString returns current value
205 */
206 public void testToString() {
207 AtomicDouble at = new AtomicDouble();
208 assertEquals("0.0", at.toString());
209 for (double x : VALUES) {
210 at.set(x);
211 assertEquals(Double.toString(x), at.toString());
212 }
213 }
214
215 /**
216 * intValue returns current value.
217 */
218 public void testIntValue() {
219 AtomicDouble at = new AtomicDouble();
220 assertEquals(0, at.intValue());
221 for (double x : VALUES) {
222 at.set(x);
223 assertEquals((int) x, at.intValue());
224 }
225 }
226
227 /**
228 * longValue returns current value.
229 */
230 public void testLongValue() {
231 AtomicDouble at = new AtomicDouble();
232 assertEquals(0L, at.longValue());
233 for (double x : VALUES) {
234 at.set(x);
235 assertEquals((long) x, at.longValue());
236 }
237 }
238
239 /**
240 * floatValue returns current value.
241 */
242 public void testFloatValue() {
243 AtomicDouble at = new AtomicDouble();
244 assertEquals(0.0f, at.floatValue());
245 for (double x : VALUES) {
246 at.set(x);
247 assertEquals((float) x, at.floatValue());
248 }
249 }
250
251 /**
252 * doubleValue returns current value.
253 */
254 public void testDoubleValue() {
255 AtomicDouble at = new AtomicDouble();
256 assertEquals(0.0d, at.doubleValue());
257 for (double x : VALUES) {
258 at.set(x);
259 assertBitEquals(x, at.doubleValue());
260 }
261 }
262
263 /**
264 * compareAndSet treats +0.0 and -0.0 as distinct values
265 */
266 public void testDistinctZeros() {
267 AtomicDouble at = new AtomicDouble(+0.0);
268 assertFalse(at.compareAndSet(-0.0, 7.0));
269 assertFalse(at.weakCompareAndSet(-0.0, 7.0));
270 assertBitEquals(+0.0, at.get());
271 assertTrue(at.compareAndSet(+0.0, -0.0));
272 assertBitEquals(-0.0, at.get());
273 assertFalse(at.compareAndSet(+0.0, 7.0));
274 assertFalse(at.weakCompareAndSet(+0.0, 7.0));
275 assertBitEquals(-0.0, at.get());
276 }
277 }