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