Changeset 1432
- Timestamp:
- Dec 18, 2011, 6:36:40 PM (11 years ago)
- Location:
- trunk
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/utility.cc
r1430 r1432 117 117 118 118 119 std::string directory_name(std::string path) 120 { 121 return dirname(path.c_str()); 122 } 123 124 125 std::string file_name(const std::string& full_path) 126 { 127 return basename(full_path.c_str()); 119 std::string directory_name(const std::string& path) 120 { 121 if (path.size()==1) { 122 if (path=="/") 123 return path; 124 else 125 return "."; 126 } 127 assert(path.size()>=2); 128 size_t pos = path.find_last_of('/', path.size()-2); 129 if (pos==std::string::npos) 130 return "."; 131 if (pos==0) 132 return "/"; 133 return path.substr(0,pos); 134 return ""; 135 } 136 137 138 std::string file_name(const std::string& path) 139 { 140 if (path.size()==1) 141 return path; 142 size_t pos = path.find_last_of('/'); 143 if (pos==std::string::npos) 144 return path; 145 if (pos==path.size()-1) 146 return file_name(path.substr(0, path.size()-1)); 147 return path.substr(pos+1); 128 148 } 129 149 -
trunk/lib/utility.h
r1430 r1432 89 89 \return directory of path. 90 90 */ 91 std::string directory_name( std::stringpath);91 std::string directory_name(const std::string& path); 92 92 93 93 /// -
trunk/test/utility.cc
r1164 r1432 30 30 #include <string> 31 31 32 void test_directory_name(theplu::svndigest::test::Suite& suite); 33 void test_file_name(theplu::svndigest::test::Suite& suite); 32 34 bool test_hex(int, unsigned int, std::string); 33 35 bool test_fnmatch(bool, std::string, std::string); 34 bool test_regexp(bool, std::string, std::string, 36 bool test_regexp(bool, std::string, std::string, 35 37 const std::vector<std::string>&); 36 38 … … 39 41 theplu::svndigest::test::Suite suite(argc, argv); 40 42 bool ok=true; 41 42 ok &= test_hex(15,2, "0f"); 43 ok &= test_hex(17,1, "1"); 44 ok &= test_hex(16,2, "10"); 45 46 ok &= test_fnmatch(true,"peter", "peter"); 47 ok &= test_fnmatch(false,"peter", "peterj"); 48 49 ok &= test_fnmatch(true,"*", "peterj"); 50 ok &= test_fnmatch(true,"p*", "peterj"); 51 ok &= test_fnmatch(true, "p*", "peter"); 52 ok &= test_fnmatch(false, "p*j", "peter"); 53 ok &= test_fnmatch(true, "p*j", "peterj"); 54 ok &= test_fnmatch(true, "*peter", "peter"); 55 56 ok &= test_fnmatch(true, "p?ter", "peter"); 57 ok &= test_fnmatch(false, "p?er", "peter"); 58 ok &= test_fnmatch(false, "p?eter", "peter"); 43 44 ok &= test_hex(15,2, "0f"); 45 ok &= test_hex(17,1, "1"); 46 ok &= test_hex(16,2, "10"); 47 48 ok &= test_fnmatch(true,"peter", "peter"); 49 ok &= test_fnmatch(false,"peter", "peterj"); 50 51 ok &= test_fnmatch(true,"*", "peterj"); 52 ok &= test_fnmatch(true,"p*", "peterj"); 53 ok &= test_fnmatch(true, "p*", "peter"); 54 ok &= test_fnmatch(false, "p*j", "peter"); 55 ok &= test_fnmatch(true, "p*j", "peterj"); 56 ok &= test_fnmatch(true, "*peter", "peter"); 57 58 ok &= test_fnmatch(true, "p?ter", "peter"); 59 ok &= test_fnmatch(false, "p?er", "peter"); 60 ok &= test_fnmatch(false, "p?eter", "peter"); 59 61 60 62 ok &= test_fnmatch(true, "filename", "filename"); … … 77 79 78 80 std::vector<std::string> vec; 79 ok &= test_regexp(true,"abcde", "abcde", vec); 81 ok &= test_regexp(true,"abcde", "abcde", vec); 80 82 vec.push_back("c"); 81 ok &= test_regexp(true,"ab?de", "abcde", vec); 83 ok &= test_regexp(true,"ab?de", "abcde", vec); 82 84 vec[0] = "bcd"; 83 ok &= test_regexp(true,"a*e", "abcde", vec); 85 ok &= test_regexp(true,"a*e", "abcde", vec); 84 86 vec.push_back(""); 85 ok &= test_regexp(true,"a*d*f", "abcddf", vec); 87 ok &= test_regexp(true,"a*d*f", "abcddf", vec); 86 88 vec[0] = "bc"; 87 89 vec[1] = "ef"; 88 ok &= test_regexp(true,"a*d*g", "abcdefg", vec); 90 ok &= test_regexp(true,"a*d*g", "abcdefg", vec); 89 91 vec.push_back(""); 90 92 vec[1]="e"; 91 93 vec[2]="f"; 92 ok &= test_regexp(true,"a*d*?g", "abcdefg", vec); 93 ok &= test_regexp(true,"a*d??g", "abcdefg", vec); 94 ok &= test_regexp(true,"a*d*?g", "abcdefg", vec); 95 ok &= test_regexp(true,"a*d??g", "abcdefg", vec); 94 96 vec.resize(2); 95 97 vec[0]="f"; … … 98 100 99 101 suite.add(ok); 102 test_directory_name(suite); 103 test_file_name(suite); 100 104 return suite.exit_status(); 101 105 } 106 107 108 void test_directory_name(theplu::svndigest::test::Suite& suite) 109 { 110 std::vector<std::string> path; 111 path.push_back("/usr/lib"); 112 path.push_back("/usr/"); 113 path.push_back("usr"); 114 path.push_back("/"); 115 path.push_back("."); 116 path.push_back(".."); 117 std::vector<std::string> dir; 118 dir.push_back("/usr"); 119 dir.push_back("/"); 120 dir.push_back("."); 121 dir.push_back("/"); 122 dir.push_back("."); 123 dir.push_back("."); 124 assert(dir.size()==path.size()); 125 using theplu::svndigest::directory_name; 126 for (size_t i=0; i<dir.size(); ++i) { 127 if (dir[i] != directory_name(path[i])) { 128 suite.add(false); 129 suite.out() << "error:\n"; 130 suite.out() << "path: " << path[i] << "\n"; 131 suite.out() << "directory_name: " << directory_name(path[i]) << "\n"; 132 suite.out() << "expected: " << dir[i] << "\n"; 133 } 134 } 135 } 136 137 138 void test_file_name(theplu::svndigest::test::Suite& suite) 139 { 140 std::vector<std::string> path; 141 path.push_back("/usr/lib"); 142 path.push_back("/usr/"); 143 path.push_back("usr"); 144 path.push_back("/"); 145 path.push_back("."); 146 path.push_back(".."); 147 std::vector<std::string> file; 148 file.push_back("lib"); 149 file.push_back("usr"); 150 file.push_back("usr"); 151 file.push_back("/"); 152 file.push_back("."); 153 file.push_back(".."); 154 assert(file.size()==path.size()); 155 using theplu::svndigest::file_name; 156 for (size_t i=0; i<file.size(); ++i) { 157 if (file[i] != file_name(path[i])) { 158 suite.add(false); 159 suite.out() << "error:\n"; 160 suite.out() << "path: " << path[i] << "\n"; 161 suite.out() << "file_name: " << file_name(path[i]) << "\n"; 162 suite.out() << "expected: " << file[i] << "\n"; 163 } 164 } 165 } 166 102 167 103 168 bool test_fnmatch(bool answ, std::string a, std::string b) … … 126 191 return true; 127 192 std::cerr << "hex(" << x << ", " << w << ") results " 128 << theplu::svndigest::hex(x,w) << ". Expects " << facit 193 << theplu::svndigest::hex(x,w) << ". Expects " << facit 129 194 << std::endl; 130 195 return false; 131 196 } 132 197 133 bool test_regexp(bool ans, std::string a, std::string b, 198 bool test_regexp(bool ans, std::string a, std::string b, 134 199 const std::vector<std::string>& vec) 135 200 { … … 141 206 << res << ". Expected " << ans << "\n" 142 207 << "resulting vector:\n"; 143 std::copy(v.begin(), v.end(), 208 std::copy(v.begin(), v.end(), 144 209 std::ostream_iterator<std::string>(std::cerr, "\n")); 145 210 std::cerr << "expected:\n"; 146 std::copy(vec.begin(), vec.end(), 211 std::copy(vec.begin(), vec.end(), 147 212 std::ostream_iterator<std::string>(std::cerr, "\n")); 148 213 return false;
Note: See TracChangeset
for help on using the changeset viewer.