Skip to content

Commit

Permalink
iox-eclipse-iceoryx#427 Add on demand function loader for mocks
Browse files Browse the repository at this point in the history
Signed-off-by: Hintz Martin (CC-AD/ESW1) <[email protected]>
  • Loading branch information
marthtz authored and elBoberido committed Dec 18, 2020
1 parent f64ec89 commit 9aef11f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 43 deletions.
9 changes: 9 additions & 0 deletions iceoryx_utils/testutils/mocks/mocks.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ template <typename T>
T assignSymbol(const std::string& functionName);
} // namespace mocks

#define STATIC_FUNCTION_LOADER_MANUAL_DEDUCE(type, functionName) \
[]() { \
static auto returnValue = mocks::assignSymbol<type>(#functionName); \
return returnValue; \
}()

#define STATIC_FUNCTION_LOADER_AUTO_DEDUCE(functionName) \
STATIC_FUNCTION_LOADER_MANUAL_DEDUCE(decltype(&functionName), functionName)

#include "mocks.inl"

#endif // IOX_UTILS_MOCKS_MOCKS_HPP
48 changes: 18 additions & 30 deletions iceoryx_utils/testutils/mocks/mqueue_mock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,43 +21,29 @@
std::unique_ptr<mqueue_MOCK> mqueue_MOCK::mock;
bool mqueue_MOCK::doUseMock = false;

namespace mqueue_orig
{
mqd_t (*mq_open)(const char*, int, mode_t, struct mq_attr*) =
mocks::assignSymbol<mqd_t (*)(const char*, int, mode_t, struct mq_attr*)>("mq_open");
mqd_t (*mq_open2)(const char*, int) = mocks::assignSymbol<mqd_t (*)(const char*, int)>("mq_open");
int (*mq_unlink)(const char*) = mocks::assignSymbol<int (*)(const char*)>("mq_unlink");
int (*mq_close)(int) = mocks::assignSymbol<int (*)(int)>("mq_close");
ssize_t (*mq_receive)(int, char*, size_t, unsigned int*) =
mocks::assignSymbol<ssize_t (*)(int, char*, size_t, unsigned int*)>("mq_receive");
ssize_t (*mq_timedreceive)(int, char*, size_t, unsigned int*, const struct timespec*) =
mocks::assignSymbol<ssize_t (*)(int, char*, size_t, unsigned int*, const struct timespec*)>("mq_timedreceive");
int (*mq_send)(int,
const char*,
size_t,
unsigned int) = mocks::assignSymbol<int (*)(int, const char*, size_t, unsigned int)>("mq_send");
int (*mq_timedsend)(int, const char*, size_t, unsigned int, const struct timespec*) =
mocks::assignSymbol<int (*)(int, const char*, size_t, unsigned int, const struct timespec*)>("mq_timedsend");
} // namespace mqueue_orig

#if defined(QNX) || defined(QNX__) || defined(__QNX__)
int mq_unlink(const char* name)
#else
int mq_unlink(const char* name) throw()
#endif
{
return (mqueue_MOCK::doUseMock) ? mqueue_MOCK::mock->mq_unlink(name) : mqueue_orig::mq_unlink(name);
return (mqueue_MOCK::doUseMock) ? mqueue_MOCK::mock->mq_unlink(name)
: STATIC_FUNCTION_LOADER_AUTO_DEDUCE(mq_unlink)(name);
}

mqd_t mq_open(const char* name, int oflag, mode_t mode, struct mq_attr* attr)
{
return (mqueue_MOCK::doUseMock) ? mqueue_MOCK::mock->mq_open(name, oflag, mode, attr)
: mqueue_orig::mq_open(name, oflag, mode, attr);
return (mqueue_MOCK::doUseMock)
? mqueue_MOCK::mock->mq_open(name, oflag, mode, attr)
: STATIC_FUNCTION_LOADER_MANUAL_DEDUCE(mqd_t(*)(const char*, int, mode_t, struct mq_attr*),
mq_open)(name, oflag, mode, attr);
}

mqd_t mq_open(const char* name, int oflag)
{
return (mqueue_MOCK::doUseMock) ? mqueue_MOCK::mock->mq_open(name, oflag) : mqueue_orig::mq_open2(name, oflag);
return (mqueue_MOCK::doUseMock)
? mqueue_MOCK::mock->mq_open(name, oflag)
: STATIC_FUNCTION_LOADER_MANUAL_DEDUCE(mqd_t(*)(const char*, int), mq_open2)(name, oflag);
}

#if defined(QNX) || defined(QNX__) || defined(__QNX__)
Expand All @@ -66,33 +52,35 @@ int mq_close(int i)
int mq_close(int i) throw()
#endif
{
return (mqueue_MOCK::doUseMock) ? mqueue_MOCK::mock->mq_close(i) : mqueue_orig::mq_close(i);
return (mqueue_MOCK::doUseMock) ? mqueue_MOCK::mock->mq_close(i) : STATIC_FUNCTION_LOADER_AUTO_DEDUCE(mq_close)(i);
}

ssize_t mq_receive(int mqdes, char* msg_ptr, size_t msg_len, unsigned int* msg_prio)
{
return (mqueue_MOCK::doUseMock) ? mqueue_MOCK::mock->mq_receive(mqdes, msg_ptr, msg_len, msg_prio)
: mqueue_orig::mq_receive(mqdes, msg_ptr, msg_len, msg_prio);
: STATIC_FUNCTION_LOADER_AUTO_DEDUCE(mq_receive)(mqdes, msg_ptr, msg_len, msg_prio);
}

ssize_t
mq_timedreceive(int mqdes, char* msg_ptr, size_t msg_len, unsigned int* msg_prio, const struct timespec* abs_timeout)
{
return (mqueue_MOCK::doUseMock) ? mqueue_MOCK::mock->mq_timedreceive(mqdes, msg_ptr, msg_len, msg_prio, abs_timeout)
: mqueue_orig::mq_timedreceive(mqdes, msg_ptr, msg_len, msg_prio, abs_timeout);
return (mqueue_MOCK::doUseMock)
? mqueue_MOCK::mock->mq_timedreceive(mqdes, msg_ptr, msg_len, msg_prio, abs_timeout)
: STATIC_FUNCTION_LOADER_AUTO_DEDUCE(mq_timedreceive)(mqdes, msg_ptr, msg_len, msg_prio, abs_timeout);
}

int mq_send(int mqdes, const char* msg_ptr, size_t msg_len, unsigned int msg_prio)
{
return (mqueue_MOCK::doUseMock) ? mqueue_MOCK::mock->mq_send(mqdes, msg_ptr, msg_len, msg_prio)
: mqueue_orig::mq_send(mqdes, msg_ptr, msg_len, msg_prio);
: STATIC_FUNCTION_LOADER_AUTO_DEDUCE(mq_send)(mqdes, msg_ptr, msg_len, msg_prio);
}

int mq_timedsend(
int mqdes, const char* msg_ptr, size_t msg_len, unsigned int msg_prio, const struct timespec* abs_timeout)

{
return (mqueue_MOCK::doUseMock) ? mqueue_MOCK::mock->mq_timedsend(mqdes, msg_ptr, msg_len, msg_prio, abs_timeout)
: mqueue_orig::mq_timedsend(mqdes, msg_ptr, msg_len, msg_prio, abs_timeout);
return (mqueue_MOCK::doUseMock)
? mqueue_MOCK::mock->mq_timedsend(mqdes, msg_ptr, msg_len, msg_prio, abs_timeout)
: STATIC_FUNCTION_LOADER_AUTO_DEDUCE(mq_timedsend)(mqdes, msg_ptr, msg_len, msg_prio, abs_timeout);
}
#endif
19 changes: 6 additions & 13 deletions iceoryx_utils/testutils/mocks/time_mock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,6 @@
std::unique_ptr<time_MOCK> time_MOCK::mock;
bool time_MOCK::doUseMock = false;

namespace time_orig
{
int (*clock_getres)(clockid_t,
struct timespec*) = mocks::assignSymbol<int (*)(clockid_t, struct timespec*)>("clock_getres");
int (*clock_gettime)(clockid_t,
struct timespec*) = mocks::assignSymbol<int (*)(clockid_t, struct timespec*)>("clock_gettime");
int (*clock_settime)(clockid_t, const struct timespec*) =
mocks::assignSymbol<int (*)(clockid_t, const struct timespec*)>("clock_settime");
} // namespace time_orig

time_MOCK::time_MOCK()
{
}
Expand All @@ -39,15 +29,17 @@ int clock_getres(clockid_t clk_id, struct timespec* res)
int clock_getres(clockid_t clk_id, struct timespec* res) noexcept
#endif
{
return (time_MOCK::doUseMock) ? time_MOCK::mock->clock_getres(clk_id, res) : time_orig::clock_getres(clk_id, res);
return (time_MOCK::doUseMock) ? time_MOCK::mock->clock_getres(clk_id, res)
: STATIC_FUNCTION_LOADER_AUTO_DEDUCE(clock_getres)(clk_id, res);
}
#if defined(QNX) || defined(QNX__) || defined(__QNX__)
int clock_gettime(clockid_t clk_id, struct timespec* res)
#else
int clock_gettime(clockid_t clk_id, struct timespec* res) noexcept
#endif
{
return (time_MOCK::doUseMock) ? time_MOCK::mock->clock_gettime(clk_id, res) : time_orig::clock_gettime(clk_id, res);
return (time_MOCK::doUseMock) ? time_MOCK::mock->clock_gettime(clk_id, res)
: STATIC_FUNCTION_LOADER_AUTO_DEDUCE(clock_gettime)(clk_id, res);
}

#if defined(QNX) || defined(QNX__) || defined(__QNX__)
Expand All @@ -56,6 +48,7 @@ int clock_settime(clockid_t clk_id, const struct timespec* res)
int clock_settime(clockid_t clk_id, const struct timespec* res) noexcept
#endif
{
return (time_MOCK::doUseMock) ? time_MOCK::mock->clock_settime(clk_id, res) : time_orig::clock_settime(clk_id, res);
return (time_MOCK::doUseMock) ? time_MOCK::mock->clock_settime(clk_id, res)
: STATIC_FUNCTION_LOADER_AUTO_DEDUCE(clock_settime)(clk_id, res);
}
#endif

0 comments on commit 9aef11f

Please sign in to comment.