public void TestTrieGetAfterFlush() { var memDb = new MemoryDictionarySource(); var trie = new PatriciaTrie(memDb); trie.Put(dog, cat); Assert.Equal(cat, trie.Get(dog)); trie.Flush(); Assert.Equal(cat, trie.Get(dog)); }
public void TestTrieGetPut() { // We can retrieve the values we put in var trie = new PatriciaTrie(); trie.Put(dog, cat); trie.Put(fish, bird); Assert.Equal(cat, trie.Get(dog)); Assert.Equal(bird, trie.Get(fish)); trie.Put(dog, fish); trie.Put(dog, bird); Assert.Equal(bird, trie.Get(dog)); }
public void TestTrieBulkData() { var memDb = new MemoryDictionarySource(); var trie = new PatriciaTrie(memDb); Dictionary <string, string> toInput = new Dictionary <string, string>(); for (int i = 0; i < 1000; i++) { toInput.Add( new Random().Next().ToString(), new Random().Next().ToString() ); } foreach (var kvp in toInput) { trie.Put(Encoding.UTF8.GetBytes(kvp.Key), Encoding.UTF8.GetBytes(kvp.Value)); } foreach (var kvp in toInput) { Assert.Equal(kvp.Value, Encoding.UTF8.GetString(trie.Get(Encoding.UTF8.GetBytes(kvp.Key)))); } trie.Put(dog, cat); trie.Put(fish, bird); trie.Put(dodecahedron, fish); trie.Flush(); byte[] savedHash = trie.GetRootHash(); var trie2 = new PatriciaTrie(memDb); trie2.SetRootHash(savedHash); Assert.Equal(cat, trie.Get(dog)); Assert.Equal(cat, trie2.Get(dog)); Assert.Equal(bird, trie2.Get(fish)); Assert.Equal(fish, trie2.Get(dodecahedron)); foreach (var kvp in toInput) { Assert.Equal(kvp.Value, Encoding.UTF8.GetString(trie2.Get(Encoding.UTF8.GetBytes(kvp.Key)))); } }
public void Remove_OneKeyOneValue_Removed() { var trie = new PatriciaTrie <int>(); trie.Add("foo", 42); trie.Remove("foo", v => true); Assert.Equal(0, Enumerable.Count <int>(trie.Get("foo"))); }
public void Add_OneKeyTwice_TwoResultValues() { var trie = new PatriciaTrie <int>(); trie.Add("foo", 42); trie.Add("foo", 43); var result = trie.Get("foo"); Assert.Equal(2, Enumerable.Count <int>(result)); }
public void Remove_OneKeyTwoValuesRemoveOne_ExpectedValueLost() { var trie = new PatriciaTrie <int>(); trie.Add("foo", 42); trie.Add("foo", 43); trie.Remove("foo", v => v == 42); Assert.Equal(43, Enumerable.First <int>(trie.Get("foo"))); }
public void TestTrieLoad() { var memDb = new MemoryDictionarySource(); var trie = new PatriciaTrie(memDb); trie.Put(dog, cat); trie.Put(fish, bird); trie.Put(dodecahedron, fish); trie.Flush(); byte[] savedHash = trie.GetRootHash(); var trie2 = new PatriciaTrie(memDb); trie2.SetRootHash(savedHash); Assert.Equal(cat, trie.Get(dog)); Assert.Equal(cat, trie2.Get(dog)); Assert.Equal(bird, trie2.Get(fish)); Assert.Equal(fish, trie2.Get(dodecahedron)); }
public void Add_OneKeyTwice_ExpectedValues() { var trie = new PatriciaTrie <int>(); trie.Add("foo", 42); trie.Add("foo", 43); var result = Enumerable.ToArray <int>(trie.Get("foo")); Assert.Equal(42, result[0]); Assert.Equal(43, result[1]); }
public void TestTrieFlush() { var memDb = new MemoryDictionarySource(); var trie = new PatriciaTrie(memDb); trie.Put(dog, cat); trie.Put(fish, bird); Assert.Equal(cat, trie.Get(dog)); Assert.Empty(memDb.Db.Keys); trie.Flush(); Assert.NotEmpty(memDb.Db.Keys); // This should be more specific in future. How many nodes are we expecting? }