size2_test.go 847 B

12345678910111213141516171819202122232425262728293031323334353637
  1. // Copyright 2012 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. package proto
  5. import (
  6. "math"
  7. "testing"
  8. )
  9. // This is a separate file and package from size_test.go because that one uses
  10. // generated messages and thus may not be in package proto without having a circular
  11. // dependency, whereas this file tests unexported details of size.go.
  12. func TestVarintSize(t *testing.T) {
  13. // Check the edge cases carefully.
  14. testCases := []struct {
  15. n uint64
  16. size int
  17. }{
  18. {0, 1},
  19. {1, 1},
  20. {127, 1},
  21. {128, 2},
  22. {16383, 2},
  23. {16384, 3},
  24. {math.MaxInt64, 9},
  25. {math.MaxInt64 + 1, 10},
  26. }
  27. for _, tc := range testCases {
  28. size := SizeVarint(tc.n)
  29. if size != tc.size {
  30. t.Errorf("sizeVarint(%d) = %d, want %d", tc.n, size, tc.size)
  31. }
  32. }
  33. }