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