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.*; |
10 |
|
import java.util.concurrent.atomic.*; |
11 |
|
import java.io.*; |
12 |
+ |
import java.util.*; |
13 |
|
|
14 |
< |
public class AtomicReferenceArrayTest extends JSR166TestCase |
13 |
< |
{ |
14 |
> |
public class AtomicReferenceArrayTest extends JSR166TestCase { |
15 |
|
public static void main (String[] args) { |
16 |
|
junit.textui.TestRunner.run (suite()); |
17 |
|
} |
20 |
|
} |
21 |
|
|
22 |
|
/** |
23 |
< |
* |
23 |
> |
* constructor creates array of given size with all elements null |
24 |
|
*/ |
25 |
< |
public void testConstructor(){ |
25 |
> |
public void testConstructor() { |
26 |
|
AtomicReferenceArray<Integer> ai = new AtomicReferenceArray<Integer>(SIZE); |
27 |
|
for (int i = 0; i < SIZE; ++i) { |
28 |
|
assertNull(ai.get(i)); |
30 |
|
} |
31 |
|
|
32 |
|
/** |
33 |
< |
* |
33 |
> |
* constructor with null array throws NPE |
34 |
|
*/ |
35 |
< |
public void testGetSet(){ |
36 |
< |
AtomicReferenceArray ai = new AtomicReferenceArray(SIZE); |
35 |
> |
public void testConstructor2NPE() { |
36 |
> |
try { |
37 |
> |
Integer[] a = null; |
38 |
> |
AtomicReferenceArray<Integer> ai = new AtomicReferenceArray<Integer>(a); |
39 |
> |
shouldThrow(); |
40 |
> |
} catch (NullPointerException success) {} |
41 |
> |
} |
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 |
> |
for (int i = 0; i < a.length; ++i) |
51 |
> |
assertEquals(a[i], ai.get(i)); |
52 |
> |
} |
53 |
> |
|
54 |
> |
|
55 |
> |
/** |
56 |
> |
* get and set for out of bound indices throw IndexOutOfBoundsException |
57 |
> |
*/ |
58 |
> |
public void testIndexing() { |
59 |
> |
AtomicReferenceArray<Integer> ai = new AtomicReferenceArray<Integer>(SIZE); |
60 |
> |
try { |
61 |
> |
ai.get(SIZE); |
62 |
> |
shouldThrow(); |
63 |
> |
} catch (IndexOutOfBoundsException success) { |
64 |
> |
} |
65 |
> |
try { |
66 |
> |
ai.get(-1); |
67 |
> |
shouldThrow(); |
68 |
> |
} catch (IndexOutOfBoundsException success) { |
69 |
> |
} |
70 |
> |
try { |
71 |
> |
ai.set(SIZE, null); |
72 |
> |
shouldThrow(); |
73 |
> |
} catch (IndexOutOfBoundsException success) { |
74 |
> |
} |
75 |
> |
try { |
76 |
> |
ai.set(-1, null); |
77 |
> |
shouldThrow(); |
78 |
> |
} catch (IndexOutOfBoundsException success) { |
79 |
> |
} |
80 |
> |
} |
81 |
> |
|
82 |
> |
/** |
83 |
> |
* get returns the last value set at index |
84 |
> |
*/ |
85 |
> |
public void testGetSet() { |
86 |
> |
AtomicReferenceArray ai = new AtomicReferenceArray(SIZE); |
87 |
|
for (int i = 0; i < SIZE; ++i) { |
88 |
|
ai.set(i, one); |
89 |
|
assertEquals(one,ai.get(i)); |
95 |
|
} |
96 |
|
|
97 |
|
/** |
98 |
< |
* |
98 |
> |
* get returns the last value lazySet at index by same thread |
99 |
|
*/ |
100 |
< |
public void testCompareAndSet(){ |
101 |
< |
AtomicReferenceArray ai = new AtomicReferenceArray(SIZE); |
100 |
> |
public void testGetLazySet() { |
101 |
> |
AtomicReferenceArray ai = new AtomicReferenceArray(SIZE); |
102 |
> |
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 |
> |
* compareAndSet succeeds in changing value if equal to expected else fails |
114 |
> |
*/ |
115 |
> |
public void testCompareAndSet() { |
116 |
> |
AtomicReferenceArray ai = new AtomicReferenceArray(SIZE); |
117 |
|
for (int i = 0; i < SIZE; ++i) { |
118 |
|
ai.set(i, one); |
119 |
|
assertTrue(ai.compareAndSet(i, one,two)); |
127 |
|
} |
128 |
|
|
129 |
|
/** |
130 |
< |
* |
130 |
> |
* compareAndSet in one thread enables another waiting for value |
131 |
> |
* to succeed |
132 |
|
*/ |
133 |
< |
public void testWeakCompareAndSet(){ |
134 |
< |
AtomicReferenceArray ai = new AtomicReferenceArray(SIZE); |
133 |
> |
public void testCompareAndSetInMultipleThreads() throws InterruptedException { |
134 |
> |
final AtomicReferenceArray a = new AtomicReferenceArray(1); |
135 |
> |
a.set(0, one); |
136 |
> |
Thread t = new Thread(new CheckedRunnable() { |
137 |
> |
public void realRun() { |
138 |
> |
while (!a.compareAndSet(0, two, three)) |
139 |
> |
Thread.yield(); |
140 |
> |
}}); |
141 |
> |
|
142 |
> |
t.start(); |
143 |
> |
assertTrue(a.compareAndSet(0, one, two)); |
144 |
> |
t.join(LONG_DELAY_MS); |
145 |
> |
assertFalse(t.isAlive()); |
146 |
> |
assertEquals(a.get(0), three); |
147 |
> |
} |
148 |
> |
|
149 |
> |
/** |
150 |
> |
* repeated weakCompareAndSet succeeds in changing value when equal |
151 |
> |
* to expected |
152 |
> |
*/ |
153 |
> |
public void testWeakCompareAndSet() { |
154 |
> |
AtomicReferenceArray ai = new AtomicReferenceArray(SIZE); |
155 |
|
for (int i = 0; i < SIZE; ++i) { |
156 |
|
ai.set(i, one); |
157 |
< |
while(!ai.weakCompareAndSet(i, one,two)); |
158 |
< |
while(!ai.weakCompareAndSet(i, two,m4)); |
157 |
> |
while (!ai.weakCompareAndSet(i, one,two)); |
158 |
> |
while (!ai.weakCompareAndSet(i, two,m4)); |
159 |
|
assertEquals(m4,ai.get(i)); |
160 |
< |
while(!ai.weakCompareAndSet(i, m4,seven)); |
160 |
> |
while (!ai.weakCompareAndSet(i, m4,seven)); |
161 |
|
assertEquals(seven,ai.get(i)); |
162 |
|
} |
163 |
|
} |
164 |
|
|
165 |
|
/** |
166 |
< |
* |
166 |
> |
* getAndSet returns previous value and sets to given value at given index |
167 |
|
*/ |
168 |
< |
public void testGetAndSet(){ |
169 |
< |
AtomicReferenceArray ai = new AtomicReferenceArray(SIZE); |
168 |
> |
public void testGetAndSet() { |
169 |
> |
AtomicReferenceArray ai = new AtomicReferenceArray(SIZE); |
170 |
|
for (int i = 0; i < SIZE; ++i) { |
171 |
|
ai.set(i, one); |
172 |
|
assertEquals(one,ai.getAndSet(i,zero)); |
176 |
|
} |
177 |
|
|
178 |
|
/** |
179 |
< |
* |
179 |
> |
* a deserialized serialized array holds same values |
180 |
|
*/ |
181 |
< |
public void testSerialization() { |
182 |
< |
AtomicReferenceArray l = new AtomicReferenceArray(SIZE); |
181 |
> |
public void testSerialization() throws Exception { |
182 |
> |
AtomicReferenceArray l = new AtomicReferenceArray(SIZE); |
183 |
|
for (int i = 0; i < SIZE; ++i) { |
184 |
|
l.set(i, new Integer(-i)); |
185 |
|
} |
186 |
|
|
187 |
< |
try { |
188 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
189 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
190 |
< |
out.writeObject(l); |
191 |
< |
out.close(); |
192 |
< |
|
193 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
194 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
195 |
< |
AtomicReferenceArray r = (AtomicReferenceArray) in.readObject(); |
196 |
< |
assertEquals(l.length(), r.length()); |
197 |
< |
for (int i = 0; i < SIZE; ++i) { |
111 |
< |
assertEquals(r.get(i), l.get(i)); |
112 |
< |
} |
113 |
< |
} catch(Exception e){ |
114 |
< |
unexpectedException(); |
187 |
> |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
188 |
> |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
189 |
> |
out.writeObject(l); |
190 |
> |
out.close(); |
191 |
> |
|
192 |
> |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
193 |
> |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
194 |
> |
AtomicReferenceArray r = (AtomicReferenceArray) in.readObject(); |
195 |
> |
assertEquals(l.length(), r.length()); |
196 |
> |
for (int i = 0; i < SIZE; ++i) { |
197 |
> |
assertEquals(r.get(i), l.get(i)); |
198 |
|
} |
199 |
|
} |
200 |
|
|
201 |
+ |
|
202 |
+ |
/** |
203 |
+ |
* toString returns current value. |
204 |
+ |
*/ |
205 |
+ |
public void testToString() { |
206 |
+ |
Integer[] a = { two, one, three, four, seven}; |
207 |
+ |
AtomicReferenceArray<Integer> ai = new AtomicReferenceArray<Integer>(a); |
208 |
+ |
assertEquals(Arrays.toString(a), ai.toString()); |
209 |
+ |
} |
210 |
|
} |