2014-02-14 22:56:09 +00:00
|
|
|
package ethutil
|
|
|
|
|
|
|
|
import (
|
2014-05-25 22:09:38 +00:00
|
|
|
"fmt"
|
2014-02-20 13:40:00 +00:00
|
|
|
"reflect"
|
2014-02-14 22:56:09 +00:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2014-02-15 12:21:11 +00:00
|
|
|
const LONG_WORD = "1234567890abcdefghijklmnopqrstuvwxxzABCEFGHIJKLMNOPQRSTUVWXYZ"
|
|
|
|
|
2014-02-14 22:56:09 +00:00
|
|
|
type MemDatabase struct {
|
|
|
|
db map[string][]byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewMemDatabase() (*MemDatabase, error) {
|
|
|
|
db := &MemDatabase{db: make(map[string][]byte)}
|
|
|
|
return db, nil
|
|
|
|
}
|
|
|
|
func (db *MemDatabase) Put(key []byte, value []byte) {
|
|
|
|
db.db[string(key)] = value
|
|
|
|
}
|
|
|
|
func (db *MemDatabase) Get(key []byte) ([]byte, error) {
|
|
|
|
return db.db[string(key)], nil
|
|
|
|
}
|
2014-02-24 11:11:00 +00:00
|
|
|
func (db *MemDatabase) Delete(key []byte) error {
|
|
|
|
delete(db.db, string(key))
|
|
|
|
return nil
|
|
|
|
}
|
2014-02-14 22:56:09 +00:00
|
|
|
func (db *MemDatabase) Print() {}
|
|
|
|
func (db *MemDatabase) Close() {}
|
|
|
|
func (db *MemDatabase) LastKnownTD() []byte { return nil }
|
|
|
|
|
2014-02-15 12:21:11 +00:00
|
|
|
func New() (*MemDatabase, *Trie) {
|
2014-02-14 22:56:09 +00:00
|
|
|
db, _ := NewMemDatabase()
|
2014-02-15 12:21:11 +00:00
|
|
|
return db, NewTrie(db, "")
|
|
|
|
}
|
2014-02-14 22:56:09 +00:00
|
|
|
|
2014-02-15 12:21:11 +00:00
|
|
|
func TestTrieSync(t *testing.T) {
|
|
|
|
db, trie := New()
|
|
|
|
|
|
|
|
trie.Update("dog", LONG_WORD)
|
2014-02-14 22:56:09 +00:00
|
|
|
if len(db.db) != 0 {
|
|
|
|
t.Error("Expected no data in database")
|
|
|
|
}
|
|
|
|
|
|
|
|
trie.Sync()
|
|
|
|
if len(db.db) == 0 {
|
|
|
|
t.Error("Expected data to be persisted")
|
|
|
|
}
|
|
|
|
}
|
2014-02-15 12:21:11 +00:00
|
|
|
|
2014-02-16 19:32:56 +00:00
|
|
|
func TestTrieDirtyTracking(t *testing.T) {
|
|
|
|
_, trie := New()
|
|
|
|
trie.Update("dog", LONG_WORD)
|
|
|
|
if !trie.cache.IsDirty {
|
|
|
|
t.Error("Expected trie to be dirty")
|
|
|
|
}
|
|
|
|
|
|
|
|
trie.Sync()
|
|
|
|
if trie.cache.IsDirty {
|
|
|
|
t.Error("Expected trie not to be dirty")
|
|
|
|
}
|
|
|
|
|
|
|
|
trie.Update("test", LONG_WORD)
|
|
|
|
trie.cache.Undo()
|
|
|
|
if trie.cache.IsDirty {
|
|
|
|
t.Error("Expected trie not to be dirty")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-02-15 12:21:11 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2014-02-20 13:40:00 +00:00
|
|
|
|
|
|
|
func TestTrieDelete(t *testing.T) {
|
|
|
|
_, trie := New()
|
|
|
|
trie.Update("cat", LONG_WORD)
|
|
|
|
exp := trie.Root
|
|
|
|
trie.Update("dog", LONG_WORD)
|
|
|
|
trie.Delete("dog")
|
|
|
|
if !reflect.DeepEqual(exp, trie.Root) {
|
|
|
|
t.Errorf("Expected tries to be equal %x : %x", exp, trie.Root)
|
|
|
|
}
|
|
|
|
|
|
|
|
trie.Update("dog", LONG_WORD)
|
|
|
|
exp = trie.Root
|
|
|
|
trie.Update("dude", LONG_WORD)
|
|
|
|
trie.Delete("dude")
|
|
|
|
if !reflect.DeepEqual(exp, trie.Root) {
|
|
|
|
t.Errorf("Expected tries to be equal %x : %x", exp, trie.Root)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTrieDeleteWithValue(t *testing.T) {
|
|
|
|
_, trie := New()
|
|
|
|
trie.Update("c", LONG_WORD)
|
|
|
|
exp := trie.Root
|
|
|
|
trie.Update("ca", LONG_WORD)
|
|
|
|
trie.Update("cat", LONG_WORD)
|
|
|
|
trie.Delete("ca")
|
|
|
|
trie.Delete("cat")
|
|
|
|
if !reflect.DeepEqual(exp, trie.Root) {
|
|
|
|
t.Errorf("Expected tries to be equal %x : %x", exp, trie.Root)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2014-02-24 11:11:00 +00:00
|
|
|
|
|
|
|
func TestTrieIterator(t *testing.T) {
|
|
|
|
_, trie := New()
|
|
|
|
trie.Update("c", LONG_WORD)
|
|
|
|
trie.Update("ca", LONG_WORD)
|
|
|
|
trie.Update("cat", LONG_WORD)
|
|
|
|
|
2014-02-25 09:50:53 +00:00
|
|
|
lenBefore := len(trie.cache.nodes)
|
2014-02-24 11:11:00 +00:00
|
|
|
it := trie.NewIterator()
|
2014-02-25 09:50:53 +00:00
|
|
|
if num := it.Purge(); num != 3 {
|
|
|
|
t.Errorf("Expected purge to return 3, got %d", num)
|
|
|
|
}
|
|
|
|
|
|
|
|
if lenBefore == len(trie.cache.nodes) {
|
|
|
|
t.Errorf("Expected cached nodes to be deleted")
|
|
|
|
}
|
2014-02-24 11:11:00 +00:00
|
|
|
}
|
2014-05-25 22:09:38 +00:00
|
|
|
|
|
|
|
func TestHashes(t *testing.T) {
|
|
|
|
_, trie := New()
|
|
|
|
trie.Update("cat", "dog")
|
|
|
|
trie.Update("ca", "dude")
|
|
|
|
trie.Update("doge", "1234567890abcdefghijklmnopqrstuvwxxzABCEFGHIJKLMNOPQRSTUVWXYZ")
|
|
|
|
trie.Update("dog", "test")
|
|
|
|
trie.Update("test", "1234567890abcdefghijklmnopqrstuvwxxzABCEFGHIJKLMNOPQRSTUVWXYZ")
|
|
|
|
fmt.Printf("%x\n", trie.Root)
|
|
|
|
trie.Delete("dog")
|
|
|
|
fmt.Printf("%x\n", trie.Root)
|
|
|
|
trie.Delete("test")
|
|
|
|
fmt.Printf("%x\n", trie.Root)
|
|
|
|
}
|