public void TestStringUtils() { Assert.AreEqual(11, StringUtils.HexCharToInteger('B')); var cbuf = new char[128]; var testNumbers = new ulong[] { 0, 1, 2, 100, 1000, 10000, 100000, 10000000, (ulong)PRNG.Next(0, 10000), (ulong)PRNG.Next(0, 10000), (ulong)PRNG.Next(0, 10000), (ulong)PRNG.Next(0, 10000), (ulong)PRNG.Next(0, 10000), (ulong)PRNG.Next(0, 10000), (ulong)PRNG.Next(0, 10000) }; foreach (var val in testNumbers) { var sans = val.ToString("X"); var str = (PRNG.NextBool() ? "0x" : "") + sans; Assert.AreEqual(val, StringUtils.FromHex(str)); var utfBytes = Encoding.UTF8.GetBytes(str); Assert.AreEqual(val, StringUtils.FromUTF8Hex(utfBytes)); var cnt = StringUtils.ToHex(val, cbuf); Assert.AreEqual(sans.Length, cnt); Assert.IsTrue(sans.AsSpan().SequenceEqual(cbuf.AsSpan(0, cnt))); } var arr = new byte[64]; PRNG.NextBytes(arr); var facit = BitConverter.ToString(arr).Replace("-", ""); var my = StringUtils.ToHex(arr); Assert.AreEqual(facit, my); var dvalstr = "0.00000000000123"; var dval = double.Parse(dvalstr, System.Globalization.CultureInfo.InvariantCulture); var lots = StringUtils.DoubleToString(dval); // this is "1.23E-12" var defaultToString = dval.ToString(System.Globalization.CultureInfo.InvariantCulture); Assert.AreEqual(dvalstr, StringUtils.DoubleToString(dval)); }
/// <summary> /// Generates a single random leaf of X.690 node tree. /// </summary> /// <param name="lengthEncoding">Definite, indefinite or random.</param> /// <returns>A tree leaf.</returns> static X690.Node RandomLeaf(LengthEncoding lengthEncoding = LengthEncoding.Definite) { switch (PRNG.Next(0, 4)) { case 0: return(new X690.Null()); case 1: return(new X690.Boolean(PRNG.NextBool())); case 2: return(new X690.Integer(PRNG.Next(int.MinValue, int.MaxValue))); case 3: return(new X690.Text(RandomString()) { IsDefiniteLength = lengthEncoding.IsDefiniteLength() }); } throw new InvalidOperationException(); }
private void TestBoolMatrix(int size) { var mat = new SymmetricMatrixBool(size); // fill for (int x = 0; x < size; x++) { for (int y = 0; y < size; y++) { mat[x, y] = PRNG.NextBool(); } } // check for (int x = 0; x < size; x++) { for (int y = 0; y < size; y++) { Assert.AreEqual(mat[x, y], mat[y, x]); } } }
public void TestDataWriter2() { ulong ss = RandomSeed.GetUInt64(); byte[] small = new byte[64]; using (var wrt = new DataWriter(1)) { for (int runs = 0; runs < 10000; runs++) { wrt.Clear(); var originalSeed = PRNG.NextUInt64(ref ss); var seed = originalSeed; { int numOps = PRNG.Next(ref seed, 1, 15); for (int o = 0; o < numOps; o++) { switch (PRNG.Next(ref seed, 0, 6)) { case 0: wrt.WriteBool(PRNG.NextBool(ref seed)); break; case 1: wrt.WriteSingle((float)PRNG.Next(ref seed, 1, 100)); break; case 2: wrt.WriteVariableInt32(PRNG.Next(ref seed, -50, 50)); break; case 3: wrt.WriteString(PRNG.NextUInt32(ref seed).ToString()); break; case 4: wrt.WriteUInt16((ushort)PRNG.Next(ref seed, 0, ushort.MaxValue)); break; case 5: var sss = small.AsSpan(0, PRNG.Next(ref seed, 1, 25)); wrt.WriteBytes(sss); break; } } } // read var rdr = wrt.ReadOnlySpan; seed = originalSeed; { int numOps = PRNG.Next(ref seed, 1, 15); for (int o = 0; o < numOps; o++) { switch (PRNG.Next(ref seed, 0, 6)) { case 0: { var facit = PRNG.NextBool(ref seed); Assert.AreEqual(facit, rdr.ReadBool()); } break; case 1: { var facit = (float)PRNG.Next(ref seed, 1, 100); Assert.AreEqual(facit, rdr.ReadSingle()); } break; case 2: { var facit = PRNG.Next(ref seed, -50, 50); Assert.AreEqual(facit, rdr.ReadVariableInt32()); } break; case 3: { var facit = PRNG.NextUInt32(ref seed).ToString(); Assert.AreEqual(facit, rdr.ReadString()); } break; case 4: { var facit = (ushort)PRNG.Next(ref seed, 0, ushort.MaxValue); Assert.AreEqual(facit, rdr.ReadUInt16()); } break; case 5: { var sss = small.AsSpan(0, PRNG.Next(ref seed, 1, 25)); var aaa = rdr.ReadBytes(sss.Length); Assert.IsTrue(sss.SequenceEqual(aaa)); } break; } } } } } }
public void TestPRNG() { const int iterations = 10000000; Console.WriteLine(); int trues = 0; int larger = 0; int smaller = 0; for (int i = 0; i < iterations; i++) { if (PRNG.NextBool()) { trues++; } int val = PRNG.Next(5, 8); // 5, 6 or 7 switch (val) { case 5: smaller++; break; case 6: break; case 7: larger++; break; default: Assert.Fail("Value " + val + " isn't 5, 6 or 7"); break; } // ranged var r1 = (int)PRNG.NextUInt32(); var r2 = (int)PRNG.NextUInt32(); if (r1 < r2) { var rr = PRNG.Next(r1, r2); Assert.IsTrue(rr >= r1 && rr < r2, rr.ToString() + " is not between " + r1 + " and " + r2); } else if (r1 > r2) { var rr = PRNG.Next(r2, r1); Assert.IsTrue(rr >= r2 && rr < r1, rr.ToString() + " is not between " + r2 + " and " + r1); } } var p = (double)trues / (double)iterations; Assert.IsTrue(p > 0.495 && p < 0.505); const double third = 1.0 / 3.0; const double offset = third * 0.05; const double low = third - offset; const double high = third + offset; p = (double)smaller / (double)iterations; Assert.IsTrue(p > low && p < high); p = (double)larger / (double)iterations; Assert.IsTrue(p > low && p < high); }
public void TestPRNG() { const int iterations = 5000000; Console.WriteLine(); int trues = 0; int larger = 0; int smaller = 0; for (int i = 0; i < iterations; i++) { if (PRNG.NextBool()) { trues++; } int val = PRNG.Next(5, 8); // 5, 6 or 7 switch (val) { case 5: smaller++; break; case 6: break; case 7: larger++; break; default: Assert.Fail("Value " + val + " isn't 5, 6 or 7"); break; } // ranged var r1 = (int)PRNG.NextUInt32(); var r2 = (int)PRNG.NextUInt32(); if (r1 < r2) { var rr = PRNG.Next(r1, r2); Assert.IsTrue(rr >= r1 && rr < r2, rr.ToString() + " is not between " + r1 + " and " + r2); } else if (r1 > r2) { var rr = PRNG.Next(r2, r1); Assert.IsTrue(rr >= r2 && rr < r1, rr.ToString() + " is not between " + r2 + " and " + r1); } } var p = (double)trues / (double)iterations; Assert.IsTrue(p > 0.495 && p < 0.505); const double third = 1.0 / 3.0; const double offset = third * 0.05; const double low = third - offset; const double high = third + offset; p = (double)smaller / (double)iterations; Assert.IsTrue(p > low && p < high); p = (double)larger / (double)iterations; Assert.IsTrue(p > low && p < high); // make sure nextdouble() and nextfloat() don't generate numbers outside range var state = RandomSeed.GetUInt64(); for (int r = 0; r < 10000000; r++) { double d = PRNG.NextDouble(ref state); Assert.IsTrue(d >= 0.0 && d < 1.0); float f = PRNG.NextFloat(ref state); Assert.IsTrue(f >= 0.0f && f < 1.0f); } }
/// <summary> /// Generates a single branch of X.690 node tree. /// </summary> /// <param name="lengthEncoding">Definite, indefinite or random.</param> /// <returns>A tree branch.</returns> static X690.Node RandomBranch(LengthEncoding lengthEncoding = LengthEncoding.Definite) => PRNG.NextBool() ? new X690.Sequence() { IsDefiniteLength = lengthEncoding.IsDefiniteLength() } as X690.Node : new X690.Set() { IsDefiniteLength = lengthEncoding.IsDefiniteLength() } as X690.Node;