ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AtomicReferenceArrayTest.java
Revision: 1.23
Committed: Fri Jun 10 19:45:01 2011 UTC (12 years, 10 months ago) by jsr166
Branch: MAIN
Changes since 1.22: +2 -2 lines
Log Message:
whitespace

File Contents

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