1 /* 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3 * 4 * This code is free software; you can redistribute it and/or modify it 5 * under the terms of the GNU General Public License version 2 only, as 6 * published by the Free Software Foundation. 7 * 8 * This code is distributed in the hope that it will be useful, but WITHOUT 9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 11 * version 2 for more details (a copy is included in the LICENSE file that 12 * accompanied this code). 13 * 14 * You should have received a copy of the GNU General Public License version 15 * 2 along with this work; if not, write to the Free Software Foundation, 16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 17 * 18 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 19 * or visit www.oracle.com if you need additional information or have any 20 * questions. 21 */ 22 23 /* 24 * This file is available under and governed by the GNU General Public 25 * License version 2 only, as published by the Free Software Foundation. 26 * However, the following notice accompanied the original version of this 27 * file: 28 * 29 * Written by Doug Lea with assistance from members of JCP JSR-166 30 * Expert Group and released to the public domain, as explained at 31 * http://creativecommons.org/publicdomain/zero/1.0/ 32 */ 33 34 import java.util.concurrent.atomic.AtomicLong; 35 36 import junit.framework.Test; 37 import junit.framework.TestSuite; 38 39 public class AtomicLong9Test extends JSR166TestCase { main(String[] args)40 public static void main(String[] args) { 41 main(suite(), args); 42 } suite()43 public static Test suite() { 44 return new TestSuite(AtomicLong9Test.class); 45 } 46 47 /** 48 * getPlain returns the last value set 49 */ testGetPlainSet()50 public void testGetPlainSet() { 51 AtomicLong ai = new AtomicLong(1); 52 assertEquals(1, ai.getPlain()); 53 ai.set(2); 54 assertEquals(2, ai.getPlain()); 55 ai.set(-3); 56 assertEquals(-3, ai.getPlain()); 57 } 58 59 /** 60 * getOpaque returns the last value set 61 */ testGetOpaqueSet()62 public void testGetOpaqueSet() { 63 AtomicLong ai = new AtomicLong(1); 64 assertEquals(1, ai.getOpaque()); 65 ai.set(2); 66 assertEquals(2, ai.getOpaque()); 67 ai.set(-3); 68 assertEquals(-3, ai.getOpaque()); 69 } 70 71 /** 72 * getAcquire returns the last value set 73 */ testGetAcquireSet()74 public void testGetAcquireSet() { 75 AtomicLong ai = new AtomicLong(1); 76 assertEquals(1, ai.getAcquire()); 77 ai.set(2); 78 assertEquals(2, ai.getAcquire()); 79 ai.set(-3); 80 assertEquals(-3, ai.getAcquire()); 81 } 82 83 /** 84 * get returns the last value setPlain 85 */ testGetSetPlain()86 public void testGetSetPlain() { 87 AtomicLong ai = new AtomicLong(1); 88 assertEquals(1, ai.get()); 89 ai.setPlain(2); 90 assertEquals(2, ai.get()); 91 ai.setPlain(-3); 92 assertEquals(-3, ai.get()); 93 } 94 95 /** 96 * get returns the last value setOpaque 97 */ testGetSetOpaque()98 public void testGetSetOpaque() { 99 AtomicLong ai = new AtomicLong(1); 100 assertEquals(1, ai.get()); 101 ai.setOpaque(2); 102 assertEquals(2, ai.get()); 103 ai.setOpaque(-3); 104 assertEquals(-3, ai.get()); 105 } 106 107 /** 108 * get returns the last value setRelease 109 */ testGetSetRelease()110 public void testGetSetRelease() { 111 AtomicLong ai = new AtomicLong(1); 112 assertEquals(1, ai.get()); 113 ai.setRelease(2); 114 assertEquals(2, ai.get()); 115 ai.setRelease(-3); 116 assertEquals(-3, ai.get()); 117 } 118 119 /** 120 * compareAndExchange succeeds in changing value if equal to 121 * expected else fails 122 */ testCompareAndExchange()123 public void testCompareAndExchange() { 124 AtomicLong ai = new AtomicLong(1); 125 assertEquals(1, ai.compareAndExchange(1, 2)); 126 assertEquals(2, ai.compareAndExchange(2, -4)); 127 assertEquals(-4, ai.get()); 128 assertEquals(-4, ai.compareAndExchange(-5, 7)); 129 assertEquals(-4, ai.get()); 130 assertEquals(-4, ai.compareAndExchange(-4, 7)); 131 assertEquals(7, ai.get()); 132 } 133 134 /** 135 * compareAndExchangeAcquire succeeds in changing value if equal to 136 * expected else fails 137 */ testCompareAndExchangeAcquire()138 public void testCompareAndExchangeAcquire() { 139 AtomicLong ai = new AtomicLong(1); 140 assertEquals(1, ai.compareAndExchangeAcquire(1, 2)); 141 assertEquals(2, ai.compareAndExchangeAcquire(2, -4)); 142 assertEquals(-4, ai.get()); 143 assertEquals(-4, ai.compareAndExchangeAcquire(-5, 7)); 144 assertEquals(-4, ai.get()); 145 assertEquals(-4, ai.compareAndExchangeAcquire(-4, 7)); 146 assertEquals(7, ai.get()); 147 } 148 149 /** 150 * compareAndExchangeRelease succeeds in changing value if equal to 151 * expected else fails 152 */ testCompareAndExchangeRelease()153 public void testCompareAndExchangeRelease() { 154 AtomicLong ai = new AtomicLong(1); 155 assertEquals(1, ai.compareAndExchangeRelease(1, 2)); 156 assertEquals(2, ai.compareAndExchangeRelease(2, -4)); 157 assertEquals(-4, ai.get()); 158 assertEquals(-4, ai.compareAndExchangeRelease(-5, 7)); 159 assertEquals(-4, ai.get()); 160 assertEquals(-4, ai.compareAndExchangeRelease(-4, 7)); 161 assertEquals(7, ai.get()); 162 } 163 164 /** 165 * repeated weakCompareAndSetPlain succeeds in changing value when equal 166 * to expected 167 */ testWeakCompareAndSetPlain()168 public void testWeakCompareAndSetPlain() { 169 AtomicLong ai = new AtomicLong(1); 170 do {} while (!ai.weakCompareAndSetPlain(1, 2)); 171 do {} while (!ai.weakCompareAndSetPlain(2, -4)); 172 assertEquals(-4, ai.get()); 173 do {} while (!ai.weakCompareAndSetPlain(-4, 7)); 174 assertEquals(7, ai.get()); 175 } 176 177 /** 178 * repeated weakCompareAndSetVolatile succeeds in changing value when equal 179 * to expected 180 */ testWeakCompareAndSetVolatile()181 public void testWeakCompareAndSetVolatile() { 182 AtomicLong ai = new AtomicLong(1); 183 do {} while (!ai.weakCompareAndSetVolatile(1, 2)); 184 do {} while (!ai.weakCompareAndSetVolatile(2, -4)); 185 assertEquals(-4, ai.get()); 186 do {} while (!ai.weakCompareAndSetVolatile(-4, 7)); 187 assertEquals(7, ai.get()); 188 } 189 190 /** 191 * repeated weakCompareAndSetAcquire succeeds in changing value when equal 192 * to expected 193 */ testWeakCompareAndSetAcquire()194 public void testWeakCompareAndSetAcquire() { 195 AtomicLong ai = new AtomicLong(1); 196 do {} while (!ai.weakCompareAndSetAcquire(1, 2)); 197 do {} while (!ai.weakCompareAndSetAcquire(2, -4)); 198 assertEquals(-4, ai.get()); 199 do {} while (!ai.weakCompareAndSetAcquire(-4, 7)); 200 assertEquals(7, ai.get()); 201 } 202 203 /** 204 * repeated weakCompareAndSetRelease succeeds in changing value when equal 205 * to expected 206 */ testWeakCompareAndSetRelease()207 public void testWeakCompareAndSetRelease() { 208 AtomicLong ai = new AtomicLong(1); 209 do {} while (!ai.weakCompareAndSetRelease(1, 2)); 210 do {} while (!ai.weakCompareAndSetRelease(2, -4)); 211 assertEquals(-4, ai.get()); 212 do {} while (!ai.weakCompareAndSetRelease(-4, 7)); 213 assertEquals(7, ai.get()); 214 } 215 216 } 217