Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Anatoli Basov committed Jun 18, 2018
1 parent 19cf5c6 commit b5865a5
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 3 deletions.
2 changes: 1 addition & 1 deletion trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func (n *node) add(key, value []byte) {

func traversePrint(n *node, prefix string) {
if n.key != nil {
fmt.Printf("%s %s\n", prefix, formatBinary(n.key))
fmt.Printf("%s %s %v\n", prefix, formatBinary(n.key), n.value)
return
}
if n.left != nil {
Expand Down
100 changes: 98 additions & 2 deletions trie_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package trie_test
import (
"bytes"
"crypto/rand"
"fmt"
"testing"

"github.com/lukesolo/merkle-trie"
Expand All @@ -16,8 +17,89 @@ func ExampleLowestNumbers() {
tree.Print()

// Output:
// 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
// 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
// 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 []
// 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001 []
}

func ExampleEmptyMerkleTrieHash() {
tree := trie.NewMerkleTrie()
fmt.Printf("%x", tree.Hash())

// Output:
// e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
}

func ExampleSetHighBit() {
tree := trie.NewMerkleTrie()
tree.Add(zero(), nil)
tree.Add(highBit(), nil)

tree.Print()

// Output:
// 0 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 []
// 1 1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 []
}

func ExampleLowestAndHighestNumbers() {
tree := trie.NewMerkleTrie()
tree.Add(max(), nil)
tree.Add(zero(), nil)

tree.Print()

// Output:
// 0 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 []
// 1 1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 []
}

func TestChangeValue(t *testing.T) {
tree := trie.NewMerkleTrie()

tree.Add([]byte{0, 0, 0, 0}, []byte{0})
tree.Add([]byte{255, 255, 0, 128}, []byte{1})
tree.Add([]byte{255, 255, 0, 0}, []byte{2})
tree.Add([]byte{127, 255, 0, 1}, []byte{3})
tree.Add([]byte{127, 255, 0, 128}, []byte{4})

hash1 := tree.Hash()

tree.Add([]byte{127, 255, 0, 1}, []byte{5})
hash2 := tree.Hash()

if bytes.Equal(hash1, hash2) {
t.Error("Change value, but hash still the same")
}

tree.Add([]byte{127, 255, 0, 1}, []byte{3})
hash3 := tree.Hash()

if !bytes.Equal(hash1, hash3) {
t.Error("Restore value, but hash doesn't equal the old one")
}

}

func ExampleMerkleTrie_MaxDepth() {
tree := trie.NewMerkleTrie()
fmt.Println(tree.MaxDepth())

tree.Add([]byte{0, 0, 0, 0}, []byte{0})
tree.Add([]byte{255, 255, 0, 128}, []byte{1})
fmt.Println(tree.MaxDepth())

tree.Add([]byte{0, 255, 0, 1}, []byte{2})
fmt.Println(tree.MaxDepth())

tree.Add([]byte{127, 255, 0, 1}, []byte{3})
tree.Add([]byte{127, 255, 0, 128}, []byte{4})
fmt.Println(tree.MaxDepth())

// Output
// 0
// 1
// 9
// 25
}

func TestIgnoreOrder(t *testing.T) {
Expand Down Expand Up @@ -55,3 +137,17 @@ func one() []byte {
one[31] = 1
return one
}

func highBit() []byte {
res := make([]byte, 32, 32)
res[0] = 128
return res
}

func max() []byte {
res := make([]byte, 32, 32)
for i := range res {
res[i] = 255
}
return res
}

0 comments on commit b5865a5

Please sign in to comment.