📄 testbinarytree.java
字号:
/* ==================================================================== * The Apache Software License, Version 1.1 * * Copyright (c) 2003 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Apache" and "Apache Software Foundation" and * "Apache POI" must not be used to endorse or promote products * derived from this software without prior written permission. For * written permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache", * "Apache POI", nor may "Apache" appear in their name, without * prior written permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. */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());
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -