Debian Codegraph @

test/utf8.c

raw
1 /*
2 * Copyright © 2014 Ran Benita <[email protected]>
3 * SPDX-License-Identifier: MIT
4 */
5
6 #include "config.h"
7
8 #include <assert.h>
9 #include <inttypes.h>
10 #include <stdbool.h>
11 #include <stddef.h>
12 #include <string.h>
13
14 #include "src/keysym.h"
15 #include "test.h"
16 #include "utf8.h"
17 #include "utf8-decoding.h"
18 #include "utils.h"
19
20 #define VALID(lit) assert(is_valid_utf8(lit, sizeof(lit)-1))
21 #define INVALID(lit) assert(!is_valid_utf8(lit, sizeof(lit)-1))
22
23 static void
24 test_is_valid_utf8(void)
25 {
26 /*
27 * Mostly taken from:
28 * https://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt
29 */
30
31 VALID("ascii");
32 VALID("\xCE\xBA\xE1\xBD\xB9\xCF\x83\xCE\xBC\xCE\xB5");
33
34 VALID("");
35 VALID("\x00");
36 VALID("\x00\x00");
37
38 VALID("\x50");
39 VALID("\xC2\x80");
40 VALID("\xE0\xA0\x80");
41 VALID("\xF0\x90\x80\x80");
42
43 /* 5/6-byte continuations aren't allowed (unlike UTF-8-test). */
44 INVALID("\xF8\x88\x80\x80\x80");
45 INVALID("\xFC\x84\x80\x80\x80\x80");
46
47 VALID("\x7F");
48 VALID("\xDF\xBF");
49 VALID("\xEF\xBF\xBF");
50 /* VALID("\xF7\xBF\xBF\xBF"); */
51
52 /* 5/6-byte continuations aren't allowed (unlike UTF-8-test). */
53 INVALID("\xFB\xBF\xBF\xBF\xBF");
54 INVALID("\xFD\xBFxBF\xBF\xBF");
55
56 VALID("\xED\x9F\xBF");
57 VALID("\xEE\x80\x80");
58 VALID("\xEF\xBF\xBD");
59 VALID("\xF4\x8F\xBF\xBF");
60 /* VALID("\xF4\x90\x80\x80"); */
61
62 INVALID("\x80");
63 INVALID("\xBF");
64 INVALID("\x80\xBF");
65 INVALID("\x80\xBF\x80");
66 INVALID("\x80\xBF\x80\xBF");
67 INVALID("\x80\xBF\x80\xBF\x80");
68 INVALID("\x80\xBF\x80\xBF\x80\xBF");
69 INVALID("\x80\xBF\x80\xBF\x80\xBF\x80");
70 INVALID("\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8A\x8B\x8C\x8D\x8E\x8F"
71 "\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9A\x9B\x9C\x9D\x9E\x9F"
72 "\xA0\xA1\xA2\xA3\xA4\xA5\xA6\xA7\xA8\xA9\xAA\xAB\xAC\xAD\xAE\xAF"
73 "\xB0\xB1\xB2\xB3\xB4\xB5\xB6\xB7\xB8\xB9\xBA\xBB\xBC\xBD\xBE\xBF");
74
75 INVALID("\xC0 \xC1 \xC2 \xC3 \xC4 \xC5 \xC6 \xC7 \xC8 \xC9 \xCA \xCB \xCC "
76 "\xCD \xCE \xCF "
77 "\xD0 \xD1 \xD2 \xD3 \xD4 \xD5 \xD6 \xD7 \xD8 \xD9 \xDA \xDB \xDD "
78 "\xDD \xDE \xDF ");
79 INVALID("\xF0 \xF1 \xF2 \xF3 \xF4 \xF5 \xF6 \xF7 ");
80 INVALID("\xF8 \xF9 \xFA \xFB ");
81 INVALID("\xFC \xFD ");
82
83 INVALID("\xC0");
84 INVALID("\xE0\x80");
85 INVALID("\xF0\x80\x80");
86 INVALID("\xF8\x80\x80\x80");
87 INVALID("\xFC\x80\x80\x80\x80");
88 INVALID("\xDF");
89 INVALID("\xEF\xBF");
90 INVALID("\xF7\xBF\xBF");
91 INVALID("\xFB\xBF\xBF\xBF");
92 INVALID("\xFD\xBF\xBF\xBF\xBF");
93
94 INVALID("\xC0\xE0\x80\xF0\x80\x80\xF8\x80\x80\x80\xFC\x80\x80\x80\x80"
95 "\xDF\xEF\xBF\xF7\xBF\xBF\xFB\xBF\xBF\xBF\xFD\xBF\xBF\xBF\xBF");
96
97 INVALID("\xFE");
98 INVALID("\xFF");
99 INVALID("\xFE\xFE\xFF\xFF");
100
101 INVALID("\xC0\xAF");
102 INVALID("\xE0\x80\xAF");
103 INVALID("\xF0\x80\x80\xAF");
104 INVALID("\xF8\x80\x80\x80\xAF");
105 INVALID("\xFC\x80\x80\x80\x80\xAF");
106
107 INVALID("\xC1\xBF");
108 INVALID("\xE0\x9F\xBF");
109 INVALID("\xF0\x8F\xBF\xBF");
110 INVALID("\xF8\x87\xBF\xBF\xBF");
111 INVALID("\xFC\x83\xBF\xBF\xBF\xBF");
112
113 INVALID("\xC0\x80");
114 INVALID("\xE0\x80\x80");
115 INVALID("\xF0\x80\x80\x80");
116 INVALID("\xF8\x80\x80\x80\x80");
117 INVALID("\xFC\x80\x80\x80\x80\x80");
118
119 INVALID("\xED\xA0\x80");
120 INVALID("\xED\xAD\xBF");
121 INVALID("\xED\xAE\x80");
122 INVALID("\xED\xAF\xBF");
123 INVALID("\xED\xB0\x80");
124 INVALID("\xED\xBE\x80");
125 INVALID("\xED\xBF\xBF");
126
127 INVALID("\xED\xA0\x80\xED\xB0\x80");
128 INVALID("\xED\xA0\x80\xED\xBF\xBF");
129 INVALID("\xED\xAD\xBF\xED\xB0\x80");
130 INVALID("\xED\xAD\xBF\xED\xBF\xBF");
131 INVALID("\xED\xAE\x80\xED\xB0\x80");
132 INVALID("\xED\xAE\x80\xED\xBF\xBF");
133 INVALID("\xED\xAF\xBF\xED\xB0\x80");
134 INVALID("\xED\xAF\xBF\xED\xBF\xBF");
135
136 /* INVALID("\xEF\xBF\xBE"); */
137 /* INVALID("\xEF\xBF\xBF"); */
138 }
139
140 static void
141 check_utf32_to_utf8(uint32_t unichar, int expected_length, const char *expected) {
142 char buffer[XKB_KEYSYM_UTF8_MAX_SIZE];
143 int length;
144
145 length = utf32_to_utf8(unichar, buffer);
146
147 assert(length == expected_length);
148 assert(streq(buffer, expected));
149 }
150
151 static void
152 test_utf32_to_utf8(void)
153 {
154 check_utf32_to_utf8(0x0, 2, "");
155 check_utf32_to_utf8(0x40, 2, "\x40");
156 check_utf32_to_utf8(0xA1, 3, "\xc2\xa1");
157 check_utf32_to_utf8(0x2701, 4, "\xe2\x9c\x81");
158 check_utf32_to_utf8(0xd800, 0, ""); // Unicode surrogates
159 check_utf32_to_utf8(0xdfff, 0, ""); // Unicode surrogates
160 check_utf32_to_utf8(0x1f004, 5, "\xf0\x9f\x80\x84");
161 check_utf32_to_utf8(0x110000, 0, "");
162 check_utf32_to_utf8(0xffffffff, 0, "");
163 }
164
165 static void
166 /* Check roundtrip UTF-32 → UTF-8 → UTF-32 */
167 test_utf8_to_utf32(void)
168 {
169 char buffer[XKB_KEYSYM_UTF8_MAX_SIZE];
170 for (uint32_t cp = 0; cp < 0x10ffff; cp++) {
171 int length = utf32_to_utf8(cp, buffer) - 1;
172 /* Check surrogates */
173 if (is_surrogate(cp)) {
174 assert(length == -1);
175 } else {
176 assert(length > 0);
177 size_t length2 = 0;
178 uint32_t cp2 = utf8_next_code_point(buffer, (size_t)length, &length2);
179 assert(cp2 != INVALID_UTF8_CODE_POINT && cp2 == cp &&
180 length2 == (size_t)length);
181 }
182 }
183 }
184
185 int
186 main(void)
187 {
188 test_init();
189
190 test_is_valid_utf8();
191 test_utf32_to_utf8();
192 test_utf8_to_utf32();
193
194 return 0;
195 }