From 114d7b23933823998e03895d3e2a1561102fb401 Mon Sep 17 00:00:00 2001 From: omerfarukz Date: Sun, 13 Nov 2022 01:20:01 +0300 Subject: [PATCH] Q & A --- AutoComplete.Tests/BitArrayHelperTests.cs | 15 ++++++++- AutoComplete.Tests/IndexBuilderTests.cs | 1 + AutoComplete.Tests/IndexSearcherTests.cs | 18 +++++++++-- AutoComplete.Tests/TrieTests.cs | 37 +++++++++++++++++++++++ AutoComplete/DataStructure/TrieNode.cs | 4 +-- AutoComplete/Helpers/BitArrayHelper.cs | 2 +- 6 files changed, 70 insertions(+), 7 deletions(-) diff --git a/AutoComplete.Tests/BitArrayHelperTests.cs b/AutoComplete.Tests/BitArrayHelperTests.cs index 8604c43..1e9aee5 100644 --- a/AutoComplete.Tests/BitArrayHelperTests.cs +++ b/AutoComplete.Tests/BitArrayHelperTests.cs @@ -1,5 +1,6 @@ using System; using System.Collections; +using System.Linq; using AutoComplete.Helpers; using Xunit; @@ -15,10 +16,22 @@ public void passing_null_args_should_throw_argument_exception() { BitArrayHelper.CopyToInt32Array(bitArray, Array.Empty(), 0); }); - + Assert.Throws(() => { BitArrayHelper.CopyToInt32Array(new BitArray(Array.Empty()), null, 0); }); } + + [Fact] + public void copy_to_int_32_to_next_block_should_pass() + { + var bits = Enumerable.Range(0, 33).Select(f => f % 2 == 0).ToArray(); + var array = new int[33]; + BitArrayHelper.CopyToInt32Array( + new BitArray(Enumerable.Range(0, 33).Select(f => f % 2 == 0).ToArray()), + array, + 0); + Assert.Equal(1, array[1]); + } } \ No newline at end of file diff --git a/AutoComplete.Tests/IndexBuilderTests.cs b/AutoComplete.Tests/IndexBuilderTests.cs index dbfa468..61042ad 100644 --- a/AutoComplete.Tests/IndexBuilderTests.cs +++ b/AutoComplete.Tests/IndexBuilderTests.cs @@ -1,5 +1,6 @@ using System.IO; using AutoComplete.Builders; +using AutoComplete.Clients.IndexSearchers; using Xunit; namespace AutoComplete.Tests; diff --git a/AutoComplete.Tests/IndexSearcherTests.cs b/AutoComplete.Tests/IndexSearcherTests.cs index 1c97ac5..bfe41df 100644 --- a/AutoComplete.Tests/IndexSearcherTests.cs +++ b/AutoComplete.Tests/IndexSearcherTests.cs @@ -42,6 +42,18 @@ public IndexSearcherTests() tailStream.Close(); } + [Fact] + public void search_on_empty_index_should_return_not_found() + { + var searcher = new InMemoryIndexSearcher(headerFileName, indexFileName); + searcher.Init(); + + var result = searcher.Search(new SearchOptions() + {Term = "notexist", MaxItemCount = 1, SuggestWhenFoundStartsWith = true}); + + Assert.Equal(TrieNodeSearchResultType.NotFound, result.ResultType); + } + [Fact] public void in_memory_search_should_return_found_starts_with() { @@ -77,7 +89,7 @@ public void in_search_should_return_not_equals() Assert.Null(result.Items); Assert.Equal(TrieNodeSearchResultType.NotFound, result.ResultType); } - + [Fact] public void in_search_should_return_completions() { @@ -90,7 +102,7 @@ public void in_search_should_return_completions() Assert.Equal(3, result.Items.Length); Assert.Equal(TrieNodeSearchResultType.FoundEquals, result.ResultType); } - + [Fact] public void in_search_should_return_not_enough_completions() { @@ -116,7 +128,7 @@ public void in_search_with_tail_should_return_completions() Assert.Equal(2, result.Items.Length); Assert.Equal(TrieNodeSearchResultType.FoundEquals, result.ResultType); } - + [Fact] public void filesystem_memory_search_should_return_found_starts_with() { diff --git a/AutoComplete.Tests/TrieTests.cs b/AutoComplete.Tests/TrieTests.cs index f17220c..0db2066 100644 --- a/AutoComplete.Tests/TrieTests.cs +++ b/AutoComplete.Tests/TrieTests.cs @@ -19,6 +19,43 @@ public void add_null_keyword_should_throw() var trie = new Trie(); Assert.Throws(() => trie.Add(null)); } + + [Fact] + public void add_null_node_to_trie_node_should_throw() + { + var trie = new TrieNode(); + Assert.Throws(() => trie.Add(null)); + } + + [Fact] + public void create_node_from_keyword_should_throw() + { + Assert.Throws(() => TrieNode.CreateFrom(null)); + Assert.Throws(() => TrieNode.CreateFrom(string.Empty)); + } + + [Fact] + public void add_same_character_to_trie_node_should_pass_without_exception() + { + var trie = new TrieNode(); + var child = new TrieNode('a'); + trie.Add(child); + trie.Add(child); + Assert.Equal(1, trie.Children.Count); + } + + [Fact] + public void add_same_character_that_have_children_to_trie_node_should_pass_without_exception() + { + var trie = new TrieNode(); + var child_0 = new TrieNode('a'); + var child_1 = new TrieNode('a'); + child_1.Add(new TrieNode('1')); + + trie.Add(child_0); + trie.Add(child_1); + Assert.Equal(1, trie.Children.Count); + } [Fact] public void add_new_sub_keyword_should_pass() diff --git a/AutoComplete/DataStructure/TrieNode.cs b/AutoComplete/DataStructure/TrieNode.cs index 013833b..0216577 100644 --- a/AutoComplete/DataStructure/TrieNode.cs +++ b/AutoComplete/DataStructure/TrieNode.cs @@ -80,7 +80,7 @@ public TrieNode() public void Add(TrieNode child) { if (child == null) - throw new ArgumentException("child"); + throw new ArgumentNullException(nameof(child)); Children ??= new SortedDictionary(new TrieCharacterComparer()); @@ -137,7 +137,7 @@ public string GetString() public static TrieNode CreateFrom(string keyword) { if (string.IsNullOrWhiteSpace(keyword) || keyword.Length == 0) - throw new ArgumentException(nameof(keyword)); + throw new ArgumentNullException(nameof(keyword)); var returnValue = new TrieNode(keyword[0]); if (keyword.Length == 1) diff --git a/AutoComplete/Helpers/BitArrayHelper.cs b/AutoComplete/Helpers/BitArrayHelper.cs index 95f86b3..23ac830 100644 --- a/AutoComplete/Helpers/BitArrayHelper.cs +++ b/AutoComplete/Helpers/BitArrayHelper.cs @@ -18,7 +18,7 @@ public static void CopyToInt32Array(this BitArray bitArray, int[] array, int ind if (array == null) throw new ArgumentNullException(nameof(array)); - + var location = 0; for (var i = 0; i < bitArray.Length; i++) {