ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AtomicReferenceArrayTest.java
(Generate patch)

Comparing jsr166/src/test/tck/AtomicReferenceArrayTest.java (file contents):
Revision 1.2 by dl, Sun Sep 7 20:39:11 2003 UTC vs.
Revision 1.26 by jsr166, Wed Aug 10 07:14:48 2011 UTC

# Line 1 | Line 1
1   /*
2 < * Written by members of JCP JSR-166 Expert Group and released to the
3 < * public domain. Use, modify, and redistribute this code in any way
4 < * without acknowledgement. Other contributors include Andrew Wright,
5 < * Jeffrey Hayes, Pat Fischer, Mike Judd.
2 > * 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/publicdomain/zero/1.0/
5 > * Other contributors include Andrew Wright, Jeffrey Hayes,
6 > * Pat Fisher, Mike Judd.
7   */
8  
9   import junit.framework.*;
10 < import java.util.concurrent.atomic.*;
11 < import java.io.*;
10 > import java.util.Arrays;
11 > import java.util.concurrent.atomic.AtomicReferenceArray;
12  
13 < public class AtomicReferenceArrayTest extends TestCase
14 < {
15 <    static final int N = 10;
15 <
16 <    static final Integer zero = new Integer(0);
17 <    static final Integer one = new Integer(1);
18 <    static final Integer two = new Integer(2);
19 <    static final Integer m3  = new Integer(-3);
20 <    static final Integer m4 = new Integer(-4);
21 <    static final Integer m5 = new Integer(-5);
22 <    static final Integer seven = new Integer(7);
23 <    static final Integer m10 = new Integer(-10);
24 <
25 <    public static void main (String[] args) {
26 <        junit.textui.TestRunner.run (suite());
13 > public class AtomicReferenceArrayTest extends JSR166TestCase {
14 >    public static void main(String[] args) {
15 >        junit.textui.TestRunner.run(suite());
16      }
17      public static Test suite() {
18          return new TestSuite(AtomicReferenceArrayTest.class);
19      }
20  
21 <    public void testConstructor(){
22 <        AtomicReferenceArray<Integer> ai = new AtomicReferenceArray<Integer>(N);
23 <        for (int i = 0; i < N; ++i) {
24 <            assertNull(ai.get(i));
21 >    /**
22 >     * constructor creates array of given size with all elements null
23 >     */
24 >    public void testConstructor() {
25 >        AtomicReferenceArray<Integer> aa = new AtomicReferenceArray<Integer>(SIZE);
26 >        for (int i = 0; i < SIZE; i++) {
27 >            assertNull(aa.get(i));
28          }
29      }
30  
31 <    public void testGetSet(){
32 <        AtomicReferenceArray ai = new AtomicReferenceArray(N);
33 <        for (int i = 0; i < N; ++i) {
34 <            ai.set(i, one);
43 <            assertEquals(one,ai.get(i));
44 <            ai.set(i, two);
45 <            assertEquals(two,ai.get(i));
46 <            ai.set(i, m3);
47 <            assertEquals(m3,ai.get(i));
48 <        }
49 <    }
50 <
51 <    public void testCompareAndSet(){
52 <        AtomicReferenceArray ai = new AtomicReferenceArray(N);
53 <        for (int i = 0; i < N; ++i) {
54 <            ai.set(i, one);
55 <            assertTrue(ai.compareAndSet(i, one,two));
56 <            assertTrue(ai.compareAndSet(i, two,m4));
57 <            assertEquals(m4,ai.get(i));
58 <            assertFalse(ai.compareAndSet(i, m5,seven));
59 <            assertFalse((seven.equals(ai.get(i))));
60 <            assertTrue(ai.compareAndSet(i, m4,seven));
61 <            assertEquals(seven,ai.get(i));
62 <        }
63 <    }
64 <
65 <    public void testWeakCompareAndSet(){
66 <        AtomicReferenceArray ai = new AtomicReferenceArray(N);
67 <        for (int i = 0; i < N; ++i) {
68 <            ai.set(i, one);
69 <            while(!ai.weakCompareAndSet(i, one,two));
70 <            while(!ai.weakCompareAndSet(i, two,m4));
71 <            assertEquals(m4,ai.get(i));
72 <            while(!ai.weakCompareAndSet(i, m4,seven));
73 <            assertEquals(seven,ai.get(i));
74 <        }
75 <    }
76 <
77 <    public void testGetAndSet(){
78 <        AtomicReferenceArray ai = new AtomicReferenceArray(N);
79 <        for (int i = 0; i < N; ++i) {
80 <            ai.set(i, one);
81 <            assertEquals(one,ai.getAndSet(i,zero));
82 <            assertEquals(0,ai.getAndSet(i,m10));
83 <            assertEquals(m10,ai.getAndSet(i,one));
84 <        }
85 <    }
86 <
87 <    public void testSerialization() {
88 <        AtomicReferenceArray l = new AtomicReferenceArray(10);
89 <        for (int i = 0; i < 10; ++i) {
90 <            l.set(i, new Integer(-i));
91 <        }
92 <
31 >    /**
32 >     * constructor with null array throws NPE
33 >     */
34 >    public void testConstructor2NPE() {
35          try {
36 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
37 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
38 <            out.writeObject(l);
39 <            out.close();
40 <
41 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
42 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
43 <            AtomicReferenceArray r = (AtomicReferenceArray) in.readObject();
44 <            assertEquals(l.length(), r.length());
45 <            for (int i = 0; i < 10; ++i) {
46 <                assertEquals(r.get(i), l.get(i));
47 <            }
48 <        } catch(Exception e){
49 <            e.printStackTrace();
50 <            fail("unexpected exception");
51 <        }
36 >            Integer[] a = null;
37 >            AtomicReferenceArray<Integer> aa = new AtomicReferenceArray<Integer>(a);
38 >            shouldThrow();
39 >        } catch (NullPointerException success) {}
40 >    }
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> aa = new AtomicReferenceArray<Integer>(a);
48 >        assertEquals(a.length, aa.length());
49 >        for (int i = 0; i < a.length; i++)
50 >            assertEquals(a[i], aa.get(i));
51 >    }
52 >
53 >    /**
54 >     * 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 >     * get and set for out of bound indices throw IndexOutOfBoundsException
70 >     */
71 >    public void testIndexing() {
72 >        AtomicReferenceArray<Integer> aa = new AtomicReferenceArray<Integer>(SIZE);
73 >        for (int index : new int[] { -1, SIZE }) {
74 >            try {
75 >                aa.get(index);
76 >                shouldThrow();
77 >            } catch (IndexOutOfBoundsException success) {}
78 >            try {
79 >                aa.set(index, null);
80 >                shouldThrow();
81 >            } catch (IndexOutOfBoundsException success) {}
82 >            try {
83 >                aa.lazySet(index, null);
84 >                shouldThrow();
85 >            } catch (IndexOutOfBoundsException success) {}
86 >            try {
87 >                aa.compareAndSet(index, null, null);
88 >                shouldThrow();
89 >            } catch (IndexOutOfBoundsException success) {}
90 >            try {
91 >                aa.weakCompareAndSet(index, null, null);
92 >                shouldThrow();
93 >            } catch (IndexOutOfBoundsException success) {}
94 >        }
95 >    }
96 >
97 >    /**
98 >     * get returns the last value set at index
99 >     */
100 >    public void testGetSet() {
101 >        AtomicReferenceArray aa = new AtomicReferenceArray(SIZE);
102 >        for (int i = 0; i < SIZE; i++) {
103 >            aa.set(i, one);
104 >            assertSame(one, aa.get(i));
105 >            aa.set(i, two);
106 >            assertSame(two, aa.get(i));
107 >            aa.set(i, m3);
108 >            assertSame(m3, aa.get(i));
109 >        }
110 >    }
111 >
112 >    /**
113 >     * get returns the last value lazySet at index by same thread
114 >     */
115 >    public void testGetLazySet() {
116 >        AtomicReferenceArray aa = new AtomicReferenceArray(SIZE);
117 >        for (int i = 0; i < SIZE; i++) {
118 >            aa.lazySet(i, one);
119 >            assertSame(one, aa.get(i));
120 >            aa.lazySet(i, two);
121 >            assertSame(two, aa.get(i));
122 >            aa.lazySet(i, m3);
123 >            assertSame(m3, aa.get(i));
124 >        }
125 >    }
126 >
127 >    /**
128 >     * compareAndSet succeeds in changing value if equal to expected else fails
129 >     */
130 >    public void testCompareAndSet() {
131 >        AtomicReferenceArray aa = new AtomicReferenceArray(SIZE);
132 >        for (int i = 0; i < SIZE; i++) {
133 >            aa.set(i, one);
134 >            assertTrue(aa.compareAndSet(i, one, two));
135 >            assertTrue(aa.compareAndSet(i, two, m4));
136 >            assertSame(m4, aa.get(i));
137 >            assertFalse(aa.compareAndSet(i, m5, seven));
138 >            assertSame(m4, aa.get(i));
139 >            assertTrue(aa.compareAndSet(i, m4, seven));
140 >            assertSame(seven, aa.get(i));
141 >        }
142 >    }
143 >
144 >    /**
145 >     * compareAndSet in one thread enables another waiting for value
146 >     * to succeed
147 >     */
148 >    public void testCompareAndSetInMultipleThreads() throws InterruptedException {
149 >        final AtomicReferenceArray a = new AtomicReferenceArray(1);
150 >        a.set(0, one);
151 >        Thread t = new Thread(new CheckedRunnable() {
152 >            public void realRun() {
153 >                while (!a.compareAndSet(0, two, three))
154 >                    Thread.yield();
155 >            }});
156 >
157 >        t.start();
158 >        assertTrue(a.compareAndSet(0, one, two));
159 >        t.join(LONG_DELAY_MS);
160 >        assertFalse(t.isAlive());
161 >        assertSame(three, a.get(0));
162 >    }
163 >
164 >    /**
165 >     * repeated weakCompareAndSet succeeds in changing value when equal
166 >     * to expected
167 >     */
168 >    public void testWeakCompareAndSet() {
169 >        AtomicReferenceArray aa = new AtomicReferenceArray(SIZE);
170 >        for (int i = 0; i < SIZE; i++) {
171 >            aa.set(i, one);
172 >            while (!aa.weakCompareAndSet(i, one, two));
173 >            while (!aa.weakCompareAndSet(i, two, m4));
174 >            assertSame(m4, aa.get(i));
175 >            while (!aa.weakCompareAndSet(i, m4, seven));
176 >            assertSame(seven, aa.get(i));
177 >        }
178 >    }
179 >
180 >    /**
181 >     * getAndSet returns previous value and sets to given value at given index
182 >     */
183 >    public void testGetAndSet() {
184 >        AtomicReferenceArray aa = new AtomicReferenceArray(SIZE);
185 >        for (int i = 0; i < SIZE; i++) {
186 >            aa.set(i, one);
187 >            assertSame(one, aa.getAndSet(i, zero));
188 >            assertSame(zero, aa.getAndSet(i, m10));
189 >            assertSame(m10, aa.getAndSet(i, one));
190 >        }
191 >    }
192 >
193 >    /**
194 >     * a deserialized serialized array holds same values
195 >     */
196 >    public void testSerialization() throws Exception {
197 >        AtomicReferenceArray x = new AtomicReferenceArray(SIZE);
198 >        for (int i = 0; i < SIZE; i++) {
199 >            x.set(i, new Integer(-i));
200 >        }
201 >        AtomicReferenceArray y = serialClone(x);
202 >        assertTrue(x != y);
203 >        assertEquals(x.length(), y.length());
204 >        for (int i = 0; i < SIZE; i++) {
205 >            assertEquals(x.get(i), y.get(i));
206 >        }
207 >    }
208 >
209 >    /**
210 >     * toString returns current value.
211 >     */
212 >    public void testToString() {
213 >        Integer[] a = { two, one, three, four, seven };
214 >        AtomicReferenceArray<Integer> aa = new AtomicReferenceArray<Integer>(a);
215 >        assertEquals(Arrays.toString(a), aa.toString());
216      }
111
217   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines