gpt4 book ai didi

c - 使用 Cmockery 验证函数指针是否相等

转载 作者:行者123 更新时间:2023-12-01 12:56:48 30 4
gpt4 key购买 nike

我正在编写单元测试,想知道如何使用 Cmockery 测试函数指针。

空调

void (*FunctionPtr)(void) = &funcA;

void funcA()
{
// calls func B
funcB();
}

测试A.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 来断言但不知道调用哪个 API。

以下是我的错误:

error during runtime:      
Test_FunctionPtr: Starting test
No entries for symbol funcB.

最佳答案

funcA() 调用函数。您需要一个指向该函数的指针,它是 funcA&funcA(您使用哪一个都没有区别:read here)。

您还想将保存在 FunctionPtr 中的值与 funcA 的地址进行比较。您不想将 FunctionPtr 指向的内存与函数进行比较。

所以我会使用 assert(FunctionPtr == funcA); 而不是 assert_memory_equal( FunctionPtr, funcA(), sizeof( funcA() ) );;

您在评论中写道您现在正在使用 assert_int_equal。请注意,这两个值都不是 int,因此如果宏在错误情况下使用 printf%d(或类似的),您将调用 undefined行为。

关于c - 使用 Cmockery 验证函数指针是否相等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45924526/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com