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