MoonTools.Bonk/Test/SpatialHashTest.cs

117 lines
4.9 KiB
C#
Raw Normal View History

using FluentAssertions;
2019-09-06 08:11:58 +00:00
using NUnit.Framework;
using MoonTools.Core.Structs;
using MoonTools.Core.Bonk;
2019-10-31 23:19:30 +00:00
using System.Numerics;
2020-01-02 09:40:03 +00:00
using System.Collections.Immutable;
2019-09-06 08:11:58 +00:00
namespace Tests
{
public class SpatialHashTest
{
[Test]
public void InsertAndRetrieve()
{
var spatialHash = new SpatialHash<int>(16);
2020-01-02 09:40:03 +00:00
var rectA = new Rectangle(4, 4);
2019-09-06 08:11:58 +00:00
var rectATransform = new Transform2D(new Vector2(-8, -8));
2020-01-02 09:40:03 +00:00
var rectB = new Rectangle(4, 4);
2019-09-06 08:11:58 +00:00
var rectBTransform = new Transform2D(new Vector2(8, 8));
2020-01-02 09:40:03 +00:00
var rectC = new Rectangle(4, 4);
2019-09-06 08:11:58 +00:00
var rectCTransform = new Transform2D(new Vector2(24, -4));
2020-01-02 09:40:03 +00:00
var rectD = new Rectangle(4, 4);
2019-09-06 08:11:58 +00:00
var rectDTransform = new Transform2D(new Vector2(24, 24));
var circleA = new Circle(2);
2019-09-06 08:11:58 +00:00
var circleATransform = new Transform2D(new Vector2(24, -8));
var circleB = new Circle(8);
2019-09-06 08:11:58 +00:00
var circleBTransform = new Transform2D(new Vector2(16, 16));
var line = new Line(new Position2D(20, -4), new Position2D(22, -12));
2019-09-06 08:11:58 +00:00
var lineTransform = new Transform2D(new Vector2(0, 0));
var point = new Point();
var pointTransform = new Transform2D(new Position2D(8, 8));
2019-12-02 06:21:50 +00:00
2020-01-05 00:13:07 +00:00
var multiRectangle = new MultiShape(
ImmutableArray.Create<(IShape2D, Transform2D)>(
2020-01-02 09:40:03 +00:00
(new Rectangle(4, 1), new Transform2D(new Position2D(-2, -2))),
(new Rectangle(4, 1), new Transform2D(new Position2D(-2, -1))),
(new Rectangle(4, 1), new Transform2D(new Position2D(-2, 0)))
)
);
var multiRectangleTransform = new Transform2D(new Position2D(8, 8));
2019-09-06 08:11:58 +00:00
spatialHash.Insert(0, rectA, rectATransform);
spatialHash.Insert(1, rectB, rectBTransform);
spatialHash.Insert(2, rectC, rectCTransform);
spatialHash.Insert(3, rectD, rectDTransform);
spatialHash.Insert(4, circleA, circleATransform);
2020-01-02 09:40:03 +00:00
spatialHash.Insert(9, circleB, circleBTransform);
2019-09-06 08:11:58 +00:00
spatialHash.Insert(6, line, lineTransform);
2019-12-02 06:21:50 +00:00
spatialHash.Insert(7, point, pointTransform);
2020-01-02 09:40:03 +00:00
spatialHash.Insert(8, multiRectangle, multiRectangleTransform);
2019-09-06 08:11:58 +00:00
spatialHash.Retrieve(0, rectA, rectATransform).Should().BeEmpty();
spatialHash.Retrieve(1, rectB, rectBTransform).Should().NotContain((1, circleB, circleBTransform));
2019-12-02 06:21:50 +00:00
spatialHash.Retrieve(1, rectB, rectBTransform).Should().Contain((7, point, pointTransform));
2020-01-02 09:40:03 +00:00
spatialHash.Retrieve(1, rectB, rectBTransform).Should().Contain((8, multiRectangle, multiRectangleTransform));
2019-09-06 08:11:58 +00:00
spatialHash.Retrieve(2, rectC, rectCTransform).Should().Contain((6, line, lineTransform)).And.Contain((4, circleA, circleATransform));
2020-01-02 09:40:03 +00:00
spatialHash.Retrieve(3, rectD, rectDTransform).Should().Contain((9, circleB, circleBTransform));
2019-09-06 08:11:58 +00:00
spatialHash.Retrieve(4, circleA, circleATransform).Should().Contain((6, line, lineTransform)).And.Contain((2, rectC, rectCTransform));
spatialHash.Retrieve(1, circleB, circleBTransform).Should().NotContain((1, rectB, rectBTransform)).And.Contain((3, rectD, rectDTransform));
spatialHash.Retrieve(6, line, lineTransform).Should().Contain((4, circleA, circleATransform)).And.Contain((2, rectC, rectCTransform));
2020-01-02 09:40:03 +00:00
spatialHash.Retrieve(8, multiRectangle, multiRectangleTransform).Should().Contain((1, rectB, rectBTransform));
2020-01-05 00:13:07 +00:00
spatialHash.Retrieve(8, multiRectangle, multiRectangleTransform).Should().NotContain((0, rectA, rectATransform));
2019-09-06 08:11:58 +00:00
}
[Test]
public void InsertAndRetrieveSameValues()
{
var spatialHash = new SpatialHash<int>(16);
2020-01-02 09:40:03 +00:00
var rectA = new Rectangle(4, 4);
var rectATransform = new Transform2D(new Vector2(-8, -8));
2020-01-02 09:40:03 +00:00
var rectB = new Rectangle(4, 4);
var rectBTransform = new Transform2D(new Vector2(-8, -8));
2020-01-02 09:40:03 +00:00
var rectC = new Rectangle(2, 2);
var rectCTransform = new Transform2D(new Vector2(-8, -8));
spatialHash.Insert(0, rectA, rectATransform);
spatialHash.Insert(1, rectB, rectBTransform);
spatialHash.Insert(2, rectC, rectCTransform);
spatialHash.Retrieve(2, rectC, rectCTransform).Should().HaveCount(2);
}
2019-09-06 08:11:58 +00:00
[Test]
public void Clear()
{
var spatialHash = new SpatialHash<int>(16);
2020-01-02 09:40:03 +00:00
var rectA = new Rectangle(4, 4);
2019-09-06 08:11:58 +00:00
var rectATransform = new Transform2D(new Vector2(-8, -8));
2020-01-02 09:40:03 +00:00
var rectB = new Rectangle(4, 4);
2019-09-06 08:11:58 +00:00
var rectBTransform = new Transform2D(new Vector2(8, 8));
spatialHash.Insert(0, rectA, rectATransform);
spatialHash.Insert(1, rectB, rectBTransform);
spatialHash.Clear();
spatialHash.Retrieve(0, rectA, rectATransform).Should().HaveCount(0);
}
}
}