Pairing Heap JUnit测试用例

Posted CSU迦叶

tags:

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

覆盖率

收获

1. 一个写断言的经验是,对于一个复杂的方法,没必要知道它到底想干什么,写测试用例的不过是关注if后面的条件,知道怎么样达到那个条件,进入那个分支就行。至于进入分支以后会如何,可以不懂的,只需要输出,看一下结果,然后把结果用做断言的条件。(PS : 这只是CST考试的思路,默认题目就是正确的,要是实际测试还默认被测都是正确的还测啥。。)

2. 每道题都会不止一个java文件,但是这些文件之间的关系有主有从,我们只需要测试主的java文件,最后对从文件没覆盖到的地方进行补漏即可。展开说说所谓的主和从,如果类A继承类B,A使用C,那么很大可能是测试A就把B和C给一块测了,所以把A称为测试的主类。

代码

package net.mooctest;

import static org.junit.Assert.*;

import org.junit.Test;

public class Pairing_HeapTest 

	private Pairing_Heap<Integer> ph,h;
	
	@Test(timeout=4000)
	public void test00() 
		Heap_Item<Integer> item0 = new Heap_Item<Integer>(0);
		ph = new Pairing_Heap<Integer>(item0);
		Pairing_Heap<Integer> ph1 = new Pairing_Heap<Integer>(ph);
		assertSame(item0,ph1.getRoot());
		assertEquals(0,ph1.peek().intValue());
		Heap_Item<Integer> item2 = new Heap_Item<Integer>(2);
		Heap_Item<Integer> item4 = new Heap_Item<Integer>(4);
		ph.push(item2);
		assertSame(item2,ph.getRoot().getSonByData(2));
		ph.push(item4);
		assertSame(item4,ph.getRoot().getSonByData(4));
		
		
		assertEquals(0,ph1.pop().intValue());
		
		
	
	
	@Test(timeout=10000,expected=NullPointerException.class)
	public void test01() 
		h = new Pairing_Heap<Integer>();
		assertNull(h.pop());
		
		Heap_Item<Integer> itemNe4 = new Heap_Item<Integer>(-4);
		Heap_Item<Integer> itemNe2 = new Heap_Item<Integer>(-2);
		itemNe4.setRightSon(itemNe2);
		assertTrue(itemNe4.hasRightSon());
		assertFalse(itemNe4.hasLeftSon());
		itemNe2.setAncestor(itemNe4);
		
		h.push(itemNe4);
		assertNull(ph);
		ph = new Pairing_Heap<Integer>(new Heap_Item<Integer>(0));
		ph.push(new Heap_Item<Integer>(2));
		ph.push(new Heap_Item<Integer>(4));
		assertNotNull(h.getRoot());
		assertNotNull(ph.getRoot());
		ph.push(h.getRoot());
		ph.checkPriority(ph.getRoot());
		ph.checkPriority(ph.getRoot().getLeftSon());
		assertEquals(-4,ph.getRoot().getData().intValue());
		assertEquals(0,ph.getRoot().getLeftSon().getData().intValue());
		assertEquals(-2,ph.getRoot().getRightSon().getData().intValue());
		assertEquals(4,ph.getRoot().getLeftSon().getLeftSon().getData().intValue());
		ph.checkPriority(ph.getRoot());
		assertFalse(ph.getRoot().replaceChild(3, new Heap_Item<Integer>(3)));
		assertFalse(ph.getRoot().replaceChild(4, new Heap_Item<Integer>(-5)));
		ph.checkPriority(ph.getRoot());
	
	
	@Test(timeout=10000,expected=NullPointerException.class)
	public void test02() 
		Heap_Item<Integer> item2 = new Heap_Item<Integer>(2);
		Heap_Item<Integer> item0 = new Heap_Item<Integer>(0);
		Heap_Item<Integer> item1 = new Heap_Item<Integer>(1);
		item2.setLeftSon(item0);
		item0.setAncestor(item2);
		item0.setRightSon(item1);
		item1.setAncestor(item0);
		Pairing_Heap<Integer> tree = new Pairing_Heap<Integer>(item2);
		tree.checkPriority(item0);
		assertEquals(2,tree.getRoot().getLeftSon().getData().intValue());
		assertEquals(1,tree.getRoot().getLeftSon().getRightSon().getData().intValue());
		assertSame(item0,item2.getAncestor());
		assertSame(item2,item0.getLeftSon());
		assertSame(item2,item1.getAncestor());
		assertSame(item1,item2.getRightSon());
		Heap_Item<Integer> itemNe1 = new Heap_Item<Integer>();
		itemNe1.setData(-1);
		assertEquals(-1,itemNe1.getData().intValue());
		item2.setLeftSon(itemNe1);
		itemNe1.setAncestor(item2);
		tree.checkPriority(item2);
		assertEquals(-1,item0.getLeftSon().getData().intValue());
		assertEquals(2,itemNe1.getLeftSon().getData().intValue());
		assertEquals(1,itemNe1.getRightSon().getData().intValue());
		tree.checkPriority(item0);
		assertEquals(-1,tree.getRoot().getData().intValue());
		assertEquals(0,itemNe1.getLeftSon().getData().intValue());
		assertEquals(1,item0.getRightSon().getData().intValue());
		assertNull(item0.getLeftSon());
		assertEquals(0,itemNe1.getLeftSon().getData().intValue());
		assertFalse(itemNe1.hasRightSon());
		Heap_Item<Integer> itemCopy = new Heap_Item<Integer>(item0);
		assertSame(itemNe1,itemCopy.getAncestor());
		assertSame(item1,itemCopy.getRightSon());
		item1.removeChild(2);
		item1.setRightSon(item2);
		assertFalse(item1.hasLeftSon());
		assertTrue(item1.hasRightSon());
		assertSame(itemNe1,item2.getMultiWayAncestor());
		itemNe1.removeChild(0);
		itemNe1.setRightSon(item0);
		assertSame(item0,itemNe1.getSonByData(0));
		assertNull(itemNe1.getSonByData(10));
		Heap_Item<Integer> item10 = new Heap_Item<Integer>(10);
		assertTrue(itemNe1.replaceChild(0, item10));
		assertSame(item10,itemNe1.getRightSon());
	

	@Test(timeout=10000,expected=NullPointerException.class)
	public void test03() 
		Heap_Item<Integer> item2 = new Heap_Item<Integer>(2);
		Heap_Item<Integer> item0 = new Heap_Item<Integer>(0);
		Pairing_Heap<Integer> tr = new Pairing_Heap<Integer>();
		tr.push(item0);
		tr.push(item2);
		System.out.println(tr.getRoot().getData().intValue());
		tr.pop();
		assertSame(item2,tr.getRoot());
		
	


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

如何在 Xcode 中运行单个测试用例?

Spring boot 不同数据库环境的集成测试

python-pytest学习-标记失败xfail

python-pytest学习-标记失败xfail

为 grails 运行 test-app -coverage 命令时出现错误,即使所有测试用例都通过了

我们需要为 Login 编写 UI 测试用例吗? [关闭]