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.4 by dl, Sat Sep 20 18:20:07 2003 UTC vs.
Revision 1.12 by jsr166, Mon Nov 16 04:57:10 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.*;
# Line 18 | Line 19 | public class AtomicLongTest extends JSR1
19      }
20  
21      /**
22 <     *
22 >     * constructor initializes to given value
23       */
24      public void testConstructor(){
25          AtomicLong ai = new AtomicLong(1);
# Line 26 | Line 27 | public class AtomicLongTest extends JSR1
27      }
28  
29      /**
30 <     *
30 >     * default constructed initializes to zero
31       */
32      public void testConstructor2(){
33          AtomicLong ai = new AtomicLong();
# Line 34 | Line 35 | public class AtomicLongTest extends JSR1
35      }
36  
37      /**
38 <     *
38 >     * get returns the last value set
39       */
40      public void testGetSet(){
41          AtomicLong ai = new AtomicLong(1);
# Line 43 | Line 44 | public class AtomicLongTest extends JSR1
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 <     *
64 >     * compareAndSet succeeds in changing value if equal to expected else fails
65       */
66      public void testCompareAndSet(){
67          AtomicLong ai = new AtomicLong(1);
# Line 60 | Line 75 | public class AtomicLongTest extends JSR1
75      }
76  
77      /**
78 <     *
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));
106 <        while(!ai.weakCompareAndSet(2,-4));
105 >        while (!ai.weakCompareAndSet(1,2));
106 >        while (!ai.weakCompareAndSet(2,-4));
107          assertEquals(-4,ai.get());
108 <        while(!ai.weakCompareAndSet(-4,7));
108 >        while (!ai.weakCompareAndSet(-4,7));
109          assertEquals(7,ai.get());
110      }
111  
112      /**
113 <     *
113 >     * getAndSet returns previous value and sets to given value
114       */
115      public void testGetAndSet(){
116          AtomicLong ai = new AtomicLong(1);
# Line 82 | Line 120 | public class AtomicLongTest extends JSR1
120      }
121  
122      /**
123 <     *
123 >     * getAndAdd returns previous value and adds given value
124       */
125      public void testGetAndAdd(){
126          AtomicLong ai = new AtomicLong(1);
# Line 93 | Line 131 | public class AtomicLongTest extends JSR1
131      }
132  
133      /**
134 <     *
134 >     * getAndDecrement returns previous value and decrements
135       */
136      public void testGetAndDecrement(){
137          AtomicLong ai = new AtomicLong(1);
# Line 103 | Line 141 | public class AtomicLongTest extends JSR1
141      }
142  
143      /**
144 <     *
144 >     * getAndIncrement returns previous value and increments
145       */
146      public void testGetAndIncrement(){
147          AtomicLong ai = new AtomicLong(1);
# Line 117 | Line 155 | public class AtomicLongTest extends JSR1
155      }
156  
157      /**
158 <     *
158 >     * addAndGet adds given value to current, and returns current value
159       */
160      public void testAddAndGet(){
161          AtomicLong ai = new AtomicLong(1);
# Line 128 | Line 166 | public class AtomicLongTest extends JSR1
166      }
167  
168      /**
169 <     *
169 >     * decrementAndGet decrements and returns current value
170       */
171      public void testDecrementAndGet(){
172          AtomicLong ai = new AtomicLong(1);
# Line 139 | Line 177 | public class AtomicLongTest extends JSR1
177      }
178  
179      /**
180 <     *
180 >     * incrementAndGet increments and returns current value
181       */
182      public void testIncrementAndGet(){
183          AtomicLong ai = new AtomicLong(1);
# Line 153 | Line 191 | public class AtomicLongTest extends JSR1
191      }
192  
193      /**
194 <     *
194 >     * a deserialized serialized atomic holds same value
195       */
196      public void testSerialization() {
197          AtomicLong l = new AtomicLong();
# Line 169 | Line 207 | public class AtomicLongTest extends JSR1
207              ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
208              AtomicLong r = (AtomicLong) in.readObject();
209              assertEquals(l.get(), r.get());
210 <        } catch(Exception e){
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