]> git.mxchange.org Git - simgear.git/blob - simgear/misc/strutils_test.cxx
Use the original filename for the compressed image message.
[simgear.git] / simgear / misc / strutils_test.cxx
1 ////////////////////////////////////////////////////////////////////////
2 // Test harness.
3 ////////////////////////////////////////////////////////////////////////
4
5 #include <simgear/compiler.h>
6
7 #include <iostream>
8 #include "strutils.hxx"
9
10 using std::cout;
11 using std::cerr;
12 using std::endl;
13
14 using namespace simgear::strutils;
15
16 #define COMPARE(a, b) \
17     if ((a) != (b))  { \
18         cerr << "failed:" << #a << " != " << #b << endl; \
19         exit(1); \
20     }
21
22 #define VERIFY(a) \
23     if (!(a))  { \
24         cerr << "failed:" << #a << endl; \
25         exit(1); \
26     }
27     
28 int main (int ac, char ** av)
29 {
30     std::string a("abcd");
31     COMPARE(strip(a), a);
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");
37     
38     
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 
43     
44     VERIFY(ends_with("banana", "ana"));
45     VERIFY(ends_with("foo.text", ".text"));
46     VERIFY(!ends_with("foo.text", ".html"));
47     
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"), "");
51     
52     COMPARE(to_int("999"), 999);
53     COMPARE(to_int("0000000"), 0);
54     COMPARE(to_int("-10000"), -10000);
55     
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);
62     
63 // since we compare numerically, leasing zeros shouldn't matter
64     VERIFY(compare_versions("0.06.7", "0.6.07") == 0);
65     
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);
70     
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");
76     
77     std::string j = join(la, "&");
78     COMPARE(j, "zero&one&two&three&four&five");
79     
80     cout << "all tests passed successfully!" << endl;
81     return 0;
82 }