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.11 by jsr166, Mon Nov 16 04:57:10 2009 UTC vs.
Revision 1.17 by jsr166, Thu Sep 16 00:52:49 2010 UTC

# Line 12 | Line 12 | import java.io.*;
12   import java.util.*;
13  
14   public class AtomicLongArrayTest extends JSR166TestCase {
15 <    public static void main (String[] args) {
16 <        junit.textui.TestRunner.run (suite());
15 >    public static void main(String[] args) {
16 >        junit.textui.TestRunner.run(suite());
17      }
18      public static Test suite() {
19          return new TestSuite(AtomicLongArrayTest.class);
# Line 22 | Line 22 | public class AtomicLongArrayTest extends
22      /**
23       * constructor creates array of given size with all elements zero
24       */
25 <    public void testConstructor(){
25 >    public void testConstructor() {
26          AtomicLongArray ai = new AtomicLongArray(SIZE);
27          for (int i = 0; i < SIZE; ++i)
28              assertEquals(0,ai.get(i));
# Line 35 | Line 35 | public class AtomicLongArrayTest extends
35          try {
36              long[] a = null;
37              AtomicLongArray ai = new AtomicLongArray(a);
38 <        } catch (NullPointerException success) {
39 <        } catch (Exception ex) {
40 <            unexpectedException();
41 <        }
38 >            shouldThrow();
39 >        } catch (NullPointerException success) {}
40      }
41  
42      /**
# Line 55 | Line 53 | public class AtomicLongArrayTest extends
53      /**
54       * get and set for out of bound indices throw IndexOutOfBoundsException
55       */
56 <    public void testIndexing(){
56 >    public void testIndexing() {
57          AtomicLongArray ai = new AtomicLongArray(SIZE);
58          try {
59              ai.get(SIZE);
60 <        } catch (IndexOutOfBoundsException success){
60 >            shouldThrow();
61 >        } catch (IndexOutOfBoundsException success) {
62          }
63          try {
64              ai.get(-1);
65 <        } catch (IndexOutOfBoundsException success){
65 >            shouldThrow();
66 >        } catch (IndexOutOfBoundsException success) {
67          }
68          try {
69              ai.set(SIZE, 0);
70 <        } catch (IndexOutOfBoundsException success){
70 >            shouldThrow();
71 >        } catch (IndexOutOfBoundsException success) {
72          }
73          try {
74              ai.set(-1, 0);
75 <        } catch (IndexOutOfBoundsException success){
75 >            shouldThrow();
76 >        } catch (IndexOutOfBoundsException success) {
77          }
78      }
79  
80      /**
81       * get returns the last value set at index
82       */
83 <    public void testGetSet(){
83 >    public void testGetSet() {
84          AtomicLongArray ai = new AtomicLongArray(SIZE);
85          for (int i = 0; i < SIZE; ++i) {
86              ai.set(i, 1);
# Line 93 | Line 95 | public class AtomicLongArrayTest extends
95      /**
96       * get returns the last value lazySet at index by same thread
97       */
98 <    public void testGetLazySet(){
98 >    public void testGetLazySet() {
99          AtomicLongArray ai = new AtomicLongArray(SIZE);
100          for (int i = 0; i < SIZE; ++i) {
101              ai.lazySet(i, 1);
# Line 108 | Line 110 | public class AtomicLongArrayTest extends
110      /**
111       * compareAndSet succeeds in changing value if equal to expected else fails
112       */
113 <    public void testCompareAndSet(){
113 >    public void testCompareAndSet() {
114          AtomicLongArray ai = new AtomicLongArray(SIZE);
115          for (int i = 0; i < SIZE; ++i) {
116              ai.set(i, 1);
# Line 116 | Line 118 | public class AtomicLongArrayTest extends
118              assertTrue(ai.compareAndSet(i, 2,-4));
119              assertEquals(-4,ai.get(i));
120              assertFalse(ai.compareAndSet(i, -5,7));
121 <            assertFalse((7 == ai.get(i)));
121 >            assertEquals(-4,ai.get(i));
122              assertTrue(ai.compareAndSet(i, -4,7));
123              assertEquals(7,ai.get(i));
124          }
# Line 126 | Line 128 | public class AtomicLongArrayTest extends
128       * compareAndSet in one thread enables another waiting for value
129       * to succeed
130       */
131 <    public void testCompareAndSetInMultipleThreads() {
131 >    public void testCompareAndSetInMultipleThreads() throws InterruptedException {
132          final AtomicLongArray a = new AtomicLongArray(1);
133          a.set(0, 1);
134 <        Thread t = new Thread(new Runnable() {
135 <                public void run() {
136 <                    while (!a.compareAndSet(0, 2, 3)) Thread.yield();
137 <                }});
138 <        try {
139 <            t.start();
140 <            assertTrue(a.compareAndSet(0, 1, 2));
141 <            t.join(LONG_DELAY_MS);
142 <            assertFalse(t.isAlive());
143 <            assertEquals(a.get(0), 3);
144 <        }
143 <        catch (Exception e) {
144 <            unexpectedException();
145 <        }
134 >        Thread t = new Thread(new CheckedRunnable() {
135 >            public void realRun() {
136 >                while (!a.compareAndSet(0, 2, 3))
137 >                    Thread.yield();
138 >            }});
139 >
140 >        t.start();
141 >        assertTrue(a.compareAndSet(0, 1, 2));
142 >        t.join(LONG_DELAY_MS);
143 >        assertFalse(t.isAlive());
144 >        assertEquals(a.get(0), 3);
145      }
146  
147      /**
148       * repeated weakCompareAndSet succeeds in changing value when equal
149       * to expected
150       */
151 <    public void testWeakCompareAndSet(){
151 >    public void testWeakCompareAndSet() {
152          AtomicLongArray ai = new AtomicLongArray(SIZE);
153          for (int i = 0; i < SIZE; ++i) {
154              ai.set(i, 1);
# Line 164 | Line 163 | public class AtomicLongArrayTest extends
163      /**
164       *  getAndSet returns previous value and sets to given value at given index
165       */
166 <    public void testGetAndSet(){
166 >    public void testGetAndSet() {
167          AtomicLongArray ai = new AtomicLongArray(SIZE);
168          for (int i = 0; i < SIZE; ++i) {
169              ai.set(i, 1);
# Line 177 | Line 176 | public class AtomicLongArrayTest extends
176      /**
177       *  getAndAdd returns previous value and adds given value
178       */
179 <    public void testGetAndAdd(){
179 >    public void testGetAndAdd() {
180          AtomicLongArray ai = new AtomicLongArray(SIZE);
181          for (int i = 0; i < SIZE; ++i) {
182              ai.set(i, 1);
# Line 191 | Line 190 | public class AtomicLongArrayTest extends
190      /**
191       * getAndDecrement returns previous value and decrements
192       */
193 <    public void testGetAndDecrement(){
193 >    public void testGetAndDecrement() {
194          AtomicLongArray ai = new AtomicLongArray(SIZE);
195          for (int i = 0; i < SIZE; ++i) {
196              ai.set(i, 1);
# Line 204 | Line 203 | public class AtomicLongArrayTest extends
203      /**
204       * getAndIncrement returns previous value and increments
205       */
206 <    public void testGetAndIncrement(){
206 >    public void testGetAndIncrement() {
207          AtomicLongArray ai = new AtomicLongArray(SIZE);
208          for (int i = 0; i < SIZE; ++i) {
209              ai.set(i, 1);
# Line 235 | Line 234 | public class AtomicLongArrayTest extends
234      /**
235       * decrementAndGet decrements and returns current value
236       */
237 <    public void testDecrementAndGet(){
237 >    public void testDecrementAndGet() {
238          AtomicLongArray ai = new AtomicLongArray(SIZE);
239          for (int i = 0; i < SIZE; ++i) {
240              ai.set(i, 1);
# Line 265 | Line 264 | public class AtomicLongArrayTest extends
264  
265      static final long COUNTDOWN = 100000;
266  
267 <    class Counter implements Runnable {
267 >    class Counter extends CheckedRunnable {
268          final AtomicLongArray ai;
269          volatile long counts;
270          Counter(AtomicLongArray a) { ai = a; }
271 <        public void run() {
271 >        public void realRun() {
272              for (;;) {
273                  boolean done = true;
274                  for (int i = 0; i < ai.length(); ++i) {
275                      long v = ai.get(i);
276 <                    threadAssertTrue(v >= 0);
276 >                    assertTrue(v >= 0);
277                      if (v != 0) {
278                          done = false;
279                          if (ai.compareAndSet(i, v, v-1))
# Line 291 | Line 290 | public class AtomicLongArrayTest extends
290       * Multiple threads using same array of counters successfully
291       * update a number of times equal to total count
292       */
293 <    public void testCountingInMultipleThreads() {
294 <        try {
295 <            final AtomicLongArray ai = new AtomicLongArray(SIZE);
296 <            for (int i = 0; i < SIZE; ++i)
297 <                ai.set(i, COUNTDOWN);
298 <            Counter c1 = new Counter(ai);
299 <            Counter c2 = new Counter(ai);
300 <            Thread t1 = new Thread(c1);
301 <            Thread t2 = new Thread(c2);
302 <            t1.start();
303 <            t2.start();
304 <            t1.join();
305 <            t2.join();
307 <            assertEquals(c1.counts+c2.counts, SIZE * COUNTDOWN);
308 <        }
309 <        catch (InterruptedException ie) {
310 <            unexpectedException();
311 <        }
293 >    public void testCountingInMultipleThreads() throws InterruptedException {
294 >        final AtomicLongArray ai = new AtomicLongArray(SIZE);
295 >        for (int i = 0; i < SIZE; ++i)
296 >            ai.set(i, COUNTDOWN);
297 >        Counter c1 = new Counter(ai);
298 >        Counter c2 = new Counter(ai);
299 >        Thread t1 = new Thread(c1);
300 >        Thread t2 = new Thread(c2);
301 >        t1.start();
302 >        t2.start();
303 >        t1.join();
304 >        t2.join();
305 >        assertEquals(c1.counts+c2.counts, SIZE * COUNTDOWN);
306      }
307  
308      /**
309       * a deserialized serialized array holds same values
310       */
311 <    public void testSerialization() {
311 >    public void testSerialization() throws Exception {
312          AtomicLongArray l = new AtomicLongArray(SIZE);
313          for (int i = 0; i < SIZE; ++i)
314              l.set(i, -i);
315  
316 <        try {
317 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
318 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
319 <            out.writeObject(l);
320 <            out.close();
321 <
322 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
323 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
324 <            AtomicLongArray r = (AtomicLongArray) in.readObject();
325 <            for (int i = 0; i < SIZE; ++i) {
332 <                assertEquals(l.get(i), r.get(i));
333 <            }
334 <        } catch (Exception e){
335 <            unexpectedException();
316 >        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
317 >        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
318 >        out.writeObject(l);
319 >        out.close();
320 >
321 >        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
322 >        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
323 >        AtomicLongArray r = (AtomicLongArray) in.readObject();
324 >        for (int i = 0; i < SIZE; ++i) {
325 >            assertEquals(l.get(i), r.get(i));
326          }
327      }
328  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines