shad-go/varfmt/fmt_test.go
Vladimir Kuznetsov 6b51d2e906 Update fmt_test.go
- add test with two-digits number index
2020-03-03 15:42:05 +03:00

119 lines
2.1 KiB
Go

package varfmt
import (
"fmt"
"strings"
"testing"
"github.com/stretchr/testify/require"
)
func TestFormat(t *testing.T) {
for _, tc := range []struct {
format string
args []interface{}
result string
}{
{
format: "{}",
args: []interface{}{0},
result: "0",
},
{
format: "{0} {0}",
args: []interface{}{1},
result: "1 1",
},
{
format: "{1} {5}",
args: []interface{}{0, 1, 2, 3, 4, 5, 6},
result: "1 5",
},
{
format: "{} {} {} {} {}",
args: []interface{}{0, 1, 2, 3, 4},
result: "0 1 2 3 4",
},
{
format: "{} {0} {0} {0} {}",
args: []interface{}{0, 1, 2, 3, 4},
result: "0 0 0 0 4",
},
{
format: "Hello, {2}",
args: []interface{}{0, 1, "World"},
result: "Hello, World",
},
{
format: "{10}",
args: []interface{}{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
result: "10",
},
} {
t.Run(tc.result, func(t *testing.T) {
require.Equal(t, tc.result, Sprintf(tc.format, tc.args...))
})
}
}
func BenchmarkFormat(b *testing.B) {
for _, tc := range []struct {
name string
format string
args []interface{}
}{
{
name: "small int",
format: "{}",
args: []interface{}{42},
},
{
name: "small string",
format: "{} {}",
args: []interface{}{"Hello", "World"},
},
{
name: "big",
format: strings.Repeat("{0}{1}", 1000),
args: []interface{}{42, 43},
},
} {
b.Run(tc.name, func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = Sprintf(tc.format, tc.args...)
}
})
}
}
func BenchmarkSprintf(b *testing.B) {
for _, tc := range []struct {
name string
format string
args []interface{}
}{
{
name: "small",
format: "%d",
args: []interface{}{42},
},
{
name: "small string",
format: "%v %v",
args: []interface{}{"Hello", "World"},
}, {
name: "big",
format: strings.Repeat("%[0]v%[1]v", 1000),
args: []interface{}{42, 43},
},
} {
b.Run(tc.name, func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = fmt.Sprintf(tc.format, tc.args...)
}
})
}
}