Final
This commit is contained in:
72
GridCastingTests/ExecutorTests.cs
Normal file
72
GridCastingTests/ExecutorTests.cs
Normal file
@@ -0,0 +1,72 @@
|
||||
using GridCasting.Executor;
|
||||
using GridCasting.Models.GridGraph;
|
||||
using Path = GridCasting.Models.Path;
|
||||
|
||||
namespace GridCastingTests;
|
||||
|
||||
public class ExecutorTests
|
||||
{
|
||||
private GridGraph _graph;
|
||||
private static readonly Path _path = new(0, 1, 1, 2, 3);
|
||||
[SetUp]
|
||||
public void Setup()
|
||||
{
|
||||
// Create a simple hex grid
|
||||
_graph = new GridGraph();
|
||||
var node = new GridGraphNode();
|
||||
for (var i = 0; i < 6; i++)
|
||||
node.Edges.Add(new GridGraphEdge(node, node, MathF.PI * i / 3, 1));
|
||||
_graph.Nodes.Add(node);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestNoCommands()
|
||||
{
|
||||
var executor = new PathExecutor(_graph, []);
|
||||
Assert.That(executor.Execute(_path), Is.False, "Executor should not execute without commands");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestWrongCommand()
|
||||
{
|
||||
var executor = new PathExecutor(_graph, []);
|
||||
executor.AddCommand(new TestPassedCommand(), _path);
|
||||
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(executor.Execute(new Path(0, 1, 2, 1)), Is.False, "Executor should not execute with wrong command");
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestCommandExecution()
|
||||
{
|
||||
var executor = new PathExecutor(_graph, []);
|
||||
executor.AddCommand(new TestPassedCommand(), _path);
|
||||
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(executor.Execute(_path), Is.True, "Executor should execute with commands");
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestCommandFamily()
|
||||
{
|
||||
var executor = new PathExecutor(_graph, []);
|
||||
executor.AddCommand(new TestPassedCommand(), new Path(0, 1, 1, 2), true);
|
||||
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(executor.Execute(_path), Is.True, "Executor should execute with commands");
|
||||
});
|
||||
}
|
||||
|
||||
public class TestPassedCommand : ICommand
|
||||
{
|
||||
public void Execute(CommandContext context)
|
||||
{
|
||||
Assert.That(context.Command, Is.EqualTo(_path), "Command executed successfully");
|
||||
}
|
||||
}
|
||||
}
|
||||
28
GridCastingTests/GridCastingTests.csproj
Normal file
28
GridCastingTests/GridCastingTests.csproj
Normal file
@@ -0,0 +1,28 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
|
||||
<IsPackable>false</IsPackable>
|
||||
<IsTestProject>true</IsTestProject>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="coverlet.collector" Version="6.0.0"/>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0"/>
|
||||
<PackageReference Include="NUnit" Version="3.14.0"/>
|
||||
<PackageReference Include="NUnit.Analyzers" Version="3.9.0"/>
|
||||
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0"/>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Using Include="NUnit.Framework"/>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\GridCasting\GridCasting.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
127
GridCastingTests/GridResolverTests.cs
Normal file
127
GridCastingTests/GridResolverTests.cs
Normal file
@@ -0,0 +1,127 @@
|
||||
using GridCasting.Models.GridGraph;
|
||||
using GridCasting.Transform;
|
||||
using IgdrasilEngine.Engine.Math.Boxes;
|
||||
using IgdrasilEngine.Engine.Math.Vectors;
|
||||
|
||||
namespace GridCastingTests;
|
||||
|
||||
public class GridResolverTests
|
||||
{
|
||||
private GridResolver _resolver;
|
||||
private static readonly int[] Expected = [0, 2, 1];
|
||||
|
||||
[SetUp]
|
||||
public void Setup()
|
||||
{
|
||||
// Create a simple hex grid
|
||||
var graph = new GridGraph();
|
||||
var node = new GridGraphNode();
|
||||
for (var i = 0; i < 6; i++)
|
||||
node.Edges.Add(new GridGraphEdge(node, node, MathF.PI * i / 3, 1));
|
||||
graph.Nodes.Add(node);
|
||||
|
||||
_resolver = new GridResolver(
|
||||
graph,
|
||||
0.1f
|
||||
);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestWorkingPath()
|
||||
{
|
||||
IVector3[] points = [
|
||||
new(7, 2, 5),
|
||||
new(7, 2, 6),
|
||||
new(7, 3, 6)
|
||||
];
|
||||
var positions = points.Select(p =>
|
||||
{
|
||||
var result = FVector2.Zero;
|
||||
for (var i = 0; i < 3; i++)
|
||||
{
|
||||
var angle = MathF.PI * i / 3;
|
||||
var value = i switch
|
||||
{
|
||||
0 => p.X,
|
||||
1 => p.Y,
|
||||
2 => p.Z,
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(i))
|
||||
};
|
||||
result += new FVector2(
|
||||
value * MathF.Cos(angle),
|
||||
value * MathF.Sin(angle)
|
||||
);
|
||||
}
|
||||
return result;
|
||||
}).ToArray();
|
||||
var path = _resolver.GetPath(positions);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(path.HasValue, Is.True, "Path is null");
|
||||
Assert.That(path.Value, Is.EqualTo(Expected), "Path is incorrect");
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestRippedPath()
|
||||
{
|
||||
IVector3[] points = [
|
||||
new(7, 2, 5),
|
||||
new(7, 2, 6),
|
||||
new(7, 4, 6)
|
||||
];
|
||||
var positions = points.Select(p =>
|
||||
{
|
||||
var result = FVector2.Zero;
|
||||
for (var i = 0; i < 3; i++)
|
||||
{
|
||||
var angle = MathF.PI * i / 3;
|
||||
var value = i switch
|
||||
{
|
||||
0 => p.X,
|
||||
1 => p.Y,
|
||||
2 => p.Z,
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(i))
|
||||
};
|
||||
result += new FVector2(
|
||||
value * MathF.Cos(angle),
|
||||
value * MathF.Sin(angle)
|
||||
);
|
||||
}
|
||||
return result;
|
||||
}).ToArray();
|
||||
var path = _resolver.GetPath(positions);
|
||||
Assert.That(path.HasValue, Is.False, "Path is not null");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestEmptyPath()
|
||||
{
|
||||
FVector2[] positions = [];
|
||||
var path = _resolver.GetPath(positions);
|
||||
Assert.That(path.HasValue, Is.False, "Path is not null");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestGrid()
|
||||
{
|
||||
var positions = _resolver.GetGridPositions(new FBox2(FVector2.One * -5, FVector2.One * 5));
|
||||
var invSqrt3 = 1f / MathF.Sqrt(3);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
foreach (var pos in positions)
|
||||
{
|
||||
var x = float.Abs(pos.X - invSqrt3 * pos.Y);
|
||||
var y = float.Abs(2 * invSqrt3 * pos.Y);
|
||||
x -= (int)x;
|
||||
y -= (int)y;
|
||||
if (x > 0.5) x -= 1;
|
||||
if (y > 0.5) y -= 1;
|
||||
|
||||
Assert.That(x, Is.EqualTo(0).Within(0.001f), $"Position {pos} is not on the grid");
|
||||
Assert.That(y, Is.EqualTo(0).Within(0.001f), $"Position {pos} is not on the grid");
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user