diff options
Diffstat (limited to 'src/pkg/strconv/itoa_test.go')
-rw-r--r-- | src/pkg/strconv/itoa_test.go | 152 |
1 files changed, 83 insertions, 69 deletions
diff --git a/src/pkg/strconv/itoa_test.go b/src/pkg/strconv/itoa_test.go index 8514b21e4..d4b09a5d8 100644 --- a/src/pkg/strconv/itoa_test.go +++ b/src/pkg/strconv/itoa_test.go @@ -5,13 +5,14 @@ package strconv_test import ( + "runtime" . "strconv" "testing" ) type itob64Test struct { in int64 - base uint + base int out string } @@ -60,73 +61,43 @@ var itob64tests = []itob64Test{ func TestItoa(t *testing.T) { for _, test := range itob64tests { - s := Itob64(test.in, test.base) + s := FormatInt(test.in, test.base) if s != test.out { - t.Errorf("Itob64(%v, %v) = %v want %v", + t.Errorf("FormatInt(%v, %v) = %v want %v", test.in, test.base, s, test.out) } - - if test.in >= 0 { - s := Uitob64(uint64(test.in), test.base) - if s != test.out { - t.Errorf("Uitob64(%v, %v) = %v want %v", - test.in, test.base, s, test.out) - } + x := AppendInt([]byte("abc"), test.in, test.base) + if string(x) != "abc"+test.out { + t.Errorf("AppendInt(%q, %v, %v) = %q want %v", + "abc", test.in, test.base, x, test.out) } - if int64(int(test.in)) == test.in { - s := Itob(int(test.in), test.base) + if test.in >= 0 { + s := FormatUint(uint64(test.in), test.base) if s != test.out { - t.Errorf("Itob(%v, %v) = %v want %v", + t.Errorf("FormatUint(%v, %v) = %v want %v", test.in, test.base, s, test.out) } - - if test.in >= 0 { - s := Uitob(uint(test.in), test.base) - if s != test.out { - t.Errorf("Uitob(%v, %v) = %v want %v", - test.in, test.base, s, test.out) - } + x := AppendUint(nil, uint64(test.in), test.base) + if string(x) != test.out { + t.Errorf("AppendUint(%q, %v, %v) = %q want %v", + "abc", uint64(test.in), test.base, x, test.out) } } - if test.base == 10 { - s := Itoa64(test.in) + if test.base == 10 && int64(int(test.in)) == test.in { + s := Itoa(int(test.in)) if s != test.out { - t.Errorf("Itoa64(%v) = %v want %v", + t.Errorf("Itoa(%v) = %v want %v", test.in, s, test.out) } - - if test.in >= 0 { - s := Uitob64(uint64(test.in), test.base) - if s != test.out { - t.Errorf("Uitob64(%v, %v) = %v want %v", - test.in, test.base, s, test.out) - } - } - - if int64(int(test.in)) == test.in { - s := Itoa(int(test.in)) - if s != test.out { - t.Errorf("Itoa(%v) = %v want %v", - test.in, s, test.out) - } - - if test.in >= 0 { - s := Uitoa(uint(test.in)) - if s != test.out { - t.Errorf("Uitoa(%v) = %v want %v", - test.in, s, test.out) - } - } - } } } } type uitob64Test struct { in uint64 - base uint + base int out string } @@ -141,34 +112,77 @@ var uitob64tests = []uitob64Test{ func TestUitoa(t *testing.T) { for _, test := range uitob64tests { - s := Uitob64(test.in, test.base) + s := FormatUint(test.in, test.base) if s != test.out { - t.Errorf("Uitob64(%v, %v) = %v want %v", + t.Errorf("FormatUint(%v, %v) = %v want %v", test.in, test.base, s, test.out) } + x := AppendUint([]byte("abc"), test.in, test.base) + if string(x) != "abc"+test.out { + t.Errorf("AppendUint(%q, %v, %v) = %q want %v", + "abc", test.in, test.base, x, test.out) + } - if uint64(uint(test.in)) == test.in { - s := Uitob(uint(test.in), test.base) - if s != test.out { - t.Errorf("Uitob(%v, %v) = %v want %v", - test.in, test.base, s, test.out) - } + } +} + +func numAllocations(f func()) int { + runtime.UpdateMemStats() + n0 := runtime.MemStats.Mallocs + f() + runtime.UpdateMemStats() + return int(runtime.MemStats.Mallocs - n0) +} + +var globalBuf [64]byte + +func TestAppendUintDoesntAllocate(t *testing.T) { + n := numAllocations(func() { + var buf [64]byte + AppendInt(buf[:0], 123, 10) + }) + want := 1 // TODO(bradfitz): this might be 0, once escape analysis is better + if n != want { + t.Errorf("with local buffer, did %d allocations, want %d", n, want) + } + n = numAllocations(func() { + AppendInt(globalBuf[:0], 123, 10) + }) + if n != 0 { + t.Errorf("with reused buffer, did %d allocations, want 0", n) + } +} + +func BenchmarkFormatInt(b *testing.B) { + for i := 0; i < b.N; i++ { + for _, test := range itob64tests { + FormatInt(test.in, test.base) } + } +} - if test.base == 10 { - s := Uitoa64(test.in) - if s != test.out { - t.Errorf("Uitoa64(%v) = %v want %v", - test.in, s, test.out) - } +func BenchmarkAppendInt(b *testing.B) { + dst := make([]byte, 0, 30) + for i := 0; i < b.N; i++ { + for _, test := range itob64tests { + AppendInt(dst, test.in, test.base) + } + } +} - if uint64(uint(test.in)) == test.in { - s := Uitoa(uint(test.in)) - if s != test.out { - t.Errorf("Uitoa(%v) = %v want %v", - test.in, s, test.out) - } - } +func BenchmarkFormatUint(b *testing.B) { + for i := 0; i < b.N; i++ { + for _, test := range uitob64tests { + FormatUint(test.in, test.base) + } + } +} + +func BenchmarkAppendUint(b *testing.B) { + dst := make([]byte, 0, 30) + for i := 0; i < b.N; i++ { + for _, test := range uitob64tests { + AppendUint(dst, test.in, test.base) } } } |