Files
mongoose-touch/unittest/test_buttons.c
2017-12-22 21:37:37 +01:00

51 lines
1.8 KiB
C

#include "test.h"
#include "buttons.h"
#include "relays.h"
extern uint32_t mqtt_pub_count;
extern uint32_t mqtt_sub_count;
int test_buttons() {
bool ret;
uint8_t idx;
float last_change;
ret = buttons_init("0,A");
ASSERT(ret!=0, "buttons_init should not have taken '0,A'");
ret = buttons_init("0,41");
ASSERT(ret!=0, "buttons_init should not have taken '0,41'");
ret = buttons_init("0,1,2,3,4,5,6,7,8");
ASSERT(ret!=0, "buttons_init should not have taken '0,1,2,3,4,5,6,7,8'");
ret = buttons_init("0");
ASSERT(ret==0, "buttons_init should have taken '0'");
ret = relays_init("16,12,13,14");
ret = buttons_init("0,2");
ASSERT(ret==0, "buttons_init should have taken ' 0, 2, '");
idx = buttons_find_by_gpio(1);
ASSERT(idx==255, "should not have gpio=1");
idx = buttons_find_by_gpio(2);
ASSERT(idx==1, "should have gpio=2 at idx=1");
last_change = buttons_get_last_change_by_gpio(2);
ASSERT(last_change==-1, "button at gpio=2 should have last_change==-1");
mqtt_pub_count=0;
buttons_touch_by_gpio(2);
ASSERT(mqtt_pub_count==2, "button should have send mqtt message");
buttons_touch_by_gpio(0);
ASSERT(mqtt_pub_count==4, "button should have send mqtt message");
buttons_touch_by_gpio(1);
ASSERT(mqtt_pub_count==4, "button should not have send mqtt message");
last_change = buttons_get_last_change_by_gpio(2);
ASSERT(last_change>0, "button at gpio=2 should have last_change>0");
last_change = buttons_get_last_change_by_idx(0);
ASSERT(last_change>0, "button at idx=0 should have last_change>0");
last_change = buttons_get_last_change_by_idx(1);
ASSERT(last_change>0, "button at idx=1 should have last_change>0");
last_change = buttons_get_last_change_by_idx(2);
ASSERT(last_change==-1, "button at idx=2 should have last_change==-1");
return 0;
}