github.com/grafana/pyroscope@v1.18.0/pkg/phlaredb/symdb/strings_test.go (about)

     1  package symdb
     2  
     3  import (
     4  	"bytes"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/require"
     9  )
    10  
    11  func Test_StringsEncoding(t *testing.T) {
    12  	type testCase struct {
    13  		description string
    14  		strings     []string
    15  	}
    16  
    17  	testCases := []testCase{
    18  		{
    19  			description: "empty",
    20  			strings:     []string{},
    21  		},
    22  		{
    23  			description: "less than block size",
    24  			strings: []string{
    25  				"a",
    26  				"b",
    27  			},
    28  		},
    29  		{
    30  			description: "exact block size",
    31  			strings: []string{
    32  				"a",
    33  				"bc",
    34  				"cde",
    35  				"def",
    36  			},
    37  		},
    38  		{
    39  			description: "greater than block size",
    40  			strings: []string{
    41  				"a",
    42  				"bc",
    43  				"cde",
    44  				"def",
    45  				"e",
    46  			},
    47  		},
    48  		{
    49  			description: "mixed encoding",
    50  			strings: []string{
    51  				"a",
    52  				"bcd",
    53  				strings.Repeat("e", 256),
    54  			},
    55  		},
    56  		{
    57  			description: "mixed encoding exact block",
    58  			strings: []string{
    59  				"a",
    60  				"b",
    61  				"c",
    62  				"d",
    63  				strings.Repeat("e", 256),
    64  				strings.Repeat("f", 256),
    65  				strings.Repeat("j", 256),
    66  				strings.Repeat("h", 256),
    67  			},
    68  		},
    69  	}
    70  
    71  	for _, tc := range testCases {
    72  		tc := tc
    73  		t.Run(tc.description, func(t *testing.T) {
    74  			var buf bytes.Buffer
    75  			w := newTestFileWriter(&buf)
    76  			e := newStringsEncoder()
    77  			e.blockSize = 4
    78  			h, err := writeSymbolsBlock(w, tc.strings, e)
    79  			require.NoError(t, err)
    80  
    81  			d, err := newStringsDecoder(h)
    82  			require.NoError(t, err)
    83  			out := make([]string, h.Length)
    84  			require.NoError(t, d.decode(out, &buf))
    85  			require.Equal(t, tc.strings, out)
    86  		})
    87  	}
    88  }