-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into 2275-upd-go
- Loading branch information
Showing
4 changed files
with
286 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package querylog | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestContainsFold(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
inS string | ||
inSubstr string | ||
want bool | ||
}{{ | ||
name: "empty", | ||
inS: "", | ||
inSubstr: "", | ||
want: true, | ||
}, { | ||
name: "shorter", | ||
inS: "a", | ||
inSubstr: "abc", | ||
want: false, | ||
}, { | ||
name: "same_len_true", | ||
inS: "abc", | ||
inSubstr: "abc", | ||
want: true, | ||
}, { | ||
name: "same_len_true_fold", | ||
inS: "abc", | ||
inSubstr: "aBc", | ||
want: true, | ||
}, { | ||
name: "same_len_false", | ||
inS: "abc", | ||
inSubstr: "def", | ||
want: false, | ||
}, { | ||
name: "longer_true", | ||
inS: "abcdedef", | ||
inSubstr: "def", | ||
want: true, | ||
}, { | ||
name: "longer_false", | ||
inS: "abcded", | ||
inSubstr: "ghi", | ||
want: false, | ||
}, { | ||
name: "longer_true_fold", | ||
inS: "abcdedef", | ||
inSubstr: "dEf", | ||
want: true, | ||
}, { | ||
name: "longer_false_fold", | ||
inS: "abcded", | ||
inSubstr: "gHi", | ||
want: false, | ||
}, { | ||
name: "longer_true_cyr_fold", | ||
inS: "абвгдедеё", | ||
inSubstr: "дЕЁ", | ||
want: true, | ||
}, { | ||
name: "longer_false_cyr_fold", | ||
inS: "абвгдедеё", | ||
inSubstr: "жЗИ", | ||
want: false, | ||
}, { | ||
name: "no_letters_true", | ||
inS: "1.2.3.4", | ||
inSubstr: "2.3.4", | ||
want: true, | ||
}, { | ||
name: "no_letters_false", | ||
inS: "1.2.3.4", | ||
inSubstr: "2.3.5", | ||
want: false, | ||
}} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
if tc.want { | ||
assert.True(t, containsFold(tc.inS, tc.inSubstr)) | ||
} else { | ||
assert.False(t, containsFold(tc.inS, tc.inSubstr)) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
var sink bool | ||
|
||
func BenchmarkContainsFold(b *testing.B) { | ||
const s = "aaahBbBhccchDDDeEehFfFhGGGhHhh" | ||
const substr = "HHH" | ||
|
||
// Compare our implementation of containsFold against a stupid solution | ||
// of calling strings.ToLower and strings.Contains. | ||
b.Run("containsfold", func(b *testing.B) { | ||
b.ReportAllocs() | ||
|
||
for i := 0; i < b.N; i++ { | ||
sink = containsFold(s, substr) | ||
} | ||
|
||
assert.True(b, sink) | ||
}) | ||
|
||
b.Run("tolower_contains", func(b *testing.B) { | ||
b.ReportAllocs() | ||
|
||
for i := 0; i < b.N; i++ { | ||
sink = strings.Contains(strings.ToLower(s), strings.ToLower(substr)) | ||
} | ||
|
||
assert.True(b, sink) | ||
}) | ||
} |