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.
31 #include "term-internal.h"
33 #define MY_ASSERT_VALS __FILE__, __LINE__, __PRETTY_FUNCTION__
34 #define MY_ASSERT_FORW _FILE, _LINE, _FUNC
35 #define MY_ASSERT_ARGS const char *_FILE, int _LINE, const char *_FUNC
36 #define MY_ASSERT(expr) \
38 if (_unlikely_(!(expr))) \
39 log_assert_failed(#expr, _FILE, _LINE, _FUNC); \
45 * These tests rely on some implementation details of term_char_t, including
46 * the way we pack characters and the internal layout of "term_char_t". These
47 * tests have to be updated once we change the implementation.
50 #define PACK(v1, v2, v3) \
52 (((((uint64_t)v1) & 0x1fffffULL) << 43) | \
53 ((((uint64_t)v2) & 0x1fffffULL) << 22) | \
54 ((((uint64_t)v3) & 0x1fffffULL) << 1) | \
57 #define PACK1(v1) PACK2((v1), 0x110000)
58 #define PACK2(v1, v2) PACK3((v1), (v2), 0x110000)
59 #define PACK3(v1, v2, v3) PACK((v1), (v2), (v3))
61 static void test_term_char_misc(void) {
64 /* test TERM_CHAR_NULL handling */
66 c = TERM_CHAR_NULL; /* c is NULL */
67 assert_se(term_char_same(c, TERM_CHAR_NULL));
68 assert_se(term_char_equal(c, TERM_CHAR_NULL));
69 assert_se(term_char_is_null(c));
70 assert_se(term_char_is_null(TERM_CHAR_NULL));
71 assert_se(!term_char_is_allocated(c));
73 /* test single char handling */
75 t = term_char_dup_append(c, 'A'); /* t is >A< now */
76 assert_se(!term_char_same(c, t));
77 assert_se(!term_char_equal(c, t));
78 assert_se(!term_char_is_allocated(t));
79 assert_se(!term_char_is_null(t));
81 /* test basic combined char handling */
83 t = term_char_dup_append(t, '~');
84 t = term_char_dup_append(t, '^'); /* t is >A~^< now */
85 assert_se(!term_char_same(c, t));
86 assert_se(!term_char_is_allocated(t));
87 assert_se(!term_char_is_null(t));
89 c = term_char_dup_append(c, 'A');
90 c = term_char_dup_append(c, '~');
91 c = term_char_dup_append(c, '^'); /* c is >A~^< now */
92 assert_se(term_char_same(c, t));
93 assert_se(term_char_equal(c, t));
95 /* test more than 2 comb-chars so the chars are allocated */
97 t = term_char_dup_append(t, '`'); /* t is >A~^`< now */
98 c = term_char_dup_append(c, '`'); /* c is >A~^`< now */
99 assert_se(!term_char_same(c, t));
100 assert_se(term_char_equal(c, t));
102 /* test dup_append() on allocated chars */
105 t = term_char_dup_append(c, '"'); /* t is >A~^`"< now */
106 assert_se(!term_char_same(c, t));
107 assert_se(!term_char_equal(c, t));
108 c = term_char_merge(c, '"'); /* c is >A~^`"< now */
109 assert_se(!term_char_same(c, t));
110 assert_se(term_char_equal(c, t));
116 static void test_term_char_packing(void) {
117 uint32_t seqs[][1024] = {
122 { 'A', '~', 'a', -1 },
124 term_char_t res[] = {
129 PACK3('A', '~', 'a'),
133 term_char_t c = TERM_CHAR_NULL;
136 * This creates term_char_t objects based on the data in @seqs and
137 * compares the result to @res. Only basic packed types are tested, no
138 * allocations are done.
141 for (i = 0; i < ELEMENTSOF(seqs); ++i) {
142 for (j = 0; j < ELEMENTSOF(seqs[i]); ++j) {
144 if (next == (uint32_t)-1)
147 c = term_char_merge(c, next);
150 assert_se(!memcmp(&c, &res[i], sizeof(c)));
151 c = term_char_free(c);
155 static void test_term_char_allocating(void) {
156 uint32_t seqs[][1024] = {
160 { 'A', '~', 'a', -1 },
161 { 'A', '~', 'a', 'b', 'c', 'd', -1 },
162 { 'A', '~', 'a', 'b', 'c', 'd', 0, '^', -1 },
163 /* exceeding implementation-defined soft-limit of 64 */
164 { 'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd',
165 'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd',
166 'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd',
167 'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd',
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', 'd', -1 },
173 term_char_t res[] = {
177 PACK3('A', '~', 'a'),
178 TERM_CHAR_NULL, /* allocated */
179 TERM_CHAR_NULL, /* allocated */
180 TERM_CHAR_NULL, /* allocated */
182 uint32_t str[][1024] = {
186 { 'A', '~', 'a', -1 },
187 { 'A', '~', 'a', 'b', 'c', 'd', -1 },
188 { 'A', '~', 'a', 'b', 'c', 'd', 0, '^', -1 },
189 { 'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd',
190 'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd',
191 'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd',
192 'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd',
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', -1 },
204 * This builds term_char_t objects based on the data in @seqs. It
205 * compares the result to @res for packed chars, otherwise it requires
206 * them to be allocated.
207 * After that, we resolve the UCS-4 string and compare it to the
208 * expected strings in @str.
211 for (i = 0; i < ELEMENTSOF(seqs); ++i) {
212 _term_char_free_ term_char_t c = TERM_CHAR_NULL;
214 for (j = 0; j < ELEMENTSOF(seqs[i]); ++j) {
216 if (next == (uint32_t)-1)
219 c = term_char_merge(c, next);
222 /* we use TERM_CHAR_NULL as marker for allocated chars here */
223 if (term_char_is_null(res[i]))
224 assert_se(term_char_is_allocated(c));
226 assert_se(!memcmp(&c, &res[i], sizeof(c)));
228 t = term_char_resolve(c, &n, NULL);
229 for (j = 0; j < ELEMENTSOF(str[i]); ++j) {
231 if (next == (uint32_t)-1)
234 assert_se(t[j] == next);
244 * The following tests work on term_line objects and verify their behavior when
245 * we modify them. To verify and set line layouts, we have two simple helpers
246 * to avoid harcoding the cell-verification all the time:
247 * line_set(): Set a line to a given layout
248 * line_assert(): Verify that a line has a given layout
250 * These functions take the line-layout encoded as a string and verify it
251 * against, or set it on, a term_line object. The format used to describe a
252 * line looks like this:
253 * example: "| | A | | | | | | 10 *AB* |"
255 * The string describes the contents of all cells of a line, separated by
256 * pipe-symbols ('|'). Whitespace are ignored, the leading pipe-symbol is
258 * The description of each cell can contain an arbitrary amount of characters
259 * in the range 'A'-'Z', 'a'-'z'. All those are combined and used as term_char_t
260 * on this cell. Any numbers in the description are combined and are used as
262 * The occurrence of a '*'-symbol marks the cell as bold, '/' marks it as italic.
263 * You can use those characters multiple times, but only the first one has an
265 * For further symbols, see parse_attr().
267 * Therefore, the following descriptions are equivalent:
268 * 1) "| | /A* | | | | | | 10 *AB* |"
269 * 2) "| | /A** | | | | | | 10 *AB* |"
270 * 3) "| | A* // | | | | | | 10 *AB* |"
271 * 4) "| | A* // | | | | | | 1 *AB* 0 |"
272 * 5) "| | A* // | | | | | | A1B0* |"
274 * The parser isn't very strict about placement of alpha/numerical characters,
275 * but simply appends all found chars. Don't make use of that feature! It's
276 * just a stupid parser to simplify these tests. Make them readable!
279 static void parse_attr(char c, term_char_t *ch, term_attr *attr, term_age_t *age) {
287 *age = *age + c - '0';
291 /* add to character */
292 *ch = term_char_merge(*ch, c);
306 static void cell_assert(MY_ASSERT_ARGS, term_cell *c, term_char_t ch, const term_attr *attr, term_age_t age) {
307 MY_ASSERT(term_char_equal(c->ch, ch));
308 MY_ASSERT(!memcmp(&c->attr, attr, sizeof(*attr)));
309 MY_ASSERT(c->age == age);
311 #define CELL_ASSERT(_cell, _ch, _attr, _age) cell_assert(MY_ASSERT_VALS, (_cell), (_ch), (_attr), (_age))
313 static void line_assert(MY_ASSERT_ARGS, term_line *l, const char *str, unsigned int fill) {
315 term_char_t ch = TERM_CHAR_NULL;
316 term_attr attr = { };
317 term_age_t age = TERM_AGE_NULL;
320 assert_se(l->fill == fill);
322 /* skip leading whitespace */
326 /* skip leading '|' */
331 while ((c = *str++)) {
334 /* end of cell-description; compare it */
335 assert_se(cell_i < l->n_cells);
336 cell_assert(MY_ASSERT_FORW,
343 ch = term_char_free(ch);
348 parse_attr(c, &ch, &attr, &age);
353 assert_se(cell_i == l->n_cells);
355 #define LINE_ASSERT(_line, _str, _fill) line_assert(MY_ASSERT_VALS, (_line), (_str), (_fill))
357 static void line_set(term_line *l, unsigned int pos, const char *str, bool insert_mode) {
358 term_char_t ch = TERM_CHAR_NULL;
359 term_attr attr = { };
360 term_age_t age = TERM_AGE_NULL;
364 parse_attr(c, &ch, &attr, &age);
366 term_line_write(l, pos, ch, 1, &attr, age, insert_mode);
369 static void line_resize(term_line *l, unsigned int width, const term_attr *attr, term_age_t age) {
370 assert_se(term_line_reserve(l, width, attr, age, width) >= 0);
371 term_line_set_width(l, width);
374 static void test_term_line_misc(void) {
377 assert_se(term_line_new(&l) >= 0);
378 assert_se(!term_line_free(l));
380 assert_se(term_line_new(NULL) < 0);
381 assert_se(!term_line_free(NULL));
383 assert_se(term_line_new(&l) >= 0);
384 assert_se(l->n_cells == 0);
385 assert_se(l->fill == 0);
386 assert_se(term_line_reserve(l, 16, NULL, 0, 0) >= 0);
387 assert_se(l->n_cells == 16);
388 assert_se(l->fill == 0);
389 assert_se(term_line_reserve(l, 512, NULL, 0, 0) >= 0);
390 assert_se(l->n_cells == 512);
391 assert_se(l->fill == 0);
392 assert_se(term_line_reserve(l, 16, NULL, 0, 0) >= 0);
393 assert_se(l->n_cells == 512);
394 assert_se(l->fill == 0);
395 assert_se(!term_line_free(l));
398 static void test_term_line_ops(void) {
400 term_attr attr_regular = { };
401 term_attr attr_bold = { .bold = true };
402 term_attr attr_italic = { .italic = true };
404 assert_se(term_line_new(&l) >= 0);
405 line_resize(l, 8, NULL, 0);
406 assert_se(l->n_cells == 8);
408 LINE_ASSERT(l, "| | | | | | | | |", 0);
410 term_line_write(l, 4, TERM_CHAR_NULL, 0, NULL, TERM_AGE_NULL, 0);
411 LINE_ASSERT(l, "| | | | | | | | |", 5);
413 term_line_write(l, 1, PACK1('A'), 1, NULL, TERM_AGE_NULL, 0);
414 LINE_ASSERT(l, "| |A| | | | | | |", 5);
416 term_line_write(l, 8, PACK2('A', 'B'), 1, NULL, TERM_AGE_NULL, 0);
417 LINE_ASSERT(l, "| |A| | | | | | |", 5);
419 term_line_write(l, 7, PACK2('A', 'B'), 1, &attr_regular, 10, 0);
420 LINE_ASSERT(l, "| |A| | | | | | 10 AB |", 8);
422 term_line_write(l, 7, PACK2('A', 'B'), 1, &attr_bold, 10, 0);
423 LINE_ASSERT(l, "| |A| | | | | | 10 *AB* |", 8);
425 term_line_reset(l, NULL, TERM_AGE_NULL);
427 LINE_ASSERT(l, "| | | | | | | | |", 0);
428 line_set(l, 2, "*wxyz* 8", 0);
429 line_set(l, 3, "/wxyz/ 8", 0);
430 LINE_ASSERT(l, "| | | *wxyz* 8 | /wxyz/ 8 | | | | |", 4);
431 line_set(l, 2, "*abc* 9", true);
432 LINE_ASSERT(l, "| | | *abc* 9 | *wxyz* 9 | /wxyz/ 9 | 9 | 9 | 9 |", 5);
433 line_set(l, 7, "*abc* 10", true);
434 LINE_ASSERT(l, "| | | *abc* 9 | *wxyz* 9 | /wxyz/ 9 | 9 | 9 | *abc* 10 |", 8);
436 term_line_erase(l, 6, 1, NULL, 11, 0);
437 LINE_ASSERT(l, "| | | *abc* 9 | *wxyz* 9 | /wxyz/ 9 | 9 | 11 | *abc* 10 |", 8);
438 term_line_erase(l, 6, 2, &attr_italic, 12, 0);
439 LINE_ASSERT(l, "| | | *abc* 9 | *wxyz* 9 | /wxyz/ 9 | 9 | 12 // | 12 // |", 6);
440 term_line_erase(l, 7, 2, &attr_regular, 13, 0);
441 LINE_ASSERT(l, "| | | *abc* 9 | *wxyz* 9 | /wxyz/ 9 | 9 | 12 // | 13 |", 6);
442 term_line_delete(l, 1, 3, &attr_bold, 14);
443 LINE_ASSERT(l, "| | /wxyz/ 14 | 14 | 14 // | 14 | 14 ** | 14 ** | 14 ** |", 3);
444 term_line_insert(l, 2, 2, &attr_regular, 15);
445 LINE_ASSERT(l, "| | /wxyz/ 14 | 15 | 15 | 15 | 15 // | 15 | 15 ** |", 5);
447 assert_se(!term_line_free(l));
450 int main(int argc, char *argv[]) {
451 test_term_char_misc();
452 test_term_char_packing();
453 test_term_char_allocating();
455 test_term_line_misc();
456 test_term_line_ops();