public void HasCount()
        {
            var bytes = new byte[] { 243, 75, 31, 139, 26 };

            EnumerableAssert.HasCount(0, new byte[0]);
            EnumerableAssert.HasCount(5, bytes);
            EnumerableAssert.HasCount(3, bytes.Where(b => b < 100));
            ExceptionAssert.ThrowsAssertFailed(() => EnumerableAssert.HasCount(6, bytes));
            ExceptionAssert.ThrowsAssertFailed(() => EnumerableAssert.HasCount(2, bytes.Where(b => b < 100)));
        }
        public void GetBytes()
        {
            var    length = 16 * 1024;
            Random random = new Random();
            var    bytes  = random.NextBytes(length);

            Assert.IsNotNull(bytes);
            EnumerableAssert.HasCount(length, bytes);

            length = 255;
            bytes  = random.NextBytes(length);
            EnumerableAssert.HasCount(length, bytes);
            Assert.IsTrue(bytes.Distinct().Count() >= length / 5, "Less than 20% of the elements are unique. This is highly unlikely to occur in a random sequence.");
        }
        public void Using()
        {
            bool throwMain    = false;
            bool throwDispose = false;

            foreach (var strategy in EnumUtility.GetValues <DisposeExceptionStrategy>())
            {
                TestStrategyNoException(strategy, throwMain, throwDispose);
            }

            throwMain    = true;
            throwDispose = false;
            TestStrategy <MainLogicException>(DisposeExceptionStrategy.Propagate, throwMain, throwDispose);
            TestStrategy <MainLogicException>(DisposeExceptionStrategy.Swallow, throwMain, throwDispose);
            TestStrategy <MainLogicException>(DisposeExceptionStrategy.Subjugate, throwMain, throwDispose);
            TestStrategy <MainLogicException>(DisposeExceptionStrategy.AggregateMultiple, throwMain, throwDispose);
            TestStrategy <AggregateException>(DisposeExceptionStrategy.AggregateAlways, throwMain, throwDispose, e =>
                                              Assert.IsInstanceOfType(e.InnerExceptions.Single(), typeof(MainLogicException)));

            throwMain    = false;
            throwDispose = true;
            TestStrategy <DisposeException>(DisposeExceptionStrategy.Propagate, throwMain, throwDispose);
            TestStrategyNoException(DisposeExceptionStrategy.Swallow, throwMain, throwDispose);
            TestStrategy <DisposeException>(DisposeExceptionStrategy.Subjugate, throwMain, throwDispose);
            TestStrategy <DisposeException>(DisposeExceptionStrategy.AggregateMultiple, throwMain, throwDispose);
            TestStrategy <AggregateException>(DisposeExceptionStrategy.AggregateAlways, throwMain, throwDispose, e =>
                                              Assert.IsInstanceOfType(e.InnerExceptions.Single(), typeof(DisposeException)));

            throwMain    = true;
            throwDispose = true;
            TestStrategy <DisposeException>(DisposeExceptionStrategy.Propagate, throwMain, throwDispose);
            TestStrategy <MainLogicException>(DisposeExceptionStrategy.Swallow, throwMain, throwDispose);
            TestStrategy <MainLogicException>(DisposeExceptionStrategy.Subjugate, throwMain, throwDispose);
            TestStrategy <AggregateException>(DisposeExceptionStrategy.AggregateMultiple, throwMain, throwDispose, e =>
            {
                EnumerableAssert.HasCount(2, e.InnerExceptions);
                Assert.IsInstanceOfType(e.InnerExceptions[0], typeof(MainLogicException));
                Assert.IsInstanceOfType(e.InnerExceptions[1], typeof(DisposeException));
            });
            TestStrategy <AggregateException>(DisposeExceptionStrategy.AggregateAlways, throwMain, throwDispose, e =>
            {
                EnumerableAssert.HasCount(2, e.InnerExceptions);
                Assert.IsInstanceOfType(e.InnerExceptions[0], typeof(MainLogicException));
                Assert.IsInstanceOfType(e.InnerExceptions[1], typeof(DisposeException));
            });
        }
Exemplo n.º 4
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.");
                    }
                }
            }
        }