Wednesday 15 February 2012

c++ - Simple/creative way to test this function without HW -



c++ - Simple/creative way to test this function without HW -

i wondering if know of simple way test function without actual hw. problem set 2 registers , want read them can verify code good.

void class::setsomeregister( uint8_t unit, uint8_t input ) { if (input<=15) ihw.setbits( offset, unit*4, 4, input ); ihw.setbit( offset_two, unit, input==16 ); }

the register functions have form

void hw::setbits/setbit { readregister() //modify writeregister() }

in read , write functions have done this.

readregister { #ifdef emulation homecoming itestregister; #else //real code #endif } writeregister( offset, const uint32_t value ) { #ifdef emulation itestregister = value; #else //real code #endif } static uint32_t itestregister;

i have tried using std::stack not work after pushing value in first setregister, pop in second. ideas how can handled simply. don't want add together much logic handle this, if possible. looking creative ideas.

you can use mocks that. recommend cpputest - it's c/c++ unit testing framework mock support , memleak detector - others googlemock alternative too.

example

here's small , basic illustration illustration (using cpputest): (i'm using c++11 features - personal preference, not mandatory!)

hw-interface class hw { public: virtual void setbit(uint32_t offset, uint8_t unit, uint8_t bits) = 0; virtual void setbits(uint32_t offset, uint8_t whatever, uint8_t unit, uint8_t bits) =0; }; hw-interface mock class hwmock : public hw { public: void setbit(uint32_t offset, uint8_t unit, uint8_t bits) override { mock().actualcall("setbit") .withparameter("offset", offset) .withparameter("unit", unit) .withparameter("bits", bits); } void setbits(uint32_t offset, uint8_t whatever, uint8_t unit, uint8_t bits) override { mock().actualcall("setbits") .withparameter("offset", offset) .withparameter("whatever", whatever) .withparameter("unit", unit) .withparameter("bits", bits); } }; some illustration implementation class exampleimpl { public: static const uint32_t offset = 10; ~exampleimpl() { delete hw; } // method under test void setsomeregister(uint8_t unit, uint8_t input) { if( input >= 15 ) { hw->setbits(offset, unit * 4, unit, input); } else { hw->setbit(offset, unit, input); } } private: hw* hw = new hwmock; }; unit tests (with mocking) test_group(so26382638) { void setup() override { hw = new hwmock; } void teardown() override { mock().clear(); delete hw; } hw* hw; exampleimpl impl; }; test(so26382638, setregistersetsbit) { const uint8_t unit = 1; const uint8_t bits = 3; mock().expectonecall("setbit") .withparameter("offset", exampleimpl::offset) .withparameter("unit", unit) .withparameter("bits", bits); impl.setsomeregister(unit, bits); mock().checkexpectations(); } test(so26382638, setregistersetsbitsif15ormore) { const uint8_t unit = 1; const uint8_t bits = 17; mock().expectonecall("setbits") .withparameter("offset", exampleimpl::offset) .withparameter("whatever", 4 * unit) .withparameter("unit", unit) .withparameter("bits", bits); impl.setsomeregister(unit, bits); mock().checkexpectations(); }

in (improvable) illustration there's lot of static data, eg. unit = 1. can pass sort of info mock using mock().setdata(...) / mock().getdata(...). can utilize many different mocks , named mocks. check manual examples.

documentation cpputest testing manual mocking manual googlemock

c++ unit-testing tdd

No comments:

Post a Comment