Writing mocks for C firmware

238 views
Skip to first unread message

Pradeepa Senanayake

unread,
Jun 27, 2016, 11:16:42 PM6/27/16
to cpputest
Hello,

We are developing a firmware which runs on top of an RTOS. Therefore, we have to mock the C APIs exposed by the RTOS core.

Our whole development is going in C in Windows. We have been using cpputest for testing and mocking C++ projects.

We know that we can use cpputest for testing C projects too. But we still could not figure out how to do the mocking. 

In C++ it was straight forward, as we could perform dependency injection to inject mocked classes which has actualCall. Is there a similar implementation for C.

Please guide me through the process. 

Thank you.

SPC

unread,
Jun 29, 2016, 9:29:54 AM6/29/16
to cpputest

James Grenning

unread,
Jun 29, 2016, 10:00:42 AM6/29/16
to cpputest

Hello

SPC sent you a page for the C API for mocking. But I think your question is more about substituting test-doubles. In C you have fewer choices for substituting test-doubles into your program for test. Options are:

  • link-time substitution
  • function pointer substitution
  • preprocessor substitution (least desirable for TDD, but needed for legacy code.

You can find a lot about this in my book.

Even when mocking C, I never use the C-API in CppUMock. I'll write the mock in C++, providing the C linkage (or other) needed. Here's an example:

#include "CppUTestExt/MockSupport.h"

extern "C"
{
#include "IOReadWrite.h"
}

void IOWrite(IOAddress addr, IOData data)
{
    mock("IO")
        .actualCall("IOWrite")
        .withParameter("addr", (int)addr)
        .withParameter("data", (int)data);
}

IOData IORead(IOAddress addr)
{
    return (IOData)mock("IO")
            .actualCall("IORead")
            .withParameter("addr", (int)addr)
            .returnValue().getIntValue();
}

Hope that helps!

James

p.s. Now for a second shameless plug: I'll be delivering live web delivered TDD training course where we go through the details of linker substitution and mocking (among other things). Here is a link to the course information https://wingman-sw.com/training/remote-delivered-tdd


James Grenning - Author of TDD for Embedded C - wingman-sw.com/tddec
wingman-sw.com
wingman-sw.com/blog
twitter.com/jwgrenning
facebook.com/wingman.sw
wingman software

--
You received this message because you are subscribed to the Google Groups "cpputest" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cpputest+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Pradeepa Senanayake

unread,
Jul 4, 2016, 1:38:52 AM7/4/16
to cpputest
Hello,

Thank you very much.

Before reading this I started using the C interface and implemented all the basic mocks. :)

Is there any benefit when using C++ interface for this?

It's better if you can change the mock_c() functions and mock() functions to have the same name. Then it will be very easy to interchange if something goes wrong. I think currently the names are almost same, but has some differences. :)

I am reading your book too, it has really good insight on this matter. 

Thanks a lot!!! 

James Grenning

unread,
Jul 6, 2016, 12:18:57 PM7/6/16
to cpputest
On 4 Jul 2016, at 0:38, Pradeepa Senanayake wrote:

> Hello,
>
> Thank you very much.
>
> Before reading this I started using the C interface and implemented
> all the
> basic mocks. :)
>
> Is there any benefit when using C++ interface for this?
It seems the functionality is the same. Why have (learn) two APIs for
the same think.

One advantage is that C programmers start to learn C++ and have its
richness that they can use for testing.

>
> It's better if you can change the mock_c() functions and mock()
> functions
> to have the same name. Then it will be very easy to interchange if
> something goes wrong. I think currently the names are almost same, but
> has
> some differences. :)

You decide. Do some in C and some in C++ and see what you think.


>
> I am reading your book too, it has really good insight on this matter.
>
> Thanks a lot!!!

thank you!

>
> On Wednesday, June 29, 2016 at 7:30:42 PM UTC+5:30, James Grenning
> wrote:
>>
>> Hello
>>
>> SPC sent you a page for the C API for mocking. But I think your
>> question
>> is more about substituting test-doubles. In C you have fewer choices
>> for
>> substituting test-doubles into your program for test. Options are:
>>
>> - link-time substitution
>> - function pointer substitution
>> - preprocessor substitution (least desirable for TDD, but needed
>> ------------------------------
>>
>> James Grenning - Author of TDD for Embedded C - wingman-sw.com/tddec
>> wingman-sw.com
>> wingman-sw.com/blog
>> twitter.com/jwgrenning
>> facebook.com/wingman.sw
>> [image: wingman software] <http://wingman-sw.com>
>> On 27 Jun 2016, at 22:16, Pradeepa Senanayake wrote:
>>
>> Hello,
>>
>> We are developing a firmware which runs on top of an RTOS. Therefore,
>> we
>> have to mock the C APIs exposed by the RTOS core.
>>
>> Our whole development is going in C in Windows. We have been using
>> cpputest
>> for testing and mocking C++ projects.
>>
>> We know that we can use cpputest for testing C projects too. But we
>> still
>> could not figure out how to do the mocking.
>>
>> In C++ it was straight forward, as we could perform dependency
>> injection to
>> inject mocked classes which has actualCall. Is there a similar
>> implementation for C.
>>
>> Please guide me through the process.
>>
>> Thank you.
>>
>> --
>> You received this message because you are subscribed to the Google
>> Groups
>> "cpputest" group.
>> To unsubscribe from this group and stop receiving emails from it,
>> send an
>> email to cpputest+u...@googlegroups.com <javascript:>.
Reply all
Reply to author
Forward
0 new messages