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 Parser Tests
31 #include "term-internal.h"
34 static void test_term_utf8_invalid(void) {
39 res = term_utf8_decode(NULL, NULL, 0);
40 assert_se(res == NULL);
42 res = term_utf8_decode(&p, NULL, 0);
43 assert_se(res != NULL);
46 res = term_utf8_decode(NULL, &len, 0);
47 assert_se(res == NULL);
51 res = term_utf8_decode(&p, &len, 0);
52 assert_se(res != NULL);
56 res = term_utf8_decode(&p, &len, 0xCf);
57 assert_se(res == NULL);
61 res = term_utf8_decode(&p, &len, 0x0);
62 assert_se(res != NULL);
66 static void test_term_utf8_range(void) {
73 /* Convert all ucs-4 chars to utf-8 and back */
75 for (i = 0; i < 0x10FFFF; ++i) {
76 ulen = term_utf8_encode(u8, i);
80 for (j = 0; j < ulen; ++j) {
81 res = term_utf8_decode(&p, &len, u8[j]);
83 assert_se(j + 1 != ulen);
87 assert_se(j + 1 == ulen);
88 assert_se(len == 1 && *res == i);
89 assert_se(i <= 127 || ulen >= 2);
94 static void test_term_utf8_mix(void) {
95 static const char source[] = {
97 0xC0, 0x80, /* overlong 0 */
98 0xC0, 0x81, /* overlong 1 */
99 0xE0, 0x80, 0x81, /* overlong 1 */
100 0xF0, 0x80, 0x80, 0x81, /* overlong 1 */
101 0xC0, 0x00, /* invalid continuation */
102 0xC0, 0xC0, 0x81, /* invalid continuation with a following overlong 1 */
103 0xF8, 0x80, 0x80, 0x80, 0x81, /* overlong 1 with 5 bytes */
104 0xE0, 0x80, 0xC0, 0x81, /* invalid 3-byte followed by valid 2-byte */
105 0xF0, 0x80, 0x80, 0xC0, 0x81, /* invalid 4-byte followed by valid 2-byte */
107 static const uint32_t result[] = {
115 0x00F8, 0x0080, 0x0080, 0x0080, 0x0081,
116 0x00E0, 0x0080, 0x0001,
117 0x00F0, 0x0080, 0x0080, 0x0001,
124 for (i = 0, j = 0; i < sizeof(source); ++i) {
125 res = term_utf8_decode(&p, &len, source[i]);
129 assert_se(j + len <= ELEMENTSOF(result));
130 assert_se(!memcmp(res, &result[j], sizeof(uint32_t) * len));
134 assert_se(j == ELEMENTSOF(result));
137 int main(int argc, char *argv[]) {
138 test_term_utf8_invalid();
139 test_term_utf8_range();
140 test_term_utf8_mix();