#format python """Unittest for ga""" import unittest from ga import * class TestEncode(unittest.TestCase): def testEncodeTwo(self): path = [1,2] self.assertEqual([1,1], encode(path)) def testEncodeFour(self): path = [4,2,1,3] self.assertEqual([4,2,1,1], encode(path)) class TestDecode(unittest.TestCase): def testDecodeTwo(self): ordinalrep = [1,1] self.assertEqual([1,2], decode(ordinalrep)) def testDecodeFour(self): ordinalrep = [4,2,1,1] self.assertEqual([4,2,1,3], decode(ordinalrep)) class TestMap(unittest.TestCase): def testGetDistance(self): path = [1,2] myMap = Map() distance = myMap.getDistance(path) self.assertEqual(10, distance) def testGetDistanceThree(self): path = [1,2,3] myMap = Map() distance = myMap.getDistance(path) self.assertEqual(19, distance) def testGetDistanceFour(self): path = [5, 3, 2, 4] myMap = Map() distance = myMap.getDistance(path) self.assertEqual(58, distance) class TestCrossOver(unittest.TestCase): def testCrossOver(self): p1 = [1,2,3,4,5,6] p2 = [4,3,2,6,5,1] self.assertEqual(([1,2,3,6,5,4], [4,3,2,1,5,6]), crossover(p1,p2,3)) class TestGlobalOptimum(unittest.TestCase): def testTwoCity(self): citypath = [1,2,3] expacted = [[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1]] self.assertEqual(expacted, getPermutation(citypath)) class TestRealMap(unittest.TestCase): def testNumOfCity(self): myMap = RealMap(3) self.assertEqual(3, len(myMap.getCities())) def testNumOfCitySix(self): myMap = RealMap() self.assertEqual(6, len(myMap.getCities())) def testGetDistanceFour(self): myMap = RealMap(4) myMap.loadMap( {1:(0,10), 2:(0, 20.0), 3:(0, 30), 4:(0,40)}) self.assertEqual(30, myMap.getDistance([1,2,3,4])) if __name__ == '__main__': unittest.main(argv=('', '-v'))