Beispiel #1
0
        public void TestRandomSeed()
        {
            // since it's random; these != 0 checks MAY THEORETICALLY fail; but the risk should be 1 in uint.maxvalue
            var a = RandomSeed.GetUInt32();

            Assert.IsTrue(a != 0);
            var b = RandomSeed.GetUInt64();

            Assert.IsTrue(b != 0);
            var expanded = RandomSeed.ExpandSeed(123);

            Assert.IsTrue((expanded & 0b11111111_11111111_11111111_11111111_00000000_00000000_00000000_00000000ul) != 0);
        }
Beispiel #2
0
        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 Test7BitEncoding()
        {
            byte[] arr = new byte[1024];

            // test variable lengths
            var numbers = new ulong[7];

            numbers = new ulong[] { 1, 25, 137, 255, 256, 10000, 1000000 };

            ulong state = RandomSeed.GetUInt64();

            for (int runs = 0; runs < 250000; runs++)
            {
                var work = arr.AsSpan();
                for (int i = 0; i < numbers.Length; i++)
                {
                    work.WriteVariableUInt64(numbers[i]);
                }

                int resultLength = arr.Length - work.Length;

                ReadOnlySpan <byte> res = arr.AsSpan(0, resultLength);

                for (int i = 0; i < numbers.Length; i++)
                {
                    var nr = res.ReadVariableUInt64();
                    Assert.AreEqual(numbers[i], nr);
                }

                // re-randomize
                numbers[0] = (ulong)PRNG.Next(ref state, 0, 6);
                numbers[1] = (ulong)PRNG.Next(ref state, 7, 300);
                numbers[2] = (ulong)PRNG.Next(ref state, 300, 5000);
                numbers[3] = (ulong)PRNG.Next(ref state, 5000, 50000);
                numbers[4] = (ulong)PRNG.Next(ref state, 50000, 500000);
                numbers[5] = (ulong)PRNG.NextUInt64(ref state);
                numbers[6] = (ulong)PRNG.NextUInt64(ref state);
            }

            // signed
            var signed = new long[7];

            for (int runs = 0; runs < 250000; runs++)
            {
                // re-randomize
                signed[0] = (long)PRNG.Next(ref state, -5, 5);
                signed[1] = (long)PRNG.Next(ref state, -100, 100);
                signed[2] = (long)PRNG.Next(ref state, -300, 300);
                signed[3] = (long)PRNG.Next(ref state, -5000, 5000);
                signed[4] = (long)PRNG.Next(ref state, -70000, 70000);
                signed[5] = (long)PRNG.NextUInt64(ref state);
                signed[6] = (long)PRNG.NextUInt64(ref state);

                var work = arr.AsSpan();
                for (int i = 0; i < signed.Length; i++)
                {
                    work.WriteVariableInt64(signed[i]);
                }

                int resultLength = arr.Length - work.Length;

                ReadOnlySpan <byte> res = arr.AsSpan(0, resultLength);

                for (int i = 0; i < signed.Length; i++)
                {
                    var nr = res.ReadVariableInt64();
                    Assert.AreEqual(signed[i], nr);
                }
            }
        }
Beispiel #4
0
        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);
            }
        }