public void InvertMatricesSharedState_Added4NonInvertibleMatrices_ReturnedCountOfNonInvertableMatrices()
        {
            // ARRANGE
            const int      ExpectedCount = 5000;
            Mock <IMatrix> matrix;
            var            matrices = new List <IMatrix>();

            for (int i = 0; i < 10000; i++)
            {
                matrix = new Mock <IMatrix>();

                if (i % 2 == 0)
                {
                    matrix.Setup(context => context.IsInvertible).Returns(true);
                }
                else
                {
                    matrix.Setup(context => context.IsInvertible).Returns(false);
                }

                matrices.Add(matrix.Object);
            }

            // ACT
            int nonInvertableMatrices = ParallelProcessingOfData.InvertMatricesSharedState(matrices);

            // ASSERT
            Assert.AreEqual(ExpectedCount, nonInvertableMatrices, "nonInvertableMatrices has an unexpected value.");
        }
        public void RotateMatrices_AddedSomeMatrices_ReturnsLoopResult()
        {
            // ARRANGE
            var mockMatrix = new Mock <IMatrix>();

            List <IMatrix> matrices = new List <IMatrix>();

            for (int i = 0; i < 100; i++)
            {
                matrices.Add(mockMatrix.Object);
            }

            // ACT
            ParallelLoopResult result = ParallelProcessingOfData.RotateMatrices(matrices, 45f);

            // ASSERT
            Assert.True(result.IsCompleted, "result.IsCompleted has unexpected value.");
        }
        public void RotateMatrices_AddedCancellationToken_ThrowsOperationCancelledException()
        {
            // ARRANGE
            var mockMatrix = new Mock <IMatrix>();

            mockMatrix.Setup(context => context.IsInvertible).Returns(true);

            List <IMatrix> matrices = new List <IMatrix>();

            for (int i = 0; i < 100; i++)
            {
                matrices.Add(mockMatrix.Object);
            }

            var cts = new CancellationTokenSource();

            // ACT

            Task.Factory.StartNew(
                () =>
            {
                Thread.Sleep(3000);
                cts.Cancel();
            });

            Exception exception = null;

            try
            {
                ParallelProcessingOfData.RotateMatrices(matrices, 45f, cts.Token);
            }
            catch (Exception ex)
            {
                exception = ex;
                System.Diagnostics.Debug.WriteLine(
                    string.Format("Parallel loop was cancelled an raised '{0}'", ex.GetType().Name));
            }

            // ASSERT
            Assert.NotNull(exception, "exception is null.");
            Assert.IsInstanceOf <OperationCanceledException>(exception, "exception has unexpected type.");
        }
        public void InvertMatrices_AddedSomeMatrices_ReturnsLoopResultCompleted()
        {
            // ARRANGE
            var mockMatrix = new Mock <IMatrix>();

            mockMatrix.Setup(context => context.IsInvertible).Returns(true);

            List <IMatrix> matrices = new List <IMatrix>();

            for (int i = 0; i < 100; i++)
            {
                matrices.Add(mockMatrix.Object);
            }

            // ACT
            ParallelLoopResult result = ParallelProcessingOfData.InvertMatrices(matrices);

            // ASSERT
            Assert.True(result.IsCompleted, "result.IsCompleted has unexpected value.");
        }