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

AtomicReferenceArrayTest

public class AtomicReferenceArrayTest 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(AtomicReferenceArrayTest.class);
    
public voidtestCompareAndSet()
compareAndSet succeeds in changing value if equal to expected else fails

        AtomicReferenceArray ai = new AtomicReferenceArray(SIZE); 
        for (int i = 0; i < SIZE; ++i) {
            ai.set(i, one);
            assertTrue(ai.compareAndSet(i, one,two));
            assertTrue(ai.compareAndSet(i, two,m4));
            assertEquals(m4,ai.get(i));
            assertFalse(ai.compareAndSet(i, m5,seven));
            assertFalse((seven.equals(ai.get(i))));
            assertTrue(ai.compareAndSet(i, m4,seven));
            assertEquals(seven,ai.get(i));
        }
    
public voidtestCompareAndSetInMultipleThreads()
compareAndSet in one thread enables another waiting for value to succeed

        final AtomicReferenceArray a = new AtomicReferenceArray(1);
        a.set(0, one);
        Thread t = new Thread(new Runnable() {
                public void run() {
                    while(!a.compareAndSet(0, two, three)) Thread.yield();
                }});
        try {
            t.start();
            assertTrue(a.compareAndSet(0, one, two));
            t.join(LONG_DELAY_MS);
            assertFalse(t.isAlive());
            assertEquals(a.get(0), three);
        }
        catch(Exception e) {
            unexpectedException();
        }
    
public voidtestConstructor()
constructor creates array of given size with all elements null

        AtomicReferenceArray<Integer> ai = new AtomicReferenceArray<Integer>(SIZE);
        for (int i = 0; i < SIZE; ++i) {
            assertNull(ai.get(i));
        }
    
public voidtestConstructor2()
constructor with array is of same size and has all elements

        Integer[] a = { two, one, three, four, seven};
        AtomicReferenceArray<Integer> ai = new AtomicReferenceArray<Integer>(a);
        assertEquals(a.length, ai.length());
        for (int i = 0; i < a.length; ++i) 
            assertEquals(a[i], ai.get(i));
    
public voidtestConstructor2NPE()
constructor with null array throws NPE

        try {
            Integer[] a = null;
            AtomicReferenceArray<Integer> ai = new AtomicReferenceArray<Integer>(a);
        } catch (NullPointerException success) {
        } catch (Exception ex) {
            unexpectedException();
        }
    
public voidtestGetAndSet()
getAndSet returns previous value and sets to given value at given index

        AtomicReferenceArray ai = new AtomicReferenceArray(SIZE); 
        for (int i = 0; i < SIZE; ++i) {
            ai.set(i, one);
            assertEquals(one,ai.getAndSet(i,zero));
            assertEquals(0,ai.getAndSet(i,m10));
            assertEquals(m10,ai.getAndSet(i,one));
        }
    
public voidtestGetSet()
get returns the last value set at index

        AtomicReferenceArray ai = new AtomicReferenceArray(SIZE); 
        for (int i = 0; i < SIZE; ++i) {
            ai.set(i, one);
            assertEquals(one,ai.get(i));
            ai.set(i, two);
            assertEquals(two,ai.get(i));
            ai.set(i, m3);
            assertEquals(m3,ai.get(i));
        }
    
public voidtestIndexing()
get and set for out of bound indices throw IndexOutOfBoundsException

        AtomicReferenceArray<Integer> ai = new AtomicReferenceArray<Integer>(SIZE);
        try {
            ai.get(SIZE);
        } catch(IndexOutOfBoundsException success){
        }
        try {
            ai.get(-1);
        } catch(IndexOutOfBoundsException success){
        }
        try {
            ai.set(SIZE, null);
        } catch(IndexOutOfBoundsException success){
        }
        try {
            ai.set(-1, null);
        } catch(IndexOutOfBoundsException success){
        }
    
public voidtestSerialization()
a deserialized serialized array holds same values

        AtomicReferenceArray l = new AtomicReferenceArray(SIZE); 
        for (int i = 0; i < SIZE; ++i) {
            l.set(i, new Integer(-i));
        }

        try {
            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));
            AtomicReferenceArray r = (AtomicReferenceArray) in.readObject();
            assertEquals(l.length(), r.length());
            for (int i = 0; i < SIZE; ++i) {
                assertEquals(r.get(i), l.get(i));
            }
        } catch(Exception e){
            unexpectedException();
        }
    
public voidtestToString()
toString returns current value.

        Integer[] a = { two, one, three, four, seven};
        AtomicReferenceArray<Integer> ai = new AtomicReferenceArray<Integer>(a);
        assertEquals(Arrays.toString(a), ai.toString());
    
public voidtestWeakCompareAndSet()
repeated weakCompareAndSet succeeds in changing value when equal to expected

        AtomicReferenceArray ai = new AtomicReferenceArray(SIZE); 
        for (int i = 0; i < SIZE; ++i) {
            ai.set(i, one);
            while(!ai.weakCompareAndSet(i, one,two));
            while(!ai.weakCompareAndSet(i, two,m4));
            assertEquals(m4,ai.get(i));
            while(!ai.weakCompareAndSet(i, m4,seven));
            assertEquals(seven,ai.get(i));
        }