portable_storage: fix misconstructed test

Signed-off-by: Ciro S. Costa <utxobr@protonmail.com>
This commit is contained in:
Ciro S. Costa 2021-04-19 09:30:14 -04:00
parent a455aad807
commit ee0f9901b8
3 changed files with 41 additions and 7 deletions

3
go.sum
View file

@ -1,5 +1,6 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/jessevdk/go-flags v1.5.0 h1:1jKYvbxEjfUl0fmqTCOfonvskHHXMjBySTLW4y9LFvc=
github.com/jessevdk/go-flags v1.5.0/go.mod h1:Fw0T6WPc1dYxT4mKEZRfG5kJhaTDP9pj1c2EWnYs/m4=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=

View file

@ -78,9 +78,23 @@ func NewPortableStorageFromBytes(bytes []byte) error {
}
func ReadVarInt(b []byte) int {
// read mask
// depending on the mask:
// read N next bytes
sizeMask := b[0] & PortableRawSizeMarkMask
switch uint32(sizeMask) {
case uint32(PortableRawSizeMarkByte):
return int(b[0] >> 2)
case uint32(PortableRawSizeMarkWord):
return int((binary.LittleEndian.Uint16(b[0:2])) >> 2)
case PortableRawSizeMarkDword:
return int((binary.LittleEndian.Uint32(b[0:4])) >> 2)
case uint32(PortableRawSizeMarkInt64):
panic("int64 not supported") // TODO
// return int((binary.LittleEndian.Uint64(b[0:8])) >> 2)
// '-> bad
default:
panic(fmt.Errorf("malformed sizemask: %+v", sizeMask))
}
return 0
}

View file

@ -12,6 +12,25 @@ import (
func TestPortableStorage(t *testing.T) {
spec.Run(t, "VarrIn", func(t *testing.T, when spec.G, it spec.S) {
it("i <= 63", func() {
b := []byte{0x08}
assert.Equal(t, levin.ReadVarInt(b), 2)
})
it("64 <= i <= 16383", func() {
b := []byte{0x01, 0x02}
assert.Equal(t, levin.ReadVarInt(b), 128)
})
it("16384 <= i <= 1073741823", func() {
b := []byte{0x02, 0x00, 0x01, 0x00}
assert.Equal(t, levin.ReadVarInt(b), 16384)
})
}, spec.Report(report.Log{}), spec.Parallel(), spec.Random())
spec.Run(t, "VarrIn", func(t *testing.T, when spec.G, it spec.S) {
it("i <= 63", func() {
@ -88,20 +107,20 @@ func TestPortableStorage(t *testing.T) {
// node_data
0x09, // len("node_data")
0x6e, 0x6f, 0x64, 0x65, 0x5f, 0x64, 0x61, 0x74, 0x61, // "node_data"
0x12, // boost_serialized_obj
0x0c, // boost_serialized_obj
0x04, // var_in(node_data.entries)
// for i in range node_data
0x03, // len("foo")
0x66, 0x6f, 0x6f, // "foo"
0x10, // boost_serialized_string
0x0a, // boost_serialized_string
0xc, // var_in(len("bar"))
0x62, 0x61, 0x72, // "bar"
// payload_data
0x0c, // len("payload_data")
0x70, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64, 0x5f, 0x64, 0x61, 0x74, 0x61, // "payload_data"
0x12, // boost_serialized_obj
0x0c, // boost_serialized_obj
0x04, // var_in(payload_data.entries)
// for i in range payload_data.entries