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