Imported Upstream version 4.8.1
[platform/upstream/gcc48.git] / libgo / go / path / path_test.go
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.
4
5 package path
6
7 import (
8         "runtime"
9         "testing"
10 )
11
12 type PathTest struct {
13         path, result string
14 }
15
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         {"/", "/"},
28
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"},
37
38         // Remove doubled slash
39         {"abc//def//ghi", "abc/def/ghi"},
40         {"//abc", "/abc"},
41         {"///abc", "/abc"},
42         {"//abc//", "/abc"},
43         {"abc//", "abc"},
44
45         // Remove . elements
46         {"abc/./def", "abc/def"},
47         {"/./abc/def", "/abc/def"},
48         {"abc/.", "abc"},
49
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"},
59
60         // Combinations
61         {"abc/./../def", "def"},
62         {"abc//./../def", "def"},
63         {"abc/../../././../def", "../../def"},
64 }
65
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)
71                 }
72                 if s := Clean(test.result); s != test.result {
73                         t.Errorf("Clean(%q) = %q, want %q", test.result, s, test.result)
74                 }
75         }
76
77         var ms runtime.MemStats
78         runtime.ReadMemStats(&ms)
79         allocs := -ms.Mallocs
80         const rounds = 100
81         for i := 0; i < rounds; i++ {
82                 for _, test := range cleantests {
83                         Clean(test.result)
84                 }
85         }
86         runtime.ReadMemStats(&ms)
87         allocs += ms.Mallocs
88         /* Fails with gccgo, which has no escape analysis.
89         if allocs >= rounds {
90                 t.Errorf("Clean cleaned paths: %d allocations per test round, want zero", allocs/rounds)
91         }
92         */
93 }
94
95 type SplitTest struct {
96         path, dir, file string
97 }
98
99 var splittests = []SplitTest{
100         {"a/b", "a/", "b"},
101         {"a/b/", "a/b/", ""},
102         {"a/", "a/", ""},
103         {"a", "", "a"},
104         {"/", "/", ""},
105 }
106
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)
111                 }
112         }
113 }
114
115 type JoinTest struct {
116         elem []string
117         path string
118 }
119
120 var jointests = []JoinTest{
121         // zero parameters
122         {[]string{}, ""},
123
124         // one parameter
125         {[]string{""}, ""},
126         {[]string{"a"}, "a"},
127
128         // two parameters
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{"", ""}, ""},
137 }
138
139 // join takes a []string and passes it to Join.
140 func join(elem []string, args ...string) string {
141         args = elem
142         return Join(args...)
143 }
144
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)
149                 }
150         }
151 }
152
153 type ExtTest struct {
154         path, ext string
155 }
156
157 var exttests = []ExtTest{
158         {"path.go", ".go"},
159         {"path.pb.go", ".go"},
160         {"a.dir/b", ""},
161         {"a.dir/b.go", ".go"},
162         {"a.dir/", ""},
163 }
164
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)
169                 }
170         }
171 }
172
173 var basetests = []PathTest{
174         // Already clean
175         {"", "."},
176         {".", "."},
177         {"/.", "."},
178         {"/", "/"},
179         {"////", "/"},
180         {"x/", "x"},
181         {"abc", "abc"},
182         {"abc/def", "def"},
183         {"a/b/.x", ".x"},
184         {"a/b/c.", "c."},
185         {"a/b/c.x", "c.x"},
186 }
187
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)
192                 }
193         }
194 }
195
196 var dirtests = []PathTest{
197         {"", "."},
198         {".", "."},
199         {"/.", "/"},
200         {"/", "/"},
201         {"////", "/"},
202         {"/foo", "/"},
203         {"x/", "x"},
204         {"abc", "."},
205         {"abc/def", "abc"},
206         {"abc////def", "abc"},
207         {"a/b/.x", "a/b"},
208         {"a/b/c.", "a/b"},
209         {"a/b/c.x", "a/b"},
210 }
211
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)
216                 }
217         }
218 }
219
220 type IsAbsTest struct {
221         path  string
222         isAbs bool
223 }
224
225 var isAbsTests = []IsAbsTest{
226         {"", false},
227         {"/", true},
228         {"/usr/bin/gcc", true},
229         {"..", false},
230         {"/a/../bb", true},
231         {".", false},
232         {"./", false},
233         {"lala", false},
234 }
235
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)
240                 }
241         }
242 }