Exemplo n.º 1
0
        void TestInProgressCallback(long elapsedMilliseconds, object result)
        {
            TestCase tc = globalTestCases[lastRunTest];

            richTextBox1.InvokeAppendText(string.Format("\r\nTest case {0}: ", lastRunTest));
            if ((currentProblem.outputParameter == typeof(double) && ((double)result).ApproximatelyEqual((double)tc.ExpectedOutput)) ||
                tc.ExpectedOutput.Equals(result))
            {
                richTextBox1.AppendText(string.Format("PASS [{0} seconds]", (double)elapsedMilliseconds / 1000), Color.DarkGreen, true);
            }
            else
            {
                richTextBox1.AppendText(string.Format("\r\nThe input was: {0}", tc.input.GetType() == typeof(Int32[]) ? string.Join(",", (Int32[])tc.input) : tc.input.ToString()), Color.Black, true);
                richTextBox1.AppendText(string.Format("\r\nThe expected output was: {0}", tc.ExpectedOutput.ToString()), Color.Black, true);
                richTextBox1.AppendText(string.Format("\r\nThe output from your method was: {0}", result.ToString()), Color.Black, true);
                richTextBox1.AppendText(string.Format("\r\nSorry, your method did not pass this test case. [{0} seconds]", (double)elapsedMilliseconds / 1000), Color.Red, true);
                waitingForResults   = false;
                textBox1.IsReadOnly = false;
                globalTestCases     = null;
                return;
            }

            lastRunTest++;
            if (lastRunTest < globalTestCases.Count)
            {
                // Recursively kick off the next one
                ThreadingHelper th = new ThreadingHelper();
                th.RunMethodAsync(delegate
                {
                    return(currentTestingMethod.Invoke(null, new object[] { globalTestCases[lastRunTest].input }));
                }, TestInProgressCallback, ProgramTimeout);
                return;
            }

            // Passing!
            richTextBox1.AppendText("\r\n\r\nCongratulations, your method passed all test cases!", Color.DarkGreen, true);
            tada.Play();
            if (!currentProblem.IsSolved)
            {
                viewSolutionToolStripMenuItem.InvokeEnable(true);
                currentProblem.IsSolved = true;
                using (StreamWriter strw = new StreamWriter(Path.Combine(problemsRoot, "Progress"), true))
                {
                    strw.WriteLine(currentProblem.ProblemId);
                }
                SolvedProblems.Add(currentProblem.ProblemId);
                foreach (var item in problemsToolStripMenuItem.DropDownItems)
                {
                    ToolStripMenuItem tsmi = item as ToolStripMenuItem;
                    if (Int32.Parse(tsmi.Text.Split('-')[0].Trim()) == currentProblem.ProblemId)
                    {
                        tsmi.InvokeAppendText(" [SOLVED]");
                    }
                }
            }
            globalTestCases     = null;
            waitingForResults   = false;
            textBox1.IsReadOnly = false;
        }