How to use gmock to test for consecutive writes to an address

Viewed 33

My project builds two targets - one is microcontroller code and second google test binary. The microcontroller target is pure C, built with GCC while unit tests run on host PC and take advantage of C++ GoogleTest framework. I use the following construction to test writes to hardware resources:

/* hardware_definition.h */

typedef struct
{
    uint32_t READY;
    uin32_t CONTROL;
}HARDWARE_t;

#ifndef TESTING
    #define HARDWARE ((HARDWARE_t*) 0xDEADBEEF);
#else
    HARDWARE_t g_HARDWARE;
    #define HARDWARE (&g_HARDWARE);
#endif

/* driver.c */
#include "hardware_definition.h"

void do_hardware_stuff()
{
    HARDWARE->READY = 1;

    for (int i = 0; i < 3; i++)
    {
        HARDWARE->CONTROL = i+10;
    }
}

/* driver_test.cpp */
extern "c"{
    #include "hardware_definition.h"
    void do_hardware_stuff();
}

class DriverTestFixture:
    public ::testing::Test
{
protected:

    void SetUp() override
    {
        m_runningInstance = this;
    }

    void TearDown() override
    {
        m_runningInstance = nullptr;
    }

};

DriverTestFixture* DriverTestFixture::m_runningInstance = nullptr;

TEST_F(DriverTestFixture, do_hardware_stuff)
{
    // call function under test
    do_hardware_stuff();

    // works fine
    ASSERT_EQ(HARDWARE->READY, 1);

    // can only be done for the last write
    ASSERT_EQ(HARDWARE->CONTROL, 30);
}

The question is, how do I test for multiple consecutive writes to HARDWARE->CONTROL? I've tried replacing #define HARDWARE (&g_HARDWARE); with proxy cpp class with overloaded operator-> but it cannot be compiled inside extern "c"{} and GCC doesn't let me use extern "c++"{}.

0 Answers
Related