[英]Verify function pointer equality with Cmockery
我正在编写单元测试,并想知道如何使用Cmockery测试函数指针。
AC
void (*FunctionPtr)(void) = &funcA;
void funcA()
{
// calls func B
funcB();
}
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() ) );
}
在运行时我遇到错误,我不知道如何解决这个问题。 可能是我使用错误的API断言但不知道要调用哪一个。
以下是我的错误:
error during runtime:
Test_FunctionPtr: Starting test
No entries for symbol funcB.
funcA()
调用该函数。 你想要一个指向函数的指针,它是funcA
或&funcA
(与你使用的那个没什么区别: 在这里阅读 )。
您还想将FunctionPtr
保存的值与funcA
的地址进行funcA
。 您不希望将FunctionPtr
指向的内存与函数进行比较。
所以代替assert_memory_equal( FunctionPtr, funcA(), sizeof( funcA() ) );
我会使用assert(FunctionPtr == funcA);
您在评论中写道,您现在正在使用assert_int_equal
。 请注意,这两个值都不是int
,因此如果宏在错误情况下使用带有%d
(或类似)的printf
,则会调用未定义的行为。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.