예제 #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;
        }
예제 #2
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;
            }

            // 写出源代码
            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);

            // 获取所有测试点文件名
            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;
        }
예제 #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;
        }