gy-521/tests/gy521_driver/test_gy521_driver.c

76 lines
1.7 KiB
C
Raw Normal View History

2023-08-31 03:16:47 +00:00
#include <stdarg.h>
#include <stddef.h>
#include <stdint.h>
#include <setjmp.h>
#include <stdlib.h>
#include <cmocka.h>
#include "gy521_driver.h"
2023-08-31 06:31:45 +00:00
#define WRITE_BIT (1<<7)
uint8_t fake_twi_addr = 0x0;
uint8_t fake_twi_data[16] = {0x0};
2023-08-31 03:16:47 +00:00
/* Fake Object for TWI_TX*/
void fake_twi_tx(uint8_t slave_addr, uint8_t *data, uint8_t size)
{
2023-08-31 06:31:45 +00:00
fake_twi_addr = slave_addr;
}
2023-08-31 03:16:47 +00:00
2023-08-31 06:31:45 +00:00
/* 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];
}
2023-08-31 03:16:47 +00:00
}
2023-08-31 06:31:45 +00:00
/*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;
2023-08-31 03:16:47 +00:00
/* Tests the donothing function */
static void test_gy521_init(void **state) {
2023-08-31 06:31:45 +00:00
gy521_module *m = gy521_new();
assert_false(gy521_init(m, 0x0));
assert_false(gy521_init(m, 0x67));
assert_false(gy521_init(m, 0x6A));
2023-08-31 03:16:47 +00:00
2023-08-31 06:31:45 +00:00
/*Check for it's confirmation of the right twi device*/
assert_false(gy521_init(m, TWI_GY521_ADDR1));
2023-08-31 03:16:47 +00:00
2023-08-31 06:31:45 +00:00
/*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);
2023-08-31 03:16:47 +00:00
2023-08-31 06:31:45 +00:00
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);
2023-08-31 03:16:47 +00:00
}
2023-08-31 06:31:45 +00:00
static void test_gy521_update(void **sate)
{
assert_false(1);
}
2023-08-31 03:16:47 +00:00
int main(void)
{
const struct CMUnitTest tests[] = {
cmocka_unit_test(test_gy521_init),
2023-08-31 06:31:45 +00:00
cmocka_unit_test(test_gy521_update),
2023-08-31 03:16:47 +00:00
};
return cmocka_run_group_tests(tests, NULL, NULL);
}