Example #1
0
        protected void JudgeAllCases(BaseSingleCaseJudger judger, ProgramTestDataFile[] dataFiles)
        {
            JudgeResult result             = JudgeResult;
            int         acceptedCasesCount = 0;

            foreach (var dataFile in dataFiles)
            {
                try
                {
                    ProgramTestData   testData     = TestDataManager.GetTestData(JudgeTask.ProblemId, dataFile);
                    SingleJudgeResult singleResult = judger.Judge(testData.Input, testData.Output);

                    if (result.ResultCode == JudgeResultCode.Accepted)
                    {
                        // 计算有时间补偿的总时间
                        result.TimeCost =
                            Math.Max(result.TimeCost, (int)(singleResult.TimeCost * LangConfig.TimeCompensation));
                        result.MemoryCost = Math.Max(result.MemoryCost, singleResult.MemoryCost);
                    }

                    if (singleResult.ResultCode == JudgeResultCode.Accepted)
                    {
                        acceptedCasesCount++;
                    }
                    else
                    {
                        // 出错时只记录第一组出错的信息
                        if (result.ResultCode == JudgeResultCode.Accepted)
                        {
                            result.ResultCode  = singleResult.ResultCode;
                            result.JudgeDetail = singleResult.JudgeDetail;
                        }

                        if (!JudgeTask.JudgeAllCases)
                        {
                            break;
                        }
                    }
                }
                catch (Exception e)
                {
                    result.ResultCode  = JudgeResultCode.JudgeFailed;
                    result.JudgeDetail = e.ToString();
                    throw new ExpectedJudgeStopException("Judge failed");
                }
            }

            result.JudgeDetail = result.JudgeDetail.Replace(Context.TempDirectory, "");
            result.PassRate    = (double)acceptedCasesCount / dataFiles.Length;
        }
Example #2
0
        public override void Judge()
        {
            //判题结果
            JudgeResult result = Context.Result;

            //正则恶意代码检查
            if (!CodeChecker.Singleton.CheckCode(JudgeTask.SourceCode, JudgeTask.Language, out string unsafeCode,
                                                 out int line))
            {
                result.ResultCode   = JudgeResultCode.CompileError;
                result.JudgeDetail  = "Include unsafe code, please remove them!";
                result.JudgeDetail += "\r\n";
                result.JudgeDetail += "line " + line + ": " + unsafeCode;
                return;
            }

            //写出源代码
            string sourceFileName = Path.Combine(Context.TempDirectory, LangConfig.SourceCodeFileName);

            File.WriteAllText(sourceFileName, JudgeTask.SourceCode);

            //编译代码
            if (LangConfig.NeedCompile)
            {
                Compiler compiler   = new Compiler(Context);
                string   compileRes = compiler.Compile();

                //检查是否有编译错误(compileRes不为空则代表有错误)
                if (!string.IsNullOrEmpty(compileRes))
                {
                    //去除路径信息
                    result.JudgeDetail = compileRes.Replace(Context.TempDirectory, "");
                    result.ResultCode  = JudgeResultCode.CompileError;
                    result.MemoryCost  = 0;
                    return;
                }
            }

            //创建单例Judger
            SingleCaseJudger judger = new SingleCaseJudger(Context);

            //获取所有测试点文件名
            Tuple <string, string>[] dataFiles = TestDataManager.GetTestDataFilesName(JudgeTask.ProblemId);
            if (dataFiles.Length == 0) //无测试数据
            {
                result.ResultCode  = JudgeResultCode.JudgeFailed;
                result.JudgeDetail = "No test data.";
                return;
            }

            int acceptedCasesCount = 0; //通过的测试点数

            for (int i = 0; i < dataFiles.Length; i++)
            {
                try
                {
                    //读入测试数据
                    TestDataManager.GetTestData(
                        JudgeTask.ProblemId, dataFiles[i].Item1, dataFiles[i].Item2,
                        out string input, out string output);

                    SingleJudgeResult singleRes = judger.Judge(input, output); //测试此测试点

                    // 评测所有测试点时, 只记录第一组出错的信息
                    if (result.ResultCode == JudgeResultCode.Accepted)
                    {
                        // 计算有时间补偿的总时间
                        result.TimeCost = Math.Max(result.TimeCost,
                                                   (int)(singleRes.TimeCost * LangConfig.TimeCompensation));
                        result.MemoryCost = Math.Max(result.MemoryCost, singleRes.MemoryCost);
                    }

                    if (singleRes.ResultCode == JudgeResultCode.Accepted)
                    {
                        acceptedCasesCount++;
                    }
                    else
                    {
                        // 出错时记录第一组出错的信息
                        if (result.ResultCode == JudgeResultCode.Accepted)
                        {
                            result.ResultCode  = singleRes.ResultCode;
                            result.JudgeDetail = singleRes.JudgeDetail;
                        }

                        if (!JudgeTask.JudgeAllCases)
                        {
                            break;
                        }
                    }
                }
                catch (Exception e)
                {
                    result.ResultCode  = JudgeResultCode.JudgeFailed;
                    result.JudgeDetail = e.ToString();
                    break;
                }
            }

            //去除目录信息
            result.JudgeDetail = result.JudgeDetail.Replace(Context.TempDirectory, "");

            //通过率
            result.PassRate = (double)acceptedCasesCount / dataFiles.Length;
        }
Example #3
0
        public override void Judge()
        {
            JudgeResult result = Context.Result;

            if (!CodeChecker.Instance.CheckCode(
                    JudgeTask.SourceCode, JudgeTask.Language,
                    out string unsafeCode, out int line))
            {
                result.ResultCode   = JudgeResultCode.CompileError;
                result.JudgeDetail  = "Include unsafe code, please remove them!";
                result.JudgeDetail += "\r\n";
                result.JudgeDetail += "line " + line + ": " + unsafeCode;
                return;
            }

            // 构建SPJ程序
            BuildSpecialJudgeProgram();

            string sourceFileName = Path.Combine(Context.TempDirectory + LangConfig.SourceCodeFileName);

            File.WriteAllText(sourceFileName, JudgeTask.SourceCode);

            if (LangConfig.NeedCompile)
            {
                Compiler compiler   = new Compiler(Context);
                string   compileRes = compiler.Compile();

                if (!string.IsNullOrEmpty(compileRes))
                {
                    // 去除路径信息
                    result.JudgeDetail = compileRes.Replace(Context.TempDirectory, "");
                    result.ResultCode  = JudgeResultCode.CompileError;
                    result.MemoryCost  = 0;
                    return;
                }
            }

            SpecialSingleCaseJudger judger = new SpecialSingleCaseJudger(Context, SpjContext);

            ProgramTestDataFile[] dataFiles = TestDataManager.GetTestDataFilesName(JudgeTask.ProblemId);
            if (dataFiles.Length == 0)
            {
                result.ResultCode  = JudgeResultCode.JudgeFailed;
                result.JudgeDetail = "No test data.";
                return;
            }

            int acceptedCasesCount = 0;

            for (int i = 0; i < dataFiles.Length; i++)
            {
                try
                {
                    ProgramTestData data = TestDataManager.GetTestData(JudgeTask.ProblemId, dataFiles[i]);

                    SingleJudgeResult singleRes = judger.Judge(data.Input, data.Output);

                    if (result.ResultCode == JudgeResultCode.Accepted)
                    {
                        result.TimeCost = Math.Max(result.TimeCost,
                                                   (int)(singleRes.TimeCost * LangConfig.TimeCompensation));
                        result.MemoryCost = Math.Max(result.MemoryCost, singleRes.MemoryCost);
                    }

                    if (singleRes.ResultCode == JudgeResultCode.Accepted)
                    {
                        acceptedCasesCount++;
                    }
                    else
                    {
                        if (result.ResultCode == JudgeResultCode.Accepted)
                        {
                            result.ResultCode  = singleRes.ResultCode;
                            result.JudgeDetail = singleRes.JudgeDetail;
                        }

                        if (!JudgeTask.JudgeAllCases)
                        {
                            break;
                        }
                    }
                }
                catch (Exception e)
                {
                    result.ResultCode  = JudgeResultCode.JudgeFailed;
                    result.JudgeDetail = e.ToString();
                    break;
                }
            }

            result.JudgeDetail = result.JudgeDetail.Replace(Context.TempDirectory, "");
            result.PassRate    = (double)acceptedCasesCount / dataFiles.Length;
        }
Example #4
0
        /// <summary>
        /// 评测此任务
        /// </summary>
        /// <returns>评测结果</returns>
        public JudgeResult Judge()
        {
            //判题结果
            JudgeResult result = new JudgeResult
            {
                SubmitID    = JudgeTask.SubmitID,
                ProblemID   = JudgeTask.ProblemID,
                Author      = JudgeTask.Author,
                JudgeDetail = "",
                MemoryCost  = 0,
                TimeCost    = 0,
                PassRate    = 0,
                ResultCode  = JudgeResultCode.Accepted
            };

            //正则恶意代码检查
            if (!CodeChecker.Singleton.CheckCode(JudgeTask.SourceCode, JudgeTask.Language, out string unsafeCode, out int line))
            {
                result.ResultCode   = JudgeResultCode.CompileError;
                result.JudgeDetail  = "Include unsafe code, please remove them!";
                result.JudgeDetail += "\r\n";
                result.JudgeDetail += "line " + line + ": " + unsafeCode;
                return(result);
            }

            //创建临时目录
            if (!Directory.Exists(JudgeTask.TempJudgeDirectory))
            {
                Directory.CreateDirectory(JudgeTask.TempJudgeDirectory);
            }

            //写出源代码
            string sourceFileName = JudgeTask.TempJudgeDirectory + Path.DirectorySeparatorChar + JudgeTask.LangConfig.SourceCodeFileName;

            File.WriteAllText(sourceFileName, JudgeTask.SourceCode);

            //编译代码
            if (JudgeTask.LangConfig.NeedCompile)
            {
                ICompiler compiler = CompilerFactory.Create(JudgeTask);
                //使用分配的独立处理器核心
                compiler.ProcessorAffinity = _affinity.Affinity;

                string compileRes = compiler.Compile(JudgeTask.LangConfig.CompilerArgs);

                //检查是否有编译错误(compileRes不为空则代表有错误)
                if (!string.IsNullOrEmpty(compileRes))
                {
                    result.JudgeDetail = compileRes.Replace(JudgeTask.TempJudgeDirectory, "");//去除路径信息
                    result.ResultCode  = JudgeResultCode.CompileError;
                    return(result);
                }
            }


            //创建单例Judger
            ISingleJudger judger = SingleJudgerFactory.Create(JudgeTask);

            judger.ProcessorAffinity = _affinity.Affinity;

            //获取所有测试点文件名
            Tuple <string, string>[] dataFiles = TestDataManager.GetTestDataFilesName(JudgeTask.ProblemID);
            if (dataFiles.Length == 0)//无测试数据
            {
                result.ResultCode  = JudgeResultCode.JudgeFailed;
                result.JudgeDetail = "No test data.";
                return(result);
            }

            int acceptedCasesCount = 0;//通过的测试点数

            for (int i = 0; i < dataFiles.Length; i++)
            {
                try
                {
                    TestDataManager.GetTestData(JudgeTask.ProblemID, dataFiles[i].Item1, dataFiles[i].Item2, out string input, out string output); //读入测试数据

                    SingleJudgeResult singleRes = judger.Judge(input, output);                                                                     //测试此测试点

                    //计算有时间补偿的总时间
                    result.TimeCost   = Math.Max(result.TimeCost, (int)(singleRes.TimeCost * JudgeTask.LangConfig.TimeCompensation));
                    result.MemoryCost = Math.Max(result.MemoryCost, singleRes.MemoryCost);

                    if (singleRes.ResultCode == JudgeResultCode.Accepted)
                    {
                        acceptedCasesCount++;
                    }
                    else
                    {
                        result.ResultCode  = singleRes.ResultCode;
                        result.JudgeDetail = singleRes.JudgeDetail;
                        break;
                    }
                }
                catch (Exception e)
                {
                    result.ResultCode  = JudgeResultCode.JudgeFailed;
                    result.JudgeDetail = e.ToString();
                    break;
                }
            }

            //去除目录信息
            result.JudgeDetail = result.JudgeDetail.Replace(JudgeTask.TempJudgeDirectory, "");

            //通过率
            result.PassRate = (double)acceptedCasesCount / dataFiles.Length;

            return(result);
        }