1 /* 2 * Written by Doug Lea with assistance from members of JCP JSR-166 3 * Expert Group and released to the public domain, as explained at 4 * http://creativecommons.org/publicdomain/zero/1.0/ 5 * Other contributors include Andrew Wright, Jeffrey Hayes, 6 * Pat Fisher, Mike Judd. 7 */ 8 9 package jsr166; 10 11 import junit.framework.*; 12 import java.util.concurrent.atomic.AtomicBoolean; 13 14 public class AtomicBooleanTest extends JSR166TestCase { 15 16 /** 17 * constructor initializes to given value 18 */ testConstructor()19 public void testConstructor() { 20 assertTrue(new AtomicBoolean(true).get()); 21 assertFalse(new AtomicBoolean(false).get()); 22 } 23 24 /** 25 * default constructed initializes to false 26 */ testConstructor2()27 public void testConstructor2() { 28 AtomicBoolean ai = new AtomicBoolean(); 29 assertFalse(ai.get()); 30 } 31 32 /** 33 * get returns the last value set 34 */ testGetSet()35 public void testGetSet() { 36 AtomicBoolean ai = new AtomicBoolean(true); 37 assertTrue(ai.get()); 38 ai.set(false); 39 assertFalse(ai.get()); 40 ai.set(true); 41 assertTrue(ai.get()); 42 } 43 44 /** 45 * get returns the last value lazySet in same thread 46 */ testGetLazySet()47 public void testGetLazySet() { 48 AtomicBoolean ai = new AtomicBoolean(true); 49 assertTrue(ai.get()); 50 ai.lazySet(false); 51 assertFalse(ai.get()); 52 ai.lazySet(true); 53 assertTrue(ai.get()); 54 } 55 56 /** 57 * compareAndSet succeeds in changing value if equal to expected else fails 58 */ testCompareAndSet()59 public void testCompareAndSet() { 60 AtomicBoolean ai = new AtomicBoolean(true); 61 assertTrue(ai.compareAndSet(true, false)); 62 assertFalse(ai.get()); 63 assertTrue(ai.compareAndSet(false, false)); 64 assertFalse(ai.get()); 65 assertFalse(ai.compareAndSet(true, false)); 66 assertFalse(ai.get()); 67 assertTrue(ai.compareAndSet(false, true)); 68 assertTrue(ai.get()); 69 } 70 71 /** 72 * compareAndSet in one thread enables another waiting for value 73 * to succeed 74 */ testCompareAndSetInMultipleThreads()75 public void testCompareAndSetInMultipleThreads() throws Exception { 76 final AtomicBoolean ai = new AtomicBoolean(true); 77 Thread t = new Thread(new CheckedRunnable() { 78 public void realRun() { 79 while (!ai.compareAndSet(false, true)) Thread.yield(); 80 }}); 81 82 t.start(); 83 assertTrue(ai.compareAndSet(true, false)); 84 t.join(LONG_DELAY_MS); 85 assertFalse(t.isAlive()); 86 } 87 88 /** 89 * repeated weakCompareAndSet succeeds in changing value when equal 90 * to expected 91 */ testWeakCompareAndSet()92 public void testWeakCompareAndSet() { 93 AtomicBoolean ai = new AtomicBoolean(true); 94 while (!ai.weakCompareAndSet(true, false)); 95 assertFalse(ai.get()); 96 while (!ai.weakCompareAndSet(false, false)); 97 assertFalse(ai.get()); 98 while (!ai.weakCompareAndSet(false, true)); 99 assertTrue(ai.get()); 100 } 101 102 /** 103 * getAndSet returns previous value and sets to given value 104 */ testGetAndSet()105 public void testGetAndSet() { 106 AtomicBoolean ai = new AtomicBoolean(true); 107 assertEquals(true, ai.getAndSet(false)); 108 assertEquals(false, ai.getAndSet(false)); 109 assertEquals(false, ai.getAndSet(true)); 110 assertTrue(ai.get()); 111 } 112 113 /** 114 * a deserialized serialized atomic holds same value 115 */ testSerialization()116 public void testSerialization() throws Exception { 117 AtomicBoolean x = new AtomicBoolean(); 118 AtomicBoolean y = serialClone(x); 119 x.set(true); 120 AtomicBoolean z = serialClone(x); 121 assertTrue(x.get()); 122 assertFalse(y.get()); 123 assertTrue(z.get()); 124 } 125 126 /** 127 * toString returns current value. 128 */ testToString()129 public void testToString() { 130 AtomicBoolean ai = new AtomicBoolean(); 131 assertEquals(Boolean.toString(false), ai.toString()); 132 ai.set(true); 133 assertEquals(Boolean.toString(true), ai.toString()); 134 } 135 136 } 137