Add screen.[ch] and unit tests

This commit is contained in:
Pim van Pelt
2017-11-26 14:18:43 +01:00
parent 79bb900def
commit 3e2e9d4e5b
11 changed files with 344 additions and 46 deletions

View File

@ -9,7 +9,7 @@ default: $(TARGET)
all: default
OBJECTS = $(patsubst %.c, %.o, $(wildcard *.c))
SRCS = frozen/frozen.c ../src/widget.c
SRCS = frozen/frozen.c ../src/widget.c ../src/screen.c
HEADERS = $(wildcard *.h)
%.o: %.c $(HEADERS)

View File

@ -1,5 +1,10 @@
{
"name": "TestScreen",
"x": 0,
"y": 0,
"w": 320,
"h": 240,
"widgets": [
{
"x": 16,

View File

@ -1,8 +1,16 @@
#include "test.h"
int test_failures=0;
int assert_count=0;
int main() {
LOG(LL_INFO, ("test_widget"));
test_widget();
test_screen();
if (test_failures) {
LOG(LL_ERROR, ("%d test failures", test_failures));
return -1;
}
LOG(LL_INFO, ("All tests passed, %d assertions OK", assert_count));
return 0;
}

View File

@ -25,7 +25,7 @@ int log_print_prefix(enum cs_log_level l, const char *func, const char *file) {
default: // LL_NONE
return 0;
}
printf ("%-5s %-10s %-15s| ", ll_str, file, func);
printf ("%-5s %-15s %-15s| ", ll_str, file, func);
return 1;
}

View File

@ -6,6 +6,20 @@
#include "frozen/frozen.h"
#include "mgos_mock.h"
extern int test_failures;
extern int assert_count;
#define ASSERT(expr, errstr) \
do { \
if (!(expr)) { \
LOG(LL_ERROR, ("ASSERT FAIL: "errstr)); \
test_failures++; \
} \
assert_count++; \
} while (0)
int test_widget(void);
int test_screen(void);

View File

@ -1,5 +1,78 @@
#include "test.h"
#include "screen.h"
#include "widget.h"
static void test_widget_add_and_remove(struct screen_t *s, const char *fn) {
struct widget_t *w1;
int num_widgets_before, num_widgets;
bool ret;
w1 = widget_create_from_file(fn);
ASSERT(w1, "widget_create_from_file() failed");
num_widgets_before = screen_get_num_widgets(s);
LOG(LL_INFO, ("Screen has %d widget(s)", num_widgets_before));
ret = screen_widget_add(s, w1);
ASSERT(true == ret, "screen_widget_add() failed for widget1");
num_widgets = screen_get_num_widgets(s);
LOG(LL_INFO, ("Screen has %d widget(s)", num_widgets));
ASSERT(num_widgets == num_widgets_before+1, "Did not see widget in screen");
ret = screen_widget_destroy(s, &w1);
ASSERT(true == ret, "screen_widget_destroy() failed for widget1");
num_widgets = screen_get_num_widgets(s);
LOG(LL_INFO, ("Screen has %d widget(s)", num_widgets));
ASSERT(num_widgets == num_widgets_before, "Too many widgets in screen");
return;
(void) s;
(void) fn;
}
int test_screen() {
struct screen_t *s = NULL;
struct widget_t *w = NULL;
uint16_t num_widgets;
LOG(LL_INFO, ("screen_create_from_file(data/TestScreen.json)"));
s = screen_create_from_file("data/TestScreen.json");
ASSERT(s, "Could not create screen");
num_widgets = screen_get_num_widgets(s);
LOG(LL_INFO, ("Screen has %d widget(s)", num_widgets));
ASSERT(num_widgets == 2, "Expecting 2 widgets in screen");
LOG(LL_INFO, ("test_widget_add_and_remove(data/TestWidget.json)"));
test_widget_add_and_remove(s, "data/TestWidget.json");
num_widgets = screen_get_num_widgets(s);
LOG(LL_INFO, ("Screen has %d widget(s)", num_widgets));
ASSERT(num_widgets == 2, "Expecting 2 widgets in screen");
LOG(LL_INFO, ("test_widget_add_from_file(data/TestWidget.json)"));
w = screen_widget_add_from_file(s, "data/TestWidget.json");
ASSERT(w, "screen_widget_add_from_file() failed for data/TestWidget.json");
num_widgets = screen_get_num_widgets(s);
LOG(LL_INFO, ("Screen has %d widget(s)", num_widgets));
ASSERT(num_widgets == 3, "Expecting 3 widgets in screen");
LOG(LL_INFO, ("screen_widget_destroy()"));
screen_widget_destroy(s, &w);
ASSERT(!w, "screen_widget_destroy() failed for data/TestWidget.json");
num_widgets = screen_get_num_widgets(s);
LOG(LL_INFO, ("Screen has %d widget(s)", num_widgets));
ASSERT(num_widgets == 2, "Expecting 2 widgets in screen");
LOG(LL_INFO, ("screen_destroy()"));
screen_destroy(&s);
ASSERT(!s, "Could not destroy screen");
return 0;
}

View File

@ -28,24 +28,21 @@ exit:
return 0;
*/
static int test_widget_add_from_file(void) {
static int test_widget_create_from_file(char *fn) {
struct widget_t *w;
const char *fn = "data/TestWidget.json";
w = widget_add_from_file(fn, 0, NULL, NULL);
if (!w) {
LOG(LL_ERROR, ("Could not add %s", fn));
return -1;
}
if (16 != w->x || 48 != w->w || 16 != w->y || 48 != w->h) {
LOG(LL_ERROR, ("widget x, y, w, h incorrect"));
return -1;
}
LOG(LL_INFO, ("Adding widget from %s", fn));
w = widget_create_from_file(fn);
ASSERT(w, "widget_create_from_file()");
ASSERT(w->x == 16, "'x' field is invalid");
ASSERT(w->y == 16, "'x' field is invalid");
ASSERT(w->w == 48, "'x' field is invalid");
ASSERT(w->h == 48, "'x' field is invalid");
return 0;
}
int test_widget() {
test_widget_add_from_file();
test_widget_create_from_file("data/TestWidget.json");
return 0;
}