/// <summary>
        /// Initializes a new instance of the CollectionStructuralDataGenerator class.
        /// </summary>
        /// <param name="itemDataGenerator">The data generator for an item of the collection.</param>
        /// <param name="random">Random number generator.</param>
        /// <param name="minCount">Collection minimum count.</param>
        /// <param name="maxCount">Colleciton maximum count.</param>
        public CollectionStructuralDataGenerator(
            IDataGenerator itemDataGenerator,
            IRandomNumberGenerator random,
            int minCount,
            int maxCount)
        {
            ExceptionUtilities.CheckArgumentNotNull(itemDataGenerator, "itemDataGenerator");
            ExceptionUtilities.CheckArgumentNotNull(random, "random");

            if (minCount < 0)
            {
                throw new TaupoArgumentException("Minimum count cannot be less than 0.");
            }

            ExceptionUtilities.CheckValidRange(minCount, "minCount", maxCount, "maxCount");

            this.itemDataGenerator = itemDataGenerator;
            this.minCount          = minCount;
            this.maxCount          = maxCount;
            this.random            = random;
        }
示例#2
0
        /// <summary>
        /// Generates random string of specified length
        /// </summary>
        /// <param name="random">The random number generator to use.</param>
        /// <param name="minLength">Minimum length of the generated string.</param>
        /// <param name="maxLength">Maximum length of the generated string.</param>
        /// <returns>Generated string.</returns>
        public string GenerateString(IRandomNumberGenerator random, int minLength, int maxLength)
        {
            ExceptionUtilities.CheckArgumentNotNull(random, "random");
            ExceptionUtilities.CheckValidRange(minLength, "minLength", maxLength, "maxLength");

            var alphabet = this.GetWordList();
            int length   = random.NextFromRange(minLength, maxLength);

            StringBuilder sb = new StringBuilder(length);

            while (sb.Length < length)
            {
                sb.Append(random.ChooseFrom(alphabet));
            }

            if (sb.Length > maxLength)
            {
                sb.Length = maxLength;
            }

            return(sb.ToString());
        }
示例#3
0
        private static void AdjustTicksBasedOnFactorAndCheckRange(DataGenerationHint[] hints, ref long minTicks, ref long maxTicks, out long factor)
        {
            factor = GetDateTimeFactor(hints);
            if (factor != 1)
            {
                long roundedMinTicks = (minTicks / factor) * factor;
                long roundedMaxTicks = (maxTicks / factor) * factor;
                if (roundedMinTicks < minTicks)
                {
                    roundedMinTicks += factor;
                }

                if (roundedMaxTicks > maxTicks)
                {
                    roundedMaxTicks -= factor;
                }

                minTicks = roundedMinTicks;
                maxTicks = roundedMaxTicks;
            }

            ExceptionUtilities.CheckValidRange(minTicks, "minimum ticks based on hints", maxTicks, "maximum ticks based on hints");
        }
示例#4
0
        /// <summary>
        /// Binary 'FromSeed' strategy where seed is converted to byte[] representation with the length in the specified range.
        /// </summary>
        /// <param name="minLength">The minimum length.</param>
        /// <param name="maxLength">The maximum length.</param>
        /// <returns>Binary 'FromSeed' delegate.</returns>
        public static Func <long, byte[]> BinaryFromSeed(int minLength, int maxLength)
        {
            ExceptionUtilities.CheckValidRange <int>(minLength, "minLength", maxLength, "maxLength");

            return(delegate(long seed)
            {
                List <byte> values = new List <byte>();
                seed = Math.Abs(seed);

                while (values.Count < maxLength && seed > 0)
                {
                    byte b = (byte)(seed % 256);
                    values.Add(b);
                    seed = seed / 256;
                }

                while (values.Count < minLength)
                {
                    values.Add(0);
                }

                return values.ToArray();
            });
        }
示例#5
0
        /// <summary>
        /// Generates random string of specified length
        /// </summary>
        /// <param name="random">The random number generator to use.</param>
        /// <param name="minLength">Minimum length of the generated string.</param>
        /// <param name="maxLength">Maximum length of the generated string.</param>
        /// <returns>Generated string.</returns>
        public string GenerateString(IRandomNumberGenerator random, int minLength, int maxLength)
        {
            ExceptionUtilities.CheckArgumentNotNull(random, "random");
            ExceptionUtilities.CheckValidRange(minLength, "minLength", maxLength, "maxLength");

            int wordCount = random.NextFromRange(2, 10);
            int length    = random.NextFromRange(minLength, maxLength);

            StringBuilder sb = new StringBuilder();

            while (sb.Length < length)
            {
                string word = this.wordGenerator.GenerateString(random, 2, Math.Max(3, length / wordCount));

                sb.Append(word);
                if (sb.Length < length)
                {
                    sb.Append(' ');
                    if (random.Next(100) < 30)
                    {
                        sb.Append(random.ChooseFrom(glueWords));
                        sb.Append(' ');
                    }
                }
            }

            if (maxLength > 0 && sb.Length > maxLength - 1)
            {
                sb.Length = maxLength - 1;
                sb.Append(".");

                sb[0] = char.ToUpperInvariant(sb[0]);
            }

            return(sb.ToString());
        }
示例#6
0
        private static TData GetNumericMaxValueAndCheckRange <TData>(DataGenerationHint[] hints, TData defaultMinValue, TData defaultMaxValue, out TData minValue)
            where TData : struct, IComparable <TData>
        {
            // For numeric types max and min hints could be of different type,
            // Example: max and min value could be int, while value type is double
            List <double> minValues = new List <double>();
            List <double> maxValues = new List <double>();

            minValues.Add((double)Convert.ChangeType(defaultMinValue, typeof(double), CultureInfo.InvariantCulture));
            maxValues.Add((double)Convert.ChangeType(defaultMaxValue, typeof(double), CultureInfo.InvariantCulture));

            foreach (var hint in hints)
            {
                Type hintType = hint.GetType();
                if (hintType.IsGenericType())
                {
                    Type hintGenericType = hintType.GetGenericTypeDefinition();
                    Type valueType       = hintType.GetGenericArguments()[0];

                    if (numericTypes.Contains(valueType))
                    {
                        List <double> listToAdd = null;
                        if (hintGenericType == typeof(MaxValueHint <>))
                        {
                            listToAdd = maxValues;
                        }
                        else if (hintGenericType == typeof(MinValueHint <>))
                        {
                            listToAdd = minValues;
                        }

                        // not all generic hints are min/max value, so don't assume the list was populated
                        if (listToAdd != null)
                        {
                            object value = hintType.GetProperty("Value").GetValue(hint, null);
                            listToAdd.Add((double)Convert.ChangeType(value, typeof(double), CultureInfo.InvariantCulture));
                        }
                    }
                }
            }

            double doubleMinValue = minValues.Max();
            double doubleMaxValue = maxValues.Min();

            ExceptionUtilities.CheckValidRange(doubleMinValue, "minimum value from hints", doubleMaxValue, "maximum value from hints");
            object convertedMinValue = WorkaroundForConvert(doubleMinValue, typeof(TData));
            object convertedMaxValue = WorkaroundForConvert(doubleMaxValue, typeof(TData));

            if ((double)Convert.ChangeType(convertedMinValue, typeof(double), CultureInfo.InvariantCulture) < doubleMinValue)
            {
                convertedMinValue = Convert.ChangeType(doubleMinValue + 1, typeof(TData), CultureInfo.InvariantCulture);
            }

            if ((double)Convert.ChangeType(convertedMaxValue, typeof(double), CultureInfo.InvariantCulture) > doubleMaxValue)
            {
                convertedMaxValue = Convert.ChangeType(doubleMaxValue - 1, typeof(TData), CultureInfo.InvariantCulture);
            }

            minValue = (TData)convertedMinValue;
            TData maxValue = (TData)convertedMaxValue;

            ExceptionUtilities.CheckValidRange(minValue, "minimum value from hints", maxValue, "maximum value from hints");

            return(maxValue);
        }