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.4 by dl, Sat Sep 20 18:20:07 2003 UTC vs.
Revision 1.6 by dl, Sat Dec 27 19:26:43 2003 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/licenses/publicdomain
5 > * Other contributors include Andrew Wright, Jeffrey Hayes,
6 > * Pat Fisher, Mike Judd.
7   */
8  
9   import junit.framework.*;
# Line 18 | Line 19 | public class AtomicLongArrayTest extends
19      }
20  
21      /**
22 <     *
22 >     * constructor creates array of given size with all elements zero
23       */
24      public void testConstructor(){
25          AtomicLongArray ai = new AtomicLongArray(SIZE);
# Line 27 | Line 28 | public class AtomicLongArrayTest extends
28      }
29  
30      /**
31 <     *
31 >     * get and set for out of bound indices throw IndexOutOfBoundsException
32 >     */
33 >    public void testIndexing(){
34 >        AtomicLongArray ai = new AtomicLongArray(SIZE);
35 >        try {
36 >            ai.get(SIZE);
37 >        } catch(IndexOutOfBoundsException success){
38 >        }
39 >        try {
40 >            ai.get(-1);
41 >        } catch(IndexOutOfBoundsException success){
42 >        }
43 >        try {
44 >            ai.set(SIZE, 0);
45 >        } catch(IndexOutOfBoundsException success){
46 >        }
47 >        try {
48 >            ai.set(-1, 0);
49 >        } catch(IndexOutOfBoundsException success){
50 >        }
51 >    }
52 >
53 >    /**
54 >     * get returns the last value set at index
55       */
56      public void testGetSet(){
57          AtomicLongArray ai = new AtomicLongArray(SIZE);
# Line 42 | Line 66 | public class AtomicLongArrayTest extends
66      }
67  
68      /**
69 <     *
69 >     * compareAndSet succeeds in changing value if equal to expected else fails
70       */
71      public void testCompareAndSet(){
72          AtomicLongArray ai = new AtomicLongArray(SIZE);
# Line 59 | Line 83 | public class AtomicLongArrayTest extends
83      }
84  
85      /**
86 <     *
86 >     * compareAndSet in one thread enables another waiting for value
87 >     * to succeed
88 >     */
89 >    public void testCompareAndSetInMultipleThreads() {
90 >        final AtomicLongArray a = new AtomicLongArray(1);
91 >        a.set(0, 1);
92 >        Thread t = new Thread(new Runnable() {
93 >                public void run() {
94 >                    while(!a.compareAndSet(0, 2, 3)) Thread.yield();
95 >                }});
96 >        try {
97 >            t.start();
98 >            assertTrue(a.compareAndSet(0, 1, 2));
99 >            t.join(LONG_DELAY_MS);
100 >            assertFalse(t.isAlive());
101 >            assertEquals(a.get(0), 3);
102 >        }
103 >        catch(Exception e) {
104 >            unexpectedException();
105 >        }
106 >    }
107 >
108 >    /**
109 >     * repeated weakCompareAndSet succeeds in changing value when equal
110 >     * to expected
111       */
112      public void testWeakCompareAndSet(){
113          AtomicLongArray ai = new AtomicLongArray(SIZE);
# Line 74 | Line 122 | public class AtomicLongArrayTest extends
122      }
123  
124      /**
125 <     *
125 >     *  getAndSet returns previous value and sets to given value at given index
126       */
127      public void testGetAndSet(){
128          AtomicLongArray ai = new AtomicLongArray(SIZE);
# Line 87 | Line 135 | public class AtomicLongArrayTest extends
135      }
136  
137      /**
138 <     *
138 >     *  getAndAdd returns previous value and adds given value
139       */
140      public void testGetAndAdd(){
141          AtomicLongArray ai = new AtomicLongArray(SIZE);
# Line 101 | Line 149 | public class AtomicLongArrayTest extends
149      }
150  
151      /**
152 <     *
152 >     * getAndDecrement returns previous value and decrements
153       */
154      public void testGetAndDecrement(){
155          AtomicLongArray ai = new AtomicLongArray(SIZE);
# Line 114 | Line 162 | public class AtomicLongArrayTest extends
162      }
163  
164      /**
165 <     *
165 >     * getAndIncrement returns previous value and increments
166       */
167      public void testGetAndIncrement(){
168          AtomicLongArray ai = new AtomicLongArray(SIZE);
# Line 131 | Line 179 | public class AtomicLongArrayTest extends
179      }
180  
181      /**
182 <     *
182 >     *  addAndGet adds given value to current, and returns current value
183       */
184      public void testAddAndGet() {
185          AtomicLongArray ai = new AtomicLongArray(SIZE);
# Line 145 | Line 193 | public class AtomicLongArrayTest extends
193      }
194  
195      /**
196 <     *
196 >     * decrementAndGet decrements and returns current value
197       */
198      public void testDecrementAndGet(){
199          AtomicLongArray ai = new AtomicLongArray(SIZE);
# Line 159 | Line 207 | public class AtomicLongArrayTest extends
207      }
208  
209      /**
210 <     *
210 >     * incrementAndGet increments and returns current value
211       */
212      public void testIncrementAndGet() {
213          AtomicLongArray ai = new AtomicLongArray(SIZE);
# Line 200 | Line 248 | public class AtomicLongArrayTest extends
248      }
249  
250      /**
251 <     *
251 >     * Multiple threads using same array of counters successfully
252 >     * update a number of times equal to total count
253       */
254      public void testCountingInMultipleThreads() {
255          try {
# Line 223 | Line 272 | public class AtomicLongArrayTest extends
272      }
273  
274      /**
275 <     *
275 >     * a deserialized serialized array holds same values
276       */
277      public void testSerialization() {
278          AtomicLongArray l = new AtomicLongArray(SIZE);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines