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.14 by jsr166, Tue Nov 17 02:50:57 2009 UTC vs.
Revision 1.19 by jsr166, Tue Mar 15 19:47:06 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   */
# Line 12 | Line 12 | import java.io.*;
12   import java.util.*;
13  
14   public class AtomicReferenceArrayTest extends JSR166TestCase {
15 <    public static void main (String[] args) {
16 <        junit.textui.TestRunner.run (suite());
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);
# Line 37 | Line 37 | public class AtomicReferenceArrayTest ex
37              Integer[] a = null;
38              AtomicReferenceArray<Integer> ai = new AtomicReferenceArray<Integer>(a);
39              shouldThrow();
40 <        } catch (NullPointerException success) {
40 >        } catch (NullPointerException success) {}
41      }
42  
43      /**
# Line 86 | Line 86 | public class AtomicReferenceArrayTest ex
86          AtomicReferenceArray ai = new AtomicReferenceArray(SIZE);
87          for (int i = 0; i < SIZE; ++i) {
88              ai.set(i, one);
89 <            assertEquals(one,ai.get(i));
89 >            assertSame(one,ai.get(i));
90              ai.set(i, two);
91 <            assertEquals(two,ai.get(i));
91 >            assertSame(two,ai.get(i));
92              ai.set(i, m3);
93 <            assertEquals(m3,ai.get(i));
93 >            assertSame(m3,ai.get(i));
94          }
95      }
96  
# Line 101 | Line 101 | public class AtomicReferenceArrayTest ex
101          AtomicReferenceArray ai = new AtomicReferenceArray(SIZE);
102          for (int i = 0; i < SIZE; ++i) {
103              ai.lazySet(i, one);
104 <            assertEquals(one,ai.get(i));
104 >            assertSame(one,ai.get(i));
105              ai.lazySet(i, two);
106 <            assertEquals(two,ai.get(i));
106 >            assertSame(two,ai.get(i));
107              ai.lazySet(i, m3);
108 <            assertEquals(m3,ai.get(i));
108 >            assertSame(m3,ai.get(i));
109          }
110      }
111  
# Line 118 | Line 118 | public class AtomicReferenceArrayTest ex
118              ai.set(i, one);
119              assertTrue(ai.compareAndSet(i, one,two));
120              assertTrue(ai.compareAndSet(i, two,m4));
121 <            assertEquals(m4,ai.get(i));
121 >            assertSame(m4,ai.get(i));
122              assertFalse(ai.compareAndSet(i, m5,seven));
123 <            assertFalse((seven.equals(ai.get(i))));
123 >            assertSame(m4,ai.get(i));
124              assertTrue(ai.compareAndSet(i, m4,seven));
125 <            assertEquals(seven,ai.get(i));
125 >            assertSame(seven,ai.get(i));
126          }
127      }
128  
# Line 130 | Line 130 | public class AtomicReferenceArrayTest ex
130       * compareAndSet in one thread enables another waiting for value
131       * to succeed
132       */
133 <    public void testCompareAndSetInMultipleThreads() {
133 >    public void testCompareAndSetInMultipleThreads() throws InterruptedException {
134          final AtomicReferenceArray a = new AtomicReferenceArray(1);
135          a.set(0, one);
136 <        Thread t = new Thread(new Runnable() {
137 <                public void run() {
138 <                    while (!a.compareAndSet(0, two, three)) Thread.yield();
139 <                }});
140 <        try {
141 <            t.start();
142 <            assertTrue(a.compareAndSet(0, one, two));
143 <            t.join(LONG_DELAY_MS);
144 <            assertFalse(t.isAlive());
145 <            assertEquals(a.get(0), three);
146 <        }
147 <        catch (Exception e) {
148 <            unexpectedException();
149 <        }
136 >        Thread t = new Thread(new CheckedRunnable() {
137 >            public void realRun() {
138 >                while (!a.compareAndSet(0, two, three))
139 >                    Thread.yield();
140 >            }});
141 >
142 >        t.start();
143 >        assertTrue(a.compareAndSet(0, one, two));
144 >        t.join(LONG_DELAY_MS);
145 >        assertFalse(t.isAlive());
146 >        assertSame(a.get(0), three);
147      }
148  
149      /**
# Line 159 | Line 156 | public class AtomicReferenceArrayTest ex
156              ai.set(i, one);
157              while (!ai.weakCompareAndSet(i, one,two));
158              while (!ai.weakCompareAndSet(i, two,m4));
159 <            assertEquals(m4,ai.get(i));
159 >            assertSame(m4,ai.get(i));
160              while (!ai.weakCompareAndSet(i, m4,seven));
161 <            assertEquals(seven,ai.get(i));
161 >            assertSame(seven,ai.get(i));
162          }
163      }
164  
# Line 172 | Line 169 | public class AtomicReferenceArrayTest ex
169          AtomicReferenceArray ai = new AtomicReferenceArray(SIZE);
170          for (int i = 0; i < SIZE; ++i) {
171              ai.set(i, one);
172 <            assertEquals(one,ai.getAndSet(i,zero));
173 <            assertEquals(0,ai.getAndSet(i,m10));
174 <            assertEquals(m10,ai.getAndSet(i,one));
172 >            assertSame(one,ai.getAndSet(i,zero));
173 >            assertSame(zero,ai.getAndSet(i,m10));
174 >            assertSame(m10,ai.getAndSet(i,one));
175          }
176      }
177  
178      /**
179       * a deserialized serialized array holds same values
180       */
181 <    public void testSerialization() {
181 >    public void testSerialization() throws Exception {
182          AtomicReferenceArray l = new AtomicReferenceArray(SIZE);
183          for (int i = 0; i < SIZE; ++i) {
184              l.set(i, new Integer(-i));
185          }
186  
187 <        try {
188 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
189 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
190 <            out.writeObject(l);
191 <            out.close();
192 <
193 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
194 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
195 <            AtomicReferenceArray r = (AtomicReferenceArray) in.readObject();
196 <            assertEquals(l.length(), r.length());
197 <            for (int i = 0; i < SIZE; ++i) {
201 <                assertEquals(r.get(i), l.get(i));
202 <            }
203 <        } catch (Exception e) {
204 <            unexpectedException();
187 >        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
188 >        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
189 >        out.writeObject(l);
190 >        out.close();
191 >
192 >        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
193 >        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
194 >        AtomicReferenceArray r = (AtomicReferenceArray) in.readObject();
195 >        assertEquals(l.length(), r.length());
196 >        for (int i = 0; i < SIZE; ++i) {
197 >            assertEquals(r.get(i), l.get(i));
198          }
199      }
200  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines