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

AtomicIntegerFieldUpdaterTest

public class AtomicIntegerFieldUpdaterTest extends JSR166TestCase

Fields Summary
volatile int
x
int
w
long
z
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] args)

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

        return new TestSuite(AtomicIntegerFieldUpdaterTest.class);
    
public voidtestAddAndGet()
addAndGet adds given value to current, and returns current value

        AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
        try {
            a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
        } catch (RuntimeException ok) {
            return;
        }
        x = 1;
        assertEquals(3,a.addAndGet(this,2));
        assertEquals(3,a.get(this));
        assertEquals(-1,a.addAndGet(this,-4));
        assertEquals(-1,a.get(this));
    
public voidtestCompareAndSet()
compareAndSet succeeds in changing value if equal to expected else fails

        AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
        try {
            a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
        } catch (RuntimeException ok) {
            return;
        }
        x = 1;
        assertTrue(a.compareAndSet(this,1,2));
        assertTrue(a.compareAndSet(this,2,-4));
        assertEquals(-4,a.get(this));
        assertFalse(a.compareAndSet(this,-5,7));
        assertFalse((7 == a.get(this)));
        assertTrue(a.compareAndSet(this,-4,7));
        assertEquals(7,a.get(this));
    
public voidtestCompareAndSetInMultipleThreads()
compareAndSet in one thread enables another waiting for value to succeed

        x = 1;
        final AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest>a;
        try {
            a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
        } catch (RuntimeException ok) {
            return;
        }

        Thread t = new Thread(new Runnable() {
                public void run() {
                    while(!a.compareAndSet(AtomicIntegerFieldUpdaterTest.this, 2, 3)) Thread.yield();
                }});
        try {
            t.start();
            assertTrue(a.compareAndSet(this, 1, 2));
            t.join(LONG_DELAY_MS);
            assertFalse(t.isAlive());
            assertEquals(a.get(this), 3);
        }
        catch(Exception e) {
            unexpectedException();
        }
    
public voidtestConstructor()
Construction with non-existent field throws RuntimeException

        try{
            AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> 
                a = AtomicIntegerFieldUpdater.newUpdater
                (AtomicIntegerFieldUpdaterTest.class, "y");
            shouldThrow();
        }
        catch (RuntimeException rt) {}
    
public voidtestConstructor2()
construction with field not of given type throws RuntimeException

        try{
            AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> 
                a = AtomicIntegerFieldUpdater.newUpdater
                (AtomicIntegerFieldUpdaterTest.class, "z");
            shouldThrow();
        }
        catch (RuntimeException rt) {}
    
public voidtestConstructor3()
construction with non-volatile field throws RuntimeException

        try{
            AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> 
                a = AtomicIntegerFieldUpdater.newUpdater
                (AtomicIntegerFieldUpdaterTest.class, "w");
            shouldThrow();
        }
        catch (RuntimeException rt) {}
    
public voidtestDecrementAndGet()
decrementAndGet decrements and returns current value

        AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
        try {
            a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
        } catch (RuntimeException ok) {
            return;
        }
        x = 1;
        assertEquals(0,a.decrementAndGet(this));
        assertEquals(-1,a.decrementAndGet(this));
        assertEquals(-2,a.decrementAndGet(this));
        assertEquals(-2,a.get(this));
    
public voidtestGetAndAdd()
getAndAdd returns previous value and adds given value

        AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
        try {
            a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
        } catch (RuntimeException ok) {
            return;
        }
        x = 1;
        assertEquals(1,a.getAndAdd(this,2));
        assertEquals(3,a.get(this));
        assertEquals(3,a.getAndAdd(this,-4));
        assertEquals(-1,a.get(this));
    
public voidtestGetAndDecrement()
getAndDecrement returns previous value and decrements

        AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
        try {
            a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
        } catch (RuntimeException ok) {
            return;
        }
        x = 1;
        assertEquals(1,a.getAndDecrement(this));
        assertEquals(0,a.getAndDecrement(this));
        assertEquals(-1,a.getAndDecrement(this));
    
public voidtestGetAndIncrement()
getAndIncrement returns previous value and increments

        AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
        try {
            a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
        } catch (RuntimeException ok) {
            return;
        }
        x = 1;
        assertEquals(1,a.getAndIncrement(this));
        assertEquals(2,a.get(this));
        a.set(this,-2);
        assertEquals(-2,a.getAndIncrement(this));
        assertEquals(-1,a.getAndIncrement(this));
        assertEquals(0,a.getAndIncrement(this));
        assertEquals(1,a.get(this));
    
public voidtestGetAndSet()
getAndSet returns previous value and sets to given value

        AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
        try {
            a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
        } catch (RuntimeException ok) {
            return;
        }
        x = 1;
        assertEquals(1,a.getAndSet(this, 0));
        assertEquals(0,a.getAndSet(this,-10));
        assertEquals(-10,a.getAndSet(this,1));
    
public voidtestGetSet()
get returns the last value set or assigned

        AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
        try {
            a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
        } catch (RuntimeException ok) {
            return;
        }
        x = 1;
        assertEquals(1,a.get(this));
        a.set(this,2);
        assertEquals(2,a.get(this));
        a.set(this,-3);
        assertEquals(-3,a.get(this));
        
    
public voidtestIncrementAndGet()
incrementAndGet increments and returns current value

        AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
        try {
            a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
        } catch (RuntimeException ok) {
            return;
        }
        x = 1;
        assertEquals(2,a.incrementAndGet(this));
        assertEquals(2,a.get(this));
        a.set(this,-2);
        assertEquals(-1,a.incrementAndGet(this));
        assertEquals(0,a.incrementAndGet(this));
        assertEquals(1,a.incrementAndGet(this));
        assertEquals(1,a.get(this));
    
public voidtestWeakCompareAndSet()
repeated weakCompareAndSet succeeds in changing value when equal to expected

        AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
        try {
            a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x");
        } catch (RuntimeException ok) {
            return;
        }
        x = 1;
        while(!a.weakCompareAndSet(this,1,2));
        while(!a.weakCompareAndSet(this,2,-4));
        assertEquals(-4,a.get(this));
        while(!a.weakCompareAndSet(this,-4,7));
        assertEquals(7,a.get(this));