string.test.h
Go to the documentation of this file.
/* -*- C++ -*- (c) 2007 Petr Rockai <me@mornfall.net>
(c) 2007 Enrico Zini <enrico@enricozini.org> */
#include <wibble/test.h>
#include <wibble/string.h>
#include <wibble/list.h>
namespace {
using namespace std;
using namespace wibble;
struct TestString {
Test fmt()
{
assert_eq(str::fmt(5), "5");
assert_eq(str::fmt(5.123), "5.123");
assert_eq(str::fmtf("ciao"), "ciao");
}
Test fmtSet()
{
std::set< int > a;
assert_eq(str::fmt(a), "{}");
a.insert( a.begin(), 2 );
assert_eq(str::fmt(a), "{ 2 }");
a.insert( a.begin(), 5 );
assert_eq(str::fmt(a), "{ 2, 5 }");
a.insert( a.begin(), 1 );
assert_eq(str::fmt(a), "{ 1, 2, 5 }");
}
Test fmtVec()
{
std::vector< int > a;
assert_eq(str::fmt(a), "[]");
a.push_back( 2 );
assert_eq(str::fmt(a), "[ 2 ]");
a.push_back( 5 );
assert_eq(str::fmt(a), "[ 2, 5 ]");
a.push_back( 1 );
assert_eq(str::fmt(a), "[ 2, 5, 1 ]");
}
Test fmtList()
{
assert_eq( str::fmt( list::Empty< int >() ), "[]" );
assert_eq( str::fmt( list::singular( 0 ) ), "[ 0 ]" );
assert_eq( str::fmt( list::append(
list::singular( 0 ),
list::singular( 2 ) ) ), "[ 0, 2 ]" );
}
Test basename()
{
assert_eq(str::basename("ciao"), "ciao");
assert_eq(str::basename("a/ciao"), "ciao");
assert_eq(str::basename("a/b/c/c/d/e/ciao"), "ciao");
assert_eq(str::basename("/ciao"), "ciao");
}
Test dirname()
{
assert_eq(str::dirname("ciao"), "");
assert_eq(str::dirname("a/ciao"), "a");
assert_eq(str::dirname("a/b/c/c/d/e/ciao"), "a/b/c/c/d/e");
assert_eq(str::dirname("/a/ciao"), "/a");
assert_eq(str::dirname("/ciao"), "/");
}
Test trim()
{
assert_eq(str::trim(" "), "");
assert_eq(str::trim(" c "), "c");
assert_eq(str::trim("ciao"), "ciao");
assert_eq(str::trim(" ciao"), "ciao");
assert_eq(str::trim(" ciao"), "ciao");
assert_eq(str::trim("ciao "), "ciao");
assert_eq(str::trim("ciao "), "ciao");
assert_eq(str::trim(" ciao "), "ciao");
assert_eq(str::trim(" ciao "), "ciao");
}
Test trim2()
{
assert_eq(str::trim(string("ciao"), ::isalpha), "");
assert_eq(str::trim(" ", ::isalpha), " ");
}
Test tolower()
{
assert_eq(str::tolower("ciao"), "ciao");
assert_eq(str::tolower("CIAO"), "ciao");
assert_eq(str::tolower("Ciao"), "ciao");
assert_eq(str::tolower("cIAO"), "ciao");
}
Test toupper()
{
assert_eq(str::toupper("ciao"), "CIAO");
assert_eq(str::toupper("CIAO"), "CIAO");
assert_eq(str::toupper("Ciao"), "CIAO");
assert_eq(str::toupper("cIAO"), "CIAO");
}
Test ucfirst()
{
assert_eq(str::ucfirst("ciao"), "Ciao");
assert_eq(str::ucfirst("CIAO"), "Ciao");
assert_eq(str::ucfirst("Ciao"), "Ciao");
assert_eq(str::ucfirst("cIAO"), "Ciao");
}
// Check startsWith
Test startsWith()
{
assert(str::startsWith("ciao", "ci"));
assert(str::startsWith("ciao", ""));
assert(str::startsWith("ciao", "ciao"));
assert(!str::startsWith("ciao", "ciaoa"));
assert(!str::startsWith("ciao", "i"));
}
Test endsWith()
{
assert(str::endsWith("ciao", "ao"));
assert(str::endsWith("ciao", ""));
assert(str::endsWith("ciao", "ciao"));
assert(!str::endsWith("ciao", "aciao"));
assert(!str::endsWith("ciao", "a"));
}
Test joinpath()
{
assert_eq(str::joinpath("a", "b"), "a/b");
assert_eq(str::joinpath("a/", "b"), "a/b");
assert_eq(str::joinpath("a", "/b"), "a/b");
assert_eq(str::joinpath("a/", "/b"), "a/b");
}
Test appendpath()
{
assert_eq(str::appendpath("a", "b"), "a/b");
assert_eq(str::appendpath("a/", "b"), "a/b");
assert_eq(str::appendpath("a", "/b"), "/b");
assert_eq(str::appendpath("a/", "/b"), "/b");
assert_eq(str::appendpath("/a", "b"), "/a/b");
assert_eq(str::appendpath("/a/", "b"), "/a/b");
assert_eq(str::appendpath("/a", "/b"), "/b");
assert_eq(str::appendpath("/a/", "/b"), "/b");
}
Test urlencode()
{
assert_eq(str::urlencode(""), "");
assert_eq(str::urlencode("antani"), "antani");
assert_eq(str::urlencode("a b c"), "a%20b%20c");
assert_eq(str::urlencode("a "), "a%20");
assert_eq(str::urldecode(""), "");
assert_eq(str::urldecode("antani"), "antani");
assert_eq(str::urldecode("a%20b"), "a b");
assert_eq(str::urldecode("a%20"), "a ");
assert_eq(str::urldecode("a%2"), "a");
assert_eq(str::urldecode("a%"), "a");
assert_eq(str::urldecode(str::urlencode("àá☣☢☠!@#$%^&*(\")/A")), "àá☣☢☠!@#$%^&*(\")/A");
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");
}
Test split1()
{
string val = "";
str::Split split("/", val);
str::Split::const_iterator i = split.begin();
assert(i == split.end());
}
Test split2()
{
string val = "foo";
str::Split split("/", val);
str::Split::const_iterator i = split.begin();
assert(i != split.end());
assert_eq(*i, "foo");
assert_eq(i.remainder(), "");
++i;
assert(i == split.end());
}
Test split3()
{
string val = "foo";
str::Split split("", val);
str::Split::const_iterator i = split.begin();
assert(i != split.end());
assert_eq(*i, "f");
assert_eq(i.remainder(), "oo");
++i;
assert_eq(*i, "o");
assert_eq(i.remainder(), "o");
++i;
assert_eq(*i, "o");
assert_eq(i.remainder(), "");
++i;
assert(i == split.end());
}
Test split4()
{
string val = "/a//foo/";
str::Split split("/", val);
str::Split::const_iterator i = split.begin();
assert(i != split.end());
assert_eq(*i, "");
assert_eq(i.remainder(), "a//foo/");
++i;
assert(i != split.end());
assert_eq(*i, "a");
assert_eq(i.remainder(), "/foo/");
++i;
assert(i != split.end());
assert_eq(*i, "");
assert_eq(i.remainder(), "foo/");
++i;
assert(i != split.end());
assert_eq(*i, "foo");
assert_eq(i.remainder(), "");
++i;
assert(i == split.end());
}
Test join()
{
string val = "/a//foo/";
str::Split split("/", val);
string res = str::join(split.begin(), split.end(), ":");
assert_eq(res, ":a::foo");
}
Test normpath()
{
assert_eq(str::normpath(""), ".");
assert_eq(str::normpath("/"), "/");
assert_eq(str::normpath("foo"), "foo");
assert_eq(str::normpath("foo/"), "foo");
assert_eq(str::normpath("/foo"), "/foo");
assert_eq(str::normpath("foo/bar"), "foo/bar");
assert_eq(str::normpath("foo/./bar"), "foo/bar");
assert_eq(str::normpath("././././foo/./././bar/././././"), "foo/bar");
assert_eq(str::normpath("/../../../../../foo"), "/foo");
assert_eq(str::normpath("foo/../foo/../foo/../foo/../"), ".");
assert_eq(str::normpath("foo//bar"), "foo/bar");
assert_eq(str::normpath("foo/./bar"), "foo/bar");
assert_eq(str::normpath("foo/foo/../bar"), "foo/bar");
}
Test base64()
{
using namespace str;
assert_eq(encodeBase64(""), "");
assert_eq(encodeBase64("c"), "Yw==");
assert_eq(encodeBase64("ci"), "Y2k=");
assert_eq(encodeBase64("cia"), "Y2lh");
assert_eq(encodeBase64("ciao"), "Y2lhbw==");
assert_eq(encodeBase64("ciao "), "Y2lhbyA=");
assert_eq(encodeBase64("ciao c"), "Y2lhbyBj");
assert_eq(encodeBase64("ciao ci"), "Y2lhbyBjaQ==");
assert_eq(encodeBase64("ciao cia"), "Y2lhbyBjaWE=");
assert_eq(encodeBase64("ciao ciao"), "Y2lhbyBjaWFv");
assert_eq(decodeBase64(encodeBase64("")), "");
assert_eq(decodeBase64(encodeBase64("c")), "c");
assert_eq(decodeBase64(encodeBase64("ci")), "ci");
assert_eq(decodeBase64(encodeBase64("cia")), "cia");
assert_eq(decodeBase64(encodeBase64("ciao")), "ciao");
assert_eq(decodeBase64(encodeBase64("ciao ")), "ciao ");
assert_eq(decodeBase64(encodeBase64("ciao c")), "ciao c");
assert_eq(decodeBase64(encodeBase64("ciao ci")), "ciao ci");
assert_eq(decodeBase64(encodeBase64("ciao cia")), "ciao cia");
assert_eq(decodeBase64(encodeBase64("ciao ciao")), "ciao ciao");
}
Test yaml()
{
string data =
"Name: value\n"
"Multiline: value1\n"
" value2\n"
" value3\n"
"Multifield:\n"
" Field1: val1\n"
" Field2: val2\n"
" continue val2\n"
"\n"
"Name: second record\n";
stringstream input(data, ios_base::in);
str::YamlStream yamlStream;
str::YamlStream::const_iterator i = yamlStream.begin(input);
assert(i != yamlStream.end());
assert_eq(i->first, "Name");
assert_eq(i->second, "value");
++i;
assert(i != yamlStream.end());
assert_eq(i->first, "Multiline");
assert_eq(i->second,
"value1\n"
"value2\n"
" value3\n");
++i;
assert(i != yamlStream.end());
assert_eq(i->first, "Multifield");
assert_eq(i->second,
"Field1: val1\n"
"Field2: val2\n"
" continue val2\n");
++i;
assert(i == yamlStream.end());
i = yamlStream.begin(input);
assert(i != yamlStream.end());
assert_eq(i->first, "Name");
assert_eq(i->second, "second record");
++i;
assert(i == yamlStream.end());
i = yamlStream.begin(input);
assert(i == yamlStream.end());
}
Test yamlComments()
{
string data =
"# comment\n"
"Name: value # comment\n"
"# comment\n"
"Multiline: value1 # comment \n"
" value2 # a\n"
" value3#b\n"
"\n"
"# comment\n"
"\n"
"Name: second record\n";
stringstream input(data, ios_base::in);
str::YamlStream yamlStream;
str::YamlStream::const_iterator i = yamlStream.begin(input);
assert(i != yamlStream.end());
assert_eq(i->first, "Name");
assert_eq(i->second, "value");
++i;
assert(i != yamlStream.end());
assert_eq(i->first, "Multiline");
assert_eq(i->second,
"value1\n"
"value2 # a\n"
" value3#b\n");
++i;
assert(i == yamlStream.end());
i = yamlStream.begin(input);
assert(i != yamlStream.end());
assert_eq(i->first, "Name");
assert_eq(i->second, "second record");
++i;
assert(i == yamlStream.end());
i = yamlStream.begin(input);
assert(i == yamlStream.end());
}
Test c_escape_unescape() {
size_t len;
assert_eq(str::c_unescape("cia\\x00o", len), string("cia\0o", 5));
assert_eq(len, 8);
assert_eq(str::c_escape(string("cia\0o", 5)), "cia\\x00o");
}
};
}
// vim:set ts=4 sw=4:
wibble::str::Split::const_iterator
Definition: string.h:322
wibble::str::Split::const_iterator::remainder
std::string remainder() const
Definition: string.h:363
wibble::str::Split
Split a string where a given substring is found.
Definition: string.h:315
wibble::str::YamlStream::const_iterator
Definition: string.h:440
wibble::str::YamlStream
Parse a record of Yaml-style field: value couples.
Definition: string.h:436
wibble::str::YamlStream::end
const_iterator end()
Definition: string.h:470
wibble::str::YamlStream::begin
const_iterator begin(std::istream &in)
Definition: string.h:469
list.h
wibble::list::singular
Singular< X > singular(const X &x)
Definition: list.h:331
wibble::list::append
Append< X, Y > append(const X &x, const Y &y)
Definition: list.h:336
wibble::str::join
std::string join(const ITER &begin, const ITER &end, const std::string &sep=", ")
Definition: string.h:406
wibble::str::trim
std::string trim(const std::string &str, const FUN &classifier)
Return the substring of 'str' without all leading and trailing characters for which 'classifier' retu...
Definition: string.h:187
wibble::str::normpath
std::string normpath(const std::string &pathname)
Normalise a pathname.
Definition: string.cpp:133
wibble::str::urlencode
std::string urlencode(const std::string &str)
Urlencode a string.
Definition: string.cpp:160
wibble::str::c_unescape
std::string c_unescape(const std::string &str, size_t &lenParsed)
Unescape a C string, stopping at the first double quotes or at the end of the string.
Definition: string.cpp:424
wibble::str::dirname
std::string dirname(const std::string &pathname)
Given a pathname, return the directory name without the file name.
Definition: string.h:134
wibble::str::toupper
std::string toupper(const std::string &str)
Convert a string to uppercase.
Definition: string.h:228
wibble::str::urldecode
std::string urldecode(const std::string &str)
Decode an urlencoded string.
Definition: string.cpp:178
wibble::str::endsWith
bool endsWith(const std::string &str, const std::string &part)
Check if a string ends with the given substring.
Definition: string.h:162
wibble::str::encodeBase64
std::string encodeBase64(const std::string &str)
Encode a string in Base64.
Definition: string.cpp:208
wibble::str::appendpath
std::string appendpath(const std::string &path1, const std::string &path2)
Definition: string.h:277
wibble::str::decodeBase64
std::string decodeBase64(const std::string &str)
Decode a string encoded in Base64.
Definition: string.cpp:241
wibble::str::startsWith
bool startsWith(const std::string &str, const std::string &part)
Check if a string starts with the given substring.
Definition: string.h:154
wibble::str::c_escape
std::string c_escape(const std::string &str)
Escape the string so it can safely used as a C string inside double quotes.
Definition: string.cpp:400
wibble::str::ucfirst
std::string ucfirst(const std::string &str)
Return the same string, with the first character uppercased.
Definition: string.h:248
wibble::str::joinpath
std::string joinpath(const std::string &path1, const std::string &path2)
Join two paths, adding slashes when appropriate.
Definition: string.h:257
wibble::str::fmtf
std::string fmtf(const char *f,...)
Definition: string.cpp:113
wibble::str::tolower
std::string tolower(const std::string &str)
Convert a string to lowercase.
Definition: string.h:238
wibble::str::basename
std::string basename(const std::string &pathname)
Given a pathname, return the file name without its path.
Definition: string.h:124
wibble::str::fmt
std::string fmt(const char *f,...)
Definition: string.cpp:123
wibble
Definition: amorph.h:17
string.h
wibble::list::Empty
Definition: list.h:284
test.h
Test
void Test
Definition: test.h:178
assert_eq
#define assert_eq(x, y)
Definition: test.h:33
assert
#define assert(x)
Definition: test.h:30