-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmarks_test.go
386 lines (348 loc) · 7.24 KB
/
benchmarks_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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
package is
import (
"testing"
)
// Benchmark variables to prevent compiler optimizations
var (
resultBool bool
resultString string
resultInt int
resultFloat float64
)
// Account validation benchmarks
func BenchmarkNickname(b *testing.B) {
cases := []struct {
name string
input string
strict bool
expected bool
}{
{"ValidSimple", "User123", false, true},
{"ValidUnicode", "用户123", false, true},
{"ValidStrict", "User123", true, true},
{"Invalid", "User@123", false, false},
{"InvalidStrict", "用户123", true, false},
}
for _, tc := range cases {
b.Run(tc.name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
resultBool = Nickname(tc.input, tc.strict)
}
})
}
}
// Bank Card validation benchmarks
func BenchmarkBankCard(b *testing.B) {
cases := []struct {
name string
input string
expected bool
}{
{"ValidVisa", "4111111111111111", true},
{"ValidMasterCard", "5555555555554444", true},
{"ValidAmEx", "378282246310005", true},
{"Invalid", "1234567812345678", false},
}
for _, tc := range cases {
b.Run(tc.name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
resultBool = BankCard(tc.input)
}
})
}
}
// IBAN validation benchmarks
func BenchmarkIBAN(b *testing.B) {
cases := []struct {
name string
input string
strict bool
expected bool
}{
{"ValidGB", "GB82 WEST 1234 5698 7654 32", false, true},
{"ValidDE", "DE89370400440532013000", true, true},
{"Invalid", "Invalid IBAN", false, false},
}
for _, tc := range cases {
b.Run(tc.name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
resultBool = IBAN(tc.input, tc.strict)
}
})
}
}
// Code validation benchmarks
func BenchmarkVariableName(b *testing.B) {
cases := []struct {
name string
input string
strict bool
expected bool
}{
{"ValidSimple", "myVariable", false, true},
{"ValidStrict", "myVariable", true, true},
{"Invalid", "1variable", false, false},
}
for _, tc := range cases {
b.Run(tc.name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
resultBool = VariableName(tc.input, tc.strict)
}
})
}
}
// Email validation benchmarks
func BenchmarkEmail(b *testing.B) {
cases := []struct {
name string
input string
expected bool
}{
{"Valid", "[email protected]", true},
{"ValidComplex", "[email protected]", true},
{"Invalid", "not-an-email", false},
{"InvalidFormat", "test@", false},
}
for _, tc := range cases {
b.Run(tc.name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
resultBool = Email(tc.input)
}
})
}
}
// Geographic validation benchmarks
func BenchmarkLatitude(b *testing.B) {
cases := []struct {
name string
input float64
expected bool
}{
{"ValidPositive", 45.0, true},
{"ValidNegative", -45.0, true},
{"Invalid", 91.0, false},
}
for _, tc := range cases {
b.Run(tc.name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
resultBool = Latitude(tc.input)
}
})
}
}
func BenchmarkLongitude(b *testing.B) {
cases := []struct {
name string
input float64
expected bool
}{
{"ValidPositive", 120.0, true},
{"ValidNegative", -120.0, true},
{"Invalid", 181.0, false},
}
for _, tc := range cases {
b.Run(tc.name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
resultBool = Longitude(tc.input)
}
})
}
}
// Hash validation benchmarks
func BenchmarkMD5(b *testing.B) {
cases := []struct {
name string
input string
expected bool
}{
{"Valid", "d41d8cd98f00b204e9800998ecf8427e", true},
{"Invalid", "not-an-md5-hash", false},
}
for _, tc := range cases {
b.Run(tc.name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
resultBool = MD5(tc.input)
}
})
}
}
// Network validation benchmarks
func BenchmarkIPv4(b *testing.B) {
cases := []struct {
name string
input string
expected bool
}{
{"Valid", "192.168.0.1", true},
{"ValidLocalhost", "127.0.0.1", true},
{"Invalid", "256.256.256.256", false},
}
for _, tc := range cases {
b.Run(tc.name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
resultBool = IPv4(tc.input)
}
})
}
}
func BenchmarkIPv6(b *testing.B) {
cases := []struct {
name string
input string
expected bool
}{
{"Valid", "2001:0db8:85a3:0000:0000:8a2e:0370:7334", true},
{"ValidShort", "::1", true},
{"Invalid", "not-an-ipv6", false},
}
for _, tc := range cases {
b.Run(tc.name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
resultBool = IPv6(tc.input)
}
})
}
}
// Number validation benchmarks
func BenchmarkEven(b *testing.B) {
cases := []struct {
name string
input int
expected bool
}{
{"ValidEven", 2, true},
{"InvalidOdd", 3, false},
}
for _, tc := range cases {
b.Run(tc.name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
resultBool = Even(tc.input)
}
})
}
}
func BenchmarkOdd(b *testing.B) {
cases := []struct {
name string
input int
expected bool
}{
{"ValidOdd", 3, true},
{"InvalidEven", 2, false},
}
for _, tc := range cases {
b.Run(tc.name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
resultBool = Odd(tc.input)
}
})
}
}
// Phone validation benchmarks
func BenchmarkE164(b *testing.B) {
cases := []struct {
name string
input string
expected bool
}{
{"Valid", "+12345678901", true},
{"Invalid", "12345678901", false},
{"InvalidFormat", "+1234abcd", false},
}
for _, tc := range cases {
b.Run(tc.name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
resultBool = E164(tc.input)
}
})
}
}
func BenchmarkPhone(b *testing.B) {
cases := []struct {
name string
input string
expected bool
}{
{"Valid", "+380961234567", true},
{"ValidWithSpaces", "+380 96 123 45 67", true},
{"Invalid", "1234567890", false},
}
for _, tc := range cases {
b.Run(tc.name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
resultBool = Phone(tc.input)
}
})
}
}
// String validation benchmarks
func BenchmarkDigit(b *testing.B) {
cases := []struct {
name string
input string
expected bool
}{
{"Valid", "12345", true},
{"Invalid", "12345a", false},
}
for _, tc := range cases {
b.Run(tc.name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
resultBool = Digit(tc.input)
}
})
}
}
func BenchmarkNumeric(b *testing.B) {
cases := []struct {
name string
input string
expected bool
}{
{"ValidInteger", "12345", true},
{"ValidDecimal", "123.45", true},
{"ValidNegative", "-123.45", true},
{"Invalid", "12345a", false},
}
for _, tc := range cases {
b.Run(tc.name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
resultBool = Numeric(tc.input)
}
})
}
}
func BenchmarkAlpha(b *testing.B) {
cases := []struct {
name string
input string
expected bool
}{
{"Valid", "abcdef", true},
{"Invalid", "abc123", false},
}
for _, tc := range cases {
b.Run(tc.name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
resultBool = Alpha(tc.input)
}
})
}
}
func BenchmarkAlnum(b *testing.B) {
cases := []struct {
name string
input string
expected bool
}{
{"Valid", "abc123", true},
{"Invalid", "abc123!", false},
}
for _, tc := range cases {
b.Run(tc.name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
resultBool = Alnum(tc.input)
}
})
}
}