コード例 #1
0
        public void TestBoundaryNegativeInfinityValue()
        {
            double?infOk   = double.NegativeInfinity;
            double?infFail = double.PositiveInfinity;
            double?empty   = null;

            DoesNotThrow(() => new RangeBoundaryFrom <double?>(infOk, RangeBoundaryFromKind.Infinite));

            Throws <ArgumentException>(() => new RangeBoundaryFrom <double?>(infOk, RangeBoundaryFromKind.Empty));
            Throws <ArgumentException>(() => new RangeBoundaryFrom <double?>(infFail, RangeBoundaryFromKind.Empty));
            Throws <ArgumentException>(() => new RangeBoundaryFrom <double?>(infFail, RangeBoundaryFromKind.Infinite));
            Throws <ArgumentException>(() => new RangeBoundaryFrom <double?>(infOk, RangeBoundaryFromKind.Inclusive));
            Throws <ArgumentException>(() => new RangeBoundaryFrom <double?>(infFail, RangeBoundaryFromKind.Inclusive));
            Throws <ArgumentException>(() => new RangeBoundaryFrom <double?>(infOk, RangeBoundaryFromKind.Exclusive));
            Throws <ArgumentException>(() => new RangeBoundaryFrom <double?>(infFail, RangeBoundaryFromKind.Exclusive));

            AreEqual(
                RangeBoundaryFrom <double?> .NegativeInfinity,
                Range.BoundaryFrom(infOk));
            AreEqual(
                RangeBoundaryFrom <double?> .NegativeInfinity,
                Range.BoundaryFromExclusive(infOk));

            AreEqual(
                RangeBoundaryFrom <double> .NegativeInfinity,
                Range.BoundaryFrom(infOk.Value));
            AreEqual(
                RangeBoundaryFrom <double> .NegativeInfinity,
                Range.BoundaryFromExclusive(infOk.Value));

            AreEqual(
                Range.BoundaryFromExclusive(infOk).GetValueOrDefault(),
                empty);
        }
コード例 #2
0
        /// <summary>
        /// Replaces exclusive boundaries with inclusive ones with the values from the selector callbacks
        /// </summary>
        /// <typeparam name="T">The type of the range values.</typeparam>
        /// <param name="range">The source range.</param>
        /// <param name="fromValueSelector">Callback to obtain a new value for the From boundary. Used if the boundary is exclusive.</param>
        /// <param name="toValueSelector">Callback to obtain a new value for the To boundary. Used if the boundary is exclusive.</param>
        /// <returns>A range with inclusive boundaries.</returns>
        public static Range <T> MakeInclusive <T>(
            this Range <T> range,
            [NotNull, InstantHandle] Func <T, T> fromValueSelector,
            [NotNull, InstantHandle] Func <T, T> toValueSelector)
        {
            if (range.IsEmpty || (!range.From.IsExclusiveBoundary && !range.To.IsExclusiveBoundary))
            {
                return(range);
            }

            var from = range.From;

            if (from.IsExclusiveBoundary)
            {
                from = Range.BoundaryFrom(fromValueSelector(from.GetValueOrDefault()));
            }
            var to = range.To;

            if (to.IsExclusiveBoundary)
            {
                to = Range.BoundaryTo(toValueSelector(to.GetValueOrDefault()));
            }

            return(range.TryCreateRange(from, to));
        }
コード例 #3
0
        public void TestBoundaryFromComplementation()
        {
            int?value1 = 1;
            int?value2 = 2;

            var boundary      = Range.BoundaryFrom(value1);
            var boundaryCompl = boundary.GetComplementation();

            IsTrue(boundaryCompl.IsComplementationFor(boundary));
            IsFalse(boundaryCompl.IsComplementationFor(Range.BoundaryFrom(value2)));
            IsFalse(boundaryCompl.IsComplementationFor(RangeBoundaryFrom <int?> .Empty));
            IsFalse(boundaryCompl.IsComplementationFor(RangeBoundaryFrom <int?> .NegativeInfinity));
            AreEqual(boundaryCompl.Value, 1);
            AreEqual(boundaryCompl.Kind, RangeBoundaryToKind.Exclusive);
            AreEqual(boundaryCompl.GetComplementation(), boundary);

            boundary      = Range.BoundaryFromExclusive(value1);
            boundaryCompl = boundary.GetComplementation();
            IsTrue(boundaryCompl.IsComplementationFor(boundary));
            IsFalse(boundaryCompl.IsComplementationFor(Range.BoundaryFrom(value2)));
            IsFalse(boundaryCompl.IsComplementationFor(RangeBoundaryFrom <int?> .Empty));
            IsFalse(boundaryCompl.IsComplementationFor(RangeBoundaryFrom <int?> .NegativeInfinity));
            AreEqual(boundaryCompl.Value, 1);
            AreEqual(boundaryCompl.Kind, RangeBoundaryToKind.Inclusive);
            AreEqual(boundaryCompl.GetComplementation(), boundary);

            boundary = RangeBoundaryFrom <int?> .Empty;
            Throws <InvalidOperationException>(() => boundary.GetComplementation());
            boundary = RangeBoundaryFrom <int?> .NegativeInfinity;
            Throws <InvalidOperationException>(() => boundary.GetComplementation());
        }
コード例 #4
0
        public static void TestBoundaryValueComparison()
        {
            int?value1 = 1;
            int?value2 = 2;

            var eFrom = RangeBoundaryFrom <int?> .Empty;
            var nInf  = Range.BoundaryFromInfinity <int?>();
            var pInf  = Range.BoundaryToInfinity <int?>();

            var fromInc1 = Range.BoundaryFrom(value1);
            var fromEx1  = Range.BoundaryFromExclusive(value1);
            var toInc1   = Range.BoundaryTo(value1);
            var toEx1    = Range.BoundaryToExclusive(value1);

            var fromInc2 = Range.BoundaryFrom(value2);
            var fromEx2  = Range.BoundaryFromExclusive(value2);
            var toInc2   = Range.BoundaryTo(value2);
            var toEx2    = Range.BoundaryToExclusive(value2);

            IsTrue(eFrom < nInf);
            IsTrue(nInf < toEx1);
            IsTrue(toEx1 < fromInc1);
            IsTrue(fromInc1 <= toInc1);
            IsTrue(toInc1 < fromEx1);
            IsTrue(fromEx1 < toEx2);
            IsTrue(toEx2 < fromInc2);
            IsTrue(fromInc2 <= toInc2);
            IsTrue(toInc2 < fromEx2);
            IsTrue(fromEx2 < pInf);
        }
コード例 #5
0
ファイル: RangeTests.cs プロジェクト: gitter-badger/CodeJam-2
        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);
        }
コード例 #6
0
        public static void TestBoundaryRawValueComparison()
        {
            int?value1 = 1;
            int?value2 = 2;
            int?empty  = null;

            var eFrom = RangeBoundaryFrom <int?> .Empty;
            var nInf  = Range.BoundaryFromInfinity <int?>();
            var pInf  = Range.BoundaryToInfinity <int?>();

            var fromInc1 = Range.BoundaryFrom(value1);
            var fromEx1  = Range.BoundaryFromExclusive(value1);
            var toInc1   = Range.BoundaryTo(value1);
            var toEx1    = Range.BoundaryToExclusive(value1);

            var fromInc2 = Range.BoundaryFrom(value2);
            var fromEx2  = Range.BoundaryFromExclusive(value2);
            var toInc2   = Range.BoundaryTo(value2);
            var toEx2    = Range.BoundaryToExclusive(value2);

            IsTrue(eFrom < empty);
            IsTrue(nInf <= empty);
            IsTrue(nInf >= empty);
            IsTrue(pInf > empty);

            IsTrue(toEx1 > empty);
            IsTrue(toInc1 > empty);
            IsTrue(fromEx2 > empty);
            IsTrue(fromInc2 > empty);

            IsTrue(empty < toEx2);
            IsTrue(empty < toInc2);
            IsTrue(empty < fromEx1);
            IsTrue(empty < fromInc1);

            IsTrue(nInf < value1);
            IsTrue(toEx1 < value1);
            IsTrue(fromEx1 > value1);
            IsTrue(toInc1 <= value1);
            IsTrue(fromInc1 >= value1);

            IsTrue(toEx1 < value2);
            IsTrue(fromEx1 < value2);
            IsTrue(toInc1 < value2);
            IsTrue(fromInc1 < value2);

            IsTrue(value2 < pInf);
            IsTrue(value2 > toEx2);
            IsTrue(value2 < fromEx2);
            IsTrue(value2 >= toInc2);
            IsTrue(value2 <= fromInc2);

            IsTrue(value1 < toEx2);
            IsTrue(value1 < fromEx2);
            IsTrue(value1 < toInc2);
            IsTrue(value1 < fromInc2);
        }
コード例 #7
0
        public static void TestRangeContains()
        {
            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);

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

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

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

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

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

            Throws <ArgumentException>(
                () => range.Contains(Range.BoundaryFrom <double?>(double.PositiveInfinity)));
            Throws <ArgumentException>(
                () => range.Contains(Range.BoundaryTo <double?>(double.NegativeInfinity)));
        }
コード例 #8
0
        public RangeBoundaryFrom <int> Test01NoValidation()
        {
            var result = RangeBoundaryFrom <int> .Empty;

            for (var i = 0; i < Count; i++)
            {
                result = Range.BoundaryFrom(i);
            }
            return(result);
        }
コード例 #9
0
        public void TestBoundaryFromProperties()
        {
            int?value1 = 1;
            int?value2 = 2;
            int?empty  = null;

            var a = new RangeBoundaryFrom <int?>();

            AreEqual(a.Kind, RangeBoundaryFromKind.Empty);
            AreEqual(a.GetValueOrDefault(), empty);
            Throws <InvalidOperationException>(() => a.Value.ToString());

            IsTrue(a.IsEmpty);
            IsFalse(a.IsNotEmpty);
            IsFalse(a.HasValue);
            IsFalse(a.IsNegativeInfinity);
            IsFalse(a.IsInclusiveBoundary);
            IsFalse(a.IsExclusiveBoundary);

            a = Range.BoundaryFromInfinity <int?>();
            AreEqual(a.Kind, RangeBoundaryFromKind.Infinite);
            AreEqual(a.GetValueOrDefault(), empty);
            Throws <InvalidOperationException>(() => a.Value.ToString());

            IsFalse(a.IsEmpty);
            IsTrue(a.IsNotEmpty);
            IsFalse(a.HasValue);
            IsTrue(a.IsNegativeInfinity);
            IsFalse(a.IsInclusiveBoundary);
            IsFalse(a.IsExclusiveBoundary);

            a = Range.BoundaryFrom(value1);
            AreEqual(a.Kind, RangeBoundaryFromKind.Inclusive);
            AreEqual(a.Value, value1);
            AreNotEqual(a.Value, value2);

            IsFalse(a.IsEmpty);
            IsTrue(a.IsNotEmpty);
            IsTrue(a.HasValue);
            IsFalse(a.IsNegativeInfinity);
            IsTrue(a.IsInclusiveBoundary);
            IsFalse(a.IsExclusiveBoundary);

            a = Range.BoundaryFromExclusive(value1);
            AreEqual(a.Kind, RangeBoundaryFromKind.Exclusive);
            AreEqual(a.Value, value1);
            AreNotEqual(a.Value, value2);

            IsFalse(a.IsEmpty);
            IsTrue(a.IsNotEmpty);
            IsTrue(a.HasValue);
            IsFalse(a.IsNegativeInfinity);
            IsFalse(a.IsInclusiveBoundary);
            IsTrue(a.IsExclusiveBoundary);
        }
コード例 #10
0
        public static void TestBoundaryKindComparison()
        {
            int?value1 = 1;

            var eFrom   = RangeBoundaryFrom <int?> .Empty;
            var eTo     = RangeBoundaryTo <int?> .Empty;
            var nInf    = Range.BoundaryFromInfinity <int?>();
            var pInf    = Range.BoundaryToInfinity <int?>();
            var fromInc = Range.BoundaryFrom(value1);
            var fromEx  = Range.BoundaryFromExclusive(value1);
            var toInc   = Range.BoundaryTo(value1);
            var toEx    = Range.BoundaryToExclusive(value1);

            AreNotEqual(fromInc, fromEx);
            AreNotEqual(fromInc, toInc);
            AreNotEqual(toInc, toEx);
            AreEqual(fromInc.Value, fromEx.Value);
            AreEqual(fromInc.Value, toInc.Value);
            AreEqual(toInc.Value, toEx.Value);

            // priority:
            // '∅' < '-∞' < 'a)' < '[a' == 'a]' < '(a' < '+∞'
            AreEqual(eFrom.CompareTo(eTo), 0);
            AreEqual(eTo.CompareTo(eFrom), 0);
            Less(eFrom, nInf);
            Less(nInf, toEx);
            Less(toEx, fromInc);
            AreEqual(fromInc.CompareTo(toInc), 0);
            AreEqual(toInc.CompareTo(fromInc), 0);
            Less(toInc, fromEx);
            Less(fromEx, pInf);

            IsTrue(eTo <= eFrom);
            IsTrue(eFrom < nInf);
            IsTrue(nInf < toEx);
            IsTrue(toEx < fromInc);
            IsTrue(toInc <= fromInc && toInc >= fromInc);
            IsTrue(fromInc < fromEx);
            IsTrue(fromEx < pInf);

            IsTrue(pInf >= fromEx);
            IsTrue(fromEx >= fromInc);
            IsTrue(fromInc >= toInc);
            IsTrue(fromInc >= toEx);
            IsTrue(toEx >= nInf);
            IsTrue(nInf >= eFrom);
            IsTrue(eFrom >= eTo);
        }
コード例 #11
0
        /// <summary>
        /// Converts sequence of elements to the composite range using only From boundary.
        /// The To boundary value is taken from the next item in sequence (+∞ for the last item in sequence)
        /// </summary>
        /// <typeparam name="TSource">The type of the values in original collection.</typeparam>
        /// <typeparam name="T">The type of the range values.</typeparam>
        /// <typeparam name="TKey">The type of the range key</typeparam>
        /// <param name="source">Original collection.</param>
        /// <param name="fromValueSelector">Callback to obtain a value for the From boundary.</param>
        /// <param name="keySelector">Callback to obtain a value for the range key.</param>
        /// <returns>A new composite range with keys filled from the original collection.</returns>
        public static CompositeRange <T, TKey> ToCompositeRangeFrom <TSource, T, TKey>(
            [NotNull] this IEnumerable <TSource> source,
            [NotNull, InstantHandle] Func <TSource, T> fromValueSelector,
            [NotNull, InstantHandle] Func <TSource, TKey> keySelector)
        {
            var keyAndFromBoundary = source
                                     .Select(s => (From: Range.BoundaryFrom(fromValueSelector(s)), Key: keySelector(s)))
                                     .OrderBy(s => s.From)
                                     .ToArray();

            if (keyAndFromBoundary.Length == 0)
            {
                return(CompositeRange <T, TKey> .Empty);
            }

            // logic is following:
            // foreach item in sequence
            //   if same boundary as before - add to pending
            //   else add all (pending to current) ranges. Store current as pending.

            var prevBoundary = RangeBoundaryFrom <T> .Empty;
            var prevKeys     = new List <TKey>();
            var ranges       = new List <Range <T, TKey> >();

            foreach (var fromWithKey in keyAndFromBoundary)
            {
                if (prevBoundary != fromWithKey.From)
                {
                    foreach (var prevKey in prevKeys)
                    {
                        ranges.Add(Range.Create(prevBoundary, fromWithKey.From.GetComplementation(), prevKey));
                    }
                    prevKeys.Clear();
                    prevBoundary = fromWithKey.From;
                }

                prevKeys.Add(fromWithKey.Key);
            }

            foreach (var prevKey in prevKeys)
            {
                ranges.Add(Range.Create(prevBoundary, RangeBoundaryTo <T> .PositiveInfinity, prevKey));
            }

            return(ranges.ToCompositeRange());
        }
コード例 #12
0
        public static void TestRangeTrimFrom()
        {
            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.TrimFrom(null), range);
            AreEqual(range.TrimFrom(double.NegativeInfinity), range);
            Throws <ArgumentException>(() => range.TrimFrom(double.PositiveInfinity));
            AreEqual(range.TrimFrom(RangeBoundaryFrom <double?> .Empty), emptyRange);
            AreEqual(range.TrimFrom(0), range);
            AreEqual(range.TrimFrom(1), range);
            AreEqual(range.TrimFrom(1.5), Range.Create(1.5, value2));
            AreEqual(range.TrimFrom(2), Range.Create(2, value2));
            AreEqual(range.TrimFrom(3), emptyRange);

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

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

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

            range = Range.CreateExclusive(value1, value2);
            AreEqual(range.TrimFrom(Range.BoundaryFrom <double?>(1)), range);
            AreEqual(range.TrimFrom(Range.BoundaryFrom <double?>(1.5)), Range.CreateExclusiveTo(1.5, value2));
            AreEqual(range.TrimFrom(Range.BoundaryFrom <double?>(2)), emptyRange);
        }
コード例 #13
0
        public static void TestBoundaryToString()
        {
            int?value1 = 1;

            AreEqual(RangeBoundaryFrom <int?> .Empty.ToString(), "∅");
            AreEqual(RangeBoundaryTo <int?> .Empty.ToString(), "∅");
            AreEqual(RangeBoundaryFrom <int?> .NegativeInfinity.ToString(), "(-∞");
            AreEqual(RangeBoundaryTo <int?> .PositiveInfinity.ToString(), "+∞)");

            AreEqual(Range.BoundaryFrom(value1).ToString(), "[1");
            AreEqual(Range.BoundaryFromExclusive(value1).ToString(), "(1");
            AreEqual(Range.BoundaryTo(value1).ToString(), "1]");
            AreEqual(Range.BoundaryToExclusive(value1).ToString(), "1)");

            AreEqual(Range.BoundaryToExclusive(value1).ToString("000"), "001)");
            AreEqual(RangeBoundaryTo <int?> .Empty.ToString("000"), "∅");
            AreEqual(RangeBoundaryFrom <int?> .NegativeInfinity.ToString("000"), "(-∞");
        }
コード例 #14
0
        public static void TestKeyedRangeExtendFrom()
        {
            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.ExtendFrom(null), Range.Create(empty, value2, RangeKey));
            AreEqual(range.ExtendFrom(double.NegativeInfinity), Range.Create(empty, value2, RangeKey));
            Throws <ArgumentException>(() => range.ExtendFrom(double.PositiveInfinity));
            AreEqual(range.ExtendFrom(RangeBoundaryFrom <double?> .Empty), range);
            AreEqual(range.ExtendFrom(0), Range.Create(0, value2, RangeKey));
            AreEqual(range.ExtendFrom(1), range);
            AreEqual(range.ExtendFrom(1.5), range);
            AreEqual(range.ExtendFrom(2), range);
            AreEqual(range.ExtendFrom(3), range);

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

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

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

            range = Range.CreateExclusive(value1, value2, RangeKey);
            AreEqual(range.ExtendFrom(Range.BoundaryFrom <double?>(1)), Range.CreateExclusiveTo(1, value2, RangeKey));
            AreEqual(range.ExtendFrom(Range.BoundaryFromExclusive <double?>(1)), range);
            AreEqual(range.ExtendFrom(Range.BoundaryFromExclusive <double?>(0)), Range.CreateExclusive(0, value2, RangeKey));
        }
コード例 #15
0
        public void TestBoundaryFromCreation()
        {
            int?value1 = 1;
            int?value2 = 2;
            int?empty  = null;

            DoesNotThrow(() => new RangeBoundaryFrom <int?>(empty, RangeBoundaryFromKind.Empty));
            DoesNotThrow(() => new RangeBoundaryFrom <int?>(empty, RangeBoundaryFromKind.Infinite));
            DoesNotThrow(() => new RangeBoundaryFrom <int?>(value1, RangeBoundaryFromKind.Exclusive));
            DoesNotThrow(() => new RangeBoundaryFrom <int?>(value2, RangeBoundaryFromKind.Inclusive));
            DoesNotThrow(() => new RangeBoundaryFrom <double?>(double.NaN, RangeBoundaryFromKind.Empty));

            Throws <ArgumentException>(() => new RangeBoundaryFrom <int?>(value1, RangeBoundaryFromKind.Empty));
            Throws <ArgumentException>(() => new RangeBoundaryFrom <int?>(value2, RangeBoundaryFromKind.Infinite));
            Throws <ArgumentException>(() => new RangeBoundaryFrom <int?>(empty, RangeBoundaryFromKind.Inclusive));
            Throws <ArgumentException>(() => new RangeBoundaryFrom <int?>(empty, RangeBoundaryFromKind.Exclusive));
            Throws <ArgumentException>(() => new RangeBoundaryFrom <double?>(double.NaN, RangeBoundaryFromKind.Exclusive));

            AreEqual(
                RangeBoundaryFrom <int?> .NegativeInfinity,
                Range.BoundaryFromInfinity <int?>());
            AreEqual(
                RangeBoundaryFrom <int?> .NegativeInfinity,
                Range.BoundaryFrom(empty));
            AreEqual(
                RangeBoundaryFrom <int?> .NegativeInfinity,
                Range.BoundaryFromExclusive(empty));
            AreEqual(
                new RangeBoundaryFrom <int?>(value1, RangeBoundaryFromKind.Inclusive),
                Range.BoundaryFrom(value1));
            AreEqual(
                new RangeBoundaryFrom <int?>(value1, RangeBoundaryFromKind.Exclusive),
                Range.BoundaryFromExclusive(value1));
            AreNotEqual(
                new RangeBoundaryFrom <int?>(value1, RangeBoundaryFromKind.Exclusive),
                Range.BoundaryFromExclusive(value2));
            AreNotEqual(
                RangeBoundaryFrom <int> .NegativeInfinity,
                Range.BoundaryFrom(0));
            AreEqual(
                new RangeBoundaryFrom <int?>(value1, RangeBoundaryFromKind.Exclusive),
                Range.BoundaryFromExclusive(value1));
        }
コード例 #16
0
        public static void TestBoundaryToEquality()
        {
            int?value1 = 1;
            int?value2 = 2;
            int?empty  = null;

            var e   = new RangeBoundaryTo <int?>();
            var inf = Range.BoundaryTo(empty);
            var a1  = Range.BoundaryTo(value1);
            var a12 = Range.BoundaryTo(value1);
            var a2  = Range.BoundaryTo(value2);
            var b1  = Range.BoundaryFrom(value1);

            AreEqual(e, RangeBoundaryTo <int?> .Empty);
            IsTrue(e == RangeBoundaryTo <int?> .Empty);
            IsFalse(e != RangeBoundaryTo <int?> .Empty);

            AreEqual(inf, RangeBoundaryTo <int?> .PositiveInfinity);
            IsTrue(inf == RangeBoundaryTo <int?> .PositiveInfinity);

            AreNotEqual(a1, empty);
            AreNotEqual(a1, inf);

            AreEqual(a1, a12);
            IsTrue(a1 == a12);
            IsFalse(a1 != a12);

            AreNotEqual(a1, a2);
            IsFalse(a1 == a2);
            IsTrue(a1 != a2);

            AreEqual(a1.Value, value1);
            AreNotEqual(a1.Value, value2);
            AreNotEqual(a1, value1);
            AreNotEqual(a1, value2);

            AreNotEqual(a1, b1);
            AreEqual(b1.Value, value1);
        }
コード例 #17
0
        public void TestBoundaryFromWithValue()
        {
            int?value1 = 1;
            int?value2 = 2;

            var boundary  = Range.BoundaryFrom(value1);
            var boundary2 = boundary.WithValue(i => i + 1);

            AreEqual(boundary.Kind, boundary2.Kind);
            AreEqual(boundary2.Value, value2);

            boundary  = Range.BoundaryFromExclusive(value1);
            boundary2 = boundary.WithValue(i => i + 1);
            AreEqual(boundary.Kind, boundary2.Kind);
            AreEqual(boundary2.Value, value2);

            boundary  = RangeBoundaryFrom <int?> .Empty;
            boundary2 = boundary.WithValue(i => i + 1);
            AreEqual(boundary, boundary2);
            boundary  = RangeBoundaryFrom <int?> .NegativeInfinity;
            boundary2 = boundary.WithValue(i => i + 1);
            AreEqual(boundary, boundary2);
        }
コード例 #18
0
        public void TestBoundaryFromToInclusive()
        {
            int?value1 = 1;
            int?empty  = null;

            var boundary  = RangeBoundaryFrom <int?> .Empty;
            var boundary2 = boundary.ToInclusive();

            IsTrue(boundary2.IsEmpty);

            boundary  = Range.BoundaryFrom(empty);
            boundary2 = boundary.ToInclusive();
            IsTrue(boundary2.IsNegativeInfinity);

            boundary  = Range.BoundaryFrom(value1);
            boundary2 = boundary.ToInclusive();
            AreEqual(boundary2.Value, boundary.Value);
            AreEqual(boundary2.Kind, RangeBoundaryFromKind.Inclusive);

            boundary  = Range.BoundaryFromExclusive(value1);
            boundary2 = boundary.ToInclusive();
            AreEqual(boundary2.Value, boundary.Value);
            AreEqual(boundary2.Kind, RangeBoundaryFromKind.Inclusive);
        }
コード例 #19
0
        public Range <T, TKey> MakeInclusive(
            [NotNull, InstantHandle] Func <T, T> fromValueSelector,
            [NotNull, InstantHandle] Func <T, T> toValueSelector)
        {
            if (IsEmpty || (!From.IsExclusiveBoundary && !To.IsExclusiveBoundary))
            {
                return(this);
            }

            var from = From;

            if (from.IsExclusiveBoundary)
            {
                from = Range.BoundaryFrom(fromValueSelector(from.GetValueOrDefault()));
            }
            var to = To;

            if (to.IsExclusiveBoundary)
            {
                to = Range.BoundaryTo(toValueSelector(to.GetValueOrDefault()));
            }

            return(TryCreateRange(from, to));
        }
コード例 #20
0
 public static Range <T> ExtendFrom <T>(this Range <T> range, T from) =>
 ExtendFrom(range, Range.BoundaryFrom(from));
コード例 #21
0
 public static Range <T> TrimFrom <T>(this Range <T> range, T from) =>
 TrimFrom(range, Range.BoundaryFrom(from));
コード例 #22
0
 public Range <T, TKey> ExtendFrom(T from) =>
 ExtendFrom(Range.BoundaryFrom(from));
コード例 #23
0
 public static bool StartsAfter <T>(this Range <T> range, T value) =>
 RangeBoundaryFrom <T> .IsValid(value) && range.From > Range.BoundaryFrom(value);
コード例 #24
0
ファイル: Range`1.cs プロジェクト: viktortat/CodeJam
 /// <summary>Creates instance of <seealso cref="Range{T}"/></summary>
 /// <param name="from">Boundary From.</param>
 /// <param name="to">Boundary To.</param>
 public Range(T from, T to) :
     this(Range.BoundaryFrom(from), Range.BoundaryTo(to))
 {
 }
コード例 #25
0
 public static bool Contains <T>(this Range <T> range, T value) =>
 RangeBoundaryFrom <T> .IsValid(value)
                         ? Contains(range, Range.BoundaryFrom(value))
                         : Contains(range, Range.BoundaryTo(value));
コード例 #26
0
ファイル: Range`2.generated.cs プロジェクト: alezhuk/CodeJam
 /// <summary>Creates instance of <seealso cref="Range{T}"/></summary>
 /// <param name="from">Boundary From.</param>
 /// <param name="to">Boundary To.</param>
 /// <param name="key">The value of the range key.</param>
 public Range(T from, T to, TKey key) :
     this(Range.BoundaryFrom(from), Range.BoundaryTo(to), key)
 {
 }
コード例 #27
0
 public Range <T, TKey> TrimFrom(T from) =>
 TrimFrom(Range.BoundaryFrom(from));
コード例 #28
0
        public static void TestKeyedRangeEndsBefore()
        {
            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);

            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, RangeKey);
            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, RangeKey);
            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, RangeKey);
            IsFalse(range.EndsBefore(1));
            IsFalse(range.EndsBefore(1.5));
            IsTrue(range.EndsBefore(2));

            range = Range.CreateExclusive(value1, value2, RangeKey);
            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)));
        }
コード例 #29
0
 /// <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 value From.</param>
 /// <returns>A range trimmed with a new From boundary.</returns>
 public static CompositeRange <T> TrimFrom <T>(this CompositeRange <T> compositeRange, T from) =>
 TrimFrom(compositeRange, Range.BoundaryFrom(from));
コード例 #30
0
 public static CompositeRange <T, TKey> ExtendFrom <T, TKey>(this CompositeRange <T, TKey> compositeRange, T from) =>
 ExtendFrom(compositeRange, Range.BoundaryFrom(from));