Ejemplo n.º 1
0
        public void CreateTest()
        {
            //Arrange
            var startDate   = new DateTime(123456789);
            var finishdate1 = startDate.AddDays(1);
            var finishdate2 = startDate.AddDays(345);
            var dayInterval = new Interval()
            {
                Start = startDate, Finish = finishdate1
            };
            var bigInterval = new Interval()
            {
                Start = startDate, Finish = finishdate2
            };

            var filterManager     = new Mock <IFiltersManager>();
            var reportAccumulator = new Mock <IReportAccumulator>();
            var statisticsService = new Mock <IStatisticsService>();
            var reportsService    = new Mock <IStandardReportService>();

            var compilerFactory = new CompilerFactory(filterManager.Object, reportAccumulator.Object, statisticsService.Object, reportsService.Object);

            //Act
            var statiscticsCompiler    = compilerFactory.Create(dayInterval);
            var reportsCompiler        = compilerFactory.Create(bigInterval);
            var allDaysReportsCompiler = compilerFactory.Create(null);

            //Assert
            Assert.IsInstanceOfType(statiscticsCompiler, typeof(StatisticsCompiler));
            Assert.IsInstanceOfType(reportsCompiler, typeof(ReportsCompiler));
            Assert.IsInstanceOfType(allDaysReportsCompiler, typeof(AllDaysReportsCompiler));
        }
Ejemplo n.º 2
0
        public virtual string Compile(XmlNode node, int indent = 0)
        {
            string result = "";

            foreach (XmlNode child in node.ChildNodes)
            {
                INodeCompiler compiler = CompilerFactory.Create(child);
                result += compiler.Compile(child, indent);
            }
            return(result);
        }
Ejemplo n.º 3
0
        public string Compile(XmlNode node, int indent = 0)
        {
            string indentString = new string('\t', indent);
            string result       = indentString + "else\n";

            XmlNode       body     = node.SelectSingleNode("body");
            INodeCompiler compiler = CompilerFactory.Create(body);

            result += compiler.Compile(body, indent + 1);

            return(result);
        }
Ejemplo n.º 4
0
        public string Compile(XmlNode node, int indent = 0)
        {
            string        indentString = new string('\t', indent);
            XmlNode       checks       = node.SelectSingleNode("checks");
            INodeCompiler compiler     = CompilerFactory.Create(checks);
            string        result       = indentString + "if (" + compiler.Compile(checks) + ") then\n";

            XmlNode body = node.SelectSingleNode("body");

            compiler = CompilerFactory.Create(body);
            result  += compiler.Compile(body, indent + 1);

            return(result);
        }
Ejemplo n.º 5
0
        public string Compile(XmlNode node, int indent = 0)
        {
            string result = "";

            foreach (XmlNode child in node.ChildNodes)
            {
                INodeCompiler compiler = CompilerFactory.Create(child);
                if (result != "")
                {
                    result += " " + child.Attributes["operator"].InnerText + " ";
                }
                result += compiler.Compile(child);
            }
            return(result);
        }
Ejemplo n.º 6
0
        public string Compile(XmlNode node, int indent = 0)
        {
            string result = "";

            foreach (XmlNode child in node.ChildNodes)
            {
                INodeCompiler compiler = CompilerFactory.Create(child);
                result += compiler.Compile(child) + ",";
            }

            if (node.ChildNodes.Count > 0)
            {
                return(Utils.ReplaceLastOccurrence(result, ",", ""));;
            }
            return(result);
        }
Ejemplo n.º 7
0
        public string Compile(XmlNode node, int indent = 0)
        {
            string indentString = new string('\t', indent);
            string result       = String.Format("{0}{1}(", indentString, node.Attributes["function"].InnerText);

            foreach (XmlNode child in node.ChildNodes)
            {
                INodeCompiler compiler = CompilerFactory.Create(child);
                result += compiler.Compile(child) + ", ";
            }

            if (node.ChildNodes.Count > 0)
            {
                return(Utils.ReplaceLastOccurrence(result, ", ", "") + ")\n");
            }
            return(result + ")\n");
        }
Ejemplo n.º 8
0
        public string Compile(XmlNode node, int indent = 0)
        {
            string result       = "";
            string indentString = new string('\t', indent);

            result += String.Format("{0}for {1},{2} in pairs({3}) do\n",
                                    indentString,
                                    node.Attributes["key"].InnerText,
                                    node.Attributes["value"].InnerText,
                                    node.Attributes["variable"].InnerText
                                    );

            foreach (XmlNode child in node.ChildNodes)
            {
                INodeCompiler compiler = CompilerFactory.Create(child);
                result += compiler.Compile(child, indent + 1);
            }

            result += indentString + "end\n";
            return(result);
        }
Ejemplo n.º 9
0
        public string Compile(XmlNode node, int indent = 0)
        {
            string result       = "";
            string indentString = new string('\t', indent);

            result += String.Format("{0}for {1} = {2}, {3}, {4}  do\n",
                                    indentString,
                                    node.Attributes["variable"].InnerText,
                                    node.Attributes["start"].InnerText,
                                    node.Attributes["end"].InnerText,
                                    (node.Attributes["increments"] != null) ? node.Attributes["increments"].InnerText : "1"
                                    );

            foreach (XmlNode child in node.ChildNodes)
            {
                INodeCompiler compiler = CompilerFactory.Create(child);
                result += compiler.Compile(child, indent + 1);
            }

            result += indentString + "end\n";
            return(result);
        }
Ejemplo n.º 10
0
        public string Compile(XmlNode node, int indent = 0)
        {
            string indentString = new string('\t', indent);
            var    children     = node.ChildNodes;

            if (children.Count > 0 && children[0].NodeType != XmlNodeType.Text)
            {
                string result = "\n";

                foreach (XmlNode child in children)
                {
                    INodeCompiler compiler = CompilerFactory.Create(child);
                    result += compiler.Compile(child, indent + 1);
                }

                return(result);
            }
            else
            {
                return(indentString + node.InnerText);
            }
        }
Ejemplo n.º 11
0
        void QueuePendingAssemblies()
        {
            if (pendingAssemblies.Count == 0)
            {
                return;
            }

            List <ScriptAssembly> assemblyCompileQueue    = null;
            List <ScriptAssembly> removePendingAssemblies = null;

            // Find assemblies that have all their references already compiled.
            foreach (var pendingAssembly in pendingAssemblies)
            {
                bool compileAssembly = true;

                foreach (var reference in pendingAssembly.ScriptAssemblyReferences)
                {
                    CompilerMessage[] messages;

                    if (!compiledAssemblies.TryGetValue(reference, out messages))
                    {
                        // If a reference is not compiling and not pending
                        // also remove this assembly from pending.
                        if (!compilerTasks.ContainsKey(reference) && !pendingAssemblies.Contains(reference))
                        {
                            if (removePendingAssemblies == null)
                            {
                                removePendingAssemblies = new List <ScriptAssembly>();
                            }

                            removePendingAssemblies.Add(pendingAssembly);
                        }

                        compileAssembly = false;
                        break;
                    }

                    // If reference has compile errors, do not compile the pending assembly.
                    bool compileErrors = messages.Any(m => m.type == CompilerMessageType.Error);

                    if (compileErrors)
                    {
                        if (removePendingAssemblies == null)
                        {
                            removePendingAssemblies = new List <ScriptAssembly>();
                        }

                        removePendingAssemblies.Add(pendingAssembly);

                        compileAssembly = false;
                        break;
                    }
                }

                if (compileAssembly)
                {
                    if (assemblyCompileQueue == null)
                    {
                        assemblyCompileQueue = new List <ScriptAssembly>();
                    }

                    assemblyCompileQueue.Add(pendingAssembly);
                }
            }

            if (removePendingAssemblies != null)
            {
                foreach (var assembly in removePendingAssemblies)
                {
                    pendingAssemblies.Remove(assembly);

                    // If a codegen assembly was removed fro pending assemblies,
                    // clear all pending compilation and post processing.
                    if (codeGenAssemblies.Contains(assembly))
                    {
                        notCompiledCodeGenAssemblies.Add(assembly);
                        pendingPostProcessorTasks.Clear();
                        pendingAssemblies.Clear();
                        assemblyCompileQueue = null;
                        break;
                    }
                }

                // All pending assemblies were removed and no assemblies
                // were queued for compilation.
                if (assemblyCompileQueue == null)
                {
                    return;
                }
            }

            // No assemblies to compile, need to wait for more references to finish compiling.
            if (assemblyCompileQueue == null)
            {
                if (compilerTasks.Count == 0)
                {
                    throw new Exception("No pending assemblies queued for compilation and no compilers running. Compilation will never finish.");
                }
                return;
            }

            // Begin compiling any queued assemblies
            foreach (var assembly in assemblyCompileQueue)
            {
                pendingAssemblies.Remove(assembly);

                if (assembly.CallOnBeforeCompilationStarted && OnBeforeCompilationStarted != null)
                {
                    OnBeforeCompilationStarted(assembly, compilePhase);
                }

                var compiler = compilerFactory.Create(assembly, buildOutputDirectory);

                compilerTasks.Add(assembly, compiler);

                // Start compiler process
                compiler.BeginCompiling();

                LogStartInfo($"# Starting compiling {assembly.Filename}", compiler.GetProcessStartInfo());

                if (OnCompilationStarted != null)
                {
                    OnCompilationStarted(assembly, compilePhase);
                }

                if (RunningMaxConcurrentProcesses)
                {
                    break;
                }
            }

            compilePhase++;
        }
Ejemplo n.º 12
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);
        }
Ejemplo n.º 13
0
        void QueuePendingAssemblies()
        {
            if (pendingAssemblies.Count == 0)
            {
                return;
            }

            List <ScriptAssembly> assemblyCompileQueue    = null;
            List <ScriptAssembly> removePendingAssemblies = null;

            // Find assemblies that have all their references already compiled.
            foreach (var pendingAssembly in pendingAssemblies)
            {
                bool compileAssembly = true;

                int unchangedAssemblyReferencesCount = 0;

                foreach (var reference in pendingAssembly.ScriptAssemblyReferences)
                {
                    CompilerMessage[] messages;

                    if (unchangedCompiledAssemblies.Contains(reference))
                    {
                        unchangedAssemblyReferencesCount++;
                        continue;
                    }

                    if (!compiledAssemblies.TryGetValue(reference, out messages))
                    {
                        // If a reference is not compiling and not pending
                        // also remove this assembly from pending.
                        if (!compilerTasks.ContainsKey(reference) && !pendingAssemblies.Contains(reference))
                        {
                            if (removePendingAssemblies == null)
                            {
                                removePendingAssemblies = new List <ScriptAssembly>();
                            }

                            removePendingAssemblies.Add(pendingAssembly);
                        }

                        compileAssembly = false;
                        break;
                    }

                    // If reference has compile errors, do not compile the pending assembly.
                    bool compileErrors = messages.Any(m => m.type == CompilerMessageType.Error);

                    if (compileErrors)
                    {
                        if (removePendingAssemblies == null)
                        {
                            removePendingAssemblies = new List <ScriptAssembly>();
                        }

                        removePendingAssemblies.Add(pendingAssembly);

                        compileAssembly = false;
                        break;
                    }
                }

                // If the assembly exists and is up to date (no compile errors)
                // and it was dirtied because it is a reference to one or more
                // dirty assemblies and none of them changed, then we do not
                // need to recompile the assembly.
                // Most expensive checks at the bottom.
                if (UseReferenceAssemblies &&
                    BuildingForEditor &&
                    !pendingAssembly.HasCompileErrors &&
                    pendingAssembly.DirtySource == DirtySource.DirtyReference &&
                    unchangedAssemblyReferencesCount > 0 &&
                    unchangedAssemblyReferencesCount == pendingAssembly.ScriptAssemblyReferences.Length &&
                    File.Exists(pendingAssembly.FullPath))
                {
                    var assemblyOutputPath = AssetPath.Combine(pendingAssembly.OutputDirectory, pendingAssembly.Filename);

                    Console.WriteLine($"- Skipping compile {assemblyOutputPath} because all references are unchanged");

                    unchangedCompiledAssemblies.Add(pendingAssembly);

                    if (removePendingAssemblies == null)
                    {
                        removePendingAssemblies = new List <ScriptAssembly>();
                    }

                    removePendingAssemblies.Add(pendingAssembly);
                    compileAssembly = false;
                }

                if (compileAssembly)
                {
                    if (assemblyCompileQueue == null)
                    {
                        assemblyCompileQueue = new List <ScriptAssembly>();
                    }

                    assemblyCompileQueue.Add(pendingAssembly);
                }
            }

            if (removePendingAssemblies != null)
            {
                foreach (var assembly in removePendingAssemblies)
                {
                    pendingAssemblies.Remove(assembly);

                    // If a codegen assembly was removed fro pending assemblies,
                    // clear all pending compilation and post processing.
                    if (codeGenAssemblies.Contains(assembly))
                    {
                        notCompiledCodeGenAssemblies.Add(assembly);
                        pendingPostProcessorTasks.Clear();
                        pendingAssemblies.Clear();
                        assemblyCompileQueue = null;
                        break;
                    }
                }

                // All pending assemblies were removed and no assemblies
                // were queued for compilation.
                if (assemblyCompileQueue == null)
                {
                    return;
                }
            }

            // No assemblies to compile, need to wait for more references to finish compiling.
            if (assemblyCompileQueue == null)
            {
                if (compilerTasks.Count == 0)
                {
                    throw new Exception("No pending assemblies queued for compilation and no compilers running. Compilation will never finish.");
                }
                return;
            }

            // Begin compiling any queued assemblies
            foreach (var assembly in assemblyCompileQueue)
            {
                pendingAssemblies.Remove(assembly);

                if (assembly.CallOnBeforeCompilationStarted && OnBeforeCompilationStarted != null)
                {
                    OnBeforeCompilationStarted(assembly, compilePhase);
                }

                var compiler = compilerFactory.Create(assembly, buildOutputDirectory);

                compilerTasks.Add(assembly, compiler);

                // Start compiler process
                compiler.BeginCompiling();

                LogStartInfo($"# Starting compiling {assembly.Filename}", compiler.GetProcessStartInfo());

                if (OnCompilationStarted != null)
                {
                    OnCompilationStarted(assembly, compilePhase);
                }

                if (RunningMaxConcurrentProcesses)
                {
                    break;
                }
            }

            compilePhase++;
        }