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. |
5 |
> |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
> |
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
|
import junit.framework.*; |
11 |
|
import java.io.*; |
12 |
|
import java.util.*; |
13 |
|
|
14 |
< |
public class AtomicReferenceArrayTest extends JSR166TestCase |
14 |
> |
public class AtomicReferenceArrayTest extends JSR166TestCase |
15 |
|
{ |
16 |
|
public static void main (String[] args) { |
17 |
|
junit.textui.TestRunner.run (suite()); |
50 |
|
Integer[] a = { two, one, three, four, seven}; |
51 |
|
AtomicReferenceArray<Integer> ai = new AtomicReferenceArray<Integer>(a); |
52 |
|
assertEquals(a.length, ai.length()); |
53 |
< |
for (int i = 0; i < a.length; ++i) |
53 |
> |
for (int i = 0; i < a.length; ++i) |
54 |
|
assertEquals(a[i], ai.get(i)); |
55 |
|
} |
56 |
|
|
62 |
|
AtomicReferenceArray<Integer> ai = new AtomicReferenceArray<Integer>(SIZE); |
63 |
|
try { |
64 |
|
ai.get(SIZE); |
65 |
< |
} catch(IndexOutOfBoundsException success){ |
65 |
> |
} catch (IndexOutOfBoundsException success){ |
66 |
|
} |
67 |
|
try { |
68 |
|
ai.get(-1); |
69 |
< |
} catch(IndexOutOfBoundsException success){ |
69 |
> |
} catch (IndexOutOfBoundsException success){ |
70 |
|
} |
71 |
|
try { |
72 |
|
ai.set(SIZE, null); |
73 |
< |
} catch(IndexOutOfBoundsException success){ |
73 |
> |
} catch (IndexOutOfBoundsException success){ |
74 |
|
} |
75 |
|
try { |
76 |
|
ai.set(-1, null); |
77 |
< |
} catch(IndexOutOfBoundsException success){ |
77 |
> |
} catch (IndexOutOfBoundsException success){ |
78 |
|
} |
79 |
|
} |
80 |
|
|
82 |
|
* get returns the last value set at index |
83 |
|
*/ |
84 |
|
public void testGetSet(){ |
85 |
< |
AtomicReferenceArray ai = new AtomicReferenceArray(SIZE); |
85 |
> |
AtomicReferenceArray ai = new AtomicReferenceArray(SIZE); |
86 |
|
for (int i = 0; i < SIZE; ++i) { |
87 |
|
ai.set(i, one); |
88 |
|
assertEquals(one,ai.get(i)); |
94 |
|
} |
95 |
|
|
96 |
|
/** |
97 |
+ |
* get returns the last value lazySet at index by same thread |
98 |
+ |
*/ |
99 |
+ |
public void testGetLazySet(){ |
100 |
+ |
AtomicReferenceArray ai = new AtomicReferenceArray(SIZE); |
101 |
+ |
for (int i = 0; i < SIZE; ++i) { |
102 |
+ |
ai.lazySet(i, one); |
103 |
+ |
assertEquals(one,ai.get(i)); |
104 |
+ |
ai.lazySet(i, two); |
105 |
+ |
assertEquals(two,ai.get(i)); |
106 |
+ |
ai.lazySet(i, m3); |
107 |
+ |
assertEquals(m3,ai.get(i)); |
108 |
+ |
} |
109 |
+ |
} |
110 |
+ |
|
111 |
+ |
/** |
112 |
|
* compareAndSet succeeds in changing value if equal to expected else fails |
113 |
|
*/ |
114 |
|
public void testCompareAndSet(){ |
115 |
< |
AtomicReferenceArray ai = new AtomicReferenceArray(SIZE); |
115 |
> |
AtomicReferenceArray ai = new AtomicReferenceArray(SIZE); |
116 |
|
for (int i = 0; i < SIZE; ++i) { |
117 |
|
ai.set(i, one); |
118 |
|
assertTrue(ai.compareAndSet(i, one,two)); |
134 |
|
a.set(0, one); |
135 |
|
Thread t = new Thread(new Runnable() { |
136 |
|
public void run() { |
137 |
< |
while(!a.compareAndSet(0, two, three)) Thread.yield(); |
137 |
> |
while (!a.compareAndSet(0, two, three)) Thread.yield(); |
138 |
|
}}); |
139 |
|
try { |
140 |
|
t.start(); |
143 |
|
assertFalse(t.isAlive()); |
144 |
|
assertEquals(a.get(0), three); |
145 |
|
} |
146 |
< |
catch(Exception e) { |
146 |
> |
catch (Exception e) { |
147 |
|
unexpectedException(); |
148 |
|
} |
149 |
|
} |
150 |
|
|
151 |
|
/** |
152 |
|
* repeated weakCompareAndSet succeeds in changing value when equal |
153 |
< |
* to expected |
153 |
> |
* to expected |
154 |
|
*/ |
155 |
|
public void testWeakCompareAndSet(){ |
156 |
< |
AtomicReferenceArray ai = new AtomicReferenceArray(SIZE); |
156 |
> |
AtomicReferenceArray ai = new AtomicReferenceArray(SIZE); |
157 |
|
for (int i = 0; i < SIZE; ++i) { |
158 |
|
ai.set(i, one); |
159 |
< |
while(!ai.weakCompareAndSet(i, one,two)); |
160 |
< |
while(!ai.weakCompareAndSet(i, two,m4)); |
159 |
> |
while (!ai.weakCompareAndSet(i, one,two)); |
160 |
> |
while (!ai.weakCompareAndSet(i, two,m4)); |
161 |
|
assertEquals(m4,ai.get(i)); |
162 |
< |
while(!ai.weakCompareAndSet(i, m4,seven)); |
162 |
> |
while (!ai.weakCompareAndSet(i, m4,seven)); |
163 |
|
assertEquals(seven,ai.get(i)); |
164 |
|
} |
165 |
|
} |
168 |
|
* getAndSet returns previous value and sets to given value at given index |
169 |
|
*/ |
170 |
|
public void testGetAndSet(){ |
171 |
< |
AtomicReferenceArray ai = new AtomicReferenceArray(SIZE); |
171 |
> |
AtomicReferenceArray ai = new AtomicReferenceArray(SIZE); |
172 |
|
for (int i = 0; i < SIZE; ++i) { |
173 |
|
ai.set(i, one); |
174 |
|
assertEquals(one,ai.getAndSet(i,zero)); |
181 |
|
* a deserialized serialized array holds same values |
182 |
|
*/ |
183 |
|
public void testSerialization() { |
184 |
< |
AtomicReferenceArray l = new AtomicReferenceArray(SIZE); |
184 |
> |
AtomicReferenceArray l = new AtomicReferenceArray(SIZE); |
185 |
|
for (int i = 0; i < SIZE; ++i) { |
186 |
|
l.set(i, new Integer(-i)); |
187 |
|
} |
199 |
|
for (int i = 0; i < SIZE; ++i) { |
200 |
|
assertEquals(r.get(i), l.get(i)); |
201 |
|
} |
202 |
< |
} catch(Exception e){ |
202 |
> |
} catch (Exception e){ |
203 |
|
unexpectedException(); |
204 |
|
} |
205 |
|
} |
207 |
|
|
208 |
|
/** |
209 |
|
* toString returns current value. |
210 |
< |
*/ |
210 |
> |
*/ |
211 |
|
public void testToString() { |
212 |
|
Integer[] a = { two, one, three, four, seven}; |
213 |
|
AtomicReferenceArray<Integer> ai = new AtomicReferenceArray<Integer>(a); |