chiark / gitweb /
importd: add new bus calls for importing local tar and raw images
[elogind.git] / src / libsystemd-terminal / test-term-parser.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 Parser Tests
23  */
24
25 #include <stdio.h>
26 #include <string.h>
27 #include "macro.h"
28 #include "term-internal.h"
29 #include "utf8.h"
30
31 static void test_term_utf8_invalid(void) {
32         term_utf8 p = { };
33         uint32_t *res;
34         size_t len;
35
36         len = term_utf8_decode(NULL, NULL, 0);
37         assert_se(!len);
38
39         len = term_utf8_decode(&p, NULL, 0);
40         assert_se(len == 1);
41
42         res = NULL;
43         len = term_utf8_decode(NULL, &res, 0);
44         assert_se(!len);
45         assert_se(res != NULL);
46         assert_se(!*res);
47
48         len = term_utf8_decode(&p, &res, 0);
49         assert_se(len == 1);
50         assert_se(res != NULL);
51         assert_se(!*res);
52
53         len = term_utf8_decode(&p, &res, 0xCf);
54         assert_se(len == 0);
55         assert_se(res != NULL);
56         assert_se(!*res);
57
58         len = term_utf8_decode(&p, &res, 0);
59         assert_se(len == 2);
60         assert_se(res != NULL);
61         assert_se(res[0] == 0xCf && res[1] == 0);
62 }
63
64 static void test_term_utf8_range(void) {
65         term_utf8 p = { };
66         uint32_t *res;
67         char u8[4];
68         uint32_t i, j;
69         size_t ulen, len;
70
71         /* Convert all ucs-4 chars to utf-8 and back */
72
73         for (i = 0; i < 0x10FFFF; ++i) {
74                 ulen = utf8_encode_unichar(u8, i);
75                 if (!ulen)
76                         continue;
77
78                 for (j = 0; j < ulen; ++j) {
79                         len = term_utf8_decode(&p, &res, u8[j]);
80                         if (len < 1) {
81                                 assert_se(j + 1 != ulen);
82                                 continue;
83                         }
84
85                         assert_se(j + 1 == ulen);
86                         assert_se(len == 1 && *res == i);
87                         assert_se(i <= 127 || ulen >= 2);
88                 }
89         }
90 }
91
92 static void test_term_utf8_mix(void) {
93         static const char source[] = {
94                 0x00,                           /* normal 0 */
95                 0xC0, 0x80,                     /* overlong 0 */
96                 0xC0, 0x81,                     /* overlong 1 */
97                 0xE0, 0x80, 0x81,               /* overlong 1 */
98                 0xF0, 0x80, 0x80, 0x81,         /* overlong 1 */
99                 0xC0, 0x00,                     /* invalid continuation */
100                 0xC0, 0xC0, 0x81,               /* invalid continuation with a following overlong 1 */
101                 0xF8, 0x80, 0x80, 0x80, 0x81,   /* overlong 1 with 5 bytes */
102                 0xE0, 0x80, 0xC0, 0x81,         /* invalid 3-byte followed by valid 2-byte */
103                 0xF0, 0x80, 0x80, 0xC0, 0x81,   /* invalid 4-byte followed by valid 2-byte */
104         };
105         static const uint32_t result[] = {
106                 0x0000,
107                 0x0000,
108                 0x0001,
109                 0x0001,
110                 0x0001,
111                 0x00C0, 0x0000,
112                 0x00C0, 0x0001,
113                 0x00F8, 0x0080, 0x0080, 0x0080, 0x0081,
114                 0x00E0, 0x0080, 0x0001,
115                 0x00F0, 0x0080, 0x0080, 0x0001,
116         };
117         term_utf8 p = { };
118         uint32_t *res;
119         unsigned int i, j;
120         size_t len;
121
122         for (i = 0, j = 0; i < sizeof(source); ++i) {
123                 len = term_utf8_decode(&p, &res, source[i]);
124                 if (len < 1)
125                         continue;
126
127                 assert_se(j + len <= ELEMENTSOF(result));
128                 assert_se(!memcmp(res, &result[j], sizeof(uint32_t) * len));
129                 j += len;
130         }
131
132         assert_se(j == ELEMENTSOF(result));
133 }
134
135 int main(int argc, char *argv[]) {
136         test_term_utf8_invalid();
137         test_term_utf8_range();
138         test_term_utf8_mix();
139
140         return 0;
141 }