1 /// Unit tests for function inside strutils package
2 #define BOOST_TEST_MODULE misc
3 #include <BoostTestTargetConfig.h>
6 #include <stdlib.h> // _set_errno() on Windows
8 #include <fstream> // std::ifstream
9 #include <simgear/compiler.h>
10 #include "strutils.hxx"
12 namespace strutils = simgear::strutils;
14 BOOST_AUTO_TEST_CASE( strutils_functions )
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");
22 // check internal spacing is preserved
23 BOOST_CHECK_EQUAL(strutils::strip("\t \na \t b\r \n "), "a \t b");
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
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"));
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"), "");
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);
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);
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");
54 std::string j = strutils::join(la, "&");
55 BOOST_CHECK_EQUAL(j, "zero&one&two&three&four&five");
57 BOOST_CHECK_EQUAL(strutils::unescape("\\ \\n\\t\\x41\\117a"), " \n\tAOa");
60 BOOST_AUTO_TEST_CASE( compare_versions )
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);
69 // since we compare numerically, leasing zeros shouldn't matter
70 BOOST_CHECK_EQUAL(strutils::compare_versions("0.06.7", "0.6.07"), 0);
73 BOOST_AUTO_TEST_CASE( md5_hex )
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");
83 BOOST_CHECK_EQUAL(strutils::md5("test"), "098f6bcd4621d373cade4e832627b4f6");
86 BOOST_AUTO_TEST_CASE( error_string )
88 #if defined(SG_WINDOWS)
94 std::ifstream f("/\\/non-existent/file/a8f7bz97-3ffe-4f5b-b8db-38ccurJL-");
96 #if defined(SG_WINDOWS)
97 errno_t saved_errno = errno;
99 int saved_errno = errno;
102 BOOST_CHECK(!f.is_open());
103 BOOST_CHECK_NE(saved_errno, 0);
104 BOOST_CHECK_GT(strutils::error_string(saved_errno).size(), 0);