Exemple #1
0
 public static EFloat RandomEFloat(IRandomGenExtended r)
 {
     if (r == null)
     {
         throw new ArgumentNullException(nameof(r));
     }
     if (r.GetInt32(100) == 0)
     {
         int x = r.GetInt32(3);
         if (x == 0)
         {
             return(EFloat.PositiveInfinity);
         }
         if (x == 1)
         {
             return(EFloat.NegativeInfinity);
         }
         if (x == 2)
         {
             return(EFloat.NaN);
         }
     }
     return(EFloat.Create(
                RandomEInteger(r),
                (EInteger)(r.GetInt32(400) - 200)));
 }
Exemple #2
0
        public static EInteger RandomEInteger(IRandomGenExtended r)
        {
            if (r == null)
            {
                throw new ArgumentNullException(nameof(r));
            }
            int selection = r.GetInt32(100);

            if (selection < 10)
            {
                int count = r.GetInt32(MaxNumberLength);
                count = (int)(((long)count * r.GetInt32(MaxNumberLength)) /
                              MaxNumberLength);
                count = (int)(((long)count * r.GetInt32(MaxNumberLength)) /
                              MaxNumberLength);
                count = Math.Max(count, 1);
                byte[] bytes = RandomByteString(r, count);
                return(EInteger.FromBytes(bytes, true));
            }
            else
            {
                byte[] bytes = RandomByteString(
                    r,
                    r.GetInt32(MaxShortNumberLength) + 1);
                return(EInteger.FromBytes(bytes, true));
            }
        }
Exemple #3
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());
        }
Exemple #4
0
        private static int GenerateUtf8(
            IRandomGenExtended ra,
            ByteWriter bs,
            StringBuilder sb,
            int len)
        {
            int r = ra.GetInt32(3);
            int r2, r3, r4;

            if (r == 0 && len >= 2)
            {
                r = 0xc2 + ra.GetInt32((0xdf - 0xc2) + 1);
                bs.Write(r);
                r2 = 0x80 + ra.GetInt32(0x40);
                bs.Write(r2);
                if (sb != null)
                {
                    sb.Append((char)(((r - 0x80) << 6) | r2));
                }
                return(2);
            }
            else if (r == 1 && len >= 3)
            {
                r = 0xe0 + ra.GetInt32(16);
                bs.Write(r);
                int lower = (r == 0xe0) ? 0xa0 : 0x80;
                int upper = (r == 0xed) ? 0x9f : 0xbf;
                r2 = lower + ra.GetInt32((upper - lower) + 1);
                bs.Write(r2);
                r3 = 0x80 + ra.GetInt32(0x40);
                bs.Write(r3);
                if (sb != null)
                {
                    sb.Append((char)(((r - 0x80) << 12) | ((r2 - 0x80) << 6) | r3));
                }
                return(3);
            }
            else if (r == 2 && len >= 4)
            {
                r = 0xf0 + ra.GetInt32(5);
                bs.Write(r);
                int lower = (r == 0xf0) ? 0x90 : 0x80;
                int upper = (r == 0xf4) ? 0x8f : 0xbf;
                r2 = lower + ra.GetInt32((upper - lower) + 1);
                bs.Write(r2);
                r3 = 0x80 + ra.GetInt32(0x40);
                bs.Write(r3);
                r4 = 0x80 + ra.GetInt32(0x40);
                bs.Write(r4);
                r = ((r - 0x80) << 18) | ((r2 - 0x80) << 12) | ((r3 - 0x80) << 6) | r4;
                if (sb != null)
                {
                    sb.Append((char)(((r - 0x10000) >> 10) | 0xd800));
                    sb.Append((char)(((r - 0x10000) & 0x3ff) | 0xdc00));
                }
                return(4);
            }
            return(0);
        }
Exemple #5
0
        public static EInteger RandomEIntegerMajorType0(IRandomGenExtended rand)
        {
            int      v  = rand.GetInt32(0x10000);
            EInteger ei = EInteger.FromInt32(v);

            ei = ei.ShiftLeft(16).Add(rand.GetInt32(0x10000));
            ei = ei.ShiftLeft(16).Add(rand.GetInt32(0x10000));
            ei = ei.ShiftLeft(16).Add(rand.GetInt32(0x10000));
            return(ei);
        }
Exemple #6
0
        public static EInteger RandomEIntegerMajorType0Or1(IRandomGenExtended
                                                           rand)
        {
            int      v  = rand.GetInt32(0x10000);
            EInteger ei = EInteger.FromInt32(v);

            ei = ei.ShiftLeft(16).Add(rand.GetInt32(0x10000));
            ei = ei.ShiftLeft(16).Add(rand.GetInt32(0x10000));
            ei = ei.ShiftLeft(16).Add(rand.GetInt32(0x10000));
            if (rand.GetInt32(2) == 0)
            {
                ei = ei.Add(1).Negate();
            }
            return(ei);
        }
Exemple #7
0
 public static EDecimal GenerateEDecimalSmall(IRandomGenExtended wrapper)
 {
     if (wrapper == null)
     {
         throw new ArgumentNullException(nameof(wrapper));
     }
     if (wrapper.GetInt32(2) == 0)
     {
         EInteger eix = EInteger.FromBytes(
             RandomByteString(wrapper, 1 + wrapper.GetInt32(36)),
             true);
         int exp = wrapper.GetInt32(25) - 12;
         return(EDecimal.Create(eix, exp));
     }
     return(EDecimal.FromString(RandomDecimalStringShort(wrapper, false)));
 }
Exemple #8
0
        private static EInteger BitHeavyEInteger(IRandomGenExtended rg, int count)
        {
            var sb = new StringBuilder();

            int[] oneChances =
            {
                999,   1, 980,  20, 750, 250, 980,
                20,  980,  20, 980,  20, 750, 250,
            };
            int oneChance = oneChances[rg.GetInt32(oneChances.Length)];

            for (var i = 0; i < count; ++i)
            {
                sb.Append((rg.GetInt32(1000) >= oneChance) ? '0' : '1');
            }
            return(EInteger.FromRadixString(sb.ToString(), 2));
        }
Exemple #9
0
        private static void GenerateArgument(
            IRandomGenExtended r,
            int majorType,
            int len,
            ByteWriter bs)
        {
            var maxArg = 4;
            var sh     = 0;
            int minArg = (len < 0x18) ? 0 : ((len <= 0xff) ? 1 :
                                             ((len <= 0xffff) ? 2 : 3));
            int arg = minArg + r.GetInt32(maxArg - minArg + 1);

            switch (arg)
            {
            case 0:
                bs.Write((majorType * 0x20) + len);
                break;

            case 1:
                bs.Write((majorType * 0x20) + 0x18);
                bs.Write(len & 0xff);
                break;

            case 2:
                bs.Write((majorType * 0x20) + 0x19);
                sh = 8;
                for (int i = 0; i < 2; ++i)
                {
                    bs.Write((len >> sh) & 0xff);
                    sh -= 8;
                }
                break;

            case 3:
                bs.Write((majorType * 0x20) + 0x1a);
                sh = 24;
                for (int i = 0; i < 4; ++i)
                {
                    bs.Write((len >> sh) & 0xff);
                    sh -= 8;
                }
                break;

            case 4:
                bs.Write((majorType * 0x20) + 0x1b);
                for (int i = 0; i < 4; ++i)
                {
                    bs.Write(0);
                }
                sh = 24;
                for (int i = 0; i < 4; ++i)
                {
                    bs.Write((len >> sh) & 0xff);
                    sh -= 8;
                }
                break;
            }
        }
Exemple #10
0
        public static double RandomDouble(IRandomGenExtended rand, int exponent)
        {
            if (exponent == Int32.MaxValue)
            {
                if (rand == null)
                {
                    throw new ArgumentNullException(nameof(rand));
                }
                exponent = rand.GetInt32(2047);
            }
            if (rand == null)
            {
                throw new ArgumentNullException(nameof(rand));
            }
            long r = rand.GetInt32(0x10000);

            r |= ((long)rand.GetInt32(0x10000)) << 16;
            if (rand.GetInt32(2) == 0)
            {
                r |= ((long)rand.GetInt32(0x10000)) << 32;
                if (rand.GetInt32(2) == 0)
                {
                    r |= ((long)rand.GetInt32(0x10000)) << 48;
                }
            }
            r &= ~0x7ff0000000000000L;   // clear exponent
            r |= ((long)exponent) << 52; // set exponent
            return(BitConverter.ToDouble(BitConverter.GetBytes((long)r), 0));
        }
Exemple #11
0
        public static EInteger RandomSmallIntegral(IRandomGenExtended r)
        {
            if (r == null)
            {
                throw new ArgumentNullException(nameof(r));
            }
            int count = r.GetInt32(MaxShortNumberLength / 2) + 1;
            var sb    = new StringBuilder();

            if (r.GetInt32(2) == 0)
            {
                sb.Append('-');
            }
            sb.Append((char)('1' + r.GetInt32(9)));
            --count;
            AppendRandomDecimals(r, sb, count);
            return(EInteger.FromString(sb.ToString()));
        }
Exemple #12
0
 private static void GenerateWhitespace(
     IRandomGenExtended ra,
     ByteWriter bs)
 {
     if (ra.GetInt32(10) == 0)
     {
         int   len = ra.GetInt32(20);
         int[] ws  = { 0x09, 0x0d, 0x0a, 0x20 };
         if (ra.GetInt32(100) == 0)
         {
             len = ra.GetInt32(100);
         }
         for (int i = 0; i < len; ++i)
         {
             bs.Write(ws[ra.GetInt32(ws.Length)]);
         }
     }
 }
Exemple #13
0
 public static byte[] RandomByteStringShort(IRandomGenExtended rand)
 {
     if (rand == null)
     {
         throw new ArgumentNullException(nameof(rand));
     }
     return(RandomByteString(
                rand,
                rand.GetInt32(MaxExclusiveShortStringLength)));
 }
Exemple #14
0
        private static void GenerateJsonString(
            IRandomGenExtended ra,
            ByteWriter bs,
            int depth)
        {
            int len = ra.GetInt32(1000) * ra.GetInt32(1000);

            len /= 1000;
            if (ra.GetInt32(50) == 0 && depth < 2)
            {
                // Exponential curve that strongly favors small numbers
                var v = (long)ra.GetInt32(1000000) * ra.GetInt32(1000000);
                len = (int)(v / 1000000);
            }
            bs.Write(0x22);
            for (int i = 0; i < len;)
            {
                int r = ra.GetInt32(10);
                if (r > 2)
                {
                    int x = 0x20 + ra.GetInt32(60);
                    if (x == (int)'\"')
                    {
                        bs.Write((int)'\\').Write(x);
                    }
                    else if (x == (int)'\\')
                    {
                        bs.Write((int)'\\').Write(x);
                    }
                    else
                    {
                        bs.Write(x);
                    }
                    ++i;
                }
                else if (r == 1)
                {
                    bs.Write((int)'\\');
                    int esc = valueEscapes[ra.GetInt32(valueEscapes.Length)];
                    bs.Write((int)esc);
                    if (esc == (int)'u')
                    {
                        GenerateUtf16(ra, bs, null);
                    }
                }
                else
                {
                    GenerateUtf8(ra, bs, null, len - i);
                }
            }
            bs.Write(0x22);
        }
Exemple #15
0
 public static EInteger RandomEIntegerSmall(IRandomGenExtended r)
 {
     if (r == null)
     {
         throw new ArgumentNullException(nameof(r));
     }
     byte[] bytes = RandomByteString(
         r,
         r.GetInt32(MaxShortNumberLength) + 1);
     return(EInteger.FromBytes(bytes, true));
 }
Exemple #16
0
        public static byte[] RandomByteString(IRandomGenExtended rand)
        {
            if (rand == null)
            {
                throw new ArgumentNullException(nameof(rand));
            }
            int x     = rand.GetInt32(MaxExclusiveStringLength);
            var bytes = new byte[x];

            rand.GetBytes(bytes, 0, bytes.Length);
            return(bytes);
        }
Exemple #17
0
        public static CBORObject RandomCBORObject(IRandomGenExtended rand, int
                                                  depth)
        {
            int nextval = rand.GetInt32(11);

            switch (nextval)
            {
            case 0:
            case 1:
            case 2:
            case 3:
                return(RandomNumberOrRational(rand));

            case 4:
                return(rand.GetInt32(2) == 0 ? CBORObject.True : CBORObject.False);

            case 5:
                return(rand.GetInt32(2) == 0 ? CBORObject.Null :
                       CBORObject.Undefined);

            case 6:
                return(CBORObject.FromObject(
                           RandomObjects.RandomTextString(rand)));

            case 7:
                return(CBORObject.FromObject(
                           RandomObjects.RandomByteString(rand)));

            case 8:
                return(RandomCBORArray(rand, depth));

            case 9:
                return(RandomCBORMap(rand, depth));

            case 10:
                return(RandomCBORTaggedObject(rand, depth));

            default: return(RandomNumber(rand));
            }
        }
Exemple #18
0
        public static CBORObject RandomCBORArray(IRandomGenExtended rand, int
                                                 depth)
        {
            int        x       = rand.GetInt32(100);
            int        count   = (x < 80) ? 2 : ((x < 93) ? 1 : ((x < 98) ? 0 : 10));
            CBORObject cborRet = CBORObject.NewArray();

            for (var i = 0; i < count; ++i)
            {
                cborRet.Add(RandomCBORObject(rand, depth + 1));
            }
            return(cborRet);
        }
Exemple #19
0
        public static CBORObject RandomCBORMap(IRandomGenExtended rand, int depth)
        {
            int        x       = rand.GetInt32(100);
            int        count   = (x < 80) ? 2 : ((x < 93) ? 1 : ((x < 98) ? 0 : 10));
            CBORObject cborRet = CBORObject.NewMap();

            for (var i = 0; i < count; ++i)
            {
                CBORObject key   = RandomCBORObject(rand, depth + 1);
                CBORObject value = RandomCBORObject(rand, depth + 1);
                cborRet[key] = value;
            }
            return(cborRet);
        }
Exemple #20
0
        public static float RandomSingle(IRandomGenExtended rand, int exponent)
        {
            if (exponent == Int32.MaxValue)
            {
                if (rand == null)
                {
                    throw new ArgumentNullException(nameof(rand));
                }
                exponent = rand.GetInt32(255);
            }
            if (rand == null)
            {
                throw new ArgumentNullException(nameof(rand));
            }
            int r = rand.GetInt32(0x10000);

            if (rand.GetInt32(2) == 0)
            {
                r |= ((int)rand.GetInt32(0x10000)) << 16;
            }
            r &= ~0x7f800000;           // clear exponent
            r |= ((int)exponent) << 23; // set exponent
            return(BitConverter.ToSingle(BitConverter.GetBytes((int)r), 0));
        }
Exemple #21
0
        public static string RandomTextString(IRandomGenExtended rand)
        {
            if (rand == null)
            {
                throw new ArgumentNullException(nameof(rand));
            }
            int length = rand.GetInt32(MaxExclusiveStringLength);
            var sb     = new StringBuilder();

            for (var i = 0; i < length; ++i)
            {
                int x = rand.GetInt32(100);
                if (x < 95)
                {
                    // ASCII
                    sb.Append((char)(0x20 + rand.GetInt32(0x60)));
                }
                else if (x < 98)
                {
                    // Supplementary character
                    x = rand.GetInt32(0x400) + 0xd800;
                    sb.Append((char)x);
                    x = rand.GetInt32(0x400) + 0xdc00;
                    sb.Append((char)x);
                }
                else if (rand.GetInt32(100) < 5)
                {
                    // 0x80, to help detect ASCII off-by-one errors
                    sb.Append((char)0x80);
                }
                else
                {
                    // BMP character
                    x = 0x20 + rand.GetInt32(0xffe0);
                    if (x >= 0xd800 && x < 0xe000)
                    {
                        // surrogate code unit, generate ASCII instead
                        x = 0x20 + rand.GetInt32(0x60);
                    }
                    sb.Append((char)x);
                }
            }
            return(sb.ToString());
        }
Exemple #22
0
        public static CBORObject RandomNumberOrRational(IRandomGenExtended rand)
        {
            object o = null;

            switch (rand.GetInt32(7))
            {
            case 0:
                o = RandomObjects.RandomDouble(
                    rand,
                    Int32.MaxValue);
                return(CBORObject.FromObject(o));

            case 1:
                o = RandomObjects.RandomSingle(
                    rand,
                    Int32.MaxValue);
                return(CBORObject.FromObject(o));

            case 2:
                return(CBORObject.FromObject(
                           RandomObjects.RandomEInteger(rand)));

            case 3:
                return(CBORObject.FromObject(
                           RandomObjects.RandomEFloat(rand)));

            case 4:
                o = RandomObjects.RandomEDecimal(rand);
                return(CBORObject.FromObject(o));

            case 5:
                o = RandomObjects.RandomInt64(rand);
                return(CBORObject.FromObject(o));

            case 6:
                o = RandomObjects.RandomERational(rand);
                return(CBORObject.FromObject(o));

            default: throw new InvalidOperationException();
            }
        }
Exemple #23
0
        private static void GenerateCodeUnit(
            IRandomGenExtended ra,
            ByteWriter bs,
            int cu)
        {
            int c;
            var shift = 12;

            for (int i = 0; i < 4; ++i)
            {
                c = (cu >> shift) & 0xf;
                if (c < 10)
                {
                    bs.Write(0x30 + c);
                }
                else
                {
                    bs.Write(0x41 + (c - 10) + (ra.GetInt32(2) * 0x20));
                }
                shift -= 4;
            }
        }
Exemple #24
0
        private static int[] RandomLesserFields(IRandomGenExtended irg, EInteger
                                                year)
        {
            int month = irg.GetInt32(12) + 1;
            int days  = IsLeapYear(year) ? ValueLeapDays[month] :
                        ValueNormalDays[month];

            return(new int[] {
                month,
                irg.GetInt32(days) + 1,
                irg.GetInt32(24),
                irg.GetInt32(60),
                irg.GetInt32(60),
                irg.GetInt32(1000000000),
                0,
            });
        }
Exemple #25
0
        private static void GenerateUtf16(
            IRandomGenExtended ra,
            ByteWriter bs,
            StringBuilder sb)
        {
            int r = ra.GetInt32(0x110000 - 0x800);

            if (r >= 0xd800)
            {
                r += 0x800;
            }
            if (r >= 0x10000)
            {
                int rc = (((r - 0x10000) >> 10) & 0x3ff) | 0xd800;
                GenerateCodeUnit(ra, bs, rc);
                if (sb != null)
                {
                    sb.Append((char)rc);
                }
                bs.Write((int)'\\');
                bs.Write((int)'u');
                rc = ((r - 0x10000) & 0x3ff) | 0xdc00;
                GenerateCodeUnit(ra, bs, rc);
                if (sb != null)
                {
                    sb.Append((char)rc);
                }
            }
            else
            {
                GenerateCodeUnit(ra, bs, r);
                if (sb != null)
                {
                    sb.Append((char)r);
                }
            }
        }
Exemple #26
0
        public static EFloat CloseToPowerOfTwo(IRandomGenExtended rg)
        {
            if (rg == null)
            {
                throw new ArgumentNullException(nameof(rg));
            }
            int pwr = (rg.GetInt32(100) < 80) ? IntInRange(rg, -20, 20) :
                      IntInRange(rg, -300, 300);
            int pwr2 = pwr - (rg.GetInt32(100) < 80 ? IntInRange(rg, 51, 61) :
                              IntInRange(rg, 2, 300));
            EFloat ef = null;

            ef = (rg.GetInt32(2) == 0) ? EFloat.Create(1,
                                                       pwr).Add(EFloat.Create(1, pwr2)) : EFloat.Create(1,
                                                                                                        pwr).Subtract(EFloat.Create(1, pwr2));
            if (rg.GetInt32(10) == 0)
            {
                pwr2 = pwr - (rg.GetInt32(100) < 80 ? IntInRange(rg, 51, 61) :
                              IntInRange(rg, 2, 300));
                ef = (rg.GetInt32(2) == 0) ? ef.Add(EFloat.Create(1, pwr2)) :
                     ef.Subtract(EFloat.Create(1, pwr2));
            }
            return(ef);
        }
Exemple #27
0
        public static CBORObject RandomCBORTaggedObject(
            IRandomGenExtended rand,
            int depth)
        {
            var tag = 0;

            if (rand.GetInt32(2) == 0)
            {
                int[] tagselection =
                {
                    2,  2, 2,  3,  3,  3, 4, 4, 4, 5, 5, 5, 30, 30,
                    30, 0, 1, 25, 26, 27,
                };
                tag = tagselection[rand.GetInt32(tagselection.Length)];
            }
            else if (rand.GetInt32(100) < 90)
            {
                return(CBORObject.FromObjectAndTag(
                           RandomCBORObject(rand, depth + 1),
                           rand.GetInt32(0x100000)));
            }
            else
            {
                return(CBORObject.FromObjectAndTag(
                           RandomCBORObject(rand, depth + 1),
                           RandomEIntegerMajorType0(rand)));
            }
            if (tag == 25)
            {
                tag = 0;
            }
            if (tag == 30)
            {
                object o = RandomObjects.RandomByteString(rand);
                return(CBORObject.FromObject(o));
            }
            {
                CBORObject cbor;
                // Console.WriteLine("tag "+tag+" "+i);
                if (tag == 0 || tag == 1 || tag == 28 || tag == 29)
                {
                    tag = 999;
                }
                if (tag == 2 || tag == 3)
                {
                    object o = RandomObjects.RandomByteStringShort(rand);
                    cbor = CBORObject.FromObject(o);
                }
                else if (tag == 4 || tag == 5)
                {
                    cbor = CBORObject.NewArray();
                    object o = RandomObjects.RandomSmallIntegral(rand);
                    cbor.Add(o);
                    o = RandomObjects.RandomEInteger(rand);
                    cbor.Add(o);
                }
                else if (tag == 30)
                {
                    cbor = CBORObject.NewArray();
                    object o = RandomObjects.RandomSmallIntegral(rand);
                    cbor.Add(o);
                    o = RandomObjects.RandomEInteger(rand);
                    cbor.Add(o);
                }
                else
                {
                    cbor = RandomCBORObject(rand, depth + 1);
                }
                return(CBORObject.FromObjectAndTag(cbor, tag));
            }
        }
Exemple #28
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());
        }
Exemple #29
0
 public static byte[] RandomUtf8Bytes(
     IRandomGenExtended rg,
     bool jsonSafe)
 {
     using (var ms = new MemoryStream()) {
         if (rg == null)
         {
             throw new ArgumentNullException(nameof(rg));
         }
         int length = 1 + rg.GetInt32(6);
         for (var i = 0; i < length; ++i)
         {
             int v = rg.GetInt32(4);
             if (v == 0)
             {
                 int b = 0xe0 + rg.GetInt32(0xee - 0xe1);
                 ms.WriteByte((byte)b);
                 if (b == 0xe0)
                 {
                     ms.WriteByte((byte)(0xa0 + rg.GetInt32(0x20)));
                 }
                 else if (b == 0xed)
                 {
                     ms.WriteByte((byte)(0x80 + rg.GetInt32(0x20)));
                 }
                 else
                 {
                     ms.WriteByte((byte)(0x80 + rg.GetInt32(0x40)));
                 }
                 ms.WriteByte((byte)(0x80 + rg.GetInt32(0x40)));
             }
             else if (v == 1)
             {
                 int b = 0xf0 + rg.GetInt32(0xf5 - 0xf0);
                 ms.WriteByte((byte)b);
                 if (b == 0xf0)
                 {
                     ms.WriteByte((byte)(0x90 + rg.GetInt32(0x30)));
                 }
                 else if (b == 0xf4)
                 {
                     ms.WriteByte((byte)(0x80 + rg.GetInt32(0x10)));
                 }
                 else
                 {
                     ms.WriteByte((byte)(0x80 + rg.GetInt32(0x40)));
                 }
                 ms.WriteByte((byte)(0x80 + rg.GetInt32(0x40)));
                 ms.WriteByte((byte)(0x80 + rg.GetInt32(0x40)));
             }
             else if (v == 2)
             {
                 if (rg.GetInt32(100) < 5)
                 {
                     // 0x80, to help detect ASCII off-by-one errors
                     ms.WriteByte((byte)0xc2);
                     ms.WriteByte((byte)0x80);
                 }
                 else
                 {
                     ms.WriteByte((byte)(0xc2 + rg.GetInt32(0xe0 - 0xc2)));
                     ms.WriteByte((byte)(0x80 + rg.GetInt32(0x40)));
                 }
             }
             else
             {
                 int ch = rg.GetInt32(0x80);
                 if (jsonSafe && (ch == (int)'\\' || ch == (int)'\"' || ch < 0x20))
                 {
                     ch = (int)'?';
                 }
                 ms.WriteByte((byte)ch);
             }
         }
         return(ms.ToArray());
     }
 }
Exemple #30
0
        public static EDecimal RandomEDecimal(IRandomGenExtended r, string[]
                                              decimalString)
        {
            if (r == null)
            {
                throw new ArgumentNullException(nameof(r));
            }
            if (r.GetInt32(100) == 0)
            {
                int x = r.GetInt32(3);
                if (x == 0)
                {
                    if (decimalString != null)
                    {
                        decimalString[0] = "Infinity";
                    }
                    return(EDecimal.PositiveInfinity);
                }
                if (x == 1)
                {
                    if (decimalString != null)
                    {
                        decimalString[0] = "-Infinity";
                    }
                    return(EDecimal.NegativeInfinity);
                }
                if (x == 2)
                {
                    if (decimalString != null)
                    {
                        decimalString[0] = "NaN";
                    }
                    return(EDecimal.NaN);
                }
                // Signaling NaN currently not generated because
                // it doesn't round-trip as well
            }
            if (r.GetInt32(100) < 30)
            {
                string str = RandomDecimalString(r);
                if (str.Length < 500)
                {
                    if (decimalString != null)
                    {
                        decimalString[0] = str;
                    }
                    return(EDecimal.FromString(str));
                }
            }
            EInteger emant = RandomEInteger(r);
            EInteger eexp  = null;

            if (r.GetInt32(100) < 95)
            {
                int exp = (r.GetInt32(100) < 80) ? (r.GetInt32(50) - 25) :
                          (r.GetInt32(5000) - 2500);
                eexp = EInteger.FromInt32(exp);
            }
            else
            {
                eexp = RandomEInteger(r);
            }
            EDecimal ed = EDecimal.Create(emant, eexp);

            if (decimalString != null)
            {
                decimalString[0] = emant.ToString() + "E" + eexp.ToString();
            }
            return(ed);
        }