ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AtomicReferenceArrayTest.java
Revision: 1.21
Committed: Tue May 31 16:16:23 2011 UTC (12 years, 11 months ago) by jsr166
Branch: MAIN
Changes since 1.20: +11 -19 lines
Log Message:
use serialClone in serialization tests; update imports

File Contents

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