Ejemplo n.º 1
0
        protected override IntegerInterval GetMaxExpansionInterval()
        {
            var maxExpansion = 0;
            var minExpansion = int.MaxValue;

            foreach (var intersectExpression in _intersectExpressions)
            {
                intersectExpression.MaxExpansionRange.ToExpansionBounds(out var lowerBound, out var upperBound);

                if (upperBound > maxExpansion)
                {
                    maxExpansion = upperBound;
                }

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

                if (minExpansion == 0 && maxExpansion == int.MaxValue)
                {
                    return(IntegerInterval.MaxInterval);
                }
            }

            var maxExpansionInterval = new IntegerInterval();

            maxExpansionInterval.TrySetValue(minExpansion, maxExpansion);

            return(maxExpansionInterval);
        }
Ejemplo n.º 2
0
        public void GetInverse_ReturnsIntersectExpressionWithTwoSubexpressionsEachHavingRepeatIntervalNull_WhenOriginalExpressionContainsSingleSubExpressionWithRepeatIntervalFrom1To2()
        {
            var repeatInterval = new IntegerInterval();

            repeatInterval.TrySetValue(1, 2);
            var inverseSubExpression = Substitute.For <IExpression>();
            var subExpression        = Substitute.For <IExpression>();

            subExpression.GetInverse().Returns(inverseSubExpression);
            subExpression.RepeatRange.Returns(repeatInterval);
            subExpression.ExpansionLength.Returns(2);
            var concatExpression = new ConcatenationExpression(_randomGenerator);

            concatExpression.Append(subExpression);

            var result = concatExpression.GetInverse();

            Assert.IsInstanceOf <IntersectionExpression>(result);

            var resultAsList = ((IntersectionExpression)result).ToConcatExpressionList();

            Assert.AreEqual(2, resultAsList.Count);
            Assert.IsNull(resultAsList[0].RepeatRange);
            Assert.IsNull(resultAsList[1].RepeatRange);
        }
Ejemplo n.º 3
0
        public void GetInverse_ReturnsIntersectExpressionWithTwoSubexpressionsOfLength3_WhenOriginalExpressionContains3SubExpressionsWithMiddleExpressionOfRepeatIntervalFrom1To2()
        {
            var repeatInterval = new IntegerInterval();

            repeatInterval.TrySetValue(1, 2);
            var inverseSubExpression = Substitute.For <IExpression>();

            inverseSubExpression.GetInverse().Returns(Substitute.For <IExpression>());
            var subExpression = Substitute.For <IExpression>();

            subExpression.GetInverse().Returns(inverseSubExpression);
            subExpression.RepeatRange.Returns(repeatInterval);
            subExpression.ExpansionLength.Returns(2);
            var concatExpression = new ConcatenationExpression(_randomGenerator);

            concatExpression.Append(inverseSubExpression).Append(subExpression).Append(inverseSubExpression);

            var result = concatExpression.GetInverse();

            Assert.IsInstanceOf <IntersectionExpression>(result);

            var resultAsList = ((IntersectionExpression)result).ToConcatExpressionList();

            Assert.AreEqual(2, resultAsList.Count);
            Assert.AreEqual(3, ((UnionExpression)resultAsList[0]).ToIntersectionExpressionList().Count);
            Assert.AreEqual(4, ((UnionExpression)resultAsList[1]).ToIntersectionExpressionList().Count);
        }
Ejemplo n.º 4
0
        protected override IParseResult TryGetTokenWithoutNullCheck(IStringStream inputStream, out IToken token)
        {
            inputStream.Pop();
            var interval = new IntegerInterval();

            interval.TrySetValue(0, 1);
            token = new IntegerIntervalToken(interval, _initialStreamPosition, 1);
            return(new SuccessParseResult());
        }
        public void ToExpansionBounds_UpperBoundIs3_WhenUpperIntervalBoundIs3()
        {
            var interval = new IntegerInterval();

            interval.TrySetValue(1, 3);

            interval.ToExpansionBounds(out _, out var upperBound);

            Assert.AreEqual(3, upperBound);
        }
        public void ToExpansionBounds_LowerBoundIs3_WhenLowerIntervalBoundIs3()
        {
            var interval = new IntegerInterval();

            interval.TrySetValue(3, 7);

            interval.ToExpansionBounds(out var lowerBound, out _);

            Assert.AreEqual(3, lowerBound);
        }
Ejemplo n.º 7
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);
        }
Ejemplo n.º 8
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);
        }
Ejemplo n.º 9
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);
        }
Ejemplo n.º 10
0
        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);
        }
Ejemplo n.º 11
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);
        }
Ejemplo n.º 12
0
        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));
        }
Ejemplo n.º 13
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);
        }
Ejemplo n.º 14
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);
        }
Ejemplo n.º 15
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));
        }