Пример #1
0
        /// <summary>
        /// Run a test
        /// </summary>
        public void TestProgram(string ProgramFilename, DataPair TestData, bool WaitForExit)
        {
            Results.Remove(Results.FirstOrDefault(r => r.Index == Data.Datas.IndexOf(TestData)));
            //define basic options
            var testProcess = new Process();

            testProcess.StartInfo.FileName = ProgramFilename;
            //testProcess.StartInfo.UserName = JudgerManager.RunningUser == null ? Environment.UserName : JudgerManager.RunningUser.Username;
            //testProcess.StartInfo.Password = JudgerManager.RunningUser == null ? new SecureString() : JudgerManager.RunningUser.Password;
            testProcess.StartInfo.UseShellExecute        = false;
            testProcess.StartInfo.CreateNoWindow         = true;
            testProcess.StartInfo.RedirectStandardInput  = true;
            testProcess.StartInfo.RedirectStandardOutput = true;
            testProcess.StartInfo.RedirectStandardError  = true;
            var result = new JudgeResult();

            testProcess.Start();
            var startTime = DateTime.Now;

            //input sample data
            testProcess.StandardInput.WriteLine(TestData.InputData);
            testProcess.StandardInput.Close();
            //calc memory size
            int  tickTime  = 0;
            bool isTimeout = false;

            while (!testProcess.HasExited)
            {
                try { result.MaximumRAM = result.MaximumRAM < testProcess.PeakVirtualMemorySize64 ? testProcess.PeakVirtualMemorySize64 : result.MaximumRAM; }
                catch (Exception) { throw; }
                tickTime += 10; //calc per 100 ms
                if ((WaitForExit && (tickTime > JudgerManager.MaximumTime)) || (!WaitForExit && (tickTime > Data.LimitTime)))
                {
                    testProcess.Kill();
                    isTimeout = true;
                    break;
                }
                Thread.Sleep(10);
            }
            //compare output
            result.Index     = Data.Datas.IndexOf(TestData);
            result.TotalTime = (DateTime.Now - startTime).TotalMilliseconds;
            result.ExitCode  = testProcess.ExitCode;
            //check exit code
            if (result.ExitCode != 0 && result.Result != JudgeResultEnum.TimeLimitExceeded)
            {
                result.Result = JudgeResultEnum.RuntimeError;
                Results.Add(result);
                return;
            }
            //process exits with code 0
            //start comparing result
            var processOutput = testProcess.StandardOutput.ReadToEnd();

            if (processOutput.Trim() == TestData.OutputData.Trim())
            {
                result.Result = JudgeResultEnum.Accepted;
            }
            else if (CompareIfPresentationError(processOutput.Trim(), TestData.OutputData.Trim()))
            {
                result.Result = JudgeResultEnum.PresentationError;
            }
            else
            {
                result.Result = JudgeResultEnum.WrongAnswer;
            }
            //limitation
            if (result.MaximumRAM > Data.LimitRAM)
            {
                result.Result &= JudgeResultEnum.MemoryLimitExceeded;
            }
            if (result.TotalTime > Data.LimitTime || isTimeout)
            {
                result.Result &= JudgeResultEnum.TimeLimitExceeded;
            }
            Results.Add(result);
        }
Пример #2
0
        /// <summary>
        /// Run a test
        /// </summary>
        public void TestProgram(string ProgramFilename, DataPair TestData, bool WaitForExit)
        {
            Results.Remove(Results.FirstOrDefault(r => r.Index == Data.Datas.IndexOf(TestData)));
            //define basic options
            var testProcess = new Process();
            testProcess.StartInfo.FileName = ProgramFilename;
            //testProcess.StartInfo.UserName = JudgerManager.RunningUser == null ? Environment.UserName : JudgerManager.RunningUser.Username;
            //testProcess.StartInfo.Password = JudgerManager.RunningUser == null ? new SecureString() : JudgerManager.RunningUser.Password;
            testProcess.StartInfo.UseShellExecute = false;
            testProcess.StartInfo.CreateNoWindow = true;
            testProcess.StartInfo.RedirectStandardInput = true;
            testProcess.StartInfo.RedirectStandardOutput = true;
            testProcess.StartInfo.RedirectStandardError = true;
            var result = new JudgeResult();
            testProcess.Start();
            var startTime = DateTime.Now;
            //input sample data
            testProcess.StandardInput.WriteLine(TestData.InputData);
            testProcess.StandardInput.Close();
            //calc memory size
            int tickTime = 0;
            bool isTimeout = false;
            while (!testProcess.HasExited)
            {
                try { result.MaximumRAM = result.MaximumRAM < testProcess.PeakVirtualMemorySize64 ? testProcess.PeakVirtualMemorySize64 : result.MaximumRAM; }
                catch (Exception){ throw; }
                tickTime += 10; //calc per 100 ms
                if ((WaitForExit && (tickTime > JudgerManager.MaximumTime)) || (!WaitForExit && (tickTime > Data.LimitTime)))
                {
                    testProcess.Kill();
                    isTimeout = true;
                    break;
                }
                Thread.Sleep(10);
            }
            //compare output
            result.Index = Data.Datas.IndexOf(TestData);
            result.TotalTime = (DateTime.Now - startTime).TotalMilliseconds;
            result.ExitCode = testProcess.ExitCode;
            //check exit code
            if (result.ExitCode != 0 && result.Result != JudgeResultEnum.TimeLimitExceeded)
            {
                result.Result = JudgeResultEnum.RuntimeError;
                Results.Add(result);
                return;
            }
            //process exits with code 0
            //start comparing result
            var processOutput = testProcess.StandardOutput.ReadToEnd();

            if (processOutput.Trim() == TestData.OutputData.Trim())
                result.Result = JudgeResultEnum.Accepted;
            else if (CompareIfPresentationError(processOutput.Trim(), TestData.OutputData.Trim()))
                result.Result = JudgeResultEnum.PresentationError;
            else
                result.Result = JudgeResultEnum.WrongAnswer;
            //limitation
            if (result.MaximumRAM > Data.LimitRAM) result.Result &= JudgeResultEnum.MemoryLimitExceeded;
            if (result.TotalTime > Data.LimitTime || isTimeout) result.Result &= JudgeResultEnum.TimeLimitExceeded;
            Results.Add(result);
        }