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.3 by dl, Sun Sep 14 20:42:40 2003 UTC vs.
Revision 1.5 by dl, Thu Sep 25 11:02:41 2003 UTC

# Line 17 | Line 17 | public class AtomicLongArrayTest extends
17          return new TestSuite(AtomicLongArrayTest.class);
18      }
19  
20 +    /**
21 +     * constructor creates array of given size with all elements zero
22 +     */
23      public void testConstructor(){
24          AtomicLongArray ai = new AtomicLongArray(SIZE);
25          for (int i = 0; i < SIZE; ++i)
26              assertEquals(0,ai.get(i));
27      }
28  
29 +    /**
30 +     * get and set for out of bound indices throw IndexOutOfBoundsException
31 +     */
32 +    public void testIndexing(){
33 +        AtomicLongArray ai = new AtomicLongArray(SIZE);
34 +        try {
35 +            ai.get(SIZE);
36 +        } catch(IndexOutOfBoundsException success){
37 +        }
38 +        try {
39 +            ai.get(-1);
40 +        } catch(IndexOutOfBoundsException success){
41 +        }
42 +        try {
43 +            ai.set(SIZE, 0);
44 +        } catch(IndexOutOfBoundsException success){
45 +        }
46 +        try {
47 +            ai.set(-1, 0);
48 +        } catch(IndexOutOfBoundsException success){
49 +        }
50 +    }
51 +
52 +    /**
53 +     * get returns the last value set at index
54 +     */
55      public void testGetSet(){
56          AtomicLongArray ai = new AtomicLongArray(SIZE);
57          for (int i = 0; i < SIZE; ++i) {
# Line 35 | Line 64 | public class AtomicLongArrayTest extends
64          }
65      }
66  
67 +    /**
68 +     * compareAndSet succeeds in changing value if equal to expected else fails
69 +     */
70      public void testCompareAndSet(){
71          AtomicLongArray ai = new AtomicLongArray(SIZE);
72          for (int i = 0; i < SIZE; ++i) {
# Line 49 | Line 81 | public class AtomicLongArrayTest extends
81          }
82      }
83  
84 +    /**
85 +     * compareAndSet in one thread enables another waiting for value
86 +     * to succeed
87 +     */
88 +    public void testCompareAndSetInMultipleThreads() {
89 +        final AtomicLongArray a = new AtomicLongArray(1);
90 +        a.set(0, 1);
91 +        Thread t = new Thread(new Runnable() {
92 +                public void run() {
93 +                    while(!a.compareAndSet(0, 2, 3)) Thread.yield();
94 +                }});
95 +        try {
96 +            t.start();
97 +            assertTrue(a.compareAndSet(0, 1, 2));
98 +            t.join(LONG_DELAY_MS);
99 +            assertFalse(t.isAlive());
100 +            assertEquals(a.get(0), 3);
101 +        }
102 +        catch(Exception e) {
103 +            unexpectedException();
104 +        }
105 +    }
106 +
107 +    /**
108 +     * repeated weakCompareAndSet succeeds in changing value when equal
109 +     * to expected
110 +     */
111      public void testWeakCompareAndSet(){
112          AtomicLongArray ai = new AtomicLongArray(SIZE);
113          for (int i = 0; i < SIZE; ++i) {
# Line 61 | Line 120 | public class AtomicLongArrayTest extends
120          }
121      }
122  
123 +    /**
124 +     *  getAndSet returns previous value and sets to given value at given index
125 +     */
126      public void testGetAndSet(){
127          AtomicLongArray ai = new AtomicLongArray(SIZE);
128          for (int i = 0; i < SIZE; ++i) {
# Line 71 | Line 133 | public class AtomicLongArrayTest extends
133          }
134      }
135  
136 +    /**
137 +     *  getAndAdd returns previous value and adds given value
138 +     */
139      public void testGetAndAdd(){
140          AtomicLongArray ai = new AtomicLongArray(SIZE);
141          for (int i = 0; i < SIZE; ++i) {
# Line 82 | Line 147 | public class AtomicLongArrayTest extends
147          }
148      }
149  
150 +    /**
151 +     * getAndDecrement returns previous value and decrements
152 +     */
153      public void testGetAndDecrement(){
154          AtomicLongArray ai = new AtomicLongArray(SIZE);
155          for (int i = 0; i < SIZE; ++i) {
# Line 92 | Line 160 | public class AtomicLongArrayTest extends
160          }
161      }
162  
163 +    /**
164 +     * getAndIncrement returns previous value and increments
165 +     */
166      public void testGetAndIncrement(){
167          AtomicLongArray ai = new AtomicLongArray(SIZE);
168          for (int i = 0; i < SIZE; ++i) {
# Line 106 | Line 177 | public class AtomicLongArrayTest extends
177          }
178      }
179  
180 +    /**
181 +     *  addAndGet adds given value to current, and returns current value
182 +     */
183      public void testAddAndGet() {
184          AtomicLongArray ai = new AtomicLongArray(SIZE);
185          for (int i = 0; i < SIZE; ++i) {
# Line 117 | Line 191 | public class AtomicLongArrayTest extends
191          }
192      }
193  
194 +    /**
195 +     * decrementAndGet decrements and returns current value
196 +     */
197      public void testDecrementAndGet(){
198          AtomicLongArray ai = new AtomicLongArray(SIZE);
199          for (int i = 0; i < SIZE; ++i) {
# Line 128 | Line 205 | public class AtomicLongArrayTest extends
205          }
206      }
207  
208 +    /**
209 +     * incrementAndGet increments and returns current value
210 +     */
211      public void testIncrementAndGet() {
212          AtomicLongArray ai = new AtomicLongArray(SIZE);
213          for (int i = 0; i < SIZE; ++i) {
# Line 166 | Line 246 | public class AtomicLongArrayTest extends
246          }
247      }
248  
249 +    /**
250 +     * Multiple threads using same array of counters successfully
251 +     * update a number of times equal to total count
252 +     */
253      public void testCountingInMultipleThreads() {
254          try {
255              final AtomicLongArray ai = new AtomicLongArray(SIZE);
# Line 182 | Line 266 | public class AtomicLongArrayTest extends
266              assertEquals(c1.counts+c2.counts, SIZE * COUNTDOWN);
267          }
268          catch(InterruptedException ie) {
269 <            fail("unexpected exception");
269 >            unexpectedException();
270          }
271      }
272  
273 +    /**
274 +     * a deserialized serialized array holds same values
275 +     */
276      public void testSerialization() {
277          AtomicLongArray l = new AtomicLongArray(SIZE);
278          for (int i = 0; i < SIZE; ++i)
# Line 204 | Line 291 | public class AtomicLongArrayTest extends
291                  assertEquals(l.get(i), r.get(i));
292              }
293          } catch(Exception e){
294 <            e.printStackTrace();
208 <            fail("unexpected exception");
294 >            unexpectedException();
295          }
296      }
297  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines