github.com/carter-ya/go-ethereum@v0.0.0-20230628080049-d2309be3983b/log/format_test.go (about) 1 package log 2 3 import ( 4 "math" 5 "math/big" 6 "math/rand" 7 "testing" 8 ) 9 10 func TestPrettyInt64(t *testing.T) { 11 tests := []struct { 12 n int64 13 s string 14 }{ 15 {0, "0"}, 16 {10, "10"}, 17 {-10, "-10"}, 18 {100, "100"}, 19 {-100, "-100"}, 20 {1000, "1000"}, 21 {-1000, "-1000"}, 22 {10000, "10000"}, 23 {-10000, "-10000"}, 24 {99999, "99999"}, 25 {-99999, "-99999"}, 26 {100000, "100,000"}, 27 {-100000, "-100,000"}, 28 {1000000, "1,000,000"}, 29 {-1000000, "-1,000,000"}, 30 {math.MaxInt64, "9,223,372,036,854,775,807"}, 31 {math.MinInt64, "-9,223,372,036,854,775,808"}, 32 } 33 for i, tt := range tests { 34 if have := FormatLogfmtInt64(tt.n); have != tt.s { 35 t.Errorf("test %d: format mismatch: have %s, want %s", i, have, tt.s) 36 } 37 } 38 } 39 40 func TestPrettyUint64(t *testing.T) { 41 tests := []struct { 42 n uint64 43 s string 44 }{ 45 {0, "0"}, 46 {10, "10"}, 47 {100, "100"}, 48 {1000, "1000"}, 49 {10000, "10000"}, 50 {99999, "99999"}, 51 {100000, "100,000"}, 52 {1000000, "1,000,000"}, 53 {math.MaxUint64, "18,446,744,073,709,551,615"}, 54 } 55 for i, tt := range tests { 56 if have := FormatLogfmtUint64(tt.n); have != tt.s { 57 t.Errorf("test %d: format mismatch: have %s, want %s", i, have, tt.s) 58 } 59 } 60 } 61 62 func TestPrettyBigInt(t *testing.T) { 63 tests := []struct { 64 int string 65 s string 66 }{ 67 {"111222333444555678999", "111,222,333,444,555,678,999"}, 68 {"-111222333444555678999", "-111,222,333,444,555,678,999"}, 69 {"11122233344455567899900", "11,122,233,344,455,567,899,900"}, 70 {"-11122233344455567899900", "-11,122,233,344,455,567,899,900"}, 71 } 72 73 for _, tt := range tests { 74 v, _ := new(big.Int).SetString(tt.int, 10) 75 if have := formatLogfmtBigInt(v); have != tt.s { 76 t.Errorf("invalid output %s, want %s", have, tt.s) 77 } 78 } 79 } 80 81 var sink string 82 83 func BenchmarkPrettyInt64Logfmt(b *testing.B) { 84 b.ReportAllocs() 85 for i := 0; i < b.N; i++ { 86 sink = FormatLogfmtInt64(rand.Int63()) 87 } 88 } 89 90 func BenchmarkPrettyUint64Logfmt(b *testing.B) { 91 b.ReportAllocs() 92 for i := 0; i < b.N; i++ { 93 sink = FormatLogfmtUint64(rand.Uint64()) 94 } 95 }