我面临着使用gtest中的fff.h来模拟calloc函数的问题。请任何人指导我如何解决这个问题,我的代码在下面。
#include <iostream>
#include "gtest/gtest.h"
#include "stub_sys.h"
#include "sav_v2x_secif.h"
#include "fff.h"
extern "C" {
DEFINE_FFF_GLOBALS;
//FAKE_VALUE_FUNC(void *, calloc, size_t, size_t);
FAKE_VALUE_FUNC(void *, malloc, size_t);
}我得到以下错误输出。
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);发布于 2022-10-04 20:18:28
最近我编写了一个测试,以检查在某些条件下是否调用了memcpy,因此我将添加我的2c:
malloc,C++标准库将如何分配内存?为了测试系统调用,我使用链接器包装。
--wrap=符号使用包装函数作为符号。对符号的任何未定义的引用将被解析为"_wrap符号“。对"_real符号“的任何未定义的引用将被解析为符号。
关于这一点,这是一个相当不错的帖子。
我非常喜欢FFF,但在这种特殊情况下,我不能使用FFF .custom_fake=__real_memcpy,因为我马上就得到了一个segfault。
我计算memcpy调用次数的测试是:
// Bit of hackery to intercep calls to memcpy.
//
// We use the linker to wrap memcpy (memcpy is now __real_memcpy) and then
// provide the __wrap_memcpy function so we can count how many times it was
// called.
//
// I've tried using FFF to mock __wrap_memcpy and then do
//
// `__wrap_memcpy_fake.custom_fake = __real_memcpy;`
//
// But I get a sigfault right away, so I had to repliacte a bit of FFF's
// functionality. OTOH I didn't have to add fff.h
extern "C"
{
void* __wrap_memcpy(void* dst, const void* src, size_t n);
extern void *__real_memcpy(void* dst, const void* src, size_t n);
}
size_t __wrap_memcpy_call_count;
void* __wrap_memcpy(void* dst, const void* src, size_t n)
{
__wrap_memcpy_call_count++;
return __real_memcpy(dst, src, n);
}
class TestMemcpyFixture:public ::testing::Test {
public:
void SetUp() override
{
__wrap_memcpy_call_count = 0;
}
};
TEST_F(TestMemcpyFixture, test_memcpy) {
// Do stuff that potentially uses memcpy
void *src, *dst;
// Reset the counter
__wrap_memcpy_call_count = 0;
ASSERT_EQ(0, my_code_that_calls_memcpy(src, dst, 42));
// memcpy must have been called only once
ASSERT_EQ(1, __wrap_memcpy_call_count);
}链接测试时,请使用链接器选项-Wl,--wrap=memcpy。正常运行测试。
好吧,那么malloc呢?我会在我的图片类中添加一个标志来控制malloc是否成功。
干杯!
https://stackoverflow.com/questions/60317841
复制相似问题