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.16 by jsr166, Sat Nov 21 02:07:26 2009 UTC vs.
Revision 1.22 by jsr166, Tue May 31 16:16:23 2011 UTC

# Line 1 | Line 1
1   /*
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
4 > * http://creativecommons.org/publicdomain/zero/1.0/
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.*;
10 > import java.util.concurrent.atomic.AtomicLong;
11  
12   public class AtomicLongTest extends JSR166TestCase {
13 <    public static void main (String[] args) {
14 <        junit.textui.TestRunner.run (suite());
13 >    public static void main(String[] args) {
14 >        junit.textui.TestRunner.run(suite());
15      }
16      public static Test suite() {
17          return new TestSuite(AtomicLongTest.class);
18      }
19  
20 +    final long[] VALUES = {
21 +        Long.MIN_VALUE,
22 +        Integer.MIN_VALUE, -1, 0, 1, 42, Integer.MAX_VALUE,
23 +        Long.MAX_VALUE,
24 +    };
25 +
26      /**
27       * constructor initializes to given value
28       */
# Line 44 | Line 49 | public class AtomicLongTest extends JSR1
49          assertEquals(2,ai.get());
50          ai.set(-3);
51          assertEquals(-3,ai.get());
47
52      }
53  
54      /**
# Line 57 | Line 61 | public class AtomicLongTest extends JSR1
61          assertEquals(2,ai.get());
62          ai.lazySet(-3);
63          assertEquals(-3,ai.get());
60
64      }
65  
66      /**
# Line 69 | Line 72 | public class AtomicLongTest extends JSR1
72          assertTrue(ai.compareAndSet(2,-4));
73          assertEquals(-4,ai.get());
74          assertFalse(ai.compareAndSet(-5,7));
75 <        assertFalse((7 == ai.get()));
75 >        assertEquals(-4,ai.get());
76          assertTrue(ai.compareAndSet(-4,7));
77          assertEquals(7,ai.get());
78      }
# Line 191 | Line 194 | public class AtomicLongTest extends JSR1
194       * a deserialized serialized atomic holds same value
195       */
196      public void testSerialization() throws Exception {
197 <        AtomicLong l = new AtomicLong();
198 <
199 <        l.set(-22);
200 <        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
201 <        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
202 <        out.writeObject(l);
203 <        out.close();
204 <
202 <        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
203 <        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
204 <        AtomicLong r = (AtomicLong) in.readObject();
205 <        assertEquals(l.get(), r.get());
197 >        AtomicLong x = new AtomicLong();
198 >        AtomicLong y = serialClone(x);
199 >        assertTrue(x != y);
200 >        x.set(-22);
201 >        AtomicLong z = serialClone(x);
202 >        assertEquals(-22, x.get());
203 >        assertEquals(0, y.get());
204 >        assertEquals(-22, z.get());
205      }
206  
207      /**
# Line 210 | Line 209 | public class AtomicLongTest extends JSR1
209       */
210      public void testToString() {
211          AtomicLong ai = new AtomicLong();
212 <        for (long i = -12; i < 6; ++i) {
212 >        assertEquals("0", ai.toString());
213 >        for (long i : VALUES) {
214              ai.set(i);
215              assertEquals(ai.toString(), Long.toString(i));
216          }
217      }
218  
219      /**
220 +     * intValue returns current value.
221 +     */
222 +    public void testIntValue() {
223 +        AtomicLong ai = new AtomicLong();
224 +        assertEquals(0, ai.intValue());
225 +        for (long x : VALUES) {
226 +            ai.set(x);
227 +            assertEquals((int)x, ai.intValue());
228 +        }
229 +    }
230 +
231 +    /**
232       * longValue returns current value.
233       */
234      public void testLongValue() {
235          AtomicLong ai = new AtomicLong();
236 <        for (int i = -12; i < 6; ++i) {
237 <            ai.set(i);
238 <            assertEquals((long)i, ai.longValue());
236 >        assertEquals(0L, ai.longValue());
237 >        for (long x : VALUES) {
238 >            ai.set(x);
239 >            assertEquals(x, ai.longValue());
240          }
241      }
242  
# Line 232 | Line 245 | public class AtomicLongTest extends JSR1
245       */
246      public void testFloatValue() {
247          AtomicLong ai = new AtomicLong();
248 <        for (int i = -12; i < 6; ++i) {
249 <            ai.set(i);
250 <            assertEquals((float)i, ai.floatValue());
248 >        assertEquals(0.0f, ai.floatValue());
249 >        for (long x : VALUES) {
250 >            ai.set(x);
251 >            assertEquals((float)x, ai.floatValue());
252          }
253      }
254  
# Line 243 | Line 257 | public class AtomicLongTest extends JSR1
257       */
258      public void testDoubleValue() {
259          AtomicLong ai = new AtomicLong();
260 <        for (int i = -12; i < 6; ++i) {
261 <            ai.set(i);
262 <            assertEquals((double)i, ai.doubleValue());
260 >        assertEquals(0.0d, ai.doubleValue());
261 >        for (long x : VALUES) {
262 >            ai.set(x);
263 >            assertEquals((double)x, ai.doubleValue());
264          }
265      }
266  
252
267   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines