Exemplo n.º 1
0
        public void Compile_GraphFileGiven_ShouldBeSuccessful()
        {
            userProgram.SetSource(@"TestFiles\test_graph.cpp");
            var result = userProgram.Compile();

            Assert.Equal(successful, result.Status);
        }
Exemplo n.º 2
0
        public void Compile_TestWithSyntaxError_ShouldFail()
        {
            userProgram.SetSource(@"TestFiles\test_syntax_error.cpp");
            var result = userProgram.Compile();

            Assert.Equal(failed, result.Status);
        }
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            if (args.Length < 1)
            {
                Console.WriteLine("Please enter a file path");
                return;
            }

            UserProgram userProgram = new UserProgram();

            UserProgram.Result sourceResult = userProgram.SetSource(args[0]);

            Console.WriteLine(sourceResult.Message);
            if (sourceResult.Status == UserProgram.Result.StatusType.Failed)
            {
                return;
            }

            UserProgram.Result compilationResult = userProgram.Compile();

            Console.WriteLine(compilationResult.Message);
            if (compilationResult.Status == UserProgram.Result.StatusType.Failed)
            {
                return;
            }

            UserProgram.Result programResult = userProgram.Execute(args.Length < 2 ? "" : args[1]);

            Console.WriteLine(programResult.Message);
            if (programResult.Status == UserProgram.Result.StatusType.Failed)
            {
                return;
            }

            string expectedOutput = args.Length < 3 ? "Hello World!" : args[2];

            UserProgram.Result outputResult = userProgram.Evaluate(expectedOutput);
            Console.WriteLine(outputResult.Message);
        }
Exemplo n.º 4
0
        public async Task <IActionResult> Result(int id, string filePath)
        {
            Problem problem = await dbContext.Problems.Include(m => m.Tests).FirstOrDefaultAsync(p => p.ProblemID == id);

            if (problem == null)
            {
                return(RedirectToAction(nameof(List)));
            }

            if (!System.IO.File.Exists(filePath))
            {
                return(RedirectToAction(nameof(Solve), new { id }));
            }

            string parentFolderPath = Directory.GetParent(filePath).FullName;

            ResultViewModel viewModel = new ResultViewModel()
            {
                ProblemID           = problem.ProblemID,
                Name                = problem.Name,
                ShowFailedTestCases = problem.ShowFailedTestCases
            };
            UserProgram userProgram = new UserProgram();

            var result = userProgram.SetSource(filePath);

            viewModel.CompilationResult = result;

            if (result.Status == UserProgram.Result.StatusType.Failed)
            {
                Directory.Delete(parentFolderPath, true);

                return(View(viewModel));
            }

            result = userProgram.Compile();
            viewModel.CompilationResult = result;

            if (result.Status == UserProgram.Result.StatusType.Failed)
            {
                Directory.Delete(parentFolderPath, true);

                viewModel.CompilationResult = result;
                return(View(viewModel));
            }

            int passed = 0;

            foreach (Test t in problem.Tests)
            {
                result = userProgram.Execute(t.GivenInput);
                ResultViewModel.TestResult testResult = new ResultViewModel.TestResult()
                {
                    Test = t, ExecutionResult = result
                };

                if (result.Status == UserProgram.Result.StatusType.Successful)
                {
                    // Format expected output lines
                    string[] expectedOutputLines = t.ExpectedOutput.Split('\n');
                    for (int i = 0; i < expectedOutputLines.Length; i++)
                    {
                        expectedOutputLines[i] = expectedOutputLines[i].Trim('\u202c').Trim();
                    }

                    result = userProgram.EvaluateAndGetResultIfFailed(expectedOutputLines);

                    if (result.Status == UserProgram.Result.StatusType.Successful)
                    {
                        passed++;
                    }

                    testResult.EvaluationResult = result;
                    viewModel.TestResults.Add(testResult);
                }
                else
                {
                    viewModel.TestResults.Add(testResult);
                }
            }

            viewModel.PassedTests = passed;

            try
            {
                Directory.Delete(parentFolderPath, true);
            }
            catch (UnauthorizedAccessException) // Sometimes the process isn't killed fast enough
            {
                Thread.Sleep(100);
                Directory.Delete(parentFolderPath, true);
            }

            ApplicationUser partialUser = await userManager.GetUserAsync(User);

            ApplicationUser currentUser = await userManager.Users
                                          .Include(u => u.ProblemResults)
                                          .ThenInclude(r => r.FirstResult)
                                          .Include(u => u.ProblemResults)
                                          .ThenInclude(r => r.BestResult)
                                          .FirstOrDefaultAsync(u => u == partialUser);

            if (currentUser != null)
            {
                bool hasProblem           = currentUser.ProblemResults.Exists(r => r.ProblemID == problem.ProblemID);
                bool gotPerfectPercentage = false;
                if (hasProblem)
                {
                    ProblemResult problemResult  = currentUser.ProblemResults.Where(r => r.ProblemID == problem.ProblemID).First();
                    int           bestPercentage = problemResult.BestResult.PercentageResult;
                    gotPerfectPercentage = bestPercentage < 100;
                }

                bool isAdmin = await userManager.IsInRoleAsync(currentUser, "Admin");

                // A unique user solved the problem and is not an Admin
                if (passed == problem.Tests.Count && (!hasProblem || (hasProblem && gotPerfectPercentage)) && !isAdmin)
                {
                    problem.TimesSolved++;

                    if (await TryUpdateModelAsync(problem))
                    {
                        try
                        {
                            await dbContext.SaveChangesAsync();
                        }
                        catch (DbUpdateException)
                        {
                            return(View(viewModel));
                        }
                    }
                }

                ProgramResult programResult = viewModel.GetProgramResult();
                if (currentUser.ProblemResults.Exists(r => r.ProblemID == problem.ProblemID))
                {
                    // Tried to solve the problem again
                    ProblemResult problemResult = currentUser.ProblemResults.First(r => r.ProblemID == problem.ProblemID);
                    if (programResult.PercentageResult > problemResult.BestResult.PercentageResult)
                    {
                        if (problemResult.BestResult != problemResult.FirstResult)
                        {
                            dbContext.Remove(problemResult.BestResult);
                        }
                        problemResult.BestResult = programResult;
                    }
                }
                else
                {
                    // Solved this problem for the first time
                    ProblemResult problemResult = new ProblemResult()
                    {
                        ProblemID   = problem.ProblemID,
                        FirstResult = programResult,
                        BestResult  = programResult
                    };
                    currentUser.ProblemResults.Add(problemResult);
                }

                await userManager.UpdateAsync(currentUser);
            }

            return(View(viewModel));
        }