10 #ifndef __PION_PIONUNITTESTDEFS_HEADER__
11 #define __PION_PIONUNITTESTDEFS_HEADER__
15 #include <boost/test/unit_test.hpp>
16 #include <boost/test/test_case_template.hpp>
17 #include <pion/PionLogger.hpp>
21 #define CHANGE_DIRECTORY _chdir
22 #define GET_DIRECTORY(a,b) _getcwd(a,b)
25 #define CHANGE_DIRECTORY chdir
26 #define GET_DIRECTORY(a,b) getcwd(a,b)
29 #define DIRECTORY_MAX_SIZE 1000
35 static void doNothing(
void* ctx,
const char* msg, ...) {
39 static char* trim(
char* str) {
40 for (
long len = strlen(str) - 1; len >= 0; len--) {
41 if (str[len] ==
'\n' || str[len] ==
'\r')
51 static bool read_lines_from_file(
const std::string& filename, std::list<std::string>& lines) {
53 std::ifstream a_file(filename.c_str(), std::ios::in | std::ios::binary);
54 if (! a_file.is_open())
58 static const unsigned int BUF_SIZE = 4096;
59 char *ptr, buf[BUF_SIZE+1];
63 while (a_file.getline(buf, BUF_SIZE)) {
65 if (*ptr !=
'\0' && *ptr !=
'#')
77 static bool check_files_match(
const std::string& fileA,
const std::string& fileB) {
79 std::list<std::string> a_lines, b_lines;
80 BOOST_REQUIRE(read_lines_from_file(fileA, a_lines));
81 BOOST_REQUIRE(read_lines_from_file(fileB, b_lines));
88 return (a_lines == b_lines);
91 static bool check_files_exact_match(
const std::string& fileA,
const std::string& fileB,
bool ignore_line_endings =
false) {
93 std::ifstream a_file(fileA.c_str(), std::ios::in | std::ios::binary);
94 BOOST_REQUIRE(a_file.is_open());
96 std::ifstream b_file(fileB.c_str(), std::ios::in | std::ios::binary);
97 BOOST_REQUIRE(b_file.is_open());
100 static const unsigned int BUF_SIZE = 4096;
101 char a_buf[BUF_SIZE];
102 char b_buf[BUF_SIZE];
104 if (ignore_line_endings) {
105 while (a_file.getline(a_buf, BUF_SIZE)) {
106 if (! b_file.getline(b_buf, BUF_SIZE))
108 PionUnitTest::trim(a_buf);
109 PionUnitTest::trim(b_buf);
110 if (strlen(a_buf) != strlen(b_buf))
112 if (memcmp(a_buf, b_buf, strlen(a_buf)) != 0)
115 if (b_file.getline(b_buf, BUF_SIZE))
118 while (a_file.read(a_buf, BUF_SIZE)) {
119 if (! b_file.read(b_buf, BUF_SIZE))
121 if (memcmp(a_buf, b_buf, BUF_SIZE) != 0)
124 if (b_file.read(b_buf, BUF_SIZE))
127 if (a_file.gcount() != b_file.gcount())
129 if (memcmp(a_buf, b_buf, a_file.gcount()) != 0)
149 std::cout <<
"global setup for all pion unit tests\n";
152 int argc = boost::unit_test::framework::master_test_suite().argc;
153 char** argv = boost::unit_test::framework::master_test_suite().argv;
155 bool verbose =
false;
157 if (argv[1][0] ==
'-' && argv[1][1] ==
'v') {
162 PION_LOG_CONFIG_BASIC;
164 std::cout <<
"Use '-v' to enable logging of errors and warnings from pion.\n";
167 PION_LOG_SETLEVEL_WARN(log_ptr);
170 std::cout <<
"global teardown for all pion unit tests\n";
238 #define BOOST_AUTO_TEST_SUITE_FIXTURE_TEMPLATE(suite_name, fixture_types) \
239 BOOST_AUTO_TEST_SUITE(suite_name) \
240 typedef fixture_types BOOST_AUTO_TEST_CASE_FIXTURE_TYPES; \
243 #define BOOST_AUTO_TEST_CASE_FIXTURE_TEMPLATE(test_name) \
244 template<typename F> \
245 struct test_name : public F \
246 { void test_method(); }; \
248 struct BOOST_AUTO_TC_INVOKER( test_name ) { \
249 template<typename TestType> \
250 static void run( boost::type<TestType>* = 0 ) \
252 test_name<TestType> t; \
257 BOOST_AUTO_TU_REGISTRAR( test_name )( \
258 boost::unit_test::ut_detail::template_test_case_gen< \
259 BOOST_AUTO_TC_INVOKER( test_name ), \
260 BOOST_AUTO_TEST_CASE_FIXTURE_TYPES >( \
261 BOOST_STRINGIZE( test_name ) ) ); \
263 template<typename F> \
264 void test_name<F>::test_method() \
271 #define CHECK_THROW_WITH_SPECIFIC_MESSAGE(S, M) \
272 bool exception_caught = false; \
275 } catch (pion::PionException& e) { \
276 exception_caught = true; \
277 BOOST_CHECK_EQUAL(e.what(), M); \
279 BOOST_CHECK(exception_caught);