Example #1
0
        public void RunTests()
        {
            stopWatch = new Stopwatch();

            var testSourceFiles = CollectTestSourceFiles();

            foreach (var testSourceFile in testSourceFiles)
            {
                var testName = System.IO.Path.GetFileNameWithoutExtension(testSourceFile);

                var outputPath         = System.IO.Path.GetDirectoryName(testSourceFile);
                var actualOutputFile   = System.IO.Path.Combine(outputPath, testName + "_actual.txt");
                var expectedOutputFile = System.IO.Path.Combine(outputPath, testName + "_expected.txt");

                // Delete the actual output file from the previous run
                System.IO.File.Delete(actualOutputFile);

                var times = new TestTimes();
                stopWatch.Restart();
                try
                {
                    RunOneTest(testSourceFile, actualOutputFile, ref times);
                }
                catch (Exception ex)
                {
                    System.IO.File.AppendAllText(actualOutputFile, ex.ToString());
                }
                stopWatch.Stop();

                var durationMillisec = stopWatch.Elapsed.TotalMilliseconds;

                var isSuccessful = AreFilesEqual(expectedOutputFile, actualOutputFile);
                ConsolePrintTestCompleted(testName, isSuccessful, durationMillisec, ref times);
            }
        }
Example #2
0
        private void RunOneTest(string testSourceFile, string outputFile, ref TestTimes times)
        {
            var watch = new QueryPerfCounter();

            using (var reader = new System.IO.StreamReader(System.IO.File.OpenRead(testSourceFile)))
            {
                var fileName  = System.IO.Path.GetFileNameWithoutExtension(testSourceFile);
                var tokenizer = new Tokenizer(reader);

                watch.Start();
                CompilerException parserException;
                SyntaxTree        syntaxTree;
                try
                {
                    var parser = new Parser(fileName, tokenizer);
                    syntaxTree      = parser.Parse();
                    parserException = null;
                }
                catch (CompilerException ex)
                {
                    parserException = ex;
                    syntaxTree      = null;
                }
                watch.Stop();
                times.parseTimeMicrosec = watch.DurationMicrosec;

                // Write parser output
                using (var sw = new StreamWriter(outputFile, append: true))
                {
                    using (var writer = new IndentedTextWriter(sw, "  "))
                    {
                        if (parserException != null)
                        {
                            writer.WriteLine("Parsing failed:");
                            foreach (var l in parserException.Message.Split(Environment.NewLine))
                            {
                                writer.WriteLine(l);
                            }
                            writer.WriteLine("");
                        }
                        else
                        {
                            writer.WriteLine("Parsing completed");
                            writer.WriteLine("Syntax Tree:");
                            syntaxTree.WriteTree(writer);
                            writer.WriteLine("");
                        }
                    }
                }

                if (parserException != null)
                {
                    return;
                }

                watch.Start();
                var           compiler = new Compiler(syntaxTree);
                CompileResult compileResult;
                try
                {
                    compileResult = compiler.Compile();
                }
                catch (CompilerException ex)
                {
                    compileResult = new CompileResult(false, null, new List <CompilerException>()
                    {
                        ex
                    });
                }
                watch.Stop();
                times.compileTimeMicrosec = watch.DurationMicrosec;

                // Write compiler output
                using (var sw = new StreamWriter(outputFile, append: true))
                {
                    using (var writer = new IndentedTextWriter(sw, "  "))
                    {
                        if (!compileResult.Succeeded)
                        {
                            writer.WriteLine("Compile failed:");
                            foreach (var compilerException in compileResult.Errors)
                            {
                                foreach (var l in compilerException.Message.Split(Environment.NewLine))
                                {
                                    writer.WriteLine(l);
                                }
                            }
                            writer.WriteLine("");
                        }
                        else
                        {
                            writer.WriteLine("Compiled module:");
                            compileResult.Module.Write(writer);
                            writer.WriteLine("");
                        }
                    }
                }

                if (!compileResult.Succeeded)
                {
                    return;
                }

                watch.Start();
                string cppCode;
                using (var sw = new StringWriter())
                {
                    using (var writer = new IndentedTextWriter(sw, "  "))
                    {
                        var gen = new CppCodeGenerator(compileResult.Module, writer);
                        gen.Generate();
                        cppCode = sw.ToString();
                    }
                }
                watch.Stop();
                times.cppGenTimeMicrosec = watch.DurationMicrosec;

                // Save the code to a file, compile it and run
                string programOutput = CompileAndRunCppCodeReturnOutput(cppCode);
                // Write code and run output
                using (var streamWriter = new StreamWriter(outputFile, append: true))
                {
                    using (var writer = new IndentedTextWriter(streamWriter, "  "))
                    {
                        writer.WriteLine("Generated Cpp code:");
                        writer.Write(cppCode);
                        writer.WriteLine("");
                        writer.WriteLine("Program output:");
                        writer.Write(programOutput);
                    }
                }
            }
        }
Example #3
0
        private void ConsolePrintTestCompleted(string testName, bool success, double durationMillisec, ref TestTimes times)
        {
            var prevColor = Console.ForegroundColor;

            if (success)
            {
                Console.ForegroundColor = ConsoleColor.Green;
                Console.Write($"✔");
            }
            else
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.Write($"✖");
            }
            Console.ForegroundColor = prevColor;
            Console.WriteLine($" {testName} ({durationMillisec:0.00} ms)");
            Console.WriteLine($"  Parse time: {times.parseTimeMicrosec/1000.0:0.00} ms");
            Console.WriteLine($"  Compile time: {times.compileTimeMicrosec/1000.0:0.00} ms");
            Console.WriteLine($"  Cpp generation time: {times.cppGenTimeMicrosec/1000.0:0.00} ms");
            Console.WriteLine();
        }
Example #4
0
 public User(string name, float[] firstTestTimes)
 {
     Name           = name;
     FirstTestTimes = new TestTimes(firstTestTimes);
 }