1 |
jsr166 |
1.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 |
jsr166 |
1.4 |
assertThrows( |
28 |
|
|
IndexOutOfBoundsException.class, |
29 |
jsr166 |
1.1 |
() -> 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 |
jsr166 |
1.3 |
() -> aa.weakCompareAndSetPlain(j, null, null), |
39 |
jsr166 |
1.1 |
() -> aa.weakCompareAndSetVolatile(j, null, null), |
40 |
|
|
() -> aa.weakCompareAndSetAcquire(j, null, null), |
41 |
jsr166 |
1.4 |
() -> aa.weakCompareAndSetRelease(j, null, null)); |
42 |
jsr166 |
1.1 |
} |
43 |
|
|
} |
44 |
|
|
|
45 |
|
|
/** |
46 |
|
|
* getPlain returns the last value set |
47 |
|
|
*/ |
48 |
|
|
public void testGetPlainSet() { |
49 |
|
|
AtomicReferenceArray<Integer> 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, m3); |
56 |
|
|
assertEquals(m3, aa.getPlain(i)); |
57 |
|
|
} |
58 |
|
|
} |
59 |
|
|
|
60 |
|
|
/** |
61 |
|
|
* getOpaque returns the last value set |
62 |
|
|
*/ |
63 |
|
|
public void testGetOpaqueSet() { |
64 |
|
|
AtomicReferenceArray<Integer> 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, m3); |
71 |
|
|
assertEquals(m3, aa.getOpaque(i)); |
72 |
|
|
} |
73 |
|
|
} |
74 |
|
|
|
75 |
|
|
/** |
76 |
|
|
* getAcquire returns the last value set |
77 |
|
|
*/ |
78 |
|
|
public void testGetAcquireSet() { |
79 |
|
|
AtomicReferenceArray<Integer> 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, m3); |
86 |
|
|
assertEquals(m3, aa.getAcquire(i)); |
87 |
|
|
} |
88 |
|
|
} |
89 |
|
|
|
90 |
|
|
/** |
91 |
|
|
* get returns the last value setPlain |
92 |
|
|
*/ |
93 |
|
|
public void testGetSetPlain() { |
94 |
|
|
AtomicReferenceArray<Integer> 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, m3); |
101 |
|
|
assertEquals(m3, aa.get(i)); |
102 |
|
|
} |
103 |
|
|
} |
104 |
|
|
|
105 |
|
|
/** |
106 |
|
|
* get returns the last value setOpaque |
107 |
|
|
*/ |
108 |
|
|
public void testGetSetOpaque() { |
109 |
|
|
AtomicReferenceArray<Integer> 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, m3); |
116 |
|
|
assertEquals(m3, aa.get(i)); |
117 |
|
|
} |
118 |
|
|
} |
119 |
|
|
|
120 |
|
|
/** |
121 |
|
|
* get returns the last value setRelease |
122 |
|
|
*/ |
123 |
|
|
public void testGetSetRelease() { |
124 |
|
|
AtomicReferenceArray<Integer> 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, m3); |
131 |
|
|
assertEquals(m3, 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<Integer> 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, m4)); |
145 |
|
|
assertEquals(m4, aa.get(i)); |
146 |
|
|
assertEquals(m4, aa.compareAndExchange(i,m5, seven)); |
147 |
|
|
assertEquals(m4, aa.get(i)); |
148 |
|
|
assertEquals(m4, aa.compareAndExchange(i, m4, 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<Integer> 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, m4)); |
163 |
|
|
assertEquals(m4, aa.get(i)); |
164 |
|
|
assertEquals(m4, aa.compareAndExchangeAcquire(i,m5, seven)); |
165 |
|
|
assertEquals(m4, aa.get(i)); |
166 |
|
|
assertEquals(m4, aa.compareAndExchangeAcquire(i, m4, 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<Integer> 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, m4)); |
181 |
|
|
assertEquals(m4, aa.get(i)); |
182 |
|
|
assertEquals(m4, aa.compareAndExchangeRelease(i,m5, seven)); |
183 |
|
|
assertEquals(m4, aa.get(i)); |
184 |
|
|
assertEquals(m4, aa.compareAndExchangeRelease(i, m4, seven)); |
185 |
|
|
assertEquals(seven, aa.get(i)); |
186 |
|
|
} |
187 |
|
|
} |
188 |
|
|
|
189 |
|
|
/** |
190 |
jsr166 |
1.3 |
* repeated weakCompareAndSetPlain succeeds in changing value when equal |
191 |
|
|
* to expected |
192 |
|
|
*/ |
193 |
|
|
public void testWeakCompareAndSetPlain() { |
194 |
|
|
AtomicReferenceArray<Integer> 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, m4)); |
199 |
|
|
assertEquals(m4, aa.get(i)); |
200 |
|
|
do {} while (!aa.weakCompareAndSetPlain(i, m4, seven)); |
201 |
|
|
assertEquals(seven, aa.get(i)); |
202 |
|
|
} |
203 |
|
|
} |
204 |
|
|
|
205 |
|
|
/** |
206 |
jsr166 |
1.1 |
* repeated weakCompareAndSetVolatile succeeds in changing value when equal |
207 |
|
|
* to expected |
208 |
|
|
*/ |
209 |
|
|
public void testWeakCompareAndSetVolatile() { |
210 |
|
|
AtomicReferenceArray<Integer> 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, m4)); |
215 |
|
|
assertEquals(m4, aa.get(i)); |
216 |
|
|
do {} while (!aa.weakCompareAndSetVolatile(i, m4, 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<Integer> 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, m4)); |
231 |
|
|
assertEquals(m4, aa.get(i)); |
232 |
|
|
do {} while (!aa.weakCompareAndSetAcquire(i, m4, 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<Integer> 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, m4)); |
247 |
|
|
assertEquals(m4, aa.get(i)); |
248 |
|
|
do {} while (!aa.weakCompareAndSetRelease(i, m4, seven)); |
249 |
|
|
assertEquals(seven, aa.get(i)); |
250 |
|
|
} |
251 |
|
|
} |
252 |
|
|
|
253 |
|
|
} |