FileDocCategorySizeDatePackage
AtomicBooleanTest.javaAPI DocAndroid 1.5 API4484Wed May 06 22:41:02 BST 2009tests.api.java.util.concurrent

AtomicBooleanTest

public class AtomicBooleanTest extends JSR166TestCase

Fields Summary
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] args)

        junit.textui.TestRunner.run (suite());
    
public static junit.framework.Testsuite()

        return new TestSuite(AtomicBooleanTest.class);
    
public voidtestCompareAndSet()
compareAndSet succeeds in changing value if equal to expected else fails

        AtomicBoolean ai = new AtomicBoolean(true);
        assertTrue(ai.compareAndSet(true,false));
        assertEquals(false,ai.get());
        assertTrue(ai.compareAndSet(false,false));
        assertEquals(false,ai.get());
        assertFalse(ai.compareAndSet(true,false));
        assertFalse((ai.get()));
        assertTrue(ai.compareAndSet(false,true));
        assertEquals(true,ai.get());
    
public voidtestCompareAndSetInMultipleThreads()
compareAndSet in one thread enables another waiting for value to succeed

        final AtomicBoolean ai = new AtomicBoolean(true);
        Thread t = new Thread(new Runnable() {
                public void run() {
                    while(!ai.compareAndSet(false, true)) Thread.yield();
                }});
        try {
            t.start();
            assertTrue(ai.compareAndSet(true, false));
            t.join(LONG_DELAY_MS);
            assertFalse(t.isAlive());
        }
        catch(Exception e) {
            unexpectedException();
        }
    
public voidtestConstructor()
constructor initializes to given value

        AtomicBoolean ai = new AtomicBoolean(true);
        assertEquals(true,ai.get());
    
public voidtestConstructor2()
default constructed initializes to false

        AtomicBoolean ai = new AtomicBoolean();
        assertEquals(false,ai.get());
    
public voidtestGetAndSet()
getAndSet returns previous value and sets to given value

        AtomicBoolean ai = new AtomicBoolean(true);
        assertEquals(true,ai.getAndSet(false));
        assertEquals(false,ai.getAndSet(false));
        assertEquals(false,ai.getAndSet(true));
        assertEquals(true,ai.get());
    
public voidtestGetSet()
get returns the last value set

        AtomicBoolean ai = new AtomicBoolean(true);
        assertEquals(true,ai.get());
        ai.set(false);
        assertEquals(false,ai.get());
        ai.set(true);
        assertEquals(true,ai.get());
        
    
public voidtestSerialization()
a deserialized serialized atomic holds same value

        AtomicBoolean l = new AtomicBoolean();

        try {
            l.set(true);
            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));
            AtomicBoolean r = (AtomicBoolean) in.readObject();
            assertEquals(l.get(), r.get());
        } catch(Exception e){
            e.printStackTrace();
            unexpectedException();
        }
    
public voidtestToString()
toString returns current value.

        AtomicBoolean ai = new AtomicBoolean(); 
        assertEquals(ai.toString(), Boolean.toString(false));
        ai.set(true);
        assertEquals(ai.toString(), Boolean.toString(true));
    
public voidtestWeakCompareAndSet()
repeated weakCompareAndSet succeeds in changing value when equal to expected

        AtomicBoolean ai = new AtomicBoolean(true);
        while(!ai.weakCompareAndSet(true,false));
        assertEquals(false,ai.get());
        while(!ai.weakCompareAndSet(false,false));
        assertEquals(false,ai.get());
        while(!ai.weakCompareAndSet(false,true));
        assertEquals(true,ai.get());