Tiny HTTP client layer on top of NetChat - and CTest support for some SimGear tests.
This commit is contained in:
@@ -33,3 +33,11 @@ set(SOURCES
|
||||
)
|
||||
|
||||
simgear_component(misc misc "${SOURCES}" "${HEADERS}")
|
||||
|
||||
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 sgmisc)
|
||||
|
||||
add_executable(test_strings strutils_test.cxx )
|
||||
add_test(test_strings ${EXECUTABLE_OUTPUT_PATH}/test_strings)
|
||||
target_link_libraries(test_strings sgmisc)
|
||||
|
||||
@@ -213,5 +213,41 @@ namespace simgear {
|
||||
return (n != string::npos) && (n == s.length() - substr.length());
|
||||
}
|
||||
|
||||
string simplify(const string& s)
|
||||
{
|
||||
string result; // reserve size of 's'?
|
||||
string::const_iterator it = s.begin(),
|
||||
end = s.end();
|
||||
|
||||
// advance to first non-space char - simplifes logic in main loop,
|
||||
// since we can always prepend a single space when we see a
|
||||
// space -> non-space transition
|
||||
for (; (it != end) && isspace(*it); ++it) { /* nothing */ }
|
||||
|
||||
bool lastWasSpace = false;
|
||||
for (; it != end; ++it) {
|
||||
char c = *it;
|
||||
if (isspace(c)) {
|
||||
lastWasSpace = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (lastWasSpace) {
|
||||
result.push_back(' ');
|
||||
}
|
||||
|
||||
lastWasSpace = false;
|
||||
result.push_back(c);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
int to_int(const std::string& s)
|
||||
{
|
||||
return atoi(s.c_str());
|
||||
}
|
||||
|
||||
} // end namespace strutils
|
||||
|
||||
} // end namespace simgear
|
||||
|
||||
@@ -116,6 +116,14 @@ namespace simgear {
|
||||
*/
|
||||
bool ends_with( const std::string & s, const std::string & substr );
|
||||
|
||||
/**
|
||||
* Strip all leading/trailing whitespace, and transform all interal
|
||||
* whitespace into a single ' ' character - i.e newlines/carriage returns/
|
||||
* tabs/multiple spaces will be condensed.
|
||||
*/
|
||||
std::string simplify(const std::string& s);
|
||||
|
||||
int to_int(const std::string& s);
|
||||
} // end namespace strutils
|
||||
} // end namespace simgear
|
||||
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
// Test harness.
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include <simgear/compiler.h>
|
||||
|
||||
#include <iostream>
|
||||
#include "strutils.hxx"
|
||||
|
||||
using std::cout;
|
||||
using std::cerr;
|
||||
using std::endl;
|
||||
|
||||
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)
|
||||
{
|
||||
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);
|
||||
|
||||
cout << "all tests passed successfully!" << endl;
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user