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.11 by jsr166, Mon Nov 2 20:28:31 2009 UTC vs.
Revision 1.20 by jsr166, Fri May 27 19:39:07 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 11 | Line 11 | import java.util.concurrent.atomic.*;
11   import java.io.*;
12   import java.util.*;
13  
14 < public class AtomicReferenceArrayTest extends JSR166TestCase
15 < {
16 <    public static void main (String[] args) {
17 <        junit.textui.TestRunner.run (suite());
14 > public class AtomicReferenceArrayTest extends JSR166TestCase {
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 23 | Line 22 | public class AtomicReferenceArrayTest ex
22      /**
23       * constructor creates array of given size with all elements null
24       */
25 <    public void testConstructor(){
25 >    public void testConstructor() {
26          AtomicReferenceArray<Integer> ai = new AtomicReferenceArray<Integer>(SIZE);
27          for (int i = 0; i < SIZE; ++i) {
28              assertNull(ai.get(i));
# Line 37 | Line 36 | public class AtomicReferenceArrayTest ex
36          try {
37              Integer[] a = null;
38              AtomicReferenceArray<Integer> ai = new AtomicReferenceArray<Integer>(a);
39 <        } catch (NullPointerException success) {
40 <        } catch (Exception ex) {
42 <            unexpectedException();
43 <        }
39 >            shouldThrow();
40 >        } catch (NullPointerException success) {}
41      }
42  
43      /**
# Line 54 | Line 51 | public class AtomicReferenceArrayTest ex
51              assertEquals(a[i], ai.get(i));
52      }
53  
57
54      /**
55       * get and set for out of bound indices throw IndexOutOfBoundsException
56       */
57 <    public void testIndexing(){
57 >    public void testIndexing() {
58          AtomicReferenceArray<Integer> ai = new AtomicReferenceArray<Integer>(SIZE);
59          try {
60              ai.get(SIZE);
61 <        } catch(IndexOutOfBoundsException success){
61 >            shouldThrow();
62 >        } catch (IndexOutOfBoundsException success) {
63          }
64          try {
65              ai.get(-1);
66 <        } catch(IndexOutOfBoundsException success){
66 >            shouldThrow();
67 >        } catch (IndexOutOfBoundsException success) {
68          }
69          try {
70              ai.set(SIZE, null);
71 <        } catch(IndexOutOfBoundsException success){
71 >            shouldThrow();
72 >        } catch (IndexOutOfBoundsException success) {
73          }
74          try {
75              ai.set(-1, null);
76 <        } catch(IndexOutOfBoundsException success){
76 >            shouldThrow();
77 >        } catch (IndexOutOfBoundsException success) {
78          }
79      }
80  
81      /**
82       * get returns the last value set at index
83       */
84 <    public void testGetSet(){
84 >    public void testGetSet() {
85          AtomicReferenceArray ai = new AtomicReferenceArray(SIZE);
86          for (int i = 0; i < SIZE; ++i) {
87              ai.set(i, one);
88 <            assertEquals(one,ai.get(i));
88 >            assertSame(one,ai.get(i));
89              ai.set(i, two);
90 <            assertEquals(two,ai.get(i));
90 >            assertSame(two,ai.get(i));
91              ai.set(i, m3);
92 <            assertEquals(m3,ai.get(i));
92 >            assertSame(m3,ai.get(i));
93          }
94      }
95  
96      /**
97       * get returns the last value lazySet at index by same thread
98       */
99 <    public void testGetLazySet(){
99 >    public void testGetLazySet() {
100          AtomicReferenceArray ai = new AtomicReferenceArray(SIZE);
101          for (int i = 0; i < SIZE; ++i) {
102              ai.lazySet(i, one);
103 <            assertEquals(one,ai.get(i));
103 >            assertSame(one,ai.get(i));
104              ai.lazySet(i, two);
105 <            assertEquals(two,ai.get(i));
105 >            assertSame(two,ai.get(i));
106              ai.lazySet(i, m3);
107 <            assertEquals(m3,ai.get(i));
107 >            assertSame(m3,ai.get(i));
108          }
109      }
110  
111      /**
112       * compareAndSet succeeds in changing value if equal to expected else fails
113       */
114 <    public void testCompareAndSet(){
114 >    public void testCompareAndSet() {
115          AtomicReferenceArray ai = new AtomicReferenceArray(SIZE);
116          for (int i = 0; i < SIZE; ++i) {
117              ai.set(i, one);
118              assertTrue(ai.compareAndSet(i, one,two));
119              assertTrue(ai.compareAndSet(i, two,m4));
120 <            assertEquals(m4,ai.get(i));
120 >            assertSame(m4,ai.get(i));
121              assertFalse(ai.compareAndSet(i, m5,seven));
122 <            assertFalse((seven.equals(ai.get(i))));
122 >            assertSame(m4,ai.get(i));
123              assertTrue(ai.compareAndSet(i, m4,seven));
124 <            assertEquals(seven,ai.get(i));
124 >            assertSame(seven,ai.get(i));
125          }
126      }
127  
# Line 129 | Line 129 | public class AtomicReferenceArrayTest ex
129       * compareAndSet in one thread enables another waiting for value
130       * to succeed
131       */
132 <    public void testCompareAndSetInMultipleThreads() {
132 >    public void testCompareAndSetInMultipleThreads() throws InterruptedException {
133          final AtomicReferenceArray a = new AtomicReferenceArray(1);
134          a.set(0, one);
135 <        Thread t = new Thread(new Runnable() {
136 <                public void run() {
137 <                    while(!a.compareAndSet(0, two, three)) Thread.yield();
138 <                }});
139 <        try {
140 <            t.start();
141 <            assertTrue(a.compareAndSet(0, one, two));
142 <            t.join(LONG_DELAY_MS);
143 <            assertFalse(t.isAlive());
144 <            assertEquals(a.get(0), three);
145 <        }
146 <        catch(Exception e) {
147 <            unexpectedException();
148 <        }
135 >        Thread t = new Thread(new CheckedRunnable() {
136 >            public void realRun() {
137 >                while (!a.compareAndSet(0, two, three))
138 >                    Thread.yield();
139 >            }});
140 >
141 >        t.start();
142 >        assertTrue(a.compareAndSet(0, one, two));
143 >        t.join(LONG_DELAY_MS);
144 >        assertFalse(t.isAlive());
145 >        assertSame(a.get(0), three);
146      }
147  
148      /**
149       * repeated weakCompareAndSet succeeds in changing value when equal
150       * to expected
151       */
152 <    public void testWeakCompareAndSet(){
152 >    public void testWeakCompareAndSet() {
153          AtomicReferenceArray ai = new AtomicReferenceArray(SIZE);
154          for (int i = 0; i < SIZE; ++i) {
155              ai.set(i, one);
156 <            while(!ai.weakCompareAndSet(i, one,two));
157 <            while(!ai.weakCompareAndSet(i, two,m4));
158 <            assertEquals(m4,ai.get(i));
159 <            while(!ai.weakCompareAndSet(i, m4,seven));
160 <            assertEquals(seven,ai.get(i));
156 >            while (!ai.weakCompareAndSet(i, one,two));
157 >            while (!ai.weakCompareAndSet(i, two,m4));
158 >            assertSame(m4,ai.get(i));
159 >            while (!ai.weakCompareAndSet(i, m4,seven));
160 >            assertSame(seven,ai.get(i));
161          }
162      }
163  
164      /**
165       * getAndSet returns previous value and sets to given value at given index
166       */
167 <    public void testGetAndSet(){
167 >    public void testGetAndSet() {
168          AtomicReferenceArray ai = new AtomicReferenceArray(SIZE);
169          for (int i = 0; i < SIZE; ++i) {
170              ai.set(i, one);
171 <            assertEquals(one,ai.getAndSet(i,zero));
172 <            assertEquals(0,ai.getAndSet(i,m10));
173 <            assertEquals(m10,ai.getAndSet(i,one));
171 >            assertSame(one,ai.getAndSet(i,zero));
172 >            assertSame(zero,ai.getAndSet(i,m10));
173 >            assertSame(m10,ai.getAndSet(i,one));
174          }
175      }
176  
177      /**
178       * a deserialized serialized array holds same values
179       */
180 <    public void testSerialization() {
180 >    public void testSerialization() throws Exception {
181          AtomicReferenceArray l = new AtomicReferenceArray(SIZE);
182          for (int i = 0; i < SIZE; ++i) {
183              l.set(i, new Integer(-i));
184          }
185  
186 <        try {
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) {
200 <                assertEquals(r.get(i), l.get(i));
201 <            }
202 <        } catch(Exception e){
203 <            unexpectedException();
186 >        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
187 >        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
188 >        out.writeObject(l);
189 >        out.close();
190 >
191 >        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
192 >        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
193 >        AtomicReferenceArray r = (AtomicReferenceArray) in.readObject();
194 >        assertEquals(l.length(), r.length());
195 >        for (int i = 0; i < SIZE; ++i) {
196 >            assertEquals(r.get(i), l.get(i));
197          }
198      }
199  
207
200      /**
201       * toString returns current value.
202       */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines