Skip to content

Commit

Permalink
Write test for AStar to check that the heuristic function is called p…
Browse files Browse the repository at this point in the history
…roperly
  • Loading branch information
tuwuhs committed Jul 22, 2020
1 parent c194ad3 commit df2f936
Showing 1 changed file with 36 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -358,5 +358,41 @@ public void AStar_Throws()
v => 0.0);
Assert.Throws<NegativeWeightException>(() => algorithm.Compute(1));
}

[Test]
public void AStar_HeuristicCalls()
{
var lineGraph = new AdjacencyGraph<int, Edge<int>>();
lineGraph.AddVerticesAndEdgeRange(new[]
{
new Edge<int>(2, 3),
new Edge<int>(3, 4),
new Edge<int>(2, 1),
new Edge<int>(1, 0)
});

int root = 2;

var heuristicCalls = new List<int>();
var algorithm = new AStarShortestPathAlgorithm<int, Edge<int>>(
lineGraph,
e => 1.0,
v =>
{
// Goal is 2, h(v) = v
heuristicCalls.Add(v);
return v;
});

algorithm.Compute(root);

// Heuristic function must be called at least 4 times
Assert.GreaterOrEqual(4, heuristicCalls.Count);

// 0 must be expanded before 4
Assert.Contains(0, heuristicCalls);
Assert.Contains(4, heuristicCalls);
Assert.Less(heuristicCalls.IndexOf(0), heuristicCalls.IndexOf(4));
}
}
}

0 comments on commit df2f936

Please sign in to comment.