Exemplo n.º 1
0
        private static void RunTrials(int sampleSize, double errorMargin)
        {
            var q = new Queue <double>();

            while (q.Count < sampleSize)
            {
                q.Enqueue(Randomness.NextSecureDouble());
            }

            for (int k = 0; k < 1000; k++)
            {
                // rotate
                q.Dequeue();
                q.Enqueue(Randomness.NextSecureDouble());

                var avg = q.Average();

                // Dividing by n−1 gives a better estimate of the population
                // standard deviation for the larger parent population than dividing by n,
                // which gives a result which is correct for the sample only.

                var actual = Math.Sqrt(q.Sum(x => (x - avg) * (x - avg)) / (q.Count - 1));

                // see http://stats.stackexchange.com/a/1014/4576

                var expected = (q.Max() - q.Min()) / Math.Sqrt(12);

                Assert.AreEqual(expected, actual, errorMargin);
            }
        }
Exemplo n.º 2
0
        public void ByteArrayComparer_SortRandomUInt32Test()
        {
            var comparer = new ByteArrayComparer();

            var r = Randomness.NextRandom();

            var list  = new List <byte[]>(1000);
            var list2 = new List <uint>(1000);

            for (int i = 0; i < 1000; i++)
            {
                var bytes = new byte[4];
                r.NextBytes(bytes);
                uint x = ((uint)bytes[0] << 24)
                         | ((uint)bytes[1] << 16)
                         | ((uint)bytes[2] << 8)
                         | bytes[3]
                ;
                list.Add(bytes);
                list2.Add(x);
            }

            list.Sort(comparer);
            list2.Sort(); // use default comparer

            CollectionAssert.AreEqual(list2, list.Select(bytes => {
                uint x = ((uint)bytes[0] << 24)
                         | ((uint)bytes[1] << 16)
                         | ((uint)bytes[2] << 8)
                         | bytes[3]
                ;
                return(x);
            }).ToList());
        }
Exemplo n.º 3
0
        public void BitTwiddling_ByteSwapUInt64Test()
        {
            var r = Randomness.NextRandom();

            var bytes = new byte[8];

            for (int i = 0; i < 1000; i++)
            {
                r.NextBytes(bytes);
                var reversed = BitConverter.GetBytes(BitConverter.ToUInt64(bytes, 0).ByteSwap());
                CollectionAssert.AreEqual(bytes.Reverse().ToArray(), reversed);
            }
        }