diff --git a/X10D.Tests/src/Core/ComparableTests.cs b/X10D.Tests/src/Core/ComparableTests.cs index 5c7570e..72dadf6 100644 --- a/X10D.Tests/src/Core/ComparableTests.cs +++ b/X10D.Tests/src/Core/ComparableTests.cs @@ -9,14 +9,26 @@ namespace X10D.Tests.Core public class ComparableTests { /// - /// Tests for . + /// Tests /// [TestMethod] public void Between() { - Assert.IsTrue(5.Between(2, 7)); - Assert.IsTrue(10.Between(9, 11)); - Assert.IsFalse(100.Between(80, 99)); + 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, 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(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)"); } } } diff --git a/X10D/src/ComparableExtensions/Clusivity.cs b/X10D/src/ComparableExtensions/Clusivity.cs new file mode 100644 index 0000000..8ea5b8a --- /dev/null +++ b/X10D/src/ComparableExtensions/Clusivity.cs @@ -0,0 +1,28 @@ +namespace X10D; + +/// +/// Provides options for clusivity. +/// +[Flags] +public enum Clusivity : byte +{ + /// + /// Indicates that the comparison will be exclusive. + /// + Exclusive, + + /// + /// Indicates that the comparison will treat the upper bound as exclusive. + /// + UpperInclusive, + + /// + /// Indicates that the comparison will treat the lower bound as exclusive. + /// + LowerInclusive, + + /// + /// Indicates that the comparison will treat both the upper and lower bound as exclusive. + /// + Inclusive +} diff --git a/X10D/src/ComparableExtensions/ComparableExtensions.cs b/X10D/src/ComparableExtensions/ComparableExtensions.cs index 3aa1650..d10052f 100644 --- a/X10D/src/ComparableExtensions/ComparableExtensions.cs +++ b/X10D/src/ComparableExtensions/ComparableExtensions.cs @@ -16,6 +16,7 @@ namespace X10D /// The value to compare. /// The exclusive lower bound. /// The exclusive upper bound. + /// The comparison clusivity. /// /// if is between the and /// @@ -43,7 +44,7 @@ namespace X10D /// // True /// /// - public static bool Between(this T1 value, T2 lower, T3 upper) + public static bool Between(this T1 value, T2 lower, T3 upper, Clusivity clusivity = Clusivity.Exclusive) where T1 : IComparable, IComparable where T2 : IComparable where T3 : IComparable @@ -60,7 +61,15 @@ namespace X10D nameof(lower)); } - return value.CompareTo(lower) > 0 && value.CompareTo(upper) < 0; + bool lowerComparison = (clusivity & Clusivity.LowerInclusive) != 0 + ? value.CompareTo(lower) >= 0 + : value.CompareTo(lower) > 0; + + bool upperComparison = (clusivity & Clusivity.UpperInclusive) != 0 + ? value.CompareTo(upper) <= 0 + : value.CompareTo(upper) < 0; + + return lowerComparison && upperComparison; } ///