aboutsummaryrefslogtreecommitdiffstats
path: root/ethutil
diff options
context:
space:
mode:
authorobscuren <geffobscura@gmail.com>2014-02-15 20:21:11 +0800
committerobscuren <geffobscura@gmail.com>2014-02-15 20:21:11 +0800
commit07c12f0b921a05aec668ae8ce63b6dfac51d76a6 (patch)
tree48f9b9b9eee50353e79a847bf94d31c1e4e0d2c3 /ethutil
parent5883446b219a2980d67ff604c7f227089e5c8619 (diff)
downloaddexon-07c12f0b921a05aec668ae8ce63b6dfac51d76a6.tar.gz
dexon-07c12f0b921a05aec668ae8ce63b6dfac51d76a6.tar.zst
dexon-07c12f0b921a05aec668ae8ce63b6dfac51d76a6.zip
Added trie tests, value tests
Diffstat (limited to 'ethutil')
-rw-r--r--ethutil/trie_test.go64
-rw-r--r--ethutil/value.go4
-rw-r--r--ethutil/value_test.go38
3 files changed, 103 insertions, 3 deletions
diff --git a/ethutil/trie_test.go b/ethutil/trie_test.go
index b87d35e1a..94414b82e 100644
--- a/ethutil/trie_test.go
+++ b/ethutil/trie_test.go
@@ -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)
+ }
+}
diff --git a/ethutil/value.go b/ethutil/value.go
index 2a990783e..f91c33983 100644
--- a/ethutil/value.go
+++ b/ethutil/value.go
@@ -60,6 +60,10 @@ func (val *Value) Uint() uint64 {
return uint64(Val)
} else if Val, ok := val.Val.(uint64); ok {
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 {
return ReadVarint(bytes.NewReader(Val))
}
diff --git a/ethutil/value_test.go b/ethutil/value_test.go
new file mode 100644
index 000000000..7d41eb1c9
--- /dev/null
+++ b/ethutil/value_test.go
@@ -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())
+ }
+}