Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mocking System functions in gtest #84

Open
singh-arulraj opened this issue Feb 20, 2020 · 1 comment
Open

Mocking System functions in gtest #84

singh-arulraj opened this issue Feb 20, 2020 · 1 comment

Comments

@singh-arulraj
Copy link

Hi, I am required to mock calloc function in gtest framework(c++ test code).
Please let me know if I am doing anything wrong.

My Code
`
#include "fff.h"

extern "C" {
DEFINE_FFF_GLOBALS;
//FAKE_VALUE_FUNC(void *, calloc, size_t, size_t);
FAKE_VALUE_FUNC(void *, malloc, size_t);

}
`

I get the following errors

X86/staging_dir/include/fff.h:1679:68: error: declaration of ‘void* malloc(size_t)’ has a different exception specifier RETURN_TYPE FFF_GCC_FUNCTION_ATTRIBUTES FUNCNAME(ARG0_TYPE arg0){ \ ^ X86/staging_dir/include/fff.h:6524:19: note: in definition of macro ‘EXPAND’ #define EXPAND(x) x ^ X86/staging_dir/include/fff.h:6548:32: note: in expansion of macro ‘EXPAND’ #define FUNC_VALUE_(N,...) EXPAND(FUNC_VALUE_N(N,__VA_ARGS__)) ^ X86/staging_dir/include/fff.h:6550:33: note: in expansion of macro ‘EXPAND’ #define FUNC_VALUE_N(N,...) EXPAND(FAKE_VALUE_FUNC ## N(__VA_ARGS__)) ^ X86/staging_dir/include/fff.h:1714:5: note: in expansion of macro ‘DEFINE_FAKE_VALUE_FUNC1’ DEFINE_FAKE_VALUE_FUNC1(RETURN_TYPE, FUNCNAME, ARG0_TYPE) \ ^ X86/staging_dir/include/fff.h:6550:40: note: in expansion of macro ‘FAKE_VALUE_FUNC1’ #define FUNC_VALUE_N(N,...) EXPAND(FAKE_VALUE_FUNC ## N(__VA_ARGS__)) ^ X86/staging_dir/include/fff.h:6548:39: note: in expansion of macro ‘FUNC_VALUE_N’ #define FUNC_VALUE_(N,...) EXPAND(FUNC_VALUE_N(N,__VA_ARGS__)) ^ X86/staging_dir/include/fff.h:6546:41: note: in expansion of macro ‘FUNC_VALUE_’ #define FAKE_VALUE_FUNC(...) EXPAND(FUNC_VALUE_(PP_NARG_MINUS2(__VA_ARGS__), __VA_ARGS__)) ^ X86/tmp/tmp/test/test_tmp.cpp:10:5: note: in expansion of macro ‘FAKE_VALUE_FUNC’ FAKE_VALUE_FUNC(void *, malloc, size_t); ^ X86/tmp/tmp/test/test_tmp.cpp:10:29: error: from previous declaration ‘void* malloc(size_t) throw ()’ FAKE_VALUE_FUNC(void *, malloc, size_t);

@r7vme
Copy link

r7vme commented Oct 21, 2020

@singh-arulraj linker-based wrapping this is what I use

#include <cstdio>
#include <cstdlib>
#include "fff.h"
DEFINE_FFF_GLOBALS;
extern "C" {
FAKE_VALUE_FUNC(void *, __wrap_malloc, size_t);
}

int main(int argc, char **argv)
{
    void* m = malloc(1);
    printf("Fake malloc called %d times.", __wrap_malloc_fake.call_count);
    return 0;
}
> g++ -o 1 -Wl,--wrap=malloc 1.c && ./1
Fake malloc called 1 times.%

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants