-
Notifications
You must be signed in to change notification settings - Fork 15
/
util_test.go
81 lines (68 loc) · 1.71 KB
/
util_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
package amt
import (
"bytes"
"fmt"
"io"
"testing"
cbor "github.com/ipfs/go-ipld-cbor"
"github.com/stretchr/testify/require"
cbg "github.com/whyrusleeping/cbor-gen"
)
var width uint = 2
func BenchmarkNodesForHeight(b *testing.B) {
width = 9
for i := 0; i < b.N; i++ {
nodesForHeight(width, i%15)
}
}
func TestNodesForHeight(t *testing.T) {
require.Equal(t, uint64(1), nodesForHeight(1, 0))
require.Equal(t, uint64(4), nodesForHeight(2, 1))
require.Equal(t, uint64(64), nodesForHeight(3, 2))
require.Equal(t, uint64(4096), nodesForHeight(4, 3))
}
// A CBOR-marshalable byte array.
// Note: this is duplicated from the HAMT tests. We should extract common CBOR manipulation utilities to a
// Filecoin shared library.
type CborByteArray []byte
func (c *CborByteArray) MarshalCBOR(w io.Writer) error {
if err := cbg.WriteMajorTypeHeader(w, cbg.MajByteString, uint64(len(*c))); err != nil {
return err
}
_, err := w.Write(*c)
return err
}
func (c *CborByteArray) UnmarshalCBOR(r io.Reader) error {
maj, extra, err := cbg.CborReadHeader(r)
if err != nil {
return err
}
if maj != cbg.MajByteString {
return fmt.Errorf("expected byte array")
}
if uint64(cap(*c)) < extra {
*c = make([]byte, extra)
}
if _, err := io.ReadFull(r, *c); err != nil {
return err
}
return nil
}
func cborstr(s string) *CborByteArray {
v := CborByteArray(s)
return &v
}
func TestStringCborRoundtrip(t *testing.T) {
input := "foo"
val := cborstr(input)
valueBuf := new(bytes.Buffer)
if err := val.MarshalCBOR(valueBuf); err != nil {
t.Fatal(err)
}
encoded := valueBuf.Bytes()
var actual CborByteArray
cbor.DecodeInto(encoded, &actual)
if string(actual) != input {
t.Errorf("got %s, wanted %s", actual, input)
}
}