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

📄 testbinarytree.java

📁 java 读写word excel ppt
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
/* ====================================================================   Licensed to the Apache Software Foundation (ASF) under one or more   contributor license agreements.  See the NOTICE file distributed with   this work for additional information regarding copyright ownership.   The ASF licenses this file to You under the Apache License, Version 2.0   (the "License"); you may not use this file except in compliance with   the License.  You may obtain a copy of the License at       http://www.apache.org/licenses/LICENSE-2.0   Unless required by applicable law or agreed to in writing, software   distributed under the License is distributed on an "AS IS" BASIS,   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   See the License for the specific language governing permissions and   limitations under the License.==================================================================== */        package org.apache.poi.util;import junit.framework.*;import java.util.*;/** * Class TestBinaryTree * * @author Marc Johnson (mjohnson at apache dot org) */public class TestBinaryTree    extends TestCase{    /**     * constructor     *     * @param name     */    public TestBinaryTree(final String name)    {        super(name);    }    /**     * test size() method     */    public void testSize()    {        Map m = new BinaryTree();        assertEquals(0, m.size());        LocalTestNode nodes[] = makeLocalNodes();        for (int k = 0; k < nodes.length; k++)        {            m.put(nodes[ k ].getKey(), nodes[ k ].getValue());            assertEquals(k + 1, m.size());        }        int count = m.size();        for (int k = 0; k < nodes.length; k++)        {            m.remove(nodes[ k ].getKey());            --count;            assertEquals(count, m.size());            // failed remove should not affect size            m.remove(nodes[ k ].getKey());            assertEquals(count, m.size());        }    }    /**     * test IsEmpty() method     */    public void testIsEmpty()    {        Map m = new BinaryTree();        assertTrue(m.isEmpty());        LocalTestNode nodes[] = makeLocalNodes();        for (int k = 0; k < nodes.length; k++)        {            m.put(nodes[ k ].getKey(), nodes[ k ].getValue());            assertTrue(!m.isEmpty());        }        int count = m.size();        for (int k = 0; k < nodes.length; k++)        {            m.remove(nodes[ k ].getKey());            --count;            if (count == 0)            {                assertTrue(m.isEmpty());            }            else            {                assertTrue(!m.isEmpty());            }            // failed remove should not affect emptiness            m.remove(nodes[ k ].getKey());            if (count == 0)            {                assertTrue(m.isEmpty());            }            else            {                assertTrue(!m.isEmpty());            }        }    }    /**     * test containsKey() method     */    public void testContainsKey()    {        Map m = new BinaryTree();        try        {            m.containsKey(new Object());            fail("should have caught ClassCastException");        }        catch (ClassCastException ignored)        {        }        try        {            m.containsKey(null);            fail("should have caught NullPointerException");        }        catch (NullPointerException ignored)        {        }        assertTrue(!m.containsKey("foo"));        LocalTestNode nodes[] = makeLocalNodes();        for (int k = 0; k < nodes.length; k++)        {            m.put(nodes[ k ].getKey(), nodes[ k ]);            assertTrue(m.containsKey(nodes[ k ].getKey()));        }        assertTrue(!m.containsKey(new Integer(-1)));        try        {            m.containsKey("foo");            fail("Should have caught ClassCastException");        }        catch (ClassCastException ignored)        {        }        for (int k = 0; k < nodes.length; k++)        {            m.remove(nodes[ k ].getKey());            assertTrue(!m.containsKey(nodes[ k ].getKey()));        }    }    /**     * test containsValue() method     */    public void testContainsValue()    {        Map           m       = new BinaryTree();        LocalTestNode nodes[] = makeLocalNodes();        for (int k = 0; k < nodes.length; k++)        {            m.put(nodes[ k ].getKey(), nodes[ k ]);            assertTrue(m.containsValue(nodes[ k ]));        }        for (int k = 0; k < nodes.length; k++)        {            m.remove(nodes[ k ].getKey());            assertTrue(!m.containsValue(nodes[ k ]));        }    }    /**     * test get() method     */    public void testGet()    {        Map m = new BinaryTree();        try        {            m.get(new Object());            fail("should have caught ClassCastException");        }        catch (ClassCastException ignored)        {        }        try        {            m.get(null);            fail("should have caught NullPointerException");        }        catch (NullPointerException ignored)        {        }        assertNull(m.get("foo"));        LocalTestNode nodes[] = makeLocalNodes();        for (int k = 0; k < nodes.length; k++)        {            m.put(nodes[ k ].getKey(), nodes[ k ]);            assertSame(m.get(nodes[ k ].getKey()), nodes[ k ]);        }        assertNull(m.get(new Integer(-1)));        try        {            m.get("foo");            fail("Should have caught ClassCastException");        }        catch (ClassCastException ignored)        {        }        for (int k = 0; k < nodes.length; k++)        {            assertNotNull(m.get(nodes[ k ].getKey()));            m.remove(nodes[ k ].getKey());            assertNull(m.get(nodes[ k ].getKey()));        }    }    /**     * test put() method     */    public void testPut()    {        Map m = new BinaryTree();        try        {            m.put(new Object(), "foo");            fail("should have caught ClassCastException");        }        catch (ClassCastException ignored)        {        }        try        {            m.put(null, "foo");            fail("should have caught NullPointerException");        }        catch (NullPointerException ignored)        {        }        try        {            m.put("foo", null);            fail("should have caught NullPointerException");        }        catch (NullPointerException ignored)        {        }        try        {            m.put("foo", new Object());            fail("should have caught ClassCastException");        }        catch (ClassCastException ignored)        {        }        LocalTestNode[] nodes = makeLocalNodes();        for (int k = 0; k < nodes.length; k++)        {            assertNull(m.put(nodes[ k ].getKey(), nodes[ k ].getValue()));            try            {                m.put(nodes[ k ].getKey(), "foo");            }            catch (IllegalArgumentException ignored)            {            }        }    }    /**     * test remove() method     */    public void testRemove()    {        BinaryTree    m       = new BinaryTree();        LocalTestNode nodes[] = makeLocalNodes();        for (int k = 0; k < nodes.length; k++)        {            m.put(nodes[ k ].getKey(), nodes[ k ]);        }        try        {            m.remove(null);            fail("should have caught NullPointerException");        }        catch (NullPointerException ignored)        {        }        try        {            m.remove(new Object());            fail("should have caught ClassCastException");        }        catch (ClassCastException ignored)        {        }        assertNull(m.remove(new Integer(-1)));        try        {            m.remove("foo");            fail("should have caught ClassCastException");        }        catch (ClassCastException ignored)        {        }        for (int k = 0; k < nodes.length; k += 2)        {            Comparable key = nodes[ k ].getKey();            assertNotNull(m.get(key));            assertSame(nodes[ k ], m.remove(key));            assertNull(m.remove(key));            assertNull(m.get(key));        }        for (int k = 1; k < nodes.length; k += 2)        {            Comparable key = nodes[ k ].getKey();            assertNotNull(m.get(key));            assertSame(nodes[ k ], m.remove(key));            assertNull(m.remove(key));            assertNull(m.get(key));        }        assertTrue(m.isEmpty());    }    /**     * Method testPutAll     */    public void testPutAll()    {        Map           m       = new BinaryTree();        LocalTestNode nodes[] = makeLocalNodes();        for (int k = 0; k < nodes.length; k++)        {            m.put(nodes[ k ].getKey(), nodes[ k ]);        }        Map m1 = new HashMap();        m1.put(null, "foo");        try        {            m.putAll(m1);            fail("Should have caught NullPointerException");        }        catch (NullPointerException ignored)        {        }        m1 = new HashMap();        m1.put(new Object(), "bar");        try        {            m.putAll(m1);            fail("Should have caught ClassCastException");        }        catch (ClassCastException ignored)        {        }        m1 = new HashMap();        m1.put("fubar", null);        try        {            m.putAll(m1);            fail("Should have caught NullPointerException");        }        catch (NullPointerException ignored)        {        }        m1 = new HashMap();        m1.put("fubar", new Object());        try        {            m.putAll(m1);            fail("Should have caught ClassCastException");        }        catch (ClassCastException ignored)        {        }        assertEquals(nodes.length, m.size());        m  = new BinaryTree();        m1 = new HashMap();        for (int k = 0; k < nodes.length; k++)        {            m1.put(nodes[ k ].getKey(), nodes[ k ].getValue());        }        m.putAll(m1);        assertEquals(nodes.length, m.size());        for (int k = 0; k < nodes.length; k++)        {            assertSame(nodes[ k ].getValue(), m.get(nodes[ k ].getKey()));        }    }    /**     * test clear() method     */    public void testClear()    {        Map           m       = new BinaryTree();        LocalTestNode nodes[] = makeLocalNodes();        for (int k = 0; k < nodes.length; k++)        {            m.put(nodes[ k ].getKey(), nodes[ k ].getValue());            assertTrue(!m.isEmpty());        }        assertTrue(!m.isEmpty());        for (int k = 0; k < nodes.length; k++)        {            assertTrue(m.containsKey(nodes[ k ].getKey()));            assertTrue(m.containsValue(nodes[ k ].getValue()));        }        m.clear();        assertTrue(m.isEmpty());        for (int k = 0; k < nodes.length; k++)        {            assertTrue(!m.containsKey(nodes[ k ].getKey()));            assertTrue(!m.containsValue(nodes[ k ].getValue()));        }    }    /**     * test keySet() method     */    public void testKeySet()    {        testKeySet(new BinaryTree());        Map           m       = new BinaryTree();        LocalTestNode nodes[] = makeLocalNodes();        for (int k = 0; k < nodes.length; k++)        {            m.put(nodes[ k ].getKey(), nodes[ k ]);        }        testKeySet(m);        m = new BinaryTree();        for (int k = 0; k < nodes.length; k++)        {            m.put(nodes[ k ].getKey(), nodes[ k ]);        }        int count = m.size();        for (Iterator iter = m.keySet().iterator(); iter.hasNext(); )        {            iter.next();            iter.remove();            --count;            assertEquals(count, m.size());

⌨️ 快捷键说明

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