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.5 by dl, Thu Sep 25 11:02:41 2003 UTC vs.
Revision 1.13 by jsr166, Mon Nov 16 05:30:07 2009 UTC

# Line 1 | Line 1
1   /*
2 < * Written by members of JCP JSR-166 Expert Group and released to the
3 < * public domain. Use, modify, and redistribute this code in any way
4 < * without acknowledgement. Other contributors include Andrew Wright,
5 < * Jeffrey Hayes, Pat Fischer, Mike Judd.
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.
7   */
8  
9   import junit.framework.*;
10   import java.util.concurrent.atomic.*;
11   import java.io.*;
12 + import java.util.*;
13  
14 < public class AtomicReferenceArrayTest extends JSR166TestCase
13 < {
14 > public class AtomicReferenceArrayTest extends JSR166TestCase {
15      public static void main (String[] args) {
16          junit.textui.TestRunner.run (suite());
17      }
# Line 21 | 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 29 | Line 30 | public class AtomicReferenceArrayTest ex
30      }
31  
32      /**
33 +     * constructor with null array throws NPE
34 +     */
35 +    public void testConstructor2NPE() {
36 +        try {
37 +            Integer[] a = null;
38 +            AtomicReferenceArray<Integer> ai = new AtomicReferenceArray<Integer>(a);
39 +        } catch (NullPointerException success) {
40 +        } catch (Exception ex) {
41 +            unexpectedException();
42 +        }
43 +    }
44 +
45 +    /**
46 +     * constructor with array is of same size and has all elements
47 +     */
48 +    public void testConstructor2() {
49 +        Integer[] a = { two, one, three, four, seven};
50 +        AtomicReferenceArray<Integer> ai = new AtomicReferenceArray<Integer>(a);
51 +        assertEquals(a.length, ai.length());
52 +        for (int i = 0; i < a.length; ++i)
53 +            assertEquals(a[i], ai.get(i));
54 +    }
55 +
56 +
57 +    /**
58       * get and set for out of bound indices throw IndexOutOfBoundsException
59       */
60 <    public void testIndexing(){
60 >    public void testIndexing() {
61          AtomicReferenceArray<Integer> ai = new AtomicReferenceArray<Integer>(SIZE);
62          try {
63              ai.get(SIZE);
64 <        } catch(IndexOutOfBoundsException success){
64 >        } catch (IndexOutOfBoundsException success) {
65          }
66          try {
67              ai.get(-1);
68 <        } catch(IndexOutOfBoundsException success){
68 >        } catch (IndexOutOfBoundsException success) {
69          }
70          try {
71              ai.set(SIZE, null);
72 <        } catch(IndexOutOfBoundsException success){
72 >        } catch (IndexOutOfBoundsException success) {
73          }
74          try {
75              ai.set(-1, null);
76 <        } catch(IndexOutOfBoundsException success){
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));
# Line 67 | Line 93 | public class AtomicReferenceArrayTest ex
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 +            assertEquals(one,ai.get(i));
103 +            ai.lazySet(i, two);
104 +            assertEquals(two,ai.get(i));
105 +            ai.lazySet(i, m3);
106 +            assertEquals(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));
# Line 92 | Line 133 | public class AtomicReferenceArrayTest ex
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();
136 >                    while (!a.compareAndSet(0, two, three)) Thread.yield();
137                  }});
138          try {
139              t.start();
# Line 101 | Line 142 | public class AtomicReferenceArrayTest ex
142              assertFalse(t.isAlive());
143              assertEquals(a.get(0), three);
144          }
145 <        catch(Exception e) {
145 >        catch (Exception e) {
146              unexpectedException();
147          }
148      }
149  
150      /**
151       * repeated weakCompareAndSet succeeds in changing value when equal
152 <     * to expected
152 >     * to expected
153       */
154 <    public void testWeakCompareAndSet(){
155 <        AtomicReferenceArray ai = new AtomicReferenceArray(SIZE);
154 >    public void testWeakCompareAndSet() {
155 >        AtomicReferenceArray ai = new AtomicReferenceArray(SIZE);
156          for (int i = 0; i < SIZE; ++i) {
157              ai.set(i, one);
158 <            while(!ai.weakCompareAndSet(i, one,two));
159 <            while(!ai.weakCompareAndSet(i, two,m4));
158 >            while (!ai.weakCompareAndSet(i, one,two));
159 >            while (!ai.weakCompareAndSet(i, two,m4));
160              assertEquals(m4,ai.get(i));
161 <            while(!ai.weakCompareAndSet(i, m4,seven));
161 >            while (!ai.weakCompareAndSet(i, m4,seven));
162              assertEquals(seven,ai.get(i));
163          }
164      }
# Line 125 | Line 166 | public class AtomicReferenceArrayTest ex
166      /**
167       * getAndSet returns previous value and sets to given value at given index
168       */
169 <    public void testGetAndSet(){
170 <        AtomicReferenceArray ai = new AtomicReferenceArray(SIZE);
169 >    public void testGetAndSet() {
170 >        AtomicReferenceArray ai = new AtomicReferenceArray(SIZE);
171          for (int i = 0; i < SIZE; ++i) {
172              ai.set(i, one);
173              assertEquals(one,ai.getAndSet(i,zero));
# Line 139 | Line 180 | public class AtomicReferenceArrayTest ex
180       * a deserialized serialized array holds same values
181       */
182      public void testSerialization() {
183 <        AtomicReferenceArray l = new AtomicReferenceArray(SIZE);
183 >        AtomicReferenceArray l = new AtomicReferenceArray(SIZE);
184          for (int i = 0; i < SIZE; ++i) {
185              l.set(i, new Integer(-i));
186          }
# Line 157 | Line 198 | public class AtomicReferenceArrayTest ex
198              for (int i = 0; i < SIZE; ++i) {
199                  assertEquals(r.get(i), l.get(i));
200              }
201 <        } catch(Exception e){
201 >        } catch (Exception e) {
202              unexpectedException();
203          }
204      }
205  
206 +
207 +    /**
208 +     * toString returns current value.
209 +     */
210 +    public void testToString() {
211 +        Integer[] a = { two, one, three, four, seven};
212 +        AtomicReferenceArray<Integer> ai = new AtomicReferenceArray<Integer>(a);
213 +        assertEquals(Arrays.toString(a), ai.toString());
214 +    }
215   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines