Skip to content

Commit

Permalink
path: add test and benchmark for long CleanPath inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
julienschmidt committed Jan 6, 2020
1 parent 7b49e86 commit 8e4b52b
Showing 1 changed file with 54 additions and 2 deletions.
56 changes: 54 additions & 2 deletions path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@
package httprouter

import (
"strings"
"testing"
)

var cleanTests = []struct {
type cleanPathTest struct {
path, result string
}{
}

var cleanTests = []cleanPathTest{
// Already clean
{"/", "/"},
{"/abc", "/abc"},
Expand Down Expand Up @@ -88,6 +91,55 @@ func TestPathCleanMallocs(t *testing.T) {
}

func BenchmarkPathClean(b *testing.B) {
b.ReportAllocs()

for i := 0; i < b.N; i++ {
for _, test := range cleanTests {
CleanPath(test.path)
}
}
}

func genLongPaths() (testPaths []cleanPathTest) {
for i := 1; i <= 1234; i++ {
ss := strings.Repeat("a", i)

correctPath := "/" + ss
testPaths = append(testPaths, cleanPathTest{
path: correctPath,
result: correctPath,
}, cleanPathTest{
path: ss,
result: correctPath,
}, cleanPathTest{
path: "//" + ss,
result: correctPath,
}, cleanPathTest{
path: "/" + ss + "/b/..",
result: correctPath,
})
}
return
}

func TestPathCleanLong(t *testing.T) {
cleanTests := genLongPaths()

for _, test := range cleanTests {
if s := CleanPath(test.path); s != test.result {
t.Errorf("CleanPath(%q) = %q, want %q", test.path, s, test.result)
}
if s := CleanPath(test.result); s != test.result {
t.Errorf("CleanPath(%q) = %q, want %q", test.result, s, test.result)
}
}
}

func BenchmarkPathCleanLong(b *testing.B) {
cleanTests := genLongPaths()
b.ResetTimer()
b.ReportAllocs()

for i := 0; i < b.N; i++ {
for _, test := range cleanTests {
CleanPath(test.path)
Expand Down

0 comments on commit 8e4b52b

Please sign in to comment.