コード例 #1
0
        private static async Task ProcessDirectory(string directory)
        {
            if (!Directory.Exists(directory))
            {
                throw new ArgumentException($"Directory {directory} doesn't exist", directory);
            }

            var files = Directory
                        .GetFiles(directory)
                        .Select(x => Path.Combine(directory, x))
                        .ToList(); // Полные пути исходных файлов

            var operationFactory = new MatrixOperationFactory(Logger);
            var streamReader     = new StreamMatrixReader(operationFactory);
            var streamWriter     = new StreamMatrixWriter();
            var progressReporter = new ProgressReporter(files.Count, Logger);

            await files.ParallelForEachAsync(async file =>
            {
                using var input = new StreamReader(file);

                var resultFile         = Path.Combine(directory, Path.GetFileNameWithoutExtension(file) + "_result.txt"); // Полный путь до файла с результатом
                await using var output = new StreamWriter(resultFile);

                var matrices = streamReader.GetMatrices(input);
                await streamWriter.WriteMatricesAsync(output, matrices);

                progressReporter.Report(file);
            });
        }
コード例 #2
0
        public void GetMatrices_TwoMatrcesInInput_TwoMatrcesInOutput()
        {
            var factory      = new NoOpOperationFactory();
            var matrixReader = new StreamMatrixReader(factory);
            var inputString  = $"noop{Environment.NewLine}{Environment.NewLine}1{Environment.NewLine}{Environment.NewLine}1{Environment.NewLine}";
            var stringReader = new StringReader(inputString);
            var matrices     = matrixReader.GetMatrices(stringReader);

            Assert.Equal(2, matrices.Count());
        }
コード例 #3
0
        public void GetMatrices_OneMatrixInInput_OneMatrixInOutput()
        {
            var factory      = new NoOpOperationFactory();
            var matrixReader = new StreamMatrixReader(factory);
            var inputString  = $"noop{Environment.NewLine}{Environment.NewLine}1{Environment.NewLine}";
            var stringReader = new StringReader(inputString);
            var matrices     = matrixReader.GetMatrices(stringReader);

            Assert.Single(matrices);
        }
コード例 #4
0
        public void GetMatrices_OperationInInput_IMatrixOperationFactoryCreateCalledWithParameter(string opName)
        {
            var mock = Mock.Get(Mock.Of <IMatrixOperationFactory>());

            mock.Setup(x => x.Create(It.IsAny <string>())).Returns(new NoOpOperation()).Verifiable();
            var matrixReader = new StreamMatrixReader(mock.Object);
            var inputString  = $"{opName}{Environment.NewLine}{Environment.NewLine}1{Environment.NewLine}";
            var stringReader = new StringReader(inputString);
            var matrices     = matrixReader.GetMatrices(stringReader);

            mock.Verify(x => x.Create(It.Is <string>(x => x == opName)), Times.Exactly(1));
        }
コード例 #5
0
        public void GetMatrices_InconsistentAmountOfColumnsInInput_ThrowsInvalidOperationException()
        {
            var factory      = new NoOpOperationFactory();
            var matrixReader = new StreamMatrixReader(factory);
            var inputString  = $"noop{Environment.NewLine}{Environment.NewLine}1{Environment.NewLine}1 2{Environment.NewLine}";
            var stringReader = new StringReader(inputString);

            Assert.Throws <InvalidOperationException>(() =>
            {
                matrixReader.GetMatrices(stringReader).ToList();
            });
        }
コード例 #6
0
        public void GetMatrices_TwoByTwoMatrixInInput_ProperOutput()
        {
            var factory          = new NoOpOperationFactory();
            var matrixReader     = new StreamMatrixReader(factory);
            var inputString      = $"noop{Environment.NewLine}{Environment.NewLine}1 2{Environment.NewLine}3 4{Environment.NewLine}";
            var stringReader     = new StringReader(inputString);
            var matrices         = matrixReader.GetMatrices(stringReader);
            var expectedMatrices = new Matrix[] { new Matrix(new int[2, 2] {
                    { 1, 2 }, { 3, 4 }
                }) };

            Assert.Equal(expectedMatrices, matrices, new MatrixComparer());
        }