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