-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
116 lines (93 loc) · 2.92 KB
/
main_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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package main
import (
"testing"
)
func TestTree_getHashRoot(t *testing.T) {
hashRootExpected := "62af5c3cb8da3e4f25061e829ebeea5c7513c54949115b1acc225930a90154da"
tree, err := CreateMerkleTree([]string{"a", "b"})
if err != nil {
t.Errorf("Exception raised while creating tree.")
return
}
if tree.GetRoot() != hashRootExpected {
t.Errorf("Hash was incorrect, got: %d, want: %d.", tree.hash, hashRootExpected)
}
}
func TestTree_GetLevel(t *testing.T) {
tree, err := CreateMerkleTree([]string{"a", "b"})
if err != nil {
t.Errorf("Exception raised while creating tree.")
return
}
_, err = tree.GetLevel(2)
if err == nil {
t.Errorf("Exception should have been raised while getting level 2, on a 1 level tree.")
}
}
func TestTree_getLevel2(t *testing.T) {
a := "ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb"
b := "3e23e8160039594a33894f6564e1b1348bbd7a0088d42c4acb73eeaed59c009d"
c := "2e7d2c03a9507ae265ecf5b5356885a53393a2029d241394997265a1a25aefc6"
tree, err := CreateMerkleTree([]string{"a", "b", "c"})
if err != nil {
t.Errorf("Exception raised while creating tree.")
return
}
nodes, err := tree.GetLevel(2)
if err != nil {
t.Errorf("Error while getting level 2.")
return
}
if len(nodes) != 3 {
t.Errorf("Node must be of length 3")
}
if a != nodes[0] {
t.Errorf("Hash was incorrect, got: %d, want: %d.", tree.hash, nodes[0])
}
if b != nodes[1] {
t.Errorf("Hash was incorrect, got: %d, want: %d.", tree.hash, nodes[1])
}
if c != nodes[2] {
t.Errorf("Hash was incorrect, got: %d, want: %d.", tree.hash, nodes[2])
}
}
func TestTree_0node(t *testing.T) {
_, err := CreateMerkleTree([]string{})
if err == nil {
t.Errorf("Exception should have been raised while creating tree.")
return
}
}
func TestTree_4nodes(t *testing.T) {
hashRootExpected := "58c89d709329eb37285837b042ab6ff72c7c8f74de0446b091b6a0131c102cfd"
tree, err := CreateMerkleTree([]string{"a", "b", "c", "d"})
if err != nil {
t.Errorf("Exception raised while creating tree.")
return
}
if tree.hash != hashRootExpected {
t.Errorf("Hash was incorrect, got: %d, want: %d.", tree.hash, hashRootExpected)
}
}
func TestTree_5nodes(t *testing.T) {
hashRootExpected := "58c89d709329eb37285837b042ab6ff72c7c8f74de0446b091b6a0131c102cfd"
tree, err := CreateMerkleTree([]string{"a", "b", "c", "d"})
if err != nil {
t.Errorf("Exception raised while creating tree.")
return
}
if tree.hash != hashRootExpected {
t.Errorf("Hash was incorrect, got: %d, want: %d.", tree.hash, hashRootExpected)
}
}
func TestTree_9nodes(t *testing.T) {
hashRootExpected := "95f57bf74ca2317b9056ad66ecd7582e6e470511d960be8bb594e51b8c8a6498"
tree, err := CreateMerkleTree([]string{"a", "b", "c", "d", "e", "f", "g", "h", "i"})
if err != nil {
t.Errorf("Exception raised while creating tree.")
return
}
if tree.hash != hashRootExpected {
t.Errorf("Hash was incorrect, got: %d, want: %d.", tree.hash, hashRootExpected)
}
}