public void GraphWithDisconnectedNodes_GetShortestPath_ThrowsArgumentException() { GraphNode source = new GraphNode(new Vector2(0f, 0f)); GraphNode destination = new GraphNode(new Vector2(10f, 10f)); Graph disconnectedGraph = new Graph(); disconnectedGraph.AddEdge( new GraphEdge(source, new GraphNode(new Vector2(1f, 1f))) ); disconnectedGraph.AddEdge( new GraphEdge(new GraphNode(new Vector2(5f, 5f)), destination) ); AStarAlgorithm astar = new AStarAlgorithm(disconnectedGraph); // Should throw an argument exception, because it cannot // calculate the shortest path between disconnected nodes. astar.GetShortestPath(source, destination); }
public void ConnectedGraph_GetDepthFirstEnumerable_StartsWithStartNode() { Graph graph = new Graph(); GraphNode start = new GraphNode(new Vector2(0, 0)); GraphNode finish = new GraphNode(new Vector2(1, 1)); graph.AddEdge(new GraphEdge(start, finish)); var enumerator = graph.GetDepthFirstEnumerable(start).GetEnumerator(); Assert.IsTrue(enumerator.Current == start, "Enumerator did not start with the correct node"); }
public void ConnectedGraph_GetShortestPath_ReturnsCorrectPath() { // Create a graph with four nodes, connected in a straight line. Graph graph = new Graph(); const int NodeCount = 4; GraphNode start = new GraphNode(new Vector2(0f, 0f)); GraphNode midFirst = new GraphNode(new Vector2(1f, 1f)); GraphNode midSecond = new GraphNode(new Vector2(2f, 2f)); GraphNode destination = new GraphNode(new Vector2(3f, 3f)); graph.AddEdge(new GraphEdge(start, midFirst)); graph.AddEdge(new GraphEdge(midFirst, midSecond)); graph.AddEdge(new GraphEdge(midSecond, destination)); AStarAlgorithm astar = new AStarAlgorithm(graph); IEnumerable<GraphNode> path = astar.GetShortestPath(start, destination).Path; Assert.IsTrue(path.Count() == NodeCount, "Path has an invalid amount of nodes"); }
private static void CreateEdges(Graph graph, GraphNode[,] nodes, int x, int y, int columns, int rows) { for (int i = x - 1; i < x + 2; i++) { for (int j = y - 1; j < y + 2; j++) { if (i > 0 && i < columns && j > 0 && j < rows && !(i == x && j == y)) { graph.AddEdge(new GraphEdge(nodes[x, y], nodes[i, j])); } } } }
public void ConnectedGraph_GetDepthFirstEnumerable_TraversesCorrectAmountOfNodes() { const int EdgeCount = 4; const int NodeCount = EdgeCount + 1; Graph graph = new Graph(); GraphNode start = new GraphNode(new Vector2()); GraphNode currentSource = start; GraphNode currentDestination; for (int i = 0; i < EdgeCount; i++) { currentDestination = new GraphNode(new Vector2()); graph.AddEdge(new GraphEdge(currentSource, currentDestination)); currentSource = currentDestination; } var enumerator = graph.GetDepthFirstEnumerable(start).GetEnumerator(); int actualCount = 1; while (enumerator.MoveNext()) { actualCount++; } Assert.AreEqual(NodeCount, actualCount, "DeothFirstEnumerator did not traverse the correct amount of nodes."); }