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.1 by dl, Sun Aug 31 19:24:53 2003 UTC vs.
Revision 1.11 by jsr166, Mon Nov 2 20:28:31 2009 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.*;
10   import java.util.concurrent.atomic.*;
11 + import java.io.*;
12  
13 < public class AtomicLongTest extends TestCase {
13 > public class AtomicLongTest extends JSR166TestCase {
14      public static void main (String[] args) {
15          junit.textui.TestRunner.run (suite());
16      }
# Line 16 | Line 18 | public class AtomicLongTest extends Test
18          return new TestSuite(AtomicLongTest.class);
19      }
20  
21 +    /**
22 +     * constructor initializes to given value
23 +     */
24      public void testConstructor(){
25          AtomicLong ai = new AtomicLong(1);
26          assertEquals(1,ai.get());
27      }
28  
29 +    /**
30 +     * default constructed initializes to zero
31 +     */
32      public void testConstructor2(){
33          AtomicLong ai = new AtomicLong();
34          assertEquals(0,ai.get());
35      }
36  
37 +    /**
38 +     * get returns the last value set
39 +     */
40      public void testGetSet(){
41          AtomicLong ai = new AtomicLong(1);
42          assertEquals(1,ai.get());
# Line 33 | Line 44 | public class AtomicLongTest extends Test
44          assertEquals(2,ai.get());
45          ai.set(-3);
46          assertEquals(-3,ai.get());
47 <        
47 >
48      }
49 +
50 +    /**
51 +     * get returns the last value lazySet in same thread
52 +     */
53 +    public void testGetLazySet(){
54 +        AtomicLong ai = new AtomicLong(1);
55 +        assertEquals(1,ai.get());
56 +        ai.lazySet(2);
57 +        assertEquals(2,ai.get());
58 +        ai.lazySet(-3);
59 +        assertEquals(-3,ai.get());
60 +
61 +    }
62 +
63 +    /**
64 +     * compareAndSet succeeds in changing value if equal to expected else fails
65 +     */
66      public void testCompareAndSet(){
67          AtomicLong ai = new AtomicLong(1);
68          assertTrue(ai.compareAndSet(1,2));
# Line 46 | Line 74 | public class AtomicLongTest extends Test
74          assertEquals(7,ai.get());
75      }
76  
77 +    /**
78 +     * compareAndSet in one thread enables another waiting for value
79 +     * to succeed
80 +     */
81 +    public void testCompareAndSetInMultipleThreads() {
82 +        final AtomicLong ai = new AtomicLong(1);
83 +        Thread t = new Thread(new Runnable() {
84 +                public void run() {
85 +                    while(!ai.compareAndSet(2, 3)) Thread.yield();
86 +                }});
87 +        try {
88 +            t.start();
89 +            assertTrue(ai.compareAndSet(1, 2));
90 +            t.join(LONG_DELAY_MS);
91 +            assertFalse(t.isAlive());
92 +            assertEquals(ai.get(), 3);
93 +        }
94 +        catch(Exception e) {
95 +            unexpectedException();
96 +        }
97 +    }
98 +
99 +    /**
100 +     * repeated weakCompareAndSet succeeds in changing value when equal
101 +     * to expected
102 +     */
103      public void testWeakCompareAndSet(){
104          AtomicLong ai = new AtomicLong(1);
105          while(!ai.weakCompareAndSet(1,2));
# Line 55 | Line 109 | public class AtomicLongTest extends Test
109          assertEquals(7,ai.get());
110      }
111  
112 +    /**
113 +     * getAndSet returns previous value and sets to given value
114 +     */
115      public void testGetAndSet(){
116          AtomicLong ai = new AtomicLong(1);
117          assertEquals(1,ai.getAndSet(0));
# Line 62 | Line 119 | public class AtomicLongTest extends Test
119          assertEquals(-10,ai.getAndSet(1));
120      }
121  
122 +    /**
123 +     * getAndAdd returns previous value and adds given value
124 +     */
125      public void testGetAndAdd(){
126          AtomicLong ai = new AtomicLong(1);
127          assertEquals(1,ai.getAndAdd(2));
# Line 70 | Line 130 | public class AtomicLongTest extends Test
130          assertEquals(-1,ai.get());
131      }
132  
133 +    /**
134 +     * getAndDecrement returns previous value and decrements
135 +     */
136      public void testGetAndDecrement(){
137          AtomicLong ai = new AtomicLong(1);
138          assertEquals(1,ai.getAndDecrement());
# Line 77 | Line 140 | public class AtomicLongTest extends Test
140          assertEquals(-1,ai.getAndDecrement());
141      }
142  
143 +    /**
144 +     * getAndIncrement returns previous value and increments
145 +     */
146      public void testGetAndIncrement(){
147          AtomicLong ai = new AtomicLong(1);
148          assertEquals(1,ai.getAndIncrement());
# Line 88 | Line 154 | public class AtomicLongTest extends Test
154          assertEquals(1,ai.get());
155      }
156  
157 +    /**
158 +     * addAndGet adds given value to current, and returns current value
159 +     */
160      public void testAddAndGet(){
161          AtomicLong ai = new AtomicLong(1);
162          assertEquals(3,ai.addAndGet(2));
# Line 96 | Line 165 | public class AtomicLongTest extends Test
165          assertEquals(-1,ai.get());
166      }
167  
168 +    /**
169 +     * decrementAndGet decrements and returns current value
170 +     */
171      public void testDecrementAndGet(){
172          AtomicLong ai = new AtomicLong(1);
173          assertEquals(0,ai.decrementAndGet());
# Line 104 | Line 176 | public class AtomicLongTest extends Test
176          assertEquals(-2,ai.get());
177      }
178  
179 +    /**
180 +     * incrementAndGet increments and returns current value
181 +     */
182      public void testIncrementAndGet(){
183          AtomicLong ai = new AtomicLong(1);
184          assertEquals(2,ai.incrementAndGet());
# Line 115 | Line 190 | public class AtomicLongTest extends Test
190          assertEquals(1,ai.get());
191      }
192  
193 +    /**
194 +     * a deserialized serialized atomic holds same value
195 +     */
196 +    public void testSerialization() {
197 +        AtomicLong l = new AtomicLong();
198 +
199 +        try {
200 +            l.set(-22);
201 +            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
202 +            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
203 +            out.writeObject(l);
204 +            out.close();
205 +
206 +            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
207 +            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
208 +            AtomicLong r = (AtomicLong) in.readObject();
209 +            assertEquals(l.get(), r.get());
210 +        } catch(Exception e){
211 +            unexpectedException();
212 +        }
213 +    }
214 +
215 +    /**
216 +     * toString returns current value.
217 +     */
218 +    public void testToString() {
219 +        AtomicLong ai = new AtomicLong();
220 +        for (long i = -12; i < 6; ++i) {
221 +            ai.set(i);
222 +            assertEquals(ai.toString(), Long.toString(i));
223 +        }
224 +    }
225 +
226 +    /**
227 +     * longValue returns current value.
228 +     */
229 +    public void testLongValue() {
230 +        AtomicLong ai = new AtomicLong();
231 +        for (int i = -12; i < 6; ++i) {
232 +            ai.set(i);
233 +            assertEquals((long)i, ai.longValue());
234 +        }
235 +    }
236 +
237 +    /**
238 +     * floatValue returns current value.
239 +     */
240 +    public void testFloatValue() {
241 +        AtomicLong ai = new AtomicLong();
242 +        for (int i = -12; i < 6; ++i) {
243 +            ai.set(i);
244 +            assertEquals((float)i, ai.floatValue());
245 +        }
246 +    }
247 +
248 +    /**
249 +     * doubleValue returns current value.
250 +     */
251 +    public void testDoubleValue() {
252 +        AtomicLong ai = new AtomicLong();
253 +        for (int i = -12; i < 6; ++i) {
254 +            ai.set(i);
255 +            assertEquals((double)i, ai.doubleValue());
256 +        }
257 +    }
258 +
259  
260   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines