Example #1
0
        public void Contains_WithEmptyInterval_ReturnsTrue(int aStart, bool aStartInclusive, int aEnd, bool aEndInclusive, bool bStartInclusive, bool bEndInclusive)
        {
            var intervalA = new IntegerInterval(aStart, aStartInclusive, aStart, aEndInclusive);
            var intervalB = new IntegerInterval(1, bStartInclusive, 0, bEndInclusive);

            var result = intervalA.Contains(intervalB);

            Assert.That(result, Is.True);
        }
Example #2
0
        public void Contains_InDegenerateInterval_ReturnsTrue(bool bStartInclusive, bool bEndInclusively)
        {
            var intervalA = new IntegerInterval(0, true, 0, true);
            var intervalB = new IntegerInterval(0, bStartInclusive, 0, bEndInclusively);

            var result = intervalA.Contains(intervalB);

            Assert.That(result, Is.True);
        }
Example #3
0
        public void GetInverse_ReturnValueHasSameRepeatIntegerIntervalAsOriginal()
        {
            var repeatRange = new IntegerInterval();
            repeatRange.TrySetValue(1, 2);
            var union = new UnionExpressionTest(new List<IExpression>(), _randomGenerator) {RepeatRange = repeatRange};

            var result = union.GetInverse().RepeatRange;

            Assert.AreEqual(repeatRange, result);
        }
Example #4
0
        public void TrySetValue_ReturnsSuccessResult_WhenLowerBoundSmallerThanUpperBound(int?lowerBound, int?upperBound)
        {
            var interval = new IntegerInterval();

            var result = interval.TrySetValue(lowerBound, upperBound);

            Assert.IsTrue(result.IsSuccess);
            Assert.AreEqual(lowerBound, interval.Start);
            Assert.AreEqual(upperBound, interval.End);
        }
Example #5
0
        public void Contains_WithEmptyInterval_ReturnsFalse(int start, int end, bool startInclusive, bool endInclusive, int value)
        {
            Assume.That(start > end);

            var interval = new IntegerInterval(start, startInclusive, end, endInclusive);

            var result = interval.Contains(value);

            Assert.That(result, Is.False);
        }
Example #6
0
        public void Contains_WithEndAdjacentZeroIntervalExcluded_ReturnsFalse(bool aStartInclusive, bool bStartInclusive, bool bEndInclusively)
        {
            var intervalA = new IntegerInterval(0, aStartInclusive, 3, false);
            var intervalB = new IntegerInterval(3, bStartInclusive, 3, bEndInclusively);

            Assume.That(!intervalB.IsEmpty());

            var result = intervalA.Contains(intervalB);

            Assert.That(result, Is.False);
        }
Example #7
0
        public void Contains_FromEmptyInterval_ReturnsFalse(bool aStartInclusive, bool aEndInclusive, int bStart, bool bStartInclusive, int bEnd, bool bEndInclusive)
        {
            var intervalA = new IntegerInterval(1, aStartInclusive, 0, aEndInclusive);
            var intervalB = new IntegerInterval(bStart, bStartInclusive, bEnd, bEndInclusive);

            Assume.That(!intervalB.IsEmpty());

            var result = intervalA.Contains(intervalB);

            Assert.That(result, Is.False);
        }
Example #8
0
        public void IntersectWith_WhenOtherIntervalIsEmpty_ReturnsNull(int aStart, bool aStartInclusive, int aEnd, bool aEndInclusive, int bStartAndEnd, bool bStartInclusive, bool bEndInclusive)
        {
            Assume.That(!bStartInclusive || !bEndInclusive);

            var intervalA = new IntegerInterval(aStart, aStartInclusive, aEnd, aEndInclusive);
            var intervalB = new IntegerInterval(0, false, 0, false);

            var actual = intervalA.IntersectWith(intervalB);

            Assert.That(actual, Is.Null);
        }
Example #9
0
        public void DifferenceWith_WhenOtherIntervalContainedByThis_ReturnsTwoIntervalsThatDontIntersectOther()
        {
            var intervalA = new IntegerInterval(0, true, 3, true);
            var intervalB = new IntegerInterval(1, true, 2, true);

            var actual = intervalA.DifferenceWith(intervalB);

            Assert.That(actual.Count, Is.EqualTo(2));
            Assert.That(actual[0].IntersectWith(intervalB).IsEmpty());
            Assert.That(actual[1].IntersectWith(intervalB).IsEmpty());
        }
Example #10
0
        public void DifferenceWith_WhenWhollyOverlappedExceptEndPoints_ReturnsTwoDegenerateIntervalsForTheEndPoints()
        {
            var intervalA = new IntegerInterval(0, true, 3, true);
            var intervalB = new IntegerInterval(0, false, 3, false);

            var actual = intervalA.DifferenceWith(intervalB);

            Assert.That(actual.Count, Is.EqualTo(2));
            Assert.That(actual[0].Start, Is.EqualTo(actual[0].End));
            Assert.That(actual[1].Start, Is.EqualTo(actual[1].End));
        }
Example #11
0
        public void IntersectWith_WhenIntervalsIntersectInAnExclusivePoint_ReturnsNull(bool aStartInclusive, bool aEndInclusive, bool bStartInclusive, bool bEndInclusive)
        {
            Assume.That(!aEndInclusive || !bStartInclusive);

            var intervalA = new IntegerInterval(0, aStartInclusive, 2, aEndInclusive);
            var intervalB = new IntegerInterval(2, bStartInclusive, 3, bEndInclusive);

            var actual = intervalA.IntersectWith(intervalB);

            Assert.That(actual, Is.Null);
        }
Example #12
0
        public void IntersectWith_WhenIntervalsIntersectInAPoint_ReturnsThatPointWithMatchingInclusivity(bool aStartInclusive, bool bEndInclusive)
        {
            var intervalA = new IntegerInterval(0, aStartInclusive, 2, true);
            var intervalB = new IntegerInterval(2, true, 3, bEndInclusive);

            var actual = intervalA.IntersectWith(intervalB);

            Assert.That(actual.Start, Is.EqualTo(intervalB.Start));
            Assert.That(actual.End, Is.EqualTo(intervalA.End));
            Assert.That(actual.StartInclusive && actual.EndInclusive, Is.EqualTo(true));
        }
Example #13
0
        public void Contains_WithStartAdjacentZeroIntervalExcluded_ReturnsFalse(bool aEndInclusive, bool bStartInclusive, bool bEndInclusively)
        {
            var intervalA = new IntegerInterval(0, false, 3, aEndInclusive);
            var intervalB = new IntegerInterval(0, bStartInclusive, 0, bEndInclusively);

            Assume.That(!intervalB.IsEmpty());

            var result = intervalA.Contains(intervalB);

            Assert.That(result, Is.False);
        }
Example #14
0
        public void Contains_FromEmptyInterval_ReturnsFalse(bool aStartInclusive, bool aEndInclusive, int bStart, bool bStartInclusive, int bEnd, bool bEndInclusive)
        {
            var intervalA = new IntegerInterval(1, aStartInclusive, 0, aEndInclusive);
            var intervalB = new IntegerInterval(bStart, bStartInclusive, bEnd, bEndInclusive);

            Assume.That(!intervalB.IsEmpty());

            var result = intervalA.Contains(intervalB);

            Assert.That(result, Is.False);
        }
Example #15
0
        public void DifferenceWith_WithIntervalsIntersectingAtEndPoint_ReturnsNewIntervalExcludingEnd()
        {
            var intervalA = new IntegerInterval(0, true, 3, true);
            var intervalB = new IntegerInterval(3, true, 3, true);

            var actual = intervalA.DifferenceWith(intervalB);

            var single = actual.Single(); // Asserts that the array contains a single entry.

            Assert.That(single.End, Is.EqualTo(intervalA.End));
            Assert.That(single.EndInclusive, Is.False);
        }
Example #16
0
        public void UnionWith_WhenIntervalsOverlap_ReturnsASingleIntervalThatContainsBothIntervals()
        {
            var intervalA = new IntegerInterval(0, false, 2, false);
            var intervalB = new IntegerInterval(1, false, 3, false);

            var actual = intervalA.UnionWith(intervalB);

            var single = actual.Single(); // Asserts that the array contains a single entry.

            Assert.That(single.Contains(intervalA));
            Assert.That(single.Contains(intervalB));
        }
Example #17
0
        public void Clone_ReturnsDifferentIntervalWithIdenticalValues()
        {
            var interval = new IntegerInterval();

            interval.TrySetValue(1, 5);

            var result = interval.Clone();

            Assert.AreNotEqual(interval, result);
            Assert.AreEqual(1, result.Start);
            Assert.AreEqual(5, result.End);
        }
Example #18
0
        public void IntersectWith_WhenIntervalsIntersectInAIntervalReversed_ReturnsThatIntervalWithMatchingInclusivity(bool aStartInclusive, bool aEndInclusive, bool bStartInclusive, bool bEndInclusive)
        {
            var intervalA = new IntegerInterval(0, aStartInclusive, 2, aEndInclusive);
            var intervalB = new IntegerInterval(1, bStartInclusive, 3, bEndInclusive);

            var actual = intervalB.IntersectWith(intervalA);

            Assert.That(actual.Start, Is.EqualTo(intervalB.Start));
            Assert.That(actual.StartInclusive, Is.EqualTo(intervalB.StartInclusive));
            Assert.That(actual.End, Is.EqualTo(intervalA.End));
            Assert.That(actual.EndInclusive, Is.EqualTo(intervalA.EndInclusive));
        }
Example #19
0
        public void DifferenceWith_WhenOtherOverlapsThisEnd_ReturnsNewIntervalExcludingStart()
        {
            var intervalA = new IntegerInterval(0, true, 2, true);
            var intervalB = new IntegerInterval(1, true, 3, true);

            var actual = intervalA.DifferenceWith(intervalB);

            var single = actual.Single(); // Asserts that the array contains a single entry.

            Assert.That(single.End, Is.EqualTo(intervalB.Start));
            Assert.That(single.EndInclusive, Is.False);
        }
Example #20
0
        public void IntersectWith_WhenIntervalsAreSameSize_ReturnsIntervalWithMostRestrictedInclusivity(bool aStartInclusive, bool aEndInclusive, bool bStartInclusive, bool bEndInclusive)
        {
            var intervalA = new IntegerInterval(0, aStartInclusive, 3, aEndInclusive);
            var intervalB = new IntegerInterval(0, bStartInclusive, 3, bEndInclusive);

            var actual = intervalA.IntersectWith(intervalB);

            Assert.That(actual.Start, Is.EqualTo(intervalB.Start));
            Assert.That(actual.StartInclusive, Is.EqualTo(intervalA.StartInclusive && intervalB.StartInclusive));
            Assert.That(actual.End, Is.EqualTo(intervalA.End));
            Assert.That(actual.EndInclusive, Is.EqualTo(intervalA.EndInclusive && intervalB.EndInclusive));
        }
Example #21
0
        public void IntersectWith_WhenIntervalsAreSameSizeAndThisIntervalMatchesMostRestrictive_ReturnsThisInterval(bool aStartInclusive, bool aEndInclusive, bool bStartInclusive, bool bEndInclusive)
        {
            Assume.That(bStartInclusive || bEndInclusive);
            Assume.That(bStartInclusive || (!bStartInclusive && !aStartInclusive));
            Assume.That(bEndInclusive || (!bEndInclusive && !aEndInclusive));

            var intervalA = new IntegerInterval(0, aStartInclusive, 3, aEndInclusive);
            var intervalB = new IntegerInterval(0, bStartInclusive, 3, bEndInclusive);

            var actual = intervalA.IntersectWith(intervalB);

            Assert.That(actual, Is.EqualTo(intervalA));
        }
        public void ToExpansionBounds_OverridesAnyInitialValuesOnLowerAndUpperBound_WhenCalledOnInitialisedBounds()
        {
            var lowerBound = 12;
            var upperBound = lowerBound + 1;
            var interval   = new IntegerInterval();

            interval.TrySetValue(1, 2);

            interval.ToExpansionBounds(out lowerBound, out upperBound);

            Assert.AreEqual(1, lowerBound);
            Assert.AreEqual(2, upperBound);
        }
Example #23
0
        public void UnionWith_WhenIntervalsIntersectAtAnIncludedPoint_ReturnsASingleIntervalThatContainsBothIntervals(bool aEndInclusive, bool bStartInclusive)
        {
            Assume.That(aEndInclusive || bStartInclusive);

            var intervalA = new IntegerInterval(0, false, 2, aEndInclusive);
            var intervalB = new IntegerInterval(2, bStartInclusive, 3, false);

            var actual = intervalA.UnionWith(intervalB);

            var single = actual.Single(); // Asserts that the array contains a single entry.

            Assert.That(single.Contains(intervalA));
            Assert.That(single.Contains(intervalB));
        }
Example #24
0
        public void DifferenceWith_WhenSetEsExcluded_ReturnsEmpty()
        {
            var set = new[]
            {
                new IntegerInterval(0, true, 2, true),
                new IntegerInterval(2, false, 3, false),
            };

            var interval = new IntegerInterval(0, true, 3, true);

            var actual = set.DifferenceWith(interval);

            Assert.That(actual.IsEmpty());
        }
Example #25
0
        public void ToRegexString_CallsRandomGeneratorWithExpectedUpperBound_WhenIntegerIntervalsUpperBoundHasSpecifiedValue(int?value, int expectedValue)
        {
            var interval = new IntegerInterval();

            interval.TrySetValue(-2, value);

            var expression = new BaseExpressionTest(_randomGenerator)
            {
                RepeatRange = interval
            };

            expression.ToStringBuilder();

            _randomGenerator.Received(1).GetNextInteger(0, expectedValue);
        }
Example #26
0
        public void UnionWith_SimplifiesSet_ReturnsOtherInterval()
        {
            var set = new[]
            {
                new IntegerInterval(0, true, 1, false),
                new IntegerInterval(2, true, 3, false),
            };

            var interval = new IntegerInterval(1, true, 2, false);

            var actual = set.UnionWith(interval);

            var single = actual.Single(); // Asserts that the array contains a single entry.

            Assert.That(single.Start, Is.EqualTo(0));
            Assert.That(single.End, Is.EqualTo(3));
        }
        private UnionExpression GetAllCharacterClassInversesUpToMaxInverseLength()
        {
            var minimalLengthTwoRange = new IntegerInterval();

            minimalLengthTwoRange.TrySetValue(2, _maxInverseLength);

            var atLeastLengthTwoWords = new CharacterClassExpression(_alphabet, _random, _maxInverseLength)
            {
                RepeatRange = minimalLengthTwoRange
            };

            atLeastLengthTwoWords.AddCharacters(_alphabet.GetAllCharacters());

            var inverseExpressions = new List <IExpression>
            {
                GetComplement(),
                atLeastLengthTwoWords
            };

            return(new UnionExpression(inverseExpressions, _random));
        }
Example #28
0
        protected override IntegerInterval GetMaxExpansionInterval()
        {
            var maxExpansionLength = 0;
            var minExpansionLength = 0;

            foreach (var elementaryExpression in _elementaryExpressions)
            {
                elementaryExpression.MaxExpansionRange.ToExpansionBounds(out var baseMinLength, out var baseMaxLength);

                elementaryExpression.RepeatRange.ToExpansionBounds(out var minExpansionFactor, out var maxExpansionFactor);

                if (!IsExpansionFactorValid(baseMaxLength, maxExpansionFactor, maxExpansionLength))
                {
                    maxExpansionLength = int.MaxValue;
                }
                else
                {
                    maxExpansionLength += maxExpansionFactor * baseMaxLength;
                }

                if (!IsExpansionFactorValid(baseMinLength, minExpansionFactor, minExpansionLength))
                {
                    minExpansionLength = int.MaxValue;
                }
                else
                {
                    minExpansionLength += minExpansionFactor * baseMinLength;
                }
            }

            var maxExpansionInterval = new IntegerInterval();

            maxExpansionInterval.TrySetValue(minExpansionLength, maxExpansionLength);

            return(maxExpansionInterval);
        }
Example #29
0
        protected override IntegerInterval GetMaxExpansionInterval()
        {
            var minExpansion = 0;
            var maxExpansion = int.MaxValue;

            foreach (var concatExpression in _concatExpressions)
            {
                concatExpression.MaxExpansionRange.ToExpansionBounds(out var minExpansionLength, out var maxExpansionLength);

                if (minExpansionLength > maxExpansion || maxExpansionLength < minExpansion)
                {
                    return(new IntegerInterval(0));
                }

                if (minExpansion < minExpansionLength)
                {
                    minExpansion = minExpansionLength;
                }

                if (maxExpansionLength < maxExpansion)
                {
                    maxExpansion = maxExpansionLength;
                }

                if (minExpansion == 0 && maxExpansion == 0)
                {
                    return(new IntegerInterval(0));
                }
            }

            var minExpansionInterval = new IntegerInterval();

            minExpansionInterval.TrySetValue(minExpansion, maxExpansion);

            return(minExpansionInterval);
        }
Example #30
0
        public void IsEmpty_WithInOrderSetSet_ReturnsFalse(int start, int end, bool startInclusive, bool endInclusive)
        {
            Assume.That(start < end);

            var intervalA = new IntegerInterval(start, startInclusive, end, endInclusive);

            var actual = intervalA.IsEmpty();

            Assert.That(actual, Is.False);
        }
Example #31
0
        public void Contains_WithEndAdjacentZeroIntervalIncluded_ReturnsTrue(bool aStartInclusive, bool bStartInclusive, bool bEndInclusively)
        {
            var intervalA = new IntegerInterval(0, aStartInclusive, 3, true);
            var intervalB = new IntegerInterval(3, bStartInclusive, 3, bEndInclusively);

            var result = intervalA.Contains(intervalB);

            Assert.That(result, Is.True);
        }
Example #32
0
        public void IsEmpty_WithBackwardsSet_ReturnsTrue(int start, int end, bool startInclusive, bool endInclusive)
        {
            Assume.That(start > end);

            var intervalA = new IntegerInterval(start, startInclusive, end, endInclusive);

            var actual = intervalA.IsEmpty();

            Assert.That(actual, Is.True);
        }
Example #33
0
        public void UnionWith_WhenIntervalsOverlap_ReturnsASingleIntervalThatContainsBothIntervals()
        {
            var intervalA = new IntegerInterval(0, false, 2, false);
            var intervalB = new IntegerInterval(1, false, 3, false);

            var actual = intervalA.UnionWith(intervalB);

            var single = actual.Single(); // Asserts that the array contains a single entry.
            Assert.That(single.Contains(intervalA));
            Assert.That(single.Contains(intervalB));
        }
Example #34
0
        public void TrySetValue_ReturnsFailureResult_WhenLowerBoundLargerThanUpperBound()
        {
            var result = new IntegerInterval().TrySetValue(1, -2);

            Assert.IsFalse(result.IsSuccess);
        }
Example #35
0
        public void IsEmpty_WithSelfExcludedSet_ReturnsTrue(int startAndEnd, bool startInclusive, bool endInclusive)
        {
            Assume.That(!startInclusive || !endInclusive);

            var intervalA = new IntegerInterval(startAndEnd, startInclusive, startAndEnd, endInclusive);

            var actual = intervalA.IsEmpty();

            Assert.That(actual, Is.True);
        }
Example #36
0
        public void UnionWith_WhenIntervalsIntersectAtAnExcludedPoint_ReturnsBothIntervals()
        {
            var intervalA = new IntegerInterval(0, false, 2, false);
            var intervalB = new IntegerInterval(2, false, 3, false);

            var actual = intervalA.UnionWith(intervalB);

            Assert.That(actual, Is.EquivalentTo(new[] { intervalA, intervalB }));
        }
Example #37
0
        public static void ToLowerExpansionBound(this IntegerInterval interval, out int lowerBound)
        {
            var repeatRange = interval ?? new IntegerInterval(1);

            lowerBound = repeatRange.Start == null || repeatRange.Start < 0 ? 0 : repeatRange.Start.Value;
        }
Example #38
0
        public void IntersectWith_WhenIntervalsIntersectInAnExclusivePoint_ReturnsNull(bool aStartInclusive, bool aEndInclusive, bool bStartInclusive, bool bEndInclusive)
        {
            Assume.That(!aEndInclusive || !bStartInclusive);

            var intervalA = new IntegerInterval(0, aStartInclusive, 2, aEndInclusive);
            var intervalB = new IntegerInterval(2, bStartInclusive, 3, bEndInclusive);

            var actual = intervalA.IntersectWith(intervalB);

            Assert.That(actual, Is.Null);
        }
Example #39
0
        public void IntersectWith_WhenIntervalsDoNotIntersect_ReturnsNull(bool aStartInclusive, bool aEndInclusive, bool bStartInclusive, bool bEndInclusive)
        {
            var intervalA = new IntegerInterval(0, aStartInclusive, 1, aEndInclusive);
            var intervalB = new IntegerInterval(2, bStartInclusive, 3, bEndInclusive);

            var actual = intervalA.IntersectWith(intervalB);

            Assert.That(actual, Is.Null);
        }
Example #40
0
        public void IntersectWith_WhenIntervalsIntersectInAIntervalReversed_ReturnsThatIntervalWithMatchingInclusivity(bool aStartInclusive, bool aEndInclusive, bool bStartInclusive, bool bEndInclusive)
        {
            var intervalA = new IntegerInterval(0, aStartInclusive, 2, aEndInclusive);
            var intervalB = new IntegerInterval(1, bStartInclusive, 3, bEndInclusive);

            var actual = intervalB.IntersectWith(intervalA);

            Assert.That(actual.Start, Is.EqualTo(intervalB.Start));
            Assert.That(actual.StartInclusive, Is.EqualTo(intervalB.StartInclusive));
            Assert.That(actual.End, Is.EqualTo(intervalA.End));
            Assert.That(actual.EndInclusive, Is.EqualTo(intervalA.EndInclusive));
        }
Example #41
0
        public void IntersectWith_WhenIntervalsAreSameSize_ReturnsIntervalWithMostRestrictedInclusivity(bool aStartInclusive, bool aEndInclusive, bool bStartInclusive, bool bEndInclusive)
        {
            var intervalA = new IntegerInterval(0, aStartInclusive, 3, aEndInclusive);
            var intervalB = new IntegerInterval(0, bStartInclusive, 3, bEndInclusive);

            var actual = intervalA.IntersectWith(intervalB);

            Assert.That(actual.Start, Is.EqualTo(intervalB.Start));
            Assert.That(actual.StartInclusive, Is.EqualTo(intervalA.StartInclusive && intervalB.StartInclusive));
            Assert.That(actual.End, Is.EqualTo(intervalA.End));
            Assert.That(actual.EndInclusive, Is.EqualTo(intervalA.EndInclusive && intervalB.EndInclusive));
        }
Example #42
0
        public void IntersectWith_WhenIntervalsAreSameSizeAndThisIntervalMatchesMostRestrictive_ReturnsThisInterval(bool aStartInclusive, bool aEndInclusive, bool bStartInclusive, bool bEndInclusive)
        {
            Assume.That(bStartInclusive || bEndInclusive);
            Assume.That(bStartInclusive || (!bStartInclusive && !aStartInclusive));
            Assume.That(bEndInclusive || (!bEndInclusive && !aEndInclusive));

            var intervalA = new IntegerInterval(0, aStartInclusive, 3, aEndInclusive);
            var intervalB = new IntegerInterval(0, bStartInclusive, 3, bEndInclusive);

            var actual = intervalA.IntersectWith(intervalB);

            Assert.That(actual, Is.EqualTo(intervalA));
        }
Example #43
0
        public void DifferenceWith_WithIntervalsIntersectingAtStartPoint_ReturnsNewIntervalExcludingStart()
        {
            var intervalA = new IntegerInterval(0, true, 3, true);
            var intervalB = new IntegerInterval(0, true, 0, true);

            var actual = intervalA.DifferenceWith(intervalB);

            var single = actual.Single(); // Asserts that the array contains a single entry.
            Assert.That(single.Start, Is.EqualTo(intervalA.Start));
            Assert.That(single.StartInclusive, Is.False);
        }
Example #44
0
        public void IsEmpty_WithNonSelfExcludedSet_ReturnsFalse(int startAndEnd)
        {
            var intervalA = new IntegerInterval(startAndEnd, true, startAndEnd, true);

            var actual = intervalA.IsEmpty();

            Assert.That(actual, Is.False);
        }
Example #45
0
        public void IntersectWith_WhenIntervalsIntersectInAPoint_ReturnsThatPointWithMatchingInclusivity(bool aStartInclusive, bool bEndInclusive)
        {
            var intervalA = new IntegerInterval(0, aStartInclusive, 2, true);
            var intervalB = new IntegerInterval(2, true, 3, bEndInclusive);

            var actual = intervalA.IntersectWith(intervalB);

            Assert.That(actual.Start, Is.EqualTo(intervalB.Start));
            Assert.That(actual.End, Is.EqualTo(intervalA.End));
            Assert.That(actual.StartInclusive && actual.EndInclusive, Is.EqualTo(true));
        }
Example #46
0
        public IParseResult <IntegerInterval> TryParseIntegerInterval(IStringStream stream, ParseIntervalSettings settings = null)
        {
            if (stream == null)
            {
                throw new ArgumentNullException();
            }

            settings = settings ?? ParseIntervalSettings.Default;

            var initialPosition = stream.CurrentPosition;
            var popCalls        = 0;

            if (popCalls >= stream.Count || !stream.LookAhead(popCalls).Equals(settings.OpenSymbol))
            {
                return(new FailureParseResult <IntegerInterval>(initialPosition, RegSeedErrorType.IntegerIntervalExpected));
            }

            popCalls++;

            var lowerBound = IsNextCharacterSymbol(settings.Separator, stream, popCalls)
                ? new SuccessParseResult <Tuple <int?, int> >(0, new Tuple <int?, int>(null, popCalls))
                : TryParseIntegerInternal(stream, popCalls, false);

            if (!lowerBound.IsSuccess)
            {
                return(new FailureParseResult <IntegerInterval>(initialPosition + popCalls, RegSeedErrorType.IntegerIntervalExpected));
            }

            popCalls = lowerBound.Value.Item2;

            if (lowerBound.Value.Item1 != null && IsNextCharacterSymbol(settings.CloseSymbol, stream, popCalls))
            {
                return(SingleIntegerInterval(lowerBound.Value.Item1.Value, stream, popCalls));
            }

            if (!IsNextCharacterSymbol(settings.Separator, stream, popCalls))
            {
                return(new FailureParseResult <IntegerInterval>(initialPosition + popCalls, RegSeedErrorType.IntegerIntervalSeparatorExpected));
            }

            popCalls++;

            var upperBound = IsNextCharacterSymbol(settings.CloseSymbol, stream, popCalls)
                ? new SuccessParseResult <Tuple <int?, int> >(0, new Tuple <int?, int>(null, popCalls))
                : TryParseIntegerInternal(stream, popCalls, false);

            if (!upperBound.IsSuccess)
            {
                return(new FailureParseResult <IntegerInterval>(initialPosition, RegSeedErrorType.CharacterRangeExpected));
            }

            popCalls = upperBound.Value.Item2;

            if (popCalls >= stream.Count || !stream.LookAhead(popCalls).Equals(settings.CloseSymbol))
            {
                return(new FailureParseResult <IntegerInterval>(initialPosition, RegSeedErrorType.IntegerIntervalExpected));
            }

            popCalls++;
            CallPop(stream, popCalls);

            var value = new IntegerInterval();

            return(value.TrySetValue(lowerBound.Value.Item1, upperBound.Value.Item1).IsSuccess
                ? new SuccessParseResult <IntegerInterval>(initialPosition, value)
                : (IParseResult <IntegerInterval>) new FailureParseResult <IntegerInterval>(initialPosition, RegSeedErrorType.InvalidInterval));
        }
Example #47
0
        public void IntersectWith_WhenThisIntervalIsEmpty_ReturnsNull(int aStartAndEnd, bool aStartInclusive, bool aEndInclusive, int bStart, bool bStartInclusive, int bEnd, bool bEndInclusive)
        {
            Assume.That(!aStartInclusive || !aEndInclusive);

            var intervalA = new IntegerInterval(aStartAndEnd, aStartInclusive, aStartAndEnd, aEndInclusive);
            var intervalB = new IntegerInterval(bStart, bStartInclusive, bEnd, bEndInclusive);

            var actual = intervalA.IntersectWith(intervalB);

            Assert.That(actual, Is.Null);
        }
Example #48
0
        public void UnionWith_SimplifiesSet_ReturnsOtherInterval()
        {
            var set = new[]
            {
                new IntegerInterval(0, true, 1, false),
                new IntegerInterval(2, true, 3, false),
            };

            var interval = new IntegerInterval(1, true, 2, false);

            var actual = set.UnionWith(interval);

            var single = actual.Single(); // Asserts that the array contains a single entry.
            Assert.That(single.Start, Is.EqualTo(0));
            Assert.That(single.End, Is.EqualTo(3));
        }
Example #49
0
        public void IntersectWith_WithEmptyInterval_ReturnsNull(bool aStartInclusive, bool aEndInclusive, int bStart, bool bStartInclusive, int bEnd, bool bEndInclusive)
        {
            var intervalA = new IntegerInterval(1, aStartInclusive, 0, aEndInclusive);
            var intervalB = new IntegerInterval(bStart, bStartInclusive, bEnd, bEndInclusive);

            var actual = intervalA.IntersectWith(intervalB);

            Assert.That(actual, Is.Null);
        }
Example #50
0
        public void UnionWith_WhenIntervalsIntersectAtAnIncludedPoint_ReturnsASingleIntervalThatContainsBothIntervals(bool aEndInclusive, bool bStartInclusive)
        {
            Assume.That(aEndInclusive || bStartInclusive);

            var intervalA = new IntegerInterval(0, false, 2, aEndInclusive);
            var intervalB = new IntegerInterval(2, bStartInclusive, 3, false);

            var actual = intervalA.UnionWith(intervalB);

            var single = actual.Single(); // Asserts that the array contains a single entry.
            Assert.That(single.Contains(intervalA));
            Assert.That(single.Contains(intervalB));
        }
Example #51
0
        public void IntersectWith_WithSameInterval_ReturnsOriginalReference(bool aStartInclusive, bool aEndInclusive)
        {
            var intervalA = new IntegerInterval(0, aStartInclusive, 3, aEndInclusive);

            var actual = intervalA.IntersectWith(intervalA);

            Assert.That(actual, Is.EqualTo(intervalA));
        }
Example #52
0
        public void UnionWith_WhenThisIntervalContainsOtherInterval_ReturnsThisInterval()
        {
            var intervalA = new IntegerInterval(0, false, 3, false);
            var intervalB = new IntegerInterval(1, false, 2, false);

            var actual = intervalA.UnionWith(intervalB);

            Assert.That(actual, Is.EquivalentTo(new[] { intervalA }));
        }
Example #53
0
        public void IntersectWith_WithWhollyContianedInterval_ReturnsContainedInterval(bool aStartInclusive, bool aEndInclusive, bool bStartInclusive, bool bEndInclusive)
        {
            var intervalA = new IntegerInterval(0, aStartInclusive, 3, aEndInclusive);
            var intervalB = new IntegerInterval(1, bStartInclusive, 2, bEndInclusive);

            var actual = intervalA.IntersectWith(intervalB);

            Assert.That(actual, Is.EqualTo(intervalB));
        }
Example #54
0
        public void UnionWith_WithThisIntervalEmpty_ReturnsOtherInterval()
        {
            var intervalA = new IntegerInterval(0, false, 0, false);
            var intervalB = new IntegerInterval(1, false, 3, false);

            var actual = intervalA.UnionWith(intervalB);

            Assert.That(actual, Is.EquivalentTo(new[] { intervalB }));
        }
Example #55
0
        public void DifferenceWith_WithEmptyInterval_ReturnsThisInterval()
        {
            var intervalA = new IntegerInterval(0, false, 3, false);
            var intervalB = new IntegerInterval(2, false, 2, false);

            var actual = intervalA.DifferenceWith(intervalB);

            Assert.That(actual, Is.EquivalentTo(new[] { intervalA }));
        }
Example #56
0
        public void Contains_WithExclusiveEnd_ReturnsFalse(int start, int end, bool startInclusive)
        {
            Assume.That(start <= end);

            var interval = new IntegerInterval(start, startInclusive, end, false);

            var result = interval.Contains(end);

            Assert.That(result, Is.False);
        }
Example #57
0
        public void DifferenceWith_WhenWhollyOverlappedExceptEndPoints_ReturnsTwoDegenerateIntervalsForTheEndPoints()
        {
            var intervalA = new IntegerInterval(0, true, 3, true);
            var intervalB = new IntegerInterval(0, false, 3, false);

            var actual = intervalA.DifferenceWith(intervalB);

            Assert.That(actual.Count, Is.EqualTo(2));
            Assert.That(actual[0].Start, Is.EqualTo(actual[0].End));
            Assert.That(actual[1].Start, Is.EqualTo(actual[1].End));
        }