Ejemplo n.º 1
0
        internal override string Generate(Random random)
        {
            int           numRepeat;
            StringBuilder buffer = new StringBuilder();

            if (this == RECompiler.InvalidNode)
            {
                //randomly choose to repeat more or less than the given range
                int repeatMore = random.Next(2);
                if ((mMaxRepeat != -1 && 1 == repeatMore) || mMinRepeat == 0)
                {
                    //repeat more than the given range
                    checked
                    {
                        numRepeat = random.Next(mMaxRepeat + 1, mMaxRepeat + 11);
                    }
                }
                else
                {
                    //repeat less than the given range
                    numRepeat = random.Next(0, mMinRepeat);
                }
            }
            else
            {
                //repeat for some number inside the given range
                checked
                {
                    int maxRepeat = (mMaxRepeat == -1) ? mMinRepeat + extraRepetitions : mMaxRepeat;

                    //don't repeat zero times if the repeated node is on the invalidating path
                    int minRepeat = (mMinRepeat == 0 && mRefNode == mReservedPath) ? 1 : mMinRepeat;

                    numRepeat = (minRepeat < maxRepeat) ? random.Next(minRepeat, maxRepeat + 1) : minRepeat;
                }
            }
            string childStr;

            if (mRefNode is RETextNode) //If the referenced node is text node, only repeat the last character
            {
                childStr = mRefNode.Generate(random);
                buffer.Append(childStr.Substring(0, childStr.Length - 1));
                childStr   = childStr[childStr.Length - 1].ToString(); //Get last character
                mSameValue = true;
            }
            else
            {
                childStr = mRefNode.Generate(random);
            }

            for (int i = 0; i < numRepeat; i++)
            {
                buffer.Append(mSameValue ? childStr : mRefNode.Generate(random));
            }

            return(buffer.ToString());
        }
Ejemplo n.º 2
0
 internal override string Generate(Random random)
 {
     if (mReservedPath != null)
     {
         //call the reserved path
         return(mReservedPath.Generate(random));
     }
     else
     {
         //call a random path
         return(Children[random.Next(Children.Count)].Generate(random));
     }
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Generates a string based on the given regular expression
        /// if any nodes are prepended with \i, then one of these nodes will be chosen
        /// at random to be invalidated
        /// </summary>
        /// <param name="random">Random object to use for generation</param>
        /// <param name="regex">Regular expression used to generate the string</param>
        /// <returns>generated string</returns>
        public static string NextString(Random random, string regex)
        {
            //reset the static variables
            RECompiler.IsInvalidSection = false;
            RECompiler.InvalidNode      = null;
            RECompiler.InvalidableNodes.Clear();

            //construct the RegEx tree
            RECompiler compiler = new RECompiler();
            RENode     node     = compiler.Compile(regex);

            //search for a signal to invalidate a node
            if (regex.IndexOf("\\i", StringComparison.Ordinal) != -1)
            {
                //something should have been invalidated
                //select a node to invalidate
                if (RECompiler.InvalidableNodes.Count == 0)
                {
                    throw new ArgumentException("Asked to generate invalid: Impossible to invalidate");
                }
                RECompiler.InvalidNode = RECompiler.InvalidableNodes[random.Next(RECompiler.InvalidableNodes.Count)];

                //Mark REOrNodes and RERepeatNodes to ensure that the invalid node will be part of the string
                RECompiler.InvalidNode.ReservePath(null);
            }

            //generate and return the string
            string result = node.Generate(random);

            if (RECompiler.InvalidNode != null)
            {
                //confirm that the generated string is invalid (e.g. [a-z]|[^a-z] will always fail)
                Regex compare = new Regex("^" + regex.Replace("\\i", "") + "$");
                if (compare.IsMatch(result))
                {
                    throw new ArgumentException(regex + ": Did not generate invalid string: " + result);
                }
            }

            return(result);
        }
Ejemplo n.º 4
0
 internal override string Generate(Random random)
 {
     return(mRefNode.Generate(random));
 }