Commit f543c4a1 authored by Dominik Charousset's avatar Dominik Charousset

improved unit tests

parent 4e6b57af
...@@ -291,3 +291,4 @@ unit_testing/test__type_list.cpp ...@@ -291,3 +291,4 @@ unit_testing/test__type_list.cpp
unit_testing/test__uniform_type.cpp unit_testing/test__uniform_type.cpp
unit_testing/test__yield_interface.cpp unit_testing/test__yield_interface.cpp
cppa/memory_cached_mixin.hpp cppa/memory_cached_mixin.hpp
unit_testing/test.cpp
...@@ -4,7 +4,7 @@ project(cppa_unit_tests CXX) ...@@ -4,7 +4,7 @@ project(cppa_unit_tests CXX)
add_custom_target(all_unit_tests) add_custom_target(all_unit_tests)
macro(add_unit_test name) macro(add_unit_test name)
add_executable(test__${name} test__${name}.cpp ${ARGN}) add_executable(test__${name} test__${name}.cpp test.cpp ${ARGN})
target_link_libraries(test__${name} ${CMAKE_DL_LIBS} ${CPPA_LIBRARY} ${PTHREAD_LIBRARIES}) target_link_libraries(test__${name} ${CMAKE_DL_LIBS} ${CPPA_LIBRARY} ${PTHREAD_LIBRARIES})
add_test(${name} ${EXECUTABLE_OUTPUT_PATH}/test__${name}) add_test(${name} ${EXECUTABLE_OUTPUT_PATH}/test__${name})
add_dependencies(test__${name} all_unit_tests) add_dependencies(test__${name} all_unit_tests)
......
#include <atomic>
#include "test.hpp"
#include "cppa/cppa.hpp"
using namespace cppa;
namespace { std::atomic<size_t> s_error_count{0}; }
void cppa_inc_error_count() {
++s_error_count;
}
size_t cppa_error_count() {
return s_error_count;
}
void cppa_unexpected_message(const char* fname, size_t line_num) {
std::cerr << "ERROR in file " << fname << " on line " << line_num
<< ": unexpected message: "
<< to_string(self->last_dequeued())
<< std::endl;
}
void cppa_unexpected_timeout(const char* fname, size_t line_num) {
std::cerr << "ERROR in file " << fname << " on line " << line_num
<< ": unexpected timeout" << std::endl;
}
...@@ -8,6 +8,14 @@ ...@@ -8,6 +8,14 @@
#include <iostream> #include <iostream>
#include <type_traits> #include <type_traits>
#include "cppa/util/scope_guard.hpp"
void cppa_inc_error_count();
size_t cppa_error_count();
void cppa_unexpected_message(const char* fname, size_t line_num);
void cppa_unexpected_timeout(const char* fname, size_t line_num);
template<typename T1, typename T2> template<typename T1, typename T2>
inline bool cppa_check_value_fun_eq(const T1& value1, const T2& value2, inline bool cppa_check_value_fun_eq(const T1& value1, const T2& value2,
typename std::enable_if< typename std::enable_if<
...@@ -29,14 +37,13 @@ inline bool cppa_check_value_fun_eq(T1 value1, T2 value2, ...@@ -29,14 +37,13 @@ inline bool cppa_check_value_fun_eq(T1 value1, T2 value2,
template<typename T1, typename T2> template<typename T1, typename T2>
inline bool cppa_check_value_fun(const T1& value1, const T2& value2, inline bool cppa_check_value_fun(const T1& value1, const T2& value2,
const char* file_name, const char* file_name,
int line_number, int line_number) {
int& error_count) {
if (cppa_check_value_fun_eq(value1, value2) == false) { if (cppa_check_value_fun_eq(value1, value2) == false) {
std::cerr << "ERROR in file " << file_name << " on line " << line_number std::cerr << "ERROR in file " << file_name << " on line " << line_number
<< " => expected value: " << value1 << " => expected value: " << value1
<< ", found: " << value2 << ", found: " << value2
<< std::endl; << std::endl;
++error_count; cppa_inc_error_count();
return false; return false;
} }
return true; return true;
...@@ -45,60 +52,56 @@ inline bool cppa_check_value_fun(const T1& value1, const T2& value2, ...@@ -45,60 +52,56 @@ inline bool cppa_check_value_fun(const T1& value1, const T2& value2,
template<typename T1, typename T2> template<typename T1, typename T2>
inline void cppa_check_value_verbose_fun(const T1& value1, const T2& value2, inline void cppa_check_value_verbose_fun(const T1& value1, const T2& value2,
const char* file_name, const char* file_name,
int line_number, int line_number) {
int& error_count) { if (cppa_check_value_fun(value1, value2, file_name, line_number)) {
if (cppa_check_value_fun(value1, value2, file_name,
line_number, error_count)) {
std::cout << "line " << line_number << " passed" << std::endl; std::cout << "line " << line_number << " passed" << std::endl;
} }
} }
#define CPPA_TEST(name) \ #define CPPA_TEST(name) \
struct cppa_test_scope { \ auto cppa_test_scope_guard = ::cppa::util::make_scope_guard([] { \
int error_count; \ std::cout << cppa_error_count() << " error(s) detected" << std::endl; \
cppa_test_scope() : error_count(0) { } \ });
~cppa_test_scope() { \
std::cout << error_count << " error(s) detected" << std::endl; \
} \
} cppa_ts;
#define CPPA_TEST_RESULT cppa_ts.error_count #define CPPA_TEST_RESULT cppa_error_count()
#ifdef CPPA_VERBOSE_CHECK
#define CPPA_IF_VERBOSE(line_of_code) line_of_code #define CPPA_IF_VERBOSE(line_of_code) line_of_code
#define CPPA_CHECK(line_of_code) \ #define CPPA_CHECK(line_of_code) \
if (!(line_of_code)) { \ if (!(line_of_code)) { \
std::cerr << "ERROR in file " << __FILE__ << " on line " << __LINE__ \ std::cerr << "ERROR in file " << __FILE__ << " on line " << __LINE__ \
<< " => " << #line_of_code << std::endl; \ << " => " << #line_of_code << std::endl; \
++cppa_ts.error_count; \ cppa_inc_error_count(); \
} \ } \
else { \ else { \
std::cout << "line " << __LINE__ << " passed" << std::endl; \ std::cout << "line " << __LINE__ << " passed" << std::endl; \
} ((void) 0) } ((void) 0)
#define CPPA_CHECK_EQUAL(lhs_loc, rhs_loc) \ #define CPPA_CHECK_EQUAL(lhs_loc, rhs_loc) \
cppa_check_value_verbose_fun((lhs_loc), (rhs_loc), __FILE__, __LINE__, \ cppa_check_value_verbose_fun((lhs_loc), (rhs_loc), __FILE__, __LINE__)
cppa_ts.error_count)
#else
#define CPPA_IF_VERBOSE(line_of_code) ((void) 0)
#define CPPA_CHECK(line_of_code) \
if (!(line_of_code)) { \
std::cerr << "ERROR in file " << __FILE__ << " on line " << __LINE__ \
<< " => " << #line_of_code << std::endl; \
++cppa_ts.error_count; \
} ((void) 0)
#define CPPA_CHECK_EQUAL(lhs_loc, rhs_loc) \
cppa_check_value_fun((lhs_loc), (rhs_loc), __FILE__, __LINE__, \
cppa_ts.error_count)
#endif
#define CPPA_ERROR(err_msg) \ #define CPPA_ERROR(err_msg) \
std::cerr << "ERROR in file " << __FILE__ << " on line " << __LINE__ \ std::cerr << "ERROR in file " << __FILE__ << " on line " << __LINE__ \
<< ": " << err_msg << std::endl; \ << ": " << err_msg << std::endl; \
++cppa_ts.error_count cppa_inc_error_count()
#define CPPA_CHECK_NOT_EQUAL(lhs_loc, rhs_loc) \
CPPA_CHECK(!((lhs_loc) == (rhs_loc)))
#define CPPA_CHECKPOINT() \
std::cout << "checkpoint at line " << __LINE__ << " passed" << std::endl
// some convenience macros for defining callbacks
#define CPPA_CHECKPOINT_CB() \
[] { CPPA_CHECKPOINT(); }
#define CPPA_UNEXPECTED_MSG_CB() \
[] { cppa_unexpected_message(__FILE__, __LINE__); }
#define CPPA_UNEXPECTED_TOUT_CB() \
[] { cppa_unexpected_timeout(__FILE__, __LINE__); }
#define CPPA_CHECK_NOT_EQUAL(lhs_loc, rhs_loc) CPPA_CHECK(((lhs_loc) != (rhs_loc))) #define CPPA_ERROR_CB(err_msg) \
[] { CPPA_ERROR(err_msg); }
typedef std::pair<std::string, std::string> string_pair; typedef std::pair<std::string, std::string> string_pair;
...@@ -110,8 +113,8 @@ inline std::vector<std::string> split(const std::string& str, char delim) { ...@@ -110,8 +113,8 @@ inline std::vector<std::string> split(const std::string& str, char delim) {
return result; return result;
} }
using std::cout; //using std::cout;
using std::endl; //using std::endl;
using std::cerr; //using std::cerr;
#endif // TEST_HPP #endif // TEST_HPP
...@@ -313,10 +313,7 @@ int main() { ...@@ -313,10 +313,7 @@ int main() {
CPPA_IF_VERBOSE(cout << "test receive with zero timeout ... " << flush); CPPA_IF_VERBOSE(cout << "test receive with zero timeout ... " << flush);
receive ( receive (
others() >> []() { others() >> CPPA_UNEXPECTED_MSG_CB(),
cerr << "WTF?? received: " << to_string(self->last_dequeued())
<< endl;
},
after(chrono::seconds(0)) >> []() { after(chrono::seconds(0)) >> []() {
// mailbox empty // mailbox empty
} }
...@@ -338,9 +335,7 @@ int main() { ...@@ -338,9 +335,7 @@ int main() {
send(mecho, "hello echo"); send(mecho, "hello echo");
receive ( receive (
on("hello echo") >> []() { }, on("hello echo") >> []() { },
others() >> [] { others() >> CPPA_UNEXPECTED_MSG_CB()
cout << "UNEXPECTED: " << to_string(self->last_dequeued()) << endl;
}
); );
CPPA_IF_VERBOSE(cout << "await ... " << endl); CPPA_IF_VERBOSE(cout << "await ... " << endl);
await_all_others_done(); await_all_others_done();
...@@ -419,8 +414,9 @@ int main() { ...@@ -419,8 +414,9 @@ int main() {
{ {
int i = 0; int i = 0;
receive_for(i, 10) ( receive_for(i, 10) (
on(atom("failure")) >> []() { } on(atom("failure")) >> [] { }
); );
CPPA_CHECKPOINT_CB();
} }
// expect 10 {'ok', value} messages // expect 10 {'ok', value} messages
{ {
...@@ -465,20 +461,14 @@ int main() { ...@@ -465,20 +461,14 @@ int main() {
CPPA_CHECK_EQUAL(42, a); CPPA_CHECK_EQUAL(42, a);
CPPA_CHECK_EQUAL(2, b); CPPA_CHECK_EQUAL(2, b);
}, },
others() >> [&]() { others() >> CPPA_UNEXPECTED_MSG_CB(),
CPPA_ERROR("unexpected message"); after(chrono::seconds(10)) >> CPPA_UNEXPECTED_TOUT_CB()
},
after(chrono::seconds(10)) >> [&]() {
CPPA_ERROR("timeout during receive_response");
}
); );
// dequeue remaining async. message // dequeue remaining async. message
receive (on(0, 0) >> []() { }); receive (on(0, 0) >> CPPA_CHECKPOINT_CB());
// make sure there's no other message in our mailbox // make sure there's no other message in our mailbox
receive ( receive (
others() >> [&]() { others() >> CPPA_UNEXPECTED_MSG_CB(),
CPPA_ERROR("unexpected message");
},
after(chrono::seconds(0)) >> []() { } after(chrono::seconds(0)) >> []() { }
); );
await_all_others_done(); await_all_others_done();
...@@ -523,12 +513,8 @@ int main() { ...@@ -523,12 +513,8 @@ int main() {
reply("nothing"); reply("nothing");
} }
); );
receive ( receive (on("goodbye!") >> CPPA_CHECKPOINT_CB());
on("goodbye!") >> [&] { CPPA_CHECKPOINT_CB();
CPPA_CHECK(true);
}
);
CPPA_CHECK(true);
receive ( receive (
on(atom("DOWN"), exit_reason::normal) >> [&] { on(atom("DOWN"), exit_reason::normal) >> [&] {
CPPA_CHECK(self->last_sender() == sync_testee); CPPA_CHECK(self->last_sender() == sync_testee);
...@@ -538,16 +524,9 @@ int main() { ...@@ -538,16 +524,9 @@ int main() {
CPPA_IF_VERBOSE(cout << "ok" << endl); CPPA_IF_VERBOSE(cout << "ok" << endl);
sync_send(sync_testee, "!?").await( sync_send(sync_testee, "!?").await(
on(atom("EXITED"), any_vals) >> [&] { on(atom("EXITED"), any_vals) >> CPPA_CHECKPOINT_CB(),
CPPA_CHECK(true); others() >> CPPA_UNEXPECTED_MSG_CB(),
}, after(chrono::milliseconds(5)) >> CPPA_UNEXPECTED_TOUT_CB()
others() >> [&] {
CPPA_ERROR("'sync_testee' still alive?; received: "
<< to_string(self->last_dequeued()));
},
after(chrono::milliseconds(5)) >> [&] {
CPPA_CHECK(false);
}
); );
auto inflater = factory::event_based( auto inflater = factory::event_based(
...@@ -566,14 +545,8 @@ int main() { ...@@ -566,14 +545,8 @@ int main() {
auto bob = inflater.spawn("Bob", joe); auto bob = inflater.spawn("Bob", joe);
send(bob, 1, "hello actor"); send(bob, 1, "hello actor");
receive ( receive (
on(4, "hello actor") >> [&] { on(4, "hello actor") >> CPPA_CHECKPOINT_CB(),
CPPA_CHECK(true); others() >> CPPA_UNEXPECTED_MSG_CB()
// done
},
others() >> [&] {
CPPA_ERROR("unexpected result");
cerr << "unexpected: " << to_string(self->last_dequeued()) << endl;
}
); );
// kill joe and bob // kill joe and bob
auto poison_pill = make_any_tuple(atom("done")); auto poison_pill = make_any_tuple(atom("done"));
......
#define CPPA_VERBOSE_CHECK
#include "test.hpp" #include "test.hpp"
#include "cppa/cppa.hpp" #include "cppa/cppa.hpp"
...@@ -115,10 +113,8 @@ int main() { ...@@ -115,10 +113,8 @@ int main() {
CPPA_TEST(test__sync_send); CPPA_TEST(test__sync_send);
auto await_success_message = [&] { auto await_success_message = [&] {
receive ( receive (
on(atom("success")) >> [&] { }, on(atom("success")) >> CPPA_CHECKPOINT_CB(),
on(atom("failure")) >> [&] { on(atom("failure")) >> CPPA_ERROR_CB("A didn't receive sync response"),
CPPA_ERROR("A didn't receive a sync response");
},
on(atom("DOWN"), arg_match).when(_x2 != exit_reason::normal) on(atom("DOWN"), arg_match).when(_x2 != exit_reason::normal)
>> [&](uint32_t err) { >> [&](uint32_t err) {
CPPA_ERROR("A exited for reason " << err); CPPA_ERROR("A exited for reason " << err);
...@@ -131,21 +127,17 @@ int main() { ...@@ -131,21 +127,17 @@ int main() {
send(spawn_monitor<A>(self), atom("go"), spawn<D>(spawn<C>())); send(spawn_monitor<A>(self), atom("go"), spawn<D>(spawn<C>()));
await_success_message(); await_success_message();
await_all_others_done(); await_all_others_done();
cout << __LINE__ << endl;
//sync_send(self, atom("NoWay")).await(
timed_sync_send(self, std::chrono::milliseconds(50), atom("NoWay")).await( timed_sync_send(self, std::chrono::milliseconds(50), atom("NoWay")).await(
on(atom("TIMEOUT")) >> [&] { on(atom("TIMEOUT")) >> CPPA_CHECKPOINT_CB(),
cout << __LINE__ << endl; others() >> CPPA_UNEXPECTED_MSG_CB()
// must timeout );
cout << "received timeout (ok)" << endl; // we should have received two DOWN messages with normal exit reason
}, int i = 0;
others() >> [&] { receive_for(i, 2) (
cout << __LINE__ << endl; on(atom("DOWN"), exit_reason::normal) >> CPPA_CHECKPOINT_CB(),
CPPA_ERROR("unexpected message: " others() >> CPPA_UNEXPECTED_MSG_CB(),
<< to_string(self->last_dequeued())); after(std::chrono::seconds(0)) >> CPPA_UNEXPECTED_TOUT_CB()
}
); );
cout << __LINE__ << endl;
shutdown(); shutdown();
return CPPA_TEST_RESULT; return CPPA_TEST_RESULT;
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment