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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
package main
import (
"testing"
"encoding/hex"
_"fmt"
)
func TestTriePut(t *testing.T) {
db, err := NewMemDatabase()
trie := NewTrie(db, "")
if err != nil {
t.Error("Error starting db")
}
key := trie.Put([]byte("testing node"))
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()
trie := NewTrie(db, "")
if err != nil {
t.Error("Error starting db")
}
trie.Update("doe", "reindeer")
trie.Update("dog", "puppy")
/*
data, _ := db.Get([]byte(trie.root))
data, _ = db.Get([]byte(DecodeNode(data)[1]))
data, _ = db.Get([]byte(DecodeNode(data)[7]))
PrintSlice(DecodeNode(data))
*/
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)
}
}
|