• 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 */
99 
testCompareAndSetInMultipleThreads()100   public void testCompareAndSetInMultipleThreads() throws Exception {
101     final AtomicDouble at = new AtomicDouble(1.0);
102     Thread t =
103         newStartedThread(
104             new CheckedRunnable() {
105               @Override
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