2013-12-29 18:09:57 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2013-12-31 20:06:39 -06:00
|
|
|
"encoding/hex"
|
2014-01-02 16:02:24 -06:00
|
|
|
_"fmt"
|
2013-12-29 18:09:57 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestTriePut(t *testing.T) {
|
|
|
|
db, err := NewMemDatabase()
|
2013-12-31 20:06:39 -06:00
|
|
|
trie := NewTrie(db, "")
|
2013-12-29 18:09:57 -06:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Error starting db")
|
|
|
|
}
|
|
|
|
|
2013-12-31 20:06:39 -06:00
|
|
|
key := trie.Put([]byte("testing node"))
|
2013-12-29 18:09:57 -06:00
|
|
|
|
|
|
|
data, err := db.Get(key)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Nothing at node")
|
|
|
|
}
|
|
|
|
|
|
|
|
s, _ := Decode(data, 0)
|
|
|
|
if str, ok := s.([]byte); ok {
|
|
|
|
if string(str) != "testing node" {
|
|
|
|
t.Error("Wrong value node", str)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
t.Error("Invalid return type")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTrieUpdate(t *testing.T) {
|
|
|
|
db, err := NewMemDatabase()
|
2013-12-31 20:06:39 -06:00
|
|
|
trie := NewTrie(db, "")
|
2013-12-29 18:09:57 -06:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Error starting db")
|
|
|
|
}
|
|
|
|
|
2013-12-31 20:06:39 -06:00
|
|
|
|
|
|
|
trie.Update("doe", "reindeer")
|
|
|
|
trie.Update("dog", "puppy")
|
2014-01-02 16:02:24 -06:00
|
|
|
/*
|
|
|
|
data, _ := db.Get([]byte(trie.root))
|
|
|
|
data, _ = db.Get([]byte(DecodeNode(data)[1]))
|
|
|
|
data, _ = db.Get([]byte(DecodeNode(data)[7]))
|
|
|
|
PrintSlice(DecodeNode(data))
|
|
|
|
*/
|
|
|
|
|
2013-12-31 20:06:39 -06:00
|
|
|
trie.Update("dogglesworth", "cat")
|
|
|
|
root := hex.EncodeToString([]byte(trie.root))
|
|
|
|
req := "e378927bfc1bd4f01a2e8d9f59bd18db8a208bb493ac0b00f93ce51d4d2af76c"
|
|
|
|
if root != req {
|
|
|
|
t.Error("trie.root do not match, expected", req, "got", root)
|
|
|
|
}
|
2013-12-29 18:09:57 -06:00
|
|
|
}
|
|
|
|
|