1 """Unittest for ga"""
2 import unittest
3 from ga import *
4
5 class TestEncode(unittest.TestCase):
6 def testEncodeTwo(self):
7 path = [1,2]
8 self.assertEqual([1,1], encode(path))
9
10 def testEncodeFour(self):
11 path = [4,2,1,3]
12 self.assertEqual([4,2,1,1], encode(path))
13
14 class TestDecode(unittest.TestCase):
15 def testDecodeTwo(self):
16 ordinalrep = [1,1]
17 self.assertEqual([1,2], decode(ordinalrep))
18
19 def testDecodeFour(self):
20 ordinalrep = [4,2,1,1]
21 self.assertEqual([4,2,1,3], decode(ordinalrep))
22
23 class TestMap(unittest.TestCase):
24 def testGetDistance(self):
25 path = [1,2]
26 myMap = Map()
27 distance = myMap.getDistance(path)
28 self.assertEqual(10, distance)
29
30 def testGetDistanceThree(self):
31 path = [1,2,3]
32 myMap = Map()
33 distance = myMap.getDistance(path)
34 self.assertEqual(19, distance)
35
36 def testGetDistanceFour(self):
37 path = [5, 3, 2, 4]
38 myMap = Map()
39 distance = myMap.getDistance(path)
40 self.assertEqual(58, distance)
41
42 class TestCrossOver(unittest.TestCase):
43 def testCrossOver(self):
44 p1 = [1,2,3,4,5,6]
45 p2 = [4,3,2,6,5,1]
46 self.assertEqual(([1,2,3,6,5,4], [4,3,2,1,5,6]), crossover(p1,p2,3))
47
48 class TestGlobalOptimum(unittest.TestCase):
49 def testTwoCity(self):
50 citypath = [1,2,3]
51 expacted = [[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1]]
52 self.assertEqual(expacted, getPermutation(citypath))
53
54 class TestRealMap(unittest.TestCase):
55 def testNumOfCity(self):
56 myMap = RealMap(3)
57 self.assertEqual(3, len(myMap.getCities()))
58
59 def testNumOfCitySix(self):
60 myMap = RealMap()
61 self.assertEqual(6, len(myMap.getCities()))
62
63 def testGetDistanceFour(self):
64 myMap = RealMap(4)
65 myMap.loadMap(
66 {1:(0,10), 2:(0, 20.0), 3:(0, 30), 4:(0,40)})
67 self.assertEqual(30, myMap.getDistance([1,2,3,4]))
68
69 if __name__ == '__main__':
70 unittest.main(argv=('', '-v'))