aboutsummaryrefslogtreecommitdiff
path: root/tests/test_tui.c
blob: 93aece07f4f8a1756698dcda47ae366cfd542f01 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
/* Unit tests for the TUI widget layer (clients/tui.c). Only the pure parts
   are exercised here; drawing is smoke-tested over a pty. */
#include <stdio.h>
#include <string.h>

#include <ncursesw/ncurses.h>

#include "tui.h"
#include "util.h"

static int failures = 0;
static int checks = 0;

#define CHECK(cond)                                                     \
    do {                                                                \
        checks++;                                                       \
        if (!(cond)) {                                                  \
            failures++;                                                 \
            fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__,     \
                    #cond);                                             \
        }                                                               \
    } while (0)

static void test_disp_width(void)
{
    CHECK(tui_disp_width("") == 0);
    CHECK(tui_disp_width("abc") == 3);
    CHECK(tui_disp_width("åäö") == 3);
    CHECK(tui_disp_width("aåb") == 3);
    CHECK(tui_disp_width_n("åäö", 2) == 1);
    CHECK(tui_disp_width_n("åäö", 4) == 2);
    CHECK(tui_disp_width_n("abc", 2) == 2);
}

static void test_formats(void)
{
    char buf[64];
    tui_kr_format(123456, buf, sizeof buf);
    CHECK(strcmp(buf, "1 234,56") == 0);
    tui_kr_format(-1000000, buf, sizeof buf);
    CHECK(strcmp(buf, "-10 000,00") == 0);
    tui_kr_format(5, buf, sizeof buf);
    CHECK(strcmp(buf, "0,05") == 0);
    tui_kr0_format(123456, buf, sizeof buf);
    CHECK(strcmp(buf, "1 235") == 0);
    tui_kr0_format(-123456, buf, sizeof buf);
    CHECK(strcmp(buf, "-1 235") == 0);
    tui_amt_col(buf, sizeof buf, 12, 123456);
    CHECK(strcmp(buf, "    1 234,56") == 0);
    snprintf(buf, sizeof buf, "åäö");
    tui_pad_field(buf, sizeof buf, 2);
    CHECK(strcmp(buf, "åä") == 0);
}

static void test_parse_kr(void)
{
    int64_t v = -1;
    CHECK(tui_parse_kr("", &v) == 0 && v == 0);
    CHECK(tui_parse_kr("0", &v) == 0 && v == 0);
    CHECK(tui_parse_kr("12", &v) == 0 && v == 1200);
    CHECK(tui_parse_kr("1 234,56", &v) == 0 && v == 123456);
    CHECK(tui_parse_kr("12,3", &v) == 0 && v == 1230);
    CHECK(tui_parse_kr("12.30", &v) == 0 && v == 1230);
    CHECK(tui_parse_kr("1.234", &v) != 0);
    CHECK(tui_parse_kr("1,23,4", &v) != 0);
    CHECK(tui_parse_kr("-5", &v) != 0);
    CHECK(tui_parse_kr("12a", &v) != 0);
}

static void test_ledit(void)
{
    char buf[32];
    struct tui_ledit e;

    snprintf(buf, sizeof buf, "abc");
    tui_le_init(&e, buf, sizeof buf);
    CHECK(e.pos == 3 && e.len == 3);
    CHECK(tui_le_key(&e, KEY_LEFT) && e.pos == 2);
    CHECK(tui_le_key(&e, 'X'));
    CHECK(strcmp(buf, "abXc") == 0 && e.pos == 3);
    CHECK(tui_le_key(&e, KEY_BACKSPACE));
    CHECK(strcmp(buf, "abc") == 0 && e.pos == 2);
    CHECK(tui_le_key(&e, KEY_DC));
    CHECK(strcmp(buf, "ab") == 0 && e.pos == 2);
    CHECK(tui_le_key(&e, KEY_HOME) && e.pos == 0);
    CHECK(tui_le_key(&e, 21)); /* ^U */
    CHECK(strcmp(buf, "") == 0 && e.len == 0);

    /* UTF-8: the caret moves by code point, not by byte */
    snprintf(buf, sizeof buf, "åä");
    tui_le_init(&e, buf, sizeof buf);
    CHECK(e.pos == 4);
    CHECK(tui_le_key(&e, KEY_LEFT) && e.pos == 2);
    CHECK(tui_le_key(&e, KEY_LEFT) && e.pos == 0);
    CHECK(tui_le_key(&e, KEY_RIGHT) && e.pos == 2);
    CHECK(tui_le_key(&e, KEY_BACKSPACE));
    CHECK(strcmp(buf, "ä") == 0);

    /* capacity: never overflow */
    snprintf(buf, sizeof buf, "12345");
    tui_le_init(&e, buf, 4);
    CHECK(tui_le_key(&e, '9') == 1 || 1);
    CHECK(strlen(buf) < 4 && e.len < 4);
    CHECK(tui_le_key(&e, KEY_UP) == 0); /* not an editing key */
}

static void test_field_fresh(void)
{
    char buf[16];
    size_t pos = (size_t)-1;
    int fresh = 1;

    /* fresh + backspace deletes the last character, it does not clear */
    snprintf(buf, sizeof buf, "abc");
    CHECK(tui_field_edit(buf, sizeof buf, &pos, KEY_BACKSPACE, &fresh));
    CHECK(strcmp(buf, "ab") == 0);

    /* the first printable still replaces the whole prefilled value */
    snprintf(buf, sizeof buf, "abc");
    pos = (size_t)-1;
    fresh = 1;
    CHECK(tui_field_edit(buf, sizeof buf, &pos, 'x', &fresh));
    CHECK(strcmp(buf, "x") == 0);

    /* movement keeps the content, clears fresh; the next printable is
       inserted at the caret */
    snprintf(buf, sizeof buf, "abc");
    pos = (size_t)-1;
    fresh = 1;
    CHECK(tui_field_edit(buf, sizeof buf, &pos, KEY_LEFT, &fresh));
    CHECK(strcmp(buf, "abc") == 0 && pos == 2 && fresh == 0);
    CHECK(tui_field_edit(buf, sizeof buf, &pos, 'x', &fresh));
    CHECK(strcmp(buf, "abxc") == 0);

    /* fresh + delete removes the character at the caret, no clear */
    snprintf(buf, sizeof buf, "abc");
    pos = 0;
    fresh = 1;
    CHECK(tui_field_edit(buf, sizeof buf, &pos, KEY_DC, &fresh));
    CHECK(strcmp(buf, "bc") == 0);
}

static void date_feed(char *buf, size_t cap, const char *digits)
{
    size_t pos = (size_t)-1;
    int fresh = 1;
    for (const char *p = digits; *p; p++)
        tui_date_field_edit(buf, cap, &pos, *p, &fresh);
}

static void test_date_field(void)
{
    char buf[16] = "";
    size_t pos = (size_t)-1;
    int fresh = 1;

    date_feed(buf, sizeof buf, "20260604");
    CHECK(strcmp(buf, "2026-06-04") == 0);
    fresh = 0;
    CHECK(tui_date_field_edit(buf, sizeof buf, &pos, KEY_BACKSPACE, &fresh));
    CHECK(strcmp(buf, "2026-06-0") == 0);
    CHECK(tui_date_field_edit(buf, sizeof buf, &pos, 21, &fresh)); /* ^U */
    CHECK(strcmp(buf, "") == 0);
    CHECK(tui_date_field_edit(buf, sizeof buf, &pos, 'x', &fresh) == 0);

    /* fresh replaces a prefilled value on the first digit */
    snprintf(buf, sizeof buf, "2025-01-01");
    pos = (size_t)-1;
    fresh = 1;
    tui_date_field_edit(buf, sizeof buf, &pos, '9', &fresh);
    CHECK(strcmp(buf, "9") == 0);
    /* full field overwrites at the caret */
    date_feed(buf, sizeof buf, "20260604");
    pos = (size_t)-1;
    fresh = 0;
    tui_date_field_edit(buf, sizeof buf, &pos, '9', &fresh);
    CHECK(strcmp(buf, "2026-06-09") == 0);

    /* fresh + backspace deletes the last digit; fresh + ^U clears */
    snprintf(buf, sizeof buf, "2026-06-04");
    pos = (size_t)-1;
    fresh = 1;
    CHECK(tui_date_field_edit(buf, sizeof buf, &pos, KEY_BACKSPACE, &fresh));
    CHECK(strcmp(buf, "2026-06-0") == 0);
    snprintf(buf, sizeof buf, "2026-06-04");
    pos = (size_t)-1;
    fresh = 1;
    CHECK(tui_date_field_edit(buf, sizeof buf, &pos, 21, &fresh));
    CHECK(strcmp(buf, "") == 0);
}

static void nav_init(struct tui_list_nav *v, int n)
{
    memset(v, 0, sizeof *v);
    v->n = n;
    v->view = 10;
}

static void test_nav(void)
{
    struct tui_list_nav v;

    nav_init(&v, 0);
    CHECK(tui_nav_key(&v, KEY_DOWN, 0, 0, 0, 0) == TUI_NAV_NONE);
    CHECK(tui_nav_key(&v, KEY_F(5), 0, 0, 0, 0) == -2);
    CHECK(tui_nav_key(&v, TUI_KEY_CTRL_N, 1, 0, 0, 0) == -4);
    CHECK(tui_nav_key(&v, TUI_KEY_CTRL_N, 0, 0, 0, 0) == TUI_NAV_NONE);
    CHECK(tui_nav_key(&v, 'q', 0, 0, 0, 0) == -1);
    CHECK(tui_nav_key(&v, 27, 0, 0, 0, 0) == -1);

    nav_init(&v, 5);
    CHECK(tui_nav_key(&v, KEY_UP, 0, 0, 0, 0) == TUI_NAV_NONE && v.sel == 0);
    CHECK(tui_nav_key(&v, KEY_DOWN, 0, 0, 0, 0) == TUI_NAV_NONE && v.sel == 1);
    CHECK(tui_nav_key(&v, KEY_END, 0, 0, 0, 0) == TUI_NAV_NONE && v.sel == 4);
    CHECK(tui_nav_key(&v, KEY_DOWN, 0, 0, 0, 0) == TUI_NAV_NONE && v.sel == 4);
    CHECK(tui_nav_key(&v, KEY_HOME, 0, 0, 0, 0) == TUI_NAV_NONE && v.sel == 0);
    CHECK(tui_nav_key(&v, KEY_NPAGE, 0, 0, 0, 0) == TUI_NAV_NONE && v.sel == 4);
    CHECK(tui_nav_key(&v, KEY_PPAGE, 0, 0, 0, 0) == TUI_NAV_NONE && v.sel == 0);

    /* digits jump in lists, activate in menus */
    CHECK(tui_nav_key(&v, '3', 0, 0, 0, 0) == TUI_NAV_NONE && v.sel == 2);
    CHECK(tui_nav_key(&v, '3', 0, 0, 0, 1) == 2);
    CHECK(tui_nav_key(&v, '9', 0, 0, 0, 0) == TUI_NAV_NONE); /* out of range */

    CHECK(tui_nav_key(&v, '\n', 0, 0, 0, 0) == v.sel);
    CHECK(tui_nav_key(&v, KEY_F(5), 0, 0, 0, 0) == -2);
    CHECK(tui_nav_key(&v, TUI_KEY_CTRL_N, 1, 0, 0, 0) == -4);
    CHECK(tui_nav_key(&v, TUI_KEY_CTRL_N, 0, 0, 0, 0) == TUI_NAV_NONE);
    CHECK(tui_nav_key(&v, 'd', 0, 1, 0, 0) == -6);
    CHECK(tui_nav_key(&v, 'd', 0, 0, 0, 0) == TUI_NAV_NONE);
    CHECK(tui_nav_key(&v, 1, 0, 0, 1, 0) == -7); /* ^A */
    CHECK(tui_nav_key(&v, 'q', 0, 0, 0, 0) == -1);
    CHECK(tui_nav_key(&v, 27, 0, 0, 0, 0) == -1);

    /* g goto takes multi-digit input and Enter only closes the prompt */
    nav_init(&v, 20);
    CHECK(tui_nav_key(&v, 'g', 0, 0, 0, 0) == TUI_NAV_NONE && v.goto_active);
    CHECK(tui_nav_key(&v, '1', 0, 0, 0, 0) == TUI_NAV_NONE && v.sel == 0);
    CHECK(tui_nav_key(&v, '2', 0, 0, 0, 0) == TUI_NAV_NONE && v.sel == 11);
    CHECK(tui_nav_key(&v, KEY_BACKSPACE, 0, 0, 0, 0) == TUI_NAV_NONE &&
          v.sel == 0);
    CHECK(tui_nav_key(&v, '\n', 0, 0, 0, 0) == TUI_NAV_NONE && !v.goto_active);
    /* the view follows the selection */
    nav_init(&v, 100);
    v.view = 10;
    tui_nav_key(&v, KEY_END, 0, 0, 0, 0);
    CHECK(v.top == 90);
}

static void test_styles(void)
{
    /* without colour every style falls back to attributes */
    CHECK(tui_style_attrs(TUI_TITLE, 0, 0) == (int)A_BOLD);
    CHECK(tui_style_attrs(TUI_HEADING, 0, 0) == (int)A_BOLD);
    CHECK(tui_style_attrs(TUI_SUBHEADING, 0, 0) == (int)A_BOLD);
    CHECK(tui_style_attrs(TUI_DIM, 0, 0) == (int)A_DIM);
    CHECK(tui_style_attrs(TUI_STATUS, 0, 0) == (int)A_DIM);
    CHECK(tui_style_attrs(TUI_RULE, 0, 0) == (int)A_DIM);
    CHECK(tui_style_attrs(TUI_POS, 0, 0) == 0);
    CHECK(tui_style_attrs(TUI_NEG, 0, 0) == 0);
    /* NO_COLOR forces the same fallback even when colours are available */
    CHECK(tui_style_attrs(TUI_POS, 1, 1) == tui_style_attrs(TUI_POS, 0, 0));
    CHECK(tui_style_attrs(TUI_RULE, 1, 1) == (int)A_DIM);
    CHECK(tui_style_attrs(TUI_TITLE, 1, 1) == (int)A_BOLD);
    /* with colour the amount styles get distinct colour pairs */
    int pos = tui_style_attrs(TUI_POS, 1, 0);
    int neg = tui_style_attrs(TUI_NEG, 1, 0);
    CHECK((pos & (int)A_COLOR) != 0);
    CHECK((neg & (int)A_COLOR) != 0);
    CHECK((pos & (int)A_COLOR) != (neg & (int)A_COLOR));
    CHECK((tui_style_attrs(TUI_HEADING, 1, 0) & (int)A_BOLD) != 0);
    CHECK((tui_style_attrs(TUI_STATUS, 1, 0) & (int)A_DIM) != 0);
}

static void test_markup(void)
{
    int st = 123;
    const char *s = tui_markup("vanlig text", &st);
    CHECK(strcmp(s, "vanlig text") == 0 && st == -1);
    s = tui_markup("\001Rubrik", &st);
    CHECK(strcmp(s, "Rubrik") == 0 && st == TUI_HEADING);
    s = tui_markup("\002dimmad rad", &st);
    CHECK(strcmp(s, "dimmad rad") == 0 && st == TUI_DIM);
    s = tui_markup("\003----------------", &st);
    CHECK(strcmp(s, "----------------") == 0 && st == TUI_RULE);
    s = tui_markup("", &st);
    CHECK(s && *s == '\0' && st == -1);
    s = tui_markup(NULL, &st);
    CHECK(s && *s == '\0' && st == -1);
    /* only a leading marker counts; the marker may be NULL-checked away */
    s = tui_markup("a\001b", &st);
    CHECK(strcmp(s, "a\001b") == 0 && st == -1);
    s = tui_markup("\004text", &st);
    CHECK(strcmp(s, "\004text") == 0 && st == -1);
    s = tui_markup("\001x", NULL);
    CHECK(strcmp(s, "x") == 0);
}

static const unsigned char SECTION_FLAGS[5] = { 0, 1, 1, 0, 1 };

static void test_nav_sections(void)
{
    struct tui_list_nav v;
    nav_init(&v, 5);
    v.selectable = SECTION_FLAGS;
    v.sel = 0; /* a header: snaps to the first selectable row */
    CHECK(tui_nav_key(&v, KEY_DOWN, 0, 0, 0, 0) == TUI_NAV_NONE && v.sel == 2);
    CHECK(tui_nav_key(&v, KEY_DOWN, 0, 0, 0, 0) == TUI_NAV_NONE && v.sel == 4);
    CHECK(tui_nav_key(&v, KEY_DOWN, 0, 0, 0, 0) == TUI_NAV_NONE && v.sel == 4);
    CHECK(tui_nav_key(&v, KEY_UP, 0, 0, 0, 0) == TUI_NAV_NONE && v.sel == 2);
    CHECK(tui_nav_key(&v, KEY_HOME, 0, 0, 0, 0) == TUI_NAV_NONE && v.sel == 1);
    CHECK(tui_nav_key(&v, KEY_END, 0, 0, 0, 0) == TUI_NAV_NONE && v.sel == 4);
    /* digits and goto count selectable rows only */
    CHECK(tui_nav_key(&v, '1', 0, 0, 0, 0) == TUI_NAV_NONE && v.sel == 1);
    CHECK(tui_nav_key(&v, '2', 0, 0, 0, 1) == 2 && v.sel == 2);
    CHECK(tui_nav_key(&v, '3', 0, 0, 0, 1) == 4 && v.sel == 4);
    CHECK(tui_nav_key(&v, '9', 0, 0, 0, 1) == TUI_NAV_NONE && v.sel == 4);
    v.sel = 1;
    CHECK(tui_nav_key(&v, 'g', 0, 0, 0, 0) == TUI_NAV_NONE && v.goto_active);
    CHECK(tui_nav_key(&v, '2', 0, 0, 0, 0) == TUI_NAV_NONE && v.sel == 2);
    CHECK(tui_nav_key(&v, KEY_BACKSPACE, 0, 0, 0, 0) == TUI_NAV_NONE &&
          v.sel == 2);
    CHECK(tui_nav_key(&v, '\n', 0, 0, 0, 0) == TUI_NAV_NONE && !v.goto_active);
    /* paging lands on selectable rows */
    nav_init(&v, 5);
    v.selectable = SECTION_FLAGS;
    v.view = 2;
    CHECK(tui_nav_key(&v, KEY_NPAGE, 0, 0, 0, 0) == TUI_NAV_NONE && v.sel == 4);
    CHECK(tui_nav_key(&v, KEY_PPAGE, 0, 0, 0, 0) == TUI_NAV_NONE && v.sel == 2);
}

static void test_form_nav(void)
{
    CHECK(TUI_NAV_NONE != TUI_FORM_SUBMIT);
    CHECK(tui_form_next(1, 3, 'x') == TUI_NAV_NONE);
    CHECK(tui_form_next(2, 5, KEY_HOME) == 0);
    CHECK(tui_form_next(2, 5, KEY_END) == 4);
    CHECK(tui_form_next(0, 5, KEY_NPAGE) == 4);
    CHECK(tui_form_next(0, 25, KEY_NPAGE) == 10);
    CHECK(tui_form_next(12, 25, KEY_PPAGE) == 2);
    CHECK(tui_form_next(3, 25, KEY_PPAGE) == 0);
    CHECK(tui_form_next(3, 25, KEY_HOME) == 0);
    CHECK(tui_form_next(0, 3, KEY_UP) == TUI_NAV_NONE);
    CHECK(tui_form_next(0, 3, KEY_DOWN) == 1);
    CHECK(tui_form_next(2, 3, KEY_DOWN) == TUI_NAV_NONE);
    CHECK(tui_form_next(1, 3, KEY_F(5)) == TUI_FORM_REFRESH);
    CHECK(tui_form_next(1, 3, TUI_KEY_CTRL_ENTER) == TUI_FORM_SUBMIT);
    CHECK(tui_form_next(1, 3, KEY_F(9)) == TUI_FORM_SUBMIT);
    CHECK(tui_form_next(1, 3, 27) == TUI_FORM_BACK);
    CHECK(tui_form_next(1, 3, 'q') == TUI_FORM_BACK);
    CHECK(tui_form_next(1, 3, 'x') == TUI_NAV_NONE);
}


static char rt_cells[8][3][64];
static void rt_cb(void *ud, int row, int col, char **buf, size_t *cap)
{
    (void)ud;
    *buf = rt_cells[row][col];
    *cap = sizeof rt_cells[row][col];
}
static void rt_info_cb(void *ud, int row, char *buf, size_t n)
{
    (void)ud;
    snprintf(buf, n, "info%d", row);
}
static const struct tui_rt_col RT_COLS[3] = {
    { "A", 2, 8, TUI_RT_EDIT },
    { "N", 12, 8, TUI_RT_INFO },
    { "B", 22, 8, TUI_RT_EDIT },
};

static int rt_key_cb(void *ud, int ch)
{
    (void)ud;
    (void)ch;
    return TUI_HOOK_NONE;
}
static void rt_footer_cb(void *ud, char *buf, size_t n)
{
    (void)ud;
    snprintf(buf, n, "footer");
}

static void test_rowtable(void)
{
    struct tui_rt t;
    memset(rt_cells, 0, sizeof rt_cells);
    tui_rt_init(&t, 1, 8, 3, RT_COLS, 6, rt_cb, rt_info_cb, NULL);
    CHECK(t.nrows == 1 && t.neditable == 2 && t.row == 0 && t.col == 0);
    CHECK(t.key == NULL && t.footer == NULL);
    tui_rt_set_key(&t, rt_key_cb);
    tui_rt_set_footer(&t, rt_footer_cb);
    CHECK(t.key == rt_key_cb && t.footer == rt_footer_cb);
    /* editable index -> actual column index (INFO columns are skipped) */
    CHECK(tui_rt_col_of(&t, 0) == 0);
    CHECK(tui_rt_col_of(&t, 1) == 2);
    CHECK(tui_rt_blank_row(&t, 0));
    CHECK(tui_rt_move(&t, 0, 1) && t.col == 1);
    CHECK(tui_rt_move(&t, 0, 1) && t.col == 0);
    CHECK(tui_rt_move(&t, -1, 0) == 0);
    CHECK(tui_rt_move(&t, 1, 0) == 0);

    /* Tab walks the row and wraps to the next row */
    t.nrows = 3;
    t.row = 0;
    t.col = 0;
    CHECK(tui_rt_tab(&t, 1) && t.row == 0 && t.col == 1);
    CHECK(tui_rt_tab(&t, 1) && t.row == 1 && t.col == 0);
    CHECK(tui_rt_tab(&t, -1) && t.row == 0 && t.col == 1);
    CHECK(tui_rt_tab(&t, -1) && t.row == 0 && t.col == 0);
    /* with a single editable column every Tab is a row move */
    struct tui_rt one;
    tui_rt_init(&one, 3, 8, 1, RT_COLS, 6, rt_cb, rt_info_cb, NULL);
    CHECK(one.neditable == 1);
    CHECK(tui_rt_tab(&one, 1) && one.row == 1 && one.col == 0);
    CHECK(tui_rt_tab(&one, -1) && one.row == 0 && one.col == 0);

    /* data in the blank row appends a new blank row */
    tui_rt_normalize(&t);
    t.row = 0;
    t.col = 0;
    snprintf(rt_cells[0][0], 64, "1930");
    CHECK(tui_rt_normalize(&t) == 2);
    snprintf(rt_cells[1][0], 64, "x");
    CHECK(tui_rt_normalize(&t) == 3);
    /* two trailing blank rows collapse into one */
    t.nrows = 5;
    CHECK(tui_rt_normalize(&t) == 3);

    /* ^X clears the row and re-normalizes */
    t.row = 1;
    t.col = 0;
    CHECK(tui_rt_clear(&t));
    CHECK(t.nrows == 2);
    CHECK(rt_cells[1][0][0] == '\0');
    CHECK(tui_rt_clear(&t));
    CHECK(t.nrows == 2); /* the second clear only hit the blank row */
    t.row = 0;
    CHECK(tui_rt_clear(&t));
    CHECK(t.nrows == 1);
    tui_rt_clear(&t);
    CHECK(t.nrows == 1); /* never below one row */
}

static void test_rt_fields(void)
{
    struct tui_rt t;
    memset(rt_cells, 0, sizeof rt_cells);
    tui_rt_init(&t, 1, 8, 3, RT_COLS, 6, rt_cb, rt_info_cb, NULL);
    CHECK(t.focus == -1);
    struct tui_form_field f[2];
    memset(f, 0, sizeof f);
    f[0].label = "Datum";
    f[1].label = "Text";
    tui_rt_set_fields(&t, f, 2);
    CHECK(t.fields == f && t.nfields == 2 && t.focus == 0);
    tui_rt_set_fields(&t, NULL, 0);
    CHECK(t.fields == NULL && t.nfields == 0 && t.focus == -1);
}

static void test_rt_focus(void)
{
    int row, col;

    /* fields 0..2, then a 2-row table with 3 editable cells each */
    row = 0;
    col = 0;
    CHECK(tui_rt_focus_step(0, 3, &row, &col, 2, 3, 1) == 1);
    CHECK(tui_rt_focus_step(1, 3, &row, &col, 2, 3, 1) == 2);
    CHECK(tui_rt_focus_step(2, 3, &row, &col, 2, 3, 1) == -1);
    CHECK(row == 0 && col == 0);
    CHECK(tui_rt_focus_step(-1, 3, &row, &col, 2, 3, 1) == -1);
    CHECK(row == 0 && col == 1);
    CHECK(tui_rt_focus_step(-1, 3, &row, &col, 2, 3, 1) == -1);
    CHECK(row == 0 && col == 2);
    CHECK(tui_rt_focus_step(-1, 3, &row, &col, 2, 3, 1) == -1);
    CHECK(row == 1 && col == 0);
    /* after the last cell Tab wraps back to the first field */
    row = 1;
    col = 2;
    CHECK(tui_rt_focus_step(-1, 3, &row, &col, 2, 3, 1) == 0);
    /* Shift-Tab from the first field lands on the last cell */
    CHECK(tui_rt_focus_step(0, 3, &row, &col, 2, 3, -1) == -1);
    CHECK(row == 1 && col == 2);
    CHECK(tui_rt_focus_step(-1, 3, &row, &col, 2, 3, -1) == -1);
    CHECK(row == 1 && col == 1);
    /* Shift-Tab from the first cell wraps back to the last field */
    row = 0;
    col = 0;
    CHECK(tui_rt_focus_step(-1, 3, &row, &col, 2, 3, -1) == 2);
    /* without fields the table still cycles through its cells */
    row = 0;
    col = 0;
    CHECK(tui_rt_focus_step(-1, 0, &row, &col, 2, 3, -1) == -1);
    CHECK(row == 1 && col == 2);
    CHECK(tui_rt_focus_step(-1, 0, &row, &col, 2, 3, 1) == -1);
    CHECK(row == 0 && col == 0);
    /* an empty table cycles only through the fields */
    row = 0;
    col = 0;
    CHECK(tui_rt_focus_step(2, 3, &row, &col, 0, 0, 1) == 0);
    CHECK(tui_rt_focus_step(0, 3, &row, &col, 0, 0, -1) == 2);
}

static void test_rt_footer(void)
{
    CHECK(tui_rt_footer_row(24) == 22);
    CHECK(tui_rt_footer_row(4) == 2);
    CHECK(tui_rt_footer_row(3) == 2);
    CHECK(tui_rt_footer_row(2) == 2);
    char f[64];
    snprintf(f, sizeof f, "Bilagor: a\nSumma 0,00   I BALANS");
    tui_rt_footer_flatten(f);
    CHECK(strcmp(f, "Bilagor: a Summa 0,00   I BALANS") == 0);
    snprintf(f, sizeof f, "en enda rad");
    tui_rt_footer_flatten(f);
    CHECK(strcmp(f, "en enda rad") == 0);
    f[0] = '\0';
    tui_rt_footer_flatten(f);
    CHECK(f[0] == '\0');
}

int main(void)
{
    test_disp_width();
    test_formats();
    test_parse_kr();
    test_ledit();
    test_field_fresh();
    test_date_field();
    test_styles();
    test_markup();
    test_nav();
    test_nav_sections();
    test_form_nav();
    test_rowtable();
    test_rt_fields();
    test_rt_focus();
    test_rt_footer();
    printf("test_tui: %d checks, %d failures\n", checks, failures);
    return failures ? 1 : 0;
}