...

Source file src/github.com/gogo/protobuf/test/issue620/issue620_test.go

Documentation: github.com/gogo/protobuf/test/issue620

     1  // Protocol Buffers for Go with Gadgets
     2  //
     3  // Copyright (c) 2019, The GoGo Authors. All rights reserved.
     4  // http://github.com/gogo/protobuf
     5  //
     6  // Redistribution and use in source and binary forms, with or without
     7  // modification, are permitted provided that the following conditions are
     8  // met:
     9  //
    10  //     * Redistributions of source code must retain the above copyright
    11  // notice, this list of conditions and the following disclaimer.
    12  //     * Redistributions in binary form must reproduce the above
    13  // copyright notice, this list of conditions and the following disclaimer
    14  // in the documentation and/or other materials provided with the
    15  // distribution.
    16  //
    17  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    18  // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    19  // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    20  // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
    21  // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    22  // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    23  // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    24  // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    25  // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    26  // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    27  // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    28  
    29  package issue620
    30  
    31  import (
    32  	"bytes"
    33  	"testing"
    34  
    35  	"github.com/gogo/protobuf/proto"
    36  )
    37  
    38  func TestEncodeShort(t *testing.T) {
    39  	exp := []byte{8, 10, 6, 66, 97, 114, 49, 50, 51}
    40  	s := "Bar123"
    41  	b1 := &Bar{Field1: &s}
    42  	bufProto := proto.NewBuffer(nil)
    43  
    44  	b2 := &BarFast{Field1: &s}
    45  	bufProtoFast := proto.NewBuffer(nil)
    46  
    47  	encodeMessageCheck(t, b1, b2, bufProto, bufProtoFast, exp)
    48  }
    49  
    50  func TestEncodeLong(t *testing.T) {
    51  	exp := []byte{8, 10, 6, 66, 97, 114, 49, 50, 51}
    52  	s := "Bar123"
    53  	b1 := &Bar{Field1: &s}
    54  	bufProto := proto.NewBuffer(make([]byte, 0, 480))
    55  	b2 := &BarFast{Field1: &s}
    56  	bufProtoFast := proto.NewBuffer(make([]byte, 0, 480))
    57  
    58  	encodeMessageCheck(t, b1, b2, bufProto, bufProtoFast, exp)
    59  }
    60  
    61  func TestEncodeDecode(t *testing.T) {
    62  	s := "Bar123"
    63  	bar := &BarFast{Field1: &s}
    64  	bufProtoFast := proto.NewBuffer(make([]byte, 0, 480))
    65  	err := bufProtoFast.EncodeMessage(bar)
    66  	errCheck(t, err)
    67  	dec := &BarFast{}
    68  	err = bufProtoFast.DecodeMessage(dec)
    69  	errCheck(t, err)
    70  	if !dec.Equal(bar) {
    71  		t.Errorf("Expect %+v got %+v after encode/decode", bar, dec)
    72  	}
    73  }
    74  
    75  func encodeMessageCheck(t *testing.T, b1, b2 proto.Message, bufProto, bufProtoFast *proto.Buffer, exp []byte) {
    76  	err := bufProto.EncodeMessage(b1)
    77  	errCheck(t, err)
    78  	err = bufProtoFast.EncodeMessage(b2)
    79  	errCheck(t, err)
    80  	checkEqual(t, exp, bufProto.Bytes())
    81  	checkEqual(t, exp, bufProtoFast.Bytes())
    82  
    83  	bufProto.Reset()
    84  	bufProtoFast.Reset()
    85  	expMore := make([]byte, 0, len(exp))
    86  	copy(expMore, exp)
    87  	for i := 0; i < 10; i++ {
    88  		err = bufProto.EncodeMessage(b1)
    89  		errCheck(t, err)
    90  		err = bufProtoFast.EncodeMessage(b2)
    91  		errCheck(t, err)
    92  		expMore = append(expMore, exp...)
    93  		checkEqual(t, expMore, bufProto.Bytes())
    94  		checkEqual(t, expMore, bufProtoFast.Bytes())
    95  	}
    96  
    97  	bufProto.Reset()
    98  	bufProtoFast.Reset()
    99  	err = bufProto.EncodeMessage(b1)
   100  	errCheck(t, err)
   101  	err = bufProtoFast.EncodeMessage(b2)
   102  	errCheck(t, err)
   103  	checkEqual(t, exp, bufProto.Bytes())
   104  	checkEqual(t, exp, bufProtoFast.Bytes())
   105  }
   106  
   107  func errCheck(t *testing.T, err error) {
   108  	t.Helper()
   109  	if err != nil {
   110  		t.Error(err.Error())
   111  	}
   112  }
   113  
   114  func checkEqual(t *testing.T, b1, b2 []byte) {
   115  	t.Helper()
   116  	if !bytes.Equal(b1, b2) {
   117  		t.Errorf("%v != %v\n", b1, b2)
   118  	}
   119  }
   120  

View as plain text