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.6 by dl, Sat Dec 27 19:26:43 2003 UTC vs.
Revision 1.21 by jsr166, Tue May 31 16:16:23 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 30 | Line 29 | public class AtomicReferenceArrayTest ex
29      }
30  
31      /**
32 +     * constructor with null array throws NPE
33 +     */
34 +    public void testConstructor2NPE() {
35 +        try {
36 +            Integer[] a = null;
37 +            AtomicReferenceArray<Integer> ai = 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> ai = new AtomicReferenceArray<Integer>(a);
48 +        assertEquals(a.length, ai.length());
49 +        for (int i = 0; i < a.length; ++i)
50 +            assertEquals(a[i], ai.get(i));
51 +    }
52 +
53 +    /**
54       * get and set for out of bound indices throw IndexOutOfBoundsException
55       */
56 <    public void testIndexing(){
56 >    public void testIndexing() {
57          AtomicReferenceArray<Integer> ai = new AtomicReferenceArray<Integer>(SIZE);
58          try {
59              ai.get(SIZE);
60 <        } catch(IndexOutOfBoundsException success){
60 >            shouldThrow();
61 >        } catch (IndexOutOfBoundsException success) {
62          }
63          try {
64              ai.get(-1);
65 <        } catch(IndexOutOfBoundsException success){
65 >            shouldThrow();
66 >        } catch (IndexOutOfBoundsException success) {
67          }
68          try {
69              ai.set(SIZE, null);
70 <        } catch(IndexOutOfBoundsException success){
70 >            shouldThrow();
71 >        } catch (IndexOutOfBoundsException success) {
72          }
73          try {
74              ai.set(-1, null);
75 <        } catch(IndexOutOfBoundsException success){
75 >            shouldThrow();
76 >        } catch (IndexOutOfBoundsException success) {
77          }
78      }
79  
80      /**
81       * get returns the last value set at index
82       */
83 <    public void testGetSet(){
84 <        AtomicReferenceArray ai = new AtomicReferenceArray(SIZE);
83 >    public void testGetSet() {
84 >        AtomicReferenceArray ai = new AtomicReferenceArray(SIZE);
85          for (int i = 0; i < SIZE; ++i) {
86              ai.set(i, one);
87 <            assertEquals(one,ai.get(i));
87 >            assertSame(one,ai.get(i));
88              ai.set(i, two);
89 <            assertEquals(two,ai.get(i));
89 >            assertSame(two,ai.get(i));
90              ai.set(i, m3);
91 <            assertEquals(m3,ai.get(i));
91 >            assertSame(m3,ai.get(i));
92 >        }
93 >    }
94 >
95 >    /**
96 >     * get returns the last value lazySet at index by same thread
97 >     */
98 >    public void testGetLazySet() {
99 >        AtomicReferenceArray ai = new AtomicReferenceArray(SIZE);
100 >        for (int i = 0; i < SIZE; ++i) {
101 >            ai.lazySet(i, one);
102 >            assertSame(one,ai.get(i));
103 >            ai.lazySet(i, two);
104 >            assertSame(two,ai.get(i));
105 >            ai.lazySet(i, m3);
106 >            assertSame(m3,ai.get(i));
107          }
108      }
109  
110      /**
111       * compareAndSet succeeds in changing value if equal to expected else fails
112       */
113 <    public void testCompareAndSet(){
114 <        AtomicReferenceArray ai = new AtomicReferenceArray(SIZE);
113 >    public void testCompareAndSet() {
114 >        AtomicReferenceArray ai = new AtomicReferenceArray(SIZE);
115          for (int i = 0; i < SIZE; ++i) {
116              ai.set(i, one);
117              assertTrue(ai.compareAndSet(i, one,two));
118              assertTrue(ai.compareAndSet(i, two,m4));
119 <            assertEquals(m4,ai.get(i));
119 >            assertSame(m4,ai.get(i));
120              assertFalse(ai.compareAndSet(i, m5,seven));
121 <            assertFalse((seven.equals(ai.get(i))));
121 >            assertSame(m4,ai.get(i));
122              assertTrue(ai.compareAndSet(i, m4,seven));
123 <            assertEquals(seven,ai.get(i));
123 >            assertSame(seven,ai.get(i));
124          }
125      }
126  
# Line 88 | Line 128 | public class AtomicReferenceArrayTest ex
128       * compareAndSet in one thread enables another waiting for value
129       * to succeed
130       */
131 <    public void testCompareAndSetInMultipleThreads() {
131 >    public void testCompareAndSetInMultipleThreads() throws InterruptedException {
132          final AtomicReferenceArray a = new AtomicReferenceArray(1);
133          a.set(0, one);
134 <        Thread t = new Thread(new Runnable() {
135 <                public void run() {
136 <                    while(!a.compareAndSet(0, two, three)) Thread.yield();
137 <                }});
138 <        try {
139 <            t.start();
140 <            assertTrue(a.compareAndSet(0, one, two));
141 <            t.join(LONG_DELAY_MS);
142 <            assertFalse(t.isAlive());
143 <            assertEquals(a.get(0), three);
144 <        }
105 <        catch(Exception e) {
106 <            unexpectedException();
107 <        }
134 >        Thread t = new Thread(new CheckedRunnable() {
135 >            public void realRun() {
136 >                while (!a.compareAndSet(0, two, three))
137 >                    Thread.yield();
138 >            }});
139 >
140 >        t.start();
141 >        assertTrue(a.compareAndSet(0, one, two));
142 >        t.join(LONG_DELAY_MS);
143 >        assertFalse(t.isAlive());
144 >        assertSame(a.get(0), three);
145      }
146  
147      /**
148       * repeated weakCompareAndSet succeeds in changing value when equal
149 <     * to expected
149 >     * to expected
150       */
151 <    public void testWeakCompareAndSet(){
152 <        AtomicReferenceArray ai = new AtomicReferenceArray(SIZE);
151 >    public void testWeakCompareAndSet() {
152 >        AtomicReferenceArray ai = new AtomicReferenceArray(SIZE);
153          for (int i = 0; i < SIZE; ++i) {
154              ai.set(i, one);
155 <            while(!ai.weakCompareAndSet(i, one,two));
156 <            while(!ai.weakCompareAndSet(i, two,m4));
157 <            assertEquals(m4,ai.get(i));
158 <            while(!ai.weakCompareAndSet(i, m4,seven));
159 <            assertEquals(seven,ai.get(i));
155 >            while (!ai.weakCompareAndSet(i, one,two));
156 >            while (!ai.weakCompareAndSet(i, two,m4));
157 >            assertSame(m4,ai.get(i));
158 >            while (!ai.weakCompareAndSet(i, m4,seven));
159 >            assertSame(seven,ai.get(i));
160          }
161      }
162  
163      /**
164       * getAndSet returns previous value and sets to given value at given index
165       */
166 <    public void testGetAndSet(){
167 <        AtomicReferenceArray ai = new AtomicReferenceArray(SIZE);
166 >    public void testGetAndSet() {
167 >        AtomicReferenceArray ai = new AtomicReferenceArray(SIZE);
168          for (int i = 0; i < SIZE; ++i) {
169              ai.set(i, one);
170 <            assertEquals(one,ai.getAndSet(i,zero));
171 <            assertEquals(0,ai.getAndSet(i,m10));
172 <            assertEquals(m10,ai.getAndSet(i,one));
170 >            assertSame(one,ai.getAndSet(i,zero));
171 >            assertSame(zero,ai.getAndSet(i,m10));
172 >            assertSame(m10,ai.getAndSet(i,one));
173          }
174      }
175  
176      /**
177       * a deserialized serialized array holds same values
178       */
179 <    public void testSerialization() {
180 <        AtomicReferenceArray l = new AtomicReferenceArray(SIZE);
181 <        for (int i = 0; i < SIZE; ++i) {
182 <            l.set(i, new Integer(-i));
179 >    public void testSerialization() throws Exception {
180 >        AtomicReferenceArray x = new AtomicReferenceArray(SIZE);
181 >        for (int i = 0; i < SIZE; i++) {
182 >            x.set(i, new Integer(-i));
183          }
184 <
185 <        try {
186 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
187 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
188 <            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 <            for (int i = 0; i < SIZE; ++i) {
159 <                assertEquals(r.get(i), l.get(i));
160 <            }
161 <        } catch(Exception e){
162 <            unexpectedException();
184 >        AtomicReferenceArray y = serialClone(x);
185 >        assertTrue(x != y);
186 >        assertEquals(x.length(), y.length());
187 >        for (int i = 0; i < SIZE; i++) {
188 >            assertEquals(x.get(i), y.get(i));
189          }
190      }
191  
192 +    /**
193 +     * toString returns current value.
194 +     */
195 +    public void testToString() {
196 +        Integer[] a = { two, one, three, four, seven};
197 +        AtomicReferenceArray<Integer> ai = new AtomicReferenceArray<Integer>(a);
198 +        assertEquals(Arrays.toString(a), ai.toString());
199 +    }
200   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines