github.com/llvm-mirror/llgo@v0.0.0-20190322182713-bf6f0a60fce1/third_party/gofrontend/libgo/go/path/path_test.go (about)

     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  	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)
    70  		}
    71  		if s := Clean(test.result); s != test.result {
    72  			t.Errorf("Clean(%q) = %q, want %q", test.result, s, test.result)
    73  		}
    74  	}
    75  }
    76  
    77  func TestCleanMallocs(t *testing.T) {
    78  	if testing.Short() {
    79  		t.Skip("skipping malloc count in short mode")
    80  	}
    81  	if runtime.GOMAXPROCS(0) > 1 {
    82  		t.Log("skipping AllocsPerRun checks; GOMAXPROCS>1")
    83  		return
    84  	}
    85  
    86  	t.Log("Skipping AllocsPerRun for gccgo")
    87  	return
    88  
    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)
    93  		}
    94  	}
    95  }
    96  
    97  type SplitTest struct {
    98  	path, dir, file string
    99  }
   100  
   101  var splittests = []SplitTest{
   102  	{"a/b", "a/", "b"},
   103  	{"a/b/", "a/b/", ""},
   104  	{"a/", "a/", ""},
   105  	{"a", "", "a"},
   106  	{"/", "/", ""},
   107  }
   108  
   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)
   113  		}
   114  	}
   115  }
   116  
   117  type JoinTest struct {
   118  	elem []string
   119  	path string
   120  }
   121  
   122  var jointests = []JoinTest{
   123  	// zero parameters
   124  	{[]string{}, ""},
   125  
   126  	// one parameter
   127  	{[]string{""}, ""},
   128  	{[]string{"a"}, "a"},
   129  
   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{"", ""}, ""},
   139  }
   140  
   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...)
   145  }
   146  
   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)
   151  		}
   152  	}
   153  }
   154  
   155  type ExtTest struct {
   156  	path, ext string
   157  }
   158  
   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/", ""},
   165  }
   166  
   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)
   171  		}
   172  	}
   173  }
   174  
   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"},
   188  }
   189  
   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)
   194  		}
   195  	}
   196  }
   197  
   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"},
   212  }
   213  
   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)
   218  		}
   219  	}
   220  }
   221  
   222  type IsAbsTest struct {
   223  	path  string
   224  	isAbs bool
   225  }
   226  
   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},
   236  }
   237  
   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)
   242  		}
   243  	}
   244  }