ccmocka

Cmocka: checking a structure passed as a parameter


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 :-(


Solution

  • 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);
    }