Example #1
0
    private static string RunTest(string inputFileName, string outputFileName)
    {
        TextWriter output = new StringWriter();

        using (TextReader input = new StreamReader(inputFileName))
        {
            if (CaideTester.IS_TOPCODER_PROBLEM)
            {
                CaideTester.TopcoderSolve(input, output);
            }
            else
            {
                Type            type        = Type.GetType("Solution");
                ConstructorInfo constructor = type.GetConstructor(new Type[] { });
                MethodInfo      method      = type.GetMethod("solve", new[] { typeof(StreamReader), typeof(StreamWriter) });
                object          instance    = constructor.Invoke(new object[] {});
                method.Invoke(instance, new object[] { input, output });
            }
        }

        // save program output
        string result = output.ToString();

        File.WriteAllText(outputFileName, result);
        return(result);
    }
Example #2
0
    public static void Main(string[] args)
    {
        // Read path to caide executable from a file in current directory
        string testDir      = ".";
        string caideExeFile = Path.Combine(testDir, "caideExe.txt");

        if (!File.Exists(caideExeFile))
        {
            testDir      = Path.Combine(".caideproblem", "test");
            caideExeFile = Path.Combine(testDir, "caideExe.txt");
        }
        if (!File.Exists(caideExeFile))
        {
            throw new InvalidOperationException("Test musts be run from problem directory");
        }
        string caideExe = File.ReadAllText(caideExeFile).Trim();

        // Prepare the list of test cases in correct order; add recently created test cases too.
        Process updateTestsProcess = Run(caideExe, "update_tests");

        updateTestsProcess.WaitForExit();
        if (updateTestsProcess.ExitCode != 0)
        {
            Console.Error.WriteLine("caide update_tests returned non-zero error code " + updateTestsProcess.ExitCode);
        }

        StringWriter report = new StringWriter();

        // Process each test case described in a file in current directory
        foreach (string line in File.ReadAllLines(Path.Combine(testDir, "testList.txt")))
        {
            string[] words = line.Split(' ');
            string   testName = words[0], testState = words[1];
            if (testState == "Skip")
            {
                Console.Error.WriteLine("Skipping test " + testName);
                report.WriteLine(testName + " skipped");
            }
            else if (testState == "Run")
            {
                Console.Error.WriteLine("Running test " + testName);
                string inputFile = Path.Combine(testDir, testName + ".in");

                string result = null;
                try
                {
                    result = RunTest(inputFile, Path.Combine(testDir, testName + ".out"));
                }
                catch
                {
                    Console.Error.WriteLine("Test " + testName + " threw an exception");
                    report.WriteLine(testName + " failed");
                    continue;
                }

                if (CaideTester.ENABLE_CUSTOM_CHECKER)
                {
                    try
                    {
                        using (StringReader output = new StringReader(result))
                            using (StreamReader input = new StreamReader(inputFile))
                            {
                                bool ok = CaideTester.CustomCheck(input, output);
                                if (ok)
                                {
                                    report.WriteLine(testName + " OK");
                                }
                                else
                                {
                                    Console.Error.WriteLine("Test " + testName + " failed!");
                                    report.WriteLine(testName + " failed");
                                }
                            }
                    }
                    catch (Exception e)
                    {
                        Console.Error.WriteLine("Checker for test " + testName + " threw an exception: " + e.Message);
                        report.WriteLine(testName + " error");
                    }
                }
                else
                {
                    report.WriteLine(testName + " ran");
                }

                if (result.Length > 200)
                {
                    result = result.Substring(0, 200) + " [...] (output truncated)\n";
                }
                Console.Error.WriteLine(result);
            }
            else
            {
                report.WriteLine(testName + " error unknown test status");
            }
        }

        File.WriteAllText(Path.Combine(testDir, "report.txt"), report.ToString());

        // optional: evaluate tests automatically
        Process evalTestsProcess = Run(caideExe, "eval_tests");

        evalTestsProcess.WaitForExit();
        if (evalTestsProcess.ExitCode != 0)
        {
            Console.Error.WriteLine("Tests failed!");
            Environment.Exit(evalTestsProcess.ExitCode);
        }
    }