• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 /*
9  * Source:
10  * http://gee.cs.oswego.edu/cgi-bin/viewcvs.cgi/jsr166/src/test/tck-jsr166e/AtomicDoubleTest.java?revision=1.8
11  * (Modified to adapt to guava coding conventions)
12  */
13 
14 package com.google.common.util.concurrent;
15 
16 /** Unit test for {@link AtomicDouble}. */
17 public class AtomicDoubleTest extends JSR166TestCase {
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 */
bitEquals(double x, double y)41   static boolean bitEquals(double x, double y) {
42     return Double.doubleToRawLongBits(x) == Double.doubleToRawLongBits(y);
43   }
44 
assertBitEquals(double x, double y)45   static void assertBitEquals(double x, double y) {
46     assertEquals(Double.doubleToRawLongBits(x), Double.doubleToRawLongBits(y));
47   }
48 
49   /** constructor initializes to given value */
testConstructor()50   public void testConstructor() {
51     for (double x : VALUES) {
52       AtomicDouble a = new AtomicDouble(x);
53       assertBitEquals(x, a.get());
54     }
55   }
56 
57   /** default constructed initializes to zero */
testConstructor2()58   public void testConstructor2() {
59     AtomicDouble a = new AtomicDouble();
60     assertBitEquals(0.0, a.get());
61   }
62 
63   /** get returns the last value set */
testGetSet()64   public void testGetSet() {
65     AtomicDouble at = new AtomicDouble(1.0);
66     assertBitEquals(1.0, at.get());
67     for (double x : VALUES) {
68       at.set(x);
69       assertBitEquals(x, at.get());
70     }
71   }
72 
73   /** get returns the last value lazySet in same thread */
testGetLazySet()74   public void testGetLazySet() {
75     AtomicDouble at = new AtomicDouble(1.0);
76     assertBitEquals(1.0, at.get());
77     for (double x : VALUES) {
78       at.lazySet(x);
79       assertBitEquals(x, at.get());
80     }
81   }
82 
83   /** compareAndSet succeeds in changing value if equal to expected else fails */
testCompareAndSet()84   public void testCompareAndSet() {
85     double prev = Math.E;
86     double unused = Math.E + Math.PI;
87     AtomicDouble at = new AtomicDouble(prev);
88     for (double x : VALUES) {
89       assertBitEquals(prev, at.get());
90       assertFalse(at.compareAndSet(unused, x));
91       assertBitEquals(prev, at.get());
92       assertTrue(at.compareAndSet(prev, x));
93       assertBitEquals(x, at.get());
94       prev = x;
95     }
96   }
97 
98   /** compareAndSet in one thread enables another waiting for value to succeed */
testCompareAndSetInMultipleThreads()99   public void testCompareAndSetInMultipleThreads() throws Exception {
100     final AtomicDouble at = new AtomicDouble(1.0);
101     Thread t =
102         newStartedThread(
103             new CheckedRunnable() {
104               public void realRun() {
105                 while (!at.compareAndSet(2.0, 3.0)) {
106                   Thread.yield();
107                 }
108               }
109             });
110 
111     assertTrue(at.compareAndSet(1.0, 2.0));
112     awaitTermination(t);
113     assertBitEquals(3.0, at.get());
114   }
115 
116   /** repeated weakCompareAndSet succeeds in changing value when equal to expected */
testWeakCompareAndSet()117   public void testWeakCompareAndSet() {
118     double prev = Math.E;
119     double unused = Math.E + Math.PI;
120     AtomicDouble at = new AtomicDouble(prev);
121     for (double x : VALUES) {
122       assertBitEquals(prev, at.get());
123       assertFalse(at.weakCompareAndSet(unused, x));
124       assertBitEquals(prev, at.get());
125       while (!at.weakCompareAndSet(prev, x)) {;
126       }
127       assertBitEquals(x, at.get());
128       prev = x;
129     }
130   }
131 
132   /** getAndSet returns previous value and sets to given value */
testGetAndSet()133   public void testGetAndSet() {
134     double prev = Math.E;
135     AtomicDouble at = new AtomicDouble(prev);
136     for (double x : VALUES) {
137       assertBitEquals(prev, at.getAndSet(x));
138       prev = x;
139     }
140   }
141 
142   /** getAndAdd returns previous value and adds given value */
testGetAndAdd()143   public void testGetAndAdd() {
144     for (double x : VALUES) {
145       for (double y : VALUES) {
146         AtomicDouble a = new AtomicDouble(x);
147         double z = a.getAndAdd(y);
148         assertBitEquals(x, z);
149         assertBitEquals(x + y, a.get());
150       }
151     }
152   }
153 
154   /** addAndGet adds given value to current, and returns current value */
testAddAndGet()155   public void testAddAndGet() {
156     for (double x : VALUES) {
157       for (double y : VALUES) {
158         AtomicDouble a = new AtomicDouble(x);
159         double z = a.addAndGet(y);
160         assertBitEquals(x + y, z);
161         assertBitEquals(x + y, a.get());
162       }
163     }
164   }
165 
166   /** a deserialized serialized atomic holds same value */
testSerialization()167   public void testSerialization() throws Exception {
168     AtomicDouble a = new AtomicDouble();
169     AtomicDouble b = serialClone(a);
170     assertNotSame(a, b);
171     a.set(-22.0);
172     AtomicDouble c = serialClone(a);
173     assertNotSame(b, c);
174     assertBitEquals(-22.0, a.get());
175     assertBitEquals(0.0, b.get());
176     assertBitEquals(-22.0, c.get());
177     for (double x : VALUES) {
178       AtomicDouble d = new AtomicDouble(x);
179       assertBitEquals(serialClone(d).get(), d.get());
180     }
181   }
182 
183   /** toString returns current value */
testToString()184   public void testToString() {
185     AtomicDouble at = new AtomicDouble();
186     assertEquals("0.0", at.toString());
187     for (double x : VALUES) {
188       at.set(x);
189       assertEquals(Double.toString(x), at.toString());
190     }
191   }
192 
193   /** intValue returns current value. */
testIntValue()194   public void testIntValue() {
195     AtomicDouble at = new AtomicDouble();
196     assertEquals(0, at.intValue());
197     for (double x : VALUES) {
198       at.set(x);
199       assertEquals((int) x, at.intValue());
200     }
201   }
202 
203   /** longValue returns current value. */
testLongValue()204   public void testLongValue() {
205     AtomicDouble at = new AtomicDouble();
206     assertEquals(0L, at.longValue());
207     for (double x : VALUES) {
208       at.set(x);
209       assertEquals((long) x, at.longValue());
210     }
211   }
212 
213   /** floatValue returns current value. */
testFloatValue()214   public void testFloatValue() {
215     AtomicDouble at = new AtomicDouble();
216     assertEquals(0.0f, at.floatValue());
217     for (double x : VALUES) {
218       at.set(x);
219       assertEquals((float) x, at.floatValue());
220     }
221   }
222 
223   /** doubleValue returns current value. */
testDoubleValue()224   public void testDoubleValue() {
225     AtomicDouble at = new AtomicDouble();
226     assertEquals(0.0d, at.doubleValue());
227     for (double x : VALUES) {
228       at.set(x);
229       assertBitEquals(x, at.doubleValue());
230     }
231   }
232 
233   /** compareAndSet treats +0.0 and -0.0 as distinct values */
testDistinctZeros()234   public void testDistinctZeros() {
235     AtomicDouble at = new AtomicDouble(+0.0);
236     assertFalse(at.compareAndSet(-0.0, 7.0));
237     assertFalse(at.weakCompareAndSet(-0.0, 7.0));
238     assertBitEquals(+0.0, at.get());
239     assertTrue(at.compareAndSet(+0.0, -0.0));
240     assertBitEquals(-0.0, at.get());
241     assertFalse(at.compareAndSet(+0.0, 7.0));
242     assertFalse(at.weakCompareAndSet(+0.0, 7.0));
243     assertBitEquals(-0.0, at.get());
244   }
245 }
246