lib/encoder: fix benchmarks

Some day in the past the Slash encode option was added to Onedrive
encoder so it began to encode slashes in file names rather then treat
them as path separators.
This patch adapts benchmark test cases accordingly.

Fixes #5659
This commit is contained in:
Ivan Andreev 2021-10-28 19:26:46 +03:00
parent f51a5eca2e
commit e78e73eae7
1 changed files with 80 additions and 23 deletions

View File

@ -266,54 +266,111 @@ const oneDrive = (Standard |
EncodeRightPeriod)
var benchTests = []struct {
in string
out string
in string
outOld string
outNew string
}{
{"", ""},
{"abc 123", "abc 123"},
{`\*<>?:|#%".~`, `.~`},
{`\*<>?:|#%".~/\*<>?:|#%".~`, `.~/.~`},
{" leading space", " leading space"},
{"~leading tilde", "leading tilde"},
{"trailing dot.", "trailing dot"},
{" leading space/ leading space/ leading space", " leading space/ leading space/ leading space"},
{"~leading tilde/~leading tilde/~leading tilde", "leading tilde/leading tilde/leading tilde"},
{"leading tilde/~leading tilde", "leading tilde/leading tilde"},
{"trailing dot./trailing dot./trailing dot.", "trailing dot/trailing dot/trailing dot"},
{
"",
"",
"",
},
{
"abc 123",
"abc 123",
"abc 123",
},
{
`\*<>?:|#%".~`,
`.~`,
`.~`,
},
{
`\*<>?:|#%".~/\*<>?:|#%".~`,
`.~/.~`,
`.~.~`,
},
{
" leading space",
" leading space",
" leading space",
},
{
"~leading tilde",
"leading tilde",
"leading tilde",
},
{
"trailing dot.",
"trailing dot",
"trailing dot",
},
{
" leading space/ leading space/ leading space",
" leading space/ leading space/ leading space",
" leading space leading space leading space",
},
{
"~leading tilde/~leading tilde/~leading tilde",
"leading tilde/leading tilde/leading tilde",
"leading tilde~leading tilde~leading tilde",
},
{
"leading tilde/~leading tilde",
"leading tilde/leading tilde",
"leading tilde~leading tilde",
},
{
"trailing dot./trailing dot./trailing dot.",
"trailing dot/trailing dot/trailing dot",
"trailing dot.trailing dot.trailing dot",
},
}
func benchReplace(b *testing.B, f func(string) string) {
func benchReplace(b *testing.B, f func(string) string, old bool) {
for range make([]struct{}, b.N) {
for _, test := range benchTests {
got := f(test.in)
if got != test.out {
b.Errorf("Encode(%q) want %q got %q", test.in, test.out, got)
out := test.outNew
if old {
out = test.outOld
}
if got != out {
b.Errorf("Encode(%q) want %q got %q", test.in, out, got)
}
}
}
}
func benchRestore(b *testing.B, f func(string) string) {
func benchRestore(b *testing.B, f func(string) string, old bool) {
for range make([]struct{}, b.N) {
for _, test := range benchTests {
got := f(test.out)
out := test.outNew
if old {
out = test.outOld
}
got := f(out)
if got != test.in {
b.Errorf("Decode(%q) want %q got %q", got, test.in, got)
b.Errorf("Decode(%q) want %q got %q", out, test.in, got)
}
}
}
}
func BenchmarkOneDriveReplaceNew(b *testing.B) {
benchReplace(b, oneDrive.Encode)
benchReplace(b, oneDrive.Encode, false)
}
func BenchmarkOneDriveReplaceOld(b *testing.B) {
benchReplace(b, replaceReservedChars)
benchReplace(b, replaceReservedChars, true)
}
func BenchmarkOneDriveRestoreNew(b *testing.B) {
benchRestore(b, oneDrive.Decode)
benchRestore(b, oneDrive.Decode, false)
}
func BenchmarkOneDriveRestoreOld(b *testing.B) {
benchRestore(b, restoreReservedChars)
benchRestore(b, restoreReservedChars, true)
}
var (