github.com/hechain20/hechain@v0.0.0-20220316014945-b544036ba106/common/ledger/blkstorage/protobuf_util_test.go (about)

     1  /*
     2  Copyright hechain. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package blkstorage
     8  
     9  import (
    10  	"testing"
    11  
    12  	"github.com/golang/protobuf/proto"
    13  	"github.com/stretchr/testify/require"
    14  )
    15  
    16  func TestBuffer(t *testing.T) {
    17  	pb := proto.NewBuffer(nil)
    18  	pb.EncodeVarint(10)
    19  	pos1 := len(pb.Bytes())
    20  	pb.EncodeRawBytes([]byte("JunkText"))
    21  	pos2 := len(pb.Bytes())
    22  	pb.EncodeRawBytes([]byte("YetAnotherJunkText"))
    23  	pos3 := len(pb.Bytes())
    24  	pb.EncodeVarint(1000000)
    25  	pos4 := len(pb.Bytes())
    26  
    27  	b := newBuffer(pb.Bytes())
    28  	b.DecodeVarint()
    29  	require.Equal(t, pos1, b.GetBytesConsumed())
    30  	b.DecodeRawBytes(false)
    31  	require.Equal(t, pos2, b.GetBytesConsumed())
    32  	b.DecodeRawBytes(false)
    33  	require.Equal(t, pos3, b.GetBytesConsumed())
    34  	b.DecodeVarint()
    35  	require.Equal(t, pos4, b.GetBytesConsumed())
    36  }