public async Task AddSolutions()
        {
            // Arrange
            var solutions = new List <SudokuSolution>();

            solutions.Add(newSolution);

            // Act
            var result = await sut.AddSolutions(solutions.ConvertAll(s => (ISudokuSolution)s));

            // Assert
            Assert.That(result.Success, Is.True);
        }
Beispiel #2
0
        public async Task <IBaseResult> AddSolutions(int limitArg)
        {
            var result = new BaseResult();

            if (limitArg == 0)
            {
                result.Success = false;
                result.Message = SolutionsMessages.SolutionsNotAddedMessage;

                return(result);
            }

            var limit = 1000;

            if (limitArg <= limit)
            {
                limit = limitArg;
            }

            var reduceLimitBy = 0;

            var solutionsInDB = new List <List <int> >();

            try
            {
                var solutions = (await solutionsRepository.GetSolvedSolutions())
                                .Objects.ConvertAll(s => (SudokuSolution)s);

                foreach (var solution in solutions)
                {
                    solutionsInDB.Add(solution.SolutionList);
                }
            }
            catch (Exception exp)
            {
                result.Success = false;
                result.Message = exp.Message;

                return(result);
            }

            var matrix = new SudokuMatrix();

            try
            {
                List <List <int> >    solutionsList = new List <List <int> >();
                List <SudokuSolution> newSolutions  = new List <SudokuSolution>();

                var continueLoop = true;

                do
                {
                    for (var i = 0; i < limit - reduceLimitBy; i++)
                    {
                        matrix.GenerateSolution();

                        if (!solutionsInDB.Contains(matrix.ToIntList()))
                        {
                            solutionsList.Add(matrix.ToIntList());
                        }
                    }

                    solutionsList = solutionsList
                                    .Distinct()
                                    .ToList();

                    if (limit == solutionsList.Count)
                    {
                        continueLoop = false;
                    }
                    else
                    {
                        reduceLimitBy = limit - solutionsList.Count;
                    }
                } while (continueLoop);

                foreach (var solutionList in solutionsList)
                {
                    newSolutions.Add(new SudokuSolution(solutionList));
                }

                var solutionsResponse = await solutionsRepository
                                        .AddSolutions(newSolutions.ConvertAll(s => (ISudokuSolution)s));

                if (solutionsResponse.Success)
                {
                    result.Success = solutionsResponse.Success;
                    result.Message = SolutionsMessages.SolutionsAddedMessage;

                    return(result);
                }
                else if (!solutionsResponse.Success && solutionsResponse.Exception != null)
                {
                    result.Success = solutionsResponse.Success;
                    result.Message = solutionsResponse.Exception.Message;

                    return(result);
                }
                else
                {
                    result.Success = false;
                    result.Message = SolutionsMessages.SolutionsNotAddedMessage;

                    return(result);
                }
            }
            catch (Exception exp)
            {
                result.Success = false;
                result.Message = exp.Message;

                return(result);
            }
        }