OLD | NEW |
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package path | 5 package path |
6 | 6 |
7 import ( | 7 import ( |
8 "runtime" | |
9 "testing" | 8 "testing" |
10 ) | 9 ) |
11 | 10 |
12 type PathTest struct { | 11 type PathTest struct { |
13 path, result string | 12 path, result string |
14 } | 13 } |
15 | 14 |
16 var cleantests = []PathTest{ | 15 var cleantests = []PathTest{ |
17 // Already clean | 16 // Already clean |
18 {"", "."}, | 17 {"", "."}, |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 {"/abc/def/../../..", "/"}, | 56 {"/abc/def/../../..", "/"}, |
58 {"abc/def/../../../ghi/jkl/../../../mno", "../../mno"}, | 57 {"abc/def/../../../ghi/jkl/../../../mno", "../../mno"}, |
59 | 58 |
60 // Combinations | 59 // Combinations |
61 {"abc/./../def", "def"}, | 60 {"abc/./../def", "def"}, |
62 {"abc//./../def", "def"}, | 61 {"abc//./../def", "def"}, |
63 {"abc/../../././../def", "../../def"}, | 62 {"abc/../../././../def", "../../def"}, |
64 } | 63 } |
65 | 64 |
66 func TestClean(t *testing.T) { | 65 func TestClean(t *testing.T) { |
67 defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(1)) | |
68 for _, test := range cleantests { | 66 for _, test := range cleantests { |
69 if s := Clean(test.path); s != test.result { | 67 if s := Clean(test.path); s != test.result { |
70 t.Errorf("Clean(%q) = %q, want %q", test.path, s, test.r
esult) | 68 t.Errorf("Clean(%q) = %q, want %q", test.path, s, test.r
esult) |
71 } | 69 } |
72 if s := Clean(test.result); s != test.result { | 70 if s := Clean(test.result); s != test.result { |
73 t.Errorf("Clean(%q) = %q, want %q", test.result, s, test
.result) | 71 t.Errorf("Clean(%q) = %q, want %q", test.result, s, test
.result) |
74 } | 72 } |
75 } | 73 } |
76 | 74 |
77 » var ms runtime.MemStats | 75 » for _, test := range cleantests { |
78 » runtime.ReadMemStats(&ms) | 76 » » allocs := testing.AllocsPerRun(100, func() { Clean(test.result)
}) |
79 » allocs := -ms.Mallocs | 77 » » if allocs > 0 { |
80 » const rounds = 100 | 78 » » » t.Errorf("Clean(%q): %v allocs, want zero", test.result,
allocs) |
81 » for i := 0; i < rounds; i++ { | |
82 » » for _, test := range cleantests { | |
83 » » » Clean(test.result) | |
84 } | 79 } |
85 } | 80 } |
86 runtime.ReadMemStats(&ms) | |
87 allocs += ms.Mallocs | |
88 if allocs >= rounds { | |
89 t.Errorf("Clean cleaned paths: %d allocations per test round, wa
nt zero", allocs/rounds) | |
90 } | |
91 } | 81 } |
92 | 82 |
93 type SplitTest struct { | 83 type SplitTest struct { |
94 path, dir, file string | 84 path, dir, file string |
95 } | 85 } |
96 | 86 |
97 var splittests = []SplitTest{ | 87 var splittests = []SplitTest{ |
98 {"a/b", "a/", "b"}, | 88 {"a/b", "a/", "b"}, |
99 {"a/b/", "a/b/", ""}, | 89 {"a/b/", "a/b/", ""}, |
100 {"a/", "a/", ""}, | 90 {"a/", "a/", ""}, |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
231 {"lala", false}, | 221 {"lala", false}, |
232 } | 222 } |
233 | 223 |
234 func TestIsAbs(t *testing.T) { | 224 func TestIsAbs(t *testing.T) { |
235 for _, test := range isAbsTests { | 225 for _, test := range isAbsTests { |
236 if r := IsAbs(test.path); r != test.isAbs { | 226 if r := IsAbs(test.path); r != test.isAbs { |
237 t.Errorf("IsAbs(%q) = %v, want %v", test.path, r, test.i
sAbs) | 227 t.Errorf("IsAbs(%q) = %v, want %v", test.path, r, test.i
sAbs) |
238 } | 228 } |
239 } | 229 } |
240 } | 230 } |
OLD | NEW |