Exemple #1
0
        private void button2_Click(object sender, EventArgs e)
        {
            if (waitingForResults)
            {
                return;
            }

            waitingForResults   = true;
            textBox1.IsReadOnly = true;

            MethodInfo method = CompileCode(textBox1.Text);

            if (method == null)
            {
                textBox1.IsReadOnly = false;
                waitingForResults   = false;
                return;
            }

            try
            {
                Stopwatch sw = new Stopwatch();
                sw.Start();

                MethodInfo solutionMethod;
                using (StreamReader sr = new StreamReader(Path.Combine(problemsRoot, currentProblem.DirectoryPath, "Solution.cs")))
                {
                    solutionMethod = CompileCode(sr.ReadToEnd());
                }
                if (solutionMethod == null)
                {
                    Debug.Assert(false);
                    richTextBox1.AppendText("Error: couldn't find solution code", Color.Red, true);
                    waitingForResults   = false;
                    textBox1.IsReadOnly = false;
                    return;
                }

                richTextBox1.AppendText("\r\nGenerating test cases, please wait... ");

                List <TestCase> testCases;
                string          testCasesFile = Path.Combine(problemsRoot, currentProblem.DirectoryPath, "TestCases.txt");
                if (File.Exists(testCasesFile))
                {
                    testCases = TestCasesGenerator.GetTestCasesFromFile(solutionMethod, testCasesFile, currentProblem.inputParameter);
                }
                else if (currentProblem.inputParameter == typeof(Int32[]))
                {
                    testCases = TestCasesGenerator.GenerateIntArray(solutionMethod, currentProblem.maxInputSize, true, true);
                }
                else if (currentProblem.inputParameter == typeof(Int32))
                {
                    testCases = TestCasesGenerator.GenerateInts(solutionMethod, currentProblem.maxInputSize);
                }
                else
                {
                    Debug.Assert(false);
                    richTextBox1.AppendText(string.Format("Error with test case: don't know how to generate test cases for type {0}", currentProblem.inputParameter.Name), Color.Red, true);
                    waitingForResults   = false;
                    textBox1.IsReadOnly = false;
                    return;
                }
                sw.Stop();
                richTextBox1.AppendText(string.Format(" generated in {0} seconds.\r\n", (double)sw.ElapsedMilliseconds / 1000));

                currentTestingMethod = method;
                globalTestCases      = testCases;
                lastRunTest          = 0;

                ThreadingHelper th = new ThreadingHelper();
                th.RunMethodAsync(delegate
                {
                    return(currentTestingMethod.Invoke(null, new object[] { globalTestCases[lastRunTest].input }));
                }, TestInProgressCallback, ProgramTimeout);
            }
            catch (Exception ex)
            {
                richTextBox1.AppendText(string.Format("\r\nAn error occurred executing your program: {0}", ex.Message), Color.Red, true);
                globalTestCases     = null;
                waitingForResults   = false;
                textBox1.IsReadOnly = false;
            }
        }
Exemple #2
0
        public void LoadProblem(string name)
        {
            string problemBody = Path.Combine(problemsRoot, name, "Problem.txt");

            if (!File.Exists(problemBody))
            {
                MessageBox.Show(string.Format("Couldn't open problem description at: {0}", problemBody));
            }

            TrySave();

            try
            {
                string[] namesplt = name.Split('-');
                using (StreamReader sr = new StreamReader(problemBody))
                {
                    CodingProblem nextProblem = new CodingProblem();
                    nextProblem.DirectoryPath = name;
                    nextProblem.ProblemId     = int.Parse(namesplt[0].Trim());
                    nextProblem.ProblemName   = namesplt[1].Trim();
                    {
                        StringBuilder sb = new StringBuilder();
                        do
                        {
                            string s = sr.ReadNoncommentLine();
                            if (!s.Equals("%%%"))
                            {
                                sb.AppendLine(s);
                            }
                            else
                            {
                                break;
                            }
                        } while (true);
                        nextProblem.ProblemDescription = sb.ToString();
                    }
                    nextProblem.inputParameter     = Type.GetType(sr.ReadNoncommentLine());
                    nextProblem.maxInputSize       = int.Parse(sr.ReadNoncommentLine());
                    nextProblem.outputParameter    = Type.GetType(sr.ReadNoncommentLine());
                    nextProblem.SampleInputString  = sr.ReadNoncommentLine();
                    nextProblem.SampleOutputString = sr.ReadNoncommentLine();
                    nextProblem.SampleInput        = TestCasesGenerator.UnmarshalObject(nextProblem.inputParameter, nextProblem.SampleInputString);
                    nextProblem.SampleOutput       = TestCasesGenerator.UnmarshalObject(nextProblem.outputParameter, nextProblem.SampleOutputString);
                    nextProblem.IsSolved           = SolvedProblems.Contains(nextProblem.ProblemId);

                    viewSolutionToolStripMenuItem.Enabled = nextProblem.IsSolved;
                    currentProblem           = nextProblem;
                    webBrowser1.DocumentText = MathJax.GetFormattedHtml(currentProblem.ToMathJaxString());
                }

                if (File.Exists(Path.Combine(problemsRoot, name, "SavedAttempt.cs")))
                {
                    using (StreamReader sr = new StreamReader(Path.Combine(problemsRoot, name, "SavedAttempt.cs")))
                    {
                        textBox1.Text = sr.ReadToEnd();
                    }
                }
                else
                {
                    textBox1.Text = currentProblem.GenerateMethodStub();
                }
                richTextBox1.Clear();
            }
            catch (Exception e)
            {
                MessageBox.Show(string.Format("error opening problem description: {0}", e.Message));
            }
        }