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.15 by jsr166, Tue Nov 17 02:53:14 2009 UTC

# 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 < {
14 > public class AtomicReferenceArrayTest extends JSR166TestCase {
15      public static void main (String[] args) {
16          junit.textui.TestRunner.run (suite());
17      }
# 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 58 | Line 55 | public class AtomicReferenceArrayTest ex
55      /**
56       * get and set for out of bound indices throw IndexOutOfBoundsException
57       */
58 <    public void testIndexing(){
58 >    public void testIndexing() {
59          AtomicReferenceArray<Integer> ai = new AtomicReferenceArray<Integer>(SIZE);
60          try {
61              ai.get(SIZE);
62 <        } catch(IndexOutOfBoundsException success){
62 >            shouldThrow();
63 >        } catch (IndexOutOfBoundsException success) {
64          }
65          try {
66              ai.get(-1);
67 <        } catch(IndexOutOfBoundsException success){
67 >            shouldThrow();
68 >        } catch (IndexOutOfBoundsException success) {
69          }
70          try {
71              ai.set(SIZE, null);
72 <        } catch(IndexOutOfBoundsException success){
72 >            shouldThrow();
73 >        } catch (IndexOutOfBoundsException success) {
74          }
75          try {
76              ai.set(-1, null);
77 <        } catch(IndexOutOfBoundsException success){
77 >            shouldThrow();
78 >        } catch (IndexOutOfBoundsException success) {
79          }
80      }
81  
82      /**
83       * get returns the last value set at index
84       */
85 <    public void testGetSet(){
85 >    public void testGetSet() {
86          AtomicReferenceArray ai = new AtomicReferenceArray(SIZE);
87          for (int i = 0; i < SIZE; ++i) {
88              ai.set(i, one);
# Line 96 | Line 97 | public class AtomicReferenceArrayTest ex
97      /**
98       * get returns the last value lazySet at index by same thread
99       */
100 <    public void testGetLazySet(){
100 >    public void testGetLazySet() {
101          AtomicReferenceArray ai = new AtomicReferenceArray(SIZE);
102          for (int i = 0; i < SIZE; ++i) {
103              ai.lazySet(i, one);
# Line 111 | Line 112 | public class AtomicReferenceArrayTest ex
112      /**
113       * compareAndSet succeeds in changing value if equal to expected else fails
114       */
115 <    public void testCompareAndSet(){
115 >    public void testCompareAndSet() {
116          AtomicReferenceArray ai = new AtomicReferenceArray(SIZE);
117          for (int i = 0; i < SIZE; ++i) {
118              ai.set(i, one);
# Line 129 | 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();
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);
145 <        }
146 <        catch(Exception e) {
147 <            unexpectedException();
148 <        }
140 >
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  
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));
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));
159 >            while (!ai.weakCompareAndSet(i, m4,seven));
160              assertEquals(seven,ai.get(i));
161          }
162      }
# Line 167 | Line 164 | public class AtomicReferenceArrayTest ex
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);
# Line 180 | Line 177 | public class AtomicReferenceArrayTest ex
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  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines