Use Boost.Test for strutils_test and add some md5/hex tests.
This commit is contained in:
@@ -53,10 +53,6 @@ add_executable(test_tabbed_values tabbed_values_test.cxx)
|
||||
add_test(tabbed_values ${EXECUTABLE_OUTPUT_PATH}/test_tabbed_values)
|
||||
target_link_libraries(test_tabbed_values ${TEST_LIBS})
|
||||
|
||||
add_executable(test_strings strutils_test.cxx )
|
||||
add_test(strings ${EXECUTABLE_OUTPUT_PATH}/test_strings)
|
||||
target_link_libraries(test_strings ${TEST_LIBS})
|
||||
|
||||
add_executable(test_streams sgstream_test.cxx )
|
||||
add_test(streams ${EXECUTABLE_OUTPUT_PATH}/test_streams)
|
||||
target_link_libraries(test_streams ${TEST_LIBS})
|
||||
@@ -77,6 +73,11 @@ add_boost_test(SVGpreserveAspectRatio
|
||||
LIBRARIES ${TEST_LIBS}
|
||||
)
|
||||
|
||||
add_boost_test(strutils
|
||||
SOURCES strutils_test.cxx
|
||||
LIBRARIES ${TEST_LIBS}
|
||||
)
|
||||
|
||||
add_boost_test(utf8tolatin1
|
||||
SOURCES utf8tolatin1_test.cxx
|
||||
LIBRARIES ${TEST_LIBS}
|
||||
|
||||
@@ -1,84 +1,80 @@
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
// Test harness.
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
/// Unit tests for function inside strutils package
|
||||
#define BOOST_TEST_MODULE misc
|
||||
#include <BoostTestTargetConfig.h>
|
||||
|
||||
#include <simgear/compiler.h>
|
||||
|
||||
#include <iostream>
|
||||
#include "strutils.hxx"
|
||||
|
||||
using std::cout;
|
||||
using std::cerr;
|
||||
using std::endl;
|
||||
namespace strutils = simgear::strutils;
|
||||
|
||||
using namespace simgear::strutils;
|
||||
|
||||
#define COMPARE(a, b) \
|
||||
if ((a) != (b)) { \
|
||||
cerr << "failed:" << #a << " != " << #b << endl; \
|
||||
exit(1); \
|
||||
}
|
||||
|
||||
#define VERIFY(a) \
|
||||
if (!(a)) { \
|
||||
cerr << "failed:" << #a << endl; \
|
||||
exit(1); \
|
||||
}
|
||||
|
||||
int main (int ac, char ** av)
|
||||
BOOST_AUTO_TEST_CASE( strutils_functions )
|
||||
{
|
||||
std::string a("abcd");
|
||||
COMPARE(strip(a), a);
|
||||
COMPARE(strip(" a "), "a");
|
||||
COMPARE(lstrip(" a "), "a ");
|
||||
COMPARE(rstrip("\ta "), "\ta");
|
||||
// check internal spacing is preserved
|
||||
COMPARE(strip("\t \na \t b\r \n "), "a \t b");
|
||||
|
||||
|
||||
VERIFY(starts_with("banana", "ban"));
|
||||
VERIFY(!starts_with("abanana", "ban"));
|
||||
VERIFY(starts_with("banana", "banana")); // pass - string starts with itself
|
||||
VERIFY(!starts_with("ban", "banana")); // fail - original string is prefix of
|
||||
|
||||
VERIFY(ends_with("banana", "ana"));
|
||||
VERIFY(ends_with("foo.text", ".text"));
|
||||
VERIFY(!ends_with("foo.text", ".html"));
|
||||
|
||||
COMPARE(simplify("\ta\t b \nc\n\r \r\n"), "a b c");
|
||||
COMPARE(simplify("The quick - brown dog!"), "The quick - brown dog!");
|
||||
COMPARE(simplify("\r\n \r\n \t \r"), "");
|
||||
|
||||
COMPARE(to_int("999"), 999);
|
||||
COMPARE(to_int("0000000"), 0);
|
||||
COMPARE(to_int("-10000"), -10000);
|
||||
|
||||
VERIFY(compare_versions("1.0.12", "1.1") < 0);
|
||||
VERIFY(compare_versions("1.1", "1.0.12") > 0);
|
||||
VERIFY(compare_versions("10.6.7", "10.6.7") == 0);
|
||||
VERIFY(compare_versions("2.0", "2.0.99") < 0);
|
||||
VERIFY(compare_versions("99", "99") == 0);
|
||||
VERIFY(compare_versions("99", "98") > 0);
|
||||
|
||||
// since we compare numerically, leasing zeros shouldn't matter
|
||||
VERIFY(compare_versions("0.06.7", "0.6.07") == 0);
|
||||
|
||||
string_list la = split("zero one two three four five");
|
||||
COMPARE(la[2], "two");
|
||||
COMPARE(la[5], "five");
|
||||
COMPARE(la.size(), 6);
|
||||
|
||||
string_list lb = split("alpha:beta:gamma:delta", ":", 2);
|
||||
COMPARE(lb.size(), 3);
|
||||
COMPARE(lb[0], "alpha");
|
||||
COMPARE(lb[1], "beta");
|
||||
COMPARE(lb[2], "gamma:delta");
|
||||
|
||||
std::string j = join(la, "&");
|
||||
COMPARE(j, "zero&one&two&three&four&five");
|
||||
std::string a("abcd");
|
||||
BOOST_CHECK_EQUAL(strutils::strip(a), a);
|
||||
BOOST_CHECK_EQUAL(strutils::strip(" a "), "a");
|
||||
BOOST_CHECK_EQUAL(strutils::lstrip(" a "), "a ");
|
||||
BOOST_CHECK_EQUAL(strutils::rstrip("\ta "), "\ta");
|
||||
|
||||
COMPARE(unescape("\\ \\n\\t\\x41\\117a"), " \n\tAOa");
|
||||
// check internal spacing is preserved
|
||||
BOOST_CHECK_EQUAL(strutils::strip("\t \na \t b\r \n "), "a \t b");
|
||||
|
||||
cout << "all tests passed successfully!" << endl;
|
||||
return 0;
|
||||
|
||||
BOOST_CHECK(strutils::starts_with("banana", "ban"));
|
||||
BOOST_CHECK(!strutils::starts_with("abanana", "ban"));
|
||||
BOOST_CHECK(strutils::starts_with("banana", "banana")); // pass - string starts with itself
|
||||
BOOST_CHECK(!strutils::starts_with("ban", "banana")); // fail - original string is prefix of
|
||||
|
||||
BOOST_CHECK(strutils::ends_with("banana", "ana"));
|
||||
BOOST_CHECK(strutils::ends_with("foo.text", ".text"));
|
||||
BOOST_CHECK(!strutils::ends_with("foo.text", ".html"));
|
||||
|
||||
BOOST_CHECK_EQUAL(strutils::simplify("\ta\t b \nc\n\r \r\n"), "a b c");
|
||||
BOOST_CHECK_EQUAL(strutils::simplify("The quick - brown dog!"), "The quick - brown dog!");
|
||||
BOOST_CHECK_EQUAL(strutils::simplify("\r\n \r\n \t \r"), "");
|
||||
|
||||
BOOST_CHECK_EQUAL(strutils::to_int("999"), 999);
|
||||
BOOST_CHECK_EQUAL(strutils::to_int("0000000"), 0);
|
||||
BOOST_CHECK_EQUAL(strutils::to_int("-10000"), -10000);
|
||||
|
||||
string_list la = strutils::split("zero one two three four five");
|
||||
BOOST_CHECK_EQUAL(la[2], "two");
|
||||
BOOST_CHECK_EQUAL(la[5], "five");
|
||||
BOOST_CHECK_EQUAL(la.size(), 6);
|
||||
|
||||
string_list lb = strutils::split("alpha:beta:gamma:delta", ":", 2);
|
||||
BOOST_CHECK_EQUAL(lb.size(), 3);
|
||||
BOOST_CHECK_EQUAL(lb[0], "alpha");
|
||||
BOOST_CHECK_EQUAL(lb[1], "beta");
|
||||
BOOST_CHECK_EQUAL(lb[2], "gamma:delta");
|
||||
|
||||
std::string j = strutils::join(la, "&");
|
||||
BOOST_CHECK_EQUAL(j, "zero&one&two&three&four&five");
|
||||
|
||||
BOOST_CHECK_EQUAL(strutils::unescape("\\ \\n\\t\\x41\\117a"), " \n\tAOa");
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE( compare_versions )
|
||||
{
|
||||
BOOST_CHECK_LT(strutils::compare_versions("1.0.12", "1.1"), 0);
|
||||
BOOST_CHECK_GT(strutils::compare_versions("1.1", "1.0.12"), 0);
|
||||
BOOST_CHECK_EQUAL(strutils::compare_versions("10.6.7", "10.6.7"), 0);
|
||||
BOOST_CHECK_LT(strutils::compare_versions("2.0", "2.0.99"), 0);
|
||||
BOOST_CHECK_EQUAL(strutils::compare_versions("99", "99"), 0);
|
||||
BOOST_CHECK_GT(strutils::compare_versions("99", "98"), 0);
|
||||
|
||||
// since we compare numerically, leasing zeros shouldn't matter
|
||||
BOOST_CHECK_EQUAL(strutils::compare_versions("0.06.7", "0.6.07"), 0);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE( md5_hex )
|
||||
{
|
||||
// hex encoding
|
||||
unsigned char raw_data[] = {0x0f, 0x1a, 0xbc, 0xd2, 0xe3, 0x45, 0x67, 0x89};
|
||||
const std::string& hex_data =
|
||||
strutils::encodeHex(raw_data, sizeof(raw_data)/sizeof(raw_data[0]));
|
||||
BOOST_REQUIRE_EQUAL(hex_data, "0f1abcd2e3456789");
|
||||
BOOST_REQUIRE_EQUAL(strutils::encodeHex("abcde"), "6162636465");
|
||||
|
||||
// md5
|
||||
BOOST_CHECK_EQUAL(strutils::md5("test"), "098f6bcd4621d373cade4e832627b4f6");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user