2016-04-05 23:45:34 +02:00
|
|
|
/** @file
|
2016-02-21 22:05:55 +01:00
|
|
|
* @author Edouard DUPIN
|
|
|
|
*
|
|
|
|
* @copyright 2016, Edouard DUPIN, all right reserved
|
|
|
|
*
|
|
|
|
* @license APACHE v2.0 (see license file)
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define NAME "SingleArg"
|
|
|
|
#include <gtest/gtest.h>
|
2016-09-30 22:28:36 +02:00
|
|
|
#include <esignal/Signal.hpp>
|
|
|
|
#include <esignal/Interface.hpp>
|
|
|
|
#include <ememory/memory.hpp>
|
|
|
|
#include <test-debug/debug.hpp>
|
2016-02-21 22:05:55 +01:00
|
|
|
|
|
|
|
static int32_t tmpRetInt32 = 0;
|
|
|
|
static std::string tmpRetString = "";
|
|
|
|
static bool tmpRetVoid = false;
|
|
|
|
|
|
|
|
static void clear() {
|
|
|
|
tmpRetVoid = false;
|
|
|
|
tmpRetInt32 = 0;
|
|
|
|
tmpRetString = "";
|
|
|
|
}
|
|
|
|
|
2016-02-22 21:23:21 +01:00
|
|
|
static void callbackVoid(){
|
2016-02-25 22:42:23 +01:00
|
|
|
TEST_VERBOSE("event void");
|
2016-02-21 22:05:55 +01:00
|
|
|
tmpRetVoid = true;
|
|
|
|
};
|
|
|
|
|
2016-02-22 21:23:21 +01:00
|
|
|
static void callbackInt(int32_t _a){
|
2016-02-25 22:42:23 +01:00
|
|
|
TEST_VERBOSE("event a=" << _a);
|
2016-02-21 22:05:55 +01:00
|
|
|
tmpRetInt32 = _a;
|
|
|
|
};
|
|
|
|
|
2016-02-22 21:23:21 +01:00
|
|
|
static void callbackConstInt(const int32_t& _a){
|
2016-02-25 22:42:23 +01:00
|
|
|
TEST_VERBOSE("event a=" << _a);
|
2016-02-21 22:05:55 +01:00
|
|
|
tmpRetInt32 = _a;
|
|
|
|
};
|
|
|
|
|
2016-02-22 21:23:21 +01:00
|
|
|
static void callbackString(std::string _b){
|
2016-02-25 22:42:23 +01:00
|
|
|
TEST_VERBOSE("event b=" << _b);
|
2016-02-21 22:05:55 +01:00
|
|
|
tmpRetString = _b;
|
|
|
|
};
|
|
|
|
|
2016-02-22 21:23:21 +01:00
|
|
|
static void callbackConstString(const std::string& _b){
|
2016-02-25 22:42:23 +01:00
|
|
|
TEST_VERBOSE("event b=" << _b);
|
2016-02-21 22:05:55 +01:00
|
|
|
tmpRetString = _b;
|
|
|
|
};
|
|
|
|
|
2016-02-22 21:23:21 +01:00
|
|
|
static void callbackIntString(int32_t _a, std::string _b){
|
2016-02-25 22:42:23 +01:00
|
|
|
TEST_VERBOSE("event a=" << _a << " b=" << _b);
|
2016-02-21 22:05:55 +01:00
|
|
|
tmpRetInt32 = _a;
|
|
|
|
tmpRetString = _b;
|
|
|
|
};
|
|
|
|
|
2016-02-22 21:23:21 +01:00
|
|
|
static void callbackConstIntString(const int32_t& _a, const std::string& _b){
|
2016-02-25 22:42:23 +01:00
|
|
|
TEST_VERBOSE("event a=" << _a << " b=" << _b);
|
2016-02-21 22:05:55 +01:00
|
|
|
tmpRetInt32 = _a;
|
|
|
|
tmpRetString = _b;
|
|
|
|
};
|
|
|
|
|
2016-02-22 21:23:21 +01:00
|
|
|
static void callbackMixedIntString(int32_t _a, const std::string& _b){
|
2016-02-25 22:42:23 +01:00
|
|
|
TEST_VERBOSE("event a=" << _a << " b=" << _b);
|
2016-02-21 22:05:55 +01:00
|
|
|
tmpRetInt32 = _a;
|
|
|
|
tmpRetString = _b;
|
|
|
|
};
|
|
|
|
|
2016-02-22 21:23:21 +01:00
|
|
|
static void callbackPolyargs(const int32_t& _a, const std::string& _b, char _char, int _int) {
|
2016-02-25 22:42:23 +01:00
|
|
|
TEST_VERBOSE("event a=" << _a << " b=" << _b << " _char=" << _char << " _int=" << _int);
|
2016-02-22 21:23:21 +01:00
|
|
|
tmpRetInt32 = _a + _int;
|
|
|
|
tmpRetString = _b + _char;
|
|
|
|
}
|
2016-02-21 22:05:55 +01:00
|
|
|
|
|
|
|
TEST(test_signal_static_func, localFunctionVoid) {
|
|
|
|
clear();
|
|
|
|
esignal::Signal<> signal;
|
|
|
|
EXPECT_EQ(signal.size(), 0);
|
|
|
|
EXPECT_EQ(signal.empty(), true);
|
2016-02-22 21:23:21 +01:00
|
|
|
esignal::Connection connection1 = signal.connect(&callbackVoid);
|
2016-02-21 22:05:55 +01:00
|
|
|
EXPECT_EQ(signal.size(), 1);
|
|
|
|
EXPECT_EQ(signal.empty(), false);
|
|
|
|
signal.emit();
|
|
|
|
EXPECT_EQ(tmpRetVoid, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(test_signal_static_func, localFunctionInt32) {
|
|
|
|
clear();
|
|
|
|
esignal::Signal<int32_t> signal;
|
|
|
|
EXPECT_EQ(signal.size(), 0);
|
|
|
|
EXPECT_EQ(signal.empty(), true);
|
2016-02-22 21:23:21 +01:00
|
|
|
esignal::Connection connection1 = signal.connect(&callbackInt);
|
2016-02-21 22:05:55 +01:00
|
|
|
EXPECT_EQ(signal.size(), 1);
|
|
|
|
EXPECT_EQ(signal.empty(), false);
|
|
|
|
signal.emit(12345);
|
|
|
|
EXPECT_EQ(tmpRetInt32, 12345);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(test_signal_static_func, localFunctionConstInt32) {
|
|
|
|
clear();
|
|
|
|
esignal::Signal<int32_t> signal;
|
|
|
|
EXPECT_EQ(signal.size(), 0);
|
|
|
|
EXPECT_EQ(signal.empty(), true);
|
2016-02-22 21:23:21 +01:00
|
|
|
esignal::Connection connection1 = signal.connect(&callbackConstInt);
|
2016-02-21 22:05:55 +01:00
|
|
|
EXPECT_EQ(signal.size(), 1);
|
|
|
|
EXPECT_EQ(signal.empty(), false);
|
|
|
|
signal.emit(34567);
|
|
|
|
EXPECT_EQ(tmpRetInt32, 34567);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(test_signal_static_func, localFunctionString) {
|
|
|
|
clear();
|
|
|
|
esignal::Signal<std::string> signal;
|
|
|
|
EXPECT_EQ(signal.size(), 0);
|
|
|
|
EXPECT_EQ(signal.empty(), true);
|
2016-02-22 21:23:21 +01:00
|
|
|
esignal::Connection connection1 = signal.connect(&callbackString);
|
2016-02-21 22:05:55 +01:00
|
|
|
EXPECT_EQ(signal.size(), 1);
|
|
|
|
EXPECT_EQ(signal.empty(), false);
|
|
|
|
signal.emit("plop");
|
|
|
|
EXPECT_EQ(tmpRetString, "plop");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(test_signal_static_func, localFunctionConstString) {
|
|
|
|
clear();
|
|
|
|
esignal::Signal<std::string> signal;
|
|
|
|
EXPECT_EQ(signal.size(), 0);
|
|
|
|
EXPECT_EQ(signal.empty(), true);
|
2016-02-22 21:23:21 +01:00
|
|
|
esignal::Connection connection1 = signal.connect(&callbackConstString);
|
2016-02-21 22:05:55 +01:00
|
|
|
EXPECT_EQ(signal.size(), 1);
|
|
|
|
EXPECT_EQ(signal.empty(), false);
|
|
|
|
signal.emit("plop1234");
|
|
|
|
EXPECT_EQ(tmpRetString, "plop1234");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(test_signal_static_func, localFunctionIntString) {
|
|
|
|
clear();
|
|
|
|
esignal::Signal<int32_t, std::string> signal;
|
|
|
|
EXPECT_EQ(signal.size(), 0);
|
|
|
|
EXPECT_EQ(signal.empty(), true);
|
2016-02-22 21:23:21 +01:00
|
|
|
esignal::Connection connection1 = signal.connect(&callbackIntString);
|
2016-02-21 22:05:55 +01:00
|
|
|
EXPECT_EQ(signal.size(), 1);
|
|
|
|
EXPECT_EQ(signal.empty(), false);
|
|
|
|
signal.emit(23456, "plop2");
|
|
|
|
EXPECT_EQ(tmpRetInt32, 23456);
|
|
|
|
EXPECT_EQ(tmpRetString, "plop2");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(test_signal_static_func, localFunctionConstIntString) {
|
|
|
|
clear();
|
|
|
|
esignal::Signal<int32_t, std::string> signal;
|
|
|
|
EXPECT_EQ(signal.size(), 0);
|
|
|
|
EXPECT_EQ(signal.empty(), true);
|
2016-02-22 21:23:21 +01:00
|
|
|
esignal::Connection connection1 = signal.connect(&callbackConstIntString);
|
2016-02-21 22:05:55 +01:00
|
|
|
EXPECT_EQ(signal.size(), 1);
|
|
|
|
EXPECT_EQ(signal.empty(), false);
|
|
|
|
signal.emit(246, "plop567");
|
|
|
|
EXPECT_EQ(tmpRetInt32, 246);
|
|
|
|
EXPECT_EQ(tmpRetString, "plop567");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(test_signal_static_func, localFunctionMixedIntString) {
|
|
|
|
clear();
|
|
|
|
esignal::Signal<int32_t, std::string> signal;
|
|
|
|
EXPECT_EQ(signal.size(), 0);
|
|
|
|
EXPECT_EQ(signal.empty(), true);
|
2016-02-22 21:23:21 +01:00
|
|
|
esignal::Connection connection1 = signal.connect(&callbackMixedIntString);
|
2016-02-21 22:05:55 +01:00
|
|
|
EXPECT_EQ(signal.size(), 1);
|
|
|
|
EXPECT_EQ(signal.empty(), false);
|
|
|
|
signal.emit(765, "plTGY");
|
|
|
|
EXPECT_EQ(tmpRetInt32, 765);
|
|
|
|
EXPECT_EQ(tmpRetString, "plTGY");
|
|
|
|
}
|
|
|
|
|
2016-02-22 21:58:56 +01:00
|
|
|
#if __CPP_VERSION__ >= 2014
|
|
|
|
template<class Func, class... Arg>
|
|
|
|
auto complete_args(Func _f, Arg... _arg) {
|
|
|
|
return [=]( auto&&... cargs ){
|
|
|
|
return _f( cargs..., _arg... );
|
|
|
|
};
|
|
|
|
}
|
|
|
|
TEST(test_signal_static_func, localFunctionConstIntStringPolyArg) {
|
|
|
|
clear();
|
|
|
|
esignal::Signal<int32_t, std::string> signal;
|
|
|
|
EXPECT_EQ(signal.size(), 0);
|
|
|
|
EXPECT_EQ(signal.empty(), true);
|
|
|
|
esignal::Connection connection1 = signal.connect(complete_args(&callbackPolyargs, 'c', 12365));
|
|
|
|
EXPECT_EQ(signal.size(), 1);
|
|
|
|
EXPECT_EQ(signal.empty(), false);
|
|
|
|
signal.emit(246, "plop567");
|
|
|
|
EXPECT_EQ(tmpRetInt32, 246 + 12365);
|
|
|
|
EXPECT_EQ(tmpRetString, "plop567c");
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
/*
|
|
|
|
template<class Func, class... ARGS_BASE, class... ARGS_CURR>
|
|
|
|
std::function<void(const ARGS_BASE&...)> complete_args(Func _f, ARGS_BASE... _arg) {
|
|
|
|
return [=](const ARGS_BASE&..., ARGS_CURR...){
|
|
|
|
return _f( cargs..., _arg... );
|
|
|
|
};
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
#endif
|
2016-02-22 21:23:21 +01:00
|
|
|
|
|
|
|
#if 0
|
|
|
|
TEST(test_signal_static_func, localFunctionConstIntStringPolyArgWithJAJA) {
|
|
|
|
clear();
|
|
|
|
esignal::Signal<int32_t, std::string> signal;
|
|
|
|
EXPECT_EQ(signal.size(), 0);
|
|
|
|
EXPECT_EQ(signal.empty(), true);
|
|
|
|
esignal::Connection connection1 = signal.connect(&callbackPolyargs, 'c', 12365);
|
|
|
|
EXPECT_EQ(signal.size(), 1);
|
|
|
|
EXPECT_EQ(signal.empty(), false);
|
|
|
|
signal.emit(246, "plop567");
|
|
|
|
EXPECT_EQ(tmpRetInt32, 246 + 12365);
|
|
|
|
EXPECT_EQ(tmpRetString, "plop567c");
|
|
|
|
}
|
2016-02-21 22:05:55 +01:00
|
|
|
#endif
|
|
|
|
|
2016-02-22 21:23:21 +01:00
|
|
|
TEST(test_signal_static_func, localFunctionLambda) {
|
|
|
|
clear();
|
|
|
|
esignal::Signal<int32_t, std::string> signal;
|
|
|
|
EXPECT_EQ(signal.size(), 0);
|
|
|
|
EXPECT_EQ(signal.empty(), true);
|
|
|
|
esignal::Connection connection1 = signal.connect(
|
|
|
|
[](int32_t _a, const std::string& _b) {
|
|
|
|
tmpRetInt32 = _a;
|
|
|
|
tmpRetString = _b;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
EXPECT_EQ(signal.size(), 1);
|
|
|
|
EXPECT_EQ(signal.empty(), false);
|
|
|
|
signal.emit(246, "plop567");
|
|
|
|
EXPECT_EQ(tmpRetInt32, 246);
|
|
|
|
EXPECT_EQ(tmpRetString, "plop567");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(test_signal_static_func, disconnect) {
|
|
|
|
clear();
|
|
|
|
esignal::Signal<int32_t> signal;
|
|
|
|
EXPECT_EQ(signal.size(), 0);
|
|
|
|
EXPECT_EQ(signal.empty(), true);
|
|
|
|
esignal::Connection connection1 = signal.connect(&callbackInt);
|
|
|
|
EXPECT_EQ(signal.size(), 1);
|
|
|
|
EXPECT_EQ(signal.empty(), false);
|
|
|
|
signal.emit(12345);
|
|
|
|
EXPECT_EQ(tmpRetInt32, 12345);
|
|
|
|
connection1.disconnect();
|
|
|
|
EXPECT_EQ(signal.size(), 0);
|
|
|
|
EXPECT_EQ(signal.empty(), true);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(test_signal_static_func, connect_disconnect_multiple) {
|
|
|
|
esignal::Signal<int32_t> signal;
|
|
|
|
EXPECT_EQ(signal.size(), 0);
|
|
|
|
EXPECT_EQ(signal.empty(), true);
|
|
|
|
esignal::Connection connection1 = signal.connect(&callbackInt);
|
|
|
|
EXPECT_EQ(signal.size(), 1);
|
|
|
|
EXPECT_EQ(signal.empty(), false);
|
|
|
|
esignal::Connection connection2 = signal.connect(&callbackInt);
|
|
|
|
EXPECT_EQ(signal.size(), 2);
|
|
|
|
EXPECT_EQ(signal.empty(), false);
|
|
|
|
esignal::Connection connection3 = signal.connect(&callbackInt);
|
|
|
|
EXPECT_EQ(signal.size(), 3);
|
|
|
|
EXPECT_EQ(signal.empty(), false);
|
|
|
|
esignal::Connection connection4 = signal.connect(&callbackInt);
|
|
|
|
EXPECT_EQ(signal.size(), 4);
|
|
|
|
EXPECT_EQ(signal.empty(), false);
|
|
|
|
connection1.disconnect();
|
|
|
|
EXPECT_EQ(signal.size(), 3);
|
|
|
|
EXPECT_EQ(signal.empty(), false);
|
|
|
|
connection2.disconnect();
|
|
|
|
EXPECT_EQ(signal.size(), 2);
|
|
|
|
EXPECT_EQ(signal.empty(), false);
|
|
|
|
connection3.disconnect();
|
|
|
|
EXPECT_EQ(signal.size(), 1);
|
|
|
|
EXPECT_EQ(signal.empty(), false);
|
|
|
|
connection4.disconnect();
|
|
|
|
EXPECT_EQ(signal.size(), 0);
|
|
|
|
EXPECT_EQ(signal.empty(), true);
|
|
|
|
connection1.disconnect();
|
|
|
|
EXPECT_EQ(signal.size(), 0);
|
|
|
|
EXPECT_EQ(signal.empty(), true);
|
|
|
|
esignal::Connection connection5;
|
|
|
|
connection5.disconnect();
|
|
|
|
connection5 = signal.connect(&callbackInt);
|
|
|
|
EXPECT_EQ(signal.size(), 1);
|
|
|
|
EXPECT_EQ(signal.empty(), false);
|
|
|
|
connection5 = signal.connect(&callbackInt);
|
|
|
|
EXPECT_EQ(signal.size(), 1);
|
|
|
|
EXPECT_EQ(signal.empty(), false);
|
|
|
|
}
|
|
|
|
|
2016-02-22 21:58:56 +01:00
|
|
|
#if __CPP_VERSION__ >= 2014
|
|
|
|
static void callbackDisconnect(esignal::Connection* _connection) {
|
|
|
|
_connection->disconnect();
|
|
|
|
}
|
|
|
|
TEST(test_signal_static_func, disconnect_inCallback) {
|
|
|
|
esignal::Signal<> signal;
|
|
|
|
EXPECT_EQ(signal.size(), 0);
|
|
|
|
EXPECT_EQ(signal.empty(), true);
|
|
|
|
esignal::Connection connection1;
|
|
|
|
connection1 = signal.connect(complete_args(&callbackDisconnect, &connection1));
|
|
|
|
EXPECT_EQ(signal.size(), 1);
|
|
|
|
EXPECT_EQ(signal.empty(), false);
|
|
|
|
signal.emit();
|
|
|
|
EXPECT_EQ(signal.size(), 0);
|
|
|
|
EXPECT_EQ(signal.empty(), true);
|
|
|
|
}
|
|
|
|
#endif
|
2016-02-21 22:05:55 +01:00
|
|
|
|