Created
October 15, 2012 19:34
-
-
Save davisford/3894730 to your computer and use it in GitHub Desktop.
mocking hardware with CppUMock
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// mock implementation of bcm2835.c library | |
void bcm2835_spi_transfern(char *buf, uint32_t len) | |
{ | |
mock_c()->actualCall("bcm2835_spi_transfern")->withParameterOfType("CharBuf", "buf", buf)->withIntParameters("len", len); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// CppUTest test for driver.c | |
TEST_GROUP(Driver) | |
{ | |
virtual void setup() | |
{ | |
mock().clear(); | |
} | |
virtual void teardown() | |
{ | |
mock().checkExpectations(); | |
mock().removeAllComparators(); | |
mock().clear(); | |
} | |
}; | |
// compares two char* buffers that may not be strings | |
class BufComparator : public MockNamedValueComparator | |
{ | |
private: | |
int len; | |
public: | |
BufComparator(int l) { len = l; } | |
virtual bool isEqual (void *obj1, void *obj2) { | |
bool equals = true; | |
char *cbuf1 = (char *)obj1; | |
char *cbuf2 = (char *)obj2; | |
for (int i = 0; i < len; i++) { | |
equals = equals && (cbuf1[i] == cbuf2[i]); | |
} | |
return equals; | |
} | |
virtual SimpleString valueToString(void *obj) { | |
return StringFrom(obj); | |
} | |
}; | |
TEST(Driver, TestAccelWrite) | |
{ | |
char buf[] = { XL362_REG_WRITE, 0x1, 0x2 }; | |
BufComparator comparator = BufComparator(sizeof(buf)); | |
mock().installComparator("CharBuf", comparator); | |
mock().expectOneCall("bcm2835_spi_transfern").withParameterOfType("CharBuf", "buf", buf).withParameter("len", sizeof(buf)); | |
accel_write_reg(XL362_REG_WRITE, 0x1); | |
mock().checkExpectations(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// my driver that uses another library I want to mock out | |
// the library i wish to mock out | |
#include "bcm2835.h" | |
#define WRITE_OP 0xA // indicates write operation to lower level driver | |
// the method I want to test | |
void accel_write_reg(uint8_t reg, uint8_t val) | |
{ | |
char buf[] = { XL362_REG_WRITE, reg, val }; | |
bcm2835_spi_transfern(buf, sizeof(buf)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment