OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package bundler |
| 6 |
| 7 import ( |
| 8 "fmt" |
| 9 "testing" |
| 10 |
| 11 "github.com/golang/protobuf/proto" |
| 12 . "github.com/smartystreets/goconvey/convey" |
| 13 ) |
| 14 |
| 15 // A proto.Message implementation with test fields. |
| 16 type testMessage struct { |
| 17 U64 uint64 `protobuf:"varint,1,opt,name=u64"` |
| 18 } |
| 19 |
| 20 func (t *testMessage) Reset() {} |
| 21 func (t *testMessage) String() string { return "" } |
| 22 func (t *testMessage) ProtoMessage() {} |
| 23 |
| 24 func TestFastSizerVarintLength(t *testing.T) { |
| 25 Convey(`A test message`, t, func() { |
| 26 for _, threshold := range []uint64{ |
| 27 0, |
| 28 0x80, |
| 29 0x4000, |
| 30 0x200000, |
| 31 0x100000000, |
| 32 0x800000000, |
| 33 0x40000000000, |
| 34 0x2000000000000, |
| 35 0x100000000000000, |
| 36 0x8000000000000000, |
| 37 } { |
| 38 |
| 39 for _, delta := range []int64{ |
| 40 -2, |
| 41 -1, |
| 42 0, |
| 43 1, |
| 44 2, |
| 45 } { |
| 46 // Add "delta" to "threshold" in a uint64-aware
manner. |
| 47 u64 := threshold |
| 48 if delta >= 0 { |
| 49 u64 += uint64(delta) |
| 50 } else { |
| 51 if u64 < uint64(-delta) { |
| 52 continue |
| 53 } |
| 54 u64 -= uint64(-delta) |
| 55 } |
| 56 |
| 57 expected := varintLength(u64) |
| 58 Convey(fmt.Sprintf(`Testing threshold 0x%x shoul
d encode to varint size %d`, u64, expected), func() { |
| 59 m := &testMessage{ |
| 60 U64: u64, |
| 61 } |
| 62 |
| 63 expectedSize := proto.Size(m) |
| 64 if u64 == 0 { |
| 65 // Proto3 doesn't encode default
values (0), so the expected size of |
| 66 // the number zero is zero. |
| 67 expectedSize = 0 |
| 68 } else { |
| 69 // Accommodate the tag ("1"). |
| 70 expectedSize -= varintLength(1) |
| 71 } |
| 72 So(expected, ShouldEqual, expectedSize) |
| 73 }) |
| 74 } |
| 75 } |
| 76 }) |
| 77 |
| 78 Convey(`Calculates protobuf size.`, t, func() { |
| 79 pbuf := &testMessage{ |
| 80 U64: 0x600dd065, |
| 81 } |
| 82 |
| 83 So(protoSize(pbuf), ShouldEqual, proto.Size(pbuf)) |
| 84 }) |
| 85 } |
OLD | NEW |