예제 #1
0
        private static double RandomDouble(Attributes.BaseLimitAttribute baseLimitAttribute = null)
        {
            if (baseLimitAttribute == null)
            {
                return(_random.NextDouble() * (double)_random.Next(Int32.MinValue, Int32.MaxValue));
            }
            if (baseLimitAttribute is Attributes.Double.LimitAttribute limit)
            {
                return(limit.MinimumValue + _random.NextDouble() * (limit.MaximumValue - limit.MinimumValue));
            }

            throw new InvalidAttributeException(typeof(Attributes.Double.LimitAttribute));
        }
예제 #2
0
        private static int RandomInt32(Attributes.BaseLimitAttribute baseLimitAttribute = null)
        {
            if (baseLimitAttribute == null)
            {
                return(_random.Next(Int32.MinValue, Int32.MaxValue));
            }

            if (baseLimitAttribute is Attributes.Int32.LimitAttribute attribute)
            {
                return(_random.Next(attribute.MinimumValue, attribute.MaximumValue));
            }

            throw new InvalidAttributeException(typeof(Attributes.Int32.LimitAttribute));
        }
예제 #3
0
        private static short RandomInt16(Attributes.BaseLimitAttribute baseLimitAttribute = null)
        {
            if (baseLimitAttribute == null)
            {
                return((short)_random.Next((int)short.MinValue, short.MaxValue));
            }

            if (baseLimitAttribute is Attributes.Int16.LimitAttribute attribute)
            {
                return((short)_random.Next(attribute.MinimumValue, attribute.MaximumValue));
            }

            throw new InvalidAttributeException(typeof(Attributes.Int16.LimitAttribute));
        }
예제 #4
0
        internal static dynamic GetRandomValues(this Type source, Attributes.BaseLimitAttribute limitAttribute = null)
        {
            IDictionary <Type, Func <dynamic> > _actionDictionary = new Dictionary <Type, Func <dynamic> >()
            {
                [typeof(string)]  = () => RandomString(),
                [typeof(sbyte)]   = () => RandomSByte(),
                [typeof(short)]   = () => RandomInt16(limitAttribute),
                [typeof(int)]     = () => RandomInt32(limitAttribute),
                [typeof(long)]    = () => RandomInt64(limitAttribute),
                [typeof(bool)]    = () => RandomBoolean(),
                [typeof(double)]  = () => RandomDouble(limitAttribute),
                [typeof(byte)]    = () => RandomByte(),
                [typeof(char)]    = () => RandomChar(),
                [typeof(ushort)]  = () => RandomUInt16(limitAttribute),
                [typeof(uint)]    = () => RandomUInt32(limitAttribute),
                [typeof(ulong)]   = () => RandomUInt64(limitAttribute),
                [typeof(decimal)] = () => RandomDecimal(),
                [typeof(float)]   = () => RandomFloat(),
            };

            return(_actionDictionary.ContainsKey(source) ? _actionDictionary[source]() : GetDefault(source));
        }
예제 #5
0
        private static UInt64 RandomUInt64(Attributes.BaseLimitAttribute baseLimitAttribute = null)
        {
            ulong minValue = UInt64.MinValue;
            ulong maxValue = UInt64.MaxValue;

            if (baseLimitAttribute is Attributes.UInt64.LimitAttribute attribute)
            {
                minValue = attribute.MinimumValue;
                maxValue = attribute.MaximumValue;
            }
            else if (baseLimitAttribute != null)
            {
                throw new InvalidAttributeException(typeof(Attributes.UInt64.LimitAttribute));
            }

            var byteArray = new byte[8];

            _random.NextBytes(byteArray);
            var result = BitConverter.ToUInt64(byteArray, 0);

            result = (ulong)(Math.Abs((decimal)result % (maxValue - minValue)) + minValue);
            return(result);
        }
예제 #6
0
        private static long RandomInt64(Attributes.BaseLimitAttribute baseLimitAttribute = null)
        {
            long minValue = Int32.MinValue;
            long maxValue = Int32.MaxValue;


            if (baseLimitAttribute is Attributes.Int64.LimitAttribute attribute)
            {
                minValue = attribute.MinimumValue;
                maxValue = attribute.MaximumValue;
            }
            else if (baseLimitAttribute != null)
            {
                throw new InvalidAttributeException(typeof(Attributes.Int64.LimitAttribute));
            }

            long result = _random.Next((Int32)(minValue >> 32), (Int32)(maxValue >> 32));

            result = (result << 32);
            result = result | (long)_random.Next((Int32)minValue >> 32, (Int32)maxValue >> 32);
            result = (Math.Abs(result % (maxValue - minValue)) + minValue);
            return(result);
        }