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.7 by dl, Thu Jan 8 01:29:46 2004 UTC vs.
Revision 1.25 by jsr166, Fri Jun 10 20:17:11 2011 UTC

# Line 1 | Line 1
1   /*
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/licenses/publicdomain
5 < * Other contributors include Andrew Wright, Jeffrey Hayes,
6 < * Pat Fisher, Mike Judd.
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 JSR166TestCase
14 < {
15 <    public static void main (String[] args) {
16 <        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);
# Line 22 | Line 21 | public class AtomicReferenceArrayTest ex
21      /**
22       * constructor creates array of given size with all elements null
23       */
24 <    public void testConstructor(){
24 >    public void testConstructor() {
25          AtomicReferenceArray<Integer> ai = new AtomicReferenceArray<Integer>(SIZE);
26          for (int i = 0; i < SIZE; ++i) {
27              assertNull(ai.get(i));
# Line 36 | Line 35 | public class AtomicReferenceArrayTest ex
35          try {
36              Integer[] a = null;
37              AtomicReferenceArray<Integer> ai = new AtomicReferenceArray<Integer>(a);
38 <        } catch (NullPointerException success) {
39 <        } catch (Exception ex) {
41 <            unexpectedException();
42 <        }
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};
46 >        Integer[] a = { two, one, three, four, seven };
47          AtomicReferenceArray<Integer> ai = new AtomicReferenceArray<Integer>(a);
48          assertEquals(a.length, ai.length());
49 <        for (int i = 0; i < a.length; ++i)
49 >        for (int i = 0; i < a.length; ++i)
50              assertEquals(a[i], ai.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(){
71 >    public void testIndexing() {
72          AtomicReferenceArray<Integer> ai = new AtomicReferenceArray<Integer>(SIZE);
73          try {
74              ai.get(SIZE);
75 <        } catch(IndexOutOfBoundsException success){
75 >            shouldThrow();
76 >        } catch (IndexOutOfBoundsException success) {
77          }
78          try {
79              ai.get(-1);
80 <        } catch(IndexOutOfBoundsException success){
80 >            shouldThrow();
81 >        } catch (IndexOutOfBoundsException success) {
82          }
83          try {
84              ai.set(SIZE, null);
85 <        } catch(IndexOutOfBoundsException success){
85 >            shouldThrow();
86 >        } catch (IndexOutOfBoundsException success) {
87          }
88          try {
89              ai.set(-1, null);
90 <        } catch(IndexOutOfBoundsException success){
90 >            shouldThrow();
91 >        } catch (IndexOutOfBoundsException success) {
92          }
93      }
94  
95      /**
96       * get returns the last value set at index
97       */
98 <    public void testGetSet(){
99 <        AtomicReferenceArray ai = new AtomicReferenceArray(SIZE);
98 >    public void testGetSet() {
99 >        AtomicReferenceArray ai = new AtomicReferenceArray(SIZE);
100          for (int i = 0; i < SIZE; ++i) {
101              ai.set(i, one);
102 <            assertEquals(one,ai.get(i));
102 >            assertSame(one, ai.get(i));
103              ai.set(i, two);
104 <            assertEquals(two,ai.get(i));
104 >            assertSame(two, ai.get(i));
105              ai.set(i, m3);
106 <            assertEquals(m3,ai.get(i));
106 >            assertSame(m3, ai.get(i));
107 >        }
108 >    }
109 >
110 >    /**
111 >     * get returns the last value lazySet at index by same thread
112 >     */
113 >    public void testGetLazySet() {
114 >        AtomicReferenceArray ai = new AtomicReferenceArray(SIZE);
115 >        for (int i = 0; i < SIZE; ++i) {
116 >            ai.lazySet(i, one);
117 >            assertSame(one, ai.get(i));
118 >            ai.lazySet(i, two);
119 >            assertSame(two, ai.get(i));
120 >            ai.lazySet(i, m3);
121 >            assertSame(m3, ai.get(i));
122          }
123      }
124  
125      /**
126       * compareAndSet succeeds in changing value if equal to expected else fails
127       */
128 <    public void testCompareAndSet(){
129 <        AtomicReferenceArray ai = new AtomicReferenceArray(SIZE);
128 >    public void testCompareAndSet() {
129 >        AtomicReferenceArray ai = new AtomicReferenceArray(SIZE);
130          for (int i = 0; i < SIZE; ++i) {
131              ai.set(i, one);
132 <            assertTrue(ai.compareAndSet(i, one,two));
133 <            assertTrue(ai.compareAndSet(i, two,m4));
134 <            assertEquals(m4,ai.get(i));
135 <            assertFalse(ai.compareAndSet(i, m5,seven));
136 <            assertFalse((seven.equals(ai.get(i))));
137 <            assertTrue(ai.compareAndSet(i, m4,seven));
138 <            assertEquals(seven,ai.get(i));
132 >            assertTrue(ai.compareAndSet(i, one, two));
133 >            assertTrue(ai.compareAndSet(i, two, m4));
134 >            assertSame(m4, ai.get(i));
135 >            assertFalse(ai.compareAndSet(i, m5, seven));
136 >            assertSame(m4, ai.get(i));
137 >            assertTrue(ai.compareAndSet(i, m4, seven));
138 >            assertSame(seven, ai.get(i));
139          }
140      }
141  
# Line 113 | Line 143 | public class AtomicReferenceArrayTest ex
143       * compareAndSet in one thread enables another waiting for value
144       * to succeed
145       */
146 <    public void testCompareAndSetInMultipleThreads() {
146 >    public void testCompareAndSetInMultipleThreads() throws InterruptedException {
147          final AtomicReferenceArray a = new AtomicReferenceArray(1);
148          a.set(0, one);
149 <        Thread t = new Thread(new Runnable() {
150 <                public void run() {
151 <                    while(!a.compareAndSet(0, two, three)) Thread.yield();
152 <                }});
153 <        try {
154 <            t.start();
155 <            assertTrue(a.compareAndSet(0, one, two));
156 <            t.join(LONG_DELAY_MS);
157 <            assertFalse(t.isAlive());
158 <            assertEquals(a.get(0), three);
159 <        }
130 <        catch(Exception e) {
131 <            unexpectedException();
132 <        }
149 >        Thread t = new Thread(new CheckedRunnable() {
150 >            public void realRun() {
151 >                while (!a.compareAndSet(0, two, three))
152 >                    Thread.yield();
153 >            }});
154 >
155 >        t.start();
156 >        assertTrue(a.compareAndSet(0, one, two));
157 >        t.join(LONG_DELAY_MS);
158 >        assertFalse(t.isAlive());
159 >        assertSame(three, a.get(0));
160      }
161  
162      /**
163       * repeated weakCompareAndSet succeeds in changing value when equal
164 <     * to expected
164 >     * to expected
165       */
166 <    public void testWeakCompareAndSet(){
167 <        AtomicReferenceArray ai = new AtomicReferenceArray(SIZE);
166 >    public void testWeakCompareAndSet() {
167 >        AtomicReferenceArray ai = new AtomicReferenceArray(SIZE);
168          for (int i = 0; i < SIZE; ++i) {
169              ai.set(i, one);
170 <            while(!ai.weakCompareAndSet(i, one,two));
171 <            while(!ai.weakCompareAndSet(i, two,m4));
172 <            assertEquals(m4,ai.get(i));
173 <            while(!ai.weakCompareAndSet(i, m4,seven));
174 <            assertEquals(seven,ai.get(i));
170 >            while (!ai.weakCompareAndSet(i, one, two));
171 >            while (!ai.weakCompareAndSet(i, two, m4));
172 >            assertSame(m4, ai.get(i));
173 >            while (!ai.weakCompareAndSet(i, m4, seven));
174 >            assertSame(seven, ai.get(i));
175          }
176      }
177  
178      /**
179       * getAndSet returns previous value and sets to given value at given index
180       */
181 <    public void testGetAndSet(){
182 <        AtomicReferenceArray ai = new AtomicReferenceArray(SIZE);
181 >    public void testGetAndSet() {
182 >        AtomicReferenceArray ai = new AtomicReferenceArray(SIZE);
183          for (int i = 0; i < SIZE; ++i) {
184              ai.set(i, one);
185 <            assertEquals(one,ai.getAndSet(i,zero));
186 <            assertEquals(0,ai.getAndSet(i,m10));
187 <            assertEquals(m10,ai.getAndSet(i,one));
185 >            assertSame(one, ai.getAndSet(i, zero));
186 >            assertSame(zero, ai.getAndSet(i, m10));
187 >            assertSame(m10, ai.getAndSet(i, one));
188          }
189      }
190  
191      /**
192       * a deserialized serialized array holds same values
193       */
194 <    public void testSerialization() {
195 <        AtomicReferenceArray l = new AtomicReferenceArray(SIZE);
196 <        for (int i = 0; i < SIZE; ++i) {
197 <            l.set(i, new Integer(-i));
194 >    public void testSerialization() throws Exception {
195 >        AtomicReferenceArray x = new AtomicReferenceArray(SIZE);
196 >        for (int i = 0; i < SIZE; i++) {
197 >            x.set(i, new Integer(-i));
198          }
199 <
200 <        try {
201 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
202 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
203 <            out.writeObject(l);
177 <            out.close();
178 <
179 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
180 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
181 <            AtomicReferenceArray r = (AtomicReferenceArray) in.readObject();
182 <            assertEquals(l.length(), r.length());
183 <            for (int i = 0; i < SIZE; ++i) {
184 <                assertEquals(r.get(i), l.get(i));
185 <            }
186 <        } catch(Exception e){
187 <            unexpectedException();
199 >        AtomicReferenceArray y = serialClone(x);
200 >        assertTrue(x != y);
201 >        assertEquals(x.length(), y.length());
202 >        for (int i = 0; i < SIZE; i++) {
203 >            assertEquals(x.get(i), y.get(i));
204          }
205      }
206  
207 +    /**
208 +     * toString returns current value.
209 +     */
210 +    public void testToString() {
211 +        Integer[] a = { two, one, three, four, seven };
212 +        AtomicReferenceArray<Integer> ai = new AtomicReferenceArray<Integer>(a);
213 +        assertEquals(Arrays.toString(a), ai.toString());
214 +    }
215   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines