1 /// Unit tests for function inside strutils package
2 #define BOOST_TEST_MODULE misc
3 #include <BoostTestTargetConfig.h>
5 #include <simgear/compiler.h>
6 #include "strutils.hxx"
8 namespace strutils = simgear::strutils;
10 BOOST_AUTO_TEST_CASE( strutils_functions )
12 std::string a("abcd");
13 BOOST_CHECK_EQUAL(strutils::strip(a), a);
14 BOOST_CHECK_EQUAL(strutils::strip(" a "), "a");
15 BOOST_CHECK_EQUAL(strutils::lstrip(" a "), "a ");
16 BOOST_CHECK_EQUAL(strutils::rstrip("\ta "), "\ta");
18 // check internal spacing is preserved
19 BOOST_CHECK_EQUAL(strutils::strip("\t \na \t b\r \n "), "a \t b");
22 BOOST_CHECK(strutils::starts_with("banana", "ban"));
23 BOOST_CHECK(!strutils::starts_with("abanana", "ban"));
24 BOOST_CHECK(strutils::starts_with("banana", "banana")); // pass - string starts with itself
25 BOOST_CHECK(!strutils::starts_with("ban", "banana")); // fail - original string is prefix of
27 BOOST_CHECK(strutils::ends_with("banana", "ana"));
28 BOOST_CHECK(strutils::ends_with("foo.text", ".text"));
29 BOOST_CHECK(!strutils::ends_with("foo.text", ".html"));
31 BOOST_CHECK_EQUAL(strutils::simplify("\ta\t b \nc\n\r \r\n"), "a b c");
32 BOOST_CHECK_EQUAL(strutils::simplify("The quick - brown dog!"), "The quick - brown dog!");
33 BOOST_CHECK_EQUAL(strutils::simplify("\r\n \r\n \t \r"), "");
35 BOOST_CHECK_EQUAL(strutils::to_int("999"), 999);
36 BOOST_CHECK_EQUAL(strutils::to_int("0000000"), 0);
37 BOOST_CHECK_EQUAL(strutils::to_int("-10000"), -10000);
39 string_list la = strutils::split("zero one two three four five");
40 BOOST_CHECK_EQUAL(la[2], "two");
41 BOOST_CHECK_EQUAL(la[5], "five");
42 BOOST_CHECK_EQUAL(la.size(), 6);
44 string_list lb = strutils::split("alpha:beta:gamma:delta", ":", 2);
45 BOOST_CHECK_EQUAL(lb.size(), 3);
46 BOOST_CHECK_EQUAL(lb[0], "alpha");
47 BOOST_CHECK_EQUAL(lb[1], "beta");
48 BOOST_CHECK_EQUAL(lb[2], "gamma:delta");
50 std::string j = strutils::join(la, "&");
51 BOOST_CHECK_EQUAL(j, "zero&one&two&three&four&five");
53 BOOST_CHECK_EQUAL(strutils::unescape("\\ \\n\\t\\x41\\117a"), " \n\tAOa");
56 BOOST_AUTO_TEST_CASE( compare_versions )
58 BOOST_CHECK_LT(strutils::compare_versions("1.0.12", "1.1"), 0);
59 BOOST_CHECK_GT(strutils::compare_versions("1.1", "1.0.12"), 0);
60 BOOST_CHECK_EQUAL(strutils::compare_versions("10.6.7", "10.6.7"), 0);
61 BOOST_CHECK_LT(strutils::compare_versions("2.0", "2.0.99"), 0);
62 BOOST_CHECK_EQUAL(strutils::compare_versions("99", "99"), 0);
63 BOOST_CHECK_GT(strutils::compare_versions("99", "98"), 0);
65 // since we compare numerically, leasing zeros shouldn't matter
66 BOOST_CHECK_EQUAL(strutils::compare_versions("0.06.7", "0.6.07"), 0);
69 BOOST_AUTO_TEST_CASE( md5_hex )
72 unsigned char raw_data[] = {0x0f, 0x1a, 0xbc, 0xd2, 0xe3, 0x45, 0x67, 0x89};
73 const std::string& hex_data =
74 strutils::encodeHex(raw_data, sizeof(raw_data)/sizeof(raw_data[0]));
75 BOOST_REQUIRE_EQUAL(hex_data, "0f1abcd2e3456789");
76 BOOST_REQUIRE_EQUAL(strutils::encodeHex("abcde"), "6162636465");
79 BOOST_CHECK_EQUAL(strutils::md5("test"), "098f6bcd4621d373cade4e832627b4f6");