summaryrefslogtreecommitdiff
path: root/src/libsystemd-terminal/test-term-page.c
blob: 9e338776e88c609430220539081a84184a5eab77 (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
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
  This file is part of systemd.

  Copyright (C) 2014 David Herrmann <dh.herrmann@gmail.com>

  systemd is free software; you can redistribute it and/or modify it
  under the terms of the GNU Lesser General Public License as published by
  the Free Software Foundation; either version 2.1 of the License, or
  (at your option) any later version.

  systemd is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public License
  along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/

/*
 * Terminal Page/Line/Cell/Char Tests
 * This tests internals of terminal page, line, cell and char handling. It
 * relies on some implementation details, so it might need to be updated if
 * those internals are changed. They should be fairly obvious, though.
 */

#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "macro.h"
#include "term-internal.h"
#include "util.h"

#define MY_ASSERT_VALS __FILE__, __LINE__, __PRETTY_FUNCTION__
#define MY_ASSERT_FORW _FILE, _LINE, _FUNC
#define MY_ASSERT_ARGS const char *_FILE, int _LINE, const char *_FUNC
#define MY_ASSERT(expr)                                                 \
        do {                                                            \
                if (_unlikely_(!(expr)))                                \
                        log_assert_failed(#expr, _FILE, _LINE, _FUNC);  \
        } while (false)                                                 \

/*
 * Character Tests
 *
 * These tests rely on some implementation details of term_char_t, including
 * the way we pack characters and the internal layout of "term_char_t". These
 * tests have to be updated once we change the implementation.
 */

#define PACK(v1, v2, v3) \
                TERM_CHAR_INIT( \
                        (((((uint64_t)v1) & 0x1fffffULL) << 43) | \
                         ((((uint64_t)v2) & 0x1fffffULL) << 22) | \
                         ((((uint64_t)v3) & 0x1fffffULL) <<  1) | \
                         0x1) \
                )
#define PACK1(v1) PACK2((v1), 0x110000)
#define PACK2(v1, v2) PACK3((v1), (v2), 0x110000)
#define PACK3(v1, v2, v3) PACK((v1), (v2), (v3))

static void test_term_char_misc(void) {
        term_char_t c, t;

        /* test TERM_CHAR_NULL handling */

        c = TERM_CHAR_NULL;                     /* c is NULL */
        assert_se(term_char_same(c, TERM_CHAR_NULL));
        assert_se(term_char_equal(c, TERM_CHAR_NULL));
        assert_se(term_char_is_null(c));
        assert_se(term_char_is_null(TERM_CHAR_NULL));
        assert_se(!term_char_is_allocated(c));

        /* test single char handling */

        t = term_char_dup_append(c, 'A');       /* t is >A< now */
        assert_se(!term_char_same(c, t));
        assert_se(!term_char_equal(c, t));
        assert_se(!term_char_is_allocated(t));
        assert_se(!term_char_is_null(t));

        /* test basic combined char handling */

        t = term_char_dup_append(t, '~');
        t = term_char_dup_append(t, '^');       /* t is >A~^< now */
        assert_se(!term_char_same(c, t));
        assert_se(!term_char_is_allocated(t));
        assert_se(!term_char_is_null(t));

        c = term_char_dup_append(c, 'A');
        c = term_char_dup_append(c, '~');
        c = term_char_dup_append(c, '^');       /* c is >A~^< now */
        assert_se(term_char_same(c, t));
        assert_se(term_char_equal(c, t));

        /* test more than 2 comb-chars so the chars are allocated */

        t = term_char_dup_append(t, '`');       /* t is >A~^`< now */
        c = term_char_dup_append(c, '`');       /* c is >A~^`< now */
        assert_se(!term_char_same(c, t));
        assert_se(term_char_equal(c, t));

        /* test dup_append() on allocated chars */

        term_char_free(t);
        t = term_char_dup_append(c, '"');       /* t is >A~^`"< now */
        assert_se(!term_char_same(c, t));
        assert_se(!term_char_equal(c, t));
        c = term_char_merge(c, '"');            /* c is >A~^`"< now */
        assert_se(!term_char_same(c, t));
        assert_se(term_char_equal(c, t));

        term_char_free(t);
        term_char_free(c);
}

static void test_term_char_packing(void)  {
        uint32_t seqs[][1024] = {
                { -1 },
                { 0, -1 },
                { 'A', '~', -1 },
                { 'A', '~', 0, -1 },
                { 'A', '~', 'a', -1 },
        };
        term_char_t res[] = {
                TERM_CHAR_NULL,
                PACK1(0),
                PACK2('A', '~'),
                PACK3('A', '~', 0),
                PACK3('A', '~', 'a'),
        };
        uint32_t next;
        unsigned int i, j;
        term_char_t c = TERM_CHAR_NULL;

        /*
         * This creates term_char_t objects based on the data in @seqs and
         * compares the result to @res. Only basic packed types are tested, no
         * allocations are done.
         */

        for (i = 0; i < ELEMENTSOF(seqs); ++i) {
                for (j = 0; j < ELEMENTSOF(seqs[i]); ++j) {
                        next = seqs[i][j];
                        if (next == (uint32_t)-1)
                                break;

                        c = term_char_merge(c, next);
                }

                assert_se(!memcmp(&c, &res[i], sizeof(c)));
                c = term_char_free(c);
        }
}

static void test_term_char_allocating(void) {
        uint32_t seqs[][1024] = {
                { 0, -1 },
                { 'A', '~', -1 },
                { 'A', '~', 0, -1 },
                { 'A', '~', 'a', -1 },
                { 'A', '~', 'a', 'b', 'c', 'd', -1 },
                { 'A', '~', 'a', 'b', 'c', 'd', 0, '^', -1 },
                /* exceeding implementation-defined soft-limit of 64 */
                { 'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd',
                  'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd',
                  'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd',
                  'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd',
                  'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd',
                  'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd',
                  'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd',
                  'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd', -1 },
        };
        term_char_t res[] = {
                PACK1(0),
                PACK2('A', '~'),
                PACK3('A', '~', 0),
                PACK3('A', '~', 'a'),
                TERM_CHAR_NULL,                 /* allocated */
                TERM_CHAR_NULL,                 /* allocated */
                TERM_CHAR_NULL,                 /* allocated */
        };
        uint32_t str[][1024] = {
                { 0, -1 },
                { 'A', '~', -1 },
                { 'A', '~', 0, -1 },
                { 'A', '~', 'a', -1 },
                { 'A', '~', 'a', 'b', 'c', 'd', -1 },
                { 'A', '~', 'a', 'b', 'c', 'd', 0, '^', -1 },
                { 'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd',
                  'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd',
                  'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd',
                  'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd',
                  'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd',
                  'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd',
                  'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd',
                  'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd', -1 },
        };
        size_t n;
        uint32_t next;
        unsigned int i, j;
        const uint32_t *t;

        /*
         * This builds term_char_t objects based on the data in @seqs. It
         * compares the result to @res for packed chars, otherwise it requires
         * them to be allocated.
         * After that, we resolve the UCS-4 string and compare it to the
         * expected strings in @str.
         */

        for (i = 0; i < ELEMENTSOF(seqs); ++i) {
                _term_char_free_ term_char_t c = TERM_CHAR_NULL;

                for (j = 0; j < ELEMENTSOF(seqs[i]); ++j) {
                        next = seqs[i][j];
                        if (next == (uint32_t)-1)
                                break;

                        c = term_char_merge(c, next);
                }

                /* we use TERM_CHAR_NULL as marker for allocated chars here */
                if (term_char_is_null(res[i]))
                        assert_se(term_char_is_allocated(c));
                else
                        assert_se(!memcmp(&c, &res[i], sizeof(c)));

                t = term_char_resolve(c, &n, NULL);
                for (j = 0; j < ELEMENTSOF(str[i]); ++j) {
                        next = str[i][j];
                        if (next == (uint32_t)-1)
                                break;

                        assert_se(t[j] == next);
                }

                assert_se(n == j);
        }
}

/*
 * Line Tests
 *
 * The following tests work on term_line objects and verify their behavior when
 * we modify them. To verify and set line layouts, we have two simple helpers
 * to avoid harcoding the cell-verification all the time:
 *   line_set(): Set a line to a given layout
 *   line_assert(): Verify that a line has a given layout
 *
 * These functions take the line-layout encoded as a string and verify it
 * against, or set it on, a term_line object. The format used to describe a
 * line looks like this:
 *       example: "|   | A |   |   |   |   |   | 10 *AB* |"
 *
 * The string describes the contents of all cells of a line, separated by
 * pipe-symbols ('|'). Whitespace are ignored, the leading pipe-symbol is
 * optional.
 * The description of each cell can contain an arbitrary amount of characters
 * in the range 'A'-'Z', 'a'-'z'. All those are combined and used as term_char_t
 * on this cell. Any numbers in the description are combined and are used as
 * cell-age.
 * The occurrence of a '*'-symbol marks the cell as bold, '/' marks it as italic.
 * You can use those characters multiple times, but only the first one has an
 * effect.
 * For further symbols, see parse_attr().
 *
 * Therefore, the following descriptions are equivalent:
 *       1) "|   | /A*   |   |   |   |   |   | 10 *AB*  |"
 *       2) "|   | /A**  |   |   |   |   |   | 10 *AB*  |"
 *       3) "|   | A* // |   |   |   |   |   | 10 *AB*  |"
 *       4) "|   | A* // |   |   |   |   |   | 1 *AB* 0 |"
 *       5) "|   | A* // |   |   |   |   |   | A1B0*    |"
 *
 * The parser isn't very strict about placement of alpha/numerical characters,
 * but simply appends all found chars. Don't make use of that feature! It's
 * just a stupid parser to simplify these tests. Make them readable!
 */

static void parse_attr(char c, term_char_t *ch, term_attr *attr, term_age_t *age) {
        switch (c) {
        case ' ':
                /* ignore */
                break;
        case '0' ... '9':
                /* increase age */
                *age = *age * 10;
                *age = *age + c - '0';
                break;
        case 'A' ... 'Z':
        case 'a' ... 'z':
                /* add to character */
                *ch = term_char_merge(*ch, c);
                break;
        case '*':
                attr->bold = true;
                break;
        case '/':
                attr->italic = true;
                break;
        default:
                assert_se(0);
                break;
        }
}

static void cell_assert(MY_ASSERT_ARGS, term_cell *c, term_char_t ch, const term_attr *attr, term_age_t age) {
        MY_ASSERT(term_char_equal(c->ch, ch));
        MY_ASSERT(!memcmp(&c->attr, attr, sizeof(*attr)));
        MY_ASSERT(c->age == age);
}
#define CELL_ASSERT(_cell, _ch, _attr, _age) cell_assert(MY_ASSERT_VALS, (_cell), (_ch), (_attr), (_age))

static void line_assert(MY_ASSERT_ARGS, term_line *l, const char *str, unsigned int fill) {
        unsigned int cell_i;
        term_char_t ch = TERM_CHAR_NULL;
        term_attr attr = { };
        term_age_t age = TERM_AGE_NULL;
        char c;

        assert_se(l->fill == fill);

        /* skip leading whitespace */
        while (*str == ' ')
                ++str;

        /* skip leading '|' */
        if (*str == '|')
                ++str;

        cell_i = 0;
        while ((c = *str++)) {
                switch (c) {
                case '|':
                        /* end of cell-description; compare it */
                        assert_se(cell_i < l->n_cells);
                        cell_assert(MY_ASSERT_FORW,
                                    &l->cells[cell_i],
                                    ch,
                                    &attr,
                                    age);

                        ++cell_i;
                        ch = term_char_free(ch);
                        zero(attr);
                        age = TERM_AGE_NULL;
                        break;
                default:
                        parse_attr(c, &ch, &attr, &age);
                        break;
                }
        }

        assert_se(cell_i == l->n_cells);
}
#define LINE_ASSERT(_line, _str, _fill) line_assert(MY_ASSERT_VALS, (_line), (_str), (_fill))

static void line_set(term_line *l, unsigned int pos, const char *str, bool insert_mode) {
        term_char_t ch = TERM_CHAR_NULL;
        term_attr attr = { };
        term_age_t age = TERM_AGE_NULL;
        char c;

        while ((c = *str++))
                parse_attr(c, &ch, &attr, &age);

        term_line_write(l, pos, ch, 1, &attr, age, insert_mode);
}

static void line_resize(term_line *l, unsigned int width, const term_attr *attr, term_age_t age) {
        assert_se(term_line_reserve(l, width, attr, age, width) >= 0);
        term_line_set_width(l, width);
}

static void test_term_line_misc(void) {
        term_line *l;

        assert_se(term_line_new(&l) >= 0);
        assert_se(!term_line_free(l));

        assert_se(term_line_new(NULL) < 0);
        assert_se(!term_line_free(NULL));

        assert_se(term_line_new(&l) >= 0);
        assert_se(l->n_cells == 0);
        assert_se(l->fill == 0);
        assert_se(term_line_reserve(l, 16, NULL, 0, 0) >= 0);
        assert_se(l->n_cells == 16);
        assert_se(l->fill == 0);
        assert_se(term_line_reserve(l, 512, NULL, 0, 0) >= 0);
        assert_se(l->n_cells == 512);
        assert_se(l->fill == 0);
        assert_se(term_line_reserve(l, 16, NULL, 0, 0) >= 0);
        assert_se(l->n_cells == 512);
        assert_se(l->fill == 0);
        assert_se(!term_line_free(l));
}

static void test_term_line_ops(void) {
        term_line *l;
        term_attr attr_regular = { };
        term_attr attr_bold = { .bold = true };
        term_attr attr_italic = { .italic = true };

        assert_se(term_line_new(&l) >= 0);
        line_resize(l, 8, NULL, 0);
        assert_se(l->n_cells == 8);

        LINE_ASSERT(l, "| | | | | | | | |", 0);

        term_line_write(l, 4, TERM_CHAR_NULL, 0, NULL, TERM_AGE_NULL, 0);
        LINE_ASSERT(l, "| | | | | | | | |", 5);

        term_line_write(l, 1, PACK1('A'), 1, NULL, TERM_AGE_NULL, 0);
        LINE_ASSERT(l, "| |A| | | | | | |", 5);

        term_line_write(l, 8, PACK2('A', 'B'), 1, NULL, TERM_AGE_NULL, 0);
        LINE_ASSERT(l, "| |A| | | | | | |", 5);

        term_line_write(l, 7, PACK2('A', 'B'), 1, &attr_regular, 10, 0);
        LINE_ASSERT(l, "| |A| | | | | | 10 AB |", 8);

        term_line_write(l, 7, PACK2('A', 'B'), 1, &attr_bold, 10, 0);
        LINE_ASSERT(l, "| |A| | | | | | 10 *AB* |", 8);

        term_line_reset(l, NULL, TERM_AGE_NULL);

        LINE_ASSERT(l, "|   |   |          |          |          |   |   |   |", 0);
        line_set(l, 2, "*wxyz* 8", 0);
        line_set(l, 3, "/wxyz/ 8", 0);
        LINE_ASSERT(l, "|   |   | *wxyz* 8 | /wxyz/ 8 |          |   |   |   |", 4);
        line_set(l, 2, "*abc* 9", true);
        LINE_ASSERT(l, "|   |   | *abc* 9  | *wxyz* 9 | /wxyz/ 9 | 9 | 9 | 9 |", 5);
        line_set(l, 7, "*abc* 10", true);
        LINE_ASSERT(l, "|   |   | *abc* 9  | *wxyz* 9 | /wxyz/ 9 | 9 | 9 | *abc* 10 |", 8);

        term_line_erase(l, 6, 1, NULL, 11, 0);
        LINE_ASSERT(l, "|   |   | *abc* 9  | *wxyz* 9 | /wxyz/ 9 | 9 | 11 | *abc* 10 |", 8);
        term_line_erase(l, 6, 2, &attr_italic, 12, 0);
        LINE_ASSERT(l, "|   |   | *abc* 9  | *wxyz* 9 | /wxyz/ 9 | 9 | 12 // | 12 // |", 6);
        term_line_erase(l, 7, 2, &attr_regular, 13, 0);
        LINE_ASSERT(l, "|   |   | *abc* 9  | *wxyz* 9 | /wxyz/ 9 | 9 | 12 // | 13 |", 6);
        term_line_delete(l, 1, 3, &attr_bold, 14);
        LINE_ASSERT(l, "|   | /wxyz/ 14 | 14 | 14 // | 14 | 14 ** | 14 ** | 14 ** |", 3);
        term_line_insert(l, 2, 2, &attr_regular, 15);
        LINE_ASSERT(l, "|   | /wxyz/ 14 | 15 | 15 | 15 | 15 // | 15 | 15 ** |", 5);

        assert_se(!term_line_free(l));
}

int main(int argc, char *argv[]) {
        test_term_char_misc();
        test_term_char_packing();
        test_term_char_allocating();

        test_term_line_misc();
        test_term_line_ops();

        return 0;
}