lib/fs: Optimize TempName + some cosmetic changes (#7911)

This commit is contained in:
greatroar
2021-08-29 10:47:53 +02:00
committed by GitHub
parent 2816780b52
commit ed98039aa5
2 changed files with 30 additions and 22 deletions
+16 -5
View File
@@ -7,20 +7,31 @@
package fs
import (
"path/filepath"
"strings"
"testing"
)
func TestLongTempFilename(t *testing.T) {
filename := ""
for i := 0; i < 300; i++ {
filename += "l"
}
filename := strings.Repeat("l", 300)
tFile := TempName(filename)
if len(tFile) < 10 || len(tFile) > 200 {
if len(tFile) < 10 || len(tFile) > 160 {
t.Fatal("Invalid long filename")
}
if !strings.HasSuffix(TempName("short"), "short.tmp") {
t.Fatal("Invalid short filename", TempName("short"))
}
}
func benchmarkTempName(b *testing.B, filename string) {
filename = filepath.Join("/Users/marieantoinette", filename)
b.ReportAllocs()
for i := 0; i < b.N; i++ {
TempName(filename)
}
}
func BenchmarkTempNameShort(b *testing.B) { benchmarkTempName(b, "somefile.txt") }
func BenchmarkTempNameLong(b *testing.B) { benchmarkTempName(b, strings.Repeat("a", 270)) }