public static string CreateRandomCookieName(Random rndGen, CreatorSettings creatorSettings)
 {
     return(PrimitiveCreator.CreateRandomString(rndGen, -1, Token, new CreatorSettings(creatorSettings)
     {
         MinStringLength = 1
     }));
 }
示例#2
0
        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);
        }
示例#3
0
        public static TimeSpan CreateInstanceOfTimeSpan(Random rndGen, CreatorSettings creatorSettings)
        {
            long     temp   = CreateInstanceOfInt64(rndGen, creatorSettings);
            TimeSpan result = TimeSpan.FromTicks(temp);

            return(result);
        }
        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);
        }
示例#5
0
        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);
        }
示例#6
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);
        }
示例#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);
        }
 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;
 }
示例#9
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);
        }
 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);
     }
 }
        public override object CreateInstanceOf(Type type, Random rndGen, CreatorSettings creatorSettings)
        {
            ulong ul;
            do
            {
                ul = PrimitiveCreator.CreateInstanceOfUInt64(rndGen, creatorSettings);
            }
            while (ul > long.MaxValue);

            return ul;
        }
示例#12
0
        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);
        }
        public override object CreateInstanceOf(Type type, Random rndGen, CreatorSettings creatorSettings)
        {
            ulong ul;

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

            return(ul);
        }
示例#14
0
        public virtual string Generate(Random random, CreatorSettings settings)
        {
            this.Random   = random;
            this.Settings = settings;

            depth++;
            var result = GenerateInternal();

            depth--;
            return(result);
        }
        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);
            }
        }
示例#16
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));
        }
示例#17
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));
        }
示例#18
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");
     }
 }
示例#19
0
        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));
        }
        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 }));
            }

            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);
        }
示例#21
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);
        }
示例#22
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);
        }
 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);
         }
     }
 }
示例#24
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);
        }
示例#25
0
        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));
        }
        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 });
            }

            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;
        }
示例#27
0
        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);
        }
        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);
        }
示例#29
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);
         }
     }
 }
示例#31
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 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;
        }
示例#33
0
        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));
                }
            }
        }
示例#34
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 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;
        }
示例#36
0
 public static Object CreateInstanceOfObject(Random rndGen, CreatorSettings creatorSettings)
 {
     return (rndGen.Next(5) == 0) ? null : new Object();
 }
示例#37
0
 public static Byte CreateInstanceOfByte(Random rndGen, CreatorSettings creatorSettings)
 {
     byte[] rndValue = new byte[1];
     rndGen.NextBytes(rndValue);
     return(rndValue[0]);
 }
示例#38
0
 public static Guid CreateInstanceOfGuid(Random rndGen, CreatorSettings creatorSettings)
 {
     byte[] temp = new byte[16];
     rndGen.NextBytes(temp);
     return(new Guid(temp));
 }
示例#39
0
 public static Boolean CreateInstanceOfBoolean(Random rndGen, CreatorSettings creatorSettings)
 {
     return rndGen.Next(2) == 0;
 }
示例#40
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");
     }
 }
示例#41
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;
        }
示例#42
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);
        }
示例#43
0
 public static Object CreateInstanceOfObject(Random rndGen, CreatorSettings creatorSettings)
 {
     return((rndGen.Next(5) == 0) ? null : new Object());
 }
 public ODataModelTypeCreator()
 {
     _valueToken = new Token(new CharactorSet().Append('a', 'z').Append('A', 'Z').Append('0', '9').ToString());
     _zeroNullProbability = new CreatorSettings() { NullValueProbability = 0.0 };
 }
        private bool GenerateRandomDataForClient(
            Type clientType,
            Type serverType,
            Random rand,
            out object value,
            int? depth = null,
            CreatorSettings settings = null)
        {
            if (clientType == serverType)
            {
                value = InstanceCreator.CreateInstanceOf(clientType, rand, settings);

                return true;
            }
            else if (this.EntityClientTypes.Contains(clientType) ||
                     this.ComplexClientTypes.Contains(clientType))
            {
                value = GenerateClientStructureRandomData(
                            clientType,
                            serverType,
                            rand,
                            depth);

                return true;
            }
            else if (serverType.IsEnum)
            {
                value = InstanceCreator.CreateInstanceOf(serverType, rand).ToString();

                return true;
            }
            else if (clientType.IsGenericType)
            {
                if (clientType.GetGenericTypeDefinition() == typeof(ObservableCollection<>))
                {
                    var values = Activator.CreateInstance(clientType);

                    if (depth <= 2)
                    {
                        var addMethod = clientType.GetMethod("Add");
                        Type elementType = clientType.GetGenericArguments()[0];
                        Type serverElementType = serverType.GetGenericArguments()[0];
                        int length = rand.Next(10);
                        for (int j = 0; j < length; j++)
                        {
                            object elementValue;
                            if (GenerateRandomDataForClient(elementType, serverElementType, rand, out elementValue, depth, new CreatorSettings() { NullValueProbability = 0.0 }))
                            {
                                addMethod.Invoke(values, new object[] { elementValue });
                            }
                        }
                    }

                    value = values;
                    return true;
                }
            }
            else if (this.primitiveTypes.Contains(clientType))
            {
                if (serverType == typeof(ushort)
                    || serverType == typeof(uint)
                    || serverType == typeof(ulong))
                {
                    value = InstanceCreator.CreateInstanceOf<ushort>(rand);
                    return true;
                }
                else if (serverType == typeof(char))
                {
                    value = InstanceCreator.CreateInstanceOf<char>(rand).ToString();
                    return true;
                }
            }

            value = null;
            return false;
        }
        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;
        }
示例#47
0
        public static string CreateRandomString(
            Random rndGen, int size, string charsToUse, CreatorSettings creatorSettings)
        {
            int maxSize = creatorSettings.MaxStringLength;
            int minSize = creatorSettings.MinStringLength;
            // invalid per the XML spec (http://www.w3.org/TR/REC-xml/#charsets), cannot be sent as XML
            const string InvalidXmlChars =
                "\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007\u0008\u000B\u000C\u000E\u000F\u0010\u0011\u0012\u0013\u0014\u0015\u0016\u0017\u0018\u0019\u001A\u001B\u001C\u001D\u001E\u001F\uFFFE\uFFFF";
            const int LowSurrogateMin  = 0xDC00;
            const int LowSurrogateMax  = 0xDFFF;
            const int HighSurrogateMin = 0xD800;
            const int HighSurrogateMax = 0xDBFF;

            if (size < 0)
            {
                double rndNumber = rndGen.NextDouble();
                if (rndNumber < creatorSettings.NullValueProbability)
                {
                    return(null); // 1% chance of null value
                }

                if (maxSize > minSize)
                {
                    size  = (int)Math.Pow(maxSize - minSize, rndNumber); // this will create more small strings than large ones
                    size += minSize;
                }
                else
                {
                    // if minsize equals to maxsize
                    size = maxSize;
                }
            }

            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < size; i++)
            {
                char c;
                if (charsToUse != null)
                {
                    c = charsToUse[rndGen.Next(charsToUse.Length)];
                    sb.Append(c);
                }
                else
                {
                    if (creatorSettings.CreateOnlyAsciiChars || rndGen.Next(2) == 0)
                    {
                        c = (char)rndGen.Next(0x20, 0x7F); // low-ascii chars
                        sb.Append(c);
                    }
                    else
                    {
                        do
                        {
                            c = (char)rndGen.Next((int)char.MinValue, (int)char.MaxValue + 1);
                        }while ((LowSurrogateMin <= c && c <= LowSurrogateMax) || (InvalidXmlChars.IndexOf(c) >= 0));
                        sb.Append(c);
                        if (HighSurrogateMin <= c && c <= HighSurrogateMax)
                        {
                            // need to add a low surrogate
                            c = (char)rndGen.Next(LowSurrogateMin, LowSurrogateMax + 1);
                            sb.Append(c);
                        }
                    }
                }
            }

            return(sb.ToString());
        }
示例#48
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;
 }
示例#49
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;
            }
        }
示例#50
0
 public static DBNull CreateInstanceOfDBNull(Random rndGen, CreatorSettings creatorSettings)
 {
     return (rndGen.Next(2) == 0) ? null : DBNull.Value;
 }
示例#51
0
 public static TimeSpan CreateInstanceOfTimeSpan(Random rndGen, CreatorSettings creatorSettings)
 {
     long temp = CreateInstanceOfInt64(rndGen, creatorSettings);
     TimeSpan result = TimeSpan.FromTicks(temp);
     return result;
 }
示例#52
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;
        }
示例#53
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;
        }
示例#54
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;
 }
示例#55
0
        public static string CreateRandomString(
            Random rndGen, int size, string charsToUse, CreatorSettings creatorSettings)
        {
            int maxSize = creatorSettings.MaxStringLength;
            int minSize = creatorSettings.MinStringLength;
            // invalid per the XML spec (http://www.w3.org/TR/REC-xml/#charsets), cannot be sent as XML
            const string InvalidXmlChars =
                "\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007\u0008\u000B\u000C\u000E\u000F\u0010\u0011\u0012\u0013\u0014\u0015\u0016\u0017\u0018\u0019\u001A\u001B\u001C\u001D\u001E\u001F\uFFFE\uFFFF";
            const int LowSurrogateMin = 0xDC00;
            const int LowSurrogateMax = 0xDFFF;
            const int HighSurrogateMin = 0xD800;
            const int HighSurrogateMax = 0xDBFF;

            if (size < 0)
            {
                double rndNumber = rndGen.NextDouble();
                if (rndNumber < creatorSettings.NullValueProbability)
                {
                    return null; // 1% chance of null value
                }

                if (maxSize > minSize)
                {
                    size = (int)Math.Pow(maxSize - minSize, rndNumber); // this will create more small strings than large ones
                    size += minSize;
                }
                else
                {
                    // if minsize equals to maxsize
                    size = maxSize;
                }
            }

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < size; i++)
            {
                char c;
                if (charsToUse != null)
                {
                    c = charsToUse[rndGen.Next(charsToUse.Length)];
                    sb.Append(c);
                }
                else
                {
                    if (creatorSettings.CreateOnlyAsciiChars || rndGen.Next(2) == 0)
                    {
                        c = (char)rndGen.Next(0x20, 0x7F); // low-ascii chars
                        sb.Append(c);
                    }
                    else
                    {
                        do
                        {
                            c = (char)rndGen.Next((int)char.MinValue, (int)char.MaxValue + 1);
                        }
                        while ((LowSurrogateMin <= c && c <= LowSurrogateMax) || (InvalidXmlChars.IndexOf(c) >= 0));
                        sb.Append(c);
                        if (HighSurrogateMin <= c && c <= HighSurrogateMax)
                        {
                            // need to add a low surrogate
                            c = (char)rndGen.Next(LowSurrogateMin, LowSurrogateMax + 1);
                            sb.Append(c);
                        }
                    }
                }
            }

            return sb.ToString();
        }
示例#56
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);
 }
示例#57
0
 public static Byte CreateInstanceOfByte(Random rndGen, CreatorSettings creatorSettings)
 {
     byte[] rndValue = new byte[1];
     rndGen.NextBytes(rndValue);
     return rndValue[0];
 }
示例#58
0
 public static Guid CreateInstanceOfGuid(Random rndGen, CreatorSettings creatorSettings)
 {
     byte[] temp = new byte[16];
     rndGen.NextBytes(temp);
     return new Guid(temp);
 }
示例#59
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;
        }
示例#60
0
 public static Boolean CreateInstanceOfBoolean(Random rndGen, CreatorSettings creatorSettings)
 {
     return(rndGen.Next(2) == 0);
 }