chiark / gitweb /
terminal: add evdev elements to idev
[elogind.git] / src / libsystemd-terminal / test-term-page.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2 /***
3   This file is part of systemd.
4
5   Copyright (C) 2014 David Herrmann <dh.herrmann@gmail.com>
6
7   systemd is free software; you can redistribute it and/or modify it
8   under the terms of the GNU Lesser General Public License as published by
9   the Free Software Foundation; either version 2.1 of the License, or
10   (at your option) any later version.
11
12   systemd is distributed in the hope that it will be useful, but
13   WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   Lesser General Public License for more details.
16
17   You should have received a copy of the GNU Lesser General Public License
18   along with systemd; If not, see <http://www.gnu.org/licenses/>.
19 ***/
20
21 /*
22  * Terminal Page/Line/Cell/Char Tests
23  * This tests internals of terminal page, line, cell and char handling. It
24  * relies on some implementation details, so it might need to be updated if
25  * those internals are changed. They should be fairly obvious, though.
26  */
27
28 #include <assert.h>
29 #include <errno.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include "macro.h"
34 #include "term-internal.h"
35 #include "util.h"
36
37 #define MY_ASSERT_VALS __FILE__, __LINE__, __PRETTY_FUNCTION__
38 #define MY_ASSERT_FORW _FILE, _LINE, _FUNC
39 #define MY_ASSERT_ARGS const char *_FILE, int _LINE, const char *_FUNC
40 #define MY_ASSERT(expr)                                                 \
41         do {                                                            \
42                 if (_unlikely_(!(expr)))                                \
43                         log_assert_failed(#expr, _FILE, _LINE, _FUNC);  \
44         } while (false)                                                 \
45
46 /*
47  * Character Tests
48  *
49  * These tests rely on some implementation details of term_char_t, including
50  * the way we pack characters and the internal layout of "term_char_t". These
51  * tests have to be updated once we change the implementation.
52  */
53
54 #define PACK(v1, v2, v3) \
55                 TERM_CHAR_INIT( \
56                         (((((uint64_t)v1) & 0x1fffffULL) << 43) | \
57                          ((((uint64_t)v2) & 0x1fffffULL) << 22) | \
58                          ((((uint64_t)v3) & 0x1fffffULL) <<  1) | \
59                          0x1) \
60                 )
61 #define PACK1(v1) PACK2((v1), 0x110000)
62 #define PACK2(v1, v2) PACK3((v1), (v2), 0x110000)
63 #define PACK3(v1, v2, v3) PACK((v1), (v2), (v3))
64
65 static void test_term_char_misc(void) {
66         term_char_t c, t;
67
68         /* test TERM_CHAR_NULL handling */
69
70         c = TERM_CHAR_NULL;                     /* c is NULL */
71         assert_se(term_char_same(c, TERM_CHAR_NULL));
72         assert_se(term_char_equal(c, TERM_CHAR_NULL));
73         assert_se(term_char_is_null(c));
74         assert_se(term_char_is_null(TERM_CHAR_NULL));
75         assert_se(!term_char_is_allocated(c));
76
77         /* test single char handling */
78
79         t = term_char_dup_append(c, 'A');       /* t is >A< now */
80         assert_se(!term_char_same(c, t));
81         assert_se(!term_char_equal(c, t));
82         assert_se(!term_char_is_allocated(t));
83         assert_se(!term_char_is_null(t));
84
85         /* test basic combined char handling */
86
87         t = term_char_dup_append(t, '~');
88         t = term_char_dup_append(t, '^');       /* t is >A~^< now */
89         assert_se(!term_char_same(c, t));
90         assert_se(!term_char_is_allocated(t));
91         assert_se(!term_char_is_null(t));
92
93         c = term_char_dup_append(c, 'A');
94         c = term_char_dup_append(c, '~');
95         c = term_char_dup_append(c, '^');       /* c is >A~^< now */
96         assert_se(term_char_same(c, t));
97         assert_se(term_char_equal(c, t));
98
99         /* test more than 2 comb-chars so the chars are allocated */
100
101         t = term_char_dup_append(t, '`');       /* t is >A~^`< now */
102         c = term_char_dup_append(c, '`');       /* c is >A~^`< now */
103         assert_se(!term_char_same(c, t));
104         assert_se(term_char_equal(c, t));
105
106         /* test dup_append() on allocated chars */
107
108         term_char_free(t);
109         t = term_char_dup_append(c, '"');       /* t is >A~^`"< now */
110         assert_se(!term_char_same(c, t));
111         assert_se(!term_char_equal(c, t));
112         c = term_char_merge(c, '"');            /* c is >A~^`"< now */
113         assert_se(!term_char_same(c, t));
114         assert_se(term_char_equal(c, t));
115
116         term_char_free(t);
117         term_char_free(c);
118 }
119
120 static void test_term_char_packing(void)  {
121         uint32_t seqs[][1024] = {
122                 { -1 },
123                 { 0, -1 },
124                 { 'A', '~', -1 },
125                 { 'A', '~', 0, -1 },
126                 { 'A', '~', 'a', -1 },
127         };
128         term_char_t res[] = {
129                 TERM_CHAR_NULL,
130                 PACK1(0),
131                 PACK2('A', '~'),
132                 PACK3('A', '~', 0),
133                 PACK3('A', '~', 'a'),
134         };
135         uint32_t next;
136         unsigned int i, j;
137         term_char_t c = TERM_CHAR_NULL;
138
139         /*
140          * This creates term_char_t objects based on the data in @seqs and
141          * compares the result to @res. Only basic packed types are tested, no
142          * allocations are done.
143          */
144
145         for (i = 0; i < ELEMENTSOF(seqs); ++i) {
146                 for (j = 0; j < ELEMENTSOF(seqs[i]); ++j) {
147                         next = seqs[i][j];
148                         if (next == (uint32_t)-1)
149                                 break;
150
151                         c = term_char_merge(c, next);
152                 }
153
154                 assert_se(!memcmp(&c, &res[i], sizeof(c)));
155                 c = term_char_free(c);
156         }
157 }
158
159 static void test_term_char_allocating(void) {
160         uint32_t seqs[][1024] = {
161                 { 0, -1 },
162                 { 'A', '~', -1 },
163                 { 'A', '~', 0, -1 },
164                 { 'A', '~', 'a', -1 },
165                 { 'A', '~', 'a', 'b', 'c', 'd', -1 },
166                 { 'A', '~', 'a', 'b', 'c', 'd', 0, '^', -1 },
167                 /* exceeding implementation-defined soft-limit of 64 */
168                 { 'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd',
169                   'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd',
170                   'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd',
171                   'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd',
172                   'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd',
173                   'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd',
174                   'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd',
175                   'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd', -1 },
176         };
177         term_char_t res[] = {
178                 PACK1(0),
179                 PACK2('A', '~'),
180                 PACK3('A', '~', 0),
181                 PACK3('A', '~', 'a'),
182                 TERM_CHAR_NULL,                 /* allocated */
183                 TERM_CHAR_NULL,                 /* allocated */
184                 TERM_CHAR_NULL,                 /* allocated */
185         };
186         uint32_t str[][1024] = {
187                 { 0, -1 },
188                 { 'A', '~', -1 },
189                 { 'A', '~', 0, -1 },
190                 { 'A', '~', 'a', -1 },
191                 { 'A', '~', 'a', 'b', 'c', 'd', -1 },
192                 { 'A', '~', 'a', 'b', 'c', 'd', 0, '^', -1 },
193                 { 'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd',
194                   'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd',
195                   'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd',
196                   'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd',
197                   'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd',
198                   'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd',
199                   'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd',
200                   'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd', -1 },
201         };
202         size_t n;
203         uint32_t next;
204         unsigned int i, j;
205         const uint32_t *t;
206
207         /*
208          * This builds term_char_t objects based on the data in @seqs. It
209          * compares the result to @res for packed chars, otherwise it requires
210          * them to be allocated.
211          * After that, we resolve the UCS-4 string and compare it to the
212          * expected strings in @str.
213          */
214
215         for (i = 0; i < ELEMENTSOF(seqs); ++i) {
216                 _term_char_free_ term_char_t c = TERM_CHAR_NULL;
217
218                 for (j = 0; j < ELEMENTSOF(seqs[i]); ++j) {
219                         next = seqs[i][j];
220                         if (next == (uint32_t)-1)
221                                 break;
222
223                         c = term_char_merge(c, next);
224                 }
225
226                 /* we use TERM_CHAR_NULL as marker for allocated chars here */
227                 if (term_char_is_null(res[i]))
228                         assert_se(term_char_is_allocated(c));
229                 else
230                         assert_se(!memcmp(&c, &res[i], sizeof(c)));
231
232                 t = term_char_resolve(c, &n, NULL);
233                 for (j = 0; j < ELEMENTSOF(str[i]); ++j) {
234                         next = str[i][j];
235                         if (next == (uint32_t)-1)
236                                 break;
237
238                         assert_se(t[j] == next);
239                 }
240
241                 assert_se(n == j);
242         }
243 }
244
245 /*
246  * Line Tests
247  *
248  * The following tests work on term_line objects and verify their behavior when
249  * we modify them. To verify and set line layouts, we have two simple helpers
250  * to avoid harcoding the cell-verification all the time:
251  *   line_set(): Set a line to a given layout
252  *   line_assert(): Verify that a line has a given layout
253  *
254  * These functions take the line-layout encoded as a string and verify it
255  * against, or set it on, a term_line object. The format used to describe a
256  * line looks like this:
257  *       example: "|   | A |   |   |   |   |   | 10 *AB* |"
258  *
259  * The string describes the contents of all cells of a line, separated by
260  * pipe-symbols ('|'). Whitespace are ignored, the leading pipe-symbol is
261  * optional.
262  * The description of each cell can contain an arbitrary amount of characters
263  * in the range 'A'-'Z', 'a'-'z'. All those are combined and used as term_char_t
264  * on this cell. Any numbers in the description are combined and are used as
265  * cell-age.
266  * The occurance of a '*'-symbol marks the cell as bold, '/' marks it as italic.
267  * You can use those characters multiple times, but only the first one has an
268  * effect.
269  * For further symbols, see parse_attr().
270  *
271  * Therefore, the following descriptions are equivalent:
272  *       1) "|   | /A*   |   |   |   |   |   | 10 *AB*  |"
273  *       2) "|   | /A**  |   |   |   |   |   | 10 *AB*  |"
274  *       3) "|   | A* // |   |   |   |   |   | 10 *AB*  |"
275  *       4) "|   | A* // |   |   |   |   |   | 1 *AB* 0 |"
276  *       5) "|   | A* // |   |   |   |   |   | A1B0*    |"
277  *
278  * The parser isn't very strict about placement of alpha/numerical characters,
279  * but simply appends all found chars. Don't make use of that feature! It's
280  * just a stupid parser to simplify these tests. Make them readable!
281  */
282
283 static void parse_attr(char c, term_char_t *ch, term_attr *attr, term_age_t *age) {
284         switch (c) {
285         case ' ':
286                 /* ignore */
287                 break;
288         case '0' ... '9':
289                 /* increase age */
290                 *age = *age * 10;
291                 *age = *age + c - '0';
292                 break;
293         case 'A' ... 'Z':
294         case 'a' ... 'z':
295                 /* add to character */
296                 *ch = term_char_merge(*ch, c);
297                 break;
298         case '*':
299                 attr->bold = true;
300                 break;
301         case '/':
302                 attr->italic = true;
303                 break;
304         default:
305                 assert_se(0);
306                 break;
307         }
308 }
309
310 static void cell_assert(MY_ASSERT_ARGS, term_cell *c, term_char_t ch, const term_attr *attr, term_age_t age) {
311         MY_ASSERT(term_char_equal(c->ch, ch));
312         MY_ASSERT(!memcmp(&c->attr, attr, sizeof(*attr)));
313         MY_ASSERT(c->age == age);
314 }
315 #define CELL_ASSERT(_cell, _ch, _attr, _age) cell_assert(MY_ASSERT_VALS, (_cell), (_ch), (_attr), (_age))
316
317 static void line_assert(MY_ASSERT_ARGS, term_line *l, const char *str, unsigned int fill) {
318         unsigned int cell_i;
319         term_char_t ch = TERM_CHAR_NULL;
320         term_attr attr = { };
321         term_age_t age = TERM_AGE_NULL;
322         char c;
323
324         assert_se(l->fill == fill);
325
326         /* skip leading whitespace */
327         while (*str == ' ')
328                 ++str;
329
330         /* skip leading '|' */
331         if (*str == '|')
332                 ++str;
333
334         cell_i = 0;
335         while ((c = *str++)) {
336                 switch (c) {
337                 case '|':
338                         /* end of cell-description; compare it */
339                         assert_se(cell_i < l->n_cells);
340                         cell_assert(MY_ASSERT_FORW,
341                                     &l->cells[cell_i],
342                                     ch,
343                                     &attr,
344                                     age);
345
346                         ++cell_i;
347                         ch = term_char_free(ch);
348                         zero(attr);
349                         age = TERM_AGE_NULL;
350                         break;
351                 default:
352                         parse_attr(c, &ch, &attr, &age);
353                         break;
354                 }
355         }
356
357         assert_se(cell_i == l->n_cells);
358 }
359 #define LINE_ASSERT(_line, _str, _fill) line_assert(MY_ASSERT_VALS, (_line), (_str), (_fill))
360
361 static void line_set(term_line *l, unsigned int pos, const char *str, bool insert_mode) {
362         term_char_t ch = TERM_CHAR_NULL;
363         term_attr attr = { };
364         term_age_t age = TERM_AGE_NULL;
365         char c;
366
367         while ((c = *str++))
368                 parse_attr(c, &ch, &attr, &age);
369
370         term_line_write(l, pos, ch, 1, &attr, age, insert_mode);
371 }
372
373 static void line_resize(term_line *l, unsigned int width, const term_attr *attr, term_age_t age) {
374         assert_se(term_line_reserve(l, width, attr, age, width) >= 0);
375         term_line_set_width(l, width);
376 }
377
378 static void test_term_line_misc(void) {
379         term_line *l;
380
381         assert_se(term_line_new(&l) >= 0);
382         assert_se(!term_line_free(l));
383
384         assert_se(term_line_new(NULL) < 0);
385         assert_se(!term_line_free(NULL));
386
387         assert_se(term_line_new(&l) >= 0);
388         assert_se(l->n_cells == 0);
389         assert_se(l->fill == 0);
390         assert_se(term_line_reserve(l, 16, NULL, 0, 0) >= 0);
391         assert_se(l->n_cells == 16);
392         assert_se(l->fill == 0);
393         assert_se(term_line_reserve(l, 512, NULL, 0, 0) >= 0);
394         assert_se(l->n_cells == 512);
395         assert_se(l->fill == 0);
396         assert_se(term_line_reserve(l, 16, NULL, 0, 0) >= 0);
397         assert_se(l->n_cells == 512);
398         assert_se(l->fill == 0);
399         assert_se(!term_line_free(l));
400 }
401
402 static void test_term_line_ops(void) {
403         term_line *l;
404         term_attr attr_regular = { };
405         term_attr attr_bold = { .bold = true };
406         term_attr attr_italic = { .italic = true };
407
408         assert_se(term_line_new(&l) >= 0);
409         line_resize(l, 8, NULL, 0);
410         assert_se(l->n_cells == 8);
411
412         LINE_ASSERT(l, "| | | | | | | | |", 0);
413
414         term_line_write(l, 4, TERM_CHAR_NULL, 0, NULL, TERM_AGE_NULL, 0);
415         LINE_ASSERT(l, "| | | | | | | | |", 5);
416
417         term_line_write(l, 1, PACK1('A'), 1, NULL, TERM_AGE_NULL, 0);
418         LINE_ASSERT(l, "| |A| | | | | | |", 5);
419
420         term_line_write(l, 8, PACK2('A', 'B'), 1, NULL, TERM_AGE_NULL, 0);
421         LINE_ASSERT(l, "| |A| | | | | | |", 5);
422
423         term_line_write(l, 7, PACK2('A', 'B'), 1, &attr_regular, 10, 0);
424         LINE_ASSERT(l, "| |A| | | | | | 10 AB |", 8);
425
426         term_line_write(l, 7, PACK2('A', 'B'), 1, &attr_bold, 10, 0);
427         LINE_ASSERT(l, "| |A| | | | | | 10 *AB* |", 8);
428
429         term_line_reset(l, NULL, TERM_AGE_NULL);
430
431         LINE_ASSERT(l, "|   |   |          |          |          |   |   |   |", 0);
432         line_set(l, 2, "*wxyz* 8", 0);
433         line_set(l, 3, "/wxyz/ 8", 0);
434         LINE_ASSERT(l, "|   |   | *wxyz* 8 | /wxyz/ 8 |          |   |   |   |", 4);
435         line_set(l, 2, "*abc* 9", true);
436         LINE_ASSERT(l, "|   |   | *abc* 9  | *wxyz* 9 | /wxyz/ 9 | 9 | 9 | 9 |", 5);
437         line_set(l, 7, "*abc* 10", true);
438         LINE_ASSERT(l, "|   |   | *abc* 9  | *wxyz* 9 | /wxyz/ 9 | 9 | 9 | *abc* 10 |", 8);
439
440         term_line_erase(l, 6, 1, NULL, 11, 0);
441         LINE_ASSERT(l, "|   |   | *abc* 9  | *wxyz* 9 | /wxyz/ 9 | 9 | 11 | *abc* 10 |", 8);
442         term_line_erase(l, 6, 2, &attr_italic, 12, 0);
443         LINE_ASSERT(l, "|   |   | *abc* 9  | *wxyz* 9 | /wxyz/ 9 | 9 | 12 // | 12 // |", 6);
444         term_line_erase(l, 7, 2, &attr_regular, 13, 0);
445         LINE_ASSERT(l, "|   |   | *abc* 9  | *wxyz* 9 | /wxyz/ 9 | 9 | 12 // | 13 |", 6);
446         term_line_delete(l, 1, 3, &attr_bold, 14);
447         LINE_ASSERT(l, "|   | /wxyz/ 14 | 14 | 14 // | 14 | 14 ** | 14 ** | 14 ** |", 3);
448         term_line_insert(l, 2, 2, &attr_regular, 15);
449         LINE_ASSERT(l, "|   | /wxyz/ 14 | 15 | 15 | 15 | 15 // | 15 | 15 ** |", 5);
450
451         assert_se(!term_line_free(l));
452 }
453
454 int main(int argc, char *argv[]) {
455         test_term_char_misc();
456         test_term_char_packing();
457         test_term_char_allocating();
458
459         test_term_line_misc();
460         test_term_line_ops();
461
462         return 0;
463 }