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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines