wp-go/helper/strings/strings_test.go

159 lines
2.9 KiB
Go
Raw Permalink Normal View History

2023-01-21 11:31:23 +00:00
package strings
2023-01-13 04:31:35 +00:00
import (
2023-02-15 16:58:59 +00:00
"fmt"
"golang.org/x/exp/constraints"
2023-02-15 16:58:59 +00:00
"strings"
"testing"
)
2023-01-13 04:31:35 +00:00
func TestStrJoin(t *testing.T) {
type args struct {
s []string
}
tests := []struct {
name string
args args
wantStr string
}{
{name: "t1", args: args{s: []string{"a", "b", "c"}}, wantStr: "abc"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
2023-01-21 11:31:23 +00:00
if gotStr := Join(tt.args.s...); gotStr != tt.wantStr {
t.Errorf("Join() = %v, want %v", gotStr, tt.wantStr)
2023-01-13 04:31:35 +00:00
}
})
}
}
func TestToInteger(t *testing.T) {
type args[T constraints.Integer] struct {
s string
z T
}
type testCase[T constraints.Integer] struct {
name string
args args[T]
want T
}
tests := []testCase[int64]{
{
name: "t1",
args: args[int64]{
"10",
0,
},
want: int64(10),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := ToInteger[int64](tt.args.s, tt.args.z); got != tt.want {
t.Errorf("StrToInt() = %v, want %v", got, tt.want)
}
})
}
}
2023-02-15 16:58:59 +00:00
func TestBuilder_WriteString(t *testing.T) {
type fields struct {
Builder *strings.Builder
}
type args struct {
s []string
}
//s :=NewBuilder()
tests := []struct {
name string
fields fields
args args
wantCount int
wantErr bool
}{
{
name: "t1",
fields: fields{
Builder: &strings.Builder{},
},
args: args{s: []string{"11", "22", "不"}},
wantErr: false,
wantCount: 7,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
b := &Builder{
Builder: tt.fields.Builder,
}
2023-02-16 03:53:24 +00:00
gotCount := b.WriteString(tt.args.s...)
2023-02-15 16:58:59 +00:00
if gotCount != tt.wantCount {
t.Errorf("WriteString() gotCount = %v, want %v", gotCount, tt.wantCount)
}
fmt.Println(b.String())
})
}
}
2023-02-22 12:59:26 +00:00
func BenchmarkBuilder_SprintfXX(b *testing.B) {
s := NewBuilder()
for i := 0; i < b.N; i++ {
s.Sprintf("%s %s %s", "a", "b", "c")
_ = s.String()
}
}
func BenchmarkSPrintfXX(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = fmt.Sprintf("%s %s %s", "a", "b", "c")
}
}
func BenchmarkStrJoinXX(b *testing.B) {
s := strings.Builder{}
for i := 0; i < b.N; i++ {
s.WriteString("a ")
s.WriteString("b ")
s.WriteString("c ")
_ = s.String()
}
}
func BenchmarkBuilderJoinXX(b *testing.B) {
s := NewBuilder()
for i := 0; i < b.N; i++ {
s.WriteString("a ", "b ", "c")
_ = s.String()
}
}
2023-02-24 16:56:52 +00:00
func TestCutSpecialDuplicate(t *testing.T) {
type args struct {
s string
char string
}
tests := []struct {
name string
args args
want string
}{
{
name: "t1",
args: args{s: "1 2 3", char: " "},
want: "1 2 3",
},
{
name: "t2",
args: args{s: "1\t\t2\t\t3", char: "\t"},
want: "1\t2\t3",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := CutSpecialDuplicate(tt.args.s, tt.args.char); got != tt.want {
t.Errorf("CutDuplicate() = %v, want %v", got, tt.want)
}
})
}
}