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. |
2 |
> |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
> |
* Expert Group and released to the public domain, as explained at |
4 |
> |
* http://creativecommons.org/licenses/publicdomain |
5 |
> |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
> |
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
|
import junit.framework.*; |
21 |
|
|
22 |
|
|
23 |
|
/** |
24 |
< |
* |
24 |
> |
* constructor creates array of given size with all elements zero |
25 |
|
*/ |
26 |
|
public void testConstructor() { |
27 |
|
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
30 |
|
} |
31 |
|
|
32 |
|
/** |
33 |
< |
* |
33 |
> |
* get and set for out of bound indices throw IndexOutOfBoundsException |
34 |
> |
*/ |
35 |
> |
public void testIndexing(){ |
36 |
> |
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
37 |
> |
try { |
38 |
> |
ai.get(SIZE); |
39 |
> |
} catch(IndexOutOfBoundsException success){ |
40 |
> |
} |
41 |
> |
try { |
42 |
> |
ai.get(-1); |
43 |
> |
} catch(IndexOutOfBoundsException success){ |
44 |
> |
} |
45 |
> |
try { |
46 |
> |
ai.set(SIZE, 0); |
47 |
> |
} catch(IndexOutOfBoundsException success){ |
48 |
> |
} |
49 |
> |
try { |
50 |
> |
ai.set(-1, 0); |
51 |
> |
} catch(IndexOutOfBoundsException success){ |
52 |
> |
} |
53 |
> |
} |
54 |
> |
|
55 |
> |
/** |
56 |
> |
* get returns the last value set at index |
57 |
|
*/ |
58 |
|
public void testGetSet() { |
59 |
|
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
68 |
|
} |
69 |
|
|
70 |
|
/** |
71 |
< |
* |
71 |
> |
* compareAndSet succeeds in changing value if equal to expected else fails |
72 |
|
*/ |
73 |
|
public void testCompareAndSet() { |
74 |
|
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
85 |
|
} |
86 |
|
|
87 |
|
/** |
88 |
< |
* |
88 |
> |
* compareAndSet in one thread enables another waiting for value |
89 |
> |
* to succeed |
90 |
> |
*/ |
91 |
> |
public void testCompareAndSetInMultipleThreads() { |
92 |
> |
final AtomicIntegerArray a = new AtomicIntegerArray(1); |
93 |
> |
a.set(0, 1); |
94 |
> |
Thread t = new Thread(new Runnable() { |
95 |
> |
public void run() { |
96 |
> |
while(!a.compareAndSet(0, 2, 3)) Thread.yield(); |
97 |
> |
}}); |
98 |
> |
try { |
99 |
> |
t.start(); |
100 |
> |
assertTrue(a.compareAndSet(0, 1, 2)); |
101 |
> |
t.join(LONG_DELAY_MS); |
102 |
> |
assertFalse(t.isAlive()); |
103 |
> |
assertEquals(a.get(0), 3); |
104 |
> |
} |
105 |
> |
catch(Exception e) { |
106 |
> |
unexpectedException(); |
107 |
> |
} |
108 |
> |
} |
109 |
> |
|
110 |
> |
/** |
111 |
> |
* repeated weakCompareAndSet succeeds in changing value when equal |
112 |
> |
* to expected |
113 |
|
*/ |
114 |
|
public void testWeakCompareAndSet() { |
115 |
|
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
124 |
|
} |
125 |
|
|
126 |
|
/** |
127 |
< |
* |
127 |
> |
* getAndSet returns previous value and sets to given value at given index |
128 |
|
*/ |
129 |
|
public void testGetAndSet() { |
130 |
|
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
137 |
|
} |
138 |
|
|
139 |
|
/** |
140 |
< |
* |
140 |
> |
* getAndAdd returns previous value and adds given value |
141 |
|
*/ |
142 |
|
public void testGetAndAdd() { |
143 |
|
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
151 |
|
} |
152 |
|
|
153 |
|
/** |
154 |
< |
* |
154 |
> |
* getAndDecrement returns previous value and decrements |
155 |
|
*/ |
156 |
|
public void testGetAndDecrement() { |
157 |
|
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
164 |
|
} |
165 |
|
|
166 |
|
/** |
167 |
< |
* |
167 |
> |
* getAndIncrement returns previous value and increments |
168 |
|
*/ |
169 |
|
public void testGetAndIncrement() { |
170 |
|
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
181 |
|
} |
182 |
|
|
183 |
|
/** |
184 |
< |
* |
184 |
> |
* addAndGet adds given value to current, and returns current value |
185 |
|
*/ |
186 |
|
public void testAddAndGet() { |
187 |
|
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
195 |
|
} |
196 |
|
|
197 |
|
/** |
198 |
< |
* |
198 |
> |
* decrementAndGet decrements and returns current value |
199 |
|
*/ |
200 |
|
public void testDecrementAndGet() { |
201 |
|
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
209 |
|
} |
210 |
|
|
211 |
|
/** |
212 |
< |
* |
212 |
> |
* incrementAndGet increments and returns current value |
213 |
|
*/ |
214 |
|
public void testIncrementAndGet() { |
215 |
|
AtomicIntegerArray ai = new AtomicIntegerArray(SIZE); |
250 |
|
} |
251 |
|
|
252 |
|
/** |
253 |
< |
* |
253 |
> |
* Multiple threads using same array of counters successfully |
254 |
> |
* update a number of times equal to total count |
255 |
|
*/ |
256 |
|
public void testCountingInMultipleThreads() { |
257 |
|
try { |
275 |
|
|
276 |
|
|
277 |
|
/** |
278 |
< |
* |
278 |
> |
* a deserialized serialized array holds same values |
279 |
|
*/ |
280 |
|
public void testSerialization() { |
281 |
|
AtomicIntegerArray l = new AtomicIntegerArray(SIZE); |