Cmocka: checking a structure passed as a parameter

会有一股神秘感。 提交于 2019-12-11 05:59:58

问题


Let's say that I declare a C struct called foo, which has an int field called bar and a char * called baz.

How do I use the Cmocka expect_ and check_expected macros to check that the structure passed was correct and both fields have the expected values? If there is an example in the documentation, I missed it.


[Update] Perhaps I can use expect_check()? But I can't find an example :-(


回答1:


Use expect_memory(...) and check_expected(...):

Example:

I assume you have a function under test fut which calls a subfunction subfunc. Your struct looks like this:

typedef struct foo_s {
  int bar;
  int baz;
} foo;

And your test driving function could look like this:

void test(void **state) {
   foo myfoo = {
     .bar = 42,
     .baz = 13,
   };
   expect_memory(subfunc, param, &myfoo, sizeof(foo));

   fut();
}

And the subfunctions could look like this:

void subfunc(foo *param){
   check_expected(param);
}


来源:https://stackoverflow.com/questions/39286206/cmocka-checking-a-structure-passed-as-a-parameter

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!