示例#1
0
        public static void TestRangeMakeInclusiveExclusive()
        {
            var range = Range.Create(1, 2);

            AreEqual(range, range.MakeInclusive(i => i - 1, i => i + 1));
            range = Range.CreateExclusive(1, 2);
            AreEqual(Range.Create(0, 3), range.MakeInclusive(i => i - 1, i => i + 1));
            range = Range.CreateExclusiveTo(1, 2);
            AreEqual(Range.Create(1, 3), range.MakeInclusive(i => i - 1, i => i + 1));

            range = Range.CreateExclusive(1, 2);
            AreEqual(range, range.MakeExclusive(i => i - 1, i => i + 1));
            range = Range.Create(1, 2);
            AreEqual(Range.CreateExclusive(0, 3), range.MakeExclusive(i => i - 1, i => i + 1));
            range = Range.CreateExclusiveFrom(1, 2);
            AreEqual(Range.CreateExclusive(1, 3), range.MakeExclusive(i => i - 1, i => i + 1));

            range = Range.CreateExclusive(2, 3);
            IsTrue(range.MakeInclusive(i => i + 1, i => i - 1).IsEmpty);
            range = Range.Create(2, 3);
            IsTrue(range.MakeExclusive(i => i + 1, i => i - 1).IsEmpty);

            range = Range.CreateExclusive(2, 3);
            IsTrue(range.MakeInclusive(i => i + 1, i => i - 1).IsEmpty);
            range = Range.Create(2, 3);
            IsTrue(range.MakeExclusive(i => i + 1, i => i - 1).IsEmpty);

            var range2 = Range.CreateExclusive(1, double.PositiveInfinity);

            IsTrue(range2.MakeInclusive(i => double.NegativeInfinity, i => i + 1).IsInfinite);
            range2 = Range.Create(double.NegativeInfinity, 2);
            IsTrue(range2.MakeExclusive(i => i + 1, i => double.PositiveInfinity).IsInfinite);
            range2 = Range.Create(double.NegativeInfinity, double.PositiveInfinity);
            AreEqual(range2, range2.MakeInclusive(i => i - 1, i => i + 1));
        }
示例#2
0
        public static void TestRangeToString()
        {
            int?value1 = 1;
            int?empty  = null;
            var key    = "Hello!";

            AreEqual(Range <int> .Empty.ToString(), "∅");
            AreEqual(Range <int> .Infinite.ToString(), "(-∞..+∞)");
            AreEqual(Range.Create(1, 1).ToString(), "[1..1]");
            AreEqual(Range.Create(1, 2).ToString(), "[1..2]");
            AreEqual(Range.CreateExclusive(1, 2).ToString(), "(1..2)");
            AreEqual(Range.CreateExclusiveFrom(1, 2).ToString(), "(1..2]");
            AreEqual(Range.CreateExclusiveTo(1, 2).ToString(), "[1..2)");
            AreEqual(Range.CreateExclusive(value1, empty).ToString("000"), "(001..+∞)");

            AreEqual(Range.Create(RangeBoundaryFrom <int?> .Empty, RangeBoundaryTo <int?> .Empty, key).ToString(), "'Hello!':∅");
            AreEqual(Range.Create(empty, empty, key).ToString(), "'Hello!':(-∞..+∞)");
            AreEqual(Range.Create(empty, empty, (string)null).ToString(), "'':(-∞..+∞)");
            AreEqual(Range.Create(1, 1, key).ToString(), "'Hello!':[1..1]");
            AreEqual(Range.Create(1, 2, key).ToString(), "'Hello!':[1..2]");
            AreEqual(Range.CreateExclusive(1, 2, key).ToString(), "'Hello!':(1..2)");
            AreEqual(Range.CreateExclusiveFrom(1, 2, key).ToString(), "'Hello!':(1..2]");
            AreEqual(Range.CreateExclusiveTo(1, 2, key).ToString(), "'Hello!':[1..2)");
            AreEqual(Range.CreateExclusive(value1, empty, 3).ToString("000"), "'3':(001..+∞)");
            AreEqual(Range.Create((int?)1, null, key).ToString(), "'Hello!':[1..+∞)");

            var cultureRu = CultureInfo.GetCultureInfo("ru-RU");
            var cultureEn = CultureInfo.GetCultureInfo("en-US");

            AreEqual(Range.Create(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.Create(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]");
        }
示例#3
0
        public static void TestRangeEquality()
        {
            int?value1 = 1;
            int?value2 = 2;
            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));
        }
示例#4
0
        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 CompositeRange <T, TKey> ToCompositeRangeExclusiveFrom <T, TKey>(
     this IEnumerable <TKey> source,
     [InstantHandle] Func <TKey, T> fromValueSelector,
     [InstantHandle] Func <TKey, T> toValueSelector)
     where TKey : notnull =>
 source
 .Select(s => Range.CreateExclusiveFrom(fromValueSelector(s), toValueSelector(s), s))
 .ToCompositeRange();
示例#6
0
        public static void TestRangeIntersection()
        {
            double value1        = 1;
            double value2        = 2;
            var    emptyFrom     = RangeBoundaryFrom <double> .Empty;
            var    emptyTo       = RangeBoundaryTo <double> .Empty;
            var    emptyRange    = Range.Create(emptyFrom, emptyTo);
            var    infiniteRange = Range.Create(double.NegativeInfinity, double.PositiveInfinity);

            var range = Range.Create(value1, value2);

            AreEqual(range.Intersect(range), range);
            AreEqual(range.Intersect(1, 2), range);
            AreEqual(range.Intersect(1.5, 1.5), Range.Create(1.5, 1.5));
            AreEqual(range.Intersect(0, 3), range);
            AreEqual(range.Intersect(1.5, 3), Range.Create(1.5, 2.0));
            AreEqual(range.Intersect(0, 1.5), Range.Create(1.0, 1.5));
            AreEqual(range.Intersect(3, 4), emptyRange);
            AreEqual(range.Intersect(-2, -1), emptyRange);
            AreEqual(range.Intersect(emptyRange), emptyRange);
            AreEqual(emptyRange.Intersect(range), emptyRange);
            AreEqual(range.Intersect(infiniteRange), range);
            AreEqual(infiniteRange.Intersect(range), range);
            AreEqual(emptyRange.Intersect(infiniteRange), emptyRange);
            AreEqual(infiniteRange.Intersect(emptyRange), emptyRange);

            range = Range.CreateExclusive(value1, value2);
            AreEqual(range.Intersect(range), range);
            AreEqual(range.Intersect(1, 2), range);
            AreEqual(range.Intersect(1.5, 1.5), Range.Create(1.5, 1.5));
            AreEqual(range.Intersect(0, 3), range);
            AreEqual(range.Intersect(1.5, 3), Range.CreateExclusiveTo(1.5, 2.0));
            AreEqual(range.Intersect(0, 1.5), Range.CreateExclusiveFrom(1.0, 1.5));
            AreEqual(range.Intersect(3, 4), emptyRange);
            AreEqual(range.Intersect(-2, -1), emptyRange);
            AreEqual(range.Intersect(emptyRange), emptyRange);
            AreEqual(emptyRange.Intersect(range), emptyRange);
            AreEqual(range.Intersect(infiniteRange), range);
            AreEqual(infiniteRange.Intersect(range), range);
            AreEqual(emptyRange.Intersect(infiniteRange), emptyRange);
            AreEqual(infiniteRange.Intersect(emptyRange), emptyRange);

            Throws <ArgumentException>(
                () => range.Intersect(2, 1));
            Throws <ArgumentException>(
                () => range.Intersect(double.PositiveInfinity, double.NegativeInfinity));
            Throws <ArgumentException>(
                () => range.Intersect(2, double.NegativeInfinity));
            Throws <ArgumentException>(
                () => range.Intersect(double.PositiveInfinity, 1));
        }
示例#7
0
        public static void TestKeyedRangeUnion()
        {
            double value1        = 1;
            double value2        = 2;
            var    emptyFrom     = RangeBoundaryFrom <double> .Empty;
            var    emptyTo       = RangeBoundaryTo <double> .Empty;
            var    emptyRange    = Range.Create(emptyFrom, emptyTo, RangeKey);
            var    infiniteRange = Range.Create(double.NegativeInfinity, double.PositiveInfinity, RangeKey);

            var range = Range.Create(value1, value2, RangeKey);

            AreEqual(range.Union(range), range);
            AreEqual(range.Union(1, 2), range);
            AreEqual(range.Union(1.5, 1.5), range);
            AreEqual(range.Union(0, 3), Range.Create(0.0, 3.0, RangeKey));
            AreEqual(range.Union(1.5, 3), Range.Create(1.0, 3.0, RangeKey));
            AreEqual(range.Union(0, 1.5), Range.Create(0.0, 2.0, RangeKey));
            AreEqual(range.Union(3, 4), Range.Create(1.0, 4.0, RangeKey));
            AreEqual(range.Union(-2, -1), Range.Create(-2.0, 2.0, RangeKey));
            AreEqual(range.Union(emptyRange), range);
            AreEqual(emptyRange.Union(range), range);
            AreEqual(range.Union(infiniteRange), infiniteRange);
            AreEqual(infiniteRange.Union(range), infiniteRange);
            AreEqual(emptyRange.Union(infiniteRange), infiniteRange);
            AreEqual(infiniteRange.Union(emptyRange), infiniteRange);

            range = Range.CreateExclusive(value1, value2, RangeKey);
            AreEqual(range.Union(range), range);
            AreEqual(range.Union(1, 2), Range.Create(1.0, 2.0, RangeKey));
            AreEqual(range.Union(1.5, 1.5), range);
            AreEqual(range.Union(0, 3), Range.Create(0.0, 3.0, RangeKey));
            AreEqual(range.Union(1.5, 3), Range.CreateExclusiveFrom(1.0, 3.0, RangeKey));
            AreEqual(range.Union(0, 1.5), Range.CreateExclusiveTo(0.0, 2.0, RangeKey));
            AreEqual(range.Union(3, 4), Range.CreateExclusiveFrom(1.0, 4.0, RangeKey));
            AreEqual(range.Union(-2, -1), Range.CreateExclusiveTo(-2.0, 2.0, RangeKey));
            AreEqual(range.Union(emptyRange), range);
            AreEqual(emptyRange.Union(range), range);
            AreEqual(range.Union(infiniteRange), infiniteRange);
            AreEqual(infiniteRange.Union(range), infiniteRange);
            AreEqual(emptyRange.Union(infiniteRange), infiniteRange);
            AreEqual(infiniteRange.Union(emptyRange), infiniteRange);

            Throws <ArgumentException>(
                () => range.Union(2, 1));
            Throws <ArgumentException>(
                () => range.Union(double.PositiveInfinity, double.NegativeInfinity));
            Throws <ArgumentException>(
                () => range.Union(2, double.NegativeInfinity));
            Throws <ArgumentException>(
                () => range.Union(double.PositiveInfinity, 1));
        }
示例#8
0
        public static void TestRangeTrimTo()
        {
            double?empty      = null;
            double?value1     = 1;
            double?value2     = 2;
            var    emptyFrom  = RangeBoundaryFrom <double?> .Empty;
            var    emptyTo    = RangeBoundaryTo <double?> .Empty;
            var    emptyRange = Range.Create(emptyFrom, emptyTo);

            var range = Range.Create(value1, value2);

            AreEqual(range.TrimTo(null), range);
            AreEqual(range.TrimTo(double.PositiveInfinity), range);
            Throws <ArgumentException>(() => range.TrimTo(double.NegativeInfinity));
            AreEqual(range.TrimTo(RangeBoundaryTo <double?> .Empty), emptyRange);
            AreEqual(range.TrimTo(0), emptyRange);
            AreEqual(range.TrimTo(1), Range.Create(value1, 1));
            AreEqual(range.TrimTo(1.5), Range.Create(value1, 1.5));
            AreEqual(range.TrimTo(2), range);
            AreEqual(range.TrimTo(3), range);

            range = Range.Create(emptyFrom, emptyTo);
            AreEqual(range.TrimTo(null), range);
            AreEqual(range.TrimTo(double.PositiveInfinity), range);
            Throws <ArgumentException>(() => range.TrimTo(double.NegativeInfinity));
            AreEqual(range.TrimTo(RangeBoundaryTo <double?> .Empty), range);
            AreEqual(range.TrimTo(0), range);

            range = Range.CreateExclusive(empty, empty);
            AreEqual(range.TrimTo(null), range);
            AreEqual(range.TrimTo(double.PositiveInfinity), range);
            Throws <ArgumentException>(() => range.TrimTo(double.NegativeInfinity));
            AreEqual(range.TrimTo(RangeBoundaryTo <double?> .Empty), emptyRange);
            AreEqual(range.TrimTo(0), Range.Create(empty, 0));

            range = Range.CreateExclusive(value1, value2);
            AreEqual(range.TrimTo(2), range);
            AreEqual(range.TrimTo(1.5), Range.CreateExclusiveFrom(value1, 1.5));
            AreEqual(range.TrimTo(1), emptyRange);

            range = Range.CreateExclusive(value1, value2);
            AreEqual(range.TrimTo(Range.BoundaryTo <double?>(2)), range);
            AreEqual(range.TrimTo(Range.BoundaryTo <double?>(1.5)), Range.CreateExclusiveFrom(value1, 1.5));
            AreEqual(range.TrimTo(Range.BoundaryTo <double?>(1)), emptyRange);
        }
示例#9
0
        public static void TestKeyedRangeExtendTo()
        {
            double?empty     = null;
            double?value1    = 1;
            double?value2    = 2;
            var    emptyFrom = RangeBoundaryFrom <double?> .Empty;
            var    emptyTo   = RangeBoundaryTo <double?> .Empty;

            var range = Range.Create(value1, value2, RangeKey);

            AreEqual(range.ExtendTo(null), Range.Create(value1, empty, RangeKey));
            AreEqual(range.ExtendTo(double.PositiveInfinity), Range.Create(value1, empty, RangeKey));
            Throws <ArgumentException>(() => range.ExtendTo(double.NegativeInfinity));
            AreEqual(range.ExtendTo(RangeBoundaryTo <double?> .Empty), range);
            AreEqual(range.ExtendTo(0), range);
            AreEqual(range.ExtendTo(1), range);
            AreEqual(range.ExtendTo(1.5), range);
            AreEqual(range.ExtendTo(2), range);
            AreEqual(range.ExtendTo(3), Range.Create(value1, 3, RangeKey));

            range = Range.Create(emptyFrom, emptyTo, RangeKey);
            AreEqual(range.ExtendTo(null), range);
            AreEqual(range.ExtendTo(double.PositiveInfinity), range);
            Throws <ArgumentException>(() => range.ExtendTo(double.NegativeInfinity));
            AreEqual(range.ExtendTo(RangeBoundaryTo <double?> .Empty), range);
            AreEqual(range.ExtendTo(0), range);

            range = Range.CreateExclusive(empty, empty, RangeKey);
            AreEqual(range.ExtendTo(null), range);
            AreEqual(range.ExtendTo(double.PositiveInfinity), range);
            Throws <ArgumentException>(() => range.ExtendTo(double.NegativeInfinity));
            AreEqual(range.ExtendTo(RangeBoundaryTo <double?> .Empty), range);
            AreEqual(range.ExtendTo(0), range);

            range = Range.CreateExclusive(value1, value2, RangeKey);
            AreEqual(range.ExtendTo(1), range);
            AreEqual(range.ExtendTo(1.5), range);
            AreEqual(range.ExtendTo(2), Range.CreateExclusiveFrom(value1, 2, RangeKey));

            range = Range.CreateExclusive(value1, value2, RangeKey);
            AreEqual(range.ExtendTo(Range.BoundaryTo <double?>(2)), Range.CreateExclusiveFrom(value1, 2, RangeKey));
            AreEqual(range.ExtendTo(Range.BoundaryToExclusive <double?>(2)), range);
            AreEqual(range.ExtendTo(Range.BoundaryToExclusive <double?>(3)), Range.CreateExclusive(value1, 3, RangeKey));
        }
示例#10
0
        public static void TestRangeWithValue()
        {
            var range = Range.Create(1, 2);

            AreEqual(Range.Create(0, 3), range.WithValues(i => i - 1, i => i + 1));

            range = Range.CreateExclusiveFrom(1, 2);
            AreEqual(Range.CreateExclusiveFrom(0, 3), range.WithValues(i => i - 1, i => i + 1));

            range = Range.CreateExclusive(1, 2);
            AreEqual(Range.CreateExclusive(2, 3), range.WithValues(i => i + 1));

            var toInf  = (double?)double.PositiveInfinity;
            var range2 = Range.CreateExclusive(1, toInf);

            IsTrue(range2.WithValues(i => (double?)null).IsInfinite);
            range2 = Range.Create(double.NegativeInfinity, toInf);
            AreEqual(range2, range2.WithValues(i => i - 1, i => i + 1));
        }
        public static void TestCompositeRangeWithKeyProperties()
        {
            var range1A = Range.Create(1, 2, "A");
            var range1B = Range.Create(1, 2, "B");
            var range2C = Range.CreateExclusiveFrom(2, 3, "C");
            var range3A = Range.Create(3, 4).WithKey("A");
            var empty   = Range <int> .Empty.WithKey((string)null);

            var infinite = Range <int> .Infinite.WithKey((string)null);

            var a = new CompositeRange <int, string>();

            AreEqual(a, CompositeRange.Create(empty, empty));
            AreEqual(a, new CompositeRange <int, string>());
            AreEqual(a.SubRanges.Count, 0);
            AreEqual(a.ContainingRange, Range <int> .Empty);
            IsTrue(a.IsEmpty);
            IsFalse(a.IsNotEmpty);
            IsTrue(a.IsMerged);

            a = new CompositeRange <int, string>(infinite);
            AreNotEqual(a, new CompositeRange <int, string>());
            AreEqual(a, CompositeRange.Create(infinite));
            AreNotEqual(a, CompositeRange.Create(infinite, infinite));
            AreEqual(a.SubRanges.Count, 1);
            AreEqual(a.ContainingRange, infinite.WithoutKey());
            AreEqual(a.SubRanges[0], infinite);
            IsFalse(a.IsEmpty);
            IsTrue(a.IsNotEmpty);
            IsTrue(a.IsMerged);

            a = CompositeRange.Create(range1A);
            AreNotEqual(a, new CompositeRange <int, string>());
            AreNotEqual(a, new CompositeRange <int, string>(infinite));
            AreNotEqual(a, CompositeRange.Create(range1A, range1A));
            AreNotEqual(a, CompositeRange.Create(range1B));
            AreEqual(a.SubRanges.Count, 1);
            AreEqual(a.ContainingRange, range1A.WithoutKey());
            AreEqual(a.SubRanges[0], range1A);
            AreNotEqual(a.SubRanges[0], range1B);
            IsFalse(a.IsEmpty);
            IsTrue(a.IsNotEmpty);
            IsTrue(a.IsMerged);

            a = CompositeRange.Create(range1A, range1B);
            AreNotEqual(a, CompositeRange <int> .Empty);
            AreNotEqual(a, CompositeRange <int> .Infinite);
            AreEqual(a, CompositeRange.Create(range1B, range1A));
            AreEqual(a.SubRanges.Count, 2);
            AreEqual(a.ContainingRange, range1A.WithoutKey());
            AreEqual(a.SubRanges[0], range1A);
            AreEqual(a.SubRanges[1], range1B);
            IsFalse(a.IsEmpty);
            IsTrue(a.IsNotEmpty);
            IsFalse(a.IsMerged);

            a = CompositeRange.Create(range1A, range2C);
            AreNotEqual(a, CompositeRange <int> .Empty);
            AreNotEqual(a, CompositeRange <int> .Infinite);
            AreEqual(a, CompositeRange.Create(range2C, range1A));
            AreEqual(a.SubRanges.Count, 2);
            AreEqual(a.ContainingRange, Range.Create(1, 3));
            AreEqual(a.SubRanges[0], range1A);
            AreEqual(a.SubRanges[1], range2C);
            IsFalse(a.IsEmpty);
            IsTrue(a.IsNotEmpty);
            IsFalse(a.IsMerged);
            IsFalse(a.SubRanges[0].HasIntersection(a.SubRanges[1]));
            IsTrue(a.SubRanges[0].To.GetComplementation() == a.SubRanges[1].From);

            a = CompositeRange.Create(range1A, range3A);
            AreNotEqual(a, CompositeRange <int> .Empty);
            AreNotEqual(a, CompositeRange <int> .Infinite);
            AreEqual(a, CompositeRange.Create(range3A, range1A));
            AreEqual(a.SubRanges.Count, 2);
            AreEqual(a.ContainingRange, Range.Create(1, 4));
            AreEqual(a.SubRanges[0], range1A);
            AreEqual(a.SubRanges[1], range3A);
            IsFalse(a.IsEmpty);
            IsTrue(a.IsNotEmpty);
            IsTrue(a.IsMerged);
        }
        public static void TestCompositeRangeRangeProperties()
        {
            var range1   = Range.Create(1, 2);
            var range2   = Range.CreateExclusiveFrom(2, 3);
            var range3   = Range.Create(3, 4);
            var empty    = Range <int> .Empty;
            var infinite = Range <int> .Infinite;

            // ReSharper disable once ObjectCreationAsStatement
            // ReSharper disable once AssignNullToNotNullAttribute
            Throws <ArgumentNullException>(() => new CompositeRange <int>(null));

            var a = new CompositeRange <int>();

            AreEqual(a, CompositeRange <int> .Empty);
            AreEqual(a, CompositeRange.Create(empty, empty));
            AreNotEqual(a, CompositeRange <int> .Infinite);
            AreEqual(a, new CompositeRange <int>());
            AreEqual(a.SubRanges.Count, 0);
            AreEqual(a.ContainingRange, empty);
            IsTrue(a.IsEmpty);
            IsFalse(a.IsNotEmpty);
            IsTrue(a.IsMerged);

            a = new CompositeRange <int>(infinite);
            AreNotEqual(a, CompositeRange <int> .Empty);
            AreEqual(a, CompositeRange <int> .Infinite);
            AreNotEqual(a, CompositeRange.Create(infinite, infinite));
            AreEqual(a.SubRanges.Count, 1);
            AreEqual(a.ContainingRange, infinite);
            AreEqual(a.SubRanges[0], infinite);
            IsFalse(a.IsEmpty);
            IsTrue(a.IsNotEmpty);
            IsTrue(a.IsMerged);

            a = CompositeRange.Create(range1);
            AreNotEqual(a, CompositeRange <int> .Empty);
            AreNotEqual(a, CompositeRange <int> .Infinite);
            AreNotEqual(a, CompositeRange.Create(range1, range1));
            AreEqual(a.SubRanges.Count, 1);
            AreEqual(a.ContainingRange, range1);
            AreEqual(a.SubRanges[0], range1);
            IsFalse(a.IsEmpty);
            IsTrue(a.IsNotEmpty);
            IsTrue(a.IsMerged);

            a = CompositeRange.Create(range1, range1);
            AreNotEqual(a, CompositeRange <int> .Empty);
            AreNotEqual(a, CompositeRange <int> .Infinite);
            AreEqual(a, CompositeRange.Create(range1, range1));
            AreEqual(a.SubRanges.Count, 2);
            AreEqual(a.ContainingRange, range1);
            AreEqual(a.SubRanges[0], range1);
            AreEqual(a.SubRanges[1], range1);
            IsFalse(a.IsEmpty);
            IsTrue(a.IsNotEmpty);
            IsFalse(a.IsMerged);

            a = CompositeRange.Create(range1, range2);
            AreNotEqual(a, CompositeRange <int> .Empty);
            AreNotEqual(a, CompositeRange <int> .Infinite);
            AreEqual(a, CompositeRange.Create(range2, range1));
            AreEqual(a.SubRanges.Count, 2);
            AreEqual(a.ContainingRange, Range.Create(1, 3));
            AreEqual(a.SubRanges[0], range1);
            AreEqual(a.SubRanges[1], range2);
            IsFalse(a.IsEmpty);
            IsTrue(a.IsNotEmpty);
            IsFalse(a.IsMerged);
            IsFalse(a.SubRanges[0].HasIntersection(a.SubRanges[1]));
            IsTrue(a.SubRanges[0].To.GetComplementation() == a.SubRanges[1].From);

            a = CompositeRange.Create(range1, range3);
            AreNotEqual(a, CompositeRange <int> .Empty);
            AreNotEqual(a, CompositeRange <int> .Infinite);
            AreEqual(a, CompositeRange.Create(range3, range1));
            AreEqual(a.SubRanges.Count, 2);
            AreEqual(a.ContainingRange, Range.Create(1, 4));
            AreEqual(a.SubRanges[0], range1);
            AreEqual(a.SubRanges[1], range3);
            IsFalse(a.IsEmpty);
            IsTrue(a.IsNotEmpty);
            IsTrue(a.IsMerged);
        }
示例#13
0
        public static void TestRangeEndsBefore()
        {
            double?empty     = null;
            double?value1    = 1;
            double?value2    = 2;
            var    emptyFrom = RangeBoundaryFrom <double?> .Empty;
            var    emptyTo   = RangeBoundaryTo <double?> .Empty;

            var range = Range.Create(value1, value2);

            IsTrue(range.EndsBefore(null));
            IsFalse(range.EndsBefore(double.NegativeInfinity));
            IsTrue(range.EndsBefore(double.PositiveInfinity));
            IsFalse(range.EndsBefore(RangeBoundaryFrom <double?> .Empty));
            IsFalse(range.EndsBefore(RangeBoundaryTo <double?> .Empty));
            IsFalse(range.EndsBefore(0));
            IsFalse(range.EndsBefore(1));
            IsFalse(range.EndsBefore(1.5));
            IsFalse(range.EndsBefore(2));
            IsTrue(range.EndsBefore(3));

            IsFalse(range.EndsBefore(Range.Create(2, empty, _rangeKey2)));
            IsTrue(range.EndsBefore(Range.CreateExclusiveFrom(2, empty, _rangeKey2)));
            IsTrue(range.EndsBefore(Range.Create(3, empty, _rangeKey2)));

            range = Range.Create(emptyFrom, emptyTo);
            IsFalse(range.EndsBefore(null));
            IsFalse(range.EndsBefore(double.NegativeInfinity));
            IsFalse(range.EndsBefore(double.PositiveInfinity));
            IsFalse(range.EndsBefore(RangeBoundaryFrom <double?> .Empty));
            IsFalse(range.EndsBefore(RangeBoundaryTo <double?> .Empty));
            IsFalse(range.EndsBefore(0));

            range = Range.CreateExclusive(empty, empty);
            IsFalse(range.EndsBefore(null));
            IsFalse(range.EndsBefore(double.NegativeInfinity));
            IsFalse(range.EndsBefore(double.PositiveInfinity));
            IsFalse(range.EndsBefore(RangeBoundaryFrom <double?> .Empty));
            IsFalse(range.EndsBefore(RangeBoundaryTo <double?> .Empty));
            IsFalse(range.EndsBefore(0));

            range = Range.CreateExclusive(value1, value2);
            IsFalse(range.EndsBefore(1));
            IsFalse(range.EndsBefore(1.5));
            IsTrue(range.EndsBefore(2));

            range = Range.CreateExclusive(value1, value2);
            IsFalse(range.EndsBefore(Range.BoundaryFrom <double?>(1)));
            IsTrue(range.EndsBefore(Range.BoundaryTo <double?>(2)));
            IsFalse(range.EndsBefore(Range.BoundaryFromExclusive <double?>(1)));
            IsFalse(range.EndsBefore(Range.BoundaryFromExclusive <double?>(1.5)));
            IsTrue(range.EndsBefore(Range.BoundaryFromExclusive <double?>(2)));
            IsFalse(range.EndsBefore(Range.BoundaryToExclusive <double?>(1)));
            IsFalse(range.EndsBefore(Range.BoundaryToExclusive <double?>(1.5)));
            IsFalse(range.EndsBefore(Range.BoundaryToExclusive <double?>(2)));

            Throws <ArgumentException>(
                () => range.EndsBefore(Range.BoundaryFrom <double?>(double.PositiveInfinity)));
            Throws <ArgumentException>(
                () => range.EndsBefore(Range.BoundaryTo <double?>(double.NegativeInfinity)));
        }
        public static void TestRangeProperties()
        {
            var range1   = Range.Create(1, 2);
            var range2   = Range.CreateExclusiveFrom(2, 3);
            var range3   = Range.Create(3, 4);
            var empty    = Range <int> .Empty;
            var infinite = Range <int> .Infinite;

            var a = new CompositeRange <int>();

            AreEqual(a, CompositeRange <int> .Empty);
            AreEqual(a, CompositeRange.Create(empty, empty));
            AreNotEqual(a, CompositeRange <int> .Infinite);
            AreEqual(a, new CompositeRange <int>());
            AreEqual(a.SubRanges.Count, 0);
            AreEqual(a.ContainingRange, empty);
            IsTrue(a.IsEmpty);
            IsFalse(a.IsNotEmpty);
            IsTrue(a.IsMerged);

            a = new CompositeRange <int>(infinite);
            AreNotEqual(a, CompositeRange <int> .Empty);
            AreEqual(a, CompositeRange <int> .Infinite);
            AreNotEqual(a, CompositeRange.Create(infinite, infinite));
            AreEqual(a.SubRanges.Count, 1);
            AreEqual(a.ContainingRange, infinite);
            AreEqual(a.SubRanges[0], infinite);
            IsFalse(a.IsEmpty);
            IsTrue(a.IsNotEmpty);
            IsTrue(a.IsMerged);

            a = CompositeRange.Create(range1);
            AreNotEqual(a, CompositeRange <int> .Empty);
            AreNotEqual(a, CompositeRange <int> .Infinite);
            AreNotEqual(a, CompositeRange.Create(range1, range1));
            AreEqual(a.SubRanges.Count, 1);
            AreEqual(a.ContainingRange, range1);
            AreEqual(a.SubRanges[0], range1);
            IsFalse(a.IsEmpty);
            IsTrue(a.IsNotEmpty);
            IsTrue(a.IsMerged);

            a = CompositeRange.Create(range1, range1);
            AreNotEqual(a, CompositeRange <int> .Empty);
            AreNotEqual(a, CompositeRange <int> .Infinite);
            AreEqual(a, CompositeRange.Create(range1, range1));
            AreEqual(a.SubRanges.Count, 2);
            AreEqual(a.ContainingRange, range1);
            AreEqual(a.SubRanges[0], range1);
            AreEqual(a.SubRanges[1], range1);
            IsFalse(a.IsEmpty);
            IsTrue(a.IsNotEmpty);
            IsFalse(a.IsMerged);

            a = CompositeRange.Create(range1, range2);
            AreNotEqual(a, CompositeRange <int> .Empty);
            AreNotEqual(a, CompositeRange <int> .Infinite);
            AreEqual(a, CompositeRange.Create(range2, range1));
            AreEqual(a.SubRanges.Count, 2);
            AreEqual(a.ContainingRange, Range.Create(1, 3));
            AreEqual(a.SubRanges[0], range1);
            AreEqual(a.SubRanges[1], range2);
            IsFalse(a.IsEmpty);
            IsTrue(a.IsNotEmpty);
            IsFalse(a.IsMerged);
            IsFalse(a.SubRanges[0].HasIntersection(a.SubRanges[1]));
            IsTrue(a.SubRanges[0].To.GetComplementation() == a.SubRanges[1].From);

            a = CompositeRange.Create(range1, range3);
            AreNotEqual(a, CompositeRange <int> .Empty);
            AreNotEqual(a, CompositeRange <int> .Infinite);
            AreEqual(a, CompositeRange.Create(range3, range1));
            AreEqual(a.SubRanges.Count, 2);
            AreEqual(a.ContainingRange, Range.Create(1, 4));
            AreEqual(a.SubRanges[0], range1);
            AreEqual(a.SubRanges[1], range3);
            IsFalse(a.IsEmpty);
            IsTrue(a.IsNotEmpty);
            IsTrue(a.IsMerged);
        }
        public static void TestCompositeRangeArgsOrderWithKeys()
        {
            var seek = new Random().Next();

            Console.WriteLine(
                $"{MethodBase.GetCurrentMethod().Name}: Rnd seek: {seek} (use the seek to reproduce test results).");
            var rnd = new Random(seek);

            var emtpy    = Range.Create(RangeBoundaryFrom <int> .Empty, RangeBoundaryTo <int> .Empty, "E");
            var infinite = Range.Create(
                RangeBoundaryFrom <int> .NegativeInfinity,
                RangeBoundaryTo <int> .PositiveInfinity, "I");
            // Equal ranges should use same keys to provide repeatable results after
            // ranges array will be shuffled.
            var ranges = new[]
            {
                emtpy,
                Range.Create(1, 2, "A"),
                Range.Create(1, 2, "A"),
                Range.Create(3, 4, "B"),
                Range.Create(3, 5, "C"),
                emtpy,
                emtpy,
                emtpy,
                Range.CreateExclusive(4, 6, "D"),
                Range.CreateExclusiveTo(7, 8, "F"),
                Range.CreateExclusiveTo(3, 5, "G"),
                Range.CreateExclusive(3, 5, "H"),
                Range.CreateExclusiveFrom(3, 5, "K"),
                Range.Create(0, 6, (string)null),
                emtpy,
                emtpy,
                infinite
            };

            var compositeRange = new CompositeRange <int, string>(ranges);

            AreEqual(
                compositeRange.ToString(),
                "(-∞..+∞): { 'I':(-∞..+∞); '':[0..6]; 'A':[1..2]; 'A':[1..2]; 'B':[3..4]; " +
                "'G':[3..5); 'C':[3..5]; 'H':(3..5); 'K':(3..5]; 'D':(4..6); 'F':[7..8) }");

            compositeRange = new CompositeRange <int, string>(ranges.OrderBy(r => rnd.Next()));
            AreEqual(
                compositeRange.ToString(),
                "(-∞..+∞): { 'I':(-∞..+∞); '':[0..6]; 'A':[1..2]; 'A':[1..2]; 'B':[3..4]; " +
                "'G':[3..5); 'C':[3..5]; 'H':(3..5); 'K':(3..5]; 'D':(4..6); 'F':[7..8) }");

            compositeRange = new CompositeRange <int, string>(ranges.Take(ranges.Length - 1));
            AreEqual(
                compositeRange.ToString(),
                "[0..8): { '':[0..6]; 'A':[1..2]; 'A':[1..2]; 'B':[3..4]; " +
                "'G':[3..5); 'C':[3..5]; 'H':(3..5); 'K':(3..5]; 'D':(4..6); 'F':[7..8) }");

            compositeRange = new CompositeRange <int, string>(ranges.Take(ranges.Length - 1).OrderBy(r => rnd.Next()));
            AreEqual(
                compositeRange.ToString(),
                "[0..8): { '':[0..6]; 'A':[1..2]; 'A':[1..2]; 'B':[3..4]; " +
                "'G':[3..5); 'C':[3..5]; 'H':(3..5); 'K':(3..5]; 'D':(4..6); 'F':[7..8) }");

            compositeRange = new CompositeRange <int, string>(ranges.Last());
            AreEqual(compositeRange.ToString(), "(-∞..+∞): { 'I':(-∞..+∞) }");

            compositeRange = new CompositeRange <int, string>(ranges.First());
            AreEqual(compositeRange.ToString(), "∅");

            compositeRange = new CompositeRange <int, string>(ranges.Take(0));
            AreEqual(compositeRange.ToString(), "∅");

            compositeRange = new CompositeRange <int, string>();
            AreEqual(compositeRange.ToString(), "∅");
        }
        public static void TestCompositeRangeArgsOrder()
        {
            var seek = new Random().Next();

            Console.WriteLine(
                $"{MethodBase.GetCurrentMethod().Name}: Rnd seek: {seek} (use the seek to reproduce test results).");
            var rnd = new Random(seek);

            var ranges = new[]
            {
                Range <int> .Empty,
                Range.Create(1, 2),
                Range.Create(1, 2),
                Range.Create(3, 4),
                Range.Create(3, 5),
                Range <int> .Empty,
                Range <int> .Empty,
                Range <int> .Empty,
                Range.CreateExclusive(4, 6),
                Range.CreateExclusiveTo(7, 8),
                Range.CreateExclusiveTo(3, 5),
                Range.CreateExclusive(3, 5),
                Range.CreateExclusiveFrom(3, 5),
                Range.Create(0, 6),
                Range <int> .Empty,
                Range <int> .Empty,
                Range <int> .Infinite
            };

            var compositeRange = new CompositeRange <int>(ranges);

            AreEqual(
                compositeRange.ToString(),
                "(-∞..+∞): { (-∞..+∞); [0..6]; [1..2]; [1..2]; [3..4]; [3..5); [3..5]; (3..5); (3..5]; (4..6); [7..8) }");

            compositeRange = ranges.OrderBy(r => rnd.Next()).ToCompositeRange();
            AreEqual(
                compositeRange.ToString(),
                "(-∞..+∞): { (-∞..+∞); [0..6]; [1..2]; [1..2]; [3..4]; [3..5); [3..5]; (3..5); (3..5]; (4..6); [7..8) }");

            compositeRange = ranges.Take(ranges.Length - 1).ToCompositeRange();
            AreEqual(
                compositeRange.ToString(),
                "[0..8): { [0..6]; [1..2]; [1..2]; [3..4]; [3..5); [3..5]; (3..5); (3..5]; (4..6); [7..8) }");

            compositeRange = ranges.Take(ranges.Length - 1).OrderBy(r => rnd.Next()).ToCompositeRange();
            AreEqual(
                compositeRange.ToString(),
                "[0..8): { [0..6]; [1..2]; [1..2]; [3..4]; [3..5); [3..5]; (3..5); (3..5]; (4..6); [7..8) }");

            compositeRange = new CompositeRange <int>(ranges.Last());
            AreEqual(compositeRange.ToString(), "(-∞..+∞): { (-∞..+∞) }");

            compositeRange = new CompositeRange <int>(ranges.First());
            AreEqual(compositeRange.ToString(), "∅");

            compositeRange = new CompositeRange <int>(ranges.Take(0));
            AreEqual(compositeRange.ToString(), "∅");

            compositeRange = new CompositeRange <int>();
            AreEqual(compositeRange.ToString(), "∅");
        }