1
0
mirror of https://github.com/oliverbooth/X10D synced 2024-11-23 00:38:47 +00:00

Move math-related extensions to Math namespace (#7)

This commit is contained in:
Oliver Booth 2022-04-23 11:14:20 +01:00
parent 7d227ad2a8
commit 224e465915
No known key found for this signature in database
GPG Key ID: 32A00B35503AF634
4 changed files with 204 additions and 133 deletions

View File

@ -1,130 +0,0 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace X10D.Tests.Core;
/// <summary>
/// Tests for <see cref="ComparableExtensions" />.
/// </summary>
[TestClass]
public class ComparableTests
{
/// <summary>
/// Tests <see cref="ComparableExtensions.Between{T1, T2, T3}" />
/// </summary>
[TestMethod]
public void Between()
{
const int lower = 5;
const int upper = 15;
const int value = 10;
Assert.IsTrue(value.Between(lower, upper), "value.Between(lower, upper)");
Assert.IsFalse(lower.Between(value, upper), "lower.Between(value, upper)");
Assert.IsFalse(upper.Between(lower, value), "upper.Between(lower, value)");
Assert.IsTrue(upper.Between(lower, upper, InclusiveOptions.UpperInclusive), "upper.Between(lower, upper, Clusivity.UpperInclusive)");
Assert.IsTrue(upper.Between(lower, upper, InclusiveOptions.Inclusive), "upper.Between(lower, upper, Clusivity.Inclusive)");
Assert.IsFalse(upper.Between(lower, upper, InclusiveOptions.LowerInclusive), "upper.Between(lower, upper, Clusivity.LowerInclusive)");
Assert.IsTrue(lower.Between(lower, upper, InclusiveOptions.LowerInclusive), "lower.Between(lower, upper, Clusivity.LowerInclusive)");
Assert.IsTrue(lower.Between(lower, upper, InclusiveOptions.Inclusive), "lower.Between(lower, upper, Clusivity.Inclusive)");
Assert.IsFalse(lower.Between(lower, upper, InclusiveOptions.UpperInclusive), "lower.Between(lower, upper, Clusivity.UpperInclusive)");
}
/// <summary>
/// Tests <see cref="ComparableExtensions.Clamp{T}" />
/// </summary>
[TestMethod]
public void Clamp()
{
const int lower = 5;
const int upper = 10;
const int value = 15;
Assert.AreEqual(upper, value.Clamp(lower, upper));
Assert.AreEqual(upper, lower.Clamp(upper, value));
Assert.AreEqual(upper, upper.Clamp(lower, value));
}
/// <summary>
/// Tests <see cref="ComparableExtensions.GreaterThan{T1, T2}" />
/// </summary>
[TestMethod]
public void GreaterThan()
{
const int first = 5;
const int second = 10;
Assert.IsTrue(second.GreaterThan(first));
Assert.IsFalse(first.GreaterThan(second));
}
/// <summary>
/// Tests <see cref="ComparableExtensions.GreaterThanOrEqualTo{T1, T2}" />
/// </summary>
[TestMethod]
public void GreaterThanOrEqualTo()
{
const int first = 5;
const int second = 10;
Assert.IsTrue(second.GreaterThanOrEqualTo(first));
Assert.IsTrue(second.GreaterThanOrEqualTo(second));
Assert.IsTrue(first.GreaterThanOrEqualTo(first));
Assert.IsFalse(first.GreaterThanOrEqualTo(second));
}
/// <summary>
/// Tests <see cref="ComparableExtensions.GreaterThan{T1, T2}" />
/// </summary>
[TestMethod]
public void LessThan()
{
const int first = 5;
const int second = 10;
Assert.IsTrue(first.LessThan(second));
Assert.IsFalse(second.LessThan(first));
}
/// <summary>
/// Tests <see cref="ComparableExtensions.LessThanOrEqualTo{T1, T2}" />
/// </summary>
[TestMethod]
public void LessThanOrEqualTo()
{
const int first = 5;
const int second = 10;
Assert.IsTrue(first.LessThanOrEqualTo(second));
Assert.IsTrue(first.LessThanOrEqualTo(first));
Assert.IsTrue(second.LessThanOrEqualTo(second));
Assert.IsFalse(second.LessThanOrEqualTo(first));
}
/// <summary>
/// Tests <see cref="ComparableExtensions.Max{T}" />
/// </summary>
[TestMethod]
public void Max()
{
const int first = 5;
const int second = 10;
Assert.AreEqual(second, first.Max(second));
Assert.AreEqual(second, second.Max(first));
}
/// <summary>
/// Tests <see cref="ComparableExtensions.Min{T}" />
/// </summary>
[TestMethod]
public void Min()
{
const int first = 5;
const int second = 10;
Assert.AreEqual(first, first.Min(second));
Assert.AreEqual(first, second.Min(first));
}
}

View File

@ -0,0 +1,201 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using X10D.Math;
namespace X10D.Tests.Math;
[TestClass]
public class ComparableTests
{
private class ComparableTestClass : IComparable<ComparableTestClass>
{
public int CompareTo(ComparableTestClass? other)
{
return 0;
}
}
private readonly int _lower = 1;
private readonly int _upper = 10;
private readonly int _value = 5;
[TestMethod]
public void Between_5_1_10_ShouldBeTrue()
{
Assert.IsTrue(_value.Between(_lower, _upper));
}
[TestMethod]
public void Between_1_1_10_ShouldBeFalse()
{
// default option is exclusive
Assert.IsFalse(_lower.Between(_lower, _upper));
}
[TestMethod]
public void Between_1_1_10_Inclusive_ShouldBeTrue()
{
Assert.IsTrue(_lower.Between(_lower, _upper, InclusiveOptions.Inclusive));
Assert.IsTrue(_lower.Between(_lower, _upper, InclusiveOptions.LowerInclusive));
Assert.IsFalse(_lower.Between(_lower, _upper, InclusiveOptions.UpperInclusive));
}
[TestMethod]
public void Between_10_1_10_ShouldBeFalse()
{
// default option is exclusive
Assert.IsFalse(_upper.Between(_lower, _upper));
}
[TestMethod]
public void Between_10_1_10_Inclusive_ShouldBeTrue()
{
Assert.IsTrue(_upper.Between(_lower, _upper, InclusiveOptions.Inclusive));
Assert.IsTrue(_upper.Between(_lower, _upper, InclusiveOptions.UpperInclusive));
Assert.IsFalse(_upper.Between(_lower, _upper, InclusiveOptions.LowerInclusive));
}
[TestMethod]
public void Between_1_10_5_ShouldThrow()
{
Assert.ThrowsException<ArgumentException>(() => _lower.Between(_upper, _value));
}
[TestMethod]
public void Between_Null_ShouldThrow()
{
ComparableTestClass? nullPointer = null;
Assert.ThrowsException<ArgumentNullException>(() => ((ComparableTestClass?)null)!.Between(nullPointer!, nullPointer!));
}
[TestMethod]
public void Clamp_3_1_5_ShouldBe3()
{
Assert.AreEqual(3, 3.Clamp(1, 5));
}
[TestMethod]
public void Clamp_10_1_5_ShouldBe5()
{
Assert.AreEqual(5, 10.Clamp(1, 5));
}
[TestMethod]
public void Clamp_n_6_5_ShouldThrow()
{
Assert.ThrowsException<ArgumentException>(() => 0.Clamp(6, 5));
}
[TestMethod]
public void GreaterThan_5_6_ShouldBeFalse()
{
Assert.IsFalse(5.GreaterThan(6));
}
[TestMethod]
public void GreaterThan_6_5_ShouldBeTrue()
{
Assert.IsTrue(6.GreaterThan(5));
}
[TestMethod]
public void GreaterThan_5_5_ShouldBeFalse()
{
Assert.IsFalse(5.LessThan(5));
}
[TestMethod]
public void GreaterThan_Null_ShouldThrow()
{
ComparableTestClass? nullPointer = null;
Assert.ThrowsException<ArgumentNullException>(() => nullPointer!.GreaterThan(nullPointer!));
}
[TestMethod]
public void GreaterThanOrEqualTo_5_5_ShouldBeTrue()
{
Assert.IsTrue(5.GreaterThanOrEqualTo(5));
}
[TestMethod]
public void GreaterThanOrEqualTo_6_5_ShouldBeTrue()
{
Assert.IsTrue(6.GreaterThanOrEqualTo(5));
}
[TestMethod]
public void GreaterThanOrEqualTo_5_6_ShouldBeFalse()
{
Assert.IsFalse(5.GreaterThanOrEqualTo(6));
}
[TestMethod]
public void GreaterThanOrEqualTo_Null_ShouldThrow()
{
ComparableTestClass? nullPointer = null;
Assert.ThrowsException<ArgumentNullException>(() => nullPointer!.GreaterThanOrEqualTo(nullPointer!));
}
[TestMethod]
public void LessThan_Null_ShouldThrow()
{
ComparableTestClass? nullPointer = null;
Assert.ThrowsException<ArgumentNullException>(() => nullPointer!.LessThan(nullPointer!));
}
[TestMethod]
public void LessThan_6_5_ShouldBeFalse()
{
Assert.IsFalse(6.LessThan(5));
}
[TestMethod]
public void LessThan_5_6_ShouldBeTrue()
{
Assert.IsTrue(5.LessThan(6));
}
[TestMethod]
public void LessThan_5_5_ShouldBeFalse()
{
Assert.IsFalse(5.LessThan(5));
}
[TestMethod]
public void LessThanOrEqualTo_5_5_ShouldBeTrue()
{
Assert.IsTrue(5.LessThanOrEqualTo(5));
}
[TestMethod]
public void LessThanOrEqualTo_5_6_ShouldBeTrue()
{
Assert.IsTrue(5.LessThanOrEqualTo(6));
}
[TestMethod]
public void LessThanOrEqualTo_6_5_ShouldBeFalse()
{
Assert.IsFalse(6.LessThanOrEqualTo(5));
}
[TestMethod]
public void LessThanOrEqualTo_Null_ShouldThrow()
{
ComparableTestClass? nullPointer = null;
Assert.ThrowsException<ArgumentNullException>(() => nullPointer!.LessThanOrEqualTo(nullPointer!));
}
[TestMethod]
public void Max_Null_ShouldThrow()
{
ComparableTestClass? nullPointer = null;
Assert.ThrowsException<ArgumentNullException>(() => nullPointer!.Max(nullPointer!));
}
[TestMethod]
public void Min_Null_ShouldThrow()
{
ComparableTestClass? nullPointer = null;
Assert.ThrowsException<ArgumentNullException>(() => nullPointer!.Min(nullPointer!));
}
}

View File

@ -1,6 +1,6 @@
using System.Globalization; using System.Globalization;
namespace X10D; namespace X10D.Math;
/// <summary> /// <summary>
/// Extension methods for <see cref="IComparable{T}" />. /// Extension methods for <see cref="IComparable{T}" />.

View File

@ -1,7 +1,7 @@
namespace X10D; namespace X10D.Math;
/// <summary> /// <summary>
/// Provides options for <see cref="ComparableExtensions.Between{T1, T2, T3}" /> clusivity. /// Provides options for <see cref="ComparableExtensions.Between{T1,T2,T3}" /> clusivity.
/// </summary> /// </summary>
[Flags] [Flags]
public enum InclusiveOptions : byte public enum InclusiveOptions : byte