]> git.mxchange.org Git - simgear.git/blob - simgear/misc/strutils_test.cxx
Fix missing include in simgear/misc/strutils_test.cxx
[simgear.git] / simgear / misc / strutils_test.cxx
1 /// Unit tests for function inside strutils package
2 #define BOOST_TEST_MODULE misc
3 #include <BoostTestTargetConfig.h>
4
5 #include <errno.h>
6 #include <stdlib.h>             // _set_errno() on Windows
7 #include <string>
8 #include <fstream>              // std::ifstream
9 #include <simgear/compiler.h>
10 #include "strutils.hxx"
11
12 namespace strutils = simgear::strutils;
13
14 BOOST_AUTO_TEST_CASE( strutils_functions )
15 {
16   std::string a("abcd");
17   BOOST_CHECK_EQUAL(strutils::strip(a), a);
18   BOOST_CHECK_EQUAL(strutils::strip(" a "), "a");
19   BOOST_CHECK_EQUAL(strutils::lstrip(" a  "), "a  ");
20   BOOST_CHECK_EQUAL(strutils::rstrip("\ta "), "\ta");
21
22   // check internal spacing is preserved
23   BOOST_CHECK_EQUAL(strutils::strip("\t \na \t b\r \n "), "a \t b");
24
25
26   BOOST_CHECK(strutils::starts_with("banana", "ban"));
27   BOOST_CHECK(!strutils::starts_with("abanana", "ban"));
28   BOOST_CHECK(strutils::starts_with("banana", "banana")); // pass - string starts with itself
29   BOOST_CHECK(!strutils::starts_with("ban", "banana")); // fail - original string is prefix of
30
31   BOOST_CHECK(strutils::ends_with("banana", "ana"));
32   BOOST_CHECK(strutils::ends_with("foo.text", ".text"));
33   BOOST_CHECK(!strutils::ends_with("foo.text", ".html"));
34
35   BOOST_CHECK_EQUAL(strutils::simplify("\ta\t b  \nc\n\r \r\n"), "a b c");
36   BOOST_CHECK_EQUAL(strutils::simplify("The quick  - brown dog!"), "The quick - brown dog!");
37   BOOST_CHECK_EQUAL(strutils::simplify("\r\n  \r\n   \t  \r"), "");
38
39   BOOST_CHECK_EQUAL(strutils::to_int("999"), 999);
40   BOOST_CHECK_EQUAL(strutils::to_int("0000000"), 0);
41   BOOST_CHECK_EQUAL(strutils::to_int("-10000"), -10000);
42
43   string_list la = strutils::split("zero one two three four five");
44   BOOST_CHECK_EQUAL(la[2], "two");
45   BOOST_CHECK_EQUAL(la[5], "five");
46   BOOST_CHECK_EQUAL(la.size(), 6);
47
48   string_list lb = strutils::split("alpha:beta:gamma:delta", ":", 2);
49   BOOST_CHECK_EQUAL(lb.size(), 3);
50   BOOST_CHECK_EQUAL(lb[0], "alpha");
51   BOOST_CHECK_EQUAL(lb[1], "beta");
52   BOOST_CHECK_EQUAL(lb[2], "gamma:delta");
53
54   std::string j = strutils::join(la, "&");
55   BOOST_CHECK_EQUAL(j, "zero&one&two&three&four&five");
56
57   BOOST_CHECK_EQUAL(strutils::unescape("\\ \\n\\t\\x41\\117a"), " \n\tAOa");
58 }
59
60 BOOST_AUTO_TEST_CASE( compare_versions )
61 {
62   BOOST_CHECK_LT(strutils::compare_versions("1.0.12", "1.1"), 0);
63   BOOST_CHECK_GT(strutils::compare_versions("1.1", "1.0.12"), 0);
64   BOOST_CHECK_EQUAL(strutils::compare_versions("10.6.7", "10.6.7"), 0);
65   BOOST_CHECK_LT(strutils::compare_versions("2.0", "2.0.99"), 0);
66   BOOST_CHECK_EQUAL(strutils::compare_versions("99", "99"), 0);
67   BOOST_CHECK_GT(strutils::compare_versions("99", "98"), 0);
68
69   // since we compare numerically, leasing zeros shouldn't matter
70   BOOST_CHECK_EQUAL(strutils::compare_versions("0.06.7", "0.6.07"), 0);
71 }
72
73 BOOST_AUTO_TEST_CASE( md5_hex )
74 {
75   // hex encoding
76   unsigned char raw_data[] = {0x0f, 0x1a, 0xbc, 0xd2, 0xe3, 0x45, 0x67, 0x89};
77   const std::string& hex_data =
78     strutils::encodeHex(raw_data, sizeof(raw_data)/sizeof(raw_data[0]));
79   BOOST_REQUIRE_EQUAL(hex_data, "0f1abcd2e3456789");
80   BOOST_REQUIRE_EQUAL(strutils::encodeHex("abcde"), "6162636465");
81
82   // md5
83   BOOST_CHECK_EQUAL(strutils::md5("test"), "098f6bcd4621d373cade4e832627b4f6");
84 }
85
86 BOOST_AUTO_TEST_CASE( error_string )
87 {
88 #if defined(SG_WINDOWS)
89   _set_errno(0);
90 #else
91   errno = 0;
92 #endif
93
94   std::ifstream f("/\\/non-existent/file/a8f7bz97-3ffe-4f5b-b8db-38ccurJL-");
95
96 #if defined(SG_WINDOWS)
97   errno_t saved_errno = errno;
98 #else
99   int saved_errno = errno;
100 #endif
101
102   BOOST_CHECK(!f.is_open());
103   BOOST_CHECK_NE(saved_errno, 0);
104   BOOST_CHECK_GT(strutils::error_string(saved_errno).size(), 0);
105 }