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

Comparing jsr166/src/test/tck/AtomicIntegerTest.java (file contents):
Revision 1.4 by dl, Sat Sep 20 18:20:07 2003 UTC vs.
Revision 1.11 by dl, Fri Oct 7 14:58:35 2005 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 AtomicIntegerTest extends J
19      }
20  
21      /**
22 <     *
22 >     * constructor initializes to given value
23       */
24      public void testConstructor(){
25          AtomicInteger ai = new AtomicInteger(1);
# Line 26 | Line 27 | public class AtomicIntegerTest extends J
27      }
28  
29      /**
30 <     *
30 >     * default constructed initializes to zero
31       */
32      public void testConstructor2(){
33          AtomicInteger ai = new AtomicInteger();
# Line 34 | Line 35 | public class AtomicIntegerTest extends J
35      }
36  
37      /**
38 <     *
38 >     * get returns the last value set
39       */
40      public void testGetSet(){
41          AtomicInteger ai = new AtomicInteger(1);
# Line 45 | Line 46 | public class AtomicIntegerTest extends J
46          assertEquals(-3,ai.get());
47          
48      }
49 +
50      /**
51 <     *
51 >     * get returns the last value lazySet in same thread
52 >     */
53 >    public void testGetLazySet(){
54 >        AtomicInteger ai = new AtomicInteger(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 >     * compareAndSet succeeds in changing value if equal to expected else fails
64       */
65      public void testCompareAndSet(){
66          AtomicInteger ai = new AtomicInteger(1);
# Line 60 | Line 74 | public class AtomicIntegerTest extends J
74      }
75  
76      /**
77 <     *
77 >     * compareAndSet in one thread enables another waiting for value
78 >     * to succeed
79 >     */
80 >    public void testCompareAndSetInMultipleThreads() {
81 >        final AtomicInteger ai = new AtomicInteger(1);
82 >        Thread t = new Thread(new Runnable() {
83 >                public void run() {
84 >                    while(!ai.compareAndSet(2, 3)) Thread.yield();
85 >                }});
86 >        try {
87 >            t.start();
88 >            assertTrue(ai.compareAndSet(1, 2));
89 >            t.join(LONG_DELAY_MS);
90 >            assertFalse(t.isAlive());
91 >            assertEquals(ai.get(), 3);
92 >        }
93 >        catch(Exception e) {
94 >            unexpectedException();
95 >        }
96 >    }
97 >
98 >    /**
99 >     * repeated weakCompareAndSet succeeds in changing value when equal
100 >     * to expected
101       */
102      public void testWeakCompareAndSet(){
103          AtomicInteger ai = new AtomicInteger(1);
# Line 72 | Line 109 | public class AtomicIntegerTest extends J
109      }
110  
111      /**
112 <     *
112 >     * getAndSet returns previous value and sets to given value
113       */
114      public void testGetAndSet(){
115          AtomicInteger ai = new AtomicInteger(1);
# Line 82 | Line 119 | public class AtomicIntegerTest extends J
119      }
120  
121      /**
122 <     *
122 >     * getAndAdd returns previous value and adds given value
123       */
124      public void testGetAndAdd(){
125          AtomicInteger ai = new AtomicInteger(1);
# Line 93 | Line 130 | public class AtomicIntegerTest extends J
130      }
131  
132      /**
133 <     *
133 >     * getAndDecrement returns previous value and decrements
134       */
135      public void testGetAndDecrement(){
136          AtomicInteger ai = new AtomicInteger(1);
# Line 103 | Line 140 | public class AtomicIntegerTest extends J
140      }
141  
142      /**
143 <     *
143 >     * getAndIncrement returns previous value and increments
144       */
145      public void testGetAndIncrement(){
146          AtomicInteger ai = new AtomicInteger(1);
# Line 117 | Line 154 | public class AtomicIntegerTest extends J
154      }
155  
156      /**
157 <     *
157 >     * addAndGet adds given value to current, and returns current value
158       */
159      public void testAddAndGet(){
160          AtomicInteger ai = new AtomicInteger(1);
# Line 128 | Line 165 | public class AtomicIntegerTest extends J
165      }
166  
167      /**
168 <     *
168 >     * decrementAndGet decrements and returns current value
169       */
170      public void testDecrementAndGet(){
171          AtomicInteger ai = new AtomicInteger(1);
# Line 139 | Line 176 | public class AtomicIntegerTest extends J
176      }
177  
178      /**
179 <     *
179 >     * incrementAndGet increments and returns current value
180       */
181      public void testIncrementAndGet(){
182          AtomicInteger ai = new AtomicInteger(1);
# Line 153 | Line 190 | public class AtomicIntegerTest extends J
190      }
191  
192      /**
193 <     *
193 >     * a deserialized serialized atomic holds same value
194       */
195      public void testSerialization() {
196          AtomicInteger l = new AtomicInteger();
# Line 174 | Line 211 | public class AtomicIntegerTest extends J
211          }
212      }
213  
214 +    /**
215 +     * toString returns current value.
216 +     */
217 +    public void testToString() {
218 +        AtomicInteger ai = new AtomicInteger();
219 +        for (int i = -12; i < 6; ++i) {
220 +            ai.set(i);
221 +            assertEquals(ai.toString(), Integer.toString(i));
222 +        }
223 +    }
224 +
225 +    /**
226 +     * intValue returns current value.
227 +     */
228 +    public void testIntValue() {
229 +        AtomicInteger ai = new AtomicInteger();
230 +        for (int i = -12; i < 6; ++i) {
231 +            ai.set(i);
232 +            assertEquals(i, ai.intValue());
233 +        }
234 +    }
235 +
236 +
237 +    /**
238 +     * longValue returns current value.
239 +     */
240 +    public void testLongValue() {
241 +        AtomicInteger ai = new AtomicInteger();
242 +        for (int i = -12; i < 6; ++i) {
243 +            ai.set(i);
244 +            assertEquals((long)i, ai.longValue());
245 +        }
246 +    }
247 +
248 +    /**
249 +     * floatValue returns current value.
250 +     */
251 +    public void testFloatValue() {
252 +        AtomicInteger ai = new AtomicInteger();
253 +        for (int i = -12; i < 6; ++i) {
254 +            ai.set(i);
255 +            assertEquals((float)i, ai.floatValue());
256 +        }
257 +    }
258 +
259 +    /**
260 +     * doubleValue returns current value.
261 +     */
262 +    public void testDoubleValue() {
263 +        AtomicInteger ai = new AtomicInteger();
264 +        for (int i = -12; i < 6; ++i) {
265 +            ai.set(i);
266 +            assertEquals((double)i, ai.doubleValue());
267 +        }
268 +    }
269 +
270 +
271  
272   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines