Example #1
0
        public static string RandomDecimalStringShort(IRandomGenExtended
                                                      wrapper, bool extended)
        {
            var sb = new StringBuilder();

            if (wrapper == null)
            {
                throw new ArgumentNullException(nameof(wrapper));
            }
            int len = 1 + wrapper.GetInt32(4);

            if (!extended)
            {
                sb.Append((char)('1' + wrapper.GetInt32(9)));
                --len;
            }
            AppendRandomDecimals(wrapper, sb, len);
            sb.Append('.');
            len = 1 + wrapper.GetInt32(36);
            AppendRandomDecimals(wrapper, sb, len);
            sb.Append('E');
            len = wrapper.GetInt32(25) - 12;
            sb.Append(TestCommon.IntToString(len));
            return(sb.ToString());
        }
Example #2
0
        public static String RandomDecimalString(RandomGenerator r)
        {
            if (r == null)
            {
                throw new ArgumentNullException(nameof(r));
            }
            int count = r.UniformInt(MaxShortNumberLength) + 1;
            var sb    = new StringBuilder();

            if (r.UniformInt(2) == 0)
            {
                sb.Append('-');
            }
            for (var i = 0; i < count; ++i)
            {
                if (i == 0 && count > 1)
                {
                    sb.Append((char)('1' + r.UniformInt(9)));
                }
                else
                {
                    sb.Append((char)('0' + r.UniformInt(10)));
                }
            }
            if (r.UniformInt(2) == 0)
            {
                sb.Append('.');
                count = r.UniformInt(30) + 1;
                for (var i = 0; i < count; ++i)
                {
                    sb.Append((char)('0' + r.UniformInt(10)));
                }
            }
            if (r.UniformInt(2) == 0)
            {
                sb.Append('E');
                count = (r.UniformInt(100) < 10) ?
                        r.UniformInt(MaxExclusiveExponentLength) :
                        r.UniformInt(10);
                if (count != 0)
                {
                    sb.Append(r.UniformInt(2) == 0 ? '+' : '-');
                }
                sb.Append(TestCommon.IntToString(count));
            }
            return(sb.ToString());
        }
Example #3
0
        public static String RandomDecimalString(RandomGenerator r)
        {
            int count = r.UniformInt(40) + 1;
            var sb    = new StringBuilder();

            if (r.UniformInt(2) == 0)
            {
                sb.Append('-');
            }
            for (var i = 0; i < count; ++i)
            {
                if (i == 0 && count > 1)
                {
                    sb.Append((char)('1' + r.UniformInt(9)));
                }
                else
                {
                    sb.Append((char)('0' + r.UniformInt(10)));
                }
            }
            if (r.UniformInt(2) == 0)
            {
                sb.Append('.');
                count = r.UniformInt(30) + 1;
                for (var i = 0; i < count; ++i)
                {
                    sb.Append((char)('0' + r.UniformInt(10)));
                }
            }
            if (r.UniformInt(2) == 0)
            {
                sb.Append('E');
                count = (r.UniformInt(100) < 10) ? r.UniformInt(5000) :
                        r.UniformInt(20);
                if (count != 0)
                {
                    sb.Append(r.UniformInt(2) == 0 ? '+' : '-');
                }
                sb.Append(TestCommon.IntToString(count));
            }
            return(sb.ToString());
        }
Example #4
0
        public static String RandomDecimalString(
            IRandomGenExtended r,
            bool extended,
            bool limitedExponent)
        {
            if (r == null)
            {
                throw new ArgumentNullException(nameof(r));
            }
            if (r.GetInt32(100) < 95)
            {
                return(RandomDecimalStringShort(r, extended));
            }
            long count = ((long)r.GetInt32(MaxNumberLength) *
                          r.GetInt32(MaxNumberLength)) / MaxNumberLength;

            count = ((long)count * r.GetInt32(MaxNumberLength)) / MaxNumberLength;
            count = Math.Max(1, count);
            long afterPointCount = 0;
            long exponentCount   = 0;
            var  smallExponent   = false;

            if (r.GetInt32(2) == 0)
            {
                afterPointCount = ((long)r.GetInt32(MaxNumberLength) *
                                   r.GetInt32(MaxNumberLength)) / MaxNumberLength;
                afterPointCount = ((long)afterPointCount *
                                   r.GetInt32(MaxNumberLength)) / MaxNumberLength;
                afterPointCount = Math.Max(1, afterPointCount);
            }
            if (r.GetInt32(2) == 0)
            {
                if (limitedExponent || r.GetInt32(10) > 0)
                {
                    exponentCount = 5;
                }
                else
                {
                    exponentCount = ((long)r.GetInt32(MaxNumberLength) *
                                     r.GetInt32(MaxNumberLength)) / MaxNumberLength;
                    exponentCount = ((long)exponentCount *
                                     r.GetInt32(MaxNumberLength)) / MaxNumberLength;
                    exponentCount = ((long)exponentCount *
                                     r.GetInt32(MaxNumberLength)) / MaxNumberLength;
                    exponentCount = Math.Max(1, exponentCount);
                }
            }
            var bufferSize = (int)Math.Min(
                Int32.MaxValue,
                8 + count + afterPointCount + exponentCount);
            var sb = new StringBuilder(bufferSize);

            if (r.GetInt32(2) == 0)
            {
                sb.Append('-');
            }
            if (!extended)
            {
                sb.Append((char)('1' + r.GetInt32(9)));
                --count;
            }
            AppendRandomDecimalsLong(r, sb, count);
            if (afterPointCount > 0)
            {
                sb.Append('.');
                AppendRandomDecimalsLong(r, sb, afterPointCount);
            }
            if (exponentCount > 0)
            {
                int rr = r.GetInt32(3);
                if (rr == 0)
                {
                    sb.Append("E");
                }
                else if (rr == 1)
                {
                    sb.Append("E+");
                }
                else if (rr == 2)
                {
                    sb.Append("E-");
                }
                if (smallExponent)
                {
                    sb.Append(TestCommon.IntToString(r.GetInt32(10000)));
                }
                else
                {
                    AppendRandomDecimalsLong(r, sb, exponentCount);
                }
            }
            return(sb.ToString());
        }