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.AtomicLongArray; 35 36 import junit.framework.Test; 37 import junit.framework.TestSuite; 38 39 public class AtomicLongArray9Test 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(AtomicLongArray9Test.class); 45 } 46 47 /** 48 * get and set for out of bound indices throw IndexOutOfBoundsException 49 */ testIndexing()50 public void testIndexing() { 51 AtomicLongArray aa = new AtomicLongArray(SIZE); 52 for (int index : new int[] { -1, SIZE }) { 53 final int j = index; 54 final Runnable[] tasks = { 55 () -> aa.getPlain(j), 56 () -> aa.getOpaque(j), 57 () -> aa.getAcquire(j), 58 () -> aa.setPlain(j, 1), 59 () -> aa.setOpaque(j, 1), 60 () -> aa.setRelease(j, 1), 61 () -> aa.compareAndExchange(j, 1, 2), 62 () -> aa.compareAndExchangeAcquire(j, 1, 2), 63 () -> aa.compareAndExchangeRelease(j, 1, 2), 64 () -> aa.weakCompareAndSetPlain(j, 1, 2), 65 () -> aa.weakCompareAndSetVolatile(j, 1, 2), 66 () -> aa.weakCompareAndSetAcquire(j, 1, 2), 67 () -> aa.weakCompareAndSetRelease(j, 1, 2), 68 }; 69 70 assertThrows(IndexOutOfBoundsException.class, tasks); 71 } 72 } 73 74 /** 75 * getPlain returns the last value set 76 */ testGetPlainSet()77 public void testGetPlainSet() { 78 AtomicLongArray aa = new AtomicLongArray(SIZE); 79 for (int i = 0; i < SIZE; i++) { 80 aa.set(i, 1); 81 assertEquals(1, aa.getPlain(i)); 82 aa.set(i, 2); 83 assertEquals(2, aa.getPlain(i)); 84 aa.set(i, -3); 85 assertEquals(-3, aa.getPlain(i)); 86 } 87 } 88 89 /** 90 * getOpaque returns the last value set 91 */ testGetOpaqueSet()92 public void testGetOpaqueSet() { 93 AtomicLongArray aa = new AtomicLongArray(SIZE); 94 for (int i = 0; i < SIZE; i++) { 95 aa.set(i, 1); 96 assertEquals(1, aa.getOpaque(i)); 97 aa.set(i, 2); 98 assertEquals(2, aa.getOpaque(i)); 99 aa.set(i, -3); 100 assertEquals(-3, aa.getOpaque(i)); 101 } 102 } 103 104 /** 105 * getAcquire returns the last value set 106 */ testGetAcquireSet()107 public void testGetAcquireSet() { 108 AtomicLongArray aa = new AtomicLongArray(SIZE); 109 for (int i = 0; i < SIZE; i++) { 110 aa.set(i, 1); 111 assertEquals(1, aa.getAcquire(i)); 112 aa.set(i, 2); 113 assertEquals(2, aa.getAcquire(i)); 114 aa.set(i, -3); 115 assertEquals(-3, aa.getAcquire(i)); 116 } 117 } 118 119 /** 120 * get returns the last value setPlain 121 */ testGetSetPlain()122 public void testGetSetPlain() { 123 AtomicLongArray aa = new AtomicLongArray(SIZE); 124 for (int i = 0; i < SIZE; i++) { 125 aa.setPlain(i, 1); 126 assertEquals(1, aa.get(i)); 127 aa.setPlain(i, 2); 128 assertEquals(2, aa.get(i)); 129 aa.setPlain(i, -3); 130 assertEquals(-3, aa.get(i)); 131 } 132 } 133 134 /** 135 * get returns the last value setOpaque 136 */ testGetSetOpaque()137 public void testGetSetOpaque() { 138 AtomicLongArray aa = new AtomicLongArray(SIZE); 139 for (int i = 0; i < SIZE; i++) { 140 aa.setOpaque(i, 1); 141 assertEquals(1, aa.get(i)); 142 aa.setOpaque(i, 2); 143 assertEquals(2, aa.get(i)); 144 aa.setOpaque(i, -3); 145 assertEquals(-3, aa.get(i)); 146 } 147 } 148 149 /** 150 * get returns the last value setRelease 151 */ testGetSetRelease()152 public void testGetSetRelease() { 153 AtomicLongArray aa = new AtomicLongArray(SIZE); 154 for (int i = 0; i < SIZE; i++) { 155 aa.setRelease(i, 1); 156 assertEquals(1, aa.get(i)); 157 aa.setRelease(i, 2); 158 assertEquals(2, aa.get(i)); 159 aa.setRelease(i, -3); 160 assertEquals(-3, aa.get(i)); 161 } 162 } 163 164 /** 165 * compareAndExchange succeeds in changing value if equal to 166 * expected else fails 167 */ testCompareAndExchange()168 public void testCompareAndExchange() { 169 AtomicLongArray aa = new AtomicLongArray(SIZE); 170 for (int i = 0; i < SIZE; i++) { 171 aa.set(i, 1); 172 assertEquals(1, aa.compareAndExchange(i, 1, 2)); 173 assertEquals(2, aa.compareAndExchange(i, 2, -4)); 174 assertEquals(-4, aa.get(i)); 175 assertEquals(-4, aa.compareAndExchange(i,-5, 7)); 176 assertEquals(-4, aa.get(i)); 177 assertEquals(-4, aa.compareAndExchange(i, -4, 7)); 178 assertEquals(7, aa.get(i)); 179 } 180 } 181 182 /** 183 * compareAndExchangeAcquire succeeds in changing value if equal to 184 * expected else fails 185 */ testCompareAndExchangeAcquire()186 public void testCompareAndExchangeAcquire() { 187 AtomicLongArray aa = new AtomicLongArray(SIZE); 188 for (int i = 0; i < SIZE; i++) { 189 aa.set(i, 1); 190 assertEquals(1, aa.compareAndExchangeAcquire(i, 1, 2)); 191 assertEquals(2, aa.compareAndExchangeAcquire(i, 2, -4)); 192 assertEquals(-4, aa.get(i)); 193 assertEquals(-4, aa.compareAndExchangeAcquire(i,-5, 7)); 194 assertEquals(-4, aa.get(i)); 195 assertEquals(-4, aa.compareAndExchangeAcquire(i, -4, 7)); 196 assertEquals(7, aa.get(i)); 197 } 198 } 199 200 /** 201 * compareAndExchangeRelease succeeds in changing value if equal to 202 * expected else fails 203 */ testCompareAndExchangeRelease()204 public void testCompareAndExchangeRelease() { 205 AtomicLongArray aa = new AtomicLongArray(SIZE); 206 for (int i = 0; i < SIZE; i++) { 207 aa.set(i, 1); 208 assertEquals(1, aa.compareAndExchangeRelease(i, 1, 2)); 209 assertEquals(2, aa.compareAndExchangeRelease(i, 2, -4)); 210 assertEquals(-4, aa.get(i)); 211 assertEquals(-4, aa.compareAndExchangeRelease(i,-5, 7)); 212 assertEquals(-4, aa.get(i)); 213 assertEquals(-4, aa.compareAndExchangeRelease(i, -4, 7)); 214 assertEquals(7, aa.get(i)); 215 } 216 } 217 218 /** 219 * repeated weakCompareAndSetPlain succeeds in changing value when equal 220 * to expected 221 */ testWeakCompareAndSetPlain()222 public void testWeakCompareAndSetPlain() { 223 AtomicLongArray aa = new AtomicLongArray(SIZE); 224 for (int i = 0; i < SIZE; i++) { 225 aa.set(i, 1); 226 do {} while (!aa.weakCompareAndSetPlain(i, 1, 2)); 227 do {} while (!aa.weakCompareAndSetPlain(i, 2, -4)); 228 assertEquals(-4, aa.get(i)); 229 do {} while (!aa.weakCompareAndSetPlain(i, -4, 7)); 230 assertEquals(7, aa.get(i)); 231 } 232 } 233 234 /** 235 * repeated weakCompareAndSetVolatile succeeds in changing value when equal 236 * to expected 237 */ testWeakCompareAndSetVolatile()238 public void testWeakCompareAndSetVolatile() { 239 AtomicLongArray aa = new AtomicLongArray(SIZE); 240 for (int i = 0; i < SIZE; i++) { 241 aa.set(i, 1); 242 do {} while (!aa.weakCompareAndSetVolatile(i, 1, 2)); 243 do {} while (!aa.weakCompareAndSetVolatile(i, 2, -4)); 244 assertEquals(-4, aa.get(i)); 245 do {} while (!aa.weakCompareAndSetVolatile(i, -4, 7)); 246 assertEquals(7, aa.get(i)); 247 } 248 } 249 250 /** 251 * repeated weakCompareAndSetAcquire succeeds in changing value when equal 252 * to expected 253 */ testWeakCompareAndSetAcquire()254 public void testWeakCompareAndSetAcquire() { 255 AtomicLongArray aa = new AtomicLongArray(SIZE); 256 for (int i = 0; i < SIZE; i++) { 257 aa.set(i, 1); 258 do {} while (!aa.weakCompareAndSetAcquire(i, 1, 2)); 259 do {} while (!aa.weakCompareAndSetAcquire(i, 2, -4)); 260 assertEquals(-4, aa.get(i)); 261 do {} while (!aa.weakCompareAndSetAcquire(i, -4, 7)); 262 assertEquals(7, aa.get(i)); 263 } 264 } 265 266 /** 267 * repeated weakCompareAndSetRelease succeeds in changing value when equal 268 * to expected 269 */ testWeakCompareAndSetRelease()270 public void testWeakCompareAndSetRelease() { 271 AtomicLongArray aa = new AtomicLongArray(SIZE); 272 for (int i = 0; i < SIZE; i++) { 273 aa.set(i, 1); 274 do {} while (!aa.weakCompareAndSetRelease(i, 1, 2)); 275 do {} while (!aa.weakCompareAndSetRelease(i, 2, -4)); 276 assertEquals(-4, aa.get(i)); 277 do {} while (!aa.weakCompareAndSetRelease(i, -4, 7)); 278 assertEquals(7, aa.get(i)); 279 } 280 } 281 282 } 283