Calin Juravle | 8f0d92b | 2013-08-01 17:26:00 +0100 | [diff] [blame] | 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 | |
Calin Juravle | 8f0d92b | 2013-08-01 17:26:00 +0100 | [diff] [blame] | 11 | import java.util.concurrent.atomic.AtomicBoolean; |
| 12 | |
Narayan Kamath | 8e9a0e9 | 2015-04-28 11:40:00 +0100 | [diff] [blame] | 13 | import junit.framework.Test; |
| 14 | import junit.framework.TestSuite; |
| 15 | |
Calin Juravle | 8f0d92b | 2013-08-01 17:26:00 +0100 | [diff] [blame] | 16 | public class AtomicBooleanTest extends JSR166TestCase { |
Narayan Kamath | 8e9a0e9 | 2015-04-28 11:40:00 +0100 | [diff] [blame] | 17 | // android-note: Removed because the CTS runner does a bad job of |
| 18 | // retrying tests that have suite() declarations. |
| 19 | // |
| 20 | // public static void main(String[] args) { |
| 21 | // main(suite(), args); |
| 22 | // } |
| 23 | // public static Test suite() { |
Przemyslaw Szczepaniak | b8b7511 | 2016-03-11 15:59:10 +0000 | [diff] [blame] | 24 | // return new TestSuite(AtomicBooleanTest.class); |
Narayan Kamath | 8e9a0e9 | 2015-04-28 11:40:00 +0100 | [diff] [blame] | 25 | // } |
Calin Juravle | 8f0d92b | 2013-08-01 17:26:00 +0100 | [diff] [blame] | 26 | |
| 27 | /** |
| 28 | * constructor initializes to given value |
| 29 | */ |
| 30 | public void testConstructor() { |
| 31 | assertTrue(new AtomicBoolean(true).get()); |
| 32 | assertFalse(new AtomicBoolean(false).get()); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * default constructed initializes to false |
| 37 | */ |
| 38 | public void testConstructor2() { |
| 39 | AtomicBoolean ai = new AtomicBoolean(); |
| 40 | assertFalse(ai.get()); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * get returns the last value set |
| 45 | */ |
| 46 | public void testGetSet() { |
| 47 | AtomicBoolean ai = new AtomicBoolean(true); |
| 48 | assertTrue(ai.get()); |
| 49 | ai.set(false); |
| 50 | assertFalse(ai.get()); |
| 51 | ai.set(true); |
| 52 | assertTrue(ai.get()); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * get returns the last value lazySet in same thread |
| 57 | */ |
| 58 | public void testGetLazySet() { |
| 59 | AtomicBoolean ai = new AtomicBoolean(true); |
| 60 | assertTrue(ai.get()); |
| 61 | ai.lazySet(false); |
| 62 | assertFalse(ai.get()); |
| 63 | ai.lazySet(true); |
| 64 | assertTrue(ai.get()); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * compareAndSet succeeds in changing value if equal to expected else fails |
| 69 | */ |
| 70 | public void testCompareAndSet() { |
| 71 | AtomicBoolean ai = new AtomicBoolean(true); |
| 72 | assertTrue(ai.compareAndSet(true, false)); |
| 73 | assertFalse(ai.get()); |
| 74 | assertTrue(ai.compareAndSet(false, false)); |
| 75 | assertFalse(ai.get()); |
| 76 | assertFalse(ai.compareAndSet(true, false)); |
| 77 | assertFalse(ai.get()); |
| 78 | assertTrue(ai.compareAndSet(false, true)); |
| 79 | assertTrue(ai.get()); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * compareAndSet in one thread enables another waiting for value |
| 84 | * to succeed |
| 85 | */ |
| 86 | public void testCompareAndSetInMultipleThreads() throws Exception { |
| 87 | final AtomicBoolean ai = new AtomicBoolean(true); |
| 88 | Thread t = new Thread(new CheckedRunnable() { |
| 89 | public void realRun() { |
| 90 | while (!ai.compareAndSet(false, true)) Thread.yield(); |
| 91 | }}); |
| 92 | |
| 93 | t.start(); |
| 94 | assertTrue(ai.compareAndSet(true, false)); |
| 95 | t.join(LONG_DELAY_MS); |
| 96 | assertFalse(t.isAlive()); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * repeated weakCompareAndSet succeeds in changing value when equal |
| 101 | * to expected |
| 102 | */ |
| 103 | public void testWeakCompareAndSet() { |
| 104 | AtomicBoolean ai = new AtomicBoolean(true); |
Narayan Kamath | 8e9a0e9 | 2015-04-28 11:40:00 +0100 | [diff] [blame] | 105 | do {} while (!ai.weakCompareAndSet(true, false)); |
Calin Juravle | 8f0d92b | 2013-08-01 17:26:00 +0100 | [diff] [blame] | 106 | assertFalse(ai.get()); |
Narayan Kamath | 8e9a0e9 | 2015-04-28 11:40:00 +0100 | [diff] [blame] | 107 | do {} while (!ai.weakCompareAndSet(false, false)); |
Calin Juravle | 8f0d92b | 2013-08-01 17:26:00 +0100 | [diff] [blame] | 108 | assertFalse(ai.get()); |
Narayan Kamath | 8e9a0e9 | 2015-04-28 11:40:00 +0100 | [diff] [blame] | 109 | do {} while (!ai.weakCompareAndSet(false, true)); |
Calin Juravle | 8f0d92b | 2013-08-01 17:26:00 +0100 | [diff] [blame] | 110 | assertTrue(ai.get()); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * getAndSet returns previous value and sets to given value |
| 115 | */ |
| 116 | public void testGetAndSet() { |
| 117 | AtomicBoolean ai = new AtomicBoolean(true); |
| 118 | assertEquals(true, ai.getAndSet(false)); |
| 119 | assertEquals(false, ai.getAndSet(false)); |
| 120 | assertEquals(false, ai.getAndSet(true)); |
| 121 | assertTrue(ai.get()); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * a deserialized serialized atomic holds same value |
| 126 | */ |
| 127 | public void testSerialization() throws Exception { |
| 128 | AtomicBoolean x = new AtomicBoolean(); |
| 129 | AtomicBoolean y = serialClone(x); |
| 130 | x.set(true); |
| 131 | AtomicBoolean z = serialClone(x); |
| 132 | assertTrue(x.get()); |
| 133 | assertFalse(y.get()); |
| 134 | assertTrue(z.get()); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * toString returns current value. |
| 139 | */ |
| 140 | public void testToString() { |
| 141 | AtomicBoolean ai = new AtomicBoolean(); |
| 142 | assertEquals(Boolean.toString(false), ai.toString()); |
| 143 | ai.set(true); |
| 144 | assertEquals(Boolean.toString(true), ai.toString()); |
| 145 | } |
| 146 | |
| 147 | } |