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