コード例 #1
0
ファイル: PpmViewer.cs プロジェクト: jebidiah252/jarvis
        public string ToHtml(TestCase test)
        {
            testCase = test;
            StringBuilder result = new StringBuilder();

            foreach (OutputFile file in test.FileOutputFiles)
            {
                if (file.FileType == OutputFile.Type.PPM)
                {
                    string htmlDiff     = string.Empty;
                    string htmlActual   = string.Empty;
                    string htmlExpected = string.Empty;

                    string ppmFile = test.HomeworkPath + file.StudentFile;

                    if (!File.Exists(ppmFile))
                    {
                        htmlActual  = "No image found!";
                        test.Passed = false;
                    }
                    else if (new FileInfo(ppmFile).Length > 6000000)
                    {
                        htmlActual  = string.Format("Generated PPM file is too large! {0} bytes!", new FileInfo(ppmFile).Length);
                        test.Passed = false;
                    }
                    else if (!CheckPpmHeader(ppmFile)) // Invalid header
                    {
                        htmlActual  = "Invalid PPM header!<br />Please check for correct PPM before uploading to Jarvis.";
                        test.Passed = false;
                    }
                    else
                    {
                        string pngExpected       = ConvertPpmToPng(test.TestsPath + file.CourseFile);
                        Bitmap expected          = new Bitmap(pngExpected);
                        string expectedBase64Png = JarvisEncoding.ConvertToBase64(pngExpected);
                        htmlExpected = string.Format("<img src='data:image/png;base64,{0}' />", expectedBase64Png);

                        try
                        {
                            bool   match        = true;
                            bool   sizeMismatch = false;
                            string pngActual    = ConvertPpmToPng(test.HomeworkPath + file.StudentFile);

                            if (File.Exists(pngActual))
                            {
                                Bitmap actual = new Bitmap(pngActual);

                                if ((actual.Width == expected.Width) && (actual.Height == expected.Height))
                                {
                                    for (int i = 0; i < actual.Width; ++i)
                                    {
                                        for (int j = 0; j < actual.Height; ++j)
                                        {
                                            Color actualColor   = expected.GetPixel(i, j);
                                            Color expectedColor = actual.GetPixel(i, j);

                                            if (Math.Abs(actualColor.R - expectedColor.R) > 10)
                                            {
                                                match = false;
                                            }

                                            if (Math.Abs(actualColor.G - expectedColor.G) > 10)
                                            {
                                                match = false;
                                            }

                                            if (Math.Abs(actualColor.B - expectedColor.B) > 10)
                                            {
                                                match = false;
                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    match        = false;
                                    sizeMismatch = true;
                                }


                                string actualBase64Png = JarvisEncoding.ConvertToBase64(pngActual);
                                htmlActual = string.Format("<img src='data:image/png;base64,{0}' />", actualBase64Png);


                                if (match)
                                {
                                    htmlDiff    = "No difference, or close enough... ;-]";
                                    test.Passed = true;
                                }
                                else
                                {
                                    htmlDiff = "Differences detected!";

                                    if (sizeMismatch)
                                    {
                                        htmlDiff += "<br />Actual Size: " + actual.Width + "x" + actual.Height + ", Expected Size: " + expected.Width + "x" + expected.Height;
                                    }
                                    //htmlDiff += "<br /><a href='data:text/html;base64," + Convert.ToBase64String( Encoding.ASCII.GetBytes()) + "'>Link here</a>";
                                    test.Passed = false;
                                }

                                actual.Dispose();
                            }
                            else
                            {
                                htmlDiff    = "Differences detected!";
                                htmlActual  = "No image found!";
                                test.Passed = false;
                            }
                        }
                        catch
                        {
                            htmlDiff    = "Invalid image!";
                            htmlActual  = "Invalid image!";
                            test.Passed = false;
                        }

                        expected.Dispose();
                    }

                    string diffBlock = Utilities.BuildDiffBlock("From PPM image:", htmlActual, htmlExpected, htmlDiff);
                    result.Append(diffBlock);

                    if (File.Exists(ppmFile))
                    {
                        File.Delete(ppmFile);
                    }
                }
            }

            return(result.ToString());
        }
コード例 #2
0
        private string Get5xx(NancyContext context)
        {
            StringBuilder responseText = new StringBuilder();

            responseText.AppendFormat("<img style='display: block; margin: auto;' src='data:image/png;base64,{0}' /><br />", JarvisEncoding.ConvertToBase64(rootPath + "/Content/error.png", false));
            responseText.Append("<p>I'm sorry sir, it appears I have malfunctioned... an email has been sent to my creator.<br /><br />");
            responseText.Append("Error message:<br />" + JarvisEncoding.ToHtmlEncoding(context.Items["ERROR_TRACE"].ToString()));
            responseText.Append("</p>");

            Utilities.SendEmail("*****@*****.**", "Jarvis Internal Server Error!!", context.Items["ERROR_TRACE"].ToString(), "");

            return(responseText.ToString());
        }