Skip to content

Commit

Permalink
test: instroduce FUNC_MOCK_NONSTATIC and FUNC_MOCK_EXTERN
Browse files Browse the repository at this point in the history
Signed-off-by: Jan Michalski <jan.michalski@intel.com>
  • Loading branch information
janekmi committed Mar 11, 2024
1 parent f3b1dfd commit 8b97c8d
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/test/Makefile.inc
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ endif
PAREN=(
extract_funcs = $(shell \
awk -F '[$(PAREN),]' \
'/(FUNC_MOCK_RET_ALWAYS|FUNC_MOCK_RET_ALWAYS_VOID|FUNC_MOCK)\$(PAREN)[^,]/ \
'/(FUNC_MOCK_RET_ALWAYS|FUNC_MOCK_RET_ALWAYS_VOID|FUNC_MOCK|FUNC_MOCK_NONSTATIC)\$(PAREN)[^,]/ \
{ \
print "-Wl,--wrap=" $$2 \
}' $(1) )
Expand Down
9 changes: 5 additions & 4 deletions src/test/core_log/core_log_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ struct vsnprintf_ctx Vsnprintf_;
struct strerror_r_ctx Strerror_r;
struct log_function_ctx Log_function_;

FUNC_MOCK(last_error_msg_get, const char *, void)
FUNC_MOCK_NONSTATIC(last_error_msg_get, const char *, void)
FUNC_MOCK_RUN_DEFAULT {
return LAST_ERROR_MSG_MOCK;
}
FUNC_MOCK_END

FUNC_MOCK(vsnprintf, int, char *__restrict __s, size_t __maxlen,
FUNC_MOCK_NONSTATIC(vsnprintf, int, char *__restrict __s, size_t __maxlen,
const char *__restrict __format, va_list __arg)
FUNC_MOCK_RUN(VALIDATED_CALL) {
if (Common.use_last_error_msg) {
Expand All @@ -43,7 +43,8 @@ FUNC_MOCK_RUN_DEFAULT {
}
FUNC_MOCK_END

FUNC_MOCK(__xpg_strerror_r, int, int __errnum, char *__buf, size_t __buflen)
FUNC_MOCK_NONSTATIC(__xpg_strerror_r, int, int __errnum, char *__buf,
size_t __buflen)
FUNC_MOCK_RUN_DEFAULT {
UT_ASSERTeq(__errnum, DUMMY_ERRNO1);
UT_ASSERTeq(__buf, Strerror_r.exp__buf);
Expand All @@ -62,7 +63,7 @@ FUNC_MOCK_RUN_DEFAULT {
}
FUNC_MOCK_END

FUNC_MOCK(core_log_default_function, void, enum core_log_level level,
FUNC_MOCK_NONSTATIC(core_log_default_function, void, enum core_log_level level,
const char *file_name, const int line_no, const char *function_name,
const char *message)
FUNC_MOCK_RUN(VALIDATED_CALL) {
Expand Down
8 changes: 4 additions & 4 deletions src/test/core_log/core_log_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ extern struct log_function_ctx {

/* mocks */

extern unsigned RCOUNTER(last_error_msg_get);
extern unsigned RCOUNTER(vsnprintf);
extern unsigned RCOUNTER(__xpg_strerror_r);
extern unsigned RCOUNTER(core_log_default_function);
FUNC_MOCK_EXTERN(last_error_msg_get);
FUNC_MOCK_EXTERN(vsnprintf);
FUNC_MOCK_EXTERN(__xpg_strerror_r);
FUNC_MOCK_EXTERN(core_log_default_function);

/* helpers */

Expand Down
3 changes: 2 additions & 1 deletion src/test/unittest/README
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ This directory contains the unit test framework used by
the Persistent Memory Development Kit unit tests.

This framework provides a support for mock objects. To mock an interface use
FUNC_MOCK_RET_ALWAYS or FUNC_MOCK macros in the test code.
FUNC_MOCK_RET_ALWAYS, FUNC_MOCK_RET_ALWAYS_VOID, FUNC_MOCK or
FUNC_MOCK_NONSTATIC macros in the test code.

The FUNC_MOCK_RET_ALWAYS is quite straightforward, it simply takes a function
name and a value that the given function has to return. For example:
Expand Down
13 changes: 11 additions & 2 deletions src/test/unittest/unittest.h
Original file line number Diff line number Diff line change
Expand Up @@ -557,13 +557,22 @@ int ut_thread_join(const char *file, int line, const char *func,
#define FUNC_MOCK_RCOUNTER_SET(name, val)\
RCOUNTER(name) = val;

#define FUNC_MOCK(name, ret_type, ...)\
#define _FUNC_MOCK(type, name, ret_type, ...)\
_FUNC_REAL_DECL(name, ret_type, ##__VA_ARGS__)\
unsigned RCOUNTER(name);\
type unsigned RCOUNTER(name);\
ret_type __wrap_##name(__VA_ARGS__);\
ret_type __wrap_##name(__VA_ARGS__) {\
switch (util_fetch_and_add32(&RCOUNTER(name), 1)) {

#define FUNC_MOCK(name, ret_type, ...) \
_FUNC_MOCK(static, name, ret_type, ##__VA_ARGS__)

#define FUNC_MOCK_NONSTATIC(name, ret_type, ...) \
_FUNC_MOCK(, name, ret_type, ##__VA_ARGS__)

#define FUNC_MOCK_EXTERN(name) \
extern unsigned RCOUNTER(name)

#define FUNC_MOCK_DLLIMPORT(name, ret_type, ...)\
__declspec(dllimport) _FUNC_REAL_DECL(name, ret_type, ##__VA_ARGS__)\
static unsigned RCOUNTER(name);\
Expand Down

0 comments on commit 8b97c8d

Please sign in to comment.