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.AtomicReferenceArray; 35 36 import junit.framework.Test; 37 import junit.framework.TestSuite; 38 39 public class AtomicReferenceArray9Test 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(AtomicReferenceArray9Test.class); 45 } 46 47 /** 48 * get and set for out of bound indices throw IndexOutOfBoundsException 49 */ testIndexing()50 public void testIndexing() { 51 AtomicReferenceArray<Integer> aa = new AtomicReferenceArray<>(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, null), 59 () -> aa.setOpaque(j, null), 60 () -> aa.setRelease(j, null), 61 () -> aa.compareAndExchange(j, null, null), 62 () -> aa.compareAndExchangeAcquire(j, null, null), 63 () -> aa.compareAndExchangeRelease(j, null, null), 64 () -> aa.weakCompareAndSetPlain(j, null, null), 65 () -> aa.weakCompareAndSetVolatile(j, null, null), 66 () -> aa.weakCompareAndSetAcquire(j, null, null), 67 () -> aa.weakCompareAndSetRelease(j, null, null), 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 AtomicReferenceArray<Integer> aa = new AtomicReferenceArray<>(SIZE); 79 for (int i = 0; i < SIZE; i++) { 80 aa.set(i, one); 81 assertEquals(one, aa.getPlain(i)); 82 aa.set(i, two); 83 assertEquals(two, aa.getPlain(i)); 84 aa.set(i, m3); 85 assertEquals(m3, aa.getPlain(i)); 86 } 87 } 88 89 /** 90 * getOpaque returns the last value set 91 */ testGetOpaqueSet()92 public void testGetOpaqueSet() { 93 AtomicReferenceArray<Integer> aa = new AtomicReferenceArray<>(SIZE); 94 for (int i = 0; i < SIZE; i++) { 95 aa.set(i, one); 96 assertEquals(one, aa.getOpaque(i)); 97 aa.set(i, two); 98 assertEquals(two, aa.getOpaque(i)); 99 aa.set(i, m3); 100 assertEquals(m3, aa.getOpaque(i)); 101 } 102 } 103 104 /** 105 * getAcquire returns the last value set 106 */ testGetAcquireSet()107 public void testGetAcquireSet() { 108 AtomicReferenceArray<Integer> aa = new AtomicReferenceArray<>(SIZE); 109 for (int i = 0; i < SIZE; i++) { 110 aa.set(i, one); 111 assertEquals(one, aa.getAcquire(i)); 112 aa.set(i, two); 113 assertEquals(two, aa.getAcquire(i)); 114 aa.set(i, m3); 115 assertEquals(m3, aa.getAcquire(i)); 116 } 117 } 118 119 /** 120 * get returns the last value setPlain 121 */ testGetSetPlain()122 public void testGetSetPlain() { 123 AtomicReferenceArray<Integer> aa = new AtomicReferenceArray<>(SIZE); 124 for (int i = 0; i < SIZE; i++) { 125 aa.setPlain(i, one); 126 assertEquals(one, aa.get(i)); 127 aa.setPlain(i, two); 128 assertEquals(two, aa.get(i)); 129 aa.setPlain(i, m3); 130 assertEquals(m3, aa.get(i)); 131 } 132 } 133 134 /** 135 * get returns the last value setOpaque 136 */ testGetSetOpaque()137 public void testGetSetOpaque() { 138 AtomicReferenceArray<Integer> aa = new AtomicReferenceArray<>(SIZE); 139 for (int i = 0; i < SIZE; i++) { 140 aa.setOpaque(i, one); 141 assertEquals(one, aa.get(i)); 142 aa.setOpaque(i, two); 143 assertEquals(two, aa.get(i)); 144 aa.setOpaque(i, m3); 145 assertEquals(m3, aa.get(i)); 146 } 147 } 148 149 /** 150 * get returns the last value setRelease 151 */ testGetSetRelease()152 public void testGetSetRelease() { 153 AtomicReferenceArray<Integer> aa = new AtomicReferenceArray<>(SIZE); 154 for (int i = 0; i < SIZE; i++) { 155 aa.setRelease(i, one); 156 assertEquals(one, aa.get(i)); 157 aa.setRelease(i, two); 158 assertEquals(two, aa.get(i)); 159 aa.setRelease(i, m3); 160 assertEquals(m3, 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 AtomicReferenceArray<Integer> aa = new AtomicReferenceArray<>(SIZE); 170 for (int i = 0; i < SIZE; i++) { 171 aa.set(i, one); 172 assertEquals(one, aa.compareAndExchange(i, one, two)); 173 assertEquals(two, aa.compareAndExchange(i, two, m4)); 174 assertEquals(m4, aa.get(i)); 175 assertEquals(m4, aa.compareAndExchange(i,m5, seven)); 176 assertEquals(m4, aa.get(i)); 177 assertEquals(m4, aa.compareAndExchange(i, m4, seven)); 178 assertEquals(seven, 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 AtomicReferenceArray<Integer> aa = new AtomicReferenceArray<>(SIZE); 188 for (int i = 0; i < SIZE; i++) { 189 aa.set(i, one); 190 assertEquals(one, aa.compareAndExchangeAcquire(i, one, two)); 191 assertEquals(two, aa.compareAndExchangeAcquire(i, two, m4)); 192 assertEquals(m4, aa.get(i)); 193 assertEquals(m4, aa.compareAndExchangeAcquire(i,m5, seven)); 194 assertEquals(m4, aa.get(i)); 195 assertEquals(m4, aa.compareAndExchangeAcquire(i, m4, seven)); 196 assertEquals(seven, 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 AtomicReferenceArray<Integer> aa = new AtomicReferenceArray<>(SIZE); 206 for (int i = 0; i < SIZE; i++) { 207 aa.set(i, one); 208 assertEquals(one, aa.compareAndExchangeRelease(i, one, two)); 209 assertEquals(two, aa.compareAndExchangeRelease(i, two, m4)); 210 assertEquals(m4, aa.get(i)); 211 assertEquals(m4, aa.compareAndExchangeRelease(i,m5, seven)); 212 assertEquals(m4, aa.get(i)); 213 assertEquals(m4, aa.compareAndExchangeRelease(i, m4, seven)); 214 assertEquals(seven, 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 AtomicReferenceArray<Integer> aa = new AtomicReferenceArray<>(SIZE); 224 for (int i = 0; i < SIZE; i++) { 225 aa.set(i, one); 226 do {} while (!aa.weakCompareAndSetPlain(i, one, two)); 227 do {} while (!aa.weakCompareAndSetPlain(i, two, m4)); 228 assertEquals(m4, aa.get(i)); 229 do {} while (!aa.weakCompareAndSetPlain(i, m4, seven)); 230 assertEquals(seven, 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 AtomicReferenceArray<Integer> aa = new AtomicReferenceArray<>(SIZE); 240 for (int i = 0; i < SIZE; i++) { 241 aa.set(i, one); 242 do {} while (!aa.weakCompareAndSetVolatile(i, one, two)); 243 do {} while (!aa.weakCompareAndSetVolatile(i, two, m4)); 244 assertEquals(m4, aa.get(i)); 245 do {} while (!aa.weakCompareAndSetVolatile(i, m4, seven)); 246 assertEquals(seven, 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 AtomicReferenceArray<Integer> aa = new AtomicReferenceArray<>(SIZE); 256 for (int i = 0; i < SIZE; i++) { 257 aa.set(i, one); 258 do {} while (!aa.weakCompareAndSetAcquire(i, one, two)); 259 do {} while (!aa.weakCompareAndSetAcquire(i, two, m4)); 260 assertEquals(m4, aa.get(i)); 261 do {} while (!aa.weakCompareAndSetAcquire(i, m4, seven)); 262 assertEquals(seven, 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 AtomicReferenceArray<Integer> aa = new AtomicReferenceArray<>(SIZE); 272 for (int i = 0; i < SIZE; i++) { 273 aa.set(i, one); 274 do {} while (!aa.weakCompareAndSetRelease(i, one, two)); 275 do {} while (!aa.weakCompareAndSetRelease(i, two, m4)); 276 assertEquals(m4, aa.get(i)); 277 do {} while (!aa.weakCompareAndSetRelease(i, m4, seven)); 278 assertEquals(seven, aa.get(i)); 279 } 280 } 281 282 } 283