-
Notifications
You must be signed in to change notification settings - Fork 0
/
trie_test.go
45 lines (36 loc) · 1.28 KB
/
trie_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package main
import (
"encoding/hex"
"testing"
)
func TestTriePut(t *testing.T) {
db := NewMemDatabase()
trie := NewTrie(db)
key := trie.Put([]byte("testing node"))
data := trie.Get(key)
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 := NewMemDatabase()
trie := NewTrie(db)
trie.PutSatae([]byte("dog"), "puppy")
trie.PutSatae([]byte("dogglesworth"), "cat")
trie.PutSatae([]byte("doe"), "reindeer")
//data := trie.Get([]byte(trie.root)) // 透過hash查找,得到root node
//data = trie.Get([]byte(DecodeNode(data)[1])) // root node 有兩個欄位,key , value,取value欄位,branch node
//data = trie.Get([]byte(DecodeNode(data)[6])) // expand node有26個欄位,對應字母a~z,取第7個欄位(index==6),得到leaf node
//PrintSliceReal(DecodeNode(data)) // 印出leaf node中的真實data:["7lesworth","cat"]
//PrintSlice(DecodeNode(data)) // 印出易讀data:["glesworth","cat"]
root := hex.EncodeToString([]byte(trie.root))
req := "e378927bfc1bd4f01a2e8d9f59bd18db8a208bb493ac0b00f93ce51d4d2af76c"
if root != req {
t.Error("trie.root do not match, expected", req, "got", root)
}
}