go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/logdog/client/butler/bundler/sizer_test.go (about)

     1  // Copyright 2015 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package bundler
    16  
    17  import (
    18  	"fmt"
    19  	"testing"
    20  
    21  	"github.com/golang/protobuf/proto"
    22  	. "github.com/smartystreets/goconvey/convey"
    23  )
    24  
    25  // A proto.Message implementation with test fields.
    26  type testMessage struct {
    27  	U64 uint64 `protobuf:"varint,1,opt,name=u64"`
    28  }
    29  
    30  func (t *testMessage) Reset()         {}
    31  func (t *testMessage) String() string { return "" }
    32  func (t *testMessage) ProtoMessage()  {}
    33  
    34  func TestFastSizerVarintLength(t *testing.T) {
    35  	Convey(`A test message`, t, func() {
    36  		for _, threshold := range []uint64{
    37  			0,
    38  			0x80,
    39  			0x4000,
    40  			0x200000,
    41  			0x100000000,
    42  			0x800000000,
    43  			0x40000000000,
    44  			0x2000000000000,
    45  			0x100000000000000,
    46  			0x8000000000000000,
    47  		} {
    48  
    49  			for _, delta := range []int64{
    50  				-2,
    51  				-1,
    52  				0,
    53  				1,
    54  				2,
    55  			} {
    56  				// Add "delta" to "threshold" in a uint64-aware manner.
    57  				u64 := threshold
    58  				if delta >= 0 {
    59  					u64 += uint64(delta)
    60  				} else {
    61  					if u64 < uint64(-delta) {
    62  						continue
    63  					}
    64  					u64 -= uint64(-delta)
    65  				}
    66  
    67  				expected := varintLength(u64)
    68  				Convey(fmt.Sprintf(`Testing threshold 0x%x should encode to varint size %d`, u64, expected), func() {
    69  					m := &testMessage{
    70  						U64: u64,
    71  					}
    72  
    73  					expectedSize := proto.Size(m)
    74  					if u64 == 0 {
    75  						// Proto3 doesn't encode default values (0), so the expected size of
    76  						// the number zero is zero.
    77  						expectedSize = 0
    78  					} else {
    79  						// Accommodate the tag ("1").
    80  						expectedSize -= varintLength(1)
    81  					}
    82  					So(expected, ShouldEqual, expectedSize)
    83  				})
    84  			}
    85  		}
    86  	})
    87  
    88  	Convey(`Calculates protobuf size.`, t, func() {
    89  		pbuf := &testMessage{
    90  			U64: 0x600dd065,
    91  		}
    92  
    93  		So(protoSize(pbuf), ShouldEqual, proto.Size(pbuf))
    94  	})
    95  }