wrote tests for the init funcitons

This commit is contained in:
Jake Goodwin 2023-08-30 23:31:45 -07:00
parent 52f234a1de
commit 4c0f16578f
1 changed files with 42 additions and 17 deletions

View File

@ -6,45 +6,70 @@
#include <cmocka.h>
#include "gy521_driver.h"
#define WRITE_BIT (1<<7)
uint8_t fake_twi_addr = 0x0;
uint8_t fake_twi_data[16] = {0x0};
/* A test case that does nothing and succeeds. */
static void null_test_success(void **state) {
(void) state; /* unused */
}
/* Fake Object for TWI_TX*/
void fake_twi_tx(uint8_t slave_addr, uint8_t *data, uint8_t size)
{
fake_twi_addr = slave_addr;
}
/* Fake Object for TWI_RX*/
void fake_twi_rx(uint8_t slave_addr, uint8_t *data, uint8_t size)
{
fake_twi_addr = slave_addr;
for(int i = 0; i < size; i++) {
*data++ = fake_twi_data[i];
}
}
/*Setup the fake twi*/
void (*gy521_twi_tx)(uint8_t, uint8_t*, uint8_t) = &fake_twi_tx;
void (*gy521_twi_rx)(uint8_t, uint8_t*, uint8_t) = &fake_twi_rx;
/* Tests the donothing function */
static void test_gy521_init(void **state) {
gy521_module *m = gy521_new();
assert_false(gy521_init(m, 0x0));
assert_false(gy521_init(m, 0x67));
assert_false(gy521_init(m, 0x6A));
_Bool result = init_gy521(0);
assert_false(result);
/*Check for it's confirmation of the right twi device*/
assert_false(gy521_init(m, TWI_GY521_ADDR1));
result = init_gy521(1);
assert_false(result);
/*Now give it the correct response*/
fake_twi_data[0] = TWI_GY521_ADDR1;
assert_true(gy521_init(m, TWI_GY521_ADDR1));
assert_true(TWI_GY521_ADDR1 == fake_twi_addr);
result = init_gy521((uint8_t) TWI_GY521_ADDR1);
assert_true(result);
fake_twi_data[0] = TWI_GY521_ADDR2;
assert_true(gy521_init(m, TWI_GY521_ADDR2));
assert_true(TWI_GY521_ADDR2 == fake_twi_addr);
gy521_free(m);
}
static void test_gy521_module_methods(void **state) {
gy521_module *module = malloc(sizeof(gy521_module));
assert_true(module_test(*module));
}
static void test_gy521_update(void **sate)
{
assert_false(1);
}
int main(void)
{
const struct CMUnitTest tests[] = {
cmocka_unit_test(null_test_success),
cmocka_unit_test(test_gy521_init),
cmocka_unit_test(test_gy521_module_methods),
cmocka_unit_test(test_gy521_update),
};
return cmocka_run_group_tests(tests, NULL, NULL);
}