Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.mapdb;/*
   * Written by Doug Lea with assistance from members of JCP JSR-166
   * Expert Group and released to the public domain, as explained at
   * http://creativecommons.org/licenses/publicdomain
   * Other contributors include Andrew Wright, Jeffrey Hayes, 
   * Pat Fisher, Mike Judd. 
   */
  
 
 public class AtomicLongTest extends TestCase {
 
     DB db = DBMaker.newMemoryDB().transactionDisable().make();
     Atomic.Long ai = .createAtomicLong("test", 1);

    
constructor initializes to given value
 
     public void testConstructor(){
         assertEquals(1,.get());
     }

    
default constructed initializes to zero
 
     public void testConstructor2(){
         Atomic.Long ai = .getAtomicLong("test2");
         assertEquals(0,ai.get());
     }

    
get returns the last value set
 
     public void testGetSet(){
         assertEquals(1,.get());
         .set(2);
         assertEquals(2,.get());
         .set(-3);
         assertEquals(-3,.get());
 
     }

    
compareAndSet succeeds in changing value if equal to expected else fails
 
     public void testCompareAndSet(){
         assertTrue(.compareAndSet(1,2));
         assertTrue(.compareAndSet(2,-4));
         assertEquals(-4,.get());
         assertFalse(.compareAndSet(-5,7));
         assertFalse((7 == .get()));
         assertTrue(.compareAndSet(-4,7));
         assertEquals(7,.get());
     }

    
compareAndSet in one thread enables another waiting for value to succeed
 
     public void testCompareAndSetInMultipleThreads() throws InterruptedException {
         Thread t = new Thread(new Runnable() {
             public void run() {
                 while(!.compareAndSet(2, 3)) Thread.yield();
             }});
 
             t.start();
             assertTrue(.compareAndSet(1, 2));
             t.join(10);
             assertFalse(t.isAlive());
             assertEquals(.get(), 3);
 
     }


    
getAndSet returns previous value and sets to given value
 
     public void testGetAndSet(){
         assertEquals(1,.getAndSet(0));
         assertEquals(0,.getAndSet(-10));
         assertEquals(-10,.getAndSet(1));
     }

    
getAndAdd returns previous value and adds given value
 
     public void testGetAndAdd(){
         assertEquals(1,.getAndAdd(2));
         assertEquals(3,.get());
         assertEquals(3,.getAndAdd(-4));
         assertEquals(-1,.get());
     }

    
getAndDecrement returns previous value and decrements
 
     public void testGetAndDecrement(){
         assertEquals(1,.getAndDecrement());
         assertEquals(0,.getAndDecrement());
        assertEquals(-1,.getAndDecrement());
    }

    
getAndIncrement returns previous value and increments
    public void testGetAndIncrement(){
        
        assertEquals(1,.getAndIncrement());
        assertEquals(2,.get());
        .set(-2);
        assertEquals(-2,.getAndIncrement());
        assertEquals(-1,.getAndIncrement());
        assertEquals(0,.getAndIncrement());
        assertEquals(1,.get());
    }

    
addAndGet adds given value to current, and returns current value
    public void testAddAndGet(){
        
        assertEquals(3,.addAndGet(2));
        assertEquals(3,.get());
        assertEquals(-1,.addAndGet(-4));
        assertEquals(-1,.get());
    }

    
decrementAndGet decrements and returns current value
    public void testDecrementAndGet(){
        
        assertEquals(0,.decrementAndGet());
        assertEquals(-1,.decrementAndGet());
        assertEquals(-2,.decrementAndGet());
        assertEquals(-2,.get());
    }

    
incrementAndGet increments and returns current value
    public void testIncrementAndGet(){
        
        assertEquals(2,.incrementAndGet());
        assertEquals(2,.get());
        .set(-2);
        assertEquals(-1,.incrementAndGet());
        assertEquals(0,.incrementAndGet());
        assertEquals(1,.incrementAndGet());
        assertEquals(1,.get());
    }


    
toString returns current value.
    public void testToString() {
        for (long i = -12; i < 6; ++i) {
            .set(i);
            assertEquals(.toString(), Long.toString(i));
        }
    }

    
longValue returns current value.
    public void testLongValue() {
        for (int i = -12; i < 6; ++i) {
            .set(i);
            assertEquals((long)i.longValue());
        }
    }

    
floatValue returns current value.
    public void testFloatValue() {
        for (int i = -12; i < 6; ++i) {
            .set(i);
            assertEquals((float)i.floatValue());
        }
    }

    
doubleValue returns current value.
    public void testDoubleValue() {
        for (int i = -12; i < 6; ++i) {
            .set(i);
            assertEquals((double)i.doubleValue());
        }
    }
    public void testTX(){
        TxMaker txMaker = DBMaker.newMemoryDB().makeTxMaker();
        DB db = txMaker.makeTx();
        ..println(db.getAtomicLong("counter").incrementAndGet());
        db.commit();
        db = txMaker.makeTx();
        ..println(db.getAtomicLong("counter").incrementAndGet());
        db.commit();
        db = txMaker.makeTx();
        ..println(db.getAtomicLong("counter").incrementAndGet());
        db.commit();
    }
New to GrepCode? Check out our FAQ X