public static void TestRangeCreate() { int?value1 = 1; int?value2 = 2; int?empty = null; var key = "Hello!"; DoesNotThrow(() => Range.Create(value1, value2)); DoesNotThrow(() => Range.Create(value1, value1)); DoesNotThrow(() => Range.Create(empty, value2)); DoesNotThrow(() => Range.Create(empty, empty)); DoesNotThrow(() => Range.CreateExclusiveFrom(value1, value2)); Throws <ArgumentException>(() => Range.CreateExclusiveFrom(value1, value1)); DoesNotThrow(() => Range.CreateExclusiveFrom(empty, value2)); DoesNotThrow(() => Range.CreateExclusiveFrom(empty, empty)); DoesNotThrow(() => Range.CreateExclusiveTo(value1, value2, key)); Throws <ArgumentException>(() => Range.CreateExclusiveTo(value2, value2, key)); DoesNotThrow(() => Range.CreateExclusiveTo(empty, value2, key)); DoesNotThrow(() => Range.CreateExclusiveTo(empty, empty, key)); DoesNotThrow(() => Range.CreateExclusive(value1, value2, key)); Throws <ArgumentException>(() => Range.CreateExclusive(value2, value2, key)); DoesNotThrow(() => Range.CreateExclusive(empty, value2, key)); DoesNotThrow(() => Range.CreateExclusive(empty, empty, key)); Throws <ArgumentException>(() => Range.Create(value2, value1, key)); Throws <ArgumentException>(() => Range.CreateExclusiveFrom(value2, value1)); Throws <ArgumentException>(() => Range.CreateExclusiveTo(value2, value1, key)); Throws <ArgumentException>(() => Range.CreateExclusive(value2, value1, key)); Throws <ArgumentException>(() => Range.Create(RangeBoundaryFrom <int?> .Empty, Range.BoundaryTo(value2), key)); Throws <ArgumentException>(() => Range.Create(Range.BoundaryFrom(empty), RangeBoundaryTo <int?> .Empty, key)); Throws <ArgumentException>(() => Range.Create(double.NegativeInfinity, double.NegativeInfinity, key)); Throws <ArgumentException>(() => Range.Create(double.PositiveInfinity, double.PositiveInfinity, key)); Throws <ArgumentException>(() => Range.Create(double.PositiveInfinity, double.NegativeInfinity, key)); Throws <ArgumentException>(() => Range.Create(double.PositiveInfinity, 2, key)); Throws <ArgumentException>(() => Range.Create(1, double.NegativeInfinity, key)); AreEqual( Range <int?> .Empty, new Range <int?>(RangeBoundaryFrom <int?> .Empty, RangeBoundaryTo <int?> .Empty)); AreEqual( Range <int?> .Infinite, Range.Create(empty, empty)); AreEqual( Range <double> .Infinite, Range.Create(double.NegativeInfinity, double.PositiveInfinity)); AreNotEqual( Range <int?> .Infinite, Range.Create(empty, empty, key)); AreEqual( Range.TryCreate(value2, value1), Range <int?> .Empty); IsTrue(Range.TryCreate(value2, value1, key).IsEmpty); IsFalse(Range.TryCreate(value1, value2, key).IsEmpty); IsTrue(Range.TryCreate(double.NegativeInfinity, double.NegativeInfinity, key).IsEmpty); }
public static Range <T2, TKey> WithValues <T, T2, TKey>( this Range <T, TKey> range, [NotNull, InstantHandle] Func <T, T2> newValueSelector) { var from = range.From.WithValue(newValueSelector); var to = range.To.WithValue(newValueSelector); return(Range.TryCreate(from, to, range.Key)); }
public static Range <T2> WithValues <T, T2>( this Range <T> range, [NotNull, InstantHandle] Func <T, T2> fromValueSelector, [NotNull, InstantHandle] Func <T, T2> toValueSelector) { var from = range.From.WithValue(fromValueSelector); var to = range.To.WithValue(toValueSelector); return(Range.TryCreate(from, to)); }
public static void TestRangeToString() { int?value1 = 1; int?empty = null; var key = "Hello!"; AreEqual(Range <int> .Empty.ToString(CultureInfo.InvariantCulture), "∅"); AreEqual(Range <int> .Infinite.ToString(CultureInfo.InvariantCulture), "(-∞..+∞)"); AreEqual(Range.Create(1, 1).ToString(CultureInfo.InvariantCulture), "[1..1]"); AreEqual(Range.TryCreate(1, 1).ToString(CultureInfo.InvariantCulture), "[1..1]"); AreEqual(Range.Create(1, 2).ToString(CultureInfo.InvariantCulture), "[1..2]"); AreEqual(Range.TryCreate(1, 2).ToString(CultureInfo.InvariantCulture), "[1..2]"); AreEqual(Range.CreateExclusive(1, 2).ToString(CultureInfo.InvariantCulture), "(1..2)"); AreEqual(Range.TryCreateExclusive(1, 2).ToString(CultureInfo.InvariantCulture), "(1..2)"); AreEqual(Range.CreateExclusiveFrom(1, 2).ToString(CultureInfo.InvariantCulture), "(1..2]"); AreEqual(Range.TryCreateExclusiveFrom(1, 2).ToString(CultureInfo.InvariantCulture), "(1..2]"); AreEqual(Range.CreateExclusiveTo(1, 2).ToString(CultureInfo.InvariantCulture), "[1..2)"); AreEqual(Range.TryCreateExclusiveTo(1, 2).ToString(CultureInfo.InvariantCulture), "[1..2)"); AreEqual(Range.CreateExclusive(value1, empty).ToString("000", CultureInfo.InvariantCulture), "(001..+∞)"); AreEqual(Range.TryCreateExclusive(value1, empty).ToString("000", CultureInfo.InvariantCulture), "(001..+∞)"); AreEqual(Range.Create(RangeBoundaryFrom <int?> .Empty, RangeBoundaryTo <int?> .Empty, key).ToString(CultureInfo.InvariantCulture), "'Hello!':∅"); AreEqual(Range.TryCreate(RangeBoundaryFrom <int?> .Empty, RangeBoundaryTo <int?> .Empty, key).ToString(CultureInfo.InvariantCulture), "'Hello!':∅"); AreEqual(Range.Create(empty, empty, key).ToString(CultureInfo.InvariantCulture), "'Hello!':(-∞..+∞)"); AreEqual(Range.TryCreate(empty, empty, key).ToString(CultureInfo.InvariantCulture), "'Hello!':(-∞..+∞)"); AreEqual(Range.Create(empty, empty, (string?)null).ToString(CultureInfo.InvariantCulture), "'':(-∞..+∞)"); AreEqual(Range.TryCreate(empty, empty, (string?)null).ToString(CultureInfo.InvariantCulture), "'':(-∞..+∞)"); AreEqual(Range.Create(1, 1, key).ToString(CultureInfo.InvariantCulture), "'Hello!':[1..1]"); AreEqual(Range.TryCreate(1, 1, key).ToString(CultureInfo.InvariantCulture), "'Hello!':[1..1]"); AreEqual(Range.Create(1, 2, key).ToString(CultureInfo.InvariantCulture), "'Hello!':[1..2]"); AreEqual(Range.TryCreate(1, 2, key).ToString(CultureInfo.InvariantCulture), "'Hello!':[1..2]"); AreEqual(Range.CreateExclusive(1, 2, key).ToString(CultureInfo.InvariantCulture), "'Hello!':(1..2)"); AreEqual(Range.TryCreateExclusive(1, 2, key).ToString(CultureInfo.InvariantCulture), "'Hello!':(1..2)"); AreEqual(Range.CreateExclusiveFrom(1, 2, key).ToString(CultureInfo.InvariantCulture), "'Hello!':(1..2]"); AreEqual(Range.TryCreateExclusiveFrom(1, 2, key).ToString(CultureInfo.InvariantCulture), "'Hello!':(1..2]"); AreEqual(Range.CreateExclusiveTo(1, 2, key).ToString(CultureInfo.InvariantCulture), "'Hello!':[1..2)"); AreEqual(Range.TryCreateExclusiveTo(1, 2, key).ToString(CultureInfo.InvariantCulture), "'Hello!':[1..2)"); AreEqual(Range.CreateExclusive(value1, empty, 3).ToString("000", CultureInfo.InvariantCulture), "'3':(001..+∞)"); AreEqual(Range.TryCreateExclusive(value1, empty, 3).ToString("000", CultureInfo.InvariantCulture), "'3':(001..+∞)"); AreEqual(Range.Create((int?)1, null, key).ToString(CultureInfo.InvariantCulture), "'Hello!':[1..+∞)"); AreEqual(Range.TryCreate((int?)1, null, key).ToString(CultureInfo.InvariantCulture), "'Hello!':[1..+∞)"); var cultureRu = new CultureInfo("ru-RU"); var cultureEn = new CultureInfo("en-US"); AreEqual(Range.Create(1.5, 2.5, 1.1).ToString("000.000", cultureRu), "'1,1':[001,500..002,500]"); AreEqual(Range.TryCreate(1.5, 2.5, 1.1).ToString("000.000", cultureRu), "'1,1':[001,500..002,500]"); AreEqual(Range.Create(1.5, 2.5, 1.1).ToString("000.000", cultureEn), "'1.1':[001.500..002.500]"); AreEqual(Range.TryCreate(1.5, 2.5, 1.1).ToString("000.000", cultureEn), "'1.1':[001.500..002.500]"); AreEqual(Range.Create(1.5, 2.5, (string?)null).ToString(null, cultureRu), "'':[1,5..2,5]"); AreEqual(Range.TryCreate(1.5, 2.5, (string?)null).ToString(null, cultureRu), "'':[1,5..2,5]"); AreEqual(Range.Create(1.5, 2.5, (string?)null).ToString(null, cultureEn), "'':[1.5..2.5]"); AreEqual(Range.TryCreate(1.5, 2.5, (string?)null).ToString(null, cultureEn), "'':[1.5..2.5]"); }
public static void TestRangeEquality() { int?value1 = 1; int?value2 = 2; int?value3 = 3; int?empty = null; var key = "Hello!"; var key2 = "Hello2!"; var eFrom = new RangeBoundaryFrom <int?>(); var eTo = new RangeBoundaryTo <int?>(); AreEqual(Range <int?> .Empty, Range.Create(eFrom, eTo)); AreEqual(Range <int?> .Infinite, Range.Create(empty, empty)); AreNotEqual(Range <double?> .Infinite, Range.Create(empty, empty)); AreEqual( Range.CreateExclusiveFrom(value1, value2).From, Range.BoundaryFromExclusive(value1)); AreEqual( Range.CreateExclusiveFrom(value1, value2).To, Range.BoundaryTo(value2)); AreNotEqual( Range.CreateExclusiveFrom(value1, value2), Range.Create(value1, value2)); IsTrue(Range.Create(value1, value2) == Range.Create <int?>(1, 2)); IsTrue(Range.Create(value1, value2) != Range.Create(value1, 1)); IsTrue(Range.Create(value1, value2, key) == Range.Create <int?, string>(1, 2, key)); IsTrue(Range.Create(value1, value2, key) != Range.Create <int?, string>(1, 2, key2)); IsTrue(Range.Create(value1, value2, key) != Range.Create <int?, string>(1, 1, key)); IsTrue(Range.TryCreate(value1, value2) == Range.Create <int?>(1, 2)); IsTrue(Range.TryCreate(value1, value2) != Range.Create(value1, 1)); IsTrue(Range.TryCreate(value1, value2, key) == Range.Create <int?, string>(1, 2, key)); IsTrue(Range.TryCreate(value1, value2, key) != Range.Create <int?, string>(1, 2, key2)); IsTrue(Range.TryCreate(value1, value2, key) != Range.Create <int?, string>(1, 1, key)); IsTrue(Range.TryCreateExclusiveFrom(value1, value2) == Range.CreateExclusiveFrom <int?>(1, 2)); IsTrue(Range.TryCreateExclusiveFrom(value1, value3) == Range.CreateExclusiveFrom(value1, 3)); IsTrue(Range.TryCreateExclusiveFrom(value1, value3) != Range.CreateExclusiveFrom(value1, 4)); IsTrue(Range.TryCreateExclusiveFrom(value1, value2, key) == Range.CreateExclusiveFrom <int?, string>(1, 2, key)); IsTrue(Range.TryCreateExclusiveFrom(value1, value2, key) != Range.CreateExclusiveFrom <int?, string>(1, 2, key2)); IsTrue(Range.TryCreateExclusiveTo(value1, value2) == Range.CreateExclusiveTo <int?>(1, 2)); IsTrue(Range.TryCreateExclusiveTo(value1, value3) == Range.CreateExclusiveTo(value1, 3)); IsTrue(Range.TryCreateExclusiveTo(value1, value3) != Range.CreateExclusiveTo(value1, 4)); IsTrue(Range.TryCreateExclusiveTo(value1, value2, key) == Range.CreateExclusiveTo <int?, string>(1, 2, key)); IsTrue(Range.TryCreateExclusiveTo(value1, value2, key) != Range.CreateExclusiveTo <int?, string>(1, 2, key2)); }
/// <summary>Trims the range from the right.</summary> /// <typeparam name="T">The type of the range values.</typeparam> /// <param name="compositeRange">The source range.</param> /// <param name="to">A new boundary To.</param> /// <returns>A range trimmed with a new To boundary.</returns> public static CompositeRange <T> TrimTo <T>(this CompositeRange <T> compositeRange, RangeBoundaryTo <T> to) => compositeRange.Intersect(Range.TryCreate(RangeBoundaryFrom <T> .NegativeInfinity, to));
/// <summary>Trims the range from the left.</summary> /// <typeparam name="T">The type of the range values.</typeparam> /// <param name="compositeRange">The source range.</param> /// <param name="from">A new boundary From.</param> /// <returns>A range trimmed with a new From boundary.</returns> public static CompositeRange <T> TrimFrom <T>(this CompositeRange <T> compositeRange, RangeBoundaryFrom <T> from) => compositeRange.Intersect(Range.TryCreate(from, RangeBoundaryTo <T> .PositiveInfinity));
private Range <T2> TryCreateRange <T2>(RangeBoundaryFrom <T2> from, RangeBoundaryTo <T2> to) => Range.TryCreate(from, to);
Range <T, TKey> IRangeFactory <T, Range <T, TKey> > .TryCreateRange(RangeBoundaryFrom <T> from, RangeBoundaryTo <T> to) => Range.TryCreate(from, to, _key);
public CompositeRange <T> TrimTo(RangeBoundaryTo <T> to) => Intersect(Range.TryCreate(RangeBoundaryFrom <T> .NegativeInfinity, to));
public CompositeRange <T> TrimFrom(RangeBoundaryFrom <T> from) => Intersect(Range.TryCreate(from, RangeBoundaryTo <T> .PositiveInfinity));