format_test.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package log
  2. import (
  3. "math"
  4. "math/rand"
  5. "testing"
  6. )
  7. func TestPrettyInt64(t *testing.T) {
  8. tests := []struct {
  9. n int64
  10. s string
  11. }{
  12. {0, "0"},
  13. {10, "10"},
  14. {-10, "-10"},
  15. {100, "100"},
  16. {-100, "-100"},
  17. {1000, "1000"},
  18. {-1000, "-1000"},
  19. {10000, "10000"},
  20. {-10000, "-10000"},
  21. {99999, "99999"},
  22. {-99999, "-99999"},
  23. {100000, "100,000"},
  24. {-100000, "-100,000"},
  25. {1000000, "1,000,000"},
  26. {-1000000, "-1,000,000"},
  27. {math.MaxInt64, "9,223,372,036,854,775,807"},
  28. {math.MinInt64, "-9,223,372,036,854,775,808"},
  29. }
  30. for i, tt := range tests {
  31. if have := FormatLogfmtInt64(tt.n); have != tt.s {
  32. t.Errorf("test %d: format mismatch: have %s, want %s", i, have, tt.s)
  33. }
  34. }
  35. }
  36. func TestPrettyUint64(t *testing.T) {
  37. tests := []struct {
  38. n uint64
  39. s string
  40. }{
  41. {0, "0"},
  42. {10, "10"},
  43. {100, "100"},
  44. {1000, "1000"},
  45. {10000, "10000"},
  46. {99999, "99999"},
  47. {100000, "100,000"},
  48. {1000000, "1,000,000"},
  49. {math.MaxUint64, "18,446,744,073,709,551,615"},
  50. }
  51. for i, tt := range tests {
  52. if have := FormatLogfmtUint64(tt.n); have != tt.s {
  53. t.Errorf("test %d: format mismatch: have %s, want %s", i, have, tt.s)
  54. }
  55. }
  56. }
  57. var sink string
  58. func BenchmarkPrettyInt64Logfmt(b *testing.B) {
  59. b.ReportAllocs()
  60. for i := 0; i < b.N; i++ {
  61. sink = FormatLogfmtInt64(rand.Int63())
  62. }
  63. }
  64. func BenchmarkPrettyUint64Logfmt(b *testing.B) {
  65. b.ReportAllocs()
  66. for i := 0; i < b.N; i++ {
  67. sink = FormatLogfmtUint64(rand.Uint64())
  68. }
  69. }