github.com/instana/go-sensor@v1.62.2-0.20240520081010-4919868049e1/autoprofile/internal/pprof/profile/proto_test.go (about)

     1  // (c) Copyright IBM Corp. 2021
     2  // (c) Copyright Instana Inc. 2020
     3  
     4  package profile
     5  
     6  import (
     7  	"reflect"
     8  	"testing"
     9  )
    10  
    11  func TestPackedEncoding(t *testing.T) {
    12  
    13  	type testcase struct {
    14  		uint64s []uint64
    15  		int64s  []int64
    16  		encoded []byte
    17  	}
    18  	for i, tc := range []testcase{
    19  		{
    20  			[]uint64{0, 1, 10, 100, 1000, 10000},
    21  			[]int64{1000, 0, 1000},
    22  			[]byte{10, 8, 0, 1, 10, 100, 232, 7, 144, 78, 18, 5, 232, 7, 0, 232, 7},
    23  		},
    24  		{
    25  			[]uint64{10000},
    26  			nil,
    27  			[]byte{8, 144, 78},
    28  		},
    29  		{
    30  			nil,
    31  			[]int64{-10000},
    32  			[]byte{16, 240, 177, 255, 255, 255, 255, 255, 255, 255, 1},
    33  		},
    34  	} {
    35  		source := &packedInts{tc.uint64s, tc.int64s}
    36  		if got, want := marshal(source), tc.encoded; !reflect.DeepEqual(got, want) {
    37  			t.Errorf("failed encode %d, got %v, want %v", i, got, want)
    38  		}
    39  
    40  		dest := new(packedInts)
    41  		if err := unmarshal(tc.encoded, dest); err != nil {
    42  			t.Errorf("failed decode %d: %v", i, err)
    43  			continue
    44  		}
    45  		if got, want := dest.uint64s, tc.uint64s; !reflect.DeepEqual(got, want) {
    46  			t.Errorf("failed decode uint64s %d, got %v, want %v", i, got, want)
    47  		}
    48  		if got, want := dest.int64s, tc.int64s; !reflect.DeepEqual(got, want) {
    49  			t.Errorf("failed decode int64s %d, got %v, want %v", i, got, want)
    50  		}
    51  	}
    52  }
    53  
    54  type packedInts struct {
    55  	uint64s []uint64
    56  	int64s  []int64
    57  }
    58  
    59  func (u *packedInts) decoder() []decoder {
    60  	return []decoder{
    61  		nil,
    62  		func(b *buffer, m message) error { return decodeUint64s(b, &m.(*packedInts).uint64s) },
    63  		func(b *buffer, m message) error { return decodeInt64s(b, &m.(*packedInts).int64s) },
    64  	}
    65  }
    66  
    67  func (u *packedInts) encode(b *buffer) {
    68  	encodeUint64s(b, 1, u.uint64s)
    69  	encodeInt64s(b, 2, u.int64s)
    70  }