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

Comparing jsr166/src/test/tck/AtomicLongTest.java (file contents):
Revision 1.2 by dl, Sun Sep 7 20:39:11 2003 UTC vs.
Revision 1.5 by dl, Thu Sep 25 11:02:41 2003 UTC

# Line 9 | Line 9 | import junit.framework.*;
9   import java.util.concurrent.atomic.*;
10   import java.io.*;
11  
12 < public class AtomicLongTest extends TestCase {
12 > public class AtomicLongTest extends JSR166TestCase {
13      public static void main (String[] args) {
14          junit.textui.TestRunner.run (suite());
15      }
# Line 17 | Line 17 | public class AtomicLongTest extends Test
17          return new TestSuite(AtomicLongTest.class);
18      }
19  
20 +    /**
21 +     * constructor initializes to given value
22 +     */
23      public void testConstructor(){
24          AtomicLong ai = new AtomicLong(1);
25          assertEquals(1,ai.get());
26      }
27  
28 +    /**
29 +     * default constructed intializes to zero
30 +     */
31      public void testConstructor2(){
32          AtomicLong ai = new AtomicLong();
33          assertEquals(0,ai.get());
34      }
35  
36 +    /**
37 +     * get returns the last value set
38 +     */
39      public void testGetSet(){
40          AtomicLong ai = new AtomicLong(1);
41          assertEquals(1,ai.get());
# Line 36 | Line 45 | public class AtomicLongTest extends Test
45          assertEquals(-3,ai.get());
46          
47      }
48 +    /**
49 +     * compareAndSet succeeds in changing value if equal to expected else fails
50 +     */
51      public void testCompareAndSet(){
52          AtomicLong ai = new AtomicLong(1);
53          assertTrue(ai.compareAndSet(1,2));
# Line 47 | Line 59 | public class AtomicLongTest extends Test
59          assertEquals(7,ai.get());
60      }
61  
62 +    /**
63 +     * compareAndSet in one thread enables another waiting for value
64 +     * to succeed
65 +     */
66 +    public void testCompareAndSetInMultipleThreads() {
67 +        final AtomicLong ai = new AtomicLong(1);
68 +        Thread t = new Thread(new Runnable() {
69 +                public void run() {
70 +                    while(!ai.compareAndSet(2, 3)) Thread.yield();
71 +                }});
72 +        try {
73 +            t.start();
74 +            assertTrue(ai.compareAndSet(1, 2));
75 +            t.join(LONG_DELAY_MS);
76 +            assertFalse(t.isAlive());
77 +            assertEquals(ai.get(), 3);
78 +        }
79 +        catch(Exception e) {
80 +            unexpectedException();
81 +        }
82 +    }
83 +
84 +    /**
85 +     * repeated weakCompareAndSet succeeds in changing value when equal
86 +     * to expected
87 +     */
88      public void testWeakCompareAndSet(){
89          AtomicLong ai = new AtomicLong(1);
90          while(!ai.weakCompareAndSet(1,2));
# Line 56 | Line 94 | public class AtomicLongTest extends Test
94          assertEquals(7,ai.get());
95      }
96  
97 +    /**
98 +     * getAndSet returns previous value and sets to given value
99 +     */
100      public void testGetAndSet(){
101          AtomicLong ai = new AtomicLong(1);
102          assertEquals(1,ai.getAndSet(0));
# Line 63 | Line 104 | public class AtomicLongTest extends Test
104          assertEquals(-10,ai.getAndSet(1));
105      }
106  
107 +    /**
108 +     * getAndAdd returns previous value and adds given value
109 +     */
110      public void testGetAndAdd(){
111          AtomicLong ai = new AtomicLong(1);
112          assertEquals(1,ai.getAndAdd(2));
# Line 71 | Line 115 | public class AtomicLongTest extends Test
115          assertEquals(-1,ai.get());
116      }
117  
118 +    /**
119 +     * getAndDecrement returns previous value and decrements
120 +     */
121      public void testGetAndDecrement(){
122          AtomicLong ai = new AtomicLong(1);
123          assertEquals(1,ai.getAndDecrement());
# Line 78 | Line 125 | public class AtomicLongTest extends Test
125          assertEquals(-1,ai.getAndDecrement());
126      }
127  
128 +    /**
129 +     * getAndIncrement returns previous value and increments
130 +     */
131      public void testGetAndIncrement(){
132          AtomicLong ai = new AtomicLong(1);
133          assertEquals(1,ai.getAndIncrement());
# Line 89 | Line 139 | public class AtomicLongTest extends Test
139          assertEquals(1,ai.get());
140      }
141  
142 +    /**
143 +     * addAndGet adds given value to current, and returns current value
144 +     */
145      public void testAddAndGet(){
146          AtomicLong ai = new AtomicLong(1);
147          assertEquals(3,ai.addAndGet(2));
# Line 97 | Line 150 | public class AtomicLongTest extends Test
150          assertEquals(-1,ai.get());
151      }
152  
153 +    /**
154 +     * decrementAndGet decrements and returns current value
155 +     */
156      public void testDecrementAndGet(){
157          AtomicLong ai = new AtomicLong(1);
158          assertEquals(0,ai.decrementAndGet());
# Line 105 | Line 161 | public class AtomicLongTest extends Test
161          assertEquals(-2,ai.get());
162      }
163  
164 +    /**
165 +     * incrementAndGet increments and returns current value
166 +     */
167      public void testIncrementAndGet(){
168          AtomicLong ai = new AtomicLong(1);
169          assertEquals(2,ai.incrementAndGet());
# Line 116 | Line 175 | public class AtomicLongTest extends Test
175          assertEquals(1,ai.get());
176      }
177  
178 +    /**
179 +     * a deserialized serialized atomic holds same value
180 +     */
181      public void testSerialization() {
182          AtomicLong l = new AtomicLong();
183  
# Line 131 | Line 193 | public class AtomicLongTest extends Test
193              AtomicLong r = (AtomicLong) in.readObject();
194              assertEquals(l.get(), r.get());
195          } catch(Exception e){
196 <            e.printStackTrace();
135 <            fail("unexpected exception");
196 >            unexpectedException();
197          }
198      }
199  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines