Pass size to Comparator for C function foo(struct array * arr, size_t size)

262 views
Skip to first unread message

pthor

unread,
Oct 17, 2016, 5:39:17 PM10/17/16
to cpputest
Hello, 
 
I am working with CppUTest and now CppUMock for the first time, and I have a small design issue for my first proper mock. 

I have the following c function which I would like to mock:  

i2c_transfer(struct i2c_msg *messages, size_t size); 

Typical C pattern for passing array pointer and size of array alongside. I have already implemented MockNamedValueComparator and MockNamedValueCopier for struct i2c_msg ,which seems to work fine for single instance and fixed hardcoded array sizes.

But I have a struggles with finding a neat way to pass the array size to the Comparator of struct i2c_msg. The problem is not to compare the freestanding size parameter, but to make my comparator to traverse over the wanted array size.

My expect_ function looks like this:

void expect_i2c_transfer(i2c_msg *expected_msg, unsigned int size, int call_order)
{
    mock()\
        .expectOneCall("i2c_transfer")\
        .withParameterOfType("struct i2c_msg", "messages", expected_msg)\
        // hardcoded size
        .withParameter("size", 2)\
        .withCallOrder(call_order)\
        .andReturnValue(true);
}

I also started to create comparators for corresponding C++ std::vector<i2c_msg> variants, but I figured I should keep the comparator true to the native data structure, and keep the otherwise typical C interface untouched.

Do anyone have a good suggestion to how to I could tackle this problem? 
Please let me know if the problem is unclear

Thanks, 
Preben

pthor

unread,
Oct 17, 2016, 7:12:59 PM10/17/16
to cpputest
I got it, 

Bas Vodde provided a suggestion in this old post https://groups.google.com/forum/#!searchin/cpputest/comparator/cpputest/CVUJwsk4itY/bbb-dnGCzYgJ that seems to work for me. 
I did not realize that It was elegant, nor possible, to wrap the native data with a wrapper, and do the compare on the wrapper. 

I can update with what I have acutally used when I have tested a bit more.

SPC

unread,
Nov 3, 2016, 10:53:28 AM11/3/16
to cpputest
I independently came up with Bas' solution, but had an issue with it.

My AllTests.cpp uses the MockSupportPlugin to check expectations for all my tests. I'm doing TDD, so I wrote my expect statement first in my new test. Since there is no call to the expected function, or even an implementation at that point, I got an "Expected call did not happen." failure, as expected. What I wasn't expecting was that the valueToString() method would blow up when the description of the missed call was printed! Since the expected object was constructed on the stack it was out of scope when the MockSupportPlugin checked the expectations. The solution was to add a mock().checkExpectations() at the end of the test that uses this model. I was concerned this might screw up the MockSupportPlugin when it came time for it to do its own check of the expectations, but I've not seen any problem to date.
Reply all
Reply to author
Forward
0 new messages