EightQueens JUnit测试用例

Posted CSU迦叶

tags:

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

慕测得分

总结

本题的Node是整个搜索空间。

遗传得分越高,说明当前的棋盘皇后之间的冲突越多。

爬山算法和模拟退火算法都是在解空间内寻找遗传得分更小的棋盘状态,但不保证找到为0的最优解。

缺陷

我感觉我断言写少了。

代码

package net.mooctest;

import static org.junit.Assert.*;

import java.util.ArrayList;

import org.junit.Test;

public class NodeTest 

	@Test(timeout=4000)
	public void test0() 
		Queen[] startBoard = new Queen[] 
				new Queen(0,0),
				new Queen(1,2),
				new Queen(2,1),
				new Queen(3,3),
				new Queen(4,5),
				new Queen(5,4),
				new Queen(6,7),
				new Queen(7,6)
		;
		Node startNode = new Node();
		startNode.setState(startBoard);
		
		RandomRestart rr = new RandomRestart(startBoard);
		rr.setStartNode(startNode);
		Node copyStart = rr.getStartNode();
		assertEquals("(0, 0)",copyStart.getState()[0].toString());
		assertSame(startNode,copyStart);
		
		Node bestNode = rr.randomRestart();
		assertEquals(0,bestNode.getHeuristic());
		Node anyone = bestNode.getRandomNeighbour(bestNode);
		assertTrue(bestNode.compareTo(anyone)<0);
		
		
	
	
	@Test(timeout=4000)
	public void test1() 
		Node node1 = new Node();
		Node node2 = new Node();
		
		Queen[] state1 = new Queen[] 
				new Queen(0,0),
				new Queen(1,1),
				new Queen(2,2),
				new Queen(3,3),
				new Queen(4,4),
				new Queen(5,5),
				new Queen(6,6),
				new Queen(7,7)
		;
		
		Queen[] state2 = new Queen[] 
				new Queen(0,0),
				new Queen(1,2),
				new Queen(2,1),
				new Queen(3,3),
				new Queen(4,5),
				new Queen(5,4),
				new Queen(6,7),
				new Queen(7,6)
		;
		
		node1.setState(state1);
		node2.setState(state2);
		node1.computeHeuristic();
		node2.computeHeuristic();
		assertEquals(28,node1.getHeuristic());
		assertEquals(10,node2.getHeuristic());
		
		System.out.println(node1.toString());
		System.out.println(node2.toString());
		
		ArrayList<Node> nbs1 = new ArrayList<Node>();
		nbs1  = node1.generateNeighbours(node1);
		
		ArrayList<Node> nbs2 = new ArrayList<Node>();
		nbs2  = node2.generateNeighbours(node2);
		
	
	
	@Test(timeout=4000)
	public void test2() 
		Queen q =  new Queen(1,1);
		q.setColumn(2);
		q.setRow(2);
		assertEquals(2,q.getColumn());
		assertEquals(2,q.getRow());
		assertEquals("(2, 2)",q.toString());
		
	
	
	@Test(timeout=4000)
	public void test3() 
		Queen[] startState = new Queen[] 
				new Queen(0,0),
				new Queen(1,1),
				new Queen(2,2),
				new Queen(3,3),
				new Queen(4,4),
				new Queen(5,5),
				new Queen(6,6),
				new Queen(7,7)
		;
		
		SimulatedAnnealing sa = new SimulatedAnnealing(startState);
		Node node  = new Node();
		node.setState(startState);
		node.computeHeuristic();
		assertEquals(node.getHeuristic(),sa.getStartNode().getHeuristic());
		
		Node generated = sa.simulatedAnneal(1,0.1);
		assertTrue(generated.compareTo(node)<0);
		int generatedNum = sa.getNodesGenerated();
		System.out.println(generatedNum);
		System.out.println(generated.toString());
		assertTrue(generatedNum>0);
		
		sa.startState();
		generated = sa.simulatedAnneal(1,0.01);
		assertTrue(sa.getNodesGenerated()>generatedNum);
		
	
	
	@Test(timeout=4000)
	public void test4()
		EightQueens eq  = new EightQueens();
		Queen[] oneState = eq.generateBoard();
		HillClimbing hc = new HillClimbing(oneState);
		assertEquals(0,hc.getNodesGenerated());
		hc.hillClimbing();
		assertNotEquals(0,hc.getNodesGenerated());
	


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

在Maven项目中运行JUnit 5测试用例

20165306 实验二 Java面向对象程序设计

StreamSupport 的 Junit 测试用例

junit4.9测试用例 spring测试用例 Assert 注解

JUnit 测试用例中“失败”的实际用途是啥?

如何使 JUnit 测试用例按顺序运行?