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.2 by dl, Sun Sep 7 20:39:11 2003 UTC vs.
Revision 1.9 by dl, Sat Jan 10 01:41:59 2004 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 TestCase
14 > public class AtomicReferenceArrayTest extends JSR166TestCase
15   {
14    static final int N = 10;
15
16    static final Integer zero = new Integer(0);
17    static final Integer one = new Integer(1);
18    static final Integer two = new Integer(2);
19    static final Integer m3  = new Integer(-3);
20    static final Integer m4 = new Integer(-4);
21    static final Integer m5 = new Integer(-5);
22    static final Integer seven = new Integer(7);
23    static final Integer m10 = new Integer(-10);
24
16      public static void main (String[] args) {
17          junit.textui.TestRunner.run (suite());
18      }
# Line 29 | Line 20 | public class AtomicReferenceArrayTest ex
20          return new TestSuite(AtomicReferenceArrayTest.class);
21      }
22  
23 +    /**
24 +     * constructor creates array of given size with all elements null
25 +     */
26      public void testConstructor(){
27 <        AtomicReferenceArray<Integer> ai = new AtomicReferenceArray<Integer>(N);
28 <        for (int i = 0; i < N; ++i) {
27 >        AtomicReferenceArray<Integer> ai = new AtomicReferenceArray<Integer>(SIZE);
28 >        for (int i = 0; i < SIZE; ++i) {
29              assertNull(ai.get(i));
30          }
31      }
32  
33 +    /**
34 +     * constructor with null array throws NPE
35 +     */
36 +    public void testConstructor2NPE() {
37 +        try {
38 +            Integer[] a = null;
39 +            AtomicReferenceArray<Integer> ai = new AtomicReferenceArray<Integer>(a);
40 +        } catch (NullPointerException success) {
41 +        } catch (Exception ex) {
42 +            unexpectedException();
43 +        }
44 +    }
45 +
46 +    /**
47 +     * constructor with array is of same size and has all elements
48 +     */
49 +    public void testConstructor2() {
50 +        Integer[] a = { two, one, three, four, seven};
51 +        AtomicReferenceArray<Integer> ai = new AtomicReferenceArray<Integer>(a);
52 +        assertEquals(a.length, ai.length());
53 +        for (int i = 0; i < a.length; ++i)
54 +            assertEquals(a[i], ai.get(i));
55 +    }
56 +
57 +
58 +    /**
59 +     * get and set for out of bound indices throw IndexOutOfBoundsException
60 +     */
61 +    public void testIndexing(){
62 +        AtomicReferenceArray<Integer> ai = new AtomicReferenceArray<Integer>(SIZE);
63 +        try {
64 +            ai.get(SIZE);
65 +        } catch(IndexOutOfBoundsException success){
66 +        }
67 +        try {
68 +            ai.get(-1);
69 +        } catch(IndexOutOfBoundsException success){
70 +        }
71 +        try {
72 +            ai.set(SIZE, null);
73 +        } catch(IndexOutOfBoundsException success){
74 +        }
75 +        try {
76 +            ai.set(-1, null);
77 +        } catch(IndexOutOfBoundsException success){
78 +        }
79 +    }
80 +
81 +    /**
82 +     * get returns the last value set at index
83 +     */
84      public void testGetSet(){
85 <        AtomicReferenceArray ai = new AtomicReferenceArray(N);
86 <        for (int i = 0; i < N; ++i) {
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));
89              ai.set(i, two);
# Line 48 | Line 93 | public class AtomicReferenceArrayTest ex
93          }
94      }
95  
96 +    /**
97 +     * compareAndSet succeeds in changing value if equal to expected else fails
98 +     */
99      public void testCompareAndSet(){
100 <        AtomicReferenceArray ai = new AtomicReferenceArray(N);
101 <        for (int i = 0; i < N; ++i) {
100 >        AtomicReferenceArray ai = new AtomicReferenceArray(SIZE);
101 >        for (int i = 0; i < SIZE; ++i) {
102              ai.set(i, one);
103              assertTrue(ai.compareAndSet(i, one,two));
104              assertTrue(ai.compareAndSet(i, two,m4));
# Line 62 | Line 110 | public class AtomicReferenceArrayTest ex
110          }
111      }
112  
113 +    /**
114 +     * compareAndSet in one thread enables another waiting for value
115 +     * to succeed
116 +     */
117 +    public void testCompareAndSetInMultipleThreads() {
118 +        final AtomicReferenceArray a = new AtomicReferenceArray(1);
119 +        a.set(0, one);
120 +        Thread t = new Thread(new Runnable() {
121 +                public void run() {
122 +                    while(!a.compareAndSet(0, two, three)) Thread.yield();
123 +                }});
124 +        try {
125 +            t.start();
126 +            assertTrue(a.compareAndSet(0, one, two));
127 +            t.join(LONG_DELAY_MS);
128 +            assertFalse(t.isAlive());
129 +            assertEquals(a.get(0), three);
130 +        }
131 +        catch(Exception e) {
132 +            unexpectedException();
133 +        }
134 +    }
135 +
136 +    /**
137 +     * repeated weakCompareAndSet succeeds in changing value when equal
138 +     * to expected
139 +     */
140      public void testWeakCompareAndSet(){
141 <        AtomicReferenceArray ai = new AtomicReferenceArray(N);
142 <        for (int i = 0; i < N; ++i) {
141 >        AtomicReferenceArray ai = new AtomicReferenceArray(SIZE);
142 >        for (int i = 0; i < SIZE; ++i) {
143              ai.set(i, one);
144              while(!ai.weakCompareAndSet(i, one,two));
145              while(!ai.weakCompareAndSet(i, two,m4));
# Line 74 | Line 149 | public class AtomicReferenceArrayTest ex
149          }
150      }
151  
152 +    /**
153 +     * getAndSet returns previous value and sets to given value at given index
154 +     */
155      public void testGetAndSet(){
156 <        AtomicReferenceArray ai = new AtomicReferenceArray(N);
157 <        for (int i = 0; i < N; ++i) {
156 >        AtomicReferenceArray ai = new AtomicReferenceArray(SIZE);
157 >        for (int i = 0; i < SIZE; ++i) {
158              ai.set(i, one);
159              assertEquals(one,ai.getAndSet(i,zero));
160              assertEquals(0,ai.getAndSet(i,m10));
# Line 84 | Line 162 | public class AtomicReferenceArrayTest ex
162          }
163      }
164  
165 +    /**
166 +     * a deserialized serialized array holds same values
167 +     */
168      public void testSerialization() {
169 <        AtomicReferenceArray l = new AtomicReferenceArray(10);
170 <        for (int i = 0; i < 10; ++i) {
169 >        AtomicReferenceArray l = new AtomicReferenceArray(SIZE);
170 >        for (int i = 0; i < SIZE; ++i) {
171              l.set(i, new Integer(-i));
172          }
173  
# Line 100 | Line 181 | public class AtomicReferenceArrayTest ex
181              ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
182              AtomicReferenceArray r = (AtomicReferenceArray) in.readObject();
183              assertEquals(l.length(), r.length());
184 <            for (int i = 0; i < 10; ++i) {
184 >            for (int i = 0; i < SIZE; ++i) {
185                  assertEquals(r.get(i), l.get(i));
186              }
187          } catch(Exception e){
188 <            e.printStackTrace();
108 <            fail("unexpected exception");
188 >            unexpectedException();
189          }
190      }
191  
192 +
193 +    /**
194 +     * toString returns current value.
195 +     */
196 +    public void testToString() {
197 +        Integer[] a = { two, one, three, four, seven};
198 +        AtomicReferenceArray<Integer> ai = new AtomicReferenceArray<Integer>(a);
199 +        assertEquals(Arrays.toString(a), ai.toString());
200 +    }
201   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines