Initial commit
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
using System.Collections;
|
||||
using BracerLib.Utility;
|
||||
using NUnit.Framework;
|
||||
using UnityEngine;
|
||||
|
||||
namespace BracerLib.Tests.Utility
|
||||
{
|
||||
public class MathUtilityTests : TestBase
|
||||
{
|
||||
private static IEnumerable BoundValues()
|
||||
{
|
||||
yield return new TestCaseData(2, 1, 3, 2).SetName("Value is between bounds");
|
||||
yield return new TestCaseData(0, 1, 3, 3).SetName("Value is below bounds");
|
||||
yield return new TestCaseData(4, 1, 3, 1).SetName("Value is above bounds");
|
||||
}
|
||||
|
||||
private static IEnumerable BetweenValuesInt()
|
||||
{
|
||||
yield return new TestCaseData(0, -1, 1, BoundsInclusivity.None, true).SetName("Value is between bounds");
|
||||
yield return new TestCaseData(-2, -1, 1, BoundsInclusivity.None, false).SetName("Value is below bounds");
|
||||
yield return new TestCaseData(2, -1, 1, BoundsInclusivity.None, false).SetName("Value is above bounds");
|
||||
yield return new TestCaseData(-1, -1, 1, BoundsInclusivity.Both, true).SetName("Value is at bound, inclusive both");
|
||||
yield return new TestCaseData(-1, -1, 1, BoundsInclusivity.Left, true).SetName("Value is at bound, inclusive left");
|
||||
yield return new TestCaseData(1, -1, 1, BoundsInclusivity.Right, true).SetName("Value is at bound, inclusive right");
|
||||
yield return new TestCaseData(1, -1, 1, BoundsInclusivity.Both, true).SetName("Value is at bound, exclusive");
|
||||
}
|
||||
|
||||
private static IEnumerable BetweenValuesFloat()
|
||||
{
|
||||
yield return new TestCaseData(0f, -1f, 1f, BoundsInclusivity.None, true).SetName("Value is between bounds");
|
||||
yield return new TestCaseData(-2f, -1f, 1f, BoundsInclusivity.None, false).SetName("Value is below bounds");
|
||||
yield return new TestCaseData(2f, -1f, 1f, BoundsInclusivity.None, false).SetName("Value is above bounds");
|
||||
yield return new TestCaseData(-1f, -1f, 1f, BoundsInclusivity.Both, true).SetName("Value is at bound, inclusive both");
|
||||
yield return new TestCaseData(-1f, -1f, 1f, BoundsInclusivity.Left, true).SetName("Value is at bound, inclusive left");
|
||||
yield return new TestCaseData(1f, -1f, 1f, BoundsInclusivity.Right, true).SetName("Value is at bound, inclusive right");
|
||||
yield return new TestCaseData(1f, -1f, 1f, BoundsInclusivity.Both, true).SetName("Value is at bound, exclusive");
|
||||
}
|
||||
|
||||
[Test]
|
||||
[TestCaseSource(nameof(BoundValues))]
|
||||
public void ValuesAreWithinBounds(int value, int min, int max, int expected)
|
||||
{
|
||||
var result = MathUtility.KeepWithinBounds(value, min, max);
|
||||
Assert.That(result, Is.EqualTo(expected));
|
||||
}
|
||||
|
||||
[Test]
|
||||
[TestCaseSource(nameof(BetweenValuesInt))]
|
||||
public void ValuesBetweenBounds(int value, int min, int max, BoundsInclusivity inclusivity, bool expected)
|
||||
{
|
||||
var result = MathUtility.IsBetween(value, min, max, inclusivity);
|
||||
Assert.That(result, Is.EqualTo(expected));
|
||||
}
|
||||
|
||||
[Test]
|
||||
[TestCaseSource(nameof(BetweenValuesFloat))]
|
||||
public void ValuesBetweenBoundsFloat(float value, float min, float max, BoundsInclusivity inclusivity, bool expected)
|
||||
{
|
||||
var result = MathUtility.IsBetween(value, min, max, inclusivity);
|
||||
Assert.That(result, Is.EqualTo(expected));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4fcf35728772424c9c2c84e29290ac5e
|
||||
timeCreated: 1780244918
|
||||
@@ -0,0 +1,129 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using BracerLib.Utility;
|
||||
using NUnit.Framework;
|
||||
using UnityEngine;
|
||||
|
||||
namespace BracerLib.Tests.Utility
|
||||
{
|
||||
public class TriangulatorTest : TestBase
|
||||
{
|
||||
private static IEnumerable AreaValueTests()
|
||||
{
|
||||
var points = new List<Vector2>();
|
||||
|
||||
yield return new TestCaseData(points.ToArray(), 0f).SetName("No points to calculate");
|
||||
|
||||
points.AddRange(new[]
|
||||
{
|
||||
Vector2.one,
|
||||
new Vector2(5f, 7f),
|
||||
new Vector2(-2f, 8f)
|
||||
});
|
||||
|
||||
yield return new TestCaseData(points.ToArray(), 23f).SetName("Area of a triangle");
|
||||
|
||||
points.Add(new Vector2(3f, 5f));
|
||||
|
||||
yield return new TestCaseData(points.ToArray(), 10f).SetName("Area of a quad");
|
||||
}
|
||||
|
||||
private static IEnumerable InsideTrianglePointsTest()
|
||||
{
|
||||
yield return new TestCaseData(
|
||||
Vector2.zero,
|
||||
new Vector2(3f, 3f),
|
||||
new Vector2(3f, 0f),
|
||||
new Vector2(2f, 1f),
|
||||
true
|
||||
).SetName("Point inside triangle");
|
||||
|
||||
yield return new TestCaseData(
|
||||
Vector2.zero,
|
||||
new Vector2(3f, 3f),
|
||||
new Vector2(3f, 0f),
|
||||
new Vector2(1f, 0f),
|
||||
true
|
||||
).SetName("Point on the line");
|
||||
|
||||
yield return new TestCaseData(
|
||||
Vector2.zero,
|
||||
new Vector2(3f, 3f),
|
||||
new Vector2(3f, 0f),
|
||||
new Vector2(3f, 0f),
|
||||
false
|
||||
).SetName("Point is one of triangle points");
|
||||
|
||||
yield return new TestCaseData(
|
||||
Vector2.zero,
|
||||
new Vector2(3f, 3f),
|
||||
new Vector2(3f, 0f),
|
||||
new Vector2(-2f, 1f),
|
||||
false
|
||||
).SetName("Point outside triangle");
|
||||
}
|
||||
|
||||
private static IEnumerable TriangulatePointsTest()
|
||||
{
|
||||
yield return new TestCaseData(
|
||||
new[]
|
||||
{
|
||||
Vector2.zero,
|
||||
Vector2.right,
|
||||
Vector2.right + Vector2.up,
|
||||
Vector2.up,
|
||||
Vector2.left + Vector2.up,
|
||||
Vector2.left
|
||||
},
|
||||
new[] { 5,3,2,2,0,5,5,4,3,2,1,0 }
|
||||
).SetName("Positive triangulation");
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[]
|
||||
{
|
||||
Vector2.zero,
|
||||
Vector2.right,
|
||||
Vector2.right + Vector2.down,
|
||||
Vector2.down,
|
||||
Vector2.left + Vector2.down,
|
||||
Vector2.left
|
||||
},
|
||||
new[] { 1,3,4,4,0,1,1,2,3,4,5,0 }
|
||||
).SetName("Negative triangulation");
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] { Vector2.zero, Vector2.right },
|
||||
Array.Empty<int>()
|
||||
).SetName("Empty triangulation");
|
||||
}
|
||||
|
||||
[Test]
|
||||
[TestCaseSource(nameof(AreaValueTests))]
|
||||
public void AreaCalculatesCorrectly(Vector2[] points, float expected)
|
||||
{
|
||||
var result = Triangulator.Area(points);
|
||||
Assert.AreEqual(expected, result);
|
||||
}
|
||||
|
||||
[Test]
|
||||
[TestCaseSource(nameof(InsideTrianglePointsTest))]
|
||||
public void PointsInsideTriangleCorrectly(Vector2 a, Vector2 b, Vector2 c, Vector2 p, bool expected)
|
||||
{
|
||||
var result = Triangulator.InsideTriangle(a, b, c, p);
|
||||
Assert.AreEqual(expected, result);
|
||||
}
|
||||
|
||||
[Test]
|
||||
[TestCaseSource(nameof(TriangulatePointsTest))]
|
||||
public void TriangulatePoints(Vector2[] points, int[] expected)
|
||||
{
|
||||
var result = Triangulator.Triangulate(points);
|
||||
Assert.AreEqual(expected.Length, result.Length);
|
||||
for (var i = 0; i < result.Length; i++)
|
||||
{
|
||||
Assert.AreEqual(expected[i], result[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0a7aaa2ed31747e5ae172f296a2808e3
|
||||
timeCreated: 1686834708
|
||||
Reference in New Issue
Block a user