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