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.13 by jsr166, Mon Nov 16 05:30:07 2009 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
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.*;
12 < import java.util.*;
10 > import java.util.Arrays;
11 > import java.util.concurrent.atomic.AtomicReferenceArray;
12  
13   public class AtomicReferenceArrayTest extends JSR166TestCase {
14 <    public static void main (String[] args) {
15 <        junit.textui.TestRunner.run (suite());
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 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      /**
# Line 53 | Line 50 | public class AtomicReferenceArrayTest ex
50              assertEquals(a[i], ai.get(i));
51      }
52  
56
53      /**
54       * get and set for out of bound indices throw IndexOutOfBoundsException
55       */
# Line 61 | Line 57 | public class AtomicReferenceArrayTest ex
57          AtomicReferenceArray<Integer> ai = new AtomicReferenceArray<Integer>(SIZE);
58          try {
59              ai.get(SIZE);
60 +            shouldThrow();
61          } catch (IndexOutOfBoundsException success) {
62          }
63          try {
64              ai.get(-1);
65 +            shouldThrow();
66          } catch (IndexOutOfBoundsException success) {
67          }
68          try {
69              ai.set(SIZE, null);
70 +            shouldThrow();
71          } catch (IndexOutOfBoundsException success) {
72          }
73          try {
74              ai.set(-1, null);
75 +            shouldThrow();
76          } catch (IndexOutOfBoundsException success) {
77          }
78      }
# Line 84 | Line 84 | public class AtomicReferenceArrayTest ex
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  
# Line 99 | Line 99 | public class AtomicReferenceArrayTest ex
99          AtomicReferenceArray ai = new AtomicReferenceArray(SIZE);
100          for (int i = 0; i < SIZE; ++i) {
101              ai.lazySet(i, one);
102 <            assertEquals(one,ai.get(i));
102 >            assertSame(one,ai.get(i));
103              ai.lazySet(i, two);
104 <            assertEquals(two,ai.get(i));
104 >            assertSame(two,ai.get(i));
105              ai.lazySet(i, m3);
106 <            assertEquals(m3,ai.get(i));
106 >            assertSame(m3,ai.get(i));
107          }
108      }
109  
# Line 116 | Line 116 | public class AtomicReferenceArrayTest ex
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 128 | 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 <        }
145 <        catch (Exception e) {
146 <            unexpectedException();
147 <        }
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      /**
# Line 157 | Line 154 | public class AtomicReferenceArrayTest ex
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));
157 >            assertSame(m4,ai.get(i));
158              while (!ai.weakCompareAndSet(i, m4,seven));
159 <            assertEquals(seven,ai.get(i));
159 >            assertSame(seven,ai.get(i));
160          }
161      }
162  
# Line 170 | Line 167 | public class AtomicReferenceArrayTest ex
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));
183 <        }
184 <
185 <        try {
186 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
187 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
188 <            out.writeObject(l);
192 <            out.close();
193 <
194 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
195 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
196 <            AtomicReferenceArray r = (AtomicReferenceArray) in.readObject();
197 <            assertEquals(l.length(), r.length());
198 <            for (int i = 0; i < SIZE; ++i) {
199 <                assertEquals(r.get(i), l.get(i));
200 <            }
201 <        } catch (Exception e) {
202 <            unexpectedException();
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 >        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  
206
192      /**
193       * toString returns current value.
194       */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines