1 |
/* |
2 |
* Written by members of JCP JSR-166 Expert Group and released to the |
3 |
* public domain. Use, modify, and redistribute this code in any way |
4 |
* without acknowledgement. Other contributors include Andrew Wright, |
5 |
* Jeffrey Hayes, Pat Fischer, Mike Judd. |
6 |
*/ |
7 |
|
8 |
import junit.framework.*; |
9 |
import java.util.concurrent.atomic.*; |
10 |
import java.io.*; |
11 |
|
12 |
public class AtomicIntegerArrayTest extends JSR166TestCase { |
13 |
|
14 |
public static void main (String[] args) { |
15 |
junit.textui.TestRunner.run (suite()); |
16 |
} |
17 |
public static Test suite() { |
18 |
return new TestSuite(AtomicIntegerArrayTest.class); |
19 |
} |
20 |
|
21 |
|
22 |
public void testConstructor(){ |
23 |
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
24 |
for (int i = 0; i < SIZE; ++i) |
25 |
assertEquals(0,ai.get(i)); |
26 |
} |
27 |
|
28 |
public void testGetSet(){ |
29 |
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
30 |
for (int i = 0; i < SIZE; ++i) { |
31 |
ai.set(i, 1); |
32 |
assertEquals(1,ai.get(i)); |
33 |
ai.set(i, 2); |
34 |
assertEquals(2,ai.get(i)); |
35 |
ai.set(i, -3); |
36 |
assertEquals(-3,ai.get(i)); |
37 |
} |
38 |
} |
39 |
|
40 |
public void testCompareAndSet(){ |
41 |
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
42 |
for (int i = 0; i < SIZE; ++i) { |
43 |
ai.set(i, 1); |
44 |
assertTrue(ai.compareAndSet(i, 1,2)); |
45 |
assertTrue(ai.compareAndSet(i, 2,-4)); |
46 |
assertEquals(-4,ai.get(i)); |
47 |
assertFalse(ai.compareAndSet(i, -5,7)); |
48 |
assertFalse((7 == ai.get(i))); |
49 |
assertTrue(ai.compareAndSet(i, -4,7)); |
50 |
assertEquals(7,ai.get(i)); |
51 |
} |
52 |
} |
53 |
|
54 |
public void testWeakCompareAndSet(){ |
55 |
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
56 |
for (int i = 0; i < SIZE; ++i) { |
57 |
ai.set(i, 1); |
58 |
while(!ai.weakCompareAndSet(i, 1,2)); |
59 |
while(!ai.weakCompareAndSet(i, 2,-4)); |
60 |
assertEquals(-4,ai.get(i)); |
61 |
while(!ai.weakCompareAndSet(i, -4,7)); |
62 |
assertEquals(7,ai.get(i)); |
63 |
} |
64 |
} |
65 |
|
66 |
public void testGetAndSet(){ |
67 |
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
68 |
for (int i = 0; i < SIZE; ++i) { |
69 |
ai.set(i, 1); |
70 |
assertEquals(1,ai.getAndSet(i,0)); |
71 |
assertEquals(0,ai.getAndSet(i,-10)); |
72 |
assertEquals(-10,ai.getAndSet(i,1)); |
73 |
} |
74 |
} |
75 |
|
76 |
public void testGetAndAdd(){ |
77 |
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
78 |
for (int i = 0; i < SIZE; ++i) { |
79 |
ai.set(i, 1); |
80 |
assertEquals(1,ai.getAndAdd(i,2)); |
81 |
assertEquals(3,ai.get(i)); |
82 |
assertEquals(3,ai.getAndAdd(i,-4)); |
83 |
assertEquals(-1,ai.get(i)); |
84 |
} |
85 |
} |
86 |
|
87 |
public void testGetAndDecrement(){ |
88 |
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
89 |
for (int i = 0; i < SIZE; ++i) { |
90 |
ai.set(i, 1); |
91 |
assertEquals(1,ai.getAndDecrement(i)); |
92 |
assertEquals(0,ai.getAndDecrement(i)); |
93 |
assertEquals(-1,ai.getAndDecrement(i)); |
94 |
} |
95 |
} |
96 |
|
97 |
public void testGetAndIncrement(){ |
98 |
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
99 |
for (int i = 0; i < SIZE; ++i) { |
100 |
ai.set(i, 1); |
101 |
assertEquals(1,ai.getAndIncrement(i)); |
102 |
assertEquals(2,ai.get(i)); |
103 |
ai.set(i,-2); |
104 |
assertEquals(-2,ai.getAndIncrement(i)); |
105 |
assertEquals(-1,ai.getAndIncrement(i)); |
106 |
assertEquals(0,ai.getAndIncrement(i)); |
107 |
assertEquals(1,ai.get(i)); |
108 |
} |
109 |
} |
110 |
|
111 |
public void testAddAndGet() { |
112 |
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
113 |
for (int i = 0; i < SIZE; ++i) { |
114 |
ai.set(i, 1); |
115 |
assertEquals(3,ai.addAndGet(i,2)); |
116 |
assertEquals(3,ai.get(i)); |
117 |
assertEquals(-1,ai.addAndGet(i,-4)); |
118 |
assertEquals(-1,ai.get(i)); |
119 |
} |
120 |
} |
121 |
|
122 |
public void testDecrementAndGet(){ |
123 |
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
124 |
for (int i = 0; i < SIZE; ++i) { |
125 |
ai.set(i, 1); |
126 |
assertEquals(0,ai.decrementAndGet(i)); |
127 |
assertEquals(-1,ai.decrementAndGet(i)); |
128 |
assertEquals(-2,ai.decrementAndGet(i)); |
129 |
assertEquals(-2,ai.get(i)); |
130 |
} |
131 |
} |
132 |
|
133 |
public void testIncrementAndGet() { |
134 |
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
135 |
for (int i = 0; i < SIZE; ++i) { |
136 |
ai.set(i, 1); |
137 |
assertEquals(2,ai.incrementAndGet(i)); |
138 |
assertEquals(2,ai.get(i)); |
139 |
ai.set(i, -2); |
140 |
assertEquals(-1,ai.incrementAndGet(i)); |
141 |
assertEquals(0,ai.incrementAndGet(i)); |
142 |
assertEquals(1,ai.incrementAndGet(i)); |
143 |
assertEquals(1,ai.get(i)); |
144 |
} |
145 |
} |
146 |
|
147 |
static final int COUNTDOWN = 100000; |
148 |
|
149 |
class Counter implements Runnable { |
150 |
final AtomicIntegerArray ai; |
151 |
volatile int counts; |
152 |
Counter(AtomicIntegerArray a) { ai = a; } |
153 |
public void run() { |
154 |
for (;;) { |
155 |
boolean done = true; |
156 |
for (int i = 0; i < ai.length(); ++i) { |
157 |
int v = ai.get(i); |
158 |
threadAssertTrue(v >= 0); |
159 |
if (v != 0) { |
160 |
done = false; |
161 |
if (ai.compareAndSet(i, v, v-1)) |
162 |
++counts; |
163 |
} |
164 |
} |
165 |
if (done) |
166 |
break; |
167 |
} |
168 |
} |
169 |
} |
170 |
|
171 |
public void testCountingInMultipleThreads() { |
172 |
try { |
173 |
final AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
174 |
for (int i = 0; i < SIZE; ++i) |
175 |
ai.set(i, COUNTDOWN); |
176 |
Counter c1 = new Counter(ai); |
177 |
Counter c2 = new Counter(ai); |
178 |
Thread t1 = new Thread(c1); |
179 |
Thread t2 = new Thread(c2); |
180 |
t1.start(); |
181 |
t2.start(); |
182 |
t1.join(); |
183 |
t2.join(); |
184 |
assertEquals(c1.counts+c2.counts, SIZE * COUNTDOWN); |
185 |
} |
186 |
catch(InterruptedException ie) { |
187 |
fail("unexpected exception"); |
188 |
} |
189 |
} |
190 |
|
191 |
|
192 |
public void testSerialization() { |
193 |
AtomicIntegerArray l = new AtomicIntegerArray(SIZE); |
194 |
for (int i = 0; i < SIZE; ++i) |
195 |
l.set(i, -i); |
196 |
|
197 |
try { |
198 |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
199 |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
200 |
out.writeObject(l); |
201 |
out.close(); |
202 |
|
203 |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
204 |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
205 |
AtomicIntegerArray r = (AtomicIntegerArray) in.readObject(); |
206 |
for (int i = 0; i < SIZE; ++i) { |
207 |
assertEquals(l.get(i), r.get(i)); |
208 |
} |
209 |
} catch(Exception e){ |
210 |
e.printStackTrace(); |
211 |
fail("unexpected exception"); |
212 |
} |
213 |
} |
214 |
|
215 |
} |