|
| 1 | + |
| 2 | +using System.Reflection; |
| 3 | + |
| 4 | +using Algorithms.Search.AStar; |
| 5 | + |
| 6 | +namespace Algorithms.Tests.Search; |
| 7 | + |
| 8 | +public static class AStarTests |
| 9 | +{ |
| 10 | + [Test] |
| 11 | + public static void ResetNodes_ResetsAllNodeProperties() |
| 12 | + { |
| 13 | + var node = new Node(new VecN(0, 0), true, 1.0) |
| 14 | + { |
| 15 | + CurrentCost = 5, |
| 16 | + EstimatedCost = 10, |
| 17 | + Parent = new Node(new VecN(1, 1), true, 1.0), |
| 18 | + State = NodeState.Closed |
| 19 | + }; |
| 20 | + var nodes = new List<Node> { node }; |
| 21 | + |
| 22 | + AStar.ResetNodes(nodes); |
| 23 | + |
| 24 | + node.CurrentCost.Should().Be(0); |
| 25 | + node.EstimatedCost.Should().Be(0); |
| 26 | + node.Parent.Should().BeNull(); |
| 27 | + node.State.Should().Be(NodeState.Unconsidered); |
| 28 | + } |
| 29 | + |
| 30 | + [Test] |
| 31 | + public static void GeneratePath_ReturnsPathFromTargetToRoot() |
| 32 | + { |
| 33 | + var start = new Node(new VecN(0, 0), true, 1.0); |
| 34 | + var mid = new Node(new VecN(1, 0), true, 1.0) { Parent = start }; |
| 35 | + var end = new Node(new VecN(2, 0), true, 1.0) { Parent = mid }; |
| 36 | + |
| 37 | + var path = AStar.GeneratePath(end); |
| 38 | + |
| 39 | + path.Should().HaveCount(3); |
| 40 | + path[0].Should().BeSameAs(start); |
| 41 | + path[1].Should().BeSameAs(mid); |
| 42 | + path[2].Should().BeSameAs(end); |
| 43 | + } |
| 44 | + |
| 45 | + [Test] |
| 46 | + public static void Compute_ReturnsEmptyList_WhenNoPathExists() |
| 47 | + { |
| 48 | + var start = new Node(new VecN(0, 0), true, 1.0); |
| 49 | + var end = new Node(new VecN(1, 0), true, 1.0); |
| 50 | + start.ConnectedNodes = []; |
| 51 | + end.ConnectedNodes = []; |
| 52 | + |
| 53 | + var path = AStar.Compute(start, end); |
| 54 | + |
| 55 | + path.Should().BeEmpty(); |
| 56 | + } |
| 57 | + |
| 58 | + [Test] |
| 59 | + public static void Compute_ReturnsPath_WhenPathExists() |
| 60 | + { |
| 61 | + var start = new Node(new VecN(0, 0), true, 1.0); |
| 62 | + var mid = new Node(new VecN(1, 0), true, 1.0); |
| 63 | + var end = new Node(new VecN(2, 0), true, 1.0); |
| 64 | + |
| 65 | + start.ConnectedNodes = [mid]; |
| 66 | + mid.ConnectedNodes = [end]; |
| 67 | + end.ConnectedNodes = []; |
| 68 | + |
| 69 | + var path = AStar.Compute(start, end); |
| 70 | + |
| 71 | + path.Should().NotBeEmpty(); |
| 72 | + path[0].Should().Be(start); |
| 73 | + path[^1].Should().Be(end); |
| 74 | + } |
| 75 | + |
| 76 | + [Test] |
| 77 | + public static void VecN_Equality_WorksAsExpected() |
| 78 | + { |
| 79 | + var a = new VecN(1, 2); |
| 80 | + var b = new VecN(1, 2); |
| 81 | + var c = new VecN(2, 1); |
| 82 | + |
| 83 | + a.Equals(b).Should().BeTrue(); |
| 84 | + a.Equals(c).Should().BeFalse(); |
| 85 | + } |
| 86 | + |
| 87 | + [Test] |
| 88 | + public static void AddOrUpdateConnected_ThrowsPathfindingException_OnSelfReference() |
| 89 | + { |
| 90 | + var node = new Node(new VecN(0, 0), true, 1.0); |
| 91 | + node.ConnectedNodes = [node]; |
| 92 | + node.State = NodeState.Open; |
| 93 | + |
| 94 | + var queue = new PriorityQueue<Node>(); |
| 95 | + |
| 96 | + Action act = () => { |
| 97 | + // Directly call the private method using reflection, otherwise we can't test this case |
| 98 | + var method = typeof(AStar).GetMethod("AddOrUpdateConnected", BindingFlags.NonPublic | BindingFlags.Static); |
| 99 | + method!.Invoke(null, [node, node, queue]); |
| 100 | + }; |
| 101 | + |
| 102 | + act.Should().Throw<TargetInvocationException>() |
| 103 | + .WithInnerException<PathfindingException>() |
| 104 | + .WithMessage("*same node twice*"); |
| 105 | + } |
| 106 | +} |
0 commit comments