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

# 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     Integer[] a = { two, one, three, four, seven};
47     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 dl 1.5 * get and set for out of bound indices throw IndexOutOfBoundsException
55     */
56 jsr166 1.13 public void testIndexing() {
57 dl 1.5 AtomicReferenceArray<Integer> ai = new AtomicReferenceArray<Integer>(SIZE);
58     try {
59     ai.get(SIZE);
60 jsr166 1.14 shouldThrow();
61 jsr166 1.13 } catch (IndexOutOfBoundsException success) {
62 dl 1.5 }
63     try {
64     ai.get(-1);
65 jsr166 1.14 shouldThrow();
66 jsr166 1.13 } catch (IndexOutOfBoundsException success) {
67 dl 1.5 }
68     try {
69     ai.set(SIZE, null);
70 jsr166 1.14 shouldThrow();
71 jsr166 1.13 } catch (IndexOutOfBoundsException success) {
72 dl 1.5 }
73     try {
74     ai.set(-1, null);
75 jsr166 1.14 shouldThrow();
76 jsr166 1.13 } catch (IndexOutOfBoundsException success) {
77 dl 1.5 }
78     }
79    
80     /**
81     * get returns the last value set at index
82 dl 1.4 */
83 jsr166 1.13 public void testGetSet() {
84 jsr166 1.11 AtomicReferenceArray ai = new AtomicReferenceArray(SIZE);
85 dl 1.3 for (int i = 0; i < SIZE; ++i) {
86 dl 1.1 ai.set(i, one);
87 jsr166 1.17 assertSame(one,ai.get(i));
88 dl 1.1 ai.set(i, two);
89 jsr166 1.17 assertSame(two,ai.get(i));
90 dl 1.1 ai.set(i, m3);
91 jsr166 1.17 assertSame(m3,ai.get(i));
92 dl 1.1 }
93     }
94    
95 dl 1.4 /**
96 dl 1.10 * get returns the last value lazySet at index by same thread
97     */
98 jsr166 1.13 public void testGetLazySet() {
99 jsr166 1.11 AtomicReferenceArray ai = new AtomicReferenceArray(SIZE);
100 dl 1.10 for (int i = 0; i < SIZE; ++i) {
101     ai.lazySet(i, one);
102 jsr166 1.17 assertSame(one,ai.get(i));
103 dl 1.10 ai.lazySet(i, two);
104 jsr166 1.17 assertSame(two,ai.get(i));
105 dl 1.10 ai.lazySet(i, m3);
106 jsr166 1.17 assertSame(m3,ai.get(i));
107 dl 1.10 }
108     }
109    
110     /**
111 dl 1.5 * compareAndSet succeeds in changing value if equal to expected else fails
112 dl 1.4 */
113 jsr166 1.13 public void testCompareAndSet() {
114 jsr166 1.11 AtomicReferenceArray ai = new AtomicReferenceArray(SIZE);
115 dl 1.3 for (int i = 0; i < SIZE; ++i) {
116 dl 1.1 ai.set(i, one);
117     assertTrue(ai.compareAndSet(i, one,two));
118     assertTrue(ai.compareAndSet(i, two,m4));
119 jsr166 1.17 assertSame(m4,ai.get(i));
120 dl 1.1 assertFalse(ai.compareAndSet(i, m5,seven));
121 jsr166 1.17 assertSame(m4,ai.get(i));
122 dl 1.1 assertTrue(ai.compareAndSet(i, m4,seven));
123 jsr166 1.17 assertSame(seven,ai.get(i));
124 dl 1.1 }
125     }
126    
127 dl 1.4 /**
128 dl 1.5 * compareAndSet in one thread enables another waiting for value
129     * to succeed
130     */
131 jsr166 1.15 public void testCompareAndSetInMultipleThreads() throws InterruptedException {
132 dl 1.5 final AtomicReferenceArray a = new AtomicReferenceArray(1);
133     a.set(0, one);
134 jsr166 1.16 Thread t = new Thread(new CheckedRunnable() {
135     public void realRun() {
136     while (!a.compareAndSet(0, two, three))
137     Thread.yield();
138     }});
139 jsr166 1.15
140     t.start();
141     assertTrue(a.compareAndSet(0, one, two));
142     t.join(LONG_DELAY_MS);
143     assertFalse(t.isAlive());
144 jsr166 1.17 assertSame(a.get(0), three);
145 dl 1.5 }
146    
147     /**
148     * repeated weakCompareAndSet succeeds in changing value when equal
149 jsr166 1.11 * to expected
150 dl 1.4 */
151 jsr166 1.13 public void testWeakCompareAndSet() {
152 jsr166 1.11 AtomicReferenceArray ai = new AtomicReferenceArray(SIZE);
153 dl 1.3 for (int i = 0; i < SIZE; ++i) {
154 dl 1.1 ai.set(i, one);
155 jsr166 1.12 while (!ai.weakCompareAndSet(i, one,two));
156     while (!ai.weakCompareAndSet(i, two,m4));
157 jsr166 1.17 assertSame(m4,ai.get(i));
158 jsr166 1.12 while (!ai.weakCompareAndSet(i, m4,seven));
159 jsr166 1.17 assertSame(seven,ai.get(i));
160 dl 1.1 }
161     }
162    
163 dl 1.4 /**
164 dl 1.5 * getAndSet returns previous value and sets to given value at given index
165 dl 1.4 */
166 jsr166 1.13 public void testGetAndSet() {
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.17 assertSame(one,ai.getAndSet(i,zero));
171     assertSame(zero,ai.getAndSet(i,m10));
172     assertSame(m10,ai.getAndSet(i,one));
173 dl 1.1 }
174     }
175    
176 dl 1.4 /**
177 dl 1.5 * a deserialized serialized array holds same values
178 dl 1.4 */
179 jsr166 1.15 public void testSerialization() throws Exception {
180 jsr166 1.21 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 dl 1.2 }
190     }
191 dl 1.1
192 dl 1.9 /**
193     * toString returns current value.
194 jsr166 1.11 */
195 dl 1.9 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 dl 1.1 }