60 lines
1.5 KiB
C#
60 lines
1.5 KiB
C#
using SpatialCollections;
|
|
using System.Numerics;
|
|
|
|
namespace QuadtreeTests
|
|
{
|
|
public class Tests
|
|
{
|
|
private class TestEntity(Vector2 position)
|
|
{
|
|
public Vector2 Position { get; } = position;
|
|
}
|
|
|
|
private List<TestEntity> _entities;
|
|
|
|
private Quadtree<TestEntity> _quadtree;
|
|
|
|
[SetUp]
|
|
public void Setup()
|
|
{
|
|
_entities = [new(new(1.0f, 1.0f)), new(new(3.0f, 3.0f)), new(new(5.0f, 8.0f))];
|
|
_quadtree = new(20, 4, e => e.Position);
|
|
}
|
|
|
|
[Test]
|
|
public void TestAdd()
|
|
{
|
|
AddEntitiesAndAssertCount();
|
|
}
|
|
|
|
[Test]
|
|
public void TestAddRemove()
|
|
{
|
|
AddEntitiesAndAssertCount();
|
|
|
|
for (int i = 0; i < _entities.Count; i++)
|
|
{
|
|
_quadtree.Remove(_entities[i]);
|
|
Assert.That(_quadtree.Count, Is.EqualTo(_entities.Count - i - 1));
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public void TestQuery()
|
|
{
|
|
AddEntitiesAndAssertCount();
|
|
List<TestEntity> result = new();
|
|
_quadtree.Query(new Rectangle(new Vector2(2.5f, 2.5f), new Vector2(10.0f, 10.0f)), result);
|
|
Assert.That(result.Count, Is.EqualTo(2));
|
|
}
|
|
|
|
private void AddEntitiesAndAssertCount()
|
|
{
|
|
for (int i = 0; i < _entities.Count; i++)
|
|
{
|
|
_quadtree.Add(_entities[i]);
|
|
Assert.That(_quadtree.Count, Is.EqualTo(i + 1));
|
|
}
|
|
}
|
|
}
|
|
} |