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)); }
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); } } if (r.GetInt32(100) == 3) { return(CloseToPowerOfTwo(r)); } return(EFloat.Create( RandomEInteger(r), (EInteger)(r.GetInt32(400) - 200))); }
private static void AppendRandomDecimals( IRandomGenExtended r, StringBuilder sb, int smallCount) { AppendRandomDecimalsLong(r, sb, smallCount); }
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); if (selection == 0 || selection == 1) { return(BitHeavyEInteger(r, count)); } else if ((selection == 2 || selection == 3) && count < 500) { return(DigitHeavyEInteger(r, count)); } byte[] bytes = RandomByteString(r, count); return(EInteger.FromBytes(bytes, true)); } else { byte[] bytes = RandomByteString( r, r.GetInt32(MaxShortNumberLength) + 1); return(EInteger.FromBytes(bytes, true)); } }
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()); }
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); }
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; } }
public static byte[] RandomByteStringShort(IRandomGenExtended rand) { if (rand == null) { throw new ArgumentNullException(nameof(rand)); } return(RandomByteString( rand, rand.GetInt32(MaxExclusiveShortStringLength))); }
public static int RandomInt32(IRandomGenExtended rand) { byte[] bytes = RandomByteString(rand, 4); int ret = ((int)bytes[0]) & 0xff; ret |= (((int)bytes[1]) & 0xff) << 8; ret |= (((int)bytes[2]) & 0xff) << 16; ret |= (((int)bytes[3]) & 0xff) << 24; return(ret); }
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); }
public static double RandomFiniteDouble(IRandomGenExtended rand) { long r = 0; do { r = RandomInt64(rand); } while (((r >> 52) & 0x7ff) == 0x7ff); return(BitConverter.ToDouble(BitConverter.GetBytes((long)r), 0)); }
public static ERational RandomERational(IRandomGenExtended rand) { EInteger bigintA = RandomEInteger(rand); EInteger bigintB = RandomEInteger(rand); if (bigintB.IsZero) { bigintB = EInteger.One; } return(ERational.Create(bigintA, bigintB)); }
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)); }
private static void AppendRandomDecimalsLong( IRandomGenExtended r, StringBuilder sb, long count) { if (sb == null) { throw new ArgumentNullException(nameof(sb)); } if (r == null) { throw new ArgumentNullException(nameof(r)); } if (count > 0) { var buflen = (int)Math.Min(0x10000, Math.Max(count + 8, 64)); var buffer = new byte[buflen]; while (count > 0) { r.GetBytes(buffer, 0, buflen); var i = 0; while (i < buflen && count > 0) { int x = ((int)buffer[i]) & 31; if (x < 30) { sb.Append(charTable[x]); --count; ++i; } else if (count >= 40 && i + 1 < buflen) { int y = (((int)buffer[i + 1]) & 0xff) % valueSpecialDecimals2.Length; sb.Append(valueSpecialDecimals2[y]); count -= 40; i += 2; } else if (count >= 10 && i + 1 < buflen) { int y = (((int)buffer[i + 1]) & 0xff) % valueSpecialDecimals.Length; sb.Append(valueSpecialDecimals[y]); count -= 10; i += 2; } else { ++i; } } } } }
public static byte[] RandomByteString(IRandomGenExtended rand, int length) { if (rand == null) { throw new ArgumentNullException(nameof(rand)); } var bytes = new byte[length]; rand.GetBytes(bytes, 0, bytes.Length); return(bytes); }
public byte[] Generate(IRandomGenExtended random) { var bs = new ByteWriter(); if (random == null) { throw new ArgumentNullException(nameof(random)); } this.Generate(random, 0, bs); byte[] ret = bs.ToBytes(); return(ret); }
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); }
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); }
private static EDecimal RandomEDecimalLowExponent(IRandomGenExtended rand) { while (true) { EDecimal ef = RandomObjects.RandomEDecimal(rand); if ( ef.Exponent.CompareTo(-20000) >= 0 && ef.Exponent.CompareTo(20000) <= 0) { return(ef); } } }
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); }
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); }
public static long RandomInt64(IRandomGenExtended rand) { byte[] bytes = RandomByteString(rand, 8); long ret = ((long)bytes[0]) & 0xff; ret |= (((long)bytes[1]) & 0xff) << 8; ret |= (((long)bytes[2]) & 0xff) << 16; ret |= (((long)bytes[3]) & 0xff) << 24; ret |= (((long)bytes[4]) & 0xff) << 32; ret |= (((long)bytes[5]) & 0xff) << 40; ret |= (((long)bytes[6]) & 0xff) << 48; ret |= (((long)bytes[7]) & 0xff) << 56; return(ret); }
private static void GenerateJsonNumber(IRandomGenExtended ra, ByteWriter bs) { if (ra.GetInt32(2) == 0) { bs.Write((int)'-'); } int len = ((ra.GetInt32(2000) * ra.GetInt32(2000)) / 2000) + 1; bs.Write(0x31 + ra.GetInt32(9)); for (int i = 0; i < len; ++i) { bs.Write(0x30 + ra.GetInt32(10)); } if (ra.GetInt32(2) == 0) { bs.Write(0x2e); len = ((ra.GetInt32(2000) * ra.GetInt32(2000)) / 2000) + 1; for (int i = 0; i < len; ++i) { bs.Write(0x30 + ra.GetInt32(10)); } } if (ra.GetInt32(2) == 0) { int rr = ra.GetInt32(3); if (rr == 0) { bs.Write((int)'E'); } else if (rr == 1) { bs.Write((int)'E').Write((int)'+'); } else if (rr == 2) { bs.Write((int)'E').Write((int)'-'); } len = 1 + ra.GetInt32(5); if (ra.GetInt32(10) == 0) { len = 1 + ((ra.GetInt32(2000) * ra.GetInt32(2000)) / 2000); } for (int i = 0; i < len; ++i) { bs.Write(0x30 + ra.GetInt32(10)); } } }
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); }
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))); }
private static void GenerateUtf8(IRandomGenExtended ra, ByteWriter bs, int length) { for (int i = 0; i < length;) { int r = ra.GetInt32(10); if (r > 0) { bs.Write(ra.GetInt32(128)); ++i; } else { r = ra.GetInt32(3); if (r == 0 && length - i >= 2) { r = 0xc2 + ra.GetInt32((0xdf - 0xc2) + 1); bs.Write(r); bs.Write(0x80 + ra.GetInt32(0x40)); i += 2; } else if (r == 1 && length - i >= 3) { r = 0xe0 + ra.GetInt32(16); bs.Write(r); int lower = (r == 0xe0) ? 0xa0 : 0x80; int upper = (r == 0xed) ? 0x9f : 0xbf; r = lower + ra.GetInt32((upper - lower) + 1); bs.Write(r); bs.Write(0x80 + ra.GetInt32(0x40)); i += 3; } else if (r == 2 && length - i >= 4) { r = 0xf0 + ra.GetInt32(5); bs.Write(r); int lower = (r == 0xf0) ? 0x90 : 0x80; int upper = (r == 0xf4) ? 0x8f : 0xbf; r = lower + ra.GetInt32((upper - lower) + 1); bs.Write(r); bs.Write(0x80 + ra.GetInt32(0x40)); bs.Write(0x80 + ra.GetInt32(0x40)); i += 4; } } } }
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)); }
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, }); }
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)]); } } }
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())); }