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

CopyOnWriteArraySetTest

public class CopyOnWriteArraySetTest extends JSR166TestCase

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

        junit.textui.TestRunner.run (suite());        
    
static java.util.concurrent.CopyOnWriteArraySetpopulatedSet(int n)

        CopyOnWriteArraySet a = new CopyOnWriteArraySet();
        assertTrue(a.isEmpty());
        for (int i = 0; i < n; ++i) 
            a.add(new Integer(i));
        assertFalse(a.isEmpty());
        assertEquals(n, a.size());
        return a;
    
public static junit.framework.Testsuite()

        return new TestSuite(CopyOnWriteArraySetTest.class);
    
public voidtestAdd2()
add will not add the element if it already exists in the set

        CopyOnWriteArraySet full = populatedSet(3);
        full.add(one);
        assertEquals(3, full.size());
    
public voidtestAdd3()
add adds the element when it does not exist in the set

        CopyOnWriteArraySet full = populatedSet(3);
        full.add(three);
        assertTrue(full.contains(three));
    
public voidtestAddAll()
addAll adds each element from the given collection

        CopyOnWriteArraySet full = populatedSet(3);
        Vector v = new Vector();
        v.add(three);
        v.add(four);
        v.add(five);
        full.addAll(v);
        assertEquals(6, full.size());
    
public voidtestAddAll2()
addAll adds each element from the given collection that did not already exist in the set

        CopyOnWriteArraySet full = populatedSet(3);
        Vector v = new Vector();
        v.add(three);
        v.add(four);
        v.add(one); // will not add this element
        full.addAll(v);
        assertEquals(5, full.size());
    
public voidtestClear()
clear removes all elements from the set

        CopyOnWriteArraySet full = populatedSet(3);
        full.clear();
        assertEquals(0, full.size());
    
public voidtestConstructor()
Default-constructed set is empty

        CopyOnWriteArraySet a = new CopyOnWriteArraySet();
        assertTrue(a.isEmpty());
    
public voidtestConstructor3()
Collection-constructed set holds all of its elements

        Integer[] ints = new Integer[SIZE];
        for (int i = 0; i < SIZE-1; ++i)
            ints[i] = new Integer(i);
        CopyOnWriteArraySet a = new CopyOnWriteArraySet(Arrays.asList(ints));
        for (int i = 0; i < SIZE; ++i) 
            assertTrue(a.contains(ints[i]));
    
public voidtestContains()
contains returns true for added elements

        CopyOnWriteArraySet full = populatedSet(3);
        assertTrue(full.contains(one));
        assertFalse(full.contains(five));
    
public voidtestContainsAll()
containsAll returns true for collections with subset of elements

        CopyOnWriteArraySet full = populatedSet(3);
        Vector v = new Vector();
        v.add(one);
        v.add(two);
        assertTrue(full.containsAll(v));
        v.add(six);
        assertFalse(full.containsAll(v));
    
public voidtestEquals()
Sets with equal elements are equal

        CopyOnWriteArraySet a = populatedSet(3);
        CopyOnWriteArraySet b = populatedSet(3);
        assertTrue(a.equals(b));
        assertTrue(b.equals(a));
        assertEquals(a.hashCode(), b.hashCode());
        a.add(m1);
        assertFalse(a.equals(b));
        assertFalse(b.equals(a));
        b.add(m1);
        assertTrue(a.equals(b));
        assertTrue(b.equals(a));
        assertEquals(a.hashCode(), b.hashCode());
    
public voidtestIsEmpty()
isEmpty is true when empty, else false

        CopyOnWriteArraySet empty = new CopyOnWriteArraySet();
        CopyOnWriteArraySet full = populatedSet(3);
        assertTrue(empty.isEmpty());
        assertFalse(full.isEmpty());
    
public voidtestIterator()
iterator() returns an iterator containing the elements of the set

        CopyOnWriteArraySet full = populatedSet(3);
        Iterator i = full.iterator();
        int j;
        for(j = 0; i.hasNext(); j++)
            assertEquals(j, ((Integer)i.next()).intValue());
        assertEquals(3, j);
    
public voidtestIteratorRemove()
iterator remove is unsupported

        CopyOnWriteArraySet full = populatedSet(3);
        Iterator it = full.iterator();
        it.next();
        try {
            it.remove();
            shouldThrow();
        }
        catch (UnsupportedOperationException success) {}
    
public voidtestRemove()
remove removes an element

        CopyOnWriteArraySet full = populatedSet(3);
        full.remove(one);
        assertFalse(full.contains(one));
        assertEquals(2, full.size());
    
public voidtestRemoveAll()
removeAll removes all elements from the given collection

        CopyOnWriteArraySet full = populatedSet(3);
        Vector v = new Vector();
        v.add(one);
        v.add(two);
        full.removeAll(v);
        assertEquals(1, full.size());
    
public voidtestSerialization()
A deserialized serialized set is equal

        CopyOnWriteArraySet q = populatedSet(SIZE);

        try {
            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
            out.writeObject(q);
            out.close();

            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
            CopyOnWriteArraySet r = (CopyOnWriteArraySet)in.readObject();
            assertEquals(q.size(), r.size());
            assertTrue(q.equals(r));
            assertTrue(r.equals(q));
        } catch(Exception e){
            unexpectedException();
        }
    
public voidtestSize()
size returns the number of elements

        CopyOnWriteArraySet empty = new CopyOnWriteArraySet();
        CopyOnWriteArraySet full = populatedSet(3);
        assertEquals(3, full.size());
        assertEquals(0, empty.size());
    
public voidtestToArray()
toArray returns an Object array containing all elements from the set

        CopyOnWriteArraySet full = populatedSet(3);
        Object[] o = full.toArray();
        assertEquals(3, o.length);
        assertEquals(0, ((Integer)o[0]).intValue());
        assertEquals(1, ((Integer)o[1]).intValue());
        assertEquals(2, ((Integer)o[2]).intValue());
    
public voidtestToArray2()
toArray returns an Integer array containing all elements from the set

        CopyOnWriteArraySet full = populatedSet(3);
        Integer[] i = new Integer[3];
        i = (Integer[])full.toArray(i);
        assertEquals(3, i.length);
        assertEquals(0, i[0].intValue());
        assertEquals(1, i[1].intValue());
        assertEquals(2, i[2].intValue());
    
public voidtestToArray_ArrayStoreException()
toArray throws an ArrayStoreException when the given array can not store the objects inside the set

        try {
            CopyOnWriteArraySet c = new CopyOnWriteArraySet();
            c.add("zfasdfsdf");
            c.add("asdadasd");
            c.toArray(new Long[5]);
            shouldThrow();
        } catch(ArrayStoreException e){}
    
public voidtestToString()
toString holds toString of elements

        CopyOnWriteArraySet full = populatedSet(3);
        String s = full.toString();
        for (int i = 0; i < 3; ++i) {
            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
        }