1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
3 This file is part of systemd.
5 Copyright (C) 2014 David Herrmann <dh.herrmann@gmail.com>
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.
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.
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/>.
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.
34 #include "term-internal.h"
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) \
42 if (_unlikely_(!(expr))) \
43 log_assert_failed(#expr, _FILE, _LINE, _FUNC); \
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.
54 #define PACK(v1, v2, v3) \
56 (((((uint64_t)v1) & 0x1fffffULL) << 43) | \
57 ((((uint64_t)v2) & 0x1fffffULL) << 22) | \
58 ((((uint64_t)v3) & 0x1fffffULL) << 1) | \
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))
65 static void test_term_char_misc(void) {
68 /* test TERM_CHAR_NULL handling */
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
));
77 /* test single char handling */
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
));
85 /* test basic combined char handling */
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
));
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
));
99 /* test more than 2 comb-chars so the chars are allocated */
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
));
106 /* test dup_append() on allocated chars */
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
));
120 static void test_term_char_packing(void) {
121 uint32_t seqs
[][1024] = {
126 { 'A', '~', 'a', -1 },
128 term_char_t res
[] = {
133 PACK3('A', '~', 'a'),
137 term_char_t c
= TERM_CHAR_NULL
;
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.
145 for (i
= 0; i
< ELEMENTSOF(seqs
); ++i
) {
146 for (j
= 0; j
< ELEMENTSOF(seqs
[i
]); ++j
) {
148 if (next
== (uint32_t)-1)
151 c
= term_char_merge(c
, next
);
154 assert_se(!memcmp(&c
, &res
[i
], sizeof(c
)));
155 c
= term_char_free(c
);
159 static void test_term_char_allocating(void) {
160 uint32_t seqs
[][1024] = {
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 },
177 term_char_t res
[] = {
181 PACK3('A', '~', 'a'),
182 TERM_CHAR_NULL
, /* allocated */
183 TERM_CHAR_NULL
, /* allocated */
184 TERM_CHAR_NULL
, /* allocated */
186 uint32_t str
[][1024] = {
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 },
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.
215 for (i
= 0; i
< ELEMENTSOF(seqs
); ++i
) {
216 _term_char_free_ term_char_t c
= TERM_CHAR_NULL
;
218 for (j
= 0; j
< ELEMENTSOF(seqs
[i
]); ++j
) {
220 if (next
== (uint32_t)-1)
223 c
= term_char_merge(c
, next
);
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
));
230 assert_se(!memcmp(&c
, &res
[i
], sizeof(c
)));
232 t
= term_char_resolve(c
, &n
, NULL
);
233 for (j
= 0; j
< ELEMENTSOF(str
[i
]); ++j
) {
235 if (next
== (uint32_t)-1)
238 assert_se(t
[j
] == next
);
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
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* |"
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
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
266 * The occurrence 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
269 * For further symbols, see parse_attr().
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* |"
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!
283 static void parse_attr(char c
, term_char_t
*ch
, term_attr
*attr
, term_age_t
*age
) {
291 *age
= *age
+ c
- '0';
295 /* add to character */
296 *ch
= term_char_merge(*ch
, c
);
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
);
315 #define CELL_ASSERT(_cell, _ch, _attr, _age) cell_assert(MY_ASSERT_VALS, (_cell), (_ch), (_attr), (_age))
317 static void line_assert(MY_ASSERT_ARGS
, term_line
*l
, const char *str
, unsigned int fill
) {
319 term_char_t ch
= TERM_CHAR_NULL
;
320 term_attr attr
= { };
321 term_age_t age
= TERM_AGE_NULL
;
324 assert_se(l
->fill
== fill
);
326 /* skip leading whitespace */
330 /* skip leading '|' */
335 while ((c
= *str
++)) {
338 /* end of cell-description; compare it */
339 assert_se(cell_i
< l
->n_cells
);
340 cell_assert(MY_ASSERT_FORW
,
347 ch
= term_char_free(ch
);
352 parse_attr(c
, &ch
, &attr
, &age
);
357 assert_se(cell_i
== l
->n_cells
);
359 #define LINE_ASSERT(_line, _str, _fill) line_assert(MY_ASSERT_VALS, (_line), (_str), (_fill))
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
;
368 parse_attr(c
, &ch
, &attr
, &age
);
370 term_line_write(l
, pos
, ch
, 1, &attr
, age
, insert_mode
);
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
);
378 static void test_term_line_misc(void) {
381 assert_se(term_line_new(&l
) >= 0);
382 assert_se(!term_line_free(l
));
384 assert_se(term_line_new(NULL
) < 0);
385 assert_se(!term_line_free(NULL
));
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
));
402 static void test_term_line_ops(void) {
404 term_attr attr_regular
= { };
405 term_attr attr_bold
= { .bold
= true };
406 term_attr attr_italic
= { .italic
= true };
408 assert_se(term_line_new(&l
) >= 0);
409 line_resize(l
, 8, NULL
, 0);
410 assert_se(l
->n_cells
== 8);
412 LINE_ASSERT(l
, "| | | | | | | | |", 0);
414 term_line_write(l
, 4, TERM_CHAR_NULL
, 0, NULL
, TERM_AGE_NULL
, 0);
415 LINE_ASSERT(l
, "| | | | | | | | |", 5);
417 term_line_write(l
, 1, PACK1('A'), 1, NULL
, TERM_AGE_NULL
, 0);
418 LINE_ASSERT(l
, "| |A| | | | | | |", 5);
420 term_line_write(l
, 8, PACK2('A', 'B'), 1, NULL
, TERM_AGE_NULL
, 0);
421 LINE_ASSERT(l
, "| |A| | | | | | |", 5);
423 term_line_write(l
, 7, PACK2('A', 'B'), 1, &attr_regular
, 10, 0);
424 LINE_ASSERT(l
, "| |A| | | | | | 10 AB |", 8);
426 term_line_write(l
, 7, PACK2('A', 'B'), 1, &attr_bold
, 10, 0);
427 LINE_ASSERT(l
, "| |A| | | | | | 10 *AB* |", 8);
429 term_line_reset(l
, NULL
, TERM_AGE_NULL
);
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);
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);
451 assert_se(!term_line_free(l
));
454 int main(int argc
, char *argv
[]) {
455 test_term_char_misc();
456 test_term_char_packing();
457 test_term_char_allocating();
459 test_term_line_misc();
460 test_term_line_ops();