Added trie tests, value tests
This commit is contained in:
@@ -6,6 +6,8 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
const LONG_WORD = "1234567890abcdefghijklmnopqrstuvwxxzABCEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
|
||||
type MemDatabase struct {
|
||||
db map[string][]byte
|
||||
}
|
||||
@@ -24,11 +26,15 @@ func (db *MemDatabase) Print() {}
|
||||
func (db *MemDatabase) Close() {}
|
||||
func (db *MemDatabase) LastKnownTD() []byte { return nil }
|
||||
|
||||
func TestTrieSync(t *testing.T) {
|
||||
func New() (*MemDatabase, *Trie) {
|
||||
db, _ := NewMemDatabase()
|
||||
trie := NewTrie(db, "")
|
||||
return db, NewTrie(db, "")
|
||||
}
|
||||
|
||||
trie.Update("dog", "kindofalongsentencewhichshouldbeencodedinitsentirety")
|
||||
func TestTrieSync(t *testing.T) {
|
||||
db, trie := New()
|
||||
|
||||
trie.Update("dog", LONG_WORD)
|
||||
if len(db.db) != 0 {
|
||||
t.Error("Expected no data in database")
|
||||
}
|
||||
@@ -38,3 +44,55 @@ func TestTrieSync(t *testing.T) {
|
||||
t.Error("Expected data to be persisted")
|
||||
}
|
||||
}
|
||||
|
||||
func TestTrieReset(t *testing.T) {
|
||||
_, trie := New()
|
||||
|
||||
trie.Update("cat", LONG_WORD)
|
||||
if len(trie.cache.nodes) == 0 {
|
||||
t.Error("Expected cached nodes")
|
||||
}
|
||||
|
||||
trie.cache.Undo()
|
||||
|
||||
if len(trie.cache.nodes) != 0 {
|
||||
t.Error("Expected no nodes after undo")
|
||||
}
|
||||
}
|
||||
|
||||
func TestTrieGet(t *testing.T) {
|
||||
_, trie := New()
|
||||
|
||||
trie.Update("cat", LONG_WORD)
|
||||
x := trie.Get("cat")
|
||||
if x != LONG_WORD {
|
||||
t.Error("expected %s, got %s", LONG_WORD, x)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTrieUpdating(t *testing.T) {
|
||||
_, trie := New()
|
||||
trie.Update("cat", LONG_WORD)
|
||||
trie.Update("cat", LONG_WORD+"1")
|
||||
x := trie.Get("cat")
|
||||
if x != LONG_WORD+"1" {
|
||||
t.Error("expected %S, got %s", LONG_WORD+"1", x)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTrieCmp(t *testing.T) {
|
||||
_, trie1 := New()
|
||||
_, trie2 := New()
|
||||
|
||||
trie1.Update("doge", LONG_WORD)
|
||||
trie2.Update("doge", LONG_WORD)
|
||||
if !trie1.Cmp(trie2) {
|
||||
t.Error("Expected tries to be equal")
|
||||
}
|
||||
|
||||
trie1.Update("dog", LONG_WORD)
|
||||
trie2.Update("cat", LONG_WORD)
|
||||
if trie1.Cmp(trie2) {
|
||||
t.Errorf("Expected tries not to be equal %x %x", trie1.Root, trie2.Root)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user