예제 #1
0
        public void Default()
        {
            Assert.IsNotNull(CachedRandom.Current);

            const int length    = 255;
            var       randoms   = new ConcurrentBag <Random>();
            var       sequences = new ConcurrentBag <byte[]>();

            Enumerable
            .Range(0, 8)
            .Select(_ => ThreadFactory.StartNew(() =>
            {
                randoms.Add(CachedRandom.Current);
                sequences.Add(CachedRandom.Current.NextBytes(length));
            }))
            .ToList()
            .ForEach(thread => thread.Join());

            EnumerableAssert.HasCount(8, randoms);
            EnumerableAssert.AllItemsAreUnique(randoms);

            foreach (var x in sequences)
            {
                Assert.IsTrue(x.Distinct().Count() >= length / 5,
                              "Less than 20% of the elements are unique. This is highly unlikely to occur in a random sequence.");
            }

            foreach (var x in sequences)
            {
                foreach (var y in sequences)
                {
                    if (!object.ReferenceEquals(x, y))
                    {
                        EqualityAssert.NotEquals(x, y, SequenceEqualityComparer <byte> .Default,
                                                 "Two sequences are identical. This is highly unlikely to occur in random sequences.");
                    }
                }
            }
        }