18 |
|
} |
19 |
|
|
20 |
|
/** |
21 |
< |
* |
21 |
> |
* constructor creates array of given size with all elements zero |
22 |
|
*/ |
23 |
|
public void testConstructor(){ |
24 |
|
AtomicLongArray ai = new AtomicLongArray(SIZE); |
27 |
|
} |
28 |
|
|
29 |
|
/** |
30 |
< |
* |
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); |
65 |
|
} |
66 |
|
|
67 |
|
/** |
68 |
< |
* |
68 |
> |
* compareAndSet succeeds in changing value if equal to expected else fails |
69 |
|
*/ |
70 |
|
public void testCompareAndSet(){ |
71 |
|
AtomicLongArray ai = new AtomicLongArray(SIZE); |
82 |
|
} |
83 |
|
|
84 |
|
/** |
85 |
< |
* |
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); |
121 |
|
} |
122 |
|
|
123 |
|
/** |
124 |
< |
* |
124 |
> |
* getAndSet returns previous value and sets to given value at given index |
125 |
|
*/ |
126 |
|
public void testGetAndSet(){ |
127 |
|
AtomicLongArray ai = new AtomicLongArray(SIZE); |
134 |
|
} |
135 |
|
|
136 |
|
/** |
137 |
< |
* |
137 |
> |
* getAndAdd returns previous value and adds given value |
138 |
|
*/ |
139 |
|
public void testGetAndAdd(){ |
140 |
|
AtomicLongArray ai = new AtomicLongArray(SIZE); |
148 |
|
} |
149 |
|
|
150 |
|
/** |
151 |
< |
* |
151 |
> |
* getAndDecrement returns previous value and decrements |
152 |
|
*/ |
153 |
|
public void testGetAndDecrement(){ |
154 |
|
AtomicLongArray ai = new AtomicLongArray(SIZE); |
161 |
|
} |
162 |
|
|
163 |
|
/** |
164 |
< |
* |
164 |
> |
* getAndIncrement returns previous value and increments |
165 |
|
*/ |
166 |
|
public void testGetAndIncrement(){ |
167 |
|
AtomicLongArray ai = new AtomicLongArray(SIZE); |
178 |
|
} |
179 |
|
|
180 |
|
/** |
181 |
< |
* |
181 |
> |
* addAndGet adds given value to current, and returns current value |
182 |
|
*/ |
183 |
|
public void testAddAndGet() { |
184 |
|
AtomicLongArray ai = new AtomicLongArray(SIZE); |
192 |
|
} |
193 |
|
|
194 |
|
/** |
195 |
< |
* |
195 |
> |
* decrementAndGet decrements and returns current value |
196 |
|
*/ |
197 |
|
public void testDecrementAndGet(){ |
198 |
|
AtomicLongArray ai = new AtomicLongArray(SIZE); |
206 |
|
} |
207 |
|
|
208 |
|
/** |
209 |
< |
* |
209 |
> |
* incrementAndGet increments and returns current value |
210 |
|
*/ |
211 |
|
public void testIncrementAndGet() { |
212 |
|
AtomicLongArray ai = new AtomicLongArray(SIZE); |
247 |
|
} |
248 |
|
|
249 |
|
/** |
250 |
< |
* |
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 { |
271 |
|
} |
272 |
|
|
273 |
|
/** |
274 |
< |
* |
274 |
> |
* a deserialized serialized array holds same values |
275 |
|
*/ |
276 |
|
public void testSerialization() { |
277 |
|
AtomicLongArray l = new AtomicLongArray(SIZE); |