Added trie tests, value tests

This commit is contained in:
obscuren 2014-02-15 13:21:11 +01:00
parent 5883446b21
commit 07c12f0b92
3 changed files with 103 additions and 3 deletions

@ -6,6 +6,8 @@ import (
"testing" "testing"
) )
const LONG_WORD = "1234567890abcdefghijklmnopqrstuvwxxzABCEFGHIJKLMNOPQRSTUVWXYZ"
type MemDatabase struct { type MemDatabase struct {
db map[string][]byte db map[string][]byte
} }
@ -24,11 +26,15 @@ func (db *MemDatabase) Print() {}
func (db *MemDatabase) Close() {} func (db *MemDatabase) Close() {}
func (db *MemDatabase) LastKnownTD() []byte { return nil } func (db *MemDatabase) LastKnownTD() []byte { return nil }
func TestTrieSync(t *testing.T) { func New() (*MemDatabase, *Trie) {
db, _ := NewMemDatabase() 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 { if len(db.db) != 0 {
t.Error("Expected no data in database") t.Error("Expected no data in database")
} }
@ -38,3 +44,55 @@ func TestTrieSync(t *testing.T) {
t.Error("Expected data to be persisted") 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)
}
}

@ -60,6 +60,10 @@ func (val *Value) Uint() uint64 {
return uint64(Val) return uint64(Val)
} else if Val, ok := val.Val.(uint64); ok { } else if Val, ok := val.Val.(uint64); ok {
return Val return Val
} else if Val, ok := val.Val.(int); ok {
return uint64(Val)
} else if Val, ok := val.Val.(uint); ok {
return uint64(Val)
} else if Val, ok := val.Val.([]byte); ok { } else if Val, ok := val.Val.([]byte); ok {
return ReadVarint(bytes.NewReader(Val)) return ReadVarint(bytes.NewReader(Val))
} }

38
ethutil/value_test.go Normal file

@ -0,0 +1,38 @@
package ethutil
import (
"testing"
)
func TestValueCmp(t *testing.T) {
val1 := NewValue("hello")
val2 := NewValue("world")
if val1.Cmp(val2) {
t.Error("Expected values not to be equal")
}
val3 := NewValue("hello")
val4 := NewValue("hello")
if !val3.Cmp(val4) {
t.Error("Expected values to be equal")
}
}
func TestValueTypes(t *testing.T) {
str := NewValue("str")
num := NewValue(1)
inter := NewValue([]interface{}{1})
if str.Str() != "str" {
t.Errorf("expected Str to return 'str', got %s", str.Str())
}
if num.Uint() != 1 {
t.Errorf("expected Uint to return '1', got %d", num.Uint())
}
exp := []interface{}{1}
if !NewValue(inter.Interface()).Cmp(NewValue(exp)) {
t.Errorf("expected Interface to return '%v', got %v", exp, num.Interface())
}
}