tempname_test.go 991 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. // Copyright (C) 2015 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package fs
  7. import (
  8. "path/filepath"
  9. "strings"
  10. "testing"
  11. )
  12. func TestLongTempFilename(t *testing.T) {
  13. filename := strings.Repeat("l", 300)
  14. tFile := TempName(filename)
  15. if len(tFile) < 10 || len(tFile) > 160 {
  16. t.Fatal("Invalid long filename")
  17. }
  18. if !strings.HasSuffix(TempName("short"), "short.tmp") {
  19. t.Fatal("Invalid short filename", TempName("short"))
  20. }
  21. }
  22. func benchmarkTempName(b *testing.B, filename string) {
  23. filename = filepath.Join("/Users/marieantoinette", filename)
  24. b.ReportAllocs()
  25. for i := 0; i < b.N; i++ {
  26. TempName(filename)
  27. }
  28. }
  29. func BenchmarkTempNameShort(b *testing.B) { benchmarkTempName(b, "somefile.txt") }
  30. func BenchmarkTempNameLong(b *testing.B) { benchmarkTempName(b, strings.Repeat("a", 270)) }