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.20 by jsr166, Fri May 27 19:39:07 2011 UTC vs.
Revision 1.29 by jsr166, Wed Dec 31 19:05:42 2014 UTC

# Line 6 | Line 6
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.*;
9 > import java.util.Arrays;
10 > import java.util.concurrent.atomic.AtomicReferenceArray;
11 >
12 > import junit.framework.Test;
13 > import junit.framework.TestSuite;
14  
15   public class AtomicReferenceArrayTest extends JSR166TestCase {
16      public static void main(String[] args) {
# Line 23 | Line 24 | public class AtomicReferenceArrayTest ex
24       * constructor creates array of given size with all elements null
25       */
26      public void testConstructor() {
27 <        AtomicReferenceArray<Integer> ai = new AtomicReferenceArray<Integer>(SIZE);
28 <        for (int i = 0; i < SIZE; ++i) {
29 <            assertNull(ai.get(i));
27 >        AtomicReferenceArray<Integer> aa = new AtomicReferenceArray<Integer>(SIZE);
28 >        for (int i = 0; i < SIZE; i++) {
29 >            assertNull(aa.get(i));
30          }
31      }
32  
# Line 35 | Line 36 | public class AtomicReferenceArrayTest ex
36      public void testConstructor2NPE() {
37          try {
38              Integer[] a = null;
39 <            AtomicReferenceArray<Integer> ai = new AtomicReferenceArray<Integer>(a);
39 >            AtomicReferenceArray<Integer> aa = new AtomicReferenceArray<Integer>(a);
40              shouldThrow();
41          } catch (NullPointerException success) {}
42      }
# Line 44 | Line 45 | public class AtomicReferenceArrayTest ex
45       * constructor with array is of same size and has all elements
46       */
47      public void testConstructor2() {
48 <        Integer[] a = { two, one, three, four, seven};
49 <        AtomicReferenceArray<Integer> ai = new AtomicReferenceArray<Integer>(a);
50 <        assertEquals(a.length, ai.length());
51 <        for (int i = 0; i < a.length; ++i)
52 <            assertEquals(a[i], ai.get(i));
48 >        Integer[] a = { two, one, three, four, seven };
49 >        AtomicReferenceArray<Integer> aa = new AtomicReferenceArray<Integer>(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 >        Integer[] a = { two, one, three, four, seven };
60 >        AtomicReferenceArray<Number> aa = new AtomicReferenceArray<Number>(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      public void testIndexing() {
74 <        AtomicReferenceArray<Integer> ai = new AtomicReferenceArray<Integer>(SIZE);
75 <        try {
76 <            ai.get(SIZE);
77 <            shouldThrow();
78 <        } catch (IndexOutOfBoundsException success) {
79 <        }
80 <        try {
81 <            ai.get(-1);
82 <            shouldThrow();
83 <        } catch (IndexOutOfBoundsException success) {
84 <        }
85 <        try {
86 <            ai.set(SIZE, null);
87 <            shouldThrow();
88 <        } catch (IndexOutOfBoundsException success) {
89 <        }
90 <        try {
91 <            ai.set(-1, null);
92 <            shouldThrow();
93 <        } catch (IndexOutOfBoundsException success) {
74 >        AtomicReferenceArray<Integer> aa = new AtomicReferenceArray<Integer>(SIZE);
75 >        for (int index : new int[] { -1, SIZE }) {
76 >            try {
77 >                aa.get(index);
78 >                shouldThrow();
79 >            } catch (IndexOutOfBoundsException success) {}
80 >            try {
81 >                aa.set(index, null);
82 >                shouldThrow();
83 >            } catch (IndexOutOfBoundsException success) {}
84 >            try {
85 >                aa.lazySet(index, null);
86 >                shouldThrow();
87 >            } catch (IndexOutOfBoundsException success) {}
88 >            try {
89 >                aa.compareAndSet(index, null, null);
90 >                shouldThrow();
91 >            } catch (IndexOutOfBoundsException success) {}
92 >            try {
93 >                aa.weakCompareAndSet(index, null, null);
94 >                shouldThrow();
95 >            } catch (IndexOutOfBoundsException success) {}
96          }
97      }
98  
# Line 82 | Line 100 | public class AtomicReferenceArrayTest ex
100       * get returns the last value set at index
101       */
102      public void testGetSet() {
103 <        AtomicReferenceArray ai = new AtomicReferenceArray(SIZE);
104 <        for (int i = 0; i < SIZE; ++i) {
105 <            ai.set(i, one);
106 <            assertSame(one,ai.get(i));
107 <            ai.set(i, two);
108 <            assertSame(two,ai.get(i));
109 <            ai.set(i, m3);
110 <            assertSame(m3,ai.get(i));
103 >        AtomicReferenceArray aa = new AtomicReferenceArray(SIZE);
104 >        for (int i = 0; i < SIZE; i++) {
105 >            aa.set(i, one);
106 >            assertSame(one, aa.get(i));
107 >            aa.set(i, two);
108 >            assertSame(two, aa.get(i));
109 >            aa.set(i, m3);
110 >            assertSame(m3, aa.get(i));
111          }
112      }
113  
# Line 97 | Line 115 | public class AtomicReferenceArrayTest ex
115       * get returns the last value lazySet at index by same thread
116       */
117      public void testGetLazySet() {
118 <        AtomicReferenceArray ai = new AtomicReferenceArray(SIZE);
119 <        for (int i = 0; i < SIZE; ++i) {
120 <            ai.lazySet(i, one);
121 <            assertSame(one,ai.get(i));
122 <            ai.lazySet(i, two);
123 <            assertSame(two,ai.get(i));
124 <            ai.lazySet(i, m3);
125 <            assertSame(m3,ai.get(i));
118 >        AtomicReferenceArray aa = new AtomicReferenceArray(SIZE);
119 >        for (int i = 0; i < SIZE; i++) {
120 >            aa.lazySet(i, one);
121 >            assertSame(one, aa.get(i));
122 >            aa.lazySet(i, two);
123 >            assertSame(two, aa.get(i));
124 >            aa.lazySet(i, m3);
125 >            assertSame(m3, aa.get(i));
126          }
127      }
128  
# Line 112 | Line 130 | public class AtomicReferenceArrayTest ex
130       * compareAndSet succeeds in changing value if equal to expected else fails
131       */
132      public void testCompareAndSet() {
133 <        AtomicReferenceArray ai = new AtomicReferenceArray(SIZE);
134 <        for (int i = 0; i < SIZE; ++i) {
135 <            ai.set(i, one);
136 <            assertTrue(ai.compareAndSet(i, one,two));
137 <            assertTrue(ai.compareAndSet(i, two,m4));
138 <            assertSame(m4,ai.get(i));
139 <            assertFalse(ai.compareAndSet(i, m5,seven));
140 <            assertSame(m4,ai.get(i));
141 <            assertTrue(ai.compareAndSet(i, m4,seven));
142 <            assertSame(seven,ai.get(i));
133 >        AtomicReferenceArray aa = new AtomicReferenceArray(SIZE);
134 >        for (int i = 0; i < SIZE; i++) {
135 >            aa.set(i, one);
136 >            assertTrue(aa.compareAndSet(i, one, two));
137 >            assertTrue(aa.compareAndSet(i, two, m4));
138 >            assertSame(m4, aa.get(i));
139 >            assertFalse(aa.compareAndSet(i, m5, seven));
140 >            assertSame(m4, aa.get(i));
141 >            assertTrue(aa.compareAndSet(i, m4, seven));
142 >            assertSame(seven, aa.get(i));
143          }
144      }
145  
# Line 142 | Line 160 | public class AtomicReferenceArrayTest ex
160          assertTrue(a.compareAndSet(0, one, two));
161          t.join(LONG_DELAY_MS);
162          assertFalse(t.isAlive());
163 <        assertSame(a.get(0), three);
163 >        assertSame(three, a.get(0));
164      }
165  
166      /**
# Line 150 | Line 168 | public class AtomicReferenceArrayTest ex
168       * to expected
169       */
170      public void testWeakCompareAndSet() {
171 <        AtomicReferenceArray ai = new AtomicReferenceArray(SIZE);
172 <        for (int i = 0; i < SIZE; ++i) {
173 <            ai.set(i, one);
174 <            while (!ai.weakCompareAndSet(i, one,two));
175 <            while (!ai.weakCompareAndSet(i, two,m4));
176 <            assertSame(m4,ai.get(i));
177 <            while (!ai.weakCompareAndSet(i, m4,seven));
178 <            assertSame(seven,ai.get(i));
171 >        AtomicReferenceArray aa = new AtomicReferenceArray(SIZE);
172 >        for (int i = 0; i < SIZE; i++) {
173 >            aa.set(i, one);
174 >            while (!aa.weakCompareAndSet(i, one, two));
175 >            while (!aa.weakCompareAndSet(i, two, m4));
176 >            assertSame(m4, aa.get(i));
177 >            while (!aa.weakCompareAndSet(i, m4, seven));
178 >            assertSame(seven, aa.get(i));
179          }
180      }
181  
# Line 165 | Line 183 | public class AtomicReferenceArrayTest ex
183       * getAndSet returns previous value and sets to given value at given index
184       */
185      public void testGetAndSet() {
186 <        AtomicReferenceArray ai = new AtomicReferenceArray(SIZE);
187 <        for (int i = 0; i < SIZE; ++i) {
188 <            ai.set(i, one);
189 <            assertSame(one,ai.getAndSet(i,zero));
190 <            assertSame(zero,ai.getAndSet(i,m10));
191 <            assertSame(m10,ai.getAndSet(i,one));
186 >        AtomicReferenceArray aa = new AtomicReferenceArray(SIZE);
187 >        for (int i = 0; i < SIZE; i++) {
188 >            aa.set(i, one);
189 >            assertSame(one, aa.getAndSet(i, zero));
190 >            assertSame(zero, aa.getAndSet(i, m10));
191 >            assertSame(m10, aa.getAndSet(i, one));
192          }
193      }
194  
# Line 178 | Line 196 | public class AtomicReferenceArrayTest ex
196       * a deserialized serialized array holds same values
197       */
198      public void testSerialization() throws Exception {
199 <        AtomicReferenceArray l = new AtomicReferenceArray(SIZE);
200 <        for (int i = 0; i < SIZE; ++i) {
201 <            l.set(i, new Integer(-i));
202 <        }
203 <
204 <        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
205 <        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
206 <        out.writeObject(l);
207 <        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));
199 >        AtomicReferenceArray x = new AtomicReferenceArray(SIZE);
200 >        for (int i = 0; i < SIZE; i++) {
201 >            x.set(i, new Integer(-i));
202 >        }
203 >        AtomicReferenceArray y = serialClone(x);
204 >        assertNotSame(x, y);
205 >        assertEquals(x.length(), y.length());
206 >        for (int i = 0; i < SIZE; i++) {
207 >            assertEquals(x.get(i), y.get(i));
208          }
209      }
210  
# Line 201 | Line 212 | public class AtomicReferenceArrayTest ex
212       * toString returns current value.
213       */
214      public void testToString() {
215 <        Integer[] a = { two, one, three, four, seven};
216 <        AtomicReferenceArray<Integer> ai = new AtomicReferenceArray<Integer>(a);
217 <        assertEquals(Arrays.toString(a), ai.toString());
215 >        Integer[] a = { two, one, three, four, seven };
216 >        AtomicReferenceArray<Integer> aa = new AtomicReferenceArray<Integer>(a);
217 >        assertEquals(Arrays.toString(a), aa.toString());
218      }
219   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines