Beispiel #1
0
        public string Compile(string source, string language, string[] input, string[] output, int timelimit, int memorylimit)
        {
            source = HttpUtility.UrlDecode(source);

            var settings = new Settings
            {
                TestingDirectory = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()),
                Compilers = new List<Compiler>
                                                       {
                                                           Compiler.VC8Compiler,
                                                           Compiler.DotNet3Compiler,
                                                           Compiler.Delphi7Compiler,
                                                           Compiler.Java6Compiler
                                                       }
            };

            var p = new Program {Source = source, MemoryLimit = memorylimit, TimeLimit = timelimit};
            var tester = new CompilationTester(settings);
            Result testResult;

            switch (language)
            {
                case "CPP": p.Language = Language.VC8; break;
                case "CS": p.Language = Language.CSharp3; break;
                case "Delphi": p.Language = Language.Delphi7; break;
                case "Java": p.Language = Language.Java6; break;
                default: return "Unsupported language";
            }

            var compileResult = tester.Compile(p.Source, p.Language);

            if (!compileResult.Compiled)
            {
                testResult = new Result
                {
                    ProgramStatus = Status.CompilationError,
                    Output = compileResult.StandartOutput
                };

                return Enum.GetName(typeof(Status), testResult.ProgramStatus).Trim();
            }

            for (var i = 0; i < input.Length; i++)
            {
                p.InputTest = input[i];
                p.OutputTest = output[i];

                var result = tester.TestProgram(p);

                if (result.ProgramStatus != Status.Accepted)
                {
                    return (Enum.GetName(typeof(Status), result.ProgramStatus) + " Test: " + i).Trim();
                }
            }

            return "Accepted";
        }
        public void GenerateFileForCompilationTest()
        {
            Settings settings = FunctionContainer.CreateDefaultSetting();
            CompilationTester compilationTester = new CompilationTester(settings);
            string source, programPath;
            Language language;

            //language tests
            source = CompileServiceLanguageSourceCode.CSCorrectSourceCode;
            language = Language.CSharp3;
            programPath = compilationTester.GenerateFileForCompilation(source, language);
            Assert.AreEqual(true, CompileServiceHelper.ValidatePath(programPath));

            source = CompileServiceLanguageSourceCode.CPPCorrectSourceCode;
            language = Language.VC8;
            programPath = compilationTester.GenerateFileForCompilation(source, language);
            Assert.AreEqual(true, CompileServiceHelper.ValidatePath(programPath));

            source = CompileServiceLanguageSourceCode.JavaCorrectSourceCode;
            language = Language.Java6;
            programPath = compilationTester.GenerateFileForCompilation(source, language);
            Assert.AreEqual(true, CompileServiceHelper.ValidatePath(programPath));

            source = CompileServiceLanguageSourceCode.DelphiCorrectSourceCode;
            language = Language.Delphi7;
            programPath = compilationTester.GenerateFileForCompilation(source, language);
            Assert.AreEqual(true, CompileServiceHelper.ValidatePath(programPath));

            //incorrect source
            source = "";
            language = Language.CSharp3;
            try
            {
                programPath = compilationTester.GenerateFileForCompilation(source, language);
                Assert.AreEqual(false,true);
            }
            catch(Exception)
            {
                Assert.AreEqual(true,true);
            }

            //incorrect compiler
            settings.Compilers = new List<Compiler>();
            compilationTester = new CompilationTester(settings);
            source = "";
            language = Language.CSharp3;
            try
            {
                programPath = compilationTester.GenerateFileForCompilation(source, language);
                Assert.AreEqual(true, false);
            }
            catch (Exception)
            {
                Assert.AreEqual(true,true);
            }
        }
Beispiel #3
0
        public void ExecuteCorrectParametersTest()
        {
            CompilationTester tester = new CompilationTester(FunctionContainer.CreateDefaultSetting());
            string programPath;
            string exeString;
            Program program = FunctionContainer.CreateProgram("", 10000, 100000);
            Result result;

            try
            {
                //-----------CS-----------
                programPath = tester.GenerateFileForCompilation(CompileServiceLanguageSourceCode.CSCorrectSourceCode,
                                                                Language.CSharp3);
                exeString = Path.ChangeExtension(programPath, "exe");
                program.InputTest = "2 3";
                program.OutputTest = "23";
                program.Language = Language.CSharp3;
                result = Runner.Execute(exeString, program);
                Assert.AreEqual(result.ProgramStatus, Status.Accepted);

                //-----------CPP-----------
                programPath = tester.GenerateFileForCompilation(CompileServiceLanguageSourceCode.CPPCorrectSourceCode,
                                                Language.VC8);
                exeString = Path.ChangeExtension(programPath, "exe");
                program.InputTest = "2 3";
                program.OutputTest = "23";
                program.Language = Language.VC8;
                result = Runner.Execute(exeString, program);
                Assert.AreEqual(result.ProgramStatus, Status.Accepted);

                //-----------Java-----------
                programPath = tester.GenerateFileForCompilation(CompileServiceLanguageSourceCode.JavaCorrectSourceCode,
                                                Language.Java6);
                exeString = Path.ChangeExtension(programPath, "exe");
                program.InputTest = "2 3";
                program.OutputTest = "23";
                program.Language = Language.Java6;
                result = Runner.Execute(exeString, program);
                Assert.AreEqual(result.ProgramStatus, Status.Accepted);

                //-----------Delphi-----------
                programPath = tester.GenerateFileForCompilation(CompileServiceLanguageSourceCode.DelphiCorrectSourceCode,
                                                Language.Delphi7);
                exeString = Path.ChangeExtension(programPath, "exe");
                program.InputTest = "";
                program.OutputTest = "Hello, world!";
                program.Language = Language.Delphi7;
                result = Runner.Execute(exeString, program);
                Assert.AreEqual(result.ProgramStatus, Status.Accepted);
            }
            catch (Exception)
            {
                Assert.AreEqual(true, false);
            }
        }
Beispiel #4
0
        public void CompileTest()
        {
            CompilationTester tester = new CompilationTester(FunctionContainer.CreateDefaultSetting());
            string programPath;
            CompileResult result;

            #region CorrectLangugeSource

            try
            {
                //-----------CS-----------
                programPath = tester.GenerateFileForCompilation(CompileServiceLanguageSourceCode.CSCorrectSourceCode,
                                                                Language.CSharp3);
                result = Compiler.DotNet3Compiler.Compile(programPath);
                CompileServiceHelper.ValidateCorrectCompilationResult(result);

                //-----------CPP-----------
                programPath = tester.GenerateFileForCompilation(CompileServiceLanguageSourceCode.CPPCorrectSourceCode,
                                                Language.VC8);
                result = Compiler.VC8Compiler.Compile(programPath);
                CompileServiceHelper.ValidateCorrectCompilationResult(result);

                //-----------Java-----------
                programPath = tester.GenerateFileForCompilation(CompileServiceLanguageSourceCode.JavaCorrectSourceCode,
                                                Language.Java6);
                result = Compiler.Java6Compiler.Compile(programPath);
                CompileServiceHelper.ValidateCorrectCompilationResult(result);

                //-----------Delphi-----------
                programPath = tester.GenerateFileForCompilation(CompileServiceLanguageSourceCode.DelphiCorrectSourceCode,
                                                Language.Delphi7);
                result = Compiler.Delphi7Compiler.Compile(programPath);
                CompileServiceHelper.ValidateCorrectCompilationResult(result);
            }
            catch (Exception)
            {
                Assert.AreEqual(true,false);
            }
            
            #endregion

            #region IncorrectLangugeSource

            try
            {
                //-----------CS-----------
                programPath = tester.GenerateFileForCompilation(CompileServiceLanguageSourceCode.CSIncorrectSourceCode,
                                                                Language.CSharp3);
                result = Compiler.DotNet3Compiler.Compile(programPath);
                CompileServiceHelper.ValidateIncorrectCompilationResult(result);

                //-----------CPP-----------
                programPath = tester.GenerateFileForCompilation(CompileServiceLanguageSourceCode.CPPIncorrectSourceCode,
                                                Language.VC8);
                result = Compiler.VC8Compiler.Compile(programPath);
                CompileServiceHelper.ValidateIncorrectCompilationResult(result);

                //-----------Java-----------
                programPath = tester.GenerateFileForCompilation(CompileServiceLanguageSourceCode.JavaIncorrectSourceCode,
                                                Language.Java6);
                result = Compiler.Java6Compiler.Compile(programPath);
                CompileServiceHelper.ValidateIncorrectCompilationResult(result);

                //-----------Delphi-----------
                programPath = tester.GenerateFileForCompilation(CompileServiceLanguageSourceCode.DelphiIncorrectSourceCode,
                                                Language.Delphi7);
                result = Compiler.Delphi7Compiler.Compile(programPath);
                CompileServiceHelper.ValidateIncorrectCompilationResult(result);
            }
            catch (Exception)
            {
                Assert.AreEqual(true, false);
            }

            #endregion

            // bad input parameters
            try
            {
                result = Compiler.DotNet3Compiler.Compile(null);
                Assert.AreEqual(true,false);
            }
            catch (Exception)
            {
                Assert.AreEqual(true,true);
            }

            try
            {
                result = Compiler.DotNet3Compiler.Compile("");
                Assert.AreEqual(true, false);
            }
            catch (Exception)
            {
                Assert.AreEqual(true, true);
            }
        }
        public void CompilationTesterConstructorTest()
        {
            Settings settings = new Settings()
                                    {
                                        TestingDirectory = "Some path",
                                        Compilers = new List<Compiler>()
                                    };

            CompilationTester tester = new CompilationTester(settings);
            Assert.AreEqual(settings, tester.Settings);

            try
            {
                settings = null;
                tester = new CompilationTester(settings);
                Assert.AreEqual(false, true);
            }
            catch (Exception)
            {
                Assert.AreEqual(true,true);
            }
        }
Beispiel #6
0
 public static CompilationTester CreateCompilationTester(Settings settings)
 {
     var tester = new CompilationTester(settings);
     return tester;
 }