1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
12 type PathTest struct {
16 var cleantests = []PathTest{
20 {"abc/def", "abc/def"},
25 {"../../abc", "../../abc"},
29 // Remove trailing slash
31 {"abc/def/", "abc/def"},
38 // Remove doubled slash
39 {"abc//def//ghi", "abc/def/ghi"},
46 {"abc/./def", "abc/def"},
47 {"/./abc/def", "/abc/def"},
51 {"abc/def/ghi/../jkl", "abc/def/jkl"},
52 {"abc/def/../ghi/../jkl", "abc/jkl"},
53 {"abc/def/..", "abc"},
54 {"abc/def/../..", "."},
55 {"/abc/def/../..", "/"},
56 {"abc/def/../../..", ".."},
57 {"/abc/def/../../..", "/"},
58 {"abc/def/../../../ghi/jkl/../../../mno", "../../mno"},
61 {"abc/./../def", "def"},
62 {"abc//./../def", "def"},
63 {"abc/../../././../def", "../../def"},
66 func TestClean(t *testing.T) {
67 defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(1))
68 for _, test := range cleantests {
69 if s := Clean(test.path); s != test.result {
70 t.Errorf("Clean(%q) = %q, want %q", test.path, s, test.result)
72 if s := Clean(test.result); s != test.result {
73 t.Errorf("Clean(%q) = %q, want %q", test.result, s, test.result)
77 var ms runtime.MemStats
78 runtime.ReadMemStats(&ms)
81 for i := 0; i < rounds; i++ {
82 for _, test := range cleantests {
86 runtime.ReadMemStats(&ms)
88 /* Fails with gccgo, which has no escape analysis.
90 t.Errorf("Clean cleaned paths: %d allocations per test round, want zero", allocs/rounds)
95 type SplitTest struct {
96 path, dir, file string
99 var splittests = []SplitTest{
101 {"a/b/", "a/b/", ""},
107 func TestSplit(t *testing.T) {
108 for _, test := range splittests {
109 if d, f := Split(test.path); d != test.dir || f != test.file {
110 t.Errorf("Split(%q) = %q, %q, want %q, %q", test.path, d, f, test.dir, test.file)
115 type JoinTest struct {
120 var jointests = []JoinTest{
126 {[]string{"a"}, "a"},
129 {[]string{"a", "b"}, "a/b"},
130 {[]string{"a", ""}, "a"},
131 {[]string{"", "b"}, "b"},
132 {[]string{"/", "a"}, "/a"},
133 {[]string{"/", ""}, "/"},
134 {[]string{"a/", "b"}, "a/b"},
135 {[]string{"a/", ""}, "a"},
136 {[]string{"", ""}, ""},
139 // join takes a []string and passes it to Join.
140 func join(elem []string, args ...string) string {
145 func TestJoin(t *testing.T) {
146 for _, test := range jointests {
147 if p := join(test.elem); p != test.path {
148 t.Errorf("join(%q) = %q, want %q", test.elem, p, test.path)
153 type ExtTest struct {
157 var exttests = []ExtTest{
159 {"path.pb.go", ".go"},
161 {"a.dir/b.go", ".go"},
165 func TestExt(t *testing.T) {
166 for _, test := range exttests {
167 if x := Ext(test.path); x != test.ext {
168 t.Errorf("Ext(%q) = %q, want %q", test.path, x, test.ext)
173 var basetests = []PathTest{
188 func TestBase(t *testing.T) {
189 for _, test := range basetests {
190 if s := Base(test.path); s != test.result {
191 t.Errorf("Base(%q) = %q, want %q", test.path, s, test.result)
196 var dirtests = []PathTest{
206 {"abc////def", "abc"},
212 func TestDir(t *testing.T) {
213 for _, test := range dirtests {
214 if s := Dir(test.path); s != test.result {
215 t.Errorf("Dir(%q) = %q, want %q", test.path, s, test.result)
220 type IsAbsTest struct {
225 var isAbsTests = []IsAbsTest{
228 {"/usr/bin/gcc", true},
236 func TestIsAbs(t *testing.T) {
237 for _, test := range isAbsTests {
238 if r := IsAbs(test.path); r != test.isAbs {
239 t.Errorf("IsAbs(%q) = %v, want %v", test.path, r, test.isAbs)