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.*; |
19 |
|
return new TestSuite(AtomicReferenceArrayTest.class); |
20 |
|
} |
21 |
|
|
22 |
+ |
/** |
23 |
+ |
* constructor creates array of given size with all elements null |
24 |
+ |
*/ |
25 |
|
public void testConstructor(){ |
26 |
|
AtomicReferenceArray<Integer> ai = new AtomicReferenceArray<Integer>(SIZE); |
27 |
|
for (int i = 0; i < SIZE; ++i) { |
29 |
|
} |
30 |
|
} |
31 |
|
|
32 |
+ |
/** |
33 |
+ |
* get and set for out of bound indices throw IndexOutOfBoundsException |
34 |
+ |
*/ |
35 |
+ |
public void testIndexing(){ |
36 |
+ |
AtomicReferenceArray<Integer> ai = new AtomicReferenceArray<Integer>(SIZE); |
37 |
+ |
try { |
38 |
+ |
ai.get(SIZE); |
39 |
+ |
} catch(IndexOutOfBoundsException success){ |
40 |
+ |
} |
41 |
+ |
try { |
42 |
+ |
ai.get(-1); |
43 |
+ |
} catch(IndexOutOfBoundsException success){ |
44 |
+ |
} |
45 |
+ |
try { |
46 |
+ |
ai.set(SIZE, null); |
47 |
+ |
} catch(IndexOutOfBoundsException success){ |
48 |
+ |
} |
49 |
+ |
try { |
50 |
+ |
ai.set(-1, null); |
51 |
+ |
} catch(IndexOutOfBoundsException success){ |
52 |
+ |
} |
53 |
+ |
} |
54 |
+ |
|
55 |
+ |
/** |
56 |
+ |
* get returns the last value set at index |
57 |
+ |
*/ |
58 |
|
public void testGetSet(){ |
59 |
|
AtomicReferenceArray ai = new AtomicReferenceArray(SIZE); |
60 |
|
for (int i = 0; i < SIZE; ++i) { |
67 |
|
} |
68 |
|
} |
69 |
|
|
70 |
+ |
/** |
71 |
+ |
* compareAndSet succeeds in changing value if equal to expected else fails |
72 |
+ |
*/ |
73 |
|
public void testCompareAndSet(){ |
74 |
|
AtomicReferenceArray ai = new AtomicReferenceArray(SIZE); |
75 |
|
for (int i = 0; i < SIZE; ++i) { |
84 |
|
} |
85 |
|
} |
86 |
|
|
87 |
+ |
/** |
88 |
+ |
* compareAndSet in one thread enables another waiting for value |
89 |
+ |
* to succeed |
90 |
+ |
*/ |
91 |
+ |
public void testCompareAndSetInMultipleThreads() { |
92 |
+ |
final AtomicReferenceArray a = new AtomicReferenceArray(1); |
93 |
+ |
a.set(0, one); |
94 |
+ |
Thread t = new Thread(new Runnable() { |
95 |
+ |
public void run() { |
96 |
+ |
while(!a.compareAndSet(0, two, three)) Thread.yield(); |
97 |
+ |
}}); |
98 |
+ |
try { |
99 |
+ |
t.start(); |
100 |
+ |
assertTrue(a.compareAndSet(0, one, two)); |
101 |
+ |
t.join(LONG_DELAY_MS); |
102 |
+ |
assertFalse(t.isAlive()); |
103 |
+ |
assertEquals(a.get(0), three); |
104 |
+ |
} |
105 |
+ |
catch(Exception e) { |
106 |
+ |
unexpectedException(); |
107 |
+ |
} |
108 |
+ |
} |
109 |
+ |
|
110 |
+ |
/** |
111 |
+ |
* repeated weakCompareAndSet succeeds in changing value when equal |
112 |
+ |
* to expected |
113 |
+ |
*/ |
114 |
|
public void testWeakCompareAndSet(){ |
115 |
|
AtomicReferenceArray ai = new AtomicReferenceArray(SIZE); |
116 |
|
for (int i = 0; i < SIZE; ++i) { |
123 |
|
} |
124 |
|
} |
125 |
|
|
126 |
+ |
/** |
127 |
+ |
* getAndSet returns previous value and sets to given value at given index |
128 |
+ |
*/ |
129 |
|
public void testGetAndSet(){ |
130 |
|
AtomicReferenceArray ai = new AtomicReferenceArray(SIZE); |
131 |
|
for (int i = 0; i < SIZE; ++i) { |
136 |
|
} |
137 |
|
} |
138 |
|
|
139 |
+ |
/** |
140 |
+ |
* a deserialized serialized array holds same values |
141 |
+ |
*/ |
142 |
|
public void testSerialization() { |
143 |
|
AtomicReferenceArray l = new AtomicReferenceArray(SIZE); |
144 |
|
for (int i = 0; i < SIZE; ++i) { |
159 |
|
assertEquals(r.get(i), l.get(i)); |
160 |
|
} |
161 |
|
} catch(Exception e){ |
162 |
< |
e.printStackTrace(); |
97 |
< |
fail("unexpected exception"); |
162 |
> |
unexpectedException(); |
163 |
|
} |
164 |
|
} |
165 |
|
|