-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathtest_tan_network_a_star.py
More file actions
66 lines (50 loc) · 2.46 KB
/
test_tan_network_a_star.py
File metadata and controls
66 lines (50 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import os
import unittest
from typing import Dict, List, Optional, Tuple
from tan_network_a_star import parse_game_input, traverse_with_a_star, generate_solution
def read_game_input_from_file(file_path: str) -> Tuple[str, str, List[str], List[str]]:
with open(file_path) as reader:
start_id = reader.readline().rstrip()
end_id = reader.readline().rstrip()
n = int(reader.readline().rstrip())
input_lines_nodes: List[str] = []
for _ in range(n):
input_lines_nodes.append(reader.readline().rstrip())
m = int(reader.readline().rstrip())
input_lines_edges: List[str] = []
for _ in range(m):
input_lines_edges.append(reader.readline().rstrip())
return start_id, end_id, input_lines_nodes, input_lines_edges
def read_solution_from_file(file_path: str) -> str:
with open(file_path, "r") as reader:
return "".join(reader)
def test_tan_network(file_name: str) -> Tuple[str, str]:
input_file_path = os.path.join("./puzzles/python3/tan-network/test-input", file_name)
start_id, end_id, input_lines_nodes, input_lines_edges = read_game_input_from_file(input_file_path)
graph = parse_game_input(start_id, end_id, input_lines_nodes, input_lines_edges)
came_from: Optional[Dict[str, str]] = traverse_with_a_star(graph)
actual = generate_solution(graph, came_from)
output_file_path = os.path.join("./puzzles/python3/tan-network/solution", file_name)
expected = read_solution_from_file(output_file_path)
return actual, expected
class TestTanNetworkAStar(unittest.TestCase):
def test_example(self):
actual, expected = test_tan_network("example.txt")
self.assertEqual(actual, expected)
def test_one_single_stop(self):
actual, expected = test_tan_network("one-single-stop.txt")
self.assertEqual(actual, expected)
def test_same_start_and_end_points(self):
actual, expected = test_tan_network("same-start-end.txt")
self.assertEqual(actual, expected)
def test_several_stages(self):
actual, expected = test_tan_network("several-stages.txt")
self.assertEqual(actual, expected)
def test_large_nb_stages(self):
actual, expected = test_tan_network("large-nb-stages.txt")
self.assertEqual(actual, expected)
def test_route_impossible(self):
actual, expected = test_tan_network("impossible.txt")
self.assertEqual(actual, expected)
if __name__ == "__main__":
unittest.main()