public void TestHtmlEncodeDecode() { //geberate a random string StringBuilder sb = new StringBuilder(); Random rnd = new Random(); int k = 0; int MAX = 1000; while (k < MAX) { char c = (char)rnd.Next(0x100); if (!char.IsSurrogate(c)) //ignore surrogates { k += 1; sb.Append(c); } } string str = sb.ToString(); string e = HtmlEncodeStrict.Apply(str); string d = System.Net.WebUtility.HtmlDecode(e); if (!str.Equals(d)) { Assert.Fail(); } }
//[TestMethod] public void RunPerformanceComparison() { //string testfilesDirectory = @"C:\Users\margus\Desktop\IJCAR12\proceedings"; string testfilesDirectory = "../../../GeneratedCodeSamples/SampleTexts"; var someTestFiles = System.IO.Directory.GetFiles(testfilesDirectory); int decodeTimeSystem_tot = 0; int decodeTimeSystemWeb_tot = 0; int decodeTimeA_tot = 0; int decodeTimeS_tot = 0; long totsize = 0; int someTestFileNr = 0; while (totsize < 100000000) { string file = someTestFiles[someTestFileNr]; someTestFileNr = (someTestFileNr + 1) % someTestFiles.Length; int LIMIT = 1000000; var fi = new System.IO.FileInfo(file); var size = fi.Length; string content = "<" + System.IO.File.ReadAllText(file, Encoding.UTF8) + ">"; string encoded = HtmlEncodeStrict.Apply(content); string decoded_System = ""; string decoded_SystemWeb = ""; string decoded_A = ""; string decoded_S = ""; //--- decode with system int decodeTimeSystemNet = System.Environment.TickCount; for (long K = 0; K < LIMIT; K += size) { decoded_System = System.Net.WebUtility.HtmlDecode(encoded); totsize += size; } decodeTimeSystemNet = System.Environment.TickCount - decodeTimeSystemNet; int decodeTimeSystemWeb = System.Environment.TickCount; for (long K = 0; K < LIMIT; K += size) { decoded_SystemWeb = System.Web.HttpUtility.HtmlDecode(encoded); } decodeTimeSystemWeb = System.Environment.TickCount - decodeTimeSystemWeb; //--- decode with methB (using StringBuilder) int decodeTimeS = System.Environment.TickCount; for (long K = 0; K < LIMIT; K += size) { decoded_S = HtmlDecodeS.Apply(encoded); } decodeTimeS = System.Environment.TickCount - decodeTimeS; //--- decode with methA (using Array) int decodeTimeA = System.Environment.TickCount; for (long K = 0; K < LIMIT; K += size) { decoded_A = HtmlDecodeA.Apply(encoded); } decodeTimeA = System.Environment.TickCount - decodeTimeA; //--- summary of times //string summary = string.Format("{0}: System:{1}ms, methA:{2}ms, methS:{3}ms", fi.Name, decodeTimeSystem, decodeTimeA, decodeTimeS); //Console.WriteLine(summary); //--- validate outputs Assert.AreEqual <string>(decoded_System, decoded_SystemWeb); if (!decoded_System.Equals(decoded_A)) { for (int i = 0; i < decoded_System.Length; i++) { if (decoded_System[i] != decoded_A[i]) { Assert.Fail(); } } } Assert.AreEqual <string>(decoded_System, decoded_S); decodeTimeSystem_tot += decodeTimeSystemNet; decodeTimeSystemWeb_tot += decodeTimeSystemWeb; decodeTimeA_tot += decodeTimeA; decodeTimeS_tot += decodeTimeS; } string summary_tot = string.Format("{0}MB: System.Net.WebUtility.HtmlDecode:{1}ms, System.Web.HttpUtility.HtmlDecode:{4}, methA:{2}ms, methS:{3}ms", totsize / 1000000, decodeTimeSystem_tot, decodeTimeA_tot, decodeTimeS_tot, decodeTimeSystemWeb_tot); Console.WriteLine(summary_tot); }