github.com/cosmos/cosmos-sdk@v0.50.10/x/group/internal/orm/key_codec_test.go (about)

     1  package orm
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/require"
     7  )
     8  
     9  func TestAddLengthPrefix(t *testing.T) {
    10  	tcs := []struct {
    11  		name     string
    12  		in       []byte
    13  		expected []byte
    14  	}{
    15  		{"empty", []byte{}, []byte{0}},
    16  		{"nil", nil, []byte{0}},
    17  		{"some data", []byte{0, 1, 100, 200}, []byte{4, 0, 1, 100, 200}},
    18  	}
    19  	for _, tc := range tcs {
    20  		t.Run(tc.name, func(t *testing.T) {
    21  			out := AddLengthPrefix(tc.in)
    22  			require.Equal(t, tc.expected, out)
    23  		})
    24  	}
    25  
    26  	require.Panics(t, func() {
    27  		AddLengthPrefix(make([]byte, 256))
    28  	})
    29  }
    30  
    31  func TestNullTerminatedBytes(t *testing.T) {
    32  	tcs := []struct {
    33  		name     string
    34  		in       string
    35  		expected []byte
    36  	}{
    37  		{"empty", "", []byte{0}},
    38  		{"some data", "abc", []byte{0x61, 0x62, 0x63, 0}},
    39  	}
    40  	for _, tc := range tcs {
    41  		t.Run(tc.name, func(t *testing.T) {
    42  			out := NullTerminatedBytes(tc.in)
    43  			require.Equal(t, tc.expected, out)
    44  		})
    45  	}
    46  }