-
Notifications
You must be signed in to change notification settings - Fork 0
/
match_test.go
66 lines (60 loc) · 1.7 KB
/
match_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
package util
import (
"fmt"
"testing"
)
var testCases = []struct {
s string
subStr string
expected int
}{
{"", "", 0},
{"", "aaa", 0},
{"aaa", "", 0},
{"hello world", "world", 1},
{"hello world world", "world", 2},
{"hellohellohello", "hello", 3},
{"aaaabaaaacaaaad", "aaaa", 3},
{"abcdefg", "h", 0},
{"ababababab", "ab", 5},
}
// TestCountOccurrences tests the CountOccurrences function with various inputs.
func TestCountOccurrences(t *testing.T) {
for _, tc := range testCases {
t.Run(fmt.Sprintf("%s_in_%s", tc.subStr, tc.s), func(t *testing.T) {
result := CountOccurrences(tc.s, tc.subStr)
if result != tc.expected {
t.Errorf("CountOccurrences(%q, %q) = %d; want %d", tc.s, tc.subStr, result, tc.expected)
}
})
}
}
// func TestCountOccurrencesBuggy(t *testing.T) {
// for _, tc := range testCases {
// t.Run(fmt.Sprintf("%s_in_%s", tc.subStr, tc.s), func(t *testing.T) {
// result := CountOccurrencesBuggy(tc.s, tc.subStr)
// if result != tc.expected {
// t.Errorf("CountOccurrences(%q, %q) = %d; want %d", tc.s, tc.subStr, result, tc.expected)
// }
// })
// }
// }
// BenchmarkCountOccurrences benchmarks the CountOccurrences function with different data sizes.
func BenchmarkCountOccurrences(b *testing.B) {
for _, tc := range testCases {
b.Run(fmt.Sprintf("%s_in_%s", tc.subStr, tc.s), func(b *testing.B) {
for n := 0; n < b.N; n++ {
CountOccurrences(tc.s, tc.subStr)
}
})
}
}
// func BenchmarkCountOccurrencesBuggy(b *testing.B) {
// for _, tc := range testCases {
// b.Run(fmt.Sprintf("%s_in_%s", tc.subStr, tc.s), func(b *testing.B) {
// for n := 0; n < b.N; n++ {
// CountOccurrencesBuggy(tc.s, tc.subStr)
// }
// })
// }
// }