ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AtomicLongArrayTest.java
(Generate patch)

Comparing jsr166/src/test/tck/AtomicLongArrayTest.java (file contents):
Revision 1.24 by jsr166, Fri Jun 10 20:17:11 2011 UTC vs.
Revision 1.25 by jsr166, Wed Aug 10 07:14:48 2011 UTC

# Line 22 | Line 22 | public class AtomicLongArrayTest extends
22       * constructor creates array of given size with all elements zero
23       */
24      public void testConstructor() {
25 <        AtomicLongArray ai = new AtomicLongArray(SIZE);
26 <        for (int i = 0; i < SIZE; ++i)
27 <            assertEquals(0, ai.get(i));
25 >        AtomicLongArray aa = new AtomicLongArray(SIZE);
26 >        for (int i = 0; i < SIZE; i++)
27 >            assertEquals(0, aa.get(i));
28      }
29  
30      /**
# Line 33 | Line 33 | public class AtomicLongArrayTest extends
33      public void testConstructor2NPE() {
34          try {
35              long[] a = null;
36 <            AtomicLongArray ai = new AtomicLongArray(a);
36 >            AtomicLongArray aa = new AtomicLongArray(a);
37              shouldThrow();
38          } catch (NullPointerException success) {}
39      }
# Line 43 | Line 43 | public class AtomicLongArrayTest extends
43       */
44      public void testConstructor2() {
45          long[] a = { 17L, 3L, -42L, 99L, -7L };
46 <        AtomicLongArray ai = new AtomicLongArray(a);
47 <        assertEquals(a.length, ai.length());
48 <        for (int i = 0; i < a.length; ++i)
49 <            assertEquals(a[i], ai.get(i));
46 >        AtomicLongArray aa = new AtomicLongArray(a);
47 >        assertEquals(a.length, aa.length());
48 >        for (int i = 0; i < a.length; i++)
49 >            assertEquals(a[i], aa.get(i));
50      }
51  
52      /**
53       * get and set for out of bound indices throw IndexOutOfBoundsException
54       */
55      public void testIndexing() {
56 <        AtomicLongArray ai = new AtomicLongArray(SIZE);
57 <        try {
58 <            ai.get(SIZE);
59 <            shouldThrow();
60 <        } catch (IndexOutOfBoundsException success) {
61 <        }
62 <        try {
63 <            ai.get(-1);
64 <            shouldThrow();
65 <        } catch (IndexOutOfBoundsException success) {
66 <        }
67 <        try {
68 <            ai.set(SIZE, 0);
69 <            shouldThrow();
70 <        } catch (IndexOutOfBoundsException success) {
71 <        }
72 <        try {
73 <            ai.set(-1, 0);
74 <            shouldThrow();
75 <        } catch (IndexOutOfBoundsException success) {
56 >        AtomicLongArray aa = new AtomicLongArray(SIZE);
57 >        for (int index : new int[] { -1, SIZE }) {
58 >            try {
59 >                aa.get(index);
60 >                shouldThrow();
61 >            } catch (IndexOutOfBoundsException success) {}
62 >            try {
63 >                aa.set(index, 1);
64 >                shouldThrow();
65 >            } catch (IndexOutOfBoundsException success) {}
66 >            try {
67 >                aa.lazySet(index, 1);
68 >                shouldThrow();
69 >            } catch (IndexOutOfBoundsException success) {}
70 >            try {
71 >                aa.compareAndSet(index, 1, 2);
72 >                shouldThrow();
73 >            } catch (IndexOutOfBoundsException success) {}
74 >            try {
75 >                aa.weakCompareAndSet(index, 1, 2);
76 >                shouldThrow();
77 >            } catch (IndexOutOfBoundsException success) {}
78 >            try {
79 >                aa.getAndAdd(index, 1);
80 >                shouldThrow();
81 >            } catch (IndexOutOfBoundsException success) {}
82 >            try {
83 >                aa.addAndGet(index, 1);
84 >                shouldThrow();
85 >            } catch (IndexOutOfBoundsException success) {}
86          }
87      }
88  
# Line 80 | Line 90 | public class AtomicLongArrayTest extends
90       * get returns the last value set at index
91       */
92      public void testGetSet() {
93 <        AtomicLongArray ai = new AtomicLongArray(SIZE);
94 <        for (int i = 0; i < SIZE; ++i) {
95 <            ai.set(i, 1);
96 <            assertEquals(1, ai.get(i));
97 <            ai.set(i, 2);
98 <            assertEquals(2, ai.get(i));
99 <            ai.set(i, -3);
100 <            assertEquals(-3, ai.get(i));
93 >        AtomicLongArray aa = new AtomicLongArray(SIZE);
94 >        for (int i = 0; i < SIZE; i++) {
95 >            aa.set(i, 1);
96 >            assertEquals(1, aa.get(i));
97 >            aa.set(i, 2);
98 >            assertEquals(2, aa.get(i));
99 >            aa.set(i, -3);
100 >            assertEquals(-3, aa.get(i));
101          }
102      }
103  
# Line 95 | Line 105 | public class AtomicLongArrayTest extends
105       * get returns the last value lazySet at index by same thread
106       */
107      public void testGetLazySet() {
108 <        AtomicLongArray ai = new AtomicLongArray(SIZE);
109 <        for (int i = 0; i < SIZE; ++i) {
110 <            ai.lazySet(i, 1);
111 <            assertEquals(1, ai.get(i));
112 <            ai.lazySet(i, 2);
113 <            assertEquals(2, ai.get(i));
114 <            ai.lazySet(i, -3);
115 <            assertEquals(-3, ai.get(i));
108 >        AtomicLongArray aa = new AtomicLongArray(SIZE);
109 >        for (int i = 0; i < SIZE; i++) {
110 >            aa.lazySet(i, 1);
111 >            assertEquals(1, aa.get(i));
112 >            aa.lazySet(i, 2);
113 >            assertEquals(2, aa.get(i));
114 >            aa.lazySet(i, -3);
115 >            assertEquals(-3, aa.get(i));
116          }
117      }
118  
# Line 110 | Line 120 | public class AtomicLongArrayTest extends
120       * compareAndSet succeeds in changing value if equal to expected else fails
121       */
122      public void testCompareAndSet() {
123 <        AtomicLongArray ai = new AtomicLongArray(SIZE);
124 <        for (int i = 0; i < SIZE; ++i) {
125 <            ai.set(i, 1);
126 <            assertTrue(ai.compareAndSet(i, 1, 2));
127 <            assertTrue(ai.compareAndSet(i, 2, -4));
128 <            assertEquals(-4, ai.get(i));
129 <            assertFalse(ai.compareAndSet(i, -5, 7));
130 <            assertEquals(-4, ai.get(i));
131 <            assertTrue(ai.compareAndSet(i, -4, 7));
132 <            assertEquals(7, ai.get(i));
123 >        AtomicLongArray aa = new AtomicLongArray(SIZE);
124 >        for (int i = 0; i < SIZE; i++) {
125 >            aa.set(i, 1);
126 >            assertTrue(aa.compareAndSet(i, 1, 2));
127 >            assertTrue(aa.compareAndSet(i, 2, -4));
128 >            assertEquals(-4, aa.get(i));
129 >            assertFalse(aa.compareAndSet(i, -5, 7));
130 >            assertEquals(-4, aa.get(i));
131 >            assertTrue(aa.compareAndSet(i, -4, 7));
132 >            assertEquals(7, aa.get(i));
133          }
134      }
135  
# Line 148 | Line 158 | public class AtomicLongArrayTest extends
158       * to expected
159       */
160      public void testWeakCompareAndSet() {
161 <        AtomicLongArray ai = new AtomicLongArray(SIZE);
162 <        for (int i = 0; i < SIZE; ++i) {
163 <            ai.set(i, 1);
164 <            while (!ai.weakCompareAndSet(i, 1, 2));
165 <            while (!ai.weakCompareAndSet(i, 2, -4));
166 <            assertEquals(-4, ai.get(i));
167 <            while (!ai.weakCompareAndSet(i, -4, 7));
168 <            assertEquals(7, ai.get(i));
161 >        AtomicLongArray aa = new AtomicLongArray(SIZE);
162 >        for (int i = 0; i < SIZE; i++) {
163 >            aa.set(i, 1);
164 >            while (!aa.weakCompareAndSet(i, 1, 2));
165 >            while (!aa.weakCompareAndSet(i, 2, -4));
166 >            assertEquals(-4, aa.get(i));
167 >            while (!aa.weakCompareAndSet(i, -4, 7));
168 >            assertEquals(7, aa.get(i));
169          }
170      }
171  
# Line 163 | Line 173 | public class AtomicLongArrayTest extends
173       * getAndSet returns previous value and sets to given value at given index
174       */
175      public void testGetAndSet() {
176 <        AtomicLongArray ai = new AtomicLongArray(SIZE);
177 <        for (int i = 0; i < SIZE; ++i) {
178 <            ai.set(i, 1);
179 <            assertEquals(1, ai.getAndSet(i, 0));
180 <            assertEquals(0, ai.getAndSet(i, -10));
181 <            assertEquals(-10, ai.getAndSet(i, 1));
176 >        AtomicLongArray aa = new AtomicLongArray(SIZE);
177 >        for (int i = 0; i < SIZE; i++) {
178 >            aa.set(i, 1);
179 >            assertEquals(1, aa.getAndSet(i, 0));
180 >            assertEquals(0, aa.getAndSet(i, -10));
181 >            assertEquals(-10, aa.getAndSet(i, 1));
182          }
183      }
184  
# Line 176 | Line 186 | public class AtomicLongArrayTest extends
186       * getAndAdd returns previous value and adds given value
187       */
188      public void testGetAndAdd() {
189 <        AtomicLongArray ai = new AtomicLongArray(SIZE);
190 <        for (int i = 0; i < SIZE; ++i) {
191 <            ai.set(i, 1);
192 <            assertEquals(1, ai.getAndAdd(i, 2));
193 <            assertEquals(3, ai.get(i));
194 <            assertEquals(3, ai.getAndAdd(i, -4));
195 <            assertEquals(-1, ai.get(i));
189 >        AtomicLongArray aa = new AtomicLongArray(SIZE);
190 >        for (int i = 0; i < SIZE; i++) {
191 >            aa.set(i, 1);
192 >            assertEquals(1, aa.getAndAdd(i, 2));
193 >            assertEquals(3, aa.get(i));
194 >            assertEquals(3, aa.getAndAdd(i, -4));
195 >            assertEquals(-1, aa.get(i));
196          }
197      }
198  
# Line 190 | Line 200 | public class AtomicLongArrayTest extends
200       * getAndDecrement returns previous value and decrements
201       */
202      public void testGetAndDecrement() {
203 <        AtomicLongArray ai = new AtomicLongArray(SIZE);
204 <        for (int i = 0; i < SIZE; ++i) {
205 <            ai.set(i, 1);
206 <            assertEquals(1, ai.getAndDecrement(i));
207 <            assertEquals(0, ai.getAndDecrement(i));
208 <            assertEquals(-1, ai.getAndDecrement(i));
203 >        AtomicLongArray aa = new AtomicLongArray(SIZE);
204 >        for (int i = 0; i < SIZE; i++) {
205 >            aa.set(i, 1);
206 >            assertEquals(1, aa.getAndDecrement(i));
207 >            assertEquals(0, aa.getAndDecrement(i));
208 >            assertEquals(-1, aa.getAndDecrement(i));
209          }
210      }
211  
# Line 203 | Line 213 | public class AtomicLongArrayTest extends
213       * getAndIncrement returns previous value and increments
214       */
215      public void testGetAndIncrement() {
216 <        AtomicLongArray ai = new AtomicLongArray(SIZE);
217 <        for (int i = 0; i < SIZE; ++i) {
218 <            ai.set(i, 1);
219 <            assertEquals(1, ai.getAndIncrement(i));
220 <            assertEquals(2, ai.get(i));
221 <            ai.set(i, -2);
222 <            assertEquals(-2, ai.getAndIncrement(i));
223 <            assertEquals(-1, ai.getAndIncrement(i));
224 <            assertEquals(0, ai.getAndIncrement(i));
225 <            assertEquals(1, ai.get(i));
216 >        AtomicLongArray aa = new AtomicLongArray(SIZE);
217 >        for (int i = 0; i < SIZE; i++) {
218 >            aa.set(i, 1);
219 >            assertEquals(1, aa.getAndIncrement(i));
220 >            assertEquals(2, aa.get(i));
221 >            aa.set(i, -2);
222 >            assertEquals(-2, aa.getAndIncrement(i));
223 >            assertEquals(-1, aa.getAndIncrement(i));
224 >            assertEquals(0, aa.getAndIncrement(i));
225 >            assertEquals(1, aa.get(i));
226          }
227      }
228  
# Line 220 | Line 230 | public class AtomicLongArrayTest extends
230       * addAndGet adds given value to current, and returns current value
231       */
232      public void testAddAndGet() {
233 <        AtomicLongArray ai = new AtomicLongArray(SIZE);
234 <        for (int i = 0; i < SIZE; ++i) {
235 <            ai.set(i, 1);
236 <            assertEquals(3, ai.addAndGet(i, 2));
237 <            assertEquals(3, ai.get(i));
238 <            assertEquals(-1, ai.addAndGet(i, -4));
239 <            assertEquals(-1, ai.get(i));
233 >        AtomicLongArray aa = new AtomicLongArray(SIZE);
234 >        for (int i = 0; i < SIZE; i++) {
235 >            aa.set(i, 1);
236 >            assertEquals(3, aa.addAndGet(i, 2));
237 >            assertEquals(3, aa.get(i));
238 >            assertEquals(-1, aa.addAndGet(i, -4));
239 >            assertEquals(-1, aa.get(i));
240          }
241      }
242  
# Line 234 | Line 244 | public class AtomicLongArrayTest extends
244       * decrementAndGet decrements and returns current value
245       */
246      public void testDecrementAndGet() {
247 <        AtomicLongArray ai = new AtomicLongArray(SIZE);
248 <        for (int i = 0; i < SIZE; ++i) {
249 <            ai.set(i, 1);
250 <            assertEquals(0, ai.decrementAndGet(i));
251 <            assertEquals(-1, ai.decrementAndGet(i));
252 <            assertEquals(-2, ai.decrementAndGet(i));
253 <            assertEquals(-2, ai.get(i));
247 >        AtomicLongArray aa = new AtomicLongArray(SIZE);
248 >        for (int i = 0; i < SIZE; i++) {
249 >            aa.set(i, 1);
250 >            assertEquals(0, aa.decrementAndGet(i));
251 >            assertEquals(-1, aa.decrementAndGet(i));
252 >            assertEquals(-2, aa.decrementAndGet(i));
253 >            assertEquals(-2, aa.get(i));
254          }
255      }
256  
# Line 248 | Line 258 | public class AtomicLongArrayTest extends
258       * incrementAndGet increments and returns current value
259       */
260      public void testIncrementAndGet() {
261 <        AtomicLongArray ai = new AtomicLongArray(SIZE);
262 <        for (int i = 0; i < SIZE; ++i) {
263 <            ai.set(i, 1);
264 <            assertEquals(2, ai.incrementAndGet(i));
265 <            assertEquals(2, ai.get(i));
266 <            ai.set(i, -2);
267 <            assertEquals(-1, ai.incrementAndGet(i));
268 <            assertEquals(0, ai.incrementAndGet(i));
269 <            assertEquals(1, ai.incrementAndGet(i));
270 <            assertEquals(1, ai.get(i));
261 >        AtomicLongArray aa = new AtomicLongArray(SIZE);
262 >        for (int i = 0; i < SIZE; i++) {
263 >            aa.set(i, 1);
264 >            assertEquals(2, aa.incrementAndGet(i));
265 >            assertEquals(2, aa.get(i));
266 >            aa.set(i, -2);
267 >            assertEquals(-1, aa.incrementAndGet(i));
268 >            assertEquals(0, aa.incrementAndGet(i));
269 >            assertEquals(1, aa.incrementAndGet(i));
270 >            assertEquals(1, aa.get(i));
271          }
272      }
273  
274      static final long COUNTDOWN = 100000;
275  
276      class Counter extends CheckedRunnable {
277 <        final AtomicLongArray ai;
277 >        final AtomicLongArray aa;
278          volatile long counts;
279 <        Counter(AtomicLongArray a) { ai = a; }
279 >        Counter(AtomicLongArray a) { aa = a; }
280          public void realRun() {
281              for (;;) {
282                  boolean done = true;
283 <                for (int i = 0; i < ai.length(); ++i) {
284 <                    long v = ai.get(i);
283 >                for (int i = 0; i < aa.length(); i++) {
284 >                    long v = aa.get(i);
285                      assertTrue(v >= 0);
286                      if (v != 0) {
287                          done = false;
288 <                        if (ai.compareAndSet(i, v, v-1))
288 >                        if (aa.compareAndSet(i, v, v-1))
289                              ++counts;
290                      }
291                  }
# Line 290 | Line 300 | public class AtomicLongArrayTest extends
300       * update a number of times equal to total count
301       */
302      public void testCountingInMultipleThreads() throws InterruptedException {
303 <        final AtomicLongArray ai = new AtomicLongArray(SIZE);
304 <        for (int i = 0; i < SIZE; ++i)
305 <            ai.set(i, COUNTDOWN);
306 <        Counter c1 = new Counter(ai);
307 <        Counter c2 = new Counter(ai);
303 >        final AtomicLongArray aa = new AtomicLongArray(SIZE);
304 >        for (int i = 0; i < SIZE; i++)
305 >            aa.set(i, COUNTDOWN);
306 >        Counter c1 = new Counter(aa);
307 >        Counter c2 = new Counter(aa);
308          Thread t1 = new Thread(c1);
309          Thread t2 = new Thread(c2);
310          t1.start();
# Line 309 | Line 319 | public class AtomicLongArrayTest extends
319       */
320      public void testSerialization() throws Exception {
321          AtomicLongArray x = new AtomicLongArray(SIZE);
322 <        for (int i = 0; i < SIZE; ++i)
322 >        for (int i = 0; i < SIZE; i++)
323              x.set(i, -i);
324          AtomicLongArray y = serialClone(x);
325          assertTrue(x != y);
326          assertEquals(x.length(), y.length());
327 <        for (int i = 0; i < SIZE; ++i) {
327 >        for (int i = 0; i < SIZE; i++) {
328              assertEquals(x.get(i), y.get(i));
329          }
330      }
# Line 324 | Line 334 | public class AtomicLongArrayTest extends
334       */
335      public void testToString() {
336          long[] a = { 17, 3, -42, 99, -7 };
337 <        AtomicLongArray ai = new AtomicLongArray(a);
338 <        assertEquals(Arrays.toString(a), ai.toString());
337 >        AtomicLongArray aa = new AtomicLongArray(a);
338 >        assertEquals(Arrays.toString(a), aa.toString());
339      }
340  
341   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines