ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AtomicReferenceArrayTest.java
Revision: 1.12
Committed: Mon Nov 16 04:57:10 2009 UTC (14 years, 5 months ago) by jsr166
Branch: MAIN
Changes since 1.11: +10 -10 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     * http://creativecommons.org/licenses/publicdomain
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     import java.util.concurrent.atomic.*;
11 dl 1.2 import java.io.*;
12 dl 1.9 import java.util.*;
13 dl 1.1
14 jsr166 1.11 public class AtomicReferenceArrayTest extends JSR166TestCase
15 dl 1.1 {
16     public static void main (String[] args) {
17     junit.textui.TestRunner.run (suite());
18     }
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 dl 1.1 public void testConstructor(){
27 dl 1.3 AtomicReferenceArray<Integer> ai = new AtomicReferenceArray<Integer>(SIZE);
28     for (int i = 0; i < SIZE; ++i) {
29 dl 1.1 assertNull(ai.get(i));
30     }
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     AtomicReferenceArray<Integer> ai = new AtomicReferenceArray<Integer>(a);
40     } catch (NullPointerException success) {
41     } catch (Exception ex) {
42     unexpectedException();
43     }
44     }
45    
46     /**
47     * constructor with array is of same size and has all elements
48     */
49     public void testConstructor2() {
50     Integer[] a = { two, one, three, four, seven};
51     AtomicReferenceArray<Integer> ai = new AtomicReferenceArray<Integer>(a);
52     assertEquals(a.length, ai.length());
53 jsr166 1.11 for (int i = 0; i < a.length; ++i)
54 dl 1.7 assertEquals(a[i], ai.get(i));
55     }
56    
57    
58     /**
59 dl 1.5 * get and set for out of bound indices throw IndexOutOfBoundsException
60     */
61     public void testIndexing(){
62     AtomicReferenceArray<Integer> ai = new AtomicReferenceArray<Integer>(SIZE);
63     try {
64     ai.get(SIZE);
65 jsr166 1.12 } catch (IndexOutOfBoundsException success){
66 dl 1.5 }
67     try {
68     ai.get(-1);
69 jsr166 1.12 } catch (IndexOutOfBoundsException success){
70 dl 1.5 }
71     try {
72     ai.set(SIZE, null);
73 jsr166 1.12 } catch (IndexOutOfBoundsException success){
74 dl 1.5 }
75     try {
76     ai.set(-1, null);
77 jsr166 1.12 } catch (IndexOutOfBoundsException success){
78 dl 1.5 }
79     }
80    
81     /**
82     * get returns the last value set at index
83 dl 1.4 */
84 dl 1.1 public void testGetSet(){
85 jsr166 1.11 AtomicReferenceArray ai = new AtomicReferenceArray(SIZE);
86 dl 1.3 for (int i = 0; i < SIZE; ++i) {
87 dl 1.1 ai.set(i, one);
88     assertEquals(one,ai.get(i));
89     ai.set(i, two);
90     assertEquals(two,ai.get(i));
91     ai.set(i, m3);
92     assertEquals(m3,ai.get(i));
93     }
94     }
95    
96 dl 1.4 /**
97 dl 1.10 * get returns the last value lazySet at index by same thread
98     */
99     public void testGetLazySet(){
100 jsr166 1.11 AtomicReferenceArray ai = new AtomicReferenceArray(SIZE);
101 dl 1.10 for (int i = 0; i < SIZE; ++i) {
102     ai.lazySet(i, one);
103     assertEquals(one,ai.get(i));
104     ai.lazySet(i, two);
105     assertEquals(two,ai.get(i));
106     ai.lazySet(i, m3);
107     assertEquals(m3,ai.get(i));
108     }
109     }
110    
111     /**
112 dl 1.5 * compareAndSet succeeds in changing value if equal to expected else fails
113 dl 1.4 */
114 dl 1.1 public void testCompareAndSet(){
115 jsr166 1.11 AtomicReferenceArray ai = new AtomicReferenceArray(SIZE);
116 dl 1.3 for (int i = 0; i < SIZE; ++i) {
117 dl 1.1 ai.set(i, one);
118     assertTrue(ai.compareAndSet(i, one,two));
119     assertTrue(ai.compareAndSet(i, two,m4));
120     assertEquals(m4,ai.get(i));
121     assertFalse(ai.compareAndSet(i, m5,seven));
122     assertFalse((seven.equals(ai.get(i))));
123     assertTrue(ai.compareAndSet(i, m4,seven));
124     assertEquals(seven,ai.get(i));
125     }
126     }
127    
128 dl 1.4 /**
129 dl 1.5 * compareAndSet in one thread enables another waiting for value
130     * to succeed
131     */
132     public void testCompareAndSetInMultipleThreads() {
133     final AtomicReferenceArray a = new AtomicReferenceArray(1);
134     a.set(0, one);
135     Thread t = new Thread(new Runnable() {
136     public void run() {
137 jsr166 1.12 while (!a.compareAndSet(0, two, three)) Thread.yield();
138 dl 1.5 }});
139     try {
140     t.start();
141     assertTrue(a.compareAndSet(0, one, two));
142     t.join(LONG_DELAY_MS);
143     assertFalse(t.isAlive());
144     assertEquals(a.get(0), three);
145     }
146 jsr166 1.12 catch (Exception e) {
147 dl 1.5 unexpectedException();
148     }
149     }
150    
151     /**
152     * repeated weakCompareAndSet succeeds in changing value when equal
153 jsr166 1.11 * to expected
154 dl 1.4 */
155 dl 1.1 public void testWeakCompareAndSet(){
156 jsr166 1.11 AtomicReferenceArray ai = new AtomicReferenceArray(SIZE);
157 dl 1.3 for (int i = 0; i < SIZE; ++i) {
158 dl 1.1 ai.set(i, one);
159 jsr166 1.12 while (!ai.weakCompareAndSet(i, one,two));
160     while (!ai.weakCompareAndSet(i, two,m4));
161 dl 1.1 assertEquals(m4,ai.get(i));
162 jsr166 1.12 while (!ai.weakCompareAndSet(i, m4,seven));
163 dl 1.1 assertEquals(seven,ai.get(i));
164     }
165     }
166    
167 dl 1.4 /**
168 dl 1.5 * getAndSet returns previous value and sets to given value at given index
169 dl 1.4 */
170 dl 1.1 public void testGetAndSet(){
171 jsr166 1.11 AtomicReferenceArray ai = new AtomicReferenceArray(SIZE);
172 dl 1.3 for (int i = 0; i < SIZE; ++i) {
173 dl 1.1 ai.set(i, one);
174     assertEquals(one,ai.getAndSet(i,zero));
175     assertEquals(0,ai.getAndSet(i,m10));
176     assertEquals(m10,ai.getAndSet(i,one));
177     }
178     }
179    
180 dl 1.4 /**
181 dl 1.5 * a deserialized serialized array holds same values
182 dl 1.4 */
183 dl 1.2 public void testSerialization() {
184 jsr166 1.11 AtomicReferenceArray l = new AtomicReferenceArray(SIZE);
185 dl 1.3 for (int i = 0; i < SIZE; ++i) {
186 dl 1.2 l.set(i, new Integer(-i));
187     }
188    
189     try {
190     ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
191     ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
192     out.writeObject(l);
193     out.close();
194    
195     ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
196     ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
197     AtomicReferenceArray r = (AtomicReferenceArray) in.readObject();
198     assertEquals(l.length(), r.length());
199 dl 1.3 for (int i = 0; i < SIZE; ++i) {
200 dl 1.2 assertEquals(r.get(i), l.get(i));
201     }
202 jsr166 1.12 } catch (Exception e){
203 dl 1.4 unexpectedException();
204 dl 1.2 }
205     }
206 dl 1.1
207 dl 1.8
208 dl 1.9 /**
209     * toString returns current value.
210 jsr166 1.11 */
211 dl 1.9 public void testToString() {
212     Integer[] a = { two, one, three, four, seven};
213     AtomicReferenceArray<Integer> ai = new AtomicReferenceArray<Integer>(a);
214     assertEquals(Arrays.toString(a), ai.toString());
215     }
216 dl 1.1 }