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