Пример #1
0
        private static async Task <ProcessExecutionResult> ProcessResult(string input, string dllFilePath)
        {
            ProcessExecutionResult processExecutionResult = new ProcessExecutionResult();
            string commandPromptArgument = @"/C dotnet " + dllFilePath;

            using (Process process = new Process())
            {
                process.StartInfo.FileName               = "cmd.exe";
                process.StartInfo.Arguments              = commandPromptArgument;
                process.StartInfo.RedirectStandardInput  = true;
                process.StartInfo.RedirectStandardOutput = true;
                process.StartInfo.RedirectStandardError  = true;
                process.StartInfo.CreateNoWindow         = true;
                process.StartInfo.UseShellExecute        = false;

                process.Start();
                process.PriorityClass = ProcessPriorityClass.High;
                await process.StandardInput.WriteLineAsync(input);

                await process.StandardInput.FlushAsync();

                process.StandardInput.Close();
                processExecutionResult.MemoryUsed = process.PrivateMemorySize64;
                bool exited = process.WaitForExit(ProcessMaxRunningTime);

                if (!exited)
                {
                    process.Kill();
                    processExecutionResult.Type = ProcessExecutionResultType.TimeLimit;
                }

                string output = await process.StandardOutput.ReadToEndAsync();

                string errors = await process.StandardError.ReadToEndAsync();

                processExecutionResult.ErrorOutput             = errors;
                processExecutionResult.ReceivedOutput          = output;
                processExecutionResult.ExitCode                = process.ExitCode;
                processExecutionResult.TimeWorked              = process.ExitTime - process.StartTime;
                processExecutionResult.PrivilegedProcessorTime = process.PrivilegedProcessorTime;
                processExecutionResult.UserProcessorTime       = process.UserProcessorTime;

                if (processExecutionResult.TotalProcessorTime.TotalMilliseconds > TimeLimit)
                {
                    processExecutionResult.Type = ProcessExecutionResultType.TimeLimit;
                }

                if (!string.IsNullOrEmpty(processExecutionResult.ErrorOutput))
                {
                    processExecutionResult.Type = ProcessExecutionResultType.RunTimeError;
                }

                if (processExecutionResult.MemoryUsed > MemoryLimit)
                {
                    processExecutionResult.Type = ProcessExecutionResultType.MemoryLimit;
                }

                return(processExecutionResult);
            }
        }
Пример #2
0
        public static void Main(string[] args)
        {
            Console.WriteLine("Enter 'zip' if you want to test code from zip file or 'cs' if you want to test only one .cs file");
            string typeOfSourceCode = Console.ReadLine();

            CSharpCompilation compilation    = null;
            string            assemblyName   = "Program";
            string            input          = "";
            string            expectedOutput = "";

            if (typeOfSourceCode == "zip")
            {
                ZipParser parser      = new ZipParser();
                var       sourceCodes = parser.ExtractZipFile("../../../../ZipSubmissionTest/Solution.zip");

                input          = "Audi 2005\r\nBMW 2001\r\nend";
                expectedOutput = $"Audi => 2005\r\nBMW => 2001";

                compilation = BuildCSharpCompilation(sourceCodes, assemblyName);
            }
            else if (typeOfSourceCode == "cs")
            {
                input          = "Pesho\r\n15\r\nPlovdiv";
                expectedOutput = $"I am Pesho - 15 years old. I am from Plovdiv.";

                string fileName   = "Program.cs";
                string filePath   = $"../../../../Test/{fileName}";
                string sourceCode = GetCSharpTestCode(filePath);
                compilation = BuildCSharpCompilation(new List <string>()
                {
                    sourceCode
                }, assemblyName);
            }

            string     outputDllName                = assemblyName + ".dll";
            string     dllFilePath                  = new FileInfo(outputDllName).FullName;
            string     workingDirectory             = new FileInfo(outputDllName).DirectoryName;
            EmitResult emitResult                   = compilation.Emit(outputDllName);
            string     runtimeConfigJsonFileContent = GenerateRuntimeConfigJsonFile();
            string     runtimeConfigJsonFilePath    = workingDirectory + "\\" + assemblyName + ".runtimeconfig.json";

            File.WriteAllText(runtimeConfigJsonFilePath, runtimeConfigJsonFileContent);

            if (!emitResult.Success)
            {
                StringBuilder errors = new StringBuilder();

                IEnumerable <Diagnostic> failures = emitResult.Diagnostics
                                                    .Where(diagnostic => diagnostic.Severity == DiagnosticSeverity.Error);

                foreach (Diagnostic diagnostic in failures)
                {
                    errors.AppendLine($"{diagnostic.Location.GetLineSpan()} {diagnostic.GetMessage()}");
                }

                Console.WriteLine("Compile time error");
                Console.WriteLine(errors.ToString());
                return;
            }

            ProcessExecutionResult processExecutionResult = ProcessResult(input, dllFilePath).GetAwaiter().GetResult();

            if (processExecutionResult.IsSuccesfull && processExecutionResult.Type == ProcessExecutionResultType.Success)
            {
                Console.WriteLine("The application was run seccessfully!");
                Console.WriteLine(processExecutionResult.ReceivedOutput);
                Console.WriteLine($"Is test correct: {processExecutionResult.ReceivedOutput.Trim() == expectedOutput}");
            }
            else
            {
                Console.WriteLine("Some errors occured!");
                Console.WriteLine(processExecutionResult.ErrorOutput);
            }

            Console.WriteLine("Process statistics");
            Console.WriteLine($"Exit code: {processExecutionResult.ExitCode}");
            Console.WriteLine($"Memory Used: {processExecutionResult.MemoryUsed}");
            Console.WriteLine($"Processor time used: {processExecutionResult.TotalProcessorTime}");
            Console.WriteLine($"Working time: {processExecutionResult.TimeWorked}");
            Console.WriteLine($"Execution Type: {processExecutionResult.Type}");
        }