arnaucube
arnaucube/arboPublic Self-hosted repository
arbo/vt_test.go
vt_test.goRaw
 1package arbo
 2
 3import (
 4	"math/big"
 5	"testing"
 6
 7	qt "github.com/frankban/quicktest"
 8)
 9
10func TestVirtualTree(t *testing.T) {
11	c := qt.New(t)
12	vTree := newVT(10, HashFunctionSha256)
13
14	c.Assert(vTree.root, qt.IsNil)
15
16	k := BigIntToBytes(big.NewInt(1))
17	v := BigIntToBytes(big.NewInt(2))
18	err := vTree.add(k, v)
19	c.Assert(err, qt.IsNil)
20
21	// check values
22	c.Assert(vTree.root.k, qt.DeepEquals, k)
23	c.Assert(vTree.root.v, qt.DeepEquals, v)
24
25	// compute hashes
26	pairs, err := vTree.computeHashes()
27	c.Assert(err, qt.IsNil)
28	c.Assert(len(pairs), qt.Equals, 1)
29
30	rootBI := BytesToBigInt(vTree.root.h)
31	c.Assert(rootBI.String(), qt.Equals,
32		"46910109172468462938850740851377282682950237270676610513794735904325820156367")
33
34	k = BigIntToBytes(big.NewInt(33))
35	v = BigIntToBytes(big.NewInt(44))
36	err = vTree.add(k, v)
37	c.Assert(err, qt.IsNil)
38
39	// compute hashes
40	pairs, err = vTree.computeHashes()
41	c.Assert(err, qt.IsNil)
42	c.Assert(len(pairs), qt.Equals, 8)
43
44	// err = vTree.printGraphviz()
45	// c.Assert(err, qt.IsNil)
46
47	rootBI = BytesToBigInt(vTree.root.h)
48	c.Assert(rootBI.String(), qt.Equals,
49		"59481735341404520835410489183267411392292882901306595567679529387376287440550")
50	c.Assert(err, qt.IsNil)
51}