2020-02-21 20:43:16 +00:00
|
|
|
package varfmt
|
|
|
|
|
|
|
|
import (
|
2020-02-22 01:28:47 +00:00
|
|
|
"fmt"
|
2020-02-21 20:43:16 +00:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestFormat(t *testing.T) {
|
2024-06-05 17:36:34 +00:00
|
|
|
s := make([]interface{}, 1002)
|
2020-03-01 04:18:10 +00:00
|
|
|
s[10] = 1
|
2020-03-01 08:56:17 +00:00
|
|
|
s[100] = 2
|
|
|
|
s[1000] = 3
|
2020-02-29 16:37:29 +00:00
|
|
|
|
2020-02-21 20:43:16 +00:00
|
|
|
for _, tc := range []struct {
|
|
|
|
format string
|
2024-06-05 17:36:34 +00:00
|
|
|
args []interface{}
|
2020-02-21 20:43:16 +00:00
|
|
|
result string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
format: "{}",
|
2024-06-05 17:36:34 +00:00
|
|
|
args: []interface{}{0},
|
2020-02-21 20:43:16 +00:00
|
|
|
result: "0",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
format: "{0} {0}",
|
2024-06-05 17:36:34 +00:00
|
|
|
args: []interface{}{1},
|
2020-02-21 20:43:16 +00:00
|
|
|
result: "1 1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
format: "{1} {5}",
|
2024-06-05 17:36:34 +00:00
|
|
|
args: []interface{}{0, 1, 2, 3, 4, 5, 6},
|
2020-02-21 20:43:16 +00:00
|
|
|
result: "1 5",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
format: "{} {} {} {} {}",
|
2024-06-05 17:36:34 +00:00
|
|
|
args: []interface{}{0, 1, 2, 3, 4},
|
2020-02-21 20:43:16 +00:00
|
|
|
result: "0 1 2 3 4",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
format: "{} {0} {0} {0} {}",
|
2024-06-05 17:36:34 +00:00
|
|
|
args: []interface{}{0, 1, 2, 3, 4},
|
2020-02-21 20:43:16 +00:00
|
|
|
result: "0 0 0 0 4",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
format: "Hello, {2}",
|
2024-06-05 17:36:34 +00:00
|
|
|
args: []interface{}{0, 1, "World"},
|
2020-02-21 20:43:16 +00:00
|
|
|
result: "Hello, World",
|
|
|
|
},
|
2020-03-10 17:49:10 +00:00
|
|
|
{
|
|
|
|
format: "He{2}o",
|
2024-06-05 17:36:34 +00:00
|
|
|
args: []interface{}{0, 1, "ll"},
|
2020-03-10 17:49:10 +00:00
|
|
|
result: "Hello",
|
|
|
|
},
|
2020-02-28 04:53:09 +00:00
|
|
|
{
|
2020-03-01 04:18:10 +00:00
|
|
|
format: "{10}",
|
|
|
|
args: s[:11],
|
|
|
|
result: "1",
|
2020-02-29 16:37:29 +00:00
|
|
|
},
|
|
|
|
{
|
2020-03-01 04:18:10 +00:00
|
|
|
format: "{100}",
|
|
|
|
args: s[:101],
|
2020-03-01 08:56:17 +00:00
|
|
|
result: "2",
|
2020-02-29 16:37:29 +00:00
|
|
|
},
|
|
|
|
{
|
2020-03-01 04:18:10 +00:00
|
|
|
format: "{1000}",
|
|
|
|
args: s[:1001],
|
2020-03-01 08:56:17 +00:00
|
|
|
result: "3",
|
2020-02-28 04:53:09 +00:00
|
|
|
},
|
2020-02-21 20:43:16 +00:00
|
|
|
} {
|
|
|
|
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
|
2024-06-05 17:36:34 +00:00
|
|
|
args []interface{}
|
2020-02-21 20:43:16 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "small int",
|
|
|
|
format: "{}",
|
2024-06-05 17:36:34 +00:00
|
|
|
args: []interface{}{42},
|
2020-02-21 20:43:16 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "small string",
|
|
|
|
format: "{} {}",
|
2024-06-05 17:36:34 +00:00
|
|
|
args: []interface{}{"Hello", "World"},
|
2020-02-21 20:43:16 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "big",
|
|
|
|
format: strings.Repeat("{0}{1}", 1000),
|
2024-06-05 17:36:34 +00:00
|
|
|
args: []interface{}{42, 43},
|
2020-02-21 20:43:16 +00:00
|
|
|
},
|
|
|
|
} {
|
|
|
|
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
|
2024-06-05 17:36:34 +00:00
|
|
|
args []interface{}
|
2020-02-21 20:43:16 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "small",
|
|
|
|
format: "%d",
|
2024-06-05 17:36:34 +00:00
|
|
|
args: []interface{}{42},
|
2020-02-21 20:43:16 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "small string",
|
|
|
|
format: "%v %v",
|
2024-06-05 17:36:34 +00:00
|
|
|
args: []interface{}{"Hello", "World"},
|
2020-02-21 20:43:16 +00:00
|
|
|
}, {
|
|
|
|
name: "big",
|
|
|
|
format: strings.Repeat("%[0]v%[1]v", 1000),
|
2024-06-05 17:36:34 +00:00
|
|
|
args: []interface{}{42, 43},
|
2020-02-21 20:43:16 +00:00
|
|
|
},
|
|
|
|
} {
|
|
|
|
b.Run(tc.name, func(b *testing.B) {
|
|
|
|
b.ReportAllocs()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
2020-02-22 01:28:47 +00:00
|
|
|
_ = fmt.Sprintf(tc.format, tc.args...)
|
2020-02-21 20:43:16 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|