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