ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AtomicReferenceArray9Test.java
Revision: 1.5
Committed: Tue Jan 26 13:33:05 2021 UTC (3 years, 3 months ago) by dl
Branch: MAIN
CVS Tags: HEAD
Changes since 1.4: +52 -52 lines
Log Message:
Replace Integer with Item class

File Contents

# Content
1 /*
2 * Written by Doug Lea with assistance from members of JCP JSR-166
3 * Expert Group and released to the public domain, as explained at
4 * http://creativecommons.org/publicdomain/zero/1.0/
5 */
6
7 import java.util.concurrent.atomic.AtomicReferenceArray;
8
9 import junit.framework.Test;
10 import junit.framework.TestSuite;
11
12 public class AtomicReferenceArray9Test extends JSR166TestCase {
13 public static void main(String[] args) {
14 main(suite(), args);
15 }
16 public static Test suite() {
17 return new TestSuite(AtomicReferenceArray9Test.class);
18 }
19
20 /**
21 * get and set for out of bound indices throw IndexOutOfBoundsException
22 */
23 public void testIndexing() {
24 AtomicReferenceArray<Integer> aa = new AtomicReferenceArray<>(SIZE);
25 for (int index : new int[] { -1, SIZE }) {
26 final int j = index;
27 assertThrows(
28 IndexOutOfBoundsException.class,
29 () -> aa.getPlain(j),
30 () -> aa.getOpaque(j),
31 () -> aa.getAcquire(j),
32 () -> aa.setPlain(j, null),
33 () -> aa.setOpaque(j, null),
34 () -> aa.setRelease(j, null),
35 () -> aa.compareAndExchange(j, null, null),
36 () -> aa.compareAndExchangeAcquire(j, null, null),
37 () -> aa.compareAndExchangeRelease(j, null, null),
38 () -> aa.weakCompareAndSetPlain(j, null, null),
39 () -> aa.weakCompareAndSetVolatile(j, null, null),
40 () -> aa.weakCompareAndSetAcquire(j, null, null),
41 () -> aa.weakCompareAndSetRelease(j, null, null));
42 }
43 }
44
45 /**
46 * getPlain returns the last value set
47 */
48 public void testGetPlainSet() {
49 AtomicReferenceArray<Item> aa = new AtomicReferenceArray<>(SIZE);
50 for (int i = 0; i < SIZE; i++) {
51 aa.set(i, one);
52 assertEquals(one, aa.getPlain(i));
53 aa.set(i, two);
54 assertEquals(two, aa.getPlain(i));
55 aa.set(i, minusThree);
56 assertEquals(minusThree, aa.getPlain(i));
57 }
58 }
59
60 /**
61 * getOpaque returns the last value set
62 */
63 public void testGetOpaqueSet() {
64 AtomicReferenceArray<Item> aa = new AtomicReferenceArray<>(SIZE);
65 for (int i = 0; i < SIZE; i++) {
66 aa.set(i, one);
67 assertEquals(one, aa.getOpaque(i));
68 aa.set(i, two);
69 assertEquals(two, aa.getOpaque(i));
70 aa.set(i, minusThree);
71 assertEquals(minusThree, aa.getOpaque(i));
72 }
73 }
74
75 /**
76 * getAcquire returns the last value set
77 */
78 public void testGetAcquireSet() {
79 AtomicReferenceArray<Item> aa = new AtomicReferenceArray<>(SIZE);
80 for (int i = 0; i < SIZE; i++) {
81 aa.set(i, one);
82 assertEquals(one, aa.getAcquire(i));
83 aa.set(i, two);
84 assertEquals(two, aa.getAcquire(i));
85 aa.set(i, minusThree);
86 assertEquals(minusThree, aa.getAcquire(i));
87 }
88 }
89
90 /**
91 * get returns the last value setPlain
92 */
93 public void testGetSetPlain() {
94 AtomicReferenceArray<Item> aa = new AtomicReferenceArray<>(SIZE);
95 for (int i = 0; i < SIZE; i++) {
96 aa.setPlain(i, one);
97 assertEquals(one, aa.get(i));
98 aa.setPlain(i, two);
99 assertEquals(two, aa.get(i));
100 aa.setPlain(i, minusThree);
101 assertEquals(minusThree, aa.get(i));
102 }
103 }
104
105 /**
106 * get returns the last value setOpaque
107 */
108 public void testGetSetOpaque() {
109 AtomicReferenceArray<Item> aa = new AtomicReferenceArray<>(SIZE);
110 for (int i = 0; i < SIZE; i++) {
111 aa.setOpaque(i, one);
112 assertEquals(one, aa.get(i));
113 aa.setOpaque(i, two);
114 assertEquals(two, aa.get(i));
115 aa.setOpaque(i, minusThree);
116 assertEquals(minusThree, aa.get(i));
117 }
118 }
119
120 /**
121 * get returns the last value setRelease
122 */
123 public void testGetSetRelease() {
124 AtomicReferenceArray<Item> aa = new AtomicReferenceArray<>(SIZE);
125 for (int i = 0; i < SIZE; i++) {
126 aa.setRelease(i, one);
127 assertEquals(one, aa.get(i));
128 aa.setRelease(i, two);
129 assertEquals(two, aa.get(i));
130 aa.setRelease(i, minusThree);
131 assertEquals(minusThree, aa.get(i));
132 }
133 }
134
135 /**
136 * compareAndExchange succeeds in changing value if equal to
137 * expected else fails
138 */
139 public void testCompareAndExchange() {
140 AtomicReferenceArray<Item> aa = new AtomicReferenceArray<>(SIZE);
141 for (int i = 0; i < SIZE; i++) {
142 aa.set(i, one);
143 assertEquals(one, aa.compareAndExchange(i, one, two));
144 assertEquals(two, aa.compareAndExchange(i, two, minusFour));
145 assertEquals(minusFour, aa.get(i));
146 assertEquals(minusFour, aa.compareAndExchange(i,minusFive, seven));
147 assertEquals(minusFour, aa.get(i));
148 assertEquals(minusFour, aa.compareAndExchange(i, minusFour, seven));
149 assertEquals(seven, aa.get(i));
150 }
151 }
152
153 /**
154 * compareAndExchangeAcquire succeeds in changing value if equal to
155 * expected else fails
156 */
157 public void testCompareAndExchangeAcquire() {
158 AtomicReferenceArray<Item> aa = new AtomicReferenceArray<>(SIZE);
159 for (int i = 0; i < SIZE; i++) {
160 aa.set(i, one);
161 assertEquals(one, aa.compareAndExchangeAcquire(i, one, two));
162 assertEquals(two, aa.compareAndExchangeAcquire(i, two, minusFour));
163 assertEquals(minusFour, aa.get(i));
164 assertEquals(minusFour, aa.compareAndExchangeAcquire(i,minusFive, seven));
165 assertEquals(minusFour, aa.get(i));
166 assertEquals(minusFour, aa.compareAndExchangeAcquire(i, minusFour, seven));
167 assertEquals(seven, aa.get(i));
168 }
169 }
170
171 /**
172 * compareAndExchangeRelease succeeds in changing value if equal to
173 * expected else fails
174 */
175 public void testCompareAndExchangeRelease() {
176 AtomicReferenceArray<Item> aa = new AtomicReferenceArray<>(SIZE);
177 for (int i = 0; i < SIZE; i++) {
178 aa.set(i, one);
179 assertEquals(one, aa.compareAndExchangeRelease(i, one, two));
180 assertEquals(two, aa.compareAndExchangeRelease(i, two, minusFour));
181 assertEquals(minusFour, aa.get(i));
182 assertEquals(minusFour, aa.compareAndExchangeRelease(i,minusFive, seven));
183 assertEquals(minusFour, aa.get(i));
184 assertEquals(minusFour, aa.compareAndExchangeRelease(i, minusFour, seven));
185 assertEquals(seven, aa.get(i));
186 }
187 }
188
189 /**
190 * repeated weakCompareAndSetPlain succeeds in changing value when equal
191 * to expected
192 */
193 public void testWeakCompareAndSetPlain() {
194 AtomicReferenceArray<Item> aa = new AtomicReferenceArray<>(SIZE);
195 for (int i = 0; i < SIZE; i++) {
196 aa.set(i, one);
197 do {} while (!aa.weakCompareAndSetPlain(i, one, two));
198 do {} while (!aa.weakCompareAndSetPlain(i, two, minusFour));
199 assertEquals(minusFour, aa.get(i));
200 do {} while (!aa.weakCompareAndSetPlain(i, minusFour, seven));
201 assertEquals(seven, aa.get(i));
202 }
203 }
204
205 /**
206 * repeated weakCompareAndSetVolatile succeeds in changing value when equal
207 * to expected
208 */
209 public void testWeakCompareAndSetVolatile() {
210 AtomicReferenceArray<Item> aa = new AtomicReferenceArray<>(SIZE);
211 for (int i = 0; i < SIZE; i++) {
212 aa.set(i, one);
213 do {} while (!aa.weakCompareAndSetVolatile(i, one, two));
214 do {} while (!aa.weakCompareAndSetVolatile(i, two, minusFour));
215 assertEquals(minusFour, aa.get(i));
216 do {} while (!aa.weakCompareAndSetVolatile(i, minusFour, seven));
217 assertEquals(seven, aa.get(i));
218 }
219 }
220
221 /**
222 * repeated weakCompareAndSetAcquire succeeds in changing value when equal
223 * to expected
224 */
225 public void testWeakCompareAndSetAcquire() {
226 AtomicReferenceArray<Item> aa = new AtomicReferenceArray<>(SIZE);
227 for (int i = 0; i < SIZE; i++) {
228 aa.set(i, one);
229 do {} while (!aa.weakCompareAndSetAcquire(i, one, two));
230 do {} while (!aa.weakCompareAndSetAcquire(i, two, minusFour));
231 assertEquals(minusFour, aa.get(i));
232 do {} while (!aa.weakCompareAndSetAcquire(i, minusFour, seven));
233 assertEquals(seven, aa.get(i));
234 }
235 }
236
237 /**
238 * repeated weakCompareAndSetRelease succeeds in changing value when equal
239 * to expected
240 */
241 public void testWeakCompareAndSetRelease() {
242 AtomicReferenceArray<Item> aa = new AtomicReferenceArray<>(SIZE);
243 for (int i = 0; i < SIZE; i++) {
244 aa.set(i, one);
245 do {} while (!aa.weakCompareAndSetRelease(i, one, two));
246 do {} while (!aa.weakCompareAndSetRelease(i, two, minusFour));
247 assertEquals(minusFour, aa.get(i));
248 do {} while (!aa.weakCompareAndSetRelease(i, minusFour, seven));
249 assertEquals(seven, aa.get(i));
250 }
251 }
252
253 }