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.15 by jsr166, Tue Nov 17 03:12:51 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 AtomicIntegerTest extends J
19      }
20  
21      /**
22 <     *
22 >     * constructor initializes to given value
23       */
24 <    public void testConstructor(){
24 >    public void testConstructor() {
25          AtomicInteger ai = new AtomicInteger(1);
26          assertEquals(1,ai.get());
27      }
28  
29      /**
30 <     *
30 >     * default constructed initializes to zero
31       */
32 <    public void testConstructor2(){
32 >    public void testConstructor2() {
33          AtomicInteger ai = new AtomicInteger();
34          assertEquals(0,ai.get());
35      }
36  
37      /**
38 <     *
38 >     * get returns the last value set
39       */
40 <    public void testGetSet(){
40 >    public void testGetSet() {
41          AtomicInteger ai = new AtomicInteger(1);
42          assertEquals(1,ai.get());
43          ai.set(2);
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 <     *
63 >     * compareAndSet succeeds in changing value if equal to expected else fails
64       */
65 <    public void testCompareAndSet(){
65 >    public void testCompareAndSet() {
66          AtomicInteger ai = new AtomicInteger(1);
67          assertTrue(ai.compareAndSet(1,2));
68          assertTrue(ai.compareAndSet(2,-4));
# 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() throws Exception {
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 >
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 >
94 >    /**
95 >     * repeated weakCompareAndSet succeeds in changing value when equal
96 >     * to expected
97       */
98 <    public void testWeakCompareAndSet(){
98 >    public void testWeakCompareAndSet() {
99          AtomicInteger ai = new AtomicInteger(1);
100 <        while(!ai.weakCompareAndSet(1,2));
101 <        while(!ai.weakCompareAndSet(2,-4));
100 >        while (!ai.weakCompareAndSet(1,2));
101 >        while (!ai.weakCompareAndSet(2,-4));
102          assertEquals(-4,ai.get());
103 <        while(!ai.weakCompareAndSet(-4,7));
103 >        while (!ai.weakCompareAndSet(-4,7));
104          assertEquals(7,ai.get());
105      }
106  
107      /**
108 <     *
108 >     * getAndSet returns previous value and sets to given value
109       */
110 <    public void testGetAndSet(){
110 >    public void testGetAndSet() {
111          AtomicInteger ai = new AtomicInteger(1);
112          assertEquals(1,ai.getAndSet(0));
113          assertEquals(0,ai.getAndSet(-10));
# Line 82 | Line 115 | public class AtomicIntegerTest extends J
115      }
116  
117      /**
118 <     *
118 >     * getAndAdd returns previous value and adds given value
119       */
120 <    public void testGetAndAdd(){
120 >    public void testGetAndAdd() {
121          AtomicInteger ai = new AtomicInteger(1);
122          assertEquals(1,ai.getAndAdd(2));
123          assertEquals(3,ai.get());
# Line 93 | Line 126 | public class AtomicIntegerTest extends J
126      }
127  
128      /**
129 <     *
129 >     * getAndDecrement returns previous value and decrements
130       */
131 <    public void testGetAndDecrement(){
131 >    public void testGetAndDecrement() {
132          AtomicInteger ai = new AtomicInteger(1);
133          assertEquals(1,ai.getAndDecrement());
134          assertEquals(0,ai.getAndDecrement());
# Line 103 | Line 136 | public class AtomicIntegerTest extends J
136      }
137  
138      /**
139 <     *
139 >     * getAndIncrement returns previous value and increments
140       */
141 <    public void testGetAndIncrement(){
141 >    public void testGetAndIncrement() {
142          AtomicInteger ai = new AtomicInteger(1);
143          assertEquals(1,ai.getAndIncrement());
144          assertEquals(2,ai.get());
# Line 117 | Line 150 | public class AtomicIntegerTest extends J
150      }
151  
152      /**
153 <     *
153 >     * addAndGet adds given value to current, and returns current value
154       */
155 <    public void testAddAndGet(){
155 >    public void testAddAndGet() {
156          AtomicInteger ai = new AtomicInteger(1);
157          assertEquals(3,ai.addAndGet(2));
158          assertEquals(3,ai.get());
# Line 128 | Line 161 | public class AtomicIntegerTest extends J
161      }
162  
163      /**
164 <     *
164 >     * decrementAndGet decrements and returns current value
165       */
166 <    public void testDecrementAndGet(){
166 >    public void testDecrementAndGet() {
167          AtomicInteger ai = new AtomicInteger(1);
168          assertEquals(0,ai.decrementAndGet());
169          assertEquals(-1,ai.decrementAndGet());
# Line 139 | Line 172 | public class AtomicIntegerTest extends J
172      }
173  
174      /**
175 <     *
175 >     * incrementAndGet increments and returns current value
176       */
177 <    public void testIncrementAndGet(){
177 >    public void testIncrementAndGet() {
178          AtomicInteger ai = new AtomicInteger(1);
179          assertEquals(2,ai.incrementAndGet());
180          assertEquals(2,ai.get());
# Line 153 | Line 186 | public class AtomicIntegerTest extends J
186      }
187  
188      /**
189 <     *
189 >     * a deserialized serialized atomic holds same value
190       */
191 <    public void testSerialization() {
191 >    public void testSerialization() throws Exception {
192          AtomicInteger l = new AtomicInteger();
193  
194 <        try {
195 <            l.set(22);
196 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
197 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
198 <            out.writeObject(l);
199 <            out.close();
200 <
201 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
202 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
203 <            AtomicInteger r = (AtomicInteger) in.readObject();
204 <            assertEquals(l.get(), r.get());
205 <        } catch(Exception e){
206 <            unexpectedException();
194 >        l.set(22);
195 >        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
196 >        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
197 >        out.writeObject(l);
198 >        out.close();
199 >
200 >        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
201 >        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
202 >        AtomicInteger r = (AtomicInteger) in.readObject();
203 >        assertEquals(l.get(), r.get());
204 >    }
205 >
206 >    /**
207 >     * toString returns current value.
208 >     */
209 >    public void testToString() {
210 >        AtomicInteger ai = new AtomicInteger();
211 >        for (int i = -12; i < 6; ++i) {
212 >            ai.set(i);
213 >            assertEquals(ai.toString(), Integer.toString(i));
214          }
215      }
216  
217 +    /**
218 +     * intValue returns current value.
219 +     */
220 +    public void testIntValue() {
221 +        AtomicInteger ai = new AtomicInteger();
222 +        for (int i = -12; i < 6; ++i) {
223 +            ai.set(i);
224 +            assertEquals(i, ai.intValue());
225 +        }
226 +    }
227 +
228 +
229 +    /**
230 +     * longValue returns current value.
231 +     */
232 +    public void testLongValue() {
233 +        AtomicInteger ai = new AtomicInteger();
234 +        for (int i = -12; i < 6; ++i) {
235 +            ai.set(i);
236 +            assertEquals((long)i, ai.longValue());
237 +        }
238 +    }
239 +
240 +    /**
241 +     * floatValue returns current value.
242 +     */
243 +    public void testFloatValue() {
244 +        AtomicInteger ai = new AtomicInteger();
245 +        for (int i = -12; i < 6; ++i) {
246 +            ai.set(i);
247 +            assertEquals((float)i, ai.floatValue());
248 +        }
249 +    }
250 +
251 +    /**
252 +     * doubleValue returns current value.
253 +     */
254 +    public void testDoubleValue() {
255 +        AtomicInteger ai = new AtomicInteger();
256 +        for (int i = -12; i < 6; ++i) {
257 +            ai.set(i);
258 +            assertEquals((double)i, ai.doubleValue());
259 +        }
260 +    }
261 +
262 +
263  
264   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines