Esempio n. 1
0
        public void TestUint64()
        {
            var g = MathRandomGeneratorExtended.NewMathRandomGenerator();

            ulong min = 0xFFFFFFFFFFFFFFFF;

            ulong max = 0;

            for (var i = 0; i < 10000; i++)
            {
                var r = g.Uint64();

                if (r < min)
                {
                    min = r;
                }

                if (r > max)
                {
                    max = r;
                }
            }

            if (min > 0x1000000000000000)
            {
                Debug.LogError("Value around lower boundary was not generated");
            }

            if (max < 0xF000000000000000)
            {
                Debug.LogError("Value around upper boundary was not generated");
            }
        }
Esempio n. 2
0
        public void TestMathRandomGenerator()
        {
            var g = MathRandomGeneratorExtended.NewMathRandomGenerator();

            var isLetter = @"^[a-zA-Z]+$";

            for (var i = 0; i < 10000; i++)
            {
                var s = g.GenerateString(10, RandExtended.runesAlpha);

                if (s.Length != 10)
                {
                    Debug.LogError("Generator returned invalid length");
                }

                if (!Regex.IsMatch(s, isLetter))
                {
                    Debug.LogError($"Generator returned unexpected character: {s}");
                }
            }
        }
Esempio n. 3
0
        public void TestIntn()
        {
            var g = MathRandomGeneratorExtended.NewMathRandomGenerator();

            var min = 100;

            var max = 0;

            for (var i = 0; i < 10000; i++)
            {
                var r = g.Intn(100);

                if (r < 0 || r >= 100)
                {
                    Debug.LogError($"Out of range Intn(100): {r}");
                }

                if (r < min)
                {
                    min = r;
                }

                if (r > max)
                {
                    max = r;
                }
            }

            if (min > 1)
            {
                Debug.LogError("Value around lower boundary was not generated");
            }

            if (max < 90)
            {
                Debug.LogError("Value around upper boundary was not generated");
            }
        }
Esempio n. 4
0
        public void TestRandomGeneratorCollision()
        {
            var g = MathRandomGeneratorExtended.NewMathRandomGenerator();


            var testCases = new Dictionary <string, Func <string> >
            {
                { "MathRandom", () => { return(g.GenerateString(10, RandExtended.runesAlpha)); } },

                { "CryptoRandom", () =>
                  {
                      var(s, err) = crypto.GenerateCryptoRandomString(10, RandExtended.runesAlpha);

                      if (err != null)
                      {
                          Debug.LogError(err);
                      }

                      return(s);
                  } }
            };

            const int N = 100;

            const int iteration = 100;

            foreach (var testCase in testCases)
            {
                var tc = testCase;

                for (var iter = 0; iter < iteration; iter++)
                {
                    var mu = new Mutex();

                    var rands = new List <string>(N);

                    for (var i = 0; i < N; i++)
                    {
                        Task t1 = Task.Run(() =>
                        {
                            var r = tc.Value.Invoke();

                            mu.WaitOne();

                            rands.Add(r);

                            mu.ReleaseMutex();
                        });

                        t1.Wait();
                    }

                    if (rands.Count != N)
                    {
                        Debug.LogError("Failed to generate randoms");
                    }

                    for (var i = 0; i < N; i++)
                    {
                        for (var j = i + 1; j < N; j++)
                        {
                            if (rands[i] == rands[j])
                            {
                                Debug.Log($"generateRandString caused collision: {rands[i]} == {rands[j]}");
                            }
                        }
                    }
                }
            }
        }