Skip to content

Commit

Permalink
Add a test to verify heuristic is called for each vertex scenario (no…
Browse files Browse the repository at this point in the history
…rmal, gray, black).
  • Loading branch information
KeRNeLith committed Jul 25, 2020
1 parent df2f936 commit a76b0ed
Showing 1 changed file with 61 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,66 @@ public void AStar_Throws()

[Test]
public void AStar_HeuristicCalls()
{
var edge01 = new Edge<int>(0, 1);
var edge02 = new Edge<int>(0, 2);
var edge03 = new Edge<int>(0, 3);
var edge14 = new Edge<int>(1, 4);
var edge23 = new Edge<int>(2, 3);
var edge34 = new Edge<int>(3, 4);

var graph = new AdjacencyGraph<int, Edge<int>>();
graph.AddVerticesAndEdgeRange(new[]
{
edge01,
edge02,
edge03,
edge23,
edge14,
edge34
});

const int root = 0;

var colorUpdates = new HashSet<GraphColor>
{
GraphColor.White, GraphColor.Gray, GraphColor.Black
};

int heuristicCalls = 0;
AStarShortestPathAlgorithm<int, Edge<int>> algorithm = null;
Func<int, double> heuristic = v =>
{
// ReSharper disable once PossibleNullReferenceException
// ReSharper disable once AccessToModifiedClosure
colorUpdates.Remove(algorithm.GetVertexColor(v));
++heuristicCalls;
return 10.0 / heuristicCalls;
};

algorithm = new AStarShortestPathAlgorithm<int, Edge<int>>(
graph,
e =>
{
if (e == edge01)
return 8.0;
if (e == edge02)
return 6.0;
if (e == edge03)
return 20.0;
if (e == edge34)
return 5.0;
return 1.0;
},
heuristic);

algorithm.Compute(root);

CollectionAssert.IsEmpty(colorUpdates);
}

[Test]
public void AStar_HeuristicCallCount()
{
var lineGraph = new AdjacencyGraph<int, Edge<int>>();
lineGraph.AddVerticesAndEdgeRange(new[]
Expand All @@ -371,7 +431,7 @@ public void AStar_HeuristicCalls()
new Edge<int>(1, 0)
});

int root = 2;
const int root = 2;

var heuristicCalls = new List<int>();
var algorithm = new AStarShortestPathAlgorithm<int, Edge<int>>(
Expand Down

0 comments on commit a76b0ed

Please sign in to comment.