简体   繁体   中英

Verify function pointer equality with Cmockery

I am writing Unit test and wondering how would I test function pointers with 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() ) );
}

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.

Below is my error:

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

funcA() calls the function. You want a pointer to the function, which is funcA or &funcA (does not make a difference which one you use: read here ).

Also you want to compare the value saved in FunctionPtr with the address of funcA . You do not want to compare the memory where FunctionPtr points to with the function.

So instead of assert_memory_equal( FunctionPtr, funcA(), sizeof( funcA() ) ); I would use assert(FunctionPtr == funcA);

You wrote in your comment that you are using assert_int_equal now. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM