Merge from mainline (167278:168000).
[official-gcc/graphite-test-results.git] / libgo / go / path / path_test.go
blob2bbb9244aa46496657b2da8b8a35ae04f856d02a
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 "os"
9 "testing"
12 type CleanTest struct {
13 path, clean string
16 var cleantests = []CleanTest{
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.clean {
69 t.Errorf("Clean(%q) = %q, want %q", test.path, s, test.clean)
74 type SplitTest struct {
75 path, dir, file string
78 var splittests = []SplitTest{
79 {"a/b", "a/", "b"},
80 {"a/b/", "a/b/", ""},
81 {"a/", "a/", ""},
82 {"a", "", "a"},
83 {"/", "/", ""},
86 func TestSplit(t *testing.T) {
87 for _, test := range splittests {
88 if d, f := Split(test.path); d != test.dir || f != test.file {
89 t.Errorf("Split(%q) = %q, %q, want %q, %q", test.path, d, f, test.dir, test.file)
94 type JoinTest struct {
95 elem []string
96 path string
99 var jointests = []JoinTest{
100 // zero parameters
101 {[]string{}, ""},
103 // one parameter
104 {[]string{""}, ""},
105 {[]string{"a"}, "a"},
107 // two parameters
108 {[]string{"a", "b"}, "a/b"},
109 {[]string{"a", ""}, "a"},
110 {[]string{"", "b"}, "b"},
111 {[]string{"/", "a"}, "/a"},
112 {[]string{"/", ""}, "/"},
113 {[]string{"a/", "b"}, "a/b"},
114 {[]string{"a/", ""}, "a"},
115 {[]string{"", ""}, ""},
118 // join takes a []string and passes it to Join.
119 func join(elem []string, args ...string) string {
120 args = elem
121 return Join(args...)
124 func TestJoin(t *testing.T) {
125 for _, test := range jointests {
126 if p := join(test.elem); p != test.path {
127 t.Errorf("join(%q) = %q, want %q", test.elem, p, test.path)
132 type ExtTest struct {
133 path, ext string
136 var exttests = []ExtTest{
137 {"path.go", ".go"},
138 {"path.pb.go", ".go"},
139 {"a.dir/b", ""},
140 {"a.dir/b.go", ".go"},
141 {"a.dir/", ""},
144 func TestExt(t *testing.T) {
145 for _, test := range exttests {
146 if x := Ext(test.path); x != test.ext {
147 t.Errorf("Ext(%q) = %q, want %q", test.path, x, test.ext)
152 type Node struct {
153 name string
154 entries []*Node // nil if the entry is a file
155 mark int
158 var tree = &Node{
159 "testdata",
160 []*Node{
161 &Node{"a", nil, 0},
162 &Node{"b", []*Node{}, 0},
163 &Node{"c", nil, 0},
164 &Node{
165 "d",
166 []*Node{
167 &Node{"x", nil, 0},
168 &Node{"y", []*Node{}, 0},
169 &Node{
170 "z",
171 []*Node{
172 &Node{"u", nil, 0},
173 &Node{"v", nil, 0},
184 func walkTree(n *Node, path string, f func(path string, n *Node)) {
185 f(path, n)
186 for _, e := range n.entries {
187 walkTree(e, Join(path, e.name), f)
191 func makeTree(t *testing.T) {
192 walkTree(tree, tree.name, func(path string, n *Node) {
193 if n.entries == nil {
194 fd, err := os.Open(path, os.O_CREAT, 0660)
195 if err != nil {
196 t.Errorf("makeTree: %v", err)
198 fd.Close()
199 } else {
200 os.Mkdir(path, 0770)
205 func markTree(n *Node) { walkTree(n, "", func(path string, n *Node) { n.mark++ }) }
207 func checkMarks(t *testing.T) {
208 walkTree(tree, tree.name, func(path string, n *Node) {
209 if n.mark != 1 {
210 t.Errorf("node %s mark = %d; expected 1", path, n.mark)
212 n.mark = 0
216 // Assumes that each node name is unique. Good enough for a test.
217 func mark(name string) {
218 walkTree(tree, tree.name, func(path string, n *Node) {
219 if n.name == name {
220 n.mark++
225 type TestVisitor struct{}
227 func (v *TestVisitor) VisitDir(path string, f *os.FileInfo) bool {
228 mark(f.Name)
229 return true
232 func (v *TestVisitor) VisitFile(path string, f *os.FileInfo) {
233 mark(f.Name)
236 func TestWalk(t *testing.T) {
237 makeTree(t)
239 // 1) ignore error handling, expect none
240 v := &TestVisitor{}
241 Walk(tree.name, v, nil)
242 checkMarks(t)
244 // 2) handle errors, expect none
245 errors := make(chan os.Error, 64)
246 Walk(tree.name, v, errors)
247 if err, ok := <-errors; ok {
248 t.Errorf("no error expected, found: s", err)
250 checkMarks(t)
252 if os.Getuid() != 0 {
253 // introduce 2 errors: chmod top-level directories to 0
254 os.Chmod(Join(tree.name, tree.entries[1].name), 0)
255 os.Chmod(Join(tree.name, tree.entries[3].name), 0)
256 // mark respective subtrees manually
257 markTree(tree.entries[1])
258 markTree(tree.entries[3])
259 // correct double-marking of directory itself
260 tree.entries[1].mark--
261 tree.entries[3].mark--
263 // 3) handle errors, expect two
264 errors = make(chan os.Error, 64)
265 os.Chmod(Join(tree.name, tree.entries[1].name), 0)
266 Walk(tree.name, v, errors)
267 for i := 1; i <= 2; i++ {
268 if _, ok := <-errors; !ok {
269 t.Errorf("%d. error expected, none found", i)
270 break
273 if err, ok := <-errors; ok {
274 t.Errorf("only two errors expected, found 3rd: %v", err)
276 // the inaccessible subtrees were marked manually
277 checkMarks(t)
280 // cleanup
281 os.Chmod(Join(tree.name, tree.entries[1].name), 0770)
282 os.Chmod(Join(tree.name, tree.entries[3].name), 0770)
283 if err := os.RemoveAll(tree.name); err != nil {
284 t.Errorf("removeTree: %v", err)
288 var basetests = []CleanTest{
289 // Already clean
290 {"", "."},
291 {".", "."},
292 {"/.", "."},
293 {"/", "/"},
294 {"////", "/"},
295 {"x/", "x"},
296 {"abc", "abc"},
297 {"abc/def", "def"},
298 {"a/b/.x", ".x"},
299 {"a/b/c.", "c."},
300 {"a/b/c.x", "c.x"},
303 func TestBase(t *testing.T) {
304 for _, test := range basetests {
305 if s := Base(test.path); s != test.clean {
306 t.Errorf("Base(%q) = %q, want %q", test.path, s, test.clean)
311 type IsAbsTest struct {
312 path string
313 isAbs bool
316 var isAbsTests = []IsAbsTest{
317 {"", false},
318 {"/", true},
319 {"/usr/bin/gcc", true},
320 {"..", false},
321 {"/a/../bb", true},
322 {".", false},
323 {"./", false},
324 {"lala", false},
327 func TestIsAbs(t *testing.T) {
328 for _, test := range isAbsTests {
329 if r := IsAbs(test.path); r != test.isAbs {
330 t.Errorf("IsAbs(%q) = %v, want %v", test.path, r, test.isAbs)