* tree-ssa-loop-ivopts.c (estimated_stmt_executions_int): Use
[official-gcc.git] / libgo / go / path / path_test.go
blob512d936e45771fd249785d380ccfa4d328f16f95
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.
5 package path
7 import (
8 "runtime"
9 "testing"
12 type PathTest struct {
13 path, result string
16 var cleantests = []PathTest{
17 // Already clean
18 {"", "."},
19 {"abc", "abc"},
20 {"abc/def", "abc/def"},
21 {"a/b/c", "a/b/c"},
22 {".", "."},
23 {"..", ".."},
24 {"../..", "../.."},
25 {"../../abc", "../../abc"},
26 {"/abc", "/abc"},
27 {"/", "/"},
29 // Remove trailing slash
30 {"abc/", "abc"},
31 {"abc/def/", "abc/def"},
32 {"a/b/c/", "a/b/c"},
33 {"./", "."},
34 {"../", ".."},
35 {"../../", "../.."},
36 {"/abc/", "/abc"},
38 // Remove doubled slash
39 {"abc//def//ghi", "abc/def/ghi"},
40 {"//abc", "/abc"},
41 {"///abc", "/abc"},
42 {"//abc//", "/abc"},
43 {"abc//", "abc"},
45 // Remove . elements
46 {"abc/./def", "abc/def"},
47 {"/./abc/def", "/abc/def"},
48 {"abc/.", "abc"},
50 // Remove .. elements
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"},
60 // Combinations
61 {"abc/./../def", "def"},
62 {"abc//./../def", "def"},
63 {"abc/../../././../def", "../../def"},
66 func TestClean(t *testing.T) {
67 for _, test := range cleantests {
68 if s := Clean(test.path); s != test.result {
69 t.Errorf("Clean(%q) = %q, want %q", test.path, s, test.result)
71 if s := Clean(test.result); s != test.result {
72 t.Errorf("Clean(%q) = %q, want %q", test.result, s, test.result)
77 func TestCleanMallocs(t *testing.T) {
78 if testing.Short() {
79 t.Skip("skipping malloc count in short mode")
81 if runtime.GOMAXPROCS(0) > 1 {
82 t.Log("skipping AllocsPerRun checks; GOMAXPROCS>1")
83 return
86 t.Log("Skipping AllocsPerRun for gccgo")
87 return
89 for _, test := range cleantests {
90 allocs := testing.AllocsPerRun(100, func() { Clean(test.result) })
91 if allocs > 0 {
92 t.Errorf("Clean(%q): %v allocs, want zero", test.result, allocs)
97 type SplitTest struct {
98 path, dir, file string
101 var splittests = []SplitTest{
102 {"a/b", "a/", "b"},
103 {"a/b/", "a/b/", ""},
104 {"a/", "a/", ""},
105 {"a", "", "a"},
106 {"/", "/", ""},
109 func TestSplit(t *testing.T) {
110 for _, test := range splittests {
111 if d, f := Split(test.path); d != test.dir || f != test.file {
112 t.Errorf("Split(%q) = %q, %q, want %q, %q", test.path, d, f, test.dir, test.file)
117 type JoinTest struct {
118 elem []string
119 path string
122 var jointests = []JoinTest{
123 // zero parameters
124 {[]string{}, ""},
126 // one parameter
127 {[]string{""}, ""},
128 {[]string{"a"}, "a"},
130 // two parameters
131 {[]string{"a", "b"}, "a/b"},
132 {[]string{"a", ""}, "a"},
133 {[]string{"", "b"}, "b"},
134 {[]string{"/", "a"}, "/a"},
135 {[]string{"/", ""}, "/"},
136 {[]string{"a/", "b"}, "a/b"},
137 {[]string{"a/", ""}, "a"},
138 {[]string{"", ""}, ""},
141 // join takes a []string and passes it to Join.
142 func join(elem []string, args ...string) string {
143 args = elem
144 return Join(args...)
147 func TestJoin(t *testing.T) {
148 for _, test := range jointests {
149 if p := join(test.elem); p != test.path {
150 t.Errorf("join(%q) = %q, want %q", test.elem, p, test.path)
155 type ExtTest struct {
156 path, ext string
159 var exttests = []ExtTest{
160 {"path.go", ".go"},
161 {"path.pb.go", ".go"},
162 {"a.dir/b", ""},
163 {"a.dir/b.go", ".go"},
164 {"a.dir/", ""},
167 func TestExt(t *testing.T) {
168 for _, test := range exttests {
169 if x := Ext(test.path); x != test.ext {
170 t.Errorf("Ext(%q) = %q, want %q", test.path, x, test.ext)
175 var basetests = []PathTest{
176 // Already clean
177 {"", "."},
178 {".", "."},
179 {"/.", "."},
180 {"/", "/"},
181 {"////", "/"},
182 {"x/", "x"},
183 {"abc", "abc"},
184 {"abc/def", "def"},
185 {"a/b/.x", ".x"},
186 {"a/b/c.", "c."},
187 {"a/b/c.x", "c.x"},
190 func TestBase(t *testing.T) {
191 for _, test := range basetests {
192 if s := Base(test.path); s != test.result {
193 t.Errorf("Base(%q) = %q, want %q", test.path, s, test.result)
198 var dirtests = []PathTest{
199 {"", "."},
200 {".", "."},
201 {"/.", "/"},
202 {"/", "/"},
203 {"////", "/"},
204 {"/foo", "/"},
205 {"x/", "x"},
206 {"abc", "."},
207 {"abc/def", "abc"},
208 {"abc////def", "abc"},
209 {"a/b/.x", "a/b"},
210 {"a/b/c.", "a/b"},
211 {"a/b/c.x", "a/b"},
214 func TestDir(t *testing.T) {
215 for _, test := range dirtests {
216 if s := Dir(test.path); s != test.result {
217 t.Errorf("Dir(%q) = %q, want %q", test.path, s, test.result)
222 type IsAbsTest struct {
223 path string
224 isAbs bool
227 var isAbsTests = []IsAbsTest{
228 {"", false},
229 {"/", true},
230 {"/usr/bin/gcc", true},
231 {"..", false},
232 {"/a/../bb", true},
233 {".", false},
234 {"./", false},
235 {"lala", false},
238 func TestIsAbs(t *testing.T) {
239 for _, test := range isAbsTests {
240 if r := IsAbs(test.path); r != test.isAbs {
241 t.Errorf("IsAbs(%q) = %v, want %v", test.path, r, test.isAbs)