1 |
dl |
1.1 |
/* |
2 |
dl |
1.6 |
* 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 |
jsr166 |
1.19 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
jsr166 |
1.11 |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
|
* Pat Fisher, Mike Judd. |
7 |
dl |
1.1 |
*/ |
8 |
|
|
|
9 |
jsr166 |
1.21 |
import java.util.Arrays; |
10 |
|
|
import java.util.concurrent.atomic.AtomicReferenceArray; |
11 |
dl |
1.1 |
|
12 |
jsr166 |
1.29 |
import junit.framework.Test; |
13 |
|
|
import junit.framework.TestSuite; |
14 |
|
|
|
15 |
jsr166 |
1.13 |
public class AtomicReferenceArrayTest extends JSR166TestCase { |
16 |
jsr166 |
1.18 |
public static void main(String[] args) { |
17 |
jsr166 |
1.32 |
main(suite(), args); |
18 |
dl |
1.1 |
} |
19 |
|
|
public static Test suite() { |
20 |
|
|
return new TestSuite(AtomicReferenceArrayTest.class); |
21 |
|
|
} |
22 |
|
|
|
23 |
dl |
1.4 |
/** |
24 |
dl |
1.5 |
* constructor creates array of given size with all elements null |
25 |
dl |
1.4 |
*/ |
26 |
jsr166 |
1.13 |
public void testConstructor() { |
27 |
jsr166 |
1.36 |
AtomicReferenceArray<Integer> aa = new AtomicReferenceArray<>(SIZE); |
28 |
jsr166 |
1.26 |
for (int i = 0; i < SIZE; i++) { |
29 |
|
|
assertNull(aa.get(i)); |
30 |
dl |
1.1 |
} |
31 |
|
|
} |
32 |
|
|
|
33 |
dl |
1.4 |
/** |
34 |
dl |
1.7 |
* constructor with null array throws NPE |
35 |
|
|
*/ |
36 |
|
|
public void testConstructor2NPE() { |
37 |
|
|
try { |
38 |
|
|
Integer[] a = null; |
39 |
jsr166 |
1.31 |
new AtomicReferenceArray<Integer>(a); |
40 |
jsr166 |
1.14 |
shouldThrow(); |
41 |
jsr166 |
1.15 |
} catch (NullPointerException success) {} |
42 |
dl |
1.7 |
} |
43 |
|
|
|
44 |
|
|
/** |
45 |
|
|
* constructor with array is of same size and has all elements |
46 |
|
|
*/ |
47 |
|
|
public void testConstructor2() { |
48 |
dl |
1.39 |
Item[] a = { two, one, three, four, seven }; |
49 |
|
|
AtomicReferenceArray<Item> aa = new AtomicReferenceArray<>(a); |
50 |
jsr166 |
1.26 |
assertEquals(a.length, aa.length()); |
51 |
|
|
for (int i = 0; i < a.length; i++) |
52 |
|
|
assertEquals(a[i], aa.get(i)); |
53 |
dl |
1.7 |
} |
54 |
|
|
|
55 |
|
|
/** |
56 |
jsr166 |
1.27 |
* Initialize AtomicReferenceArray<Class> with SubClass[] |
57 |
jsr166 |
1.22 |
*/ |
58 |
|
|
public void testConstructorSubClassArray() { |
59 |
dl |
1.39 |
Item[] a = { two, one, three, four, seven }; |
60 |
jsr166 |
1.38 |
AtomicReferenceArray<Number> aa = new AtomicReferenceArray<>(a); |
61 |
jsr166 |
1.22 |
assertEquals(a.length, aa.length()); |
62 |
jsr166 |
1.26 |
for (int i = 0; i < a.length; i++) { |
63 |
jsr166 |
1.22 |
assertSame(a[i], aa.get(i)); |
64 |
|
|
Long x = Long.valueOf(i); |
65 |
|
|
aa.set(i, x); |
66 |
|
|
assertSame(x, aa.get(i)); |
67 |
|
|
} |
68 |
|
|
} |
69 |
|
|
|
70 |
|
|
/** |
71 |
dl |
1.5 |
* get and set for out of bound indices throw IndexOutOfBoundsException |
72 |
|
|
*/ |
73 |
dl |
1.39 |
@SuppressWarnings("deprecation") |
74 |
jsr166 |
1.13 |
public void testIndexing() { |
75 |
jsr166 |
1.36 |
AtomicReferenceArray<Integer> aa = new AtomicReferenceArray<>(SIZE); |
76 |
jsr166 |
1.26 |
for (int index : new int[] { -1, SIZE }) { |
77 |
|
|
try { |
78 |
|
|
aa.get(index); |
79 |
|
|
shouldThrow(); |
80 |
|
|
} catch (IndexOutOfBoundsException success) {} |
81 |
|
|
try { |
82 |
|
|
aa.set(index, null); |
83 |
|
|
shouldThrow(); |
84 |
|
|
} catch (IndexOutOfBoundsException success) {} |
85 |
|
|
try { |
86 |
|
|
aa.lazySet(index, null); |
87 |
|
|
shouldThrow(); |
88 |
|
|
} catch (IndexOutOfBoundsException success) {} |
89 |
|
|
try { |
90 |
|
|
aa.compareAndSet(index, null, null); |
91 |
|
|
shouldThrow(); |
92 |
|
|
} catch (IndexOutOfBoundsException success) {} |
93 |
|
|
try { |
94 |
|
|
aa.weakCompareAndSet(index, null, null); |
95 |
|
|
shouldThrow(); |
96 |
|
|
} catch (IndexOutOfBoundsException success) {} |
97 |
dl |
1.5 |
} |
98 |
|
|
} |
99 |
|
|
|
100 |
|
|
/** |
101 |
|
|
* get returns the last value set at index |
102 |
dl |
1.4 |
*/ |
103 |
jsr166 |
1.13 |
public void testGetSet() { |
104 |
jsr166 |
1.40 |
AtomicReferenceArray<Item> aa = new AtomicReferenceArray<>(SIZE); |
105 |
jsr166 |
1.26 |
for (int i = 0; i < SIZE; i++) { |
106 |
|
|
aa.set(i, one); |
107 |
|
|
assertSame(one, aa.get(i)); |
108 |
|
|
aa.set(i, two); |
109 |
|
|
assertSame(two, aa.get(i)); |
110 |
dl |
1.39 |
aa.set(i, minusThree); |
111 |
|
|
assertSame(minusThree, aa.get(i)); |
112 |
dl |
1.1 |
} |
113 |
|
|
} |
114 |
|
|
|
115 |
dl |
1.4 |
/** |
116 |
dl |
1.10 |
* get returns the last value lazySet at index by same thread |
117 |
|
|
*/ |
118 |
jsr166 |
1.13 |
public void testGetLazySet() { |
119 |
jsr166 |
1.40 |
AtomicReferenceArray<Item> aa = new AtomicReferenceArray<>(SIZE); |
120 |
jsr166 |
1.26 |
for (int i = 0; i < SIZE; i++) { |
121 |
|
|
aa.lazySet(i, one); |
122 |
|
|
assertSame(one, aa.get(i)); |
123 |
|
|
aa.lazySet(i, two); |
124 |
|
|
assertSame(two, aa.get(i)); |
125 |
dl |
1.39 |
aa.lazySet(i, minusThree); |
126 |
|
|
assertSame(minusThree, aa.get(i)); |
127 |
dl |
1.10 |
} |
128 |
|
|
} |
129 |
|
|
|
130 |
|
|
/** |
131 |
dl |
1.5 |
* compareAndSet succeeds in changing value if equal to expected else fails |
132 |
dl |
1.4 |
*/ |
133 |
jsr166 |
1.13 |
public void testCompareAndSet() { |
134 |
jsr166 |
1.40 |
AtomicReferenceArray<Item> aa = new AtomicReferenceArray<>(SIZE); |
135 |
jsr166 |
1.26 |
for (int i = 0; i < SIZE; i++) { |
136 |
|
|
aa.set(i, one); |
137 |
|
|
assertTrue(aa.compareAndSet(i, one, two)); |
138 |
dl |
1.39 |
assertTrue(aa.compareAndSet(i, two, minusFour)); |
139 |
|
|
assertSame(minusFour, aa.get(i)); |
140 |
|
|
assertFalse(aa.compareAndSet(i, minusFive, seven)); |
141 |
|
|
assertSame(minusFour, aa.get(i)); |
142 |
|
|
assertTrue(aa.compareAndSet(i, minusFour, seven)); |
143 |
jsr166 |
1.26 |
assertSame(seven, aa.get(i)); |
144 |
dl |
1.1 |
} |
145 |
|
|
} |
146 |
|
|
|
147 |
dl |
1.4 |
/** |
148 |
dl |
1.5 |
* compareAndSet in one thread enables another waiting for value |
149 |
|
|
* to succeed |
150 |
|
|
*/ |
151 |
jsr166 |
1.15 |
public void testCompareAndSetInMultipleThreads() throws InterruptedException { |
152 |
jsr166 |
1.40 |
final AtomicReferenceArray<Item> a = new AtomicReferenceArray<>(1); |
153 |
dl |
1.5 |
a.set(0, one); |
154 |
jsr166 |
1.16 |
Thread t = new Thread(new CheckedRunnable() { |
155 |
|
|
public void realRun() { |
156 |
|
|
while (!a.compareAndSet(0, two, three)) |
157 |
|
|
Thread.yield(); |
158 |
|
|
}}); |
159 |
jsr166 |
1.15 |
|
160 |
|
|
t.start(); |
161 |
|
|
assertTrue(a.compareAndSet(0, one, two)); |
162 |
|
|
t.join(LONG_DELAY_MS); |
163 |
|
|
assertFalse(t.isAlive()); |
164 |
jsr166 |
1.25 |
assertSame(three, a.get(0)); |
165 |
dl |
1.5 |
} |
166 |
|
|
|
167 |
|
|
/** |
168 |
|
|
* repeated weakCompareAndSet succeeds in changing value when equal |
169 |
jsr166 |
1.11 |
* to expected |
170 |
dl |
1.4 |
*/ |
171 |
dl |
1.39 |
@SuppressWarnings("deprecation") |
172 |
jsr166 |
1.13 |
public void testWeakCompareAndSet() { |
173 |
jsr166 |
1.40 |
AtomicReferenceArray<Item> aa = new AtomicReferenceArray<>(SIZE); |
174 |
jsr166 |
1.26 |
for (int i = 0; i < SIZE; i++) { |
175 |
|
|
aa.set(i, one); |
176 |
jsr166 |
1.30 |
do {} while (!aa.weakCompareAndSet(i, one, two)); |
177 |
dl |
1.39 |
do {} while (!aa.weakCompareAndSet(i, two, minusFour)); |
178 |
|
|
assertSame(minusFour, aa.get(i)); |
179 |
|
|
do {} while (!aa.weakCompareAndSet(i, minusFour, seven)); |
180 |
jsr166 |
1.26 |
assertSame(seven, aa.get(i)); |
181 |
dl |
1.1 |
} |
182 |
|
|
} |
183 |
|
|
|
184 |
dl |
1.4 |
/** |
185 |
dl |
1.5 |
* getAndSet returns previous value and sets to given value at given index |
186 |
dl |
1.4 |
*/ |
187 |
jsr166 |
1.13 |
public void testGetAndSet() { |
188 |
jsr166 |
1.40 |
AtomicReferenceArray<Item> aa = new AtomicReferenceArray<>(SIZE); |
189 |
jsr166 |
1.26 |
for (int i = 0; i < SIZE; i++) { |
190 |
|
|
aa.set(i, one); |
191 |
|
|
assertSame(one, aa.getAndSet(i, zero)); |
192 |
dl |
1.39 |
assertSame(zero, aa.getAndSet(i, minusTen)); |
193 |
|
|
assertSame(minusTen, aa.getAndSet(i, one)); |
194 |
dl |
1.1 |
} |
195 |
|
|
} |
196 |
|
|
|
197 |
dl |
1.4 |
/** |
198 |
jsr166 |
1.37 |
* a deserialized/reserialized array holds same values in same order |
199 |
dl |
1.4 |
*/ |
200 |
jsr166 |
1.15 |
public void testSerialization() throws Exception { |
201 |
jsr166 |
1.40 |
AtomicReferenceArray<Item> x = new AtomicReferenceArray<>(SIZE); |
202 |
jsr166 |
1.21 |
for (int i = 0; i < SIZE; i++) { |
203 |
dl |
1.39 |
x.set(i, minusOne); |
204 |
jsr166 |
1.21 |
} |
205 |
dl |
1.39 |
AtomicReferenceArray<Item> y = serialClone(x); |
206 |
jsr166 |
1.28 |
assertNotSame(x, y); |
207 |
jsr166 |
1.21 |
assertEquals(x.length(), y.length()); |
208 |
|
|
for (int i = 0; i < SIZE; i++) { |
209 |
|
|
assertEquals(x.get(i), y.get(i)); |
210 |
dl |
1.2 |
} |
211 |
|
|
} |
212 |
dl |
1.1 |
|
213 |
dl |
1.9 |
/** |
214 |
|
|
* toString returns current value. |
215 |
jsr166 |
1.11 |
*/ |
216 |
dl |
1.9 |
public void testToString() { |
217 |
dl |
1.39 |
Item[] a = { two, one, three, four, seven }; |
218 |
|
|
AtomicReferenceArray<Item> aa = new AtomicReferenceArray<>(a); |
219 |
jsr166 |
1.26 |
assertEquals(Arrays.toString(a), aa.toString()); |
220 |
dl |
1.9 |
} |
221 |
dl |
1.33 |
|
222 |
dl |
1.1 |
} |