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
unit_testing/test__uniform_type.cpp
unit_testing/test__yield_interface.cpp
cppa/memory_cached_mixin.hpp
unit_testing/test.cpp
......@@ -4,7 +4,7 @@ project(cppa_unit_tests CXX)
add_custom_target(all_unit_tests)
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})
add_test(${name} ${EXECUTABLE_OUTPUT_PATH}/test__${name})
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 @@
#include <iostream>
#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>
inline bool cppa_check_value_fun_eq(const T1& value1, const T2& value2,
typename std::enable_if<
......@@ -29,14 +37,13 @@ inline bool cppa_check_value_fun_eq(T1 value1, T2 value2,
template<typename T1, typename T2>
inline bool cppa_check_value_fun(const T1& value1, const T2& value2,
const char* file_name,
int line_number,
int& error_count) {
int line_number) {
if (cppa_check_value_fun_eq(value1, value2) == false) {
std::cerr << "ERROR in file " << file_name << " on line " << line_number
<< " => expected value: " << value1
<< ", found: " << value2
<< std::endl;
++error_count;
cppa_inc_error_count();
return false;
}
return true;
......@@ -45,60 +52,56 @@ inline bool cppa_check_value_fun(const T1& value1, const T2& value2,
template<typename T1, typename T2>
inline void cppa_check_value_verbose_fun(const T1& value1, const T2& value2,
const char* file_name,
int line_number,
int& error_count) {
if (cppa_check_value_fun(value1, value2, file_name,
line_number, error_count)) {
int line_number) {
if (cppa_check_value_fun(value1, value2, file_name, line_number)) {
std::cout << "line " << line_number << " passed" << std::endl;
}
}
#define CPPA_TEST(name) \
struct cppa_test_scope { \
int error_count; \
cppa_test_scope() : error_count(0) { } \
~cppa_test_scope() { \
std::cout << error_count << " error(s) detected" << std::endl; \
} \
} cppa_ts;
auto cppa_test_scope_guard = ::cppa::util::make_scope_guard([] { \
std::cout << cppa_error_count() << " error(s) detected" << std::endl; \
});
#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_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; \
cppa_inc_error_count(); \
} \
else { \
std::cout << "line " << __LINE__ << " passed" << std::endl; \
} ((void) 0)
#define CPPA_CHECK_EQUAL(lhs_loc, rhs_loc) \
cppa_check_value_verbose_fun((lhs_loc), (rhs_loc), __FILE__, __LINE__, \
cppa_ts.error_count)
cppa_check_value_verbose_fun((lhs_loc), (rhs_loc), __FILE__, __LINE__)
#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) \
std::cerr << "ERROR in file " << __FILE__ << " on line " << __LINE__ \
<< ": " << 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;
......@@ -110,8 +113,8 @@ inline std::vector<std::string> split(const std::string& str, char delim) {
return result;
}
using std::cout;
using std::endl;
using std::cerr;
//using std::cout;
//using std::endl;
//using std::cerr;
#endif // TEST_HPP
......@@ -313,10 +313,7 @@ int main() {
CPPA_IF_VERBOSE(cout << "test receive with zero timeout ... " << flush);
receive (
others() >> []() {
cerr << "WTF?? received: " << to_string(self->last_dequeued())
<< endl;
},
others() >> CPPA_UNEXPECTED_MSG_CB(),
after(chrono::seconds(0)) >> []() {
// mailbox empty
}
......@@ -338,9 +335,7 @@ int main() {
send(mecho, "hello echo");
receive (
on("hello echo") >> []() { },
others() >> [] {
cout << "UNEXPECTED: " << to_string(self->last_dequeued()) << endl;
}
others() >> CPPA_UNEXPECTED_MSG_CB()
);
CPPA_IF_VERBOSE(cout << "await ... " << endl);
await_all_others_done();
......@@ -419,8 +414,9 @@ int main() {
{
int i = 0;
receive_for(i, 10) (
on(atom("failure")) >> []() { }
on(atom("failure")) >> [] { }
);
CPPA_CHECKPOINT_CB();
}
// expect 10 {'ok', value} messages
{
......@@ -465,20 +461,14 @@ int main() {
CPPA_CHECK_EQUAL(42, a);
CPPA_CHECK_EQUAL(2, b);
},
others() >> [&]() {
CPPA_ERROR("unexpected message");
},
after(chrono::seconds(10)) >> [&]() {
CPPA_ERROR("timeout during receive_response");
}
others() >> CPPA_UNEXPECTED_MSG_CB(),
after(chrono::seconds(10)) >> CPPA_UNEXPECTED_TOUT_CB()
);
// 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
receive (
others() >> [&]() {
CPPA_ERROR("unexpected message");
},
others() >> CPPA_UNEXPECTED_MSG_CB(),
after(chrono::seconds(0)) >> []() { }
);
await_all_others_done();
......@@ -523,12 +513,8 @@ int main() {
reply("nothing");
}
);
receive (
on("goodbye!") >> [&] {
CPPA_CHECK(true);
}
);
CPPA_CHECK(true);
receive (on("goodbye!") >> CPPA_CHECKPOINT_CB());
CPPA_CHECKPOINT_CB();
receive (
on(atom("DOWN"), exit_reason::normal) >> [&] {
CPPA_CHECK(self->last_sender() == sync_testee);
......@@ -538,16 +524,9 @@ int main() {
CPPA_IF_VERBOSE(cout << "ok" << endl);
sync_send(sync_testee, "!?").await(
on(atom("EXITED"), any_vals) >> [&] {
CPPA_CHECK(true);
},
others() >> [&] {
CPPA_ERROR("'sync_testee' still alive?; received: "
<< to_string(self->last_dequeued()));
},
after(chrono::milliseconds(5)) >> [&] {
CPPA_CHECK(false);
}
on(atom("EXITED"), any_vals) >> CPPA_CHECKPOINT_CB(),
others() >> CPPA_UNEXPECTED_MSG_CB(),
after(chrono::milliseconds(5)) >> CPPA_UNEXPECTED_TOUT_CB()
);
auto inflater = factory::event_based(
......@@ -566,14 +545,8 @@ int main() {
auto bob = inflater.spawn("Bob", joe);
send(bob, 1, "hello actor");
receive (
on(4, "hello actor") >> [&] {
CPPA_CHECK(true);
// done
},
others() >> [&] {
CPPA_ERROR("unexpected result");
cerr << "unexpected: " << to_string(self->last_dequeued()) << endl;
}
on(4, "hello actor") >> CPPA_CHECKPOINT_CB(),
others() >> CPPA_UNEXPECTED_MSG_CB()
);
// kill joe and bob
auto poison_pill = make_any_tuple(atom("done"));
......
#define CPPA_VERBOSE_CHECK
#include "test.hpp"
#include "cppa/cppa.hpp"
......@@ -115,10 +113,8 @@ int main() {
CPPA_TEST(test__sync_send);
auto await_success_message = [&] {
receive (
on(atom("success")) >> [&] { },
on(atom("failure")) >> [&] {
CPPA_ERROR("A didn't receive a sync response");
},
on(atom("success")) >> CPPA_CHECKPOINT_CB(),
on(atom("failure")) >> CPPA_ERROR_CB("A didn't receive sync response"),
on(atom("DOWN"), arg_match).when(_x2 != exit_reason::normal)
>> [&](uint32_t err) {
CPPA_ERROR("A exited for reason " << err);
......@@ -131,21 +127,17 @@ int main() {
send(spawn_monitor<A>(self), atom("go"), spawn<D>(spawn<C>()));
await_success_message();
await_all_others_done();
cout << __LINE__ << endl;
//sync_send(self, atom("NoWay")).await(
timed_sync_send(self, std::chrono::milliseconds(50), atom("NoWay")).await(
on(atom("TIMEOUT")) >> [&] {
cout << __LINE__ << endl;
// must timeout
cout << "received timeout (ok)" << endl;
},
others() >> [&] {
cout << __LINE__ << endl;
CPPA_ERROR("unexpected message: "
<< to_string(self->last_dequeued()));
}
on(atom("TIMEOUT")) >> CPPA_CHECKPOINT_CB(),
others() >> CPPA_UNEXPECTED_MSG_CB()
);
// we should have received two DOWN messages with normal exit reason
int i = 0;
receive_for(i, 2) (
on(atom("DOWN"), exit_reason::normal) >> CPPA_CHECKPOINT_CB(),
others() >> CPPA_UNEXPECTED_MSG_CB(),
after(std::chrono::seconds(0)) >> CPPA_UNEXPECTED_TOUT_CB()
);
cout << __LINE__ << endl;
shutdown();
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