Esempio n. 1
0
        /// <summary>
        /// Creates the random string of predefined character types to the specified length.
        /// </summary>
        /// <returns></returns>
        public static string RandomString(int length, StringOptions stringOptions)
        {
            var builder = new StringBuilder(length);

            var actions = new List<Action>();

            if (StringOptions.All == stringOptions || stringOptions.IsSet(StringOptions.Space))
            {
                actions.Add(() => builder.Append(' '));
            }

            if (StringOptions.All == stringOptions || stringOptions.IsSet(StringOptions.AlphaLower))
            {
                actions.Add(() => builder.Append(Convert.ToChar(Convert.ToInt32(Math.Floor(26 * Generator.NextDouble() + 97)))));
            }

            if (StringOptions.All == stringOptions || stringOptions.IsSet(StringOptions.AlphaUpper))
            {
                actions.Add(() => builder.Append(Convert.ToChar(Convert.ToInt32(Math.Floor(26 * Generator.NextDouble() + 65)))));
            }

            if (StringOptions.All == stringOptions || stringOptions.IsSet(StringOptions.Numeric))
            {
                actions.Add(() => builder.Append(Convert.ToChar(Convert.ToInt32(Math.Floor(10 * Generator.NextDouble() + 48)))));
            }

            if (StringOptions.All == stringOptions || stringOptions.IsSet(StringOptions.Punctuation))
            {
                actions.Add(() => builder.Append(RandomPunctuation()));
            }

            if (StringOptions.All == stringOptions || stringOptions.IsSet(StringOptions.Typography))
            {
                actions.Add(() => builder.Append(RandomTypography()));
            }

            if (actions.Count == 0)
            {
                throw new ArgumentException("Unknown string options configuration", "stringOptions");
            }

            for (int i = 0; i < length; i++)
            {
                actions[Generator.Next(0, actions.Count)]();
            }

            return builder.ToString();
        }
Esempio n. 2
0
        /// <summary>
        /// Creates the random string of predefined character types to the specified length
        /// </summary>
        /// <returns></returns>
        public static string RandomString(int length, StringOptions stringOptions)
        {
            StringBuilder builder = new StringBuilder(length);
            Random random = RandomGenerator();

            Action spc = () => builder.Append(' ');
            Action ucc = () => builder.Append(Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65))));
            Action lcc = () => builder.Append(Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 97))));
            Action nmc = () => builder.Append(Convert.ToChar(Convert.ToInt32(Math.Floor(10 * random.NextDouble() + 48))));
            Action puc = () => builder.Append(RandomPunctuation());
            Action tyc = () => builder.Append(RandomTypography());

            List<Action> actions = new List<Action>();

            if (StringOptions.All == stringOptions)
            {
                actions.Add(spc);
                actions.Add(ucc);
                actions.Add(lcc);
                actions.Add(nmc);
                actions.Add(puc);
                actions.Add(tyc);
            }
            else
            {
                if (stringOptions.IsSet(StringOptions.Space))
                {
                    actions.Add(spc);
                }

                if (stringOptions.IsSet(StringOptions.AlphaLower))
                {
                    actions.Add(lcc);
                }

                if (stringOptions.IsSet(StringOptions.AlphaUpper))
                {
                    actions.Add(ucc);
                }

                if (stringOptions.IsSet(StringOptions.Numeric))
                {
                    actions.Add(nmc);
                }

                if (stringOptions.IsSet(StringOptions.Punctuation))
                {
                    actions.Add(puc);
                }

                if (stringOptions.IsSet(StringOptions.Typography))
                {
                    actions.Add(tyc);
                }
            }

            if (actions.Count == 0)
            {
                throw new ArgumentException("Unknown string options configuration", "stringOptions");
            }

            for (int i = 0; i < length; i++)
            {
                actions[random.Next(0, actions.Count)]();
            }

            return builder.ToString();
        }