MCPcopy
hub / github.com/canopy-network/canopy / TestMerkleTree

Function TestMerkleTree

lib/util_test.go:103–191  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

101}
102
103func TestMerkleTree(t *testing.T) {
104 tests := []struct {
105 name string
106 detail string
107 items [][]byte
108 expectedRoot []byte
109 expectedTree [][]byte
110 }{
111 {
112 name: "empty input",
113 detail: "there are no items",
114 items: nil,
115 expectedRoot: []byte{},
116 expectedTree: [][]byte{},
117 },
118 {
119 name: "one item",
120 detail: "there's one entry in the tree",
121 items: [][]byte{[]byte("a")},
122 expectedRoot: crypto.Hash([]byte("a")),
123 expectedTree: [][]byte{
124 crypto.Hash([]byte("a")),
125 },
126 },
127 {
128 name: "multi-item",
129 detail: "there are multiple items added",
130 items: [][]byte{
131 []byte("a"),
132 []byte("b"),
133 []byte("c"),
134 []byte("d"),
135 },
136 expectedTree: [][]byte{
137 crypto.Hash([]byte("a")),
138 crypto.Hash([]byte("b")),
139 crypto.Hash([]byte("c")),
140 crypto.Hash([]byte("d")),
141 crypto.Hash(append(crypto.Hash([]byte("a")), crypto.Hash([]byte("b"))...)),
142 crypto.Hash(append(crypto.Hash([]byte("c")), crypto.Hash([]byte("d"))...)),
143 crypto.Hash(append(
144 crypto.Hash(append(crypto.Hash([]byte("a")), crypto.Hash([]byte("b"))...)),
145 crypto.Hash(append(crypto.Hash([]byte("c")), crypto.Hash([]byte("d"))...))...,
146 )),
147 },
148 expectedRoot: crypto.Hash(
149 append(
150 crypto.Hash(append(crypto.Hash([]byte("a")), crypto.Hash([]byte("b"))...)),
151 crypto.Hash(append(crypto.Hash([]byte("c")), crypto.Hash([]byte("d"))...))...,
152 ),
153 ),
154 },
155 {
156 name: "'non power of two' multi-item",
157 detail: "there are multiple items, but not enough for a perfect po2 tree",
158 items: [][]byte{
159 []byte("a"),
160 []byte("b"),

Callers

nothing calls this directly

Calls 4

HashFunction · 0.92
RunMethod · 0.80
EqualMethod · 0.80
MerkleTreeFunction · 0.70

Tested by

no test coverage detected