listtest.java

来自「mallet是自然语言处理、机器学习领域的一个开源项目。」· Java 代码 · 共 639 行 · 第 1/2 页

JAVA
639
字号
//Copyright (c) Corporation for National Research Initiativespackage javatests;import java.util.ArrayList;import java.util.Arrays;import java.util.Collection;import java.util.Collections;import java.util.Iterator;import java.util.List;import java.util.ListIterator;import java.util.NoSuchElementException;/** * @author updikca1 */public abstract class ListTest {        public static ListTest getArrayListTest(final boolean makeReadOnly) {        return new ListTest() {            public List newInstance(Collection c) {                List l = null;                if(c == null) {                    l = new ArrayList();                } else {                    l = new ArrayList(c);                }                return (makeReadOnly)                 		? Collections.unmodifiableList(l)                		: l;            }            public boolean isReadOnly() {                return makeReadOnly;            }                    };    }        public static void verifyImutability(List l) {                String message = "Expected UnsupportedOperationException.";                try {            l.add(0, new Integer(0));            TestSupport.assertThat(false, message);        } catch (UnsupportedOperationException e) {}                try {            l.add(new Integer(0));             TestSupport.assertThat(false, message);        } catch (UnsupportedOperationException e) {}                try {            l.addAll(null);              TestSupport.assertThat(false, message);        } catch (UnsupportedOperationException e) {}                try {            l.addAll(0, null);             TestSupport.assertThat(false, message);        } catch (UnsupportedOperationException e) {}                try {            l.clear();                  TestSupport.assertThat(false, message);        } catch (UnsupportedOperationException e) {}                try {            l.remove(0);             TestSupport.assertThat(false, message);        } catch (UnsupportedOperationException e) {}                try {            l.remove(new Object());            TestSupport.assertThat(false, message);        } catch (UnsupportedOperationException e) {}                try {            l.removeAll(null);             TestSupport.assertThat(false, message);        } catch (UnsupportedOperationException e) {}                try {            l.retainAll(null);            TestSupport.assertThat(false, message);        } catch (UnsupportedOperationException e) {}        try {            l.set(0,new Integer(0));            TestSupport.assertThat(false, message);        } catch (UnsupportedOperationException e) {}       }        private final List nullList;        protected List defaultList() {        List l = new ArrayList();        for (int i = 0; i < 4; i++) {            l.add(new Integer(i));        }        return newInstance(l);    }        /**     * Implementations must supply an empty list if the collection is null.     * @param c Initial collection or null for empty.     * @return the List instance     */    public List newInstance(Collection c) {        throw new UnsupportedOperationException("This method must be overridden");    }        /**     * @return true if the list is read-only (like PyTuple)     */    public boolean isReadOnly() {        throw new UnsupportedOperationException("This method must be overridden");    }        {        nullList = newInstance(Arrays.asList(new Object[] {null}));    }        public void testAll() {                test_get();        test_equals();        test_size();        test_contains();        test_containsAll();                try {            defaultList().hashCode();            test_hashCode();        } catch (Exception e) {            // skip unhashable types        }                        test_subList();        test_lastIndexOf();        test_listIterator();        test_toArray();        test_toArray_typed();              if(!isReadOnly()) {                 test_add();            test_add_index();            test_set();            test_clear();            test_addAll();            test_addAll_index();              test_remove();            test_remove_index();            test_removeAll();            test_retainAll();        } else {            verifyImutability(newInstance(null));        }    }        /** Tests get(int index) */    public void test_get() {        TestSupport.assertThat(defaultList().get(0).equals(new Integer(0)),        		"get() did not return expected value of Integer(0)");        try {            defaultList().get(-1);            TestSupport.assertThat(false,             		"get(<negative index>) did not throw IndexOutOfBoundsException");        } catch (IndexOutOfBoundsException e) {}                try {            defaultList().get(-1);            TestSupport.assertThat(false,             		"get(<index too big>) did not throw IndexOutOfBoundsException");        } catch (IndexOutOfBoundsException e) {}    }        /** Tests set(int index, Object element) */    public void test_set() {                try {            newInstance(null).set(-1, "spam");            TestSupport.assertThat(false,             		"get(<negative index>) did not throw IndexOutOfBoundsException");        } catch (IndexOutOfBoundsException e) {}                try {            newInstance(null).set(0, "spam");            TestSupport.assertThat(false,             	"set(<index too big>) did not throw IndexOutOfBoundsException");        } catch (IndexOutOfBoundsException e) {}                List a = defaultList();        a.set(a.size() - 1 , "spam");        TestSupport.assertThat(a.get(a.size() - 1).equals("spam"),         		"set() object was not retrieved via get()");    }        /** Tests add(Object o) */     public void test_add() {            List a = newInstance(null);        for (int i = 0; i < 4; i++) {            a.add(new Integer(i));        }        TestSupport.assertEquals(a, defaultList(), "add(Object o) failed");    }        /** Tests isEmpty() */    public void test_isEmpty() {        List a = newInstance(null);        TestSupport.assertThat(a.isEmpty(),                 "isEmpty() is false on an emtpy List");        a.addAll(defaultList());        TestSupport.assertThat(!a.isEmpty(),                 "isEmpty() is true on a non-empty List)" );        a.clear();        TestSupport.assertThat(a.isEmpty(),                 "isEmpty() is false on an emtpy List");            }        /** Tests size() */    public void test_size() {         List b = newInstance(null);        TestSupport.assertThat(b.size() == 0, "empty list size was not 0");        TestSupport.assertThat(defaultList().size() == 4,                 "default list did not have a size of 4");         }        /** Tests  add(int index, Object element) */           public void test_add_index() {        List a = newInstance(null);        List b = defaultList();        for (int i = 0; i < b.size(); i++) {            a.add(i, b.get(i));        }                try {            a.add(a.size() + 1, new Integer(a.size() + 1));              TestSupport.assertThat(false, "expected IndexOutOfBoundsException");        } catch (IndexOutOfBoundsException e) {}                try {            a.add(-1, new Integer(-1));              TestSupport.assertThat(false, "expected IndexOutOfBoundsException");        } catch (IndexOutOfBoundsException e) {}                               }        /** Tests  equals(Object o)*/           public void test_equals() {        TestSupport.assertEquals(defaultList(), defaultList(),         		"Identical lists weren't equal()");        TestSupport.assertNotEquals(newInstance(null), defaultList(),         		"Different lists were equal()");        TestSupport.assertNotEquals(newInstance(null), new Object(),         		"List was equal to a non-List type");    }        /** Tests addAll(Collection c) */    public void test_addAll() {        List a = defaultList();        List b = defaultList();                TestSupport.assertThat(a.addAll(b) == true,         		"Mutating addAll(Collection) returned false");        TestSupport.assertThat(a.addAll(newInstance(null)) == false,         		"Idempotent addAll(Collection) returned true");        TestSupport.assertThat(b.addAll(b) == true,                "Mutating addAll(Collection) returned false");        TestSupport.assertEquals(a, b,                 "Expected equal objects from addAll(collection)");        TestSupport.assertThat(a.size() == 8,         		"Expected List to have size 8 after addAll(Collection)");          }        /** Tests indexOf(Object o) */           public void indexOf() {        TestSupport.assertThat(defaultList().indexOf(new Integer(3)) == 3,                 "indexOf(3) did not return 3");         TestSupport.assertThat(defaultList().indexOf(new Integer(42)) == -1,         		"indexOf() non-existing entry did not return -1");        TestSupport.assertThat(defaultList().indexOf(null) == -1,         		"indexOf() non-existing null did not return -1");             }        /** Tests contains(Object o) */           public void test_contains() {         TestSupport.assertThat(defaultList().contains(new Integer(42)) == false,         		"contains() returned true for non-existing entry");        TestSupport.assertThat(defaultList().contains(new Integer(0)) == true,         		"contains() returned false for existing entry");        TestSupport.assertThat(nullList.contains(null) == true,         		"contains() returned false for existing null entry");        TestSupport.assertThat(defaultList().contains(null) == false,         		"contains() returned true for non-existing null entry");    }        /** Tests remove(Object o) */    public void test_remove() {        List a = defaultList();        a.add(null);        TestSupport.assertThat(a.remove(null) == true,         		"remove() existing null entry returned false");        TestSupport.assertThat(a.remove(null) == false,         		"remove() non-existing null entry returned false");        a.add("spam");        TestSupport.assertThat(a.remove("spam") == true,         		"remove() existing entry returned false");        TestSupport.assertThat(a.remove("spam") == false,         		"remove() non-existing entry returned true");    }            /** Tests remove(int index) */    public void test_remove_index() {                    List a = defaultList();        for (int i = 0, n = a.size(); i < n; i++) {            a.remove(0);        }        TestSupport.assertThat(a.size() == 0,                 "remove()-d all entries but size() not 0");        

⌨️ 快捷键说明

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