using System.Collections; using BracerLib.Utility; using BracerLib.Utility.Testing; using NUnit.Framework; 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)); } } }