Oliver Booth 2022-04-20 16:35:44 +01:00
parent 5b5fa42b46
commit 66e7e79561
No known key found for this signature in database
GPG Key ID: 32A00B35503AF634
3 changed files with 13 additions and 12 deletions

View File

@ -1,4 +1,4 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace X10D.Tests.Core
{
@ -22,13 +22,13 @@ namespace X10D.Tests.Core
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, Clusivity.UpperInclusive), "upper.Between(lower, upper, Clusivity.UpperInclusive)");
Assert.IsTrue(upper.Between(lower, upper, Clusivity.Inclusive), "upper.Between(lower, upper, Clusivity.Inclusive)");
Assert.IsFalse(upper.Between(lower, upper, Clusivity.LowerInclusive), "upper.Between(lower, upper, Clusivity.LowerInclusive)");
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, Clusivity.LowerInclusive), "lower.Between(lower, upper, Clusivity.LowerInclusive)");
Assert.IsTrue(lower.Between(lower, upper, Clusivity.Inclusive), "lower.Between(lower, upper, Clusivity.Inclusive)");
Assert.IsFalse(lower.Between(lower, upper, Clusivity.UpperInclusive), "lower.Between(lower, upper, Clusivity.UpperInclusive)");
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)");
}
}
}

View File

@ -16,7 +16,7 @@ public static class ComparableExtensions
/// <param name="value">The value to compare.</param>
/// <param name="lower">The exclusive lower bound.</param>
/// <param name="upper">The exclusive upper bound.</param>
/// <param name="clusivity">The comparison clusivity.</param>
/// <param name="inclusiveOptions">The comparison clusivity.</param>
/// <returns>
/// <see langword="true" /> if <paramref name="value" /> is between the <paramref name="lower" /> and
/// <paramref name="upper" />
@ -44,7 +44,8 @@ public static class ComparableExtensions
/// // True
/// </code>
/// </example>
public static bool Between<T1, T2, T3>(this T1 value, T2 lower, T3 upper, Clusivity clusivity = Clusivity.None)
public static bool Between<T1, T2, T3>(this T1 value, T2 lower, T3 upper,
InclusiveOptions inclusiveOptions = InclusiveOptions.None)
where T1 : IComparable<T2>, IComparable<T3>
where T2 : IComparable<T3>
where T3 : IComparable<T2>
@ -61,11 +62,11 @@ public static class ComparableExtensions
nameof(lower));
}
bool lowerComparison = (clusivity & Clusivity.LowerInclusive) != 0
bool lowerComparison = (inclusiveOptions & InclusiveOptions.LowerInclusive) != 0
? value.CompareTo(lower) >= 0
: value.CompareTo(lower) > 0;
bool upperComparison = (clusivity & Clusivity.UpperInclusive) != 0
bool upperComparison = (inclusiveOptions & InclusiveOptions.UpperInclusive) != 0
? value.CompareTo(upper) <= 0
: value.CompareTo(upper) < 0;

View File

@ -4,7 +4,7 @@
/// Provides options for <see cref="ComparableExtensions.Between{T1, T2, T3}" /> clusivity.
/// </summary>
[Flags]
public enum Clusivity : byte
public enum InclusiveOptions : byte
{
/// <summary>
/// Indicates that the comparison will be exclusive.