/// <summary> /// Performance testing. After all the functionality test passes, the next thing to /// take care of is the performance. Can you make your code faster? /// And no, you shouldn't change the tests. /// </summary> /// <param name="theHistory">theHistory abstract class to TheHistory implementation</param> public void RunAllPerformanceTests(TheHistory theHistory) { Stopwatch stopWatch = new Stopwatch(); stopWatch.Restart(); theHistory.Add(ReadFromFile("Resources/Iliad.txt")); stopWatch.Stop(); Console.WriteLine("Reading words from file took " + stopWatch.ElapsedMilliseconds + " ms (the size is now " + theHistory.Size() + ")"); stopWatch.Restart(); theHistory.RemoveWord("king"); theHistory.RemoveWord("Zeus"); theHistory.RemoveWord("Apollo"); theHistory.RemoveWord("it"); stopWatch.Stop(); Console.WriteLine("Removing words took " + stopWatch.ElapsedMilliseconds + " ms"); stopWatch.Restart(); theHistory.Replace("Achilles", "Il"); theHistory.Replace("Agamemnon", "Il"); theHistory.Replace("Priam", "Trumm"); theHistory.Replace("chariot", "tank"); theHistory.Replace("bow", "missile"); theHistory.Replace("arrow", "nuke"); theHistory.Replace("the", "the"); stopWatch.Stop(); Console.WriteLine("Replacing words took " + stopWatch.ElapsedMilliseconds + " ms"); stopWatch.Restart(); theHistory.Replace("Il", "Pet Il"); theHistory.Replace("Pet Il", "Pet Il (blessed be his name)"); theHistory.Replace("Trumm", "coward and insane Trumm"); theHistory.Replace("the", "the big"); theHistory.Replace("the big", "the very big"); theHistory.Replace("a", "a big"); theHistory.Replace("a big", "a very big"); stopWatch.Stop(); Console.WriteLine("Replacing multiple words (with insertion) took " + stopWatch.ElapsedMilliseconds + " ms"); stopWatch.Restart(); theHistory.Replace("Pet Il (blessed be his name)", "Pet Il (blessed be the name)"); theHistory.Replace("coward and insane Trumm", "coward and liar Trumm"); theHistory.Replace("the very big", "the super big"); theHistory.Replace("the super big", "the really big"); theHistory.Replace("a very big", "a super big"); theHistory.Replace("a super big", "a really big"); stopWatch.Stop(); Console.WriteLine("Replacing multiple words (equal number) took " + stopWatch.ElapsedMilliseconds + " ms"); stopWatch.Restart(); theHistory.Replace("Pet Un (blessed be the name)", "Pet Un The Wise"); theHistory.Replace("coward and liar Trumm", "President Trumm"); theHistory.Replace("the really big", "the big"); theHistory.Replace("the big", "the"); theHistory.Replace("a really big", "a big"); theHistory.Replace("a big", "a"); stopWatch.Stop(); Console.WriteLine("Replacing multiple words (with removal) took " + stopWatch.ElapsedMilliseconds + " ms"); }
/// <summary> /// Helper method for running a functionality test on TheHistory. /// Note we are using TheHistory abstract class so we can /// use this method with _any_ TheHistory implementation! /// </summary> /// <param name="theHistory">the abstract class we are using to run the test</param> /// <param name="sourceText">the text what TheHistory will use to run the test on</param> /// <param name="fromWords">these are the words we are looking for in the "sourceText"</param> /// <param name="toWords">we would like to change the text found in the "fromWords" to this text</param> /// <returns>returns the modified text</returns> private string RunFunctionalityTest(TheHistory theHistory, string sourceText, string fromWords, string toWords) { theHistory.Add(sourceText); theHistory.Replace(fromWords, toWords); string result = theHistory.ToString(); theHistory.Clear(); return(result); }