wibble
0.1.28
|
00001 /* -*- C++ -*- (c) 2007 Petr Rockai <me@mornfall.net> 00002 (c) 2007 Enrico Zini <enrico@enricozini.org> */ 00003 00004 #include <wibble/test.h> 00005 #include <wibble/string.h> 00006 #include <wibble/list.h> 00007 00008 namespace { 00009 00010 using namespace std; 00011 using namespace wibble; 00012 00013 struct TestString { 00014 00015 Test fmt() 00016 { 00017 assert_eq(str::fmt(5), "5"); 00018 assert_eq(str::fmt(5.123), "5.123"); 00019 assert_eq(str::fmtf("ciao"), "ciao"); 00020 } 00021 00022 Test fmtSet() 00023 { 00024 std::set< int > a; 00025 assert_eq(str::fmt(a), "{}"); 00026 a.insert( a.begin(), 2 ); 00027 assert_eq(str::fmt(a), "{ 2 }"); 00028 a.insert( a.begin(), 5 ); 00029 assert_eq(str::fmt(a), "{ 2, 5 }"); 00030 a.insert( a.begin(), 1 ); 00031 assert_eq(str::fmt(a), "{ 1, 2, 5 }"); 00032 } 00033 00034 Test fmtVec() 00035 { 00036 std::vector< int > a; 00037 assert_eq(str::fmt(a), "[]"); 00038 a.push_back( 2 ); 00039 assert_eq(str::fmt(a), "[ 2 ]"); 00040 a.push_back( 5 ); 00041 assert_eq(str::fmt(a), "[ 2, 5 ]"); 00042 a.push_back( 1 ); 00043 assert_eq(str::fmt(a), "[ 2, 5, 1 ]"); 00044 } 00045 00046 Test fmtList() 00047 { 00048 assert_eq( str::fmt( list::Empty< int >() ), "[]" ); 00049 assert_eq( str::fmt( list::singular( 0 ) ), "[ 0 ]" ); 00050 assert_eq( str::fmt( list::append( 00051 list::singular( 0 ), 00052 list::singular( 2 ) ) ), "[ 0, 2 ]" ); 00053 } 00054 00055 Test basename() 00056 { 00057 assert_eq(str::basename("ciao"), "ciao"); 00058 assert_eq(str::basename("a/ciao"), "ciao"); 00059 assert_eq(str::basename("a/b/c/c/d/e/ciao"), "ciao"); 00060 assert_eq(str::basename("/ciao"), "ciao"); 00061 } 00062 00063 Test dirname() 00064 { 00065 assert_eq(str::dirname("ciao"), ""); 00066 assert_eq(str::dirname("a/ciao"), "a"); 00067 assert_eq(str::dirname("a/b/c/c/d/e/ciao"), "a/b/c/c/d/e"); 00068 assert_eq(str::dirname("/a/ciao"), "/a"); 00069 assert_eq(str::dirname("/ciao"), "/"); 00070 } 00071 00072 Test trim() 00073 { 00074 assert_eq(str::trim(" "), ""); 00075 assert_eq(str::trim(" c "), "c"); 00076 assert_eq(str::trim("ciao"), "ciao"); 00077 assert_eq(str::trim(" ciao"), "ciao"); 00078 assert_eq(str::trim(" ciao"), "ciao"); 00079 assert_eq(str::trim("ciao "), "ciao"); 00080 assert_eq(str::trim("ciao "), "ciao"); 00081 assert_eq(str::trim(" ciao "), "ciao"); 00082 assert_eq(str::trim(" ciao "), "ciao"); 00083 } 00084 00085 Test trim2() 00086 { 00087 assert_eq(str::trim(string("ciao"), ::isalpha), ""); 00088 assert_eq(str::trim(" ", ::isalpha), " "); 00089 } 00090 00091 Test tolower() 00092 { 00093 assert_eq(str::tolower("ciao"), "ciao"); 00094 assert_eq(str::tolower("CIAO"), "ciao"); 00095 assert_eq(str::tolower("Ciao"), "ciao"); 00096 assert_eq(str::tolower("cIAO"), "ciao"); 00097 } 00098 00099 Test toupper() 00100 { 00101 assert_eq(str::toupper("ciao"), "CIAO"); 00102 assert_eq(str::toupper("CIAO"), "CIAO"); 00103 assert_eq(str::toupper("Ciao"), "CIAO"); 00104 assert_eq(str::toupper("cIAO"), "CIAO"); 00105 } 00106 00107 Test ucfirst() 00108 { 00109 assert_eq(str::ucfirst("ciao"), "Ciao"); 00110 assert_eq(str::ucfirst("CIAO"), "Ciao"); 00111 assert_eq(str::ucfirst("Ciao"), "Ciao"); 00112 assert_eq(str::ucfirst("cIAO"), "Ciao"); 00113 } 00114 00115 // Check startsWith 00116 Test startsWith() 00117 { 00118 assert(str::startsWith("ciao", "ci")); 00119 assert(str::startsWith("ciao", "")); 00120 assert(str::startsWith("ciao", "ciao")); 00121 assert(!str::startsWith("ciao", "ciaoa")); 00122 assert(!str::startsWith("ciao", "i")); 00123 } 00124 00125 Test endsWith() 00126 { 00127 assert(str::endsWith("ciao", "ao")); 00128 assert(str::endsWith("ciao", "")); 00129 assert(str::endsWith("ciao", "ciao")); 00130 assert(!str::endsWith("ciao", "aciao")); 00131 assert(!str::endsWith("ciao", "a")); 00132 } 00133 00134 Test joinpath() 00135 { 00136 assert_eq(str::joinpath("a", "b"), "a/b"); 00137 assert_eq(str::joinpath("a/", "b"), "a/b"); 00138 assert_eq(str::joinpath("a", "/b"), "a/b"); 00139 assert_eq(str::joinpath("a/", "/b"), "a/b"); 00140 } 00141 00142 Test urlencode() 00143 { 00144 assert_eq(str::urlencode(""), ""); 00145 assert_eq(str::urlencode("antani"), "antani"); 00146 assert_eq(str::urlencode("a b c"), "a%20b%20c"); 00147 assert_eq(str::urlencode("a "), "a%20"); 00148 00149 assert_eq(str::urldecode(""), ""); 00150 assert_eq(str::urldecode("antani"), "antani"); 00151 assert_eq(str::urldecode("a%20b"), "a b"); 00152 assert_eq(str::urldecode("a%20"), "a "); 00153 assert_eq(str::urldecode("a%2"), "a"); 00154 assert_eq(str::urldecode("a%"), "a"); 00155 00156 assert_eq(str::urldecode(str::urlencode("àá☣☢☠!@#$%^&*(\")/A")), "àá☣☢☠!@#$%^&*(\")/A"); 00157 assert_eq(str::urldecode(str::urlencode("http://zz:ss@a.b:31/c?d=e&f=g")), "http://zz:ss@a.b:31/c?d=e&f=g"); 00158 } 00159 00160 Test split1() 00161 { 00162 string val = ""; 00163 str::Split split("/", val); 00164 str::Split::const_iterator i = split.begin(); 00165 assert(i == split.end()); 00166 } 00167 00168 Test split2() 00169 { 00170 string val = "foo"; 00171 str::Split split("/", val); 00172 str::Split::const_iterator i = split.begin(); 00173 assert(i != split.end()); 00174 assert_eq(*i, "foo"); 00175 assert_eq(i.remainder(), ""); 00176 ++i; 00177 assert(i == split.end()); 00178 } 00179 00180 Test split3() 00181 { 00182 string val = "foo"; 00183 str::Split split("", val); 00184 str::Split::const_iterator i = split.begin(); 00185 assert(i != split.end()); 00186 assert_eq(*i, "f"); 00187 assert_eq(i.remainder(), "oo"); 00188 ++i; 00189 assert_eq(*i, "o"); 00190 assert_eq(i.remainder(), "o"); 00191 ++i; 00192 assert_eq(*i, "o"); 00193 assert_eq(i.remainder(), ""); 00194 ++i; 00195 assert(i == split.end()); 00196 } 00197 00198 Test split4() 00199 { 00200 string val = "/a//foo/"; 00201 str::Split split("/", val); 00202 str::Split::const_iterator i = split.begin(); 00203 assert(i != split.end()); 00204 assert_eq(*i, ""); 00205 assert_eq(i.remainder(), "a//foo/"); 00206 ++i; 00207 assert(i != split.end()); 00208 assert_eq(*i, "a"); 00209 assert_eq(i.remainder(), "/foo/"); 00210 ++i; 00211 assert(i != split.end()); 00212 assert_eq(*i, ""); 00213 assert_eq(i.remainder(), "foo/"); 00214 ++i; 00215 assert(i != split.end()); 00216 assert_eq(*i, "foo"); 00217 assert_eq(i.remainder(), ""); 00218 ++i; 00219 assert(i == split.end()); 00220 } 00221 00222 Test join() 00223 { 00224 string val = "/a//foo/"; 00225 str::Split split("/", val); 00226 string res = str::join(split.begin(), split.end(), ":"); 00227 assert_eq(res, ":a::foo"); 00228 } 00229 00230 Test normpath() 00231 { 00232 assert_eq(str::normpath(""), "."); 00233 assert_eq(str::normpath("/"), "/"); 00234 assert_eq(str::normpath("foo"), "foo"); 00235 assert_eq(str::normpath("foo/"), "foo"); 00236 assert_eq(str::normpath("/foo"), "/foo"); 00237 assert_eq(str::normpath("foo/bar"), "foo/bar"); 00238 assert_eq(str::normpath("foo/./bar"), "foo/bar"); 00239 assert_eq(str::normpath("././././foo/./././bar/././././"), "foo/bar"); 00240 assert_eq(str::normpath("/../../../../../foo"), "/foo"); 00241 assert_eq(str::normpath("foo/../foo/../foo/../foo/../"), "."); 00242 assert_eq(str::normpath("foo//bar"), "foo/bar"); 00243 assert_eq(str::normpath("foo/./bar"), "foo/bar"); 00244 assert_eq(str::normpath("foo/foo/../bar"), "foo/bar"); 00245 } 00246 00247 Test base64() 00248 { 00249 using namespace str; 00250 assert_eq(encodeBase64(""), ""); 00251 assert_eq(encodeBase64("c"), "Yw=="); 00252 assert_eq(encodeBase64("ci"), "Y2k="); 00253 assert_eq(encodeBase64("cia"), "Y2lh"); 00254 assert_eq(encodeBase64("ciao"), "Y2lhbw=="); 00255 assert_eq(encodeBase64("ciao "), "Y2lhbyA="); 00256 assert_eq(encodeBase64("ciao c"), "Y2lhbyBj"); 00257 assert_eq(encodeBase64("ciao ci"), "Y2lhbyBjaQ=="); 00258 assert_eq(encodeBase64("ciao cia"), "Y2lhbyBjaWE="); 00259 assert_eq(encodeBase64("ciao ciao"), "Y2lhbyBjaWFv"); 00260 00261 assert_eq(decodeBase64(encodeBase64("")), ""); 00262 assert_eq(decodeBase64(encodeBase64("c")), "c"); 00263 assert_eq(decodeBase64(encodeBase64("ci")), "ci"); 00264 assert_eq(decodeBase64(encodeBase64("cia")), "cia"); 00265 assert_eq(decodeBase64(encodeBase64("ciao")), "ciao"); 00266 assert_eq(decodeBase64(encodeBase64("ciao ")), "ciao "); 00267 assert_eq(decodeBase64(encodeBase64("ciao c")), "ciao c"); 00268 assert_eq(decodeBase64(encodeBase64("ciao ci")), "ciao ci"); 00269 assert_eq(decodeBase64(encodeBase64("ciao cia")), "ciao cia"); 00270 assert_eq(decodeBase64(encodeBase64("ciao ciao")), "ciao ciao"); 00271 } 00272 00273 Test yaml() 00274 { 00275 string data = 00276 "Name: value\n" 00277 "Multiline: value1\n" 00278 " value2\n" 00279 " value3\n" 00280 "Multifield:\n" 00281 " Field1: val1\n" 00282 " Field2: val2\n" 00283 " continue val2\n" 00284 "\n" 00285 "Name: second record\n"; 00286 stringstream input(data, ios_base::in); 00287 str::YamlStream yamlStream; 00288 str::YamlStream::const_iterator i = yamlStream.begin(input); 00289 assert(i != yamlStream.end()); 00290 assert_eq(i->first, "Name"); 00291 assert_eq(i->second, "value"); 00292 00293 ++i; 00294 assert(i != yamlStream.end()); 00295 assert_eq(i->first, "Multiline"); 00296 assert_eq(i->second, 00297 "value1\n" 00298 "value2\n" 00299 " value3\n"); 00300 00301 ++i; 00302 assert(i != yamlStream.end()); 00303 assert_eq(i->first, "Multifield"); 00304 assert_eq(i->second, 00305 "Field1: val1\n" 00306 "Field2: val2\n" 00307 " continue val2\n"); 00308 00309 ++i; 00310 assert(i == yamlStream.end()); 00311 00312 i = yamlStream.begin(input); 00313 assert(i != yamlStream.end()); 00314 assert_eq(i->first, "Name"); 00315 assert_eq(i->second, "second record"); 00316 00317 ++i; 00318 assert(i == yamlStream.end()); 00319 00320 i = yamlStream.begin(input); 00321 assert(i == yamlStream.end()); 00322 } 00323 00324 Test yamlComments() 00325 { 00326 string data = 00327 "# comment\n" 00328 "Name: value # comment\n" 00329 "# comment\n" 00330 "Multiline: value1 # comment \n" 00331 " value2 # a\n" 00332 " value3#b\n" 00333 "\n" 00334 "# comment\n" 00335 "\n" 00336 "Name: second record\n"; 00337 stringstream input(data, ios_base::in); 00338 str::YamlStream yamlStream; 00339 str::YamlStream::const_iterator i = yamlStream.begin(input); 00340 assert(i != yamlStream.end()); 00341 assert_eq(i->first, "Name"); 00342 assert_eq(i->second, "value"); 00343 00344 ++i; 00345 assert(i != yamlStream.end()); 00346 assert_eq(i->first, "Multiline"); 00347 assert_eq(i->second, 00348 "value1\n" 00349 "value2 # a\n" 00350 " value3#b\n"); 00351 00352 ++i; 00353 assert(i == yamlStream.end()); 00354 00355 i = yamlStream.begin(input); 00356 assert(i != yamlStream.end()); 00357 assert_eq(i->first, "Name"); 00358 assert_eq(i->second, "second record"); 00359 00360 ++i; 00361 assert(i == yamlStream.end()); 00362 00363 i = yamlStream.begin(input); 00364 assert(i == yamlStream.end()); 00365 } 00366 }; 00367 00368 } 00369 00370 // vim:set ts=4 sw=4: