[英]Verify function pointer equality with Cmockery
I am writing Unit test and wondering how would I test function pointers with Cmockery. 我正在编写单元测试,并想知道如何使用Cmockery测试函数指针。
Ac AC
void (*FunctionPtr)(void) = &funcA;
void funcA()
{
// calls func B
funcB();
}
TestA.c TestA.c
void Test_A( void ** state )
{
// test for FunA; working as expected
}
void Test_FunctionPtr( void** state )
{
// how to check here that FunctionPtr holds memory location of funcA?
// I tried something like below:
assert_memory_equal( FunctionPtr, funcA(), sizeof( funcA() ) );
}
During runtime I am getting error and I have no clue how to resolve that. 在运行时我遇到错误,我不知道如何解决这个问题。 May be I using wrong API to assert but don't know which one to call.
可能是我使用错误的API断言但不知道要调用哪一个。
Below is my error: 以下是我的错误:
error during runtime:
Test_FunctionPtr: Starting test
No entries for symbol funcB.
funcA()
calls the function. funcA()
调用该函数。 You want a pointer to the function, which is funcA
or &funcA
(does not make a difference which one you use: read here ). 你想要一个指向函数的指针,它是
funcA
或&funcA
(与你使用的那个没什么区别: 在这里阅读 )。
Also you want to compare the value saved in FunctionPtr
with the address of funcA
. 您还想将
FunctionPtr
保存的值与funcA
的地址进行funcA
。 You do not want to compare the memory where FunctionPtr
points to with the function. 您不希望将
FunctionPtr
指向的内存与函数进行比较。
So instead of assert_memory_equal( FunctionPtr, funcA(), sizeof( funcA() ) );
所以代替
assert_memory_equal( FunctionPtr, funcA(), sizeof( funcA() ) );
I would use assert(FunctionPtr == funcA);
我会使用
assert(FunctionPtr == funcA);
You wrote in your comment that you are using assert_int_equal
now. 您在评论中写道,您现在正在使用
assert_int_equal
。 Note that both values are not int
, so if the macro uses printf
with %d
(or similar) in an error case you are invoking undefined behaviour. 请注意,这两个值都不是
int
,因此如果宏在错误情况下使用带有%d
(或类似)的printf
,则会调用未定义的行为。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.