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