test, flog: logging improvements

* Add an ft_assert_eval() shortcut to check equality, log results, and
  assert at the same time. Somewhat heavy, but good enough.
* Improve line numbers.
This commit is contained in:
Lephenixnoir 2021-05-18 22:45:54 +02:00
parent ee0beb8028
commit c7a6d0484a
Signed by: Lephenixnoir
GPG Key ID: 1BBA026E13FC0495
4 changed files with 28 additions and 12 deletions

View File

@ -40,12 +40,21 @@ void ft_test_run(ft_test *test);
/* ft_test_done(): Check whether a test is done */
bool ft_test_done(ft_test *test);
/* ft_assert(): Record an assertion in a test */
void ft_assert(ft_test *test, int expression,
/* ft_assert(): Record an assertion in a test, return value */
int ft_assert(ft_test *test, int expression,
char const *file, int line, char const *expression_str);
#define ft_assert(test, expression) \
ft_assert(test, expression, __FILE__, __LINE__, #expression)
/* ft_assert_eval(): Evaluate expression, log result and assert result */
#define ft_assert_eval(test, expression, expected, resultfmt) { \
if(ft_assert(test, (expression) == (expected))) \
ft_log(test, #expression " = " #expected "\n"); \
else \
ft_log(test, #expression " = " resultfmt " [wanted " #expected "]\n", \
(expression)); \
}
/* ft_log(): Write some data in a test's log */
__attribute__((format(printf, 2, 3)))
void ft_log(ft_test *test, char const *format, ...);

View File

@ -70,7 +70,7 @@ int main(void)
gscreen_set_tab_fkeys_visible(scr, 1, false);
flog *testlog = flog_create(NULL);
flog_set_line_spacing(testlog, 2);
flog_set_line_spacing(testlog, 3);
gscreen_add_tab(scr, testlog, testlog);
gscreen_set_tab_fkeys_visible(scr, 2, false);

View File

@ -29,7 +29,7 @@ bool ft_test_done(ft_test *test)
return test->passed + test->skipped + test->failed > 0;
}
void ft_assert(ft_test *test, int expression, char const *file, int line,
int ft_assert(ft_test *test, int expression, char const *file, int line,
char const *str)
{
if(expression) {
@ -39,6 +39,7 @@ void ft_assert(ft_test *test, int expression, char const *file, int line,
test->failed++;
ft_log(test, "%s:%d: assertion failed:\n %s\n", file, line, str);
}
return expression;
}
void ft_log(ft_test *test, char const *format, ...)

View File

@ -34,6 +34,7 @@ void flog_set_log(flog *l, char const *log, int log_size)
l->log = log;
l->log_size = log_size;
l->top = 0;
l->widget.dirty = 1;
}
@ -105,8 +106,13 @@ static void flog_poly_render(void *l0, int x, int y)
int current_line = 0;
int current_y = y;
while(log < l->log + l->log_size)
{
if(current_line >= l->top && current_line < l->top + l->visible)
dprint_opt(x+22, current_y, C_BLACK, C_NONE,
DTEXT_RIGHT, DTEXT_TOP, "%d", current_line+1);
if(log[0] == '\n') {
log++;
}
@ -115,19 +121,19 @@ static void flog_poly_render(void *l0, int x, int y)
char const *endline = strchrnul(log, '\n');
int len = max(1, min(endscreen-log, endline-log));
if(current_line >= l->top && current_line < l->top + l->visible) {
dprint_opt(x+22, current_y, C_RGB(21,21,21), C_NONE,
DTEXT_RIGHT, DTEXT_TOP, "%d", current_line+1);
dtext_opt(x+28, current_y, C_BLACK, C_NONE, DTEXT_LEFT,
if(current_line >= l->top && current_line < l->top + l->visible)
dtext_opt(x+30, current_y, C_BLACK, C_NONE, DTEXT_LEFT,
DTEXT_TOP, log, len);
}
log += len + (log[len] == '\n');
}
current_line++;
if(current_line > l->top)
if(current_line >= l->top && current_line < l->top + l->visible) {
int previous_y = current_y;
current_y += l->font->line_height + l->line_spacing;
dline(x+26, previous_y, x+26, current_y-1, C_BLACK);
}
current_line++;
}
if(current_line > l->visible) {
@ -163,7 +169,7 @@ static bool flog_poly_event(void *l0, jevent e)
return true;
}
if(key == KEY_DOWN) {
l->top = min(l->lines - l->visible, l->top + scroll_speed);
l->top = max(0, min(l->lines - l->visible, l->top + scroll_speed));
l->widget.update = 1;
return true;
}