Add some basic unit tests
This commit is contained in:
parent
eee2cd5be4
commit
263e215f41
@ -1,4 +1,4 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
@ -12,6 +12,7 @@
|
||||
<ItemGroup>
|
||||
<PackageReference Include="coverlet.collector" Version="6.0.0" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
|
||||
<PackageReference Include="NSubstitute" Version="5.3.0" />
|
||||
<PackageReference Include="NUnit" Version="3.14.0" />
|
||||
<PackageReference Include="NUnit.Analyzers" Version="3.9.0" />
|
||||
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
|
||||
|
@ -1,19 +1,74 @@
|
||||
using NSubstitute;
|
||||
using SpatialCollections;
|
||||
using System.Numerics;
|
||||
|
||||
namespace QuadtreeTests
|
||||
{
|
||||
public class Tests
|
||||
{
|
||||
private class TestWorldObject(Vector2 position) : IWorldObject
|
||||
{
|
||||
public Vector2 Position { get; } = position;
|
||||
|
||||
public float BoundingRadius { get; } = 1.0f;
|
||||
|
||||
public BoundingBox2 BoundingBox { get; } = new BoundingBox2(position - Vector2.One, position + Vector2.One);
|
||||
}
|
||||
|
||||
private List<Vector2> _positions;
|
||||
|
||||
private List<IWorldObject> _objects;
|
||||
|
||||
private Quadtree _quadtree;
|
||||
|
||||
[SetUp]
|
||||
public void Setup()
|
||||
{
|
||||
_positions = [new Vector2(1.0f, 1.0f), new Vector2(3.0f, 3.0f), new Vector2(5.0f, 8.0f)];
|
||||
_objects = new();
|
||||
_quadtree = new(20, 4);
|
||||
|
||||
for (int i = 0; i < _positions.Count; i++)
|
||||
{
|
||||
var obj = new TestWorldObject(_positions[i]);
|
||||
_objects.Add(obj);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test1()
|
||||
public void TestAdd()
|
||||
{
|
||||
Quadtree quadtree = new(20, 4);
|
||||
Assert.Pass();
|
||||
AddObjectsAndAssertCount();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestAddRemove()
|
||||
{
|
||||
AddObjectsAndAssertCount();
|
||||
|
||||
for (int i = 0; i < _objects.Count; i++)
|
||||
{
|
||||
_quadtree.Remove(_objects[i]);
|
||||
Assert.That(_quadtree.Count, Is.EqualTo(_objects.Count - i - 1));
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestQuery()
|
||||
{
|
||||
AddObjectsAndAssertCount();
|
||||
List<IWorldObject> result = new();
|
||||
_quadtree.Query(new BoundingBox2(new Vector2(3.5f, 3.5f), new Vector2(10.0f, 10.0f)), result);
|
||||
Assert.That(result.Count, Is.EqualTo(2));
|
||||
}
|
||||
|
||||
private void AddObjectsAndAssertCount()
|
||||
{
|
||||
for (int i = 0; i < _objects.Count; i++)
|
||||
{
|
||||
_quadtree.Add(_objects[i]);
|
||||
Assert.That(_quadtree.Count, Is.EqualTo(i + 1));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user