ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck-jsr166e/AtomicDoubleTest.java
Revision: 1.3
Committed: Wed Aug 10 04:33:41 2011 UTC (12 years, 9 months ago) by jsr166
Branch: MAIN
Changes since 1.2: +1 -1 lines
Log Message:
seems to be production quality

File Contents

# User Rev Content
1 jsr166 1.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     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     };
38    
39     /** The notion of equality used by AtomicDouble */
40     boolean bitEquals(double x, double y) {
41     return Double.doubleToRawLongBits(x) == Double.doubleToRawLongBits(y);
42     }
43    
44     void assertBitEquals(double x, double y) {
45     assertEquals(Double.doubleToRawLongBits(x),
46     Double.doubleToRawLongBits(y));
47     }
48    
49     /**
50     * constructor initializes to given value
51     */
52     public void testConstructor() {
53     for (double x : VALUES) {
54     AtomicDouble a = new AtomicDouble(x);
55     assertBitEquals(x, a.get());
56     }
57     }
58    
59     /**
60     * default constructed initializes to zero
61     */
62     public void testConstructor2() {
63     AtomicDouble a = new AtomicDouble();
64     assertBitEquals(0.0, a.get());
65     }
66    
67     /**
68     * get returns the last value set
69     */
70     public void testGetSet() {
71     AtomicDouble at = new AtomicDouble(1.0);
72     assertBitEquals(1.0, at.get());
73     for (double x : VALUES) {
74     at.set(x);
75     assertBitEquals(x, at.get());
76     }
77     }
78    
79     /**
80     * get returns the last value lazySet in same thread
81     */
82     public void testGetLazySet() {
83     AtomicDouble at = new AtomicDouble(1.0);
84     assertBitEquals(1.0, at.get());
85     for (double x : VALUES) {
86     at.lazySet(x);
87     assertBitEquals(x, at.get());
88     }
89     }
90    
91     /**
92     * compareAndSet succeeds in changing value if equal to expected else fails
93     */
94     public void testCompareAndSet() {
95     double prev = Math.E;
96     double unused = Math.E + Math.PI;
97     AtomicDouble at = new AtomicDouble(prev);
98     for (double x : VALUES) {
99     assertBitEquals(prev, at.get());
100     assertFalse(at.compareAndSet(unused, x));
101     assertBitEquals(prev, at.get());
102     assertTrue(at.compareAndSet(prev, x));
103     assertBitEquals(x, at.get());
104     prev = x;
105     }
106     }
107    
108     /**
109     * compareAndSet in one thread enables another waiting for value
110     * to succeed
111     */
112     public void testCompareAndSetInMultipleThreads() throws Exception {
113     final AtomicDouble at = new AtomicDouble(1.0);
114     Thread t = new Thread(new CheckedRunnable() {
115     public void realRun() {
116     while (!at.compareAndSet(2.0, 3.0))
117     Thread.yield();
118     }});
119    
120     t.start();
121     assertTrue(at.compareAndSet(1.0, 2.0));
122     t.join(LONG_DELAY_MS);
123     assertFalse(t.isAlive());
124     assertBitEquals(3.0, at.get());
125     }
126    
127     /**
128     * repeated weakCompareAndSet succeeds in changing value when equal
129     * to expected
130     */
131     public void testWeakCompareAndSet() {
132     double prev = Math.E;
133     double unused = Math.E + Math.PI;
134     AtomicDouble at = new AtomicDouble(prev);
135     for (double x : VALUES) {
136     assertBitEquals(prev, at.get());
137 jsr166 1.2 assertFalse(at.weakCompareAndSet(unused, x));
138 jsr166 1.1 assertBitEquals(prev, at.get());
139     while (!at.weakCompareAndSet(prev, x))
140     ;
141     assertBitEquals(x, at.get());
142     prev = x;
143     }
144     }
145    
146     /**
147     * getAndSet returns previous value and sets to given value
148     */
149     public void testGetAndSet() {
150     double prev = Math.E;
151     double unused = Math.E + Math.PI;
152     AtomicDouble at = new AtomicDouble(prev);
153     for (double x : VALUES) {
154     assertBitEquals(prev, at.getAndSet(x));
155     prev = x;
156     }
157     }
158    
159     /**
160     * getAndAdd returns previous value and adds given value
161     */
162     public void testGetAndAdd() {
163     for (double x : VALUES) {
164     for (double y : VALUES) {
165     AtomicDouble a = new AtomicDouble(x);
166     double z = a.getAndAdd(y);
167     assertBitEquals(x, z);
168     assertBitEquals(x + y, a.get());
169     }
170     }
171     }
172    
173     /**
174     * addAndGet adds given value to current, and returns current value
175     */
176     public void testAddAndGet() {
177     for (double x : VALUES) {
178     for (double y : VALUES) {
179     AtomicDouble a = new AtomicDouble(x);
180     double z = a.addAndGet(y);
181     assertBitEquals(x + y, z);
182     assertBitEquals(x + y, a.get());
183     }
184     }
185     }
186    
187     /**
188     * a deserialized serialized atomic holds same value
189     */
190     public void testSerialization() throws Exception {
191     AtomicDouble a = new AtomicDouble();
192     AtomicDouble b = serialClone(a);
193     assertTrue(a != b);
194     a.set(-22.0);
195     AtomicDouble c = serialClone(a);
196     assertBitEquals(-22.0, a.get());
197     assertBitEquals(0.0, b.get());
198     assertBitEquals(-22.0, c.get());
199     for (double x : VALUES) {
200     AtomicDouble d = new AtomicDouble(x);
201     assertBitEquals(serialClone(d).get(), d.get());
202     }
203     }
204    
205     /**
206     * toString returns current value.
207     */
208     public void testToString() {
209     AtomicDouble at = new AtomicDouble();
210     assertEquals("0.0", at.toString());
211     for (double x : VALUES) {
212     at.set(x);
213     assertEquals(Double.toString(x), at.toString());
214     }
215     }
216    
217     /**
218     * intValue returns current value.
219     */
220     public void testIntValue() {
221     AtomicDouble at = new AtomicDouble();
222     assertEquals(0, at.intValue());
223     for (double x : VALUES) {
224     at.set(x);
225     assertEquals((int)x, at.intValue());
226     }
227     }
228    
229     /**
230     * longValue returns current value.
231     */
232     public void testLongValue() {
233     AtomicDouble at = new AtomicDouble();
234     assertEquals(0L, at.longValue());
235     for (double x : VALUES) {
236     at.set(x);
237     assertEquals((long)x, at.longValue());
238     }
239     }
240    
241     /**
242     * floatValue returns current value.
243     */
244     public void testFloatValue() {
245     AtomicDouble at = new AtomicDouble();
246     assertEquals(0.0f, at.floatValue());
247     for (double x : VALUES) {
248     at.set(x);
249     assertEquals((float)x, at.floatValue());
250     }
251     }
252    
253     /**
254     * doubleValue returns current value.
255     */
256     public void testDoubleValue() {
257     AtomicDouble at = new AtomicDouble();
258     assertEquals(0.0d, at.doubleValue());
259     for (double x : VALUES) {
260     at.set(x);
261     assertBitEquals(x, at.doubleValue());
262     }
263     }
264    
265     /**
266 jsr166 1.3 * compareAndSet treats +0.0 and -0.0 as distinct values
267 jsr166 1.1 */
268     public void testDistinctZeros() {
269     AtomicDouble at = new AtomicDouble(+0.0);
270     assertFalse(at.compareAndSet(-0.0, 7.0));
271     assertFalse(at.weakCompareAndSet(-0.0, 7.0));
272     assertBitEquals(+0.0, at.get());
273     assertTrue(at.compareAndSet(+0.0, -0.0));
274     assertBitEquals(-0.0, at.get());
275     assertFalse(at.compareAndSet(+0.0, 7.0));
276     assertFalse(at.weakCompareAndSet(+0.0, 7.0));
277     assertBitEquals(-0.0, at.get());
278     }
279    
280     }