log: fix formatting of big.Int (#22679)

* log: fix formatting of big.Int

The implementation of formatLogfmtBigInt had two issues: it crashed when
the number was actually large enough to hit the big integer case, and
modified the big.Int while formatting it.

* log: don't call FormatLogfmtInt64 for int16

* log: separate from decimals back, not front

Co-authored-by: Péter Szilágyi <peterke@gmail.com>
This commit is contained in:
Felix Lange
2021-04-16 08:27:16 +02:00
committed by GitHub
parent 3cfd0fe7a8
commit fda93f643e
2 changed files with 54 additions and 24 deletions

View File

@@ -2,6 +2,7 @@ package log
import (
"math"
"math/big"
"math/rand"
"testing"
)
@@ -58,6 +59,25 @@ func TestPrettyUint64(t *testing.T) {
}
}
func TestPrettyBigInt(t *testing.T) {
tests := []struct {
int string
s string
}{
{"111222333444555678999", "111,222,333,444,555,678,999"},
{"-111222333444555678999", "-111,222,333,444,555,678,999"},
{"11122233344455567899900", "11,122,233,344,455,567,899,900"},
{"-11122233344455567899900", "-11,122,233,344,455,567,899,900"},
}
for _, tt := range tests {
v, _ := new(big.Int).SetString(tt.int, 10)
if have := formatLogfmtBigInt(v); have != tt.s {
t.Errorf("invalid output %s, want %s", have, tt.s)
}
}
}
var sink string
func BenchmarkPrettyInt64Logfmt(b *testing.B) {