17 |
|
return new TestSuite(AtomicLongArrayTest.class); |
18 |
|
} |
19 |
|
|
20 |
+ |
/** |
21 |
+ |
* constructor creates array of given size with all elements zero |
22 |
+ |
*/ |
23 |
|
public void testConstructor(){ |
24 |
|
AtomicLongArray ai = new AtomicLongArray(SIZE); |
25 |
|
for (int i = 0; i < SIZE; ++i) |
26 |
|
assertEquals(0,ai.get(i)); |
27 |
|
} |
28 |
|
|
29 |
+ |
/** |
30 |
+ |
* get and set for out of bound indices throw IndexOutOfBoundsException |
31 |
+ |
*/ |
32 |
+ |
public void testIndexing(){ |
33 |
+ |
AtomicLongArray ai = new AtomicLongArray(SIZE); |
34 |
+ |
try { |
35 |
+ |
ai.get(SIZE); |
36 |
+ |
} catch(IndexOutOfBoundsException success){ |
37 |
+ |
} |
38 |
+ |
try { |
39 |
+ |
ai.get(-1); |
40 |
+ |
} catch(IndexOutOfBoundsException success){ |
41 |
+ |
} |
42 |
+ |
try { |
43 |
+ |
ai.set(SIZE, 0); |
44 |
+ |
} catch(IndexOutOfBoundsException success){ |
45 |
+ |
} |
46 |
+ |
try { |
47 |
+ |
ai.set(-1, 0); |
48 |
+ |
} catch(IndexOutOfBoundsException success){ |
49 |
+ |
} |
50 |
+ |
} |
51 |
+ |
|
52 |
+ |
/** |
53 |
+ |
* get returns the last value set at index |
54 |
+ |
*/ |
55 |
|
public void testGetSet(){ |
56 |
|
AtomicLongArray ai = new AtomicLongArray(SIZE); |
57 |
|
for (int i = 0; i < SIZE; ++i) { |
64 |
|
} |
65 |
|
} |
66 |
|
|
67 |
+ |
/** |
68 |
+ |
* compareAndSet succeeds in changing value if equal to expected else fails |
69 |
+ |
*/ |
70 |
|
public void testCompareAndSet(){ |
71 |
|
AtomicLongArray ai = new AtomicLongArray(SIZE); |
72 |
|
for (int i = 0; i < SIZE; ++i) { |
81 |
|
} |
82 |
|
} |
83 |
|
|
84 |
+ |
/** |
85 |
+ |
* compareAndSet in one thread enables another waiting for value |
86 |
+ |
* to succeed |
87 |
+ |
*/ |
88 |
+ |
public void testCompareAndSetInMultipleThreads() { |
89 |
+ |
final AtomicLongArray a = new AtomicLongArray(1); |
90 |
+ |
a.set(0, 1); |
91 |
+ |
Thread t = new Thread(new Runnable() { |
92 |
+ |
public void run() { |
93 |
+ |
while(!a.compareAndSet(0, 2, 3)) Thread.yield(); |
94 |
+ |
}}); |
95 |
+ |
try { |
96 |
+ |
t.start(); |
97 |
+ |
assertTrue(a.compareAndSet(0, 1, 2)); |
98 |
+ |
t.join(LONG_DELAY_MS); |
99 |
+ |
assertFalse(t.isAlive()); |
100 |
+ |
assertEquals(a.get(0), 3); |
101 |
+ |
} |
102 |
+ |
catch(Exception e) { |
103 |
+ |
unexpectedException(); |
104 |
+ |
} |
105 |
+ |
} |
106 |
+ |
|
107 |
+ |
/** |
108 |
+ |
* repeated weakCompareAndSet succeeds in changing value when equal |
109 |
+ |
* to expected |
110 |
+ |
*/ |
111 |
|
public void testWeakCompareAndSet(){ |
112 |
|
AtomicLongArray ai = new AtomicLongArray(SIZE); |
113 |
|
for (int i = 0; i < SIZE; ++i) { |
120 |
|
} |
121 |
|
} |
122 |
|
|
123 |
+ |
/** |
124 |
+ |
* getAndSet returns previous value and sets to given value at given index |
125 |
+ |
*/ |
126 |
|
public void testGetAndSet(){ |
127 |
|
AtomicLongArray ai = new AtomicLongArray(SIZE); |
128 |
|
for (int i = 0; i < SIZE; ++i) { |
133 |
|
} |
134 |
|
} |
135 |
|
|
136 |
+ |
/** |
137 |
+ |
* getAndAdd returns previous value and adds given value |
138 |
+ |
*/ |
139 |
|
public void testGetAndAdd(){ |
140 |
|
AtomicLongArray ai = new AtomicLongArray(SIZE); |
141 |
|
for (int i = 0; i < SIZE; ++i) { |
147 |
|
} |
148 |
|
} |
149 |
|
|
150 |
+ |
/** |
151 |
+ |
* getAndDecrement returns previous value and decrements |
152 |
+ |
*/ |
153 |
|
public void testGetAndDecrement(){ |
154 |
|
AtomicLongArray ai = new AtomicLongArray(SIZE); |
155 |
|
for (int i = 0; i < SIZE; ++i) { |
160 |
|
} |
161 |
|
} |
162 |
|
|
163 |
+ |
/** |
164 |
+ |
* getAndIncrement returns previous value and increments |
165 |
+ |
*/ |
166 |
|
public void testGetAndIncrement(){ |
167 |
|
AtomicLongArray ai = new AtomicLongArray(SIZE); |
168 |
|
for (int i = 0; i < SIZE; ++i) { |
177 |
|
} |
178 |
|
} |
179 |
|
|
180 |
+ |
/** |
181 |
+ |
* addAndGet adds given value to current, and returns current value |
182 |
+ |
*/ |
183 |
|
public void testAddAndGet() { |
184 |
|
AtomicLongArray ai = new AtomicLongArray(SIZE); |
185 |
|
for (int i = 0; i < SIZE; ++i) { |
191 |
|
} |
192 |
|
} |
193 |
|
|
194 |
+ |
/** |
195 |
+ |
* decrementAndGet decrements and returns current value |
196 |
+ |
*/ |
197 |
|
public void testDecrementAndGet(){ |
198 |
|
AtomicLongArray ai = new AtomicLongArray(SIZE); |
199 |
|
for (int i = 0; i < SIZE; ++i) { |
205 |
|
} |
206 |
|
} |
207 |
|
|
208 |
+ |
/** |
209 |
+ |
* incrementAndGet increments and returns current value |
210 |
+ |
*/ |
211 |
|
public void testIncrementAndGet() { |
212 |
|
AtomicLongArray ai = new AtomicLongArray(SIZE); |
213 |
|
for (int i = 0; i < SIZE; ++i) { |
246 |
|
} |
247 |
|
} |
248 |
|
|
249 |
+ |
/** |
250 |
+ |
* Multiple threads using same array of counters successfully |
251 |
+ |
* update a number of times equal to total count |
252 |
+ |
*/ |
253 |
|
public void testCountingInMultipleThreads() { |
254 |
|
try { |
255 |
|
final AtomicLongArray ai = new AtomicLongArray(SIZE); |
266 |
|
assertEquals(c1.counts+c2.counts, SIZE * COUNTDOWN); |
267 |
|
} |
268 |
|
catch(InterruptedException ie) { |
269 |
< |
fail("unexpected exception"); |
269 |
> |
unexpectedException(); |
270 |
|
} |
271 |
|
} |
272 |
|
|
273 |
+ |
/** |
274 |
+ |
* a deserialized serialized array holds same values |
275 |
+ |
*/ |
276 |
|
public void testSerialization() { |
277 |
|
AtomicLongArray l = new AtomicLongArray(SIZE); |
278 |
|
for (int i = 0; i < SIZE; ++i) |
291 |
|
assertEquals(l.get(i), r.get(i)); |
292 |
|
} |
293 |
|
} catch(Exception e){ |
294 |
< |
e.printStackTrace(); |
208 |
< |
fail("unexpected exception"); |
294 |
> |
unexpectedException(); |
295 |
|
} |
296 |
|
} |
297 |
|
|