⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testshortlist.java

📁 Office格式转换代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        assertTrue(!list2.equals(list));    }    /**     * test the get method     */    public void testGet()    {        ShortList list = new ShortList();        for (short j = 0; j < 1000; j++)        {            list.add(j);        }        for (short j = 0; j < 1001; j++)        {            try            {                assertEquals(j, list.get(j));                if (j == 1000)                {                    fail("should have gotten exception");                }            }            catch (IndexOutOfBoundsException e)            {                if (j != 1000)                {                    fail("unexpected IndexOutOfBoundsException");                }            }        }    }    /**     * test the indexOf method     */    public void testIndexOf()    {        ShortList list = new ShortList();        for (short j = 0; j < 1000; j++)        {            list.add(( short ) (j / 2));        }        for (short j = 0; j < 1000; j++)        {            if (j < 500)            {                assertEquals(j * 2, list.indexOf(j));            }            else            {                assertEquals(-1, list.indexOf(j));            }        }    }    /**     * test the isEmpty method     */    public void testIsEmpty()    {        ShortList list1 = new ShortList();        ShortList list2 = new ShortList(1000);        ShortList list3 = new ShortList(list1);        assertTrue(list1.isEmpty());        assertTrue(list2.isEmpty());        assertTrue(list3.isEmpty());        list1.add(( short ) 1);        list2.add(( short ) 2);        list3 = new ShortList(list2);        assertTrue(!list1.isEmpty());        assertTrue(!list2.isEmpty());        assertTrue(!list3.isEmpty());        list1.clear();        list2.remove(0);        list3.removeValue(( short ) 2);        assertTrue(list1.isEmpty());        assertTrue(list2.isEmpty());        assertTrue(list3.isEmpty());    }    /**     * test the lastIndexOf method     */    public void testLastIndexOf()    {        ShortList list = new ShortList();        for (short j = 0; j < 1000; j++)        {            list.add(( short ) (j / 2));        }        for (short j = 0; j < 1000; j++)        {            if (j < 500)            {                assertEquals(1 + j * 2, list.lastIndexOf(j));            }            else            {                assertEquals(-1, list.indexOf(j));            }        }    }    /**     * test the remove method     */    public void testRemove()    {        ShortList list = new ShortList();        for (short j = 0; j < 1000; j++)        {            list.add(j);        }        for (short j = 0; j < 1000; j++)        {            assertEquals(j, list.remove(0));            assertEquals(( short ) (999 - j), list.size());        }        for (short j = 0; j < 1000; j++)        {            list.add(j);        }        for (short j = 0; j < 1000; j++)        {            assertEquals(( short ) (999 - j),                         list.remove(( short ) (999 - j)));            assertEquals(999 - j, list.size());        }        try        {            list.remove(0);            fail("should have caught IndexOutOfBoundsException");        }        catch (IndexOutOfBoundsException e)        {            // as expected        }    }    /**     * test the removeValue method     */    public void testRemoveValue()    {        ShortList list = new ShortList();        for (short j = 0; j < 1000; j++)        {            list.add(( short ) (j / 2));        }        for (short j = 0; j < 1000; j++)        {            if (j < 500)            {                assertTrue(list.removeValue(j));                assertTrue(list.removeValue(j));            }            assertTrue(!list.removeValue(j));        }    }    /**     * test the removeAll method     */    public void testRemoveAll()    {        ShortList list = new ShortList();        for (short j = 0; j < 1000; j++)        {            list.add(j);        }        ShortList listCopy = new ShortList(list);        ShortList listOdd  = new ShortList();        ShortList listEven = new ShortList();        for (short j = 0; j < 1000; j++)        {            if (j % 2 == 0)            {                listEven.add(j);            }            else            {                listOdd.add(j);            }        }        list.removeAll(listEven);        assertEquals(list, listOdd);        list.removeAll(listOdd);        assertTrue(list.isEmpty());        listCopy.removeAll(listOdd);        assertEquals(listCopy, listEven);        listCopy.removeAll(listEven);        assertTrue(listCopy.isEmpty());    }    /**     * test the retainAll method     */    public void testRetainAll()    {        ShortList list = new ShortList();        for (short j = 0; j < 1000; j++)        {            list.add(j);        }        ShortList listCopy = new ShortList(list);        ShortList listOdd  = new ShortList();        ShortList listEven = new ShortList();        for (short j = 0; j < 1000; j++)        {            if (j % 2 == 0)            {                listEven.add(j);            }            else            {                listOdd.add(j);            }        }        list.retainAll(listOdd);        assertEquals(list, listOdd);        list.retainAll(listEven);        assertTrue(list.isEmpty());        listCopy.retainAll(listEven);        assertEquals(listCopy, listEven);        listCopy.retainAll(listOdd);        assertTrue(listCopy.isEmpty());    }    /**     * test the set method     */    public void testSet()    {        ShortList list = new ShortList();        for (short j = 0; j < 1000; j++)        {            list.add(j);        }        for (short j = 0; j < 1001; j++)        {            try            {                list.set(j, ( short ) (j + 1));                if (j == 1000)                {                    fail("Should have gotten exception");                }                assertEquals(j + 1, list.get(j));            }            catch (IndexOutOfBoundsException e)            {                if (j != 1000)                {                    fail("premature exception");                }            }        }    }    /**     * test the size method     */    public void testSize()    {        ShortList list = new ShortList();        for (short j = 0; j < 1000; j++)        {            assertEquals(j, list.size());            list.add(j);            assertEquals(j + 1, list.size());        }        for (short j = 0; j < 1000; j++)        {            assertEquals(1000 - j, list.size());            list.removeValue(j);            assertEquals(999 - j, list.size());        }    }    /**     * test the toArray method     */    public void testToArray()    {        ShortList list = new ShortList();        for (short j = 0; j < 1000; j++)        {            list.add(j);        }        short[] a1 = list.toArray();        assertEquals(a1.length, list.size());        for (short j = 0; j < 1000; j++)        {            assertEquals(a1[ j ], list.get(j));        }        short[] a2 = new short[ list.size() ];        short[] a3 = list.toArray(a2);        assertSame(a2, a3);        for (short j = 0; j < 1000; j++)        {            assertEquals(a2[ j ], list.get(j));        }        short[] aShort = new short[ list.size() - 1 ];        short[] aLong  = new short[ list.size() + 1 ];        short[] a4     = list.toArray(aShort);        short[] a5     = list.toArray(aLong);        assertTrue(a4 != aShort);        assertTrue(a5 != aLong);        assertEquals(a4.length, list.size());        for (short j = 0; j < 1000; j++)        {            assertEquals(a3[ j ], list.get(j));        }        assertEquals(a5.length, list.size());        for (short j = 0; j < 1000; j++)        {            assertEquals(a5[ j ], list.get(j));        }    }    /**     * main method to run the unit tests     *     * @param unused_args     */    public static void main(String [] unused_args)    {        System.out.println("Testing util.ShortList functionality");        junit.textui.TestRunner.run(TestShortList.class);    }}

⌨️ 快捷键说明

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