Kylin/UnitTest/Universal/MessageManagerTest.cpp

187 lines
5.9 KiB
C++
Raw Normal View History

2024-01-06 00:06:35 +08:00
#include <BoostLog.h>
#include <FunctionTraits.h>
#include <MessageManager.h>
#include <boost/function_types/function_type.hpp>
#include <boost/test/unit_test.hpp>
static int test(int a, int b) {
return a + b;
}
class Test {
public:
int test(int a, int b) {
result = a + b + 3;
return result;
}
int result{0};
};
BOOST_AUTO_TEST_SUITE(MessageManagerTest, *boost::unit_test::enabled())
// returned-function-return-type (* function-name (parameter-list) ) (function-to-return-parameter-list)
BOOST_AUTO_TEST_CASE(FunctionTraitsNormalFunctionTest) {
using namespace std::placeholders;
MessageManager m;
m.registerTopic("123", [](int a) -> void { a = a + 102; });
BOOST_CHECK_EQUAL(m.topicCount<int(int)>("123"), 1);
Test t;
m.registerTopic("123", [&t](int a, int b) { return t.test(a, b); });
BOOST_CHECK_EQUAL(m.topicCount<int(int, int)>("123"), 1);
m.registerTopic("123", std::bind(&Test::test, &t, _1, _2));
BOOST_CHECK_EQUAL(m.topicCount<int(int, int)>("123"), 2);
m.registerTopic("123", &test);
BOOST_CHECK_EQUAL(m.topicCount<int(int, int)>("123"), 3);
m.removeTopic<int(int, int)>("123");
BOOST_CHECK_EQUAL(m.topicCount<int(int, int)>("123"), 0);
m.registerTopic("123", [](const std::string &title) {});
m.registerTopic(
"123", [](int type, const std::string &title, const std::string &message, const std::function<void()> &yes) {});
}
BOOST_AUTO_TEST_CASE(SyncMessage) {
using namespace std::placeholders;
MessageManager manager;
int result1 = 0;
std::thread::id id;
manager.registerTopic("123", [&result1](int a) {
result1 += a;
return a + 102;
});
int result2 = 0;
manager.registerTopic("123", [&result2](const int &a) {
result2 = a + 102;
return result2;
});
manager.sendMessage("123", 5);
BOOST_CHECK_EQUAL(result1, 5);
BOOST_CHECK_EQUAL(result2, 107);
int result3 = 0;
manager.registerTopic("123", [&result3](int a, int b) {
result3 = a + b;
return 0;
});
Test t;
manager.registerTopic("123", std::bind(&Test::test, &t, _1, _2));
manager.sendMessage("123", 12, 13);
BOOST_CHECK_EQUAL(result3, 25);
BOOST_CHECK_EQUAL(t.result, 28);
int result4 = 0;
manager.registerTopic("123", [&result4]() {
result4 = 102;
return result4;
});
manager.sendMessage("123");
BOOST_CHECK_EQUAL(result4, 102);
constexpr auto magic = "MagicString";
std::string result5;
manager.registerTopic("test", [&result5](const std::string &text) { result5 = text; });
// manager.sendMessage<std::string>("test", magic); // 编译不通过
// manager.sendMessage("test", magic); // const char*无法匹配到std::string
// BOOST_CHECK_EQUAL(result5, magic);
// result5.clear();
manager.sendMessage<void(const std::string &), std::string>("test", magic);
BOOST_CHECK_EQUAL(result5, magic);
result5.clear();
manager.sendMessage<void(const std::string &)>("test", std::string(magic));
BOOST_CHECK_EQUAL(result5, magic);
result5.clear();
manager.sendMessage<void(std::string)>("test", magic);
BOOST_CHECK_EQUAL(result5, magic);
result5.clear();
manager.sendMessage("test", std::string(magic));
BOOST_CHECK_EQUAL(result5, magic);
}
BOOST_AUTO_TEST_CASE(SyncMessageWithVoidArg) {
MessageManager manager;
constexpr auto Topic = MessageManager::Message<>("test1");
int result = 0;
manager.registerTopic("test1", [&result]() -> void { result = 250; });
manager.sendMessage("test1");
BOOST_CHECK_EQUAL(result, 250);
result = 0;
manager.sendMessage(Topic);
BOOST_CHECK_EQUAL(result, 250);
}
BOOST_AUTO_TEST_CASE(AsyncMessage) {
int result = 0;
std::thread::id id;
auto test1 = [&result, &id](int a, int b) -> void {
result = a + b;
id = std::this_thread::get_id();
};
MessageManager manager;
manager.registerTopic("test1", test1);
manager.sendAsyncMessage("test1", 2, 6);
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
BOOST_CHECK_EQUAL(result, 8);
BOOST_CHECK_NE(std::this_thread::get_id(), id);
constexpr auto magic = "MagicString";
manager.registerTopic("StringCompare", [magic](const std::string &text) { BOOST_CHECK_EQUAL(text, magic); });
{
std::string string(magic);
manager.sendAsyncMessage("StringCompare", string);
}
}
BOOST_AUTO_TEST_CASE(AsyncMessageImplictConversion) {
float result1 = 0;
auto test1 = [&result1](int a, int b) -> void { result1 = a + b; };
float result2 = 0;
auto test2 = [&result2](int a, int b) -> void { result2 = a + b; };
MessageManager manager;
manager.registerTopic("test1", test1);
manager.registerTopic("test2", test2);
manager.sendAsyncMessage<decltype(test1)>("test1", 2.6, 6);
manager.sendAsyncMessage("test2", (int)3.6, 6);
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
BOOST_CHECK_EQUAL(result1, 8);
BOOST_CHECK_EQUAL(result2, 9);
}
BOOST_AUTO_TEST_CASE(DefineTopicUseMessage) {
constexpr auto TestTopic1 = MessageManager::Message<int, int>("TestTopic1");
int result1 = 0;
auto test1 = [&result1](int a, int b) -> void { result1 = a + b; };
MessageManager manager;
manager.registerTopic(TestTopic1, test1);
manager.sendMessage(TestTopic1, 2, 3);
BOOST_CHECK_EQUAL(result1, 5);
std::string result2;
constexpr auto TestTopic2 = MessageManager::Message<const std::string &>("TestTopic2");
auto test2 = [&result2](const std::string &text) -> void { result2 = text; };
manager.registerTopic(TestTopic2, test2);
manager.sendMessage(TestTopic2, std::string("hello"));
std::string message("hello");
// manager.sendMessage(TestTopic2, message);
// manager.sendMessage(TestTopic2, "hello");
BOOST_CHECK_EQUAL(result2, "hello");
}
BOOST_AUTO_TEST_SUITE_END()