ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AtomicReferenceArrayTest.java
Revision: 1.30
Committed: Wed Dec 31 19:21:20 2014 UTC (9 years, 4 months ago) by jsr166
Branch: MAIN
Changes since 1.29: +3 -3 lines
Log Message:
prefer do {} while (...); to while (...);

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