Source file src/internal/profile/proto_test.go

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

View as plain text