ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AtomicIntegerArrayTest.java
Revision: 1.7
Committed: Thu Jan 8 01:29:46 2004 UTC (20 years, 4 months ago) by dl
Branch: MAIN
Changes since 1.6: +24 -0 lines
Log Message:
Add Atomic array constructor tests; adjust timings on other tests

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/licenses/publicdomain
5 * Other contributors include Andrew Wright, Jeffrey Hayes,
6 * Pat Fisher, Mike Judd.
7 */
8
9 import junit.framework.*;
10 import java.util.concurrent.atomic.*;
11 import java.io.*;
12
13 public class AtomicIntegerArrayTest extends JSR166TestCase {
14
15 public static void main (String[] args) {
16 junit.textui.TestRunner.run (suite());
17 }
18 public static Test suite() {
19 return new TestSuite(AtomicIntegerArrayTest.class);
20 }
21
22
23 /**
24 * constructor creates array of given size with all elements zero
25 */
26 public void testConstructor() {
27 AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
28 for (int i = 0; i < SIZE; ++i)
29 assertEquals(0,ai.get(i));
30 }
31
32 /**
33 * constructor with null array throws NPE
34 */
35 public void testConstructor2NPE() {
36 try {
37 int[] a = null;
38 AtomicIntegerArray ai = new AtomicIntegerArray(a);
39 } catch (NullPointerException success) {
40 } catch (Exception ex) {
41 unexpectedException();
42 }
43 }
44
45 /**
46 * constructor with array is of same size and has all elements
47 */
48 public void testConstructor2() {
49 int[] a = { 17, 3, -42, 99, -7};
50 AtomicIntegerArray ai = new AtomicIntegerArray(a);
51 assertEquals(a.length, ai.length());
52 for (int i = 0; i < a.length; ++i)
53 assertEquals(a[i], ai.get(i));
54 }
55
56 /**
57 * get and set for out of bound indices throw IndexOutOfBoundsException
58 */
59 public void testIndexing(){
60 AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
61 try {
62 ai.get(SIZE);
63 } catch(IndexOutOfBoundsException success){
64 }
65 try {
66 ai.get(-1);
67 } catch(IndexOutOfBoundsException success){
68 }
69 try {
70 ai.set(SIZE, 0);
71 } catch(IndexOutOfBoundsException success){
72 }
73 try {
74 ai.set(-1, 0);
75 } catch(IndexOutOfBoundsException success){
76 }
77 }
78
79 /**
80 * get returns the last value set at index
81 */
82 public void testGetSet() {
83 AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
84 for (int i = 0; i < SIZE; ++i) {
85 ai.set(i, 1);
86 assertEquals(1,ai.get(i));
87 ai.set(i, 2);
88 assertEquals(2,ai.get(i));
89 ai.set(i, -3);
90 assertEquals(-3,ai.get(i));
91 }
92 }
93
94 /**
95 * compareAndSet succeeds in changing value if equal to expected else fails
96 */
97 public void testCompareAndSet() {
98 AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
99 for (int i = 0; i < SIZE; ++i) {
100 ai.set(i, 1);
101 assertTrue(ai.compareAndSet(i, 1,2));
102 assertTrue(ai.compareAndSet(i, 2,-4));
103 assertEquals(-4,ai.get(i));
104 assertFalse(ai.compareAndSet(i, -5,7));
105 assertFalse((7 == ai.get(i)));
106 assertTrue(ai.compareAndSet(i, -4,7));
107 assertEquals(7,ai.get(i));
108 }
109 }
110
111 /**
112 * compareAndSet in one thread enables another waiting for value
113 * to succeed
114 */
115 public void testCompareAndSetInMultipleThreads() {
116 final AtomicIntegerArray a = new AtomicIntegerArray(1);
117 a.set(0, 1);
118 Thread t = new Thread(new Runnable() {
119 public void run() {
120 while(!a.compareAndSet(0, 2, 3)) Thread.yield();
121 }});
122 try {
123 t.start();
124 assertTrue(a.compareAndSet(0, 1, 2));
125 t.join(LONG_DELAY_MS);
126 assertFalse(t.isAlive());
127 assertEquals(a.get(0), 3);
128 }
129 catch(Exception e) {
130 unexpectedException();
131 }
132 }
133
134 /**
135 * repeated weakCompareAndSet succeeds in changing value when equal
136 * to expected
137 */
138 public void testWeakCompareAndSet() {
139 AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
140 for (int i = 0; i < SIZE; ++i) {
141 ai.set(i, 1);
142 while(!ai.weakCompareAndSet(i, 1,2));
143 while(!ai.weakCompareAndSet(i, 2,-4));
144 assertEquals(-4,ai.get(i));
145 while(!ai.weakCompareAndSet(i, -4,7));
146 assertEquals(7,ai.get(i));
147 }
148 }
149
150 /**
151 * getAndSet returns previous value and sets to given value at given index
152 */
153 public void testGetAndSet() {
154 AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
155 for (int i = 0; i < SIZE; ++i) {
156 ai.set(i, 1);
157 assertEquals(1,ai.getAndSet(i,0));
158 assertEquals(0,ai.getAndSet(i,-10));
159 assertEquals(-10,ai.getAndSet(i,1));
160 }
161 }
162
163 /**
164 * getAndAdd returns previous value and adds given value
165 */
166 public void testGetAndAdd() {
167 AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
168 for (int i = 0; i < SIZE; ++i) {
169 ai.set(i, 1);
170 assertEquals(1,ai.getAndAdd(i,2));
171 assertEquals(3,ai.get(i));
172 assertEquals(3,ai.getAndAdd(i,-4));
173 assertEquals(-1,ai.get(i));
174 }
175 }
176
177 /**
178 * getAndDecrement returns previous value and decrements
179 */
180 public void testGetAndDecrement() {
181 AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
182 for (int i = 0; i < SIZE; ++i) {
183 ai.set(i, 1);
184 assertEquals(1,ai.getAndDecrement(i));
185 assertEquals(0,ai.getAndDecrement(i));
186 assertEquals(-1,ai.getAndDecrement(i));
187 }
188 }
189
190 /**
191 * getAndIncrement returns previous value and increments
192 */
193 public void testGetAndIncrement() {
194 AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
195 for (int i = 0; i < SIZE; ++i) {
196 ai.set(i, 1);
197 assertEquals(1,ai.getAndIncrement(i));
198 assertEquals(2,ai.get(i));
199 ai.set(i,-2);
200 assertEquals(-2,ai.getAndIncrement(i));
201 assertEquals(-1,ai.getAndIncrement(i));
202 assertEquals(0,ai.getAndIncrement(i));
203 assertEquals(1,ai.get(i));
204 }
205 }
206
207 /**
208 * addAndGet adds given value to current, and returns current value
209 */
210 public void testAddAndGet() {
211 AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
212 for (int i = 0; i < SIZE; ++i) {
213 ai.set(i, 1);
214 assertEquals(3,ai.addAndGet(i,2));
215 assertEquals(3,ai.get(i));
216 assertEquals(-1,ai.addAndGet(i,-4));
217 assertEquals(-1,ai.get(i));
218 }
219 }
220
221 /**
222 * decrementAndGet decrements and returns current value
223 */
224 public void testDecrementAndGet() {
225 AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
226 for (int i = 0; i < SIZE; ++i) {
227 ai.set(i, 1);
228 assertEquals(0,ai.decrementAndGet(i));
229 assertEquals(-1,ai.decrementAndGet(i));
230 assertEquals(-2,ai.decrementAndGet(i));
231 assertEquals(-2,ai.get(i));
232 }
233 }
234
235 /**
236 * incrementAndGet increments and returns current value
237 */
238 public void testIncrementAndGet() {
239 AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
240 for (int i = 0; i < SIZE; ++i) {
241 ai.set(i, 1);
242 assertEquals(2,ai.incrementAndGet(i));
243 assertEquals(2,ai.get(i));
244 ai.set(i, -2);
245 assertEquals(-1,ai.incrementAndGet(i));
246 assertEquals(0,ai.incrementAndGet(i));
247 assertEquals(1,ai.incrementAndGet(i));
248 assertEquals(1,ai.get(i));
249 }
250 }
251
252 static final int COUNTDOWN = 100000;
253
254 class Counter implements Runnable {
255 final AtomicIntegerArray ai;
256 volatile int counts;
257 Counter(AtomicIntegerArray a) { ai = a; }
258 public void run() {
259 for (;;) {
260 boolean done = true;
261 for (int i = 0; i < ai.length(); ++i) {
262 int v = ai.get(i);
263 threadAssertTrue(v >= 0);
264 if (v != 0) {
265 done = false;
266 if (ai.compareAndSet(i, v, v-1))
267 ++counts;
268 }
269 }
270 if (done)
271 break;
272 }
273 }
274 }
275
276 /**
277 * Multiple threads using same array of counters successfully
278 * update a number of times equal to total count
279 */
280 public void testCountingInMultipleThreads() {
281 try {
282 final AtomicIntegerArray ai = new AtomicIntegerArray(SIZE);
283 for (int i = 0; i < SIZE; ++i)
284 ai.set(i, COUNTDOWN);
285 Counter c1 = new Counter(ai);
286 Counter c2 = new Counter(ai);
287 Thread t1 = new Thread(c1);
288 Thread t2 = new Thread(c2);
289 t1.start();
290 t2.start();
291 t1.join();
292 t2.join();
293 assertEquals(c1.counts+c2.counts, SIZE * COUNTDOWN);
294 }
295 catch(InterruptedException ie) {
296 unexpectedException();
297 }
298 }
299
300
301 /**
302 * a deserialized serialized array holds same values
303 */
304 public void testSerialization() {
305 AtomicIntegerArray l = new AtomicIntegerArray(SIZE);
306 for (int i = 0; i < SIZE; ++i)
307 l.set(i, -i);
308
309 try {
310 ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
311 ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
312 out.writeObject(l);
313 out.close();
314
315 ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
316 ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
317 AtomicIntegerArray r = (AtomicIntegerArray) in.readObject();
318 for (int i = 0; i < SIZE; ++i) {
319 assertEquals(l.get(i), r.get(i));
320 }
321 } catch(Exception e){
322 e.printStackTrace();
323 unexpectedException();
324 }
325 }
326
327 }