-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathstr_test.go
201 lines (172 loc) · 5.87 KB
/
str_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package pgo_test
import (
"math/rand"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/arthurkushman/pgo"
)
// TestStrReplace is a unit test function that tests the StrReplace function.
//
// It replaces "fox" with "cat" and "dog" with "fox" in the given subject string.
// The function asserts that the resulting string is equal to the expected result.
//
// Parameters:
//
// t: *testing.T - the testing object used for assertion.
//
// Return type: None.
func TestStrReplace(t *testing.T) {
subject := "The quick brown fox jumped over the lazy dog"
subject, err := pgo.StrReplace("fox", "cat", subject)
assert.NoError(t, err)
subject, err = pgo.StrReplace("dog", "fox", subject)
assert.NoError(t, err)
result := "The quick brown cat jumped over the lazy fox"
assert.Equalf(t, subject, result, "want %s, got %s", subject, result)
}
// TestStrIReplace tests the StrIReplace function.
//
// This function takes input strings and replaces specified substrings case-insensitively.
func TestStrIReplace(t *testing.T) {
subject := "The quick brown fox jumped over the lazy dog"
subject, err := pgo.StrIReplace("Fox", "cat", subject)
assert.NoError(t, err)
subject, err = pgo.StrIReplace([]string{"DOG", "QuiCK"}, []string{"fox", "slow"}, subject)
assert.NoError(t, err)
result := "The slow brown cat jumped over the lazy fox"
assert.Equalf(t, subject, result, "want %s, got %s", subject, result)
}
// TestStrReplaceCount description of the Go function.
//
// It tests the StrReplace function by replacing a specified number of occurrences of a substring in a given string.
// Error is returned if the replacement fails.
func TestStrReplaceCount(t *testing.T) {
subject := "The quick brown fox jumped over the lazy fox or dog"
str, err := pgo.StrReplace("fox", "cat", subject, 1)
assert.NoError(t, err)
result := "The quick brown cat jumped over the lazy fox or dog"
assert.Equalf(t, str, result, "want %s, got %s", result, str)
}
// TestStrReplaceArray description of the Go function.
//
// It tests the StrReplace function by replacing occurrences of strings in the subject with new strings.
func TestStrReplaceArray(t *testing.T) {
subject := "The quick brown fox jumped over the lazy dog"
str, err := pgo.StrReplace([]string{"fox", "dog"}, []string{"cat", "elephant"}, subject)
assert.NoError(t, err)
result := "The quick brown cat jumped over the lazy elephant"
assert.Equalf(t, str, result, "want %s, got %s", result, str)
}
// TestStrReplaceErrs tests the StrReplace function.
//
// It checks the behavior of StrReplace when replacing substrings in a given subject string.
func TestStrReplaceErrs(t *testing.T) {
subject := "The quick brown fox jumped over the lazy dog"
str, err := pgo.StrReplace([]string{"fox", "dog"}, "", subject)
assert.Error(t, err)
assert.Equalf(t, str, subject, "want %s, got %s", subject, str)
}
// TestHTTPBuildQuery is a Go test function for testing the HTTPBuildQuery function.
//
// It does not take any parameters and does not return any values.
func TestHTTPBuildQuery(t *testing.T) {
queryStr := pgo.HTTPBuildQuery(map[string]interface{}{
"foo": "bar",
"bar": "baz",
"s": []string{"1", "foo", "2", "bar", "3", "baz"},
"num": 123,
"bigNum": int64(1238873737737737373),
"amount": 623.937,
"isActive": true,
})
want := "amount=623.937&bar=baz&bigNum=1238873737737737373&foo=bar&isActive=true&num=123&s=1&s=foo&s=2&s=bar&s=3&s=baz"
assert.Equal(t, queryStr, want, "want %s, got %s", queryStr, want)
queryStr2 := pgo.HTTPBuildQuery(map[string]interface{}{})
assert.Empty(t, queryStr2, "built str from an empty map must be empty")
}
// TestConcatFast is a Go function to test the ConcatFast function.
//
// It takes a slice of structs with name, s (a slice of strings), and result fields, and it runs tests for the ConcatFast function.
func TestConcatFast(t *testing.T) {
tests := []struct {
name string
s []string
result string
}{
{
name: "concat 3 strings",
s: []string{"foo", "bar", "bazzz"},
result: "foobarbazzz",
},
{
name: "concat 0 strings",
s: []string{},
result: "",
},
{
name: "concat random strings",
s: []string{"impEdfCJyek3jn5kj3nkj35nkj35nkj3nkj3n5kjn3kjn35kjn5", "IpDtUOSwMy", "sMIaQYdeON", "TZTwRNgZfx",
"kybtlfzfJa", "UJQJXhknLe", "GKDmxroeFv",
"ifguLESWvm333334241341231242414k12m4k1m24k1m2k4m1k24n1l2n41ln41lk2n4k12"},
result: "impEdfCJyek3jn5kj3nkj35nkj35nkj3nkj3n5kjn3kjn35kjn5IpDtUOSwMysMIaQYdeONTZTwRNgZfxkybtlfzfJaUJQJXhknLeGKDmxroeFvifguLESWvm333334241341231242414k12m4k1m24k1m2k4m1k24n1l2n41ln41lk2n4k12",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
resStr := pgo.ConcatFast(tc.s...)
require.Equal(t, tc.result, resStr)
})
}
}
func BenchmarkConcatFast(b *testing.B) {
s := generateRandomSliceOfStrings()
for i := 0; i < b.N; i++ {
pgo.ConcatFast(s...)
}
}
func BenchmarkConcatFast2(b *testing.B) {
s := generateRandomSliceOfStrings()
for i := 0; i < b.N; i++ {
stringBuilder(s...)
}
}
// generateRandomSliceOfStrings generates a slice of 15 random strings.
//
// No parameters.
// Returns a slice of strings.
func generateRandomSliceOfStrings() []string {
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
r := rand.New(rand.NewSource(time.Now().UnixNano()))
s := make([]string, 15)
for i := range s {
bt := make([]byte, 10)
for j := range bt {
bt[j] = letterBytes[r.Intn(len(letterBytes))]
}
s[i] = string(bt)
}
return s
}
// stringBuilder concatenates multiple strings into a single string.
//
// It takes in a variadic parameter `s` of type string.
// Returns a string.
func stringBuilder(s ...string) string {
l := len(s)
if l == 0 {
return ""
}
b := strings.Builder{}
n := 0
for i := 0; i < l; i++ {
n += len(s[i])
}
b.Grow(n)
for i := 0; i < l; i++ {
b.WriteString(s[i])
}
return b.String()
}