]> git.mxchange.org Git - simgear.git/blob - simgear/misc/strutils_test.cxx
Tiny HTTP client layer on top of NetChat - and CTest support for some SimGear tests.
[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     cout << "all tests passed successfully!" << endl;
57     return 0;
58 }