Beispiel #1
0
        public static TimeSpan CreateInstanceOfTimeSpan(Random rndGen, CreatorSettings creatorSettings)
        {
            long     temp   = CreateInstanceOfInt64(rndGen, creatorSettings);
            TimeSpan result = TimeSpan.FromTicks(temp);

            return(result);
        }
Beispiel #2
0
        public static Uri CreateInstanceOfUri(Random rndGen, CreatorSettings creatorSettings)
        {
            Uri     result = null;
            UriKind kind;

            try
            {
                string uriString = UriCreator.CreateUri(rndGen, out kind);
                result = new Uri(uriString, kind);
            }
            catch (ArgumentException)
            {
                // From http://msdn.microsoft.com/en-us/library/ms131565.aspx
                // uriKind is invalid.
                result = new Uri("my.schema://*****:*****@my.domain/path1/path2?query1=123&query2=%22hello%22");
            }
            catch (UriFormatException)
            {
                // From http://msdn.microsoft.com/en-us/library/ms131565.aspx
                // uriKind is invalid.
                result = new Uri("my.schema://*****:*****@my.domain/path1/path2?query1=123&query2=%22hello%22");
            }

            return(result);
        }
 public static string CreateRandomCookieName(Random rndGen, CreatorSettings creatorSettings)
 {
     return(PrimitiveCreator.CreateRandomString(rndGen, -1, Token, new CreatorSettings(creatorSettings)
     {
         MinStringLength = 1
     }));
 }
        internal static JObject CreateInstanceOfJObject(Random rndGen, CreatorSettings creatorSettings, int currDepth = 0)
        {
            var obj = new JObject();

            var existingPropertyName = new HashSet <string>();
            var maxPropCount         = rndGen.Next(creatorSettings.MaxListLength);

            int currPropCount = 0;

            while (currPropCount < maxPropCount)
            {
                var propertyName = InstanceCreator.CreateInstanceOf <string>(rndGen, new CreatorSettings(creatorSettings)
                {
                    NullValueProbability = 0.0
                });
                if (!existingPropertyName.Contains(propertyName))
                {
                    obj.Add(propertyName, CreateInstanceOfJToken(rndGen, creatorSettings, currDepth + 1));

                    existingPropertyName.Add(propertyName);
                    currPropCount++;
                }
            }

            return(obj);
        }
        private static object CreateInstanceOfListOfT(Type listType, Random rndGen, CreatorSettings creatorSettings)
        {
            Type   type      = listType.GetGenericArguments()[0];
            double rndNumber = rndGen.NextDouble();

            if (rndNumber < creatorSettings.NullValueProbability)
            {
                return(null); // 1% chance of null value
            }

            int size = (int)Math.Pow(creatorSettings.MaxListLength, rndNumber);

            // this will create more small lists than large ones
            if (creatorSettings.AllowEmptyCollection)
            {
                size--;
            }
            object     result    = Activator.CreateInstance(listType);
            MethodInfo addMethod = listType.GetMethod("Add");

            for (int i = 0; i < size; i++)
            {
                addMethod.Invoke(result, new object[] { CreateInstanceOf(type, rndGen, creatorSettings) });
            }

            return(result);
        }
        private static object CreateInstanceOfArray(Type arrayType, Random rndGen, CreatorSettings creatorSettings)
        {
            Type   type      = arrayType.GetElementType();
            double rndNumber = rndGen.NextDouble();

            if (rndNumber < creatorSettings.NullValueProbability)
            {
                return(null); // 1% chance of null value
            }

            int size = (int)Math.Pow(creatorSettings.MaxArrayLength, rndNumber);

            // this will create more small arrays than large ones
            if (creatorSettings.AllowEmptyCollection)
            {
                size--;
            }
            Array result = Array.CreateInstance(type, size);

            for (int i = 0; i < size; i++)
            {
                result.SetValue(CreateInstanceOf(type, rndGen, creatorSettings), i);
            }

            return(result);
        }
Beispiel #7
0
        //[CLSCompliant(false)]
        public static SByte CreateInstanceOfSByte(Random rndGen, CreatorSettings creatorSettings)
        {
            byte[] rndValue = new byte[1];
            rndGen.NextBytes(rndValue);
            SByte result = (SByte)rndValue[0];

            return(result);
        }
Beispiel #8
0
        public static DateTimeOffset CreateInstanceOfDateTimeOffset(Random rndGen, CreatorSettings creatorSettings)
        {
            DateTime temp = CreateInstanceOfDateTime(rndGen, creatorSettings);

            temp = DateTime.SpecifyKind(temp, DateTimeKind.Unspecified);
            int            offsetMinutes = rndGen.Next(-14 * 60, 14 * 60);
            DateTimeOffset result        = new DateTimeOffset(temp, TimeSpan.FromMinutes(offsetMinutes));

            return(result);
        }
Beispiel #9
0
        public static Decimal CreateInstanceOfDecimal(Random rndGen, CreatorSettings creatorSettings)
        {
            int       low             = CreateInstanceOfInt32(rndGen, creatorSettings);
            int       mid             = CreateInstanceOfInt32(rndGen, creatorSettings);
            int       high            = CreateInstanceOfInt32(rndGen, creatorSettings);
            bool      isNegative      = rndGen.Next(2) == 0;
            const int MaxDecimalScale = 28;
            byte      scale           = (byte)rndGen.Next(0, MaxDecimalScale + 1);

            return(new Decimal(low, mid, high, isNegative, scale));
        }
Beispiel #10
0
        public static string CreateInstanceOfString(Random rndGen, CreatorSettings creatorSettings)
        {
            double rndNumber = rndGen.NextDouble();

            if (rndNumber < creatorSettings.NullValueProbability)
            {
                return(null);
            }

            return(CreateRandomString(rndGen, -1, null, creatorSettings));
        }
        public override object CreateInstanceOf(Type type, Random rndGen, CreatorSettings creatorSettings)
        {
            ulong ul;

            do
            {
                ul = PrimitiveCreator.CreateInstanceOfUInt64(rndGen, creatorSettings);
            }while (ul > long.MaxValue);

            return(ul);
        }
        internal static object CreateInstanceOf(Type type, Random rndGen, CreatorSettings creatorSettings)
        {
            if (!creatorSettings.EnterRecursion())
            {
                return(null);
            }

            if (rndGen.NextDouble() < creatorSettings.NullValueProbability && !type.IsValueType)
            {
                return(null);
            }

            // Test convention #1: if the type has a .ctor(Random), call it and the
            // type will initialize itself.
            ConstructorInfo randomCtor = type.GetConstructor(new Type[] { typeof(Random) });

            if (randomCtor != null)
            {
                return(randomCtor.Invoke(new object[] { rndGen }));
            }

            // Test convention #2: if the type has a static method CreateInstance(Random, CreatorSettings), call it and
            // an new instance will be returned
            var createInstanceMtd = type.GetMethod("CreateInstance", BindingFlags.Static | BindingFlags.Public);

            if (createInstanceMtd != null)
            {
                return(createInstanceMtd.Invoke(null, new object[] { rndGen, creatorSettings }));
            }

            // Test convention #3: use the default constructor for classes and set public
            // properties and fields to random values.
            object result = null;

            if (type.IsValueType)
            {
                result = Activator.CreateInstance(type);
            }
            else
            {
                ConstructorInfo defaultCtor = type.GetConstructor(Type.EmptyTypes);
                if (defaultCtor == null)
                {
                    throw new ArgumentException("Type " + type.FullName + " does not have a default constructor.");
                }

                result = defaultCtor.Invoke(new object[0]);
            }

            SetPublicProperties(type, result, rndGen, creatorSettings);
            SetPublicFields(type, result, rndGen, creatorSettings);
            creatorSettings.LeaveRecursion();
            return(result);
        }
Beispiel #13
0
        public virtual string Generate(Random random, CreatorSettings settings)
        {
            this.Random   = random;
            this.Settings = settings;

            depth++;
            var result = GenerateInternal();

            depth--;
            return(result);
        }
        internal static JArray CreateInstanceOfJArray(Random rndGen, CreatorSettings creatorSettings, int currDepth = 0)
        {
            var arr = new JArray();
            var max = rndGen.Next(creatorSettings.MaxArrayLength);

            for (int i = 0; i < max; i++)
            {
                arr.Add(CreateInstanceOfJToken(rndGen, creatorSettings, currDepth + 1));
            }
            return(arr);
        }
Beispiel #15
0
 public static object CreatePrimitiveInstance(Type type, Random rndGen, CreatorSettings creatorSettings)
 {
     if (creators.ContainsKey(type))
     {
         return(creators[type].Invoke(null, new object[] { rndGen, creatorSettings }));
     }
     else
     {
         throw new ArgumentException("Type " + type.FullName + " not supported");
     }
 }
        private static void SetPublicFields(Type type, object obj, Random rndGen, CreatorSettings creatorSettings)
        {
            List <FieldInfo> fields = new List <FieldInfo>(type.GetFields(BindingFlags.Public | BindingFlags.Instance));

            fields.Sort(new Comparison <FieldInfo>(CompareMemberNames));
            foreach (FieldInfo field in fields)
            {
                object fieldValue = InstanceCreator.CreateInstanceOf(field.FieldType, rndGen, creatorSettings);
                field.SetValue(obj, fieldValue);
            }
        }
        private static object CreateInstanceOfNullableOfT(
            Type nullableOfTType, Random rndGen, CreatorSettings creatorSettings)
        {
            if (rndGen.Next(5) == 0)
            {
                return(null);
            }

            Type type = nullableOfTType.GetGenericArguments()[0];

            return(CreateInstanceOf(type, rndGen, creatorSettings));
        }
Beispiel #18
0
        public static DateTime CreateInstanceOfDateTime(Random rndGen, CreatorSettings creatorSettings)
        {
            long temp = CreateInstanceOfInt64(rndGen, creatorSettings);

            temp = Math.Abs(temp);
            DateTime result;

            try
            {
                result = new DateTime(temp % (DateTime.MaxValue.Ticks + 1));
            }
            catch (ArgumentOutOfRangeException)
            {
                // jasonv - approved; specific, commented
                // From http://msdn.microsoft.com/en-us/library/z2xf7zzk.aspx
                // ticks is less than MinValue or greater than MaxValue.
                result = DateTime.Now;
            }

            int kind = rndGen.Next(3);

            switch (kind)
            {
            case 0:
                result = DateTime.SpecifyKind(result, DateTimeKind.Local);
                break;

            case 1:
                result = DateTime.SpecifyKind(result, DateTimeKind.Unspecified);
                break;

            default:
                result = DateTime.SpecifyKind(result, DateTimeKind.Utc);
                break;
            }

            if (!creatorSettings.CreateDateTimeWithSubMilliseconds)
            {
                result = new DateTime(
                    result.Year,
                    result.Month,
                    result.Day,
                    result.Hour,
                    result.Minute,
                    result.Second,
                    result.Millisecond,
                    result.Kind);
            }

            return(result);
        }
Beispiel #19
0
        //[CLSCompliant(false)]
        public static UInt64 CreateInstanceOfUInt64(Random rndGen, CreatorSettings creatorSettings)
        {
            byte[] rndValue = new byte[8];
            rndGen.NextBytes(rndValue);
            UInt64 result = 0;

            for (int i = 0; i < rndValue.Length; i++)
            {
                result = (UInt64)(result << 8);
                result = (UInt64)(result | (UInt64)rndValue[i]);
            }

            return(result);
        }
Beispiel #20
0
        public static Int32 CreateInstanceOfInt32(Random rndGen, CreatorSettings creatorSettings)
        {
            byte[] rndValue = new byte[4];
            rndGen.NextBytes(rndValue);
            Int32 result = 0;

            for (int i = 0; i < rndValue.Length; i++)
            {
                result = (Int32)(result << 8);
                result = (Int32)(result | (Int32)rndValue[i]);
            }

            return(result);
        }
        internal static JValue CreateInstanceOfJValue(Random rndGen, CreatorSettings creatorSettings)
        {
            var rnd = rndGen.Next(20);

            object value;

            switch (rnd)
            {
            case 0:
            case 1:
                value = InstanceCreator.CreateInstanceOf <bool>(rndGen, creatorSettings);
                break;

            case 2:
            case 3:
            case 4:
            case 5:
            case 6:
                value = InstanceCreator.CreateInstanceOf <int>(rndGen, creatorSettings);
                break;

            case 7:
            case 8:
            // workaround for Bug371740
            //value = InstanceCreator.CreateInstanceOf<double>(rndGen, creatorSettings);
            //break;
            case 9:
                value = InstanceCreator.CreateInstanceOf <DateTime>(rndGen, creatorSettings);
                break;

            case 10:
                value = InstanceCreator.CreateInstanceOf <TimeSpan>(rndGen, creatorSettings);
                break;

            case 11:
                value = InstanceCreator.CreateInstanceOf <Guid>(rndGen, creatorSettings);
                break;

            case 12:
                value = InstanceCreator.CreateInstanceOf <Uri>(rndGen, creatorSettings);
                break;

            default:
                value = InstanceCreator.CreateInstanceOf <string>(rndGen, creatorSettings);
                break;
            }

            return(new JValue(value));
        }
        private static object CreateInstanceOfDictionaryOfKAndV(
            Type dictionaryType, Random rndGen, CreatorSettings creatorSettings)
        {
            double nullValueProbability = creatorSettings.NullValueProbability;

            Type[] genericArgs = dictionaryType.GetGenericArguments();
            Type   typeK       = genericArgs[0];
            Type   typeV       = genericArgs[1];
            double rndNumber   = rndGen.NextDouble();

            if (rndNumber < creatorSettings.NullValueProbability)
            {
                return(null); // 1% chance of null value
            }

            int size = (int)Math.Pow(creatorSettings.MaxListLength, rndNumber);

            // this will create more small dictionaries than large ones
            if (creatorSettings.AllowEmptyCollection)
            {
                size--;
            }
            object     result            = Activator.CreateInstance(dictionaryType);
            MethodInfo addMethod         = dictionaryType.GetMethod("Add");
            MethodInfo containsKeyMethod = dictionaryType.GetMethod("ContainsKey");

            for (int i = 0; i < size; i++)
            {
                //Dictionary Keys cannot be null.Set null probability to 0
                creatorSettings.NullValueProbability = 0;
                object newKey = CreateInstanceOf(typeK, rndGen, creatorSettings);
                //Reset null instance probability
                creatorSettings.NullValueProbability = nullValueProbability;
                bool containsKey = (bool)containsKeyMethod.Invoke(result, new object[] { newKey });
                if (!containsKey)
                {
                    object newValue = CreateInstanceOf(typeV, rndGen, creatorSettings);
                    addMethod.Invoke(result, new object[] { newKey, newValue });
                }
            }

            return(result);
        }
Beispiel #23
0
 public static Char CreateInstanceOfChar(Random rndGen, CreatorSettings creatorSettings)
 {
     if (creatorSettings.CreateOnlyAsciiChars)
     {
         return((Char)rndGen.Next(0x20, 0x7F));
     }
     else if (creatorSettings.DontCreateSurrogateChars)
     {
         char c;
         do
         {
             c = (Char)rndGen.Next((int)Char.MinValue, (int)Char.MaxValue);
         }while (Char.IsSurrogate(c));
         return(c);
     }
     else
     {
         return((Char)rndGen.Next((int)Char.MinValue, (int)Char.MaxValue + 1));
     }
 }
        internal static JToken CreateInstanceOfJToken(Random rndGen, CreatorSettings creatorSettings, int currDepth = 0)
        {
            var depth = rndGen.Next(MaxDepth);

            if (currDepth >= depth)
            {
                return(CreateInstanceOfJValue(rndGen, creatorSettings));
            }
            else
            {
                switch (rndGen.Next(3))
                {
                case 0:
                    return(CreateInstanceOfJValue(rndGen, creatorSettings));

                case 1:
                    return(CreateInstanceOfJArray(rndGen, creatorSettings));

                default:
                    return(CreateInstanceOfJObject(rndGen, creatorSettings));
                }
            }
        }
Beispiel #25
0
        public static Single CreateInstanceOfSingle(Random rndGen, CreatorSettings creatorSettings)
        {
            bool   negative = rndGen.Next(2) == 0;
            int    temp     = rndGen.Next(40);
            Single result;

            switch (temp)
            {
            case 0:
                return(Single.NaN);

            case 1:
                return(Single.PositiveInfinity);

            case 2:
                return(Single.NegativeInfinity);

            case 3:
                return(Single.MinValue);

            case 4:
                return(Single.MaxValue);

            case 5:
                return(Single.Epsilon);

            default:
                result = (Single)(rndGen.NextDouble() * 100000);
                if (negative)
                {
                    result = -result;
                }

                return(result);
            }
        }
 public static string CreateRandomCookieValue(Random rndGen, CreatorSettings creatorSettings)
 {
     return(PrimitiveCreator.CreateRandomString(rndGen, -1, CookieOctet, creatorSettings));
 }
        private static NameValueCollection CreateRandomNameValueCollection(Random rndGen, CreatorSettings creatorSettings)
        {
            NameValueCollection col = new NameValueCollection();
            int size = rndGen.Next(20);

            for (int i = 0; i < size; i++)
            {
                col.Add(
                    CreateRandomCookieValue(rndGen, creatorSettings),
                    CreateRandomCookieValue(rndGen, creatorSettings));
            }

            return(col);
        }
 public static string CreateRandomDomain(Random rndGen, CreatorSettings creatorSettings)
 {
     return(DomainSyntax.Generate(rndGen, creatorSettings));
 }
        public static CookieHeaderValue CreateInstanceOfCookieHeaderValue(Random rndGen, CreatorSettings creatorSettings)
        {
            creatorSettings.NullValueProbability = 0.0;
            string name = CreateRandomCookieName(rndGen, creatorSettings);

            CookieHeaderValue header;

            if (rndGen.Next(2) == 0)
            {
                header = new CookieHeaderValue(name, CreateRandomCookieValue(rndGen, creatorSettings));
            }
            else
            {
                header = new CookieHeaderValue(name, CreateRandomNameValueCollection(rndGen, creatorSettings));
            }

            header.Domain   = CreateRandomDomain(rndGen, creatorSettings);
            header.Expires  = InstanceCreator.CreateInstanceOf <DateTimeOffset?>(rndGen, creatorSettings);
            header.HttpOnly = InstanceCreator.CreateInstanceOf <bool>(rndGen, creatorSettings);
            header.MaxAge   = InstanceCreator.CreateInstanceOf <TimeSpan?>(rndGen, creatorSettings);
            header.Secure   = InstanceCreator.CreateInstanceOf <bool>(rndGen, creatorSettings);
            header.Path     = CreateRandomPath(rndGen, creatorSettings);

            return(header);
        }
        private static void SetPublicProperties(Type type, object obj, Random rndGen, CreatorSettings creatorSettings)
        {
            List <PropertyInfo> properties =
                new List <PropertyInfo>(type.GetProperties(BindingFlags.Public | BindingFlags.Instance));

            properties.Sort(new Comparison <PropertyInfo>(CompareMemberNames));
            foreach (PropertyInfo property in properties)
            {
                if (property.CanWrite)
                {
                    object propertyValue = InstanceCreator.CreateInstanceOf(property.PropertyType, rndGen, creatorSettings);
                    property.SetValue(obj, propertyValue, null);
                }
            }
        }