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 AtomicBooleanTest extends TestCase{
    DB db = DBMaker.newMemoryDB().transactionDisable().make();
    Atomic.Boolean ai = .createAtomicBoolean("test"true);

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

    
default constructed initializes to false
    public void testConstructor2() {
        Atomic.Boolean ai = .getAtomicBoolean("test2");
        assertEquals(false,ai.get());
    }

    
get returns the last value set
    public void testGetSet() {
        assertEquals(true,.get());
        .set(false);
        assertEquals(false,.get());
        .set(true);
        assertEquals(true,.get());
    }

    
compareAndSet succeeds in changing value if equal to expected else fails
    public void testCompareAndSet() {
        assertTrue(.compareAndSet(true,false));
        assertEquals(false,.get());
        assertTrue(.compareAndSet(false,false));
        assertEquals(false,.get());
        assertFalse(.compareAndSet(true,false));
        assertFalse((.get()));
        assertTrue(.compareAndSet(false,true));
        assertEquals(true,.get());
    }

    
compareAndSet in one thread enables another waiting for value to succeed
        Thread t = new Thread(new Runnable() {
            public void run() {
                while(!.compareAndSet(falsetrue)) Thread.yield();
            }});
            t.start();
            assertTrue(.compareAndSet(truefalse));
            t.join(10);
            assertFalse(t.isAlive());
    }

    
getAndSet returns previous value and sets to given value
    public void testGetAndSet() {
        assertEquals(true,.getAndSet(false));
        assertEquals(false,.getAndSet(false));
        assertEquals(false,.getAndSet(true));
        assertEquals(true,.get());
    }
    
toString returns current value.
    public void testToString() {
        Atomic.Boolean ai = .getAtomicBoolean"test2");
        assertEquals(ai.toString(), Boolean.toString(false));
        ai.set(true);
        assertEquals(ai.toString(), Boolean.toString(true));
    }
New to GrepCode? Check out our FAQ X