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