ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AtomicReferenceArrayTest.java
Revision: 1.6
Committed: Sat Dec 27 19:26:43 2003 UTC (20 years, 4 months ago) by dl
Branch: MAIN
Changes since 1.5: +5 -4 lines
Log Message:
Headers reference Creative Commons

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     * 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.1
13 dl 1.3 public class AtomicReferenceArrayTest extends JSR166TestCase
14 dl 1.1 {
15     public static void main (String[] args) {
16     junit.textui.TestRunner.run (suite());
17     }
18     public static Test suite() {
19     return new TestSuite(AtomicReferenceArrayTest.class);
20     }
21    
22 dl 1.4 /**
23 dl 1.5 * constructor creates array of given size with all elements null
24 dl 1.4 */
25 dl 1.1 public void testConstructor(){
26 dl 1.3 AtomicReferenceArray<Integer> ai = new AtomicReferenceArray<Integer>(SIZE);
27     for (int i = 0; i < SIZE; ++i) {
28 dl 1.1 assertNull(ai.get(i));
29     }
30     }
31    
32 dl 1.4 /**
33 dl 1.5 * get and set for out of bound indices throw IndexOutOfBoundsException
34     */
35     public void testIndexing(){
36     AtomicReferenceArray<Integer> ai = new AtomicReferenceArray<Integer>(SIZE);
37     try {
38     ai.get(SIZE);
39     } catch(IndexOutOfBoundsException success){
40     }
41     try {
42     ai.get(-1);
43     } catch(IndexOutOfBoundsException success){
44     }
45     try {
46     ai.set(SIZE, null);
47     } catch(IndexOutOfBoundsException success){
48     }
49     try {
50     ai.set(-1, null);
51     } catch(IndexOutOfBoundsException success){
52     }
53     }
54    
55     /**
56     * get returns the last value set at index
57 dl 1.4 */
58 dl 1.1 public void testGetSet(){
59 dl 1.3 AtomicReferenceArray ai = new AtomicReferenceArray(SIZE);
60     for (int i = 0; i < SIZE; ++i) {
61 dl 1.1 ai.set(i, one);
62     assertEquals(one,ai.get(i));
63     ai.set(i, two);
64     assertEquals(two,ai.get(i));
65     ai.set(i, m3);
66     assertEquals(m3,ai.get(i));
67     }
68     }
69    
70 dl 1.4 /**
71 dl 1.5 * compareAndSet succeeds in changing value if equal to expected else fails
72 dl 1.4 */
73 dl 1.1 public void testCompareAndSet(){
74 dl 1.3 AtomicReferenceArray ai = new AtomicReferenceArray(SIZE);
75     for (int i = 0; i < SIZE; ++i) {
76 dl 1.1 ai.set(i, one);
77     assertTrue(ai.compareAndSet(i, one,two));
78     assertTrue(ai.compareAndSet(i, two,m4));
79     assertEquals(m4,ai.get(i));
80     assertFalse(ai.compareAndSet(i, m5,seven));
81     assertFalse((seven.equals(ai.get(i))));
82     assertTrue(ai.compareAndSet(i, m4,seven));
83     assertEquals(seven,ai.get(i));
84     }
85     }
86    
87 dl 1.4 /**
88 dl 1.5 * compareAndSet in one thread enables another waiting for value
89     * to succeed
90     */
91     public void testCompareAndSetInMultipleThreads() {
92     final AtomicReferenceArray a = new AtomicReferenceArray(1);
93     a.set(0, one);
94     Thread t = new Thread(new Runnable() {
95     public void run() {
96     while(!a.compareAndSet(0, two, three)) Thread.yield();
97     }});
98     try {
99     t.start();
100     assertTrue(a.compareAndSet(0, one, two));
101     t.join(LONG_DELAY_MS);
102     assertFalse(t.isAlive());
103     assertEquals(a.get(0), three);
104     }
105     catch(Exception e) {
106     unexpectedException();
107     }
108     }
109    
110     /**
111     * repeated weakCompareAndSet succeeds in changing value when equal
112     * to expected
113 dl 1.4 */
114 dl 1.1 public void testWeakCompareAndSet(){
115 dl 1.3 AtomicReferenceArray ai = new AtomicReferenceArray(SIZE);
116     for (int i = 0; i < SIZE; ++i) {
117 dl 1.1 ai.set(i, one);
118     while(!ai.weakCompareAndSet(i, one,two));
119     while(!ai.weakCompareAndSet(i, two,m4));
120     assertEquals(m4,ai.get(i));
121     while(!ai.weakCompareAndSet(i, m4,seven));
122     assertEquals(seven,ai.get(i));
123     }
124     }
125    
126 dl 1.4 /**
127 dl 1.5 * getAndSet returns previous value and sets to given value at given index
128 dl 1.4 */
129 dl 1.1 public void testGetAndSet(){
130 dl 1.3 AtomicReferenceArray ai = new AtomicReferenceArray(SIZE);
131     for (int i = 0; i < SIZE; ++i) {
132 dl 1.1 ai.set(i, one);
133     assertEquals(one,ai.getAndSet(i,zero));
134     assertEquals(0,ai.getAndSet(i,m10));
135     assertEquals(m10,ai.getAndSet(i,one));
136     }
137     }
138    
139 dl 1.4 /**
140 dl 1.5 * a deserialized serialized array holds same values
141 dl 1.4 */
142 dl 1.2 public void testSerialization() {
143 dl 1.3 AtomicReferenceArray l = new AtomicReferenceArray(SIZE);
144     for (int i = 0; i < SIZE; ++i) {
145 dl 1.2 l.set(i, new Integer(-i));
146     }
147    
148     try {
149     ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
150     ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
151     out.writeObject(l);
152     out.close();
153    
154     ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
155     ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
156     AtomicReferenceArray r = (AtomicReferenceArray) in.readObject();
157     assertEquals(l.length(), r.length());
158 dl 1.3 for (int i = 0; i < SIZE; ++i) {
159 dl 1.2 assertEquals(r.get(i), l.get(i));
160     }
161     } catch(Exception e){
162 dl 1.4 unexpectedException();
163 dl 1.2 }
164     }
165 dl 1.1
166     }