Skip to content

Commit

Permalink
Merge pull request #3777 from greymistcube/chore/trie-remove-test
Browse files Browse the repository at this point in the history
✅ Added tests for `ITrie`
  • Loading branch information
greymistcube authored May 3, 2024
2 parents 5bcd797 + 8c622b7 commit 1b7e5fd
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
3 changes: 2 additions & 1 deletion Libplanet.Store/Trie/ITrie.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ public interface ITrie

/// <summary>
/// Removes the value at the path corresponding to given <paramref name="key"/>
/// <em>in memory</em>.
/// <em>in memory</em>. If there is no <see cref="IValue"/> at <paramref name="key"/>,
/// this does nothing.
/// </summary>
/// <param name="key">The unique key to associate with the <paramref name="value"/>.</param>
/// <returns>Returns new updated <see cref="ITrie"/>.</returns>
Expand Down
32 changes: 31 additions & 1 deletion Libplanet.Tests/Store/Trie/MerkleTrieTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ public void SetValueToFullNode()
public void RemoveValue()
{
IKeyValueStore keyValueStore = new MemoryKeyValueStore();
IStateStore stateStore = new TrieStateStore(new MemoryKeyValueStore());
IStateStore stateStore = new TrieStateStore(keyValueStore);
ITrie trie = stateStore.GetStateRoot(null);
HashDigest<SHA256> nullTrieHash = trie.Hash;

Expand Down Expand Up @@ -529,5 +529,35 @@ public void RemoveValue()
Assert.Empty(expected);
Assert.Null(trie.Root);
}

[Fact]
public void RemoveValueNoOp()
{
IStateStore stateStore = new TrieStateStore(new MemoryKeyValueStore());
ITrie trie = stateStore.GetStateRoot(null);

KeyBytes key00 = new KeyBytes(new byte[] { 0x00 });
KeyBytes key0000 = new KeyBytes(new byte[] { 0x00, 0x00 });
IValue value0000 = new Text("0000");
KeyBytes key0011 = new KeyBytes(new byte[] { 0x00, 0x11 });
IValue value0011 = new Text("0011");
KeyBytes key000000 = new KeyBytes(new byte[] { 0x00, 0x00, 0x00 });

trie = trie.Set(key0000, value0000);
trie = trie.Set(key0011, value0011);
trie = stateStore.Commit(trie);
int expectedNodeCount = trie.IterateNodes().Count();
int expectedValueCount = trie.IterateValues().Count();
HashDigest<SHA256> expectedHash = trie.Hash;

// Does nothing without throwing an exception when trying to remove value from
// a path where there is a node without value or a non-existent path.
trie = trie.Remove(key00);
trie = trie.Remove(key000000);
trie = stateStore.Commit(trie);
Assert.Equal(expectedNodeCount, trie.IterateNodes().Count());
Assert.Equal(expectedValueCount, trie.IterateValues().Count());
Assert.Equal(expectedHash, trie.Hash);
}
}
}

0 comments on commit 1b7e5fd

Please sign in to comment.