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.3 by dl, Sun Sep 14 20:42:40 2003 UTC vs.
Revision 1.12 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.*;
# Line 17 | Line 18 | public class AtomicIntegerTest extends J
18          return new TestSuite(AtomicIntegerTest.class);
19      }
20  
21 +    /**
22 +     * constructor initializes to given value
23 +     */
24      public void testConstructor(){
25          AtomicInteger ai = new AtomicInteger(1);
26          assertEquals(1,ai.get());
27      }
28  
29 +    /**
30 +     * default constructed initializes to zero
31 +     */
32      public void testConstructor2(){
33          AtomicInteger ai = new AtomicInteger();
34          assertEquals(0,ai.get());
35      }
36  
37 +    /**
38 +     * get returns the last value set
39 +     */
40      public void testGetSet(){
41          AtomicInteger ai = new AtomicInteger(1);
42          assertEquals(1,ai.get());
# Line 34 | Line 44 | public class AtomicIntegerTest extends J
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 +        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);
67          assertTrue(ai.compareAndSet(1,2));
# Line 47 | Line 73 | public class AtomicIntegerTest extends J
73          assertEquals(7,ai.get());
74      }
75  
76 +    /**
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);
104          while(!ai.weakCompareAndSet(1,2));
# Line 56 | Line 108 | public class AtomicIntegerTest extends J
108          assertEquals(7,ai.get());
109      }
110  
111 +    /**
112 +     * getAndSet returns previous value and sets to given value
113 +     */
114      public void testGetAndSet(){
115          AtomicInteger ai = new AtomicInteger(1);
116          assertEquals(1,ai.getAndSet(0));
# Line 63 | Line 118 | public class AtomicIntegerTest extends J
118          assertEquals(-10,ai.getAndSet(1));
119      }
120  
121 +    /**
122 +     * getAndAdd returns previous value and adds given value
123 +     */
124      public void testGetAndAdd(){
125          AtomicInteger ai = new AtomicInteger(1);
126          assertEquals(1,ai.getAndAdd(2));
# Line 71 | Line 129 | public class AtomicIntegerTest extends J
129          assertEquals(-1,ai.get());
130      }
131  
132 +    /**
133 +     * getAndDecrement returns previous value and decrements
134 +     */
135      public void testGetAndDecrement(){
136          AtomicInteger ai = new AtomicInteger(1);
137          assertEquals(1,ai.getAndDecrement());
# Line 78 | Line 139 | public class AtomicIntegerTest extends J
139          assertEquals(-1,ai.getAndDecrement());
140      }
141  
142 +    /**
143 +     * getAndIncrement returns previous value and increments
144 +     */
145      public void testGetAndIncrement(){
146          AtomicInteger ai = new AtomicInteger(1);
147          assertEquals(1,ai.getAndIncrement());
# Line 89 | Line 153 | public class AtomicIntegerTest extends J
153          assertEquals(1,ai.get());
154      }
155  
156 +    /**
157 +     * addAndGet adds given value to current, and returns current value
158 +     */
159      public void testAddAndGet(){
160          AtomicInteger ai = new AtomicInteger(1);
161          assertEquals(3,ai.addAndGet(2));
# Line 97 | Line 164 | public class AtomicIntegerTest extends J
164          assertEquals(-1,ai.get());
165      }
166  
167 +    /**
168 +     * decrementAndGet decrements and returns current value
169 +     */
170      public void testDecrementAndGet(){
171          AtomicInteger ai = new AtomicInteger(1);
172          assertEquals(0,ai.decrementAndGet());
# Line 105 | Line 175 | public class AtomicIntegerTest extends J
175          assertEquals(-2,ai.get());
176      }
177  
178 +    /**
179 +     * incrementAndGet increments and returns current value
180 +     */
181      public void testIncrementAndGet(){
182          AtomicInteger ai = new AtomicInteger(1);
183          assertEquals(2,ai.incrementAndGet());
# Line 116 | Line 189 | public class AtomicIntegerTest extends J
189          assertEquals(1,ai.get());
190      }
191  
192 +    /**
193 +     * a deserialized serialized atomic holds same value
194 +     */
195      public void testSerialization() {
196          AtomicInteger l = new AtomicInteger();
197  
# Line 131 | Line 207 | public class AtomicIntegerTest extends J
207              AtomicInteger r = (AtomicInteger) in.readObject();
208              assertEquals(l.get(), r.get());
209          } catch(Exception e){
210 <            e.printStackTrace();
135 <            fail("unexpected exception");
210 >            unexpectedException();
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