Example #1
0
	def test_no_resolve(self):
		'''
			测试无解的特殊情况
		'''
		algo_2 = KnightPath(2)
		self.assertEqual(algo_2.getPath(1,1),None)
		algo_3 = KnightPath(3)
		self.assertEqual(algo_3.getPath(2,2),None)
Example #2
0
class KnightPathTest(unittest.TestCase):

	def setUp(self):
		self.n = random.randint(3,50) #random.randint(3,50);
		self.algo = KnightPath(self.n)
		self.end = Node(self.n,self.n,None,self.n)

	def test_it(self):
		for x in range(1,self.n+1):
			for y in range(1,self.n+1):
				self.check_result(self.algo.getPath(x,y),x,y)
				print 'x=%s,y=%s,OK' % (x,y)

	def test_no_resolve(self):
		'''
			测试无解的特殊情况
		'''
		algo_2 = KnightPath(2)
		self.assertEqual(algo_2.getPath(1,1),None)
		algo_3 = KnightPath(3)
		self.assertEqual(algo_3.getPath(2,2),None)

	def check_result(self,path,x,y):
		l = len(path)
		start = path[0]
		end = path[l-1]
		#首位节点必须正好是起始点
		self.assertEqual(end,self.end)
		self.assertEqual(start,Node(x,y,None,self.n))
		nset = set(path)
		#没有节点重复
		self.assertEqual(len(nset),len(path))
		#没有节点无效
		map(lambda node:self.assertTrue(node.valid()),path)
		l = len(path)
		for i in range(l-1):
			a = path[i]
			b = path[i + 1]
			d = square(a.x - b.x) + square(a.y - b.y)
			#路径间隔的平方距离必须是5
			self.assertEqual(d,5)