コード例 #1
0
 /** get returns the last value set */
 public void testGetSet() {
   AtomicLong ai = new AtomicLong(1);
   assertEquals(1, ai.get());
   ai.set(2);
   assertEquals(2, ai.get());
   ai.set(-3);
   assertEquals(-3, ai.get());
 }
コード例 #2
0
 /** doubleValue returns current value. */
 public void testDoubleValue() {
   AtomicLong ai = new AtomicLong();
   for (int i = -12; i < 6; ++i) {
     ai.set(i);
     assertEquals((double) i, ai.doubleValue(), 0.0);
   }
 }
コード例 #3
0
 /** floatValue returns current value. */
 public void testFloatValue() {
   AtomicLong ai = new AtomicLong();
   for (int i = -12; i < 6; ++i) {
     ai.set(i);
     assertEquals((float) i, ai.floatValue(), 0.0f);
   }
 }
コード例 #4
0
 /** longValue returns current value. */
 public void testLongValue() {
   AtomicLong ai = new AtomicLong();
   for (int i = -12; i < 6; ++i) {
     ai.set(i);
     assertEquals((long) i, ai.longValue());
   }
 }
コード例 #5
0
 /** toString returns current value. */
 public void testToString() {
   AtomicLong ai = new AtomicLong();
   for (long i = -12; i < 6; ++i) {
     ai.set(i);
     assertEquals(ai.toString(), Long.toString(i));
   }
 }
コード例 #6
0
 /** incrementAndGet increments and returns current value */
 public void testIncrementAndGet() {
   AtomicLong ai = new AtomicLong(1);
   assertEquals(2, ai.incrementAndGet());
   assertEquals(2, ai.get());
   ai.set(-2);
   assertEquals(-1, ai.incrementAndGet());
   assertEquals(0, ai.incrementAndGet());
   assertEquals(1, ai.incrementAndGet());
   assertEquals(1, ai.get());
 }
コード例 #7
0
  /** a deserialized serialized atomic holds same value */
  public void testSerialization() {
    AtomicLong l = new AtomicLong();

    try {
      l.set(-22);
      ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
      ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
      out.writeObject(l);
      out.close();

      ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
      ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
      AtomicLong r = (AtomicLong) in.readObject();
      assertEquals(l.get(), r.get());
    } catch (Exception e) {
      unexpectedException();
    }
  }