public void TestNullArticleContent() { Article a = new Article(); var result = StringHelper.CalculateMostCommonCharacter(a); Assert.IsTrue(true, "This test should throw an exception before this line executes."); }
public static ArticleInfo CalculateMostCommonCharacter(Article input, StringHelperOptions options = null) { if (input == null) throw new ArgumentNullException("Article cannot be null."); if (input.Content == null) throw new ArgumentNullException("Article.Content cannot be null."); if (options == null) options = defaultOptions; var chars = new Dictionary<char, int>(); var output = new ArticleInfo(); output.Article = input; foreach (var c in input.Content) { if (options.IgnoreSpaces && c == ' ') continue; if (!chars.ContainsKey(c)) chars[c] = 0; chars[c]++; } foreach (var kvp in chars) { if (output.MostCommonCharacterCount < kvp.Value) { output.MostCommonCharacter = kvp.Key; output.MostCommonCharacterCount = kvp.Value; } } return output; }
public void TestEmptyArticleContent() { var NULL_TERMINATOR = '\0'; Article a = new Article() { Content = "" }; var result = StringHelper.CalculateMostCommonCharacter(a); Assert.AreEqual(NULL_TERMINATOR, result.MostCommonCharacter, "Empty article contents should return the null terminator as the most common character."); Assert.AreEqual(0, result.MostCommonCharacterCount, "Empty article contents should return the 0 as the most common character count."); }
public void TestPunctuation() { var expected = '&'; var expectedCount = 7; Article a = new Article() { Content = "!@@###$$$$%%%%%^^^^^^&&&&&&&" }; var result = StringHelper.CalculateMostCommonCharacter(a); Assert.AreEqual(expected, result.MostCommonCharacter, "Punctuation should count as a character."); Assert.AreEqual(expectedCount, result.MostCommonCharacterCount, "Punctuation should be counted accurately."); }
public void TestTies() { var expected = 'a'; var expectedCount = 2; Article a = new Article() { Content = "aabb" }; var result = StringHelper.CalculateMostCommonCharacter(a); Assert.AreEqual(expected, result.MostCommonCharacter, "Ties should go to the character found first."); Assert.AreEqual(expectedCount, result.MostCommonCharacterCount, "Ties should be counted accurately."); }
public void TestCalculateMostCommonCharacter() { var expected = 'a'; var expectedCount = 2; Article a = new Article() { Content = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()_+a" }; var result = StringHelper.CalculateMostCommonCharacter(a); Assert.AreEqual(expected, result.MostCommonCharacter, "The most common character should be found in the input string."); Assert.AreEqual(expectedCount, result.MostCommonCharacterCount, "The most common character found in the input string should be counted accurately."); }
private static void Process(string path) { // Load file contents // Calculate most common character // Save result var a = new Article() { Path = path }; a.Content = File.ReadAllText(path); var ai = StringHelper.CalculateMostCommonCharacter(a, options); ai.Article = a; // Could do many things here... send off to a storage service, put it on a queue for further processing, etc. // We'll keep it simple and serialize this out to a simple json file. File.WriteAllText(string.Format("article-info-{0}", path), JsonConvert.SerializeObject(ai)); }
public void TestIgnoreSpaces() { var expected = 'b'; var expectedCount = 1; Article a = new Article() { Content = "h e l l o " }; var result = StringHelper.CalculateMostCommonCharacter(a, new StringHelperOptions() { IgnoreSpaces = true }); Assert.AreEqual(expected, result.MostCommonCharacter, "Punctuation should count as a character."); Assert.AreEqual(expectedCount, result.MostCommonCharacterCount, "Punctuation should be counted accurately."); }