AVLTree JUnit测试用例

Posted CSU迦叶

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了AVLTree JUnit测试用例相关的知识,希望对你有一定的参考价值。

覆盖率如下

总结

(1) remove函数测得很差,还不能理解其业务逻辑

(2) 还没明白异常怎么测(提交的时候不能有failure,但是赛题又有bug,这很矛盾。。)

测试用例如下 

package net.mooctest;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import org.junit.rules.ExpectedException;
import java.lang.NullPointerException;

import org.junit.Rule;
import org.junit.Test;

public class AVLTreeTest 

  @Test(timeout = 4000)
  public void test00() 

	  AVLTree<Integer> tree = new AVLTree<Integer>();
	  assertTrue(tree.isEmpty());
	  AVLTree.AvlNode<Integer> node  = new AVLTree.AvlNode<Integer>(0);
	  tree.root = node;
	  assertFalse(tree.isEmpty());
	  

  
  
  
//  @Rule
//  public ExpectedException thrown = ExpectedException.none();
//  
//  @Test(timeout = 4000)
//  public void testInsertException() 
//	  AVLTree<Integer> tree = new AVLTree<Integer>();
//	  tree.insert(0);
//	  thrown.expect(Exception.class);
//	  thrown.expectMessage("Attempting to insert duplicate value");
	  tree.insert(0);
//  
//  
  
  @Test(timeout = 4000)
  public void testInsertAndSerializeFix() 
	  AVLTree<Integer> tree = new AVLTree<Integer>();
	  tree.insert(0);
	  tree.insert(-1);
	  tree.insert(-2);//触发rotateWithLeftChild
	  assertEquals("-2 -1 0 ",tree.serializeInfix());
	  tree.makeEmpty();
	  tree.insert(0);
	  tree.insert(-2);
	  tree.insert(-1);//触发doubleWithLeftChild
	  assertEquals("-2 -1 0 ",tree.serializeInfix());
	  assertEquals("-1 -2 0 ",tree.serializePrefix());
	  tree.makeEmpty();
	  tree.insert(0);
	  tree.insert(1);
	  tree.insert(2);//触发rotateWithRightChild
	  assertEquals("0 1 2 ",tree.serializeInfix());
	  assertEquals("1 0 2 ",tree.serializePrefix());
	  tree.makeEmpty();
	  tree.insert(0);
	  tree.insert(2);
	  tree.insert(1);//触发doubleWithRightChild
	  assertEquals("0 1 2 ",tree.serializeInfix());
	  assertEquals("1 0 2 ",tree.serializePrefix());
	  assertEquals(12,tree.countInsertions);
	  assertEquals(2,tree.countSingleRotations);
	  assertEquals(2,tree.countDoubleRotations);
  
  
  @Test(timeout=4000)
  public void testFindAndContains() 
	  AVLTree<Integer> tree = new AVLTree<Integer>();
	  assertNull(tree.findMin());
	  assertNull(tree.findMax());
	  assertFalse(tree.contains(0));
	  tree.root = new AVLTree.AvlNode<Integer>(0); 
	  tree.insert(-1);
	  tree.insert(-2);
	  tree.insert(1);
	  tree.insert(2);
	  assertEquals("-2 -1 0 1 2 ",tree.serializeInfix());
	  assertEquals(-2,tree.findMin().intValue());
	  assertEquals(2,tree.findMax().intValue());
	  assertTrue(tree.contains(-2));
	  assertFalse(tree.contains(-4));
	  assertTrue(tree.contains(2));
	  assertFalse(tree.contains(4));
  
  
 
  
  @Test(timeout=4000)
  public void testCheckBalenceAndGetDepth() 
	  AVLTree<Integer> tree = new AVLTree<Integer>();
	  tree.insert(0);
	  assertEquals(1,tree.getDepth(tree.root));
	  assertTrue(tree.checkBalanceOfTree(tree.root));
	  tree.insert(-1);
	  tree.insert(1);
	  assertEquals(2,tree.getDepth(tree.root));
	  assertTrue(tree.checkBalanceOfTree(tree.root));
	  //还需要返回false的CheckBalence结果
	  //情况1 只有根节点不平衡
	  AVLTree.AvlNode<Integer> nodene1 = new AVLTree.AvlNode<Integer>(-1);
	  AVLTree.AvlNode<Integer> node0 = new AVLTree.AvlNode<Integer>(0,nodene1,null);
	  AVLTree.AvlNode<Integer> nodepo1 = new AVLTree.AvlNode<Integer>(1,node0,null);
	  tree.root = nodepo1;
	  assertFalse(tree.checkBalanceOfTree(tree.root));
	  //情况2 左子树不平衡
	  AVLTree.AvlNode<Integer> nodepo2 = new AVLTree.AvlNode<Integer>(2,nodepo1,null);
	  tree.root = nodepo2;
	  assertFalse(tree.checkBalanceOfTree(tree.root));
	  //情况3 右子树不平衡
	  AVLTree.AvlNode<Integer> nodene2 = new AVLTree.AvlNode<Integer>(-2,null,nodepo1);
	  tree.root = nodene2;
	  assertFalse(tree.checkBalanceOfTree(tree.root));
	  assertTrue(tree.checkOrderingOfTree(tree.root));
  
  
  @Test(timeout=4000)
  public void testCheckOrder() 
	  AVLTree<Integer> tree = new AVLTree<Integer>();
	  AVLTree.AvlNode<Integer> nodepo1 = new AVLTree.AvlNode<Integer>(1);
	  AVLTree.AvlNode<Integer> node0 = new AVLTree.AvlNode<Integer>(0,nodepo1,null);
	  tree.root = node0;
	  assertFalse(tree.checkOrderingOfTree(tree.root));
	  tree.root.left=null;
	  AVLTree.AvlNode<Integer> nodene1 = new AVLTree.AvlNode<Integer>(-1);
	  tree.root.right = nodene1;
	  assertFalse(tree.checkOrderingOfTree(tree.root));
	  tree.root.right = null;
	  assertTrue(tree.checkOrderingOfTree(tree.root));
	  tree.root.right = nodepo1;
	  assertTrue(tree.checkOrderingOfTree(tree.root));
  
  
  @Rule
  public ExpectedException thrown2 = ExpectedException.none();
  
  @Test(timeout=4000)
  public void testRemove() 
//	  thrown2.expect(NullPointerException.class);
	  AVLTree<Integer> tree = new AVLTree<Integer>();
	  assertNull(tree.root);
//	  tree.remove(0);
//	  assertNull(tree.root);
	  tree.insert(0);
	  tree.insert(-1);
	  tree.insert(1);
	  tree.insert(2);
	  tree.insert(3);
	  tree.insert(-2);
	  tree.remove(-2);
	  tree.remove(-1);
	  tree.insert(-5);
	  tree.insert(4);
	  tree.insert(-3);
	  tree.remove(-5);
	  tree.insert(-1);
	  tree.remove(4);
	  tree.insert(-2);
	  tree.remove(2);
//	  tree.remove(-2);
	  System.out.println(tree.serializeInfix());
	  System.out.println(tree.serializePrefix());
  
  

赛题链接

以上是关于AVLTree JUnit测试用例的主要内容,如果未能解决你的问题,请参考以下文章

AVLTree(二叉平衡树)底层实现

AVLTree(二叉平衡树)底层实现

AVLTree(二叉平衡树)底层实现

Java怎么进行单元测试?有具体过程和例子吗

学习数据结构笔记(12) --- [平衡二叉搜索树(AVLTREE)]

Junit @Rule如何运作?