Skip to content

Commit

Permalink
Q & A
Browse files Browse the repository at this point in the history
  • Loading branch information
omerfarukz committed Nov 12, 2022
1 parent 0450aa1 commit 114d7b2
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 7 deletions.
15 changes: 14 additions & 1 deletion AutoComplete.Tests/BitArrayHelperTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections;
using System.Linq;
using AutoComplete.Helpers;
using Xunit;

Expand All @@ -15,10 +16,22 @@ public void passing_null_args_should_throw_argument_exception()
{
BitArrayHelper.CopyToInt32Array(bitArray, Array.Empty<int>(), 0);
});

Assert.Throws<ArgumentNullException>(() =>
{
BitArrayHelper.CopyToInt32Array(new BitArray(Array.Empty<bool>()), 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]);
}
}
1 change: 1 addition & 0 deletions AutoComplete.Tests/IndexBuilderTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.IO;
using AutoComplete.Builders;
using AutoComplete.Clients.IndexSearchers;
using Xunit;

namespace AutoComplete.Tests;
Expand Down
18 changes: 15 additions & 3 deletions AutoComplete.Tests/IndexSearcherTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down Expand Up @@ -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()
{
Expand All @@ -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()
{
Expand All @@ -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()
{
Expand Down
37 changes: 37 additions & 0 deletions AutoComplete.Tests/TrieTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,43 @@ public void add_null_keyword_should_throw()
var trie = new Trie();
Assert.Throws<ArgumentNullException>(() => trie.Add(null));
}

[Fact]
public void add_null_node_to_trie_node_should_throw()
{
var trie = new TrieNode();
Assert.Throws<ArgumentNullException>(() => trie.Add(null));
}

[Fact]
public void create_node_from_keyword_should_throw()
{
Assert.Throws<ArgumentNullException>(() => TrieNode.CreateFrom(null));
Assert.Throws<ArgumentNullException>(() => 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()
Expand Down
4 changes: 2 additions & 2 deletions AutoComplete/DataStructure/TrieNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<char, TrieNode>(new TrieCharacterComparer());

Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion AutoComplete/Helpers/BitArrayHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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++)
{
Expand Down

0 comments on commit 114d7b2

Please sign in to comment.