74 lines
2.0 KiB
C#
74 lines
2.0 KiB
C#
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 TestAdd()
|
|
{
|
|
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));
|
|
}
|
|
}
|
|
}
|
|
} |