1 ////////////////////////////////////////////////////////////////////////
3 ////////////////////////////////////////////////////////////////////////
5 #include <simgear/compiler.h>
8 #include "strutils.hxx"
14 using namespace simgear::strutils;
16 #define COMPARE(a, b) \
18 cerr << "failed:" << #a << " != " << #b << endl; \
24 cerr << "failed:" << #a << endl; \
28 int main (int ac, char ** av)
30 std::string a("abcd");
32 COMPARE(strip(" a "), "a");
33 COMPARE(lstrip(" a "), "a ");
34 COMPARE(rstrip("\ta "), "\ta");
35 // check internal spacing is preserved
36 COMPARE(strip("\t \na \t b\r \n "), "a \t b");
39 VERIFY(starts_with("banana", "ban"));
40 VERIFY(!starts_with("abanana", "ban"));
41 VERIFY(starts_with("banana", "banana")); // pass - string starts with itself
42 VERIFY(!starts_with("ban", "banana")); // fail - original string is prefix of
44 VERIFY(ends_with("banana", "ana"));
45 VERIFY(ends_with("foo.text", ".text"));
46 VERIFY(!ends_with("foo.text", ".html"));
48 COMPARE(simplify("\ta\t b \nc\n\r \r\n"), "a b c");
49 COMPARE(simplify("The quick - brown dog!"), "The quick - brown dog!");
50 COMPARE(simplify("\r\n \r\n \t \r"), "");
52 COMPARE(to_int("999"), 999);
53 COMPARE(to_int("0000000"), 0);
54 COMPARE(to_int("-10000"), -10000);
56 VERIFY(compare_versions("1.0.12", "1.1") < 0);
57 VERIFY(compare_versions("1.1", "1.0.12") > 0);
58 VERIFY(compare_versions("10.6.7", "10.6.7") == 0);
59 VERIFY(compare_versions("2.0", "2.0.99") < 0);
60 VERIFY(compare_versions("99", "99") == 0);
61 VERIFY(compare_versions("99", "98") > 0);
63 // since we compare numerically, leasing zeros shouldn't matter
64 VERIFY(compare_versions("0.06.7", "0.6.07") == 0);
66 string_list la = split("zero one two three four five");
67 COMPARE(la[2], "two");
68 COMPARE(la[5], "five");
69 COMPARE(la.size(), 6);
71 string_list lb = split("alpha:beta:gamma:delta", ":", 2);
72 COMPARE(lb.size(), 3);
73 COMPARE(lb[0], "alpha");
74 COMPARE(lb[1], "beta");
75 COMPARE(lb[2], "gamma:delta");
77 std::string j = join(la, "&");
78 COMPARE(j, "zero&one&two&three&four&five");
80 cout << "all tests passed successfully!" << endl;