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