copyonwritearraylisttest.java

来自「SRI international 发布的OAA框架软件」· Java 代码 · 共 623 行 · 第 1/2 页

JAVA
623
字号
     *   remove  removes and returns the object at the given index
     */
    public void testRemove() {
	CopyOnWriteArrayList full = populatedArray(3);
	assertEquals(two, full.remove(2));
	assertEquals(2, full.size());
    }

    /**
     *   removeAll  removes all elements from the given collection
     */
    public void testRemoveAll() {
	CopyOnWriteArrayList full = populatedArray(3);
	Vector v = new Vector();
	v.add(one);
	v.add(two);
	full.removeAll(v);
	assertEquals(1, full.size());
    }

    /**
     *   set  changes the element at the given index
     */
    public void testSet() {
	CopyOnWriteArrayList full = populatedArray(3);
	assertEquals(two, full.set(2, four));
	assertEquals(4, ((Integer)full.get(2)).intValue());
    }

    /**
     *   size returns the number of elements
     */
    public void testSize() {
	CopyOnWriteArrayList empty = new CopyOnWriteArrayList();
	CopyOnWriteArrayList full = populatedArray(SIZE);
	assertEquals(SIZE, full.size());
	assertEquals(0, empty.size());
    }

    /**
     *   toArray returns an Object array containing all elements from the list
     */
    public void testToArray() {
	CopyOnWriteArrayList full = populatedArray(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());
    }

    /**
     *   toArray returns an Integer array containing all elements from
     *   the list
     */
    public void testToArray2() {
	CopyOnWriteArrayList full = populatedArray(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());
    }


    /**
     * sublists contains elements at indexes offset from their base
     */
    public void testSubList() {
	CopyOnWriteArrayList a = populatedArray(10);
        assertTrue(a.subList(1,1).isEmpty());
	for(int j = 0; j < 9; ++j) {
	    for(int i = j ; i < 10; ++i) {
		List b = a.subList(j,i);
		for(int k = j; k < i; ++k) {
		    assertEquals(new Integer(k), b.get(k-j));
		}
	    }
	}

	List s = a.subList(2, 5);
        assertEquals(s.size(), 3);
        s.set(2, m1);
        assertEquals(a.get(4), m1);
	s.clear();
        assertEquals(a.size(), 7);
    }

    // Exception tests

    /**
     *   toArray throws an ArrayStoreException when the given array
     *  can not store the objects inside the list
     */
    public void testToArray_ArrayStoreException() {
        try {
            CopyOnWriteArrayList c = new CopyOnWriteArrayList();
            c.add("zfasdfsdf");
            c.add("asdadasd");
            c.toArray(new Long[5]);
	    shouldThrow();
        } catch(ArrayStoreException e){}
    }

    /**
     *   get throws an IndexOutOfBoundsException on a negative index
     */
    public void testGet1_IndexOutOfBoundsException() {
        try {
            CopyOnWriteArrayList c = new CopyOnWriteArrayList();
            c.get(-1);
            shouldThrow();
        } catch(IndexOutOfBoundsException e){}
    }

    /**
     *   get throws an IndexOutOfBoundsException on a too high index
     */
    public void testGet2_IndexOutOfBoundsException() {
        try {
            CopyOnWriteArrayList c = new CopyOnWriteArrayList();
            c.add("asdasd");
            c.add("asdad");
            c.get(100);
            shouldThrow();
        } catch(IndexOutOfBoundsException e){}
    }

    /**
     *   set throws an IndexOutOfBoundsException on a negative index
     */
    public void testSet1_IndexOutOfBoundsException() {
        try {
            CopyOnWriteArrayList c = new CopyOnWriteArrayList();
            c.set(-1,"qwerty");
            shouldThrow();
        } catch(IndexOutOfBoundsException e){}
    }

    /**
     *   set throws an IndexOutOfBoundsException on a too high index
     */
    public void testSet2() {
        try {
            CopyOnWriteArrayList c = new CopyOnWriteArrayList();
            c.add("asdasd");
            c.add("asdad");
            c.set(100, "qwerty");
            shouldThrow();
        } catch(IndexOutOfBoundsException e){}
    }

    /**
     *   add throws an IndexOutOfBoundsException on a negative index
     */
    public void testAdd1_IndexOutOfBoundsException() {
        try {
            CopyOnWriteArrayList c = new CopyOnWriteArrayList();
            c.add(-1,"qwerty");
            shouldThrow();
        } catch(IndexOutOfBoundsException e){}
    }

    /**
     *   add throws an IndexOutOfBoundsException on a too high index
     */
    public void testAdd2_IndexOutOfBoundsException() {
        try {
            CopyOnWriteArrayList c = new CopyOnWriteArrayList();
            c.add("asdasd");
            c.add("asdasdasd");
            c.add(100, "qwerty");
            shouldThrow();
        } catch(IndexOutOfBoundsException e){}
    }

    /**
     *   remove throws an IndexOutOfBoundsException on a negative index
     */
    public void testRemove1_IndexOutOfBounds() {
        try {
            CopyOnWriteArrayList c = new CopyOnWriteArrayList();
            c.remove(-1);
            shouldThrow();
        } catch(IndexOutOfBoundsException e){}
    }

    /**
     *   remove throws an IndexOutOfBoundsException on a too high index
     */
    public void testRemove2_IndexOutOfBounds() {
        try {
            CopyOnWriteArrayList c = new CopyOnWriteArrayList();
            c.add("asdasd");
            c.add("adasdasd");
            c.remove(100);
            shouldThrow();
        } catch(IndexOutOfBoundsException e){}
    }

    /**
     *   addAll throws an IndexOutOfBoundsException on a negative index
     */
    public void testAddAll1_IndexOutOfBoundsException() {
        try {
            CopyOnWriteArrayList c = new CopyOnWriteArrayList();
            c.addAll(-1,new LinkedList());
            shouldThrow();
        } catch(IndexOutOfBoundsException e){}
    }

    /**
     *   addAll throws an IndexOutOfBoundsException on a too high index
     */
    public void testAddAll2_IndexOutOfBoundsException() {
        try {
            CopyOnWriteArrayList c = new CopyOnWriteArrayList();
            c.add("asdasd");
            c.add("asdasdasd");
            c.addAll(100, new LinkedList());
            shouldThrow();
        } catch(IndexOutOfBoundsException e){}
    }

    /**
     *   listIterator throws an IndexOutOfBoundsException on a negative index
     */
    public void testListIterator1_IndexOutOfBoundsException() {
        try {
            CopyOnWriteArrayList c = new CopyOnWriteArrayList();
            c.listIterator(-1);
            shouldThrow();
        } catch(IndexOutOfBoundsException e){}
    }

    /**
     *   listIterator throws an IndexOutOfBoundsException on a too high index
     */
    public void testListIterator2_IndexOutOfBoundsException() {
        try {
            CopyOnWriteArrayList c = new CopyOnWriteArrayList();
            c.add("adasd");
            c.add("asdasdas");
            c.listIterator(100);
            shouldThrow();
        } catch(IndexOutOfBoundsException e){}
    }

    /**
     *   subList throws an IndexOutOfBoundsException on a negative index
     */
    public void testSubList1_IndexOutOfBoundsException() {
        try {
            CopyOnWriteArrayList c = new CopyOnWriteArrayList();
            c.subList(-1,100);

            shouldThrow();
        } catch(IndexOutOfBoundsException e){}
    }

    /**
     *   subList throws an IndexOutOfBoundsException on a too high index
     */
    public void testSubList2_IndexOutOfBoundsException() {
        try {
            CopyOnWriteArrayList c = new CopyOnWriteArrayList();
            c.add("asdasd");
            c.subList(1,100);
            shouldThrow();
        } catch(IndexOutOfBoundsException e){}
    }

    /**
     *   subList throws IndexOutOfBoundsException when the second index
     *  is lower then the first
     */
    public void testSubList3_IndexOutOfBoundsException() {
        try {
            CopyOnWriteArrayList c = new CopyOnWriteArrayList();
            c.subList(3,1);

            shouldThrow();
        } catch(IndexOutOfBoundsException e){}
    }

    /**
     * a deserialized serialiszed list is equal
     */
    public void testSerialization() {
        CopyOnWriteArrayList q = populatedArray(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));
            CopyOnWriteArrayList r = (CopyOnWriteArrayList)in.readObject();
            assertEquals(q.size(), r.size());
            assertTrue(q.equals(r));
            assertTrue(r.equals(q));
        } catch(Exception e){
            unexpectedException();
        }
    }

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?