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.21 by jsr166, Tue May 31 16:16:23 2011 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/publicdomain/zero/1.0/
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.*;
10 > import java.util.Arrays;
11 > import java.util.concurrent.atomic.AtomicReferenceArray;
12  
13 < public class AtomicReferenceArrayTest extends TestCase
14 < {
15 <    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 <
25 <    public static void main (String[] args) {
26 <        junit.textui.TestRunner.run (suite());
13 > public class AtomicReferenceArrayTest extends JSR166TestCase {
14 >    public static void main(String[] args) {
15 >        junit.textui.TestRunner.run(suite());
16      }
17      public static Test suite() {
18          return new TestSuite(AtomicReferenceArrayTest.class);
19      }
20  
21 <    public void testConstructor(){
22 <        AtomicReferenceArray<Integer> ai = new AtomicReferenceArray<Integer>(N);
23 <        for (int i = 0; i < N; ++i) {
21 >    /**
22 >     * constructor creates array of given size with all elements null
23 >     */
24 >    public void testConstructor() {
25 >        AtomicReferenceArray<Integer> ai = new AtomicReferenceArray<Integer>(SIZE);
26 >        for (int i = 0; i < SIZE; ++i) {
27              assertNull(ai.get(i));
28          }
29      }
30  
31 <    public void testGetSet(){
32 <        AtomicReferenceArray ai = new AtomicReferenceArray(N);
33 <        for (int i = 0; i < N; ++i) {
31 >    /**
32 >     * constructor with null array throws NPE
33 >     */
34 >    public void testConstructor2NPE() {
35 >        try {
36 >            Integer[] a = null;
37 >            AtomicReferenceArray<Integer> ai = new AtomicReferenceArray<Integer>(a);
38 >            shouldThrow();
39 >        } catch (NullPointerException success) {}
40 >    }
41 >
42 >    /**
43 >     * constructor with array is of same size and has all elements
44 >     */
45 >    public void testConstructor2() {
46 >        Integer[] a = { two, one, three, four, seven};
47 >        AtomicReferenceArray<Integer> ai = new AtomicReferenceArray<Integer>(a);
48 >        assertEquals(a.length, ai.length());
49 >        for (int i = 0; i < a.length; ++i)
50 >            assertEquals(a[i], ai.get(i));
51 >    }
52 >
53 >    /**
54 >     * get and set for out of bound indices throw IndexOutOfBoundsException
55 >     */
56 >    public void testIndexing() {
57 >        AtomicReferenceArray<Integer> ai = new AtomicReferenceArray<Integer>(SIZE);
58 >        try {
59 >            ai.get(SIZE);
60 >            shouldThrow();
61 >        } catch (IndexOutOfBoundsException success) {
62 >        }
63 >        try {
64 >            ai.get(-1);
65 >            shouldThrow();
66 >        } catch (IndexOutOfBoundsException success) {
67 >        }
68 >        try {
69 >            ai.set(SIZE, null);
70 >            shouldThrow();
71 >        } catch (IndexOutOfBoundsException success) {
72 >        }
73 >        try {
74 >            ai.set(-1, null);
75 >            shouldThrow();
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);
85 >        for (int i = 0; i < SIZE; ++i) {
86              ai.set(i, one);
87 <            assertEquals(one,ai.get(i));
87 >            assertSame(one,ai.get(i));
88              ai.set(i, two);
89 <            assertEquals(two,ai.get(i));
89 >            assertSame(two,ai.get(i));
90              ai.set(i, m3);
91 <            assertEquals(m3,ai.get(i));
91 >            assertSame(m3,ai.get(i));
92 >        }
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 >            assertSame(one,ai.get(i));
103 >            ai.lazySet(i, two);
104 >            assertSame(two,ai.get(i));
105 >            ai.lazySet(i, m3);
106 >            assertSame(m3,ai.get(i));
107          }
108      }
109  
110 <    public void testCompareAndSet(){
111 <        AtomicReferenceArray ai = new AtomicReferenceArray(N);
112 <        for (int i = 0; i < N; ++i) {
110 >    /**
111 >     * compareAndSet succeeds in changing value if equal to expected else fails
112 >     */
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));
118              assertTrue(ai.compareAndSet(i, two,m4));
119 <            assertEquals(m4,ai.get(i));
119 >            assertSame(m4,ai.get(i));
120              assertFalse(ai.compareAndSet(i, m5,seven));
121 <            assertFalse((seven.equals(ai.get(i))));
121 >            assertSame(m4,ai.get(i));
122              assertTrue(ai.compareAndSet(i, m4,seven));
123 <            assertEquals(seven,ai.get(i));
123 >            assertSame(seven,ai.get(i));
124          }
125      }
126  
127 <    public void testWeakCompareAndSet(){
128 <        AtomicReferenceArray ai = new AtomicReferenceArray(N);
129 <        for (int i = 0; i < N; ++i) {
127 >    /**
128 >     * compareAndSet in one thread enables another waiting for value
129 >     * to succeed
130 >     */
131 >    public void testCompareAndSetInMultipleThreads() throws InterruptedException {
132 >        final AtomicReferenceArray a = new AtomicReferenceArray(1);
133 >        a.set(0, one);
134 >        Thread t = new Thread(new CheckedRunnable() {
135 >            public void realRun() {
136 >                while (!a.compareAndSet(0, two, three))
137 >                    Thread.yield();
138 >            }});
139 >
140 >        t.start();
141 >        assertTrue(a.compareAndSet(0, one, two));
142 >        t.join(LONG_DELAY_MS);
143 >        assertFalse(t.isAlive());
144 >        assertSame(a.get(0), three);
145 >    }
146 >
147 >    /**
148 >     * repeated weakCompareAndSet succeeds in changing value when equal
149 >     * to expected
150 >     */
151 >    public void testWeakCompareAndSet() {
152 >        AtomicReferenceArray ai = new AtomicReferenceArray(SIZE);
153 >        for (int i = 0; i < SIZE; ++i) {
154              ai.set(i, one);
155 <            while(!ai.weakCompareAndSet(i, one,two));
156 <            while(!ai.weakCompareAndSet(i, two,m4));
157 <            assertEquals(m4,ai.get(i));
158 <            while(!ai.weakCompareAndSet(i, m4,seven));
159 <            assertEquals(seven,ai.get(i));
155 >            while (!ai.weakCompareAndSet(i, one,two));
156 >            while (!ai.weakCompareAndSet(i, two,m4));
157 >            assertSame(m4,ai.get(i));
158 >            while (!ai.weakCompareAndSet(i, m4,seven));
159 >            assertSame(seven,ai.get(i));
160          }
161      }
162  
163 <    public void testGetAndSet(){
164 <        AtomicReferenceArray ai = new AtomicReferenceArray(N);
165 <        for (int i = 0; i < N; ++i) {
163 >    /**
164 >     * getAndSet returns previous value and sets to given value at given index
165 >     */
166 >    public void testGetAndSet() {
167 >        AtomicReferenceArray ai = new AtomicReferenceArray(SIZE);
168 >        for (int i = 0; i < SIZE; ++i) {
169              ai.set(i, one);
170 <            assertEquals(one,ai.getAndSet(i,zero));
171 <            assertEquals(0,ai.getAndSet(i,m10));
172 <            assertEquals(m10,ai.getAndSet(i,one));
170 >            assertSame(one,ai.getAndSet(i,zero));
171 >            assertSame(zero,ai.getAndSet(i,m10));
172 >            assertSame(m10,ai.getAndSet(i,one));
173          }
174      }
175  
176 <    public void testSerialization() {
177 <        AtomicReferenceArray l = new AtomicReferenceArray(10);
178 <        for (int i = 0; i < 10; ++i) {
179 <            l.set(i, new Integer(-i));
180 <        }
181 <
182 <        try {
183 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
184 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
185 <            out.writeObject(l);
186 <            out.close();
187 <
188 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
100 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
101 <            AtomicReferenceArray r = (AtomicReferenceArray) in.readObject();
102 <            assertEquals(l.length(), r.length());
103 <            for (int i = 0; i < 10; ++i) {
104 <                assertEquals(r.get(i), l.get(i));
105 <            }
106 <        } catch(Exception e){
107 <            e.printStackTrace();
108 <            fail("unexpected exception");
176 >    /**
177 >     * a deserialized serialized array holds same values
178 >     */
179 >    public void testSerialization() throws Exception {
180 >        AtomicReferenceArray x = new AtomicReferenceArray(SIZE);
181 >        for (int i = 0; i < SIZE; i++) {
182 >            x.set(i, new Integer(-i));
183 >        }
184 >        AtomicReferenceArray y = serialClone(x);
185 >        assertTrue(x != y);
186 >        assertEquals(x.length(), y.length());
187 >        for (int i = 0; i < SIZE; i++) {
188 >            assertEquals(x.get(i), y.get(i));
189          }
190      }
191  
192 +    /**
193 +     * toString returns current value.
194 +     */
195 +    public void testToString() {
196 +        Integer[] a = { two, one, three, four, seven};
197 +        AtomicReferenceArray<Integer> ai = new AtomicReferenceArray<Integer>(a);
198 +        assertEquals(Arrays.toString(a), ai.toString());
199 +    }
200   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines