-
Notifications
You must be signed in to change notification settings - Fork 21.1k
Expand file tree
/
Copy pathAStarSearchTest.java
More file actions
116 lines (86 loc) · 3.37 KB
/
AStarSearchTest.java
File metadata and controls
116 lines (86 loc) · 3.37 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package com.thealgorithms.others;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
/**
* Test cases for AStarSearch algorithm.
*/
class AStarSearchTest {
/**
* Simple heuristic that returns 0 (reduces A* to Dijkstra).
*/
private static final AStarSearch.Heuristic ZERO_HEURISTIC = (node, goal) -> 0;
private static Map<AStarSearch.Node, List<AStarSearch.Edge>> graph;
private static AStarSearch.Node nodeA;
private static AStarSearch.Node nodeB;
private static AStarSearch.Node nodeC;
private static AStarSearch.Node nodeD;
@BeforeAll
static void setUp() {
graph = new HashMap<>();
nodeA = new AStarSearch.Node("A");
nodeB = new AStarSearch.Node("B");
nodeC = new AStarSearch.Node("C");
nodeD = new AStarSearch.Node("D");
graph.put(nodeA, Arrays.asList(new AStarSearch.Edge(nodeB, 1), new AStarSearch.Edge(nodeC, 4)));
graph.put(nodeB, Arrays.asList(new AStarSearch.Edge(nodeC, 2), new AStarSearch.Edge(nodeD, 5)));
graph.put(nodeC, Arrays.asList(new AStarSearch.Edge(nodeD, 1)));
graph.put(nodeD, Collections.emptyList());
}
@Test
void testPathExists() {
List<AStarSearch.Node> path = AStarSearch.findPath(graph, nodeA, nodeD, ZERO_HEURISTIC);
// Expected shortest path: A -> B -> C -> D
List<AStarSearch.Node> expected = Arrays.asList(nodeA, nodeB, nodeC, nodeD);
assertEquals(expected, path);
}
@Test
void testDirectPath() {
List<AStarSearch.Node> path = AStarSearch.findPath(graph, nodeA, nodeB, ZERO_HEURISTIC);
List<AStarSearch.Node> expected = Arrays.asList(nodeA, nodeB);
assertEquals(expected, path);
}
@Test
void testStartEqualsGoal() {
List<AStarSearch.Node> path = AStarSearch.findPath(graph, nodeA, nodeA, ZERO_HEURISTIC);
List<AStarSearch.Node> expected = Collections.singletonList(nodeA);
assertEquals(expected, path);
}
@Test
void testNoPathExists() {
AStarSearch.Node nodeE = new AStarSearch.Node("nodeE");
List<AStarSearch.Node> path = AStarSearch.findPath(graph, nodeE, nodeA, ZERO_HEURISTIC);
assertTrue(path.isEmpty());
}
@Test
void testPathCostOptimality() {
List<AStarSearch.Node> path = AStarSearch.findPath(graph, nodeA, nodeD, ZERO_HEURISTIC);
// Calculate total cost
double cost = calculatePathCost(path);
// Expected shortest cost = 1 (A->B) + 2 (B->C) + 1 (C->D) = 4
assertEquals(4.0, cost);
}
/**
* Utility method to calculate path cost.
*/
private double calculatePathCost(List<AStarSearch.Node> path) {
double total = 0;
for (int i = 0; i < path.size() - 1; i++) {
AStarSearch.Node current = path.get(i);
AStarSearch.Node next = path.get(i + 1);
for (AStarSearch.Edge edge : graph.getOrDefault(current, Collections.emptyList())) {
if (edge.target.equals(next)) {
total += edge.cost;
break;
}
}
}
return total;
}
}