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 |
|
|
* 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.1 |
|
13 |
dl |
1.3 |
public class AtomicReferenceArrayTest extends JSR166TestCase |
14 |
dl |
1.1 |
{ |
15 |
|
|
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 |
dl |
1.1 |
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 |
|
|
} catch (NullPointerException success) { |
40 |
|
|
} catch (Exception ex) { |
41 |
|
|
unexpectedException(); |
42 |
|
|
} |
43 |
|
|
} |
44 |
|
|
|
45 |
|
|
/** |
46 |
|
|
* constructor with array is of same size and has all elements |
47 |
|
|
*/ |
48 |
|
|
public void testConstructor2() { |
49 |
|
|
Integer[] a = { two, one, three, four, seven}; |
50 |
|
|
AtomicReferenceArray<Integer> ai = new AtomicReferenceArray<Integer>(a); |
51 |
|
|
assertEquals(a.length, ai.length()); |
52 |
|
|
for (int i = 0; i < a.length; ++i) |
53 |
|
|
assertEquals(a[i], ai.get(i)); |
54 |
|
|
} |
55 |
|
|
|
56 |
|
|
|
57 |
|
|
/** |
58 |
dl |
1.5 |
* get and set for out of bound indices throw IndexOutOfBoundsException |
59 |
|
|
*/ |
60 |
|
|
public void testIndexing(){ |
61 |
|
|
AtomicReferenceArray<Integer> ai = new AtomicReferenceArray<Integer>(SIZE); |
62 |
|
|
try { |
63 |
|
|
ai.get(SIZE); |
64 |
|
|
} catch(IndexOutOfBoundsException success){ |
65 |
|
|
} |
66 |
|
|
try { |
67 |
|
|
ai.get(-1); |
68 |
|
|
} catch(IndexOutOfBoundsException success){ |
69 |
|
|
} |
70 |
|
|
try { |
71 |
|
|
ai.set(SIZE, null); |
72 |
|
|
} catch(IndexOutOfBoundsException success){ |
73 |
|
|
} |
74 |
|
|
try { |
75 |
|
|
ai.set(-1, null); |
76 |
|
|
} catch(IndexOutOfBoundsException success){ |
77 |
|
|
} |
78 |
|
|
} |
79 |
|
|
|
80 |
|
|
/** |
81 |
|
|
* get returns the last value set at index |
82 |
dl |
1.4 |
*/ |
83 |
dl |
1.1 |
public void testGetSet(){ |
84 |
dl |
1.3 |
AtomicReferenceArray ai = new AtomicReferenceArray(SIZE); |
85 |
|
|
for (int i = 0; i < SIZE; ++i) { |
86 |
dl |
1.1 |
ai.set(i, one); |
87 |
|
|
assertEquals(one,ai.get(i)); |
88 |
|
|
ai.set(i, two); |
89 |
|
|
assertEquals(two,ai.get(i)); |
90 |
|
|
ai.set(i, m3); |
91 |
|
|
assertEquals(m3,ai.get(i)); |
92 |
|
|
} |
93 |
|
|
} |
94 |
|
|
|
95 |
dl |
1.4 |
/** |
96 |
dl |
1.5 |
* compareAndSet succeeds in changing value if equal to expected else fails |
97 |
dl |
1.4 |
*/ |
98 |
dl |
1.1 |
public void testCompareAndSet(){ |
99 |
dl |
1.3 |
AtomicReferenceArray ai = new AtomicReferenceArray(SIZE); |
100 |
|
|
for (int i = 0; i < SIZE; ++i) { |
101 |
dl |
1.1 |
ai.set(i, one); |
102 |
|
|
assertTrue(ai.compareAndSet(i, one,two)); |
103 |
|
|
assertTrue(ai.compareAndSet(i, two,m4)); |
104 |
|
|
assertEquals(m4,ai.get(i)); |
105 |
|
|
assertFalse(ai.compareAndSet(i, m5,seven)); |
106 |
|
|
assertFalse((seven.equals(ai.get(i)))); |
107 |
|
|
assertTrue(ai.compareAndSet(i, m4,seven)); |
108 |
|
|
assertEquals(seven,ai.get(i)); |
109 |
|
|
} |
110 |
|
|
} |
111 |
|
|
|
112 |
dl |
1.4 |
/** |
113 |
dl |
1.5 |
* compareAndSet in one thread enables another waiting for value |
114 |
|
|
* to succeed |
115 |
|
|
*/ |
116 |
|
|
public void testCompareAndSetInMultipleThreads() { |
117 |
|
|
final AtomicReferenceArray a = new AtomicReferenceArray(1); |
118 |
|
|
a.set(0, one); |
119 |
|
|
Thread t = new Thread(new Runnable() { |
120 |
|
|
public void run() { |
121 |
|
|
while(!a.compareAndSet(0, two, three)) Thread.yield(); |
122 |
|
|
}}); |
123 |
|
|
try { |
124 |
|
|
t.start(); |
125 |
|
|
assertTrue(a.compareAndSet(0, one, two)); |
126 |
|
|
t.join(LONG_DELAY_MS); |
127 |
|
|
assertFalse(t.isAlive()); |
128 |
|
|
assertEquals(a.get(0), three); |
129 |
|
|
} |
130 |
|
|
catch(Exception e) { |
131 |
|
|
unexpectedException(); |
132 |
|
|
} |
133 |
|
|
} |
134 |
|
|
|
135 |
|
|
/** |
136 |
|
|
* repeated weakCompareAndSet succeeds in changing value when equal |
137 |
|
|
* to expected |
138 |
dl |
1.4 |
*/ |
139 |
dl |
1.1 |
public void testWeakCompareAndSet(){ |
140 |
dl |
1.3 |
AtomicReferenceArray ai = new AtomicReferenceArray(SIZE); |
141 |
|
|
for (int i = 0; i < SIZE; ++i) { |
142 |
dl |
1.1 |
ai.set(i, one); |
143 |
|
|
while(!ai.weakCompareAndSet(i, one,two)); |
144 |
|
|
while(!ai.weakCompareAndSet(i, two,m4)); |
145 |
|
|
assertEquals(m4,ai.get(i)); |
146 |
|
|
while(!ai.weakCompareAndSet(i, m4,seven)); |
147 |
|
|
assertEquals(seven,ai.get(i)); |
148 |
|
|
} |
149 |
|
|
} |
150 |
|
|
|
151 |
dl |
1.4 |
/** |
152 |
dl |
1.5 |
* getAndSet returns previous value and sets to given value at given index |
153 |
dl |
1.4 |
*/ |
154 |
dl |
1.1 |
public void testGetAndSet(){ |
155 |
dl |
1.3 |
AtomicReferenceArray ai = new AtomicReferenceArray(SIZE); |
156 |
|
|
for (int i = 0; i < SIZE; ++i) { |
157 |
dl |
1.1 |
ai.set(i, one); |
158 |
|
|
assertEquals(one,ai.getAndSet(i,zero)); |
159 |
|
|
assertEquals(0,ai.getAndSet(i,m10)); |
160 |
|
|
assertEquals(m10,ai.getAndSet(i,one)); |
161 |
|
|
} |
162 |
|
|
} |
163 |
|
|
|
164 |
dl |
1.4 |
/** |
165 |
dl |
1.5 |
* a deserialized serialized array holds same values |
166 |
dl |
1.4 |
*/ |
167 |
dl |
1.2 |
public void testSerialization() { |
168 |
dl |
1.3 |
AtomicReferenceArray l = new AtomicReferenceArray(SIZE); |
169 |
|
|
for (int i = 0; i < SIZE; ++i) { |
170 |
dl |
1.2 |
l.set(i, new Integer(-i)); |
171 |
|
|
} |
172 |
|
|
|
173 |
|
|
try { |
174 |
|
|
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
175 |
|
|
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
176 |
|
|
out.writeObject(l); |
177 |
|
|
out.close(); |
178 |
|
|
|
179 |
|
|
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
180 |
|
|
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
181 |
|
|
AtomicReferenceArray r = (AtomicReferenceArray) in.readObject(); |
182 |
|
|
assertEquals(l.length(), r.length()); |
183 |
dl |
1.3 |
for (int i = 0; i < SIZE; ++i) { |
184 |
dl |
1.2 |
assertEquals(r.get(i), l.get(i)); |
185 |
|
|
} |
186 |
|
|
} catch(Exception e){ |
187 |
dl |
1.4 |
unexpectedException(); |
188 |
dl |
1.2 |
} |
189 |
|
|
} |
190 |
dl |
1.1 |
|
191 |
|
|
} |