xref: /aosp_15_r20/external/libxkbcommon/test/utf8.c (revision 2b949d0487e80d67f1fda82db69e101e761f8064)
1*2b949d04SAndroid Build Coastguard Worker /*
2*2b949d04SAndroid Build Coastguard Worker  * Copyright © 2014 Ran Benita <[email protected]>
3*2b949d04SAndroid Build Coastguard Worker  *
4*2b949d04SAndroid Build Coastguard Worker  * Permission is hereby granted, free of charge, to any person obtaining a
5*2b949d04SAndroid Build Coastguard Worker  * copy of this software and associated documentation files (the "Software"),
6*2b949d04SAndroid Build Coastguard Worker  * to deal in the Software without restriction, including without limitation
7*2b949d04SAndroid Build Coastguard Worker  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8*2b949d04SAndroid Build Coastguard Worker  * and/or sell copies of the Software, and to permit persons to whom the
9*2b949d04SAndroid Build Coastguard Worker  * Software is furnished to do so, subject to the following conditions:
10*2b949d04SAndroid Build Coastguard Worker  *
11*2b949d04SAndroid Build Coastguard Worker  * The above copyright notice and this permission notice (including the next
12*2b949d04SAndroid Build Coastguard Worker  * paragraph) shall be included in all copies or substantial portions of the
13*2b949d04SAndroid Build Coastguard Worker  * Software.
14*2b949d04SAndroid Build Coastguard Worker  *
15*2b949d04SAndroid Build Coastguard Worker  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16*2b949d04SAndroid Build Coastguard Worker  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17*2b949d04SAndroid Build Coastguard Worker  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18*2b949d04SAndroid Build Coastguard Worker  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19*2b949d04SAndroid Build Coastguard Worker  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20*2b949d04SAndroid Build Coastguard Worker  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21*2b949d04SAndroid Build Coastguard Worker  * DEALINGS IN THE SOFTWARE.
22*2b949d04SAndroid Build Coastguard Worker  */
23*2b949d04SAndroid Build Coastguard Worker 
24*2b949d04SAndroid Build Coastguard Worker #include "config.h"
25*2b949d04SAndroid Build Coastguard Worker 
26*2b949d04SAndroid Build Coastguard Worker #include <assert.h>
27*2b949d04SAndroid Build Coastguard Worker #include <inttypes.h>
28*2b949d04SAndroid Build Coastguard Worker #include <stdbool.h>
29*2b949d04SAndroid Build Coastguard Worker #include <stddef.h>
30*2b949d04SAndroid Build Coastguard Worker #include <string.h>
31*2b949d04SAndroid Build Coastguard Worker 
32*2b949d04SAndroid Build Coastguard Worker #include "utf8.h"
33*2b949d04SAndroid Build Coastguard Worker #include "utils.h"
34*2b949d04SAndroid Build Coastguard Worker 
35*2b949d04SAndroid Build Coastguard Worker #define VALID(lit) assert(is_valid_utf8(lit, sizeof(lit)-1))
36*2b949d04SAndroid Build Coastguard Worker #define INVALID(lit) assert(!is_valid_utf8(lit, sizeof(lit)-1))
37*2b949d04SAndroid Build Coastguard Worker 
38*2b949d04SAndroid Build Coastguard Worker static void
test_is_valid_utf8(void)39*2b949d04SAndroid Build Coastguard Worker test_is_valid_utf8(void)
40*2b949d04SAndroid Build Coastguard Worker {
41*2b949d04SAndroid Build Coastguard Worker     /*
42*2b949d04SAndroid Build Coastguard Worker      * Mostly taken from:
43*2b949d04SAndroid Build Coastguard Worker      * https://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt
44*2b949d04SAndroid Build Coastguard Worker      */
45*2b949d04SAndroid Build Coastguard Worker 
46*2b949d04SAndroid Build Coastguard Worker     VALID("ascii");
47*2b949d04SAndroid Build Coastguard Worker     VALID("\xCE\xBA\xE1\xBD\xB9\xCF\x83\xCE\xBC\xCE\xB5");
48*2b949d04SAndroid Build Coastguard Worker 
49*2b949d04SAndroid Build Coastguard Worker     VALID("");
50*2b949d04SAndroid Build Coastguard Worker     VALID("\x00");
51*2b949d04SAndroid Build Coastguard Worker     VALID("\x00\x00");
52*2b949d04SAndroid Build Coastguard Worker 
53*2b949d04SAndroid Build Coastguard Worker     VALID("\x50");
54*2b949d04SAndroid Build Coastguard Worker     VALID("\xC2\x80");
55*2b949d04SAndroid Build Coastguard Worker     VALID("\xE0\xA0\x80");
56*2b949d04SAndroid Build Coastguard Worker     VALID("\xF0\x90\x80\x80");
57*2b949d04SAndroid Build Coastguard Worker 
58*2b949d04SAndroid Build Coastguard Worker     /* 5/6-byte continuations aren't allowed (unlike UTF-8-test). */
59*2b949d04SAndroid Build Coastguard Worker     INVALID("\xF8\x88\x80\x80\x80");
60*2b949d04SAndroid Build Coastguard Worker     INVALID("\xFC\x84\x80\x80\x80\x80");
61*2b949d04SAndroid Build Coastguard Worker 
62*2b949d04SAndroid Build Coastguard Worker     VALID("\x7F");
63*2b949d04SAndroid Build Coastguard Worker     VALID("\xDF\xBF");
64*2b949d04SAndroid Build Coastguard Worker     VALID("\xEF\xBF\xBF");
65*2b949d04SAndroid Build Coastguard Worker     /* VALID("\xF7\xBF\xBF\xBF"); */
66*2b949d04SAndroid Build Coastguard Worker 
67*2b949d04SAndroid Build Coastguard Worker     /* 5/6-byte continuations aren't allowed (unlike UTF-8-test). */
68*2b949d04SAndroid Build Coastguard Worker     INVALID("\xFB\xBF\xBF\xBF\xBF");
69*2b949d04SAndroid Build Coastguard Worker     INVALID("\xFD\xBFxBF\xBF\xBF");
70*2b949d04SAndroid Build Coastguard Worker 
71*2b949d04SAndroid Build Coastguard Worker     VALID("\xED\x9F\xBF");
72*2b949d04SAndroid Build Coastguard Worker     VALID("\xEE\x80\x80");
73*2b949d04SAndroid Build Coastguard Worker     VALID("\xEF\xBF\xBD");
74*2b949d04SAndroid Build Coastguard Worker     VALID("\xF4\x8F\xBF\xBF");
75*2b949d04SAndroid Build Coastguard Worker     /* VALID("\xF4\x90\x80\x80"); */
76*2b949d04SAndroid Build Coastguard Worker 
77*2b949d04SAndroid Build Coastguard Worker     INVALID("\x80");
78*2b949d04SAndroid Build Coastguard Worker     INVALID("\xBF");
79*2b949d04SAndroid Build Coastguard Worker     INVALID("\x80\xBF");
80*2b949d04SAndroid Build Coastguard Worker     INVALID("\x80\xBF\x80");
81*2b949d04SAndroid Build Coastguard Worker     INVALID("\x80\xBF\x80\xBF");
82*2b949d04SAndroid Build Coastguard Worker     INVALID("\x80\xBF\x80\xBF\x80");
83*2b949d04SAndroid Build Coastguard Worker     INVALID("\x80\xBF\x80\xBF\x80\xBF");
84*2b949d04SAndroid Build Coastguard Worker     INVALID("\x80\xBF\x80\xBF\x80\xBF\x80");
85*2b949d04SAndroid Build Coastguard Worker     INVALID("\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8A\x8B\x8C\x8D\x8E\x8F"
86*2b949d04SAndroid Build Coastguard Worker             "\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9A\x9B\x9C\x9D\x9E\x9F"
87*2b949d04SAndroid Build Coastguard Worker             "\xA0\xA1\xA2\xA3\xA4\xA5\xA6\xA7\xA8\xA9\xAA\xAB\xAC\xAD\xAE\xAF"
88*2b949d04SAndroid Build Coastguard Worker             "\xB0\xB1\xB2\xB3\xB4\xB5\xB6\xB7\xB8\xB9\xBA\xBB\xBC\xBD\xBE\xBF");
89*2b949d04SAndroid Build Coastguard Worker 
90*2b949d04SAndroid Build Coastguard Worker     INVALID("\xC0 \xC1 \xC2 \xC3 \xC4 \xC5 \xC6 \xC7 \xC8 \xC9 \xCA \xCB \xCC "
91*2b949d04SAndroid Build Coastguard Worker             "\xCD \xCE \xCF "
92*2b949d04SAndroid Build Coastguard Worker             "\xD0 \xD1 \xD2 \xD3 \xD4 \xD5 \xD6 \xD7 \xD8 \xD9 \xDA \xDB \xDD "
93*2b949d04SAndroid Build Coastguard Worker             "\xDD \xDE \xDF ");
94*2b949d04SAndroid Build Coastguard Worker     INVALID("\xF0 \xF1 \xF2 \xF3 \xF4 \xF5 \xF6 \xF7 ");
95*2b949d04SAndroid Build Coastguard Worker     INVALID("\xF8 \xF9 \xFA \xFB ");
96*2b949d04SAndroid Build Coastguard Worker     INVALID("\xFC \xFD ");
97*2b949d04SAndroid Build Coastguard Worker 
98*2b949d04SAndroid Build Coastguard Worker     INVALID("\xC0");
99*2b949d04SAndroid Build Coastguard Worker     INVALID("\xE0\x80");
100*2b949d04SAndroid Build Coastguard Worker     INVALID("\xF0\x80\x80");
101*2b949d04SAndroid Build Coastguard Worker     INVALID("\xF8\x80\x80\x80");
102*2b949d04SAndroid Build Coastguard Worker     INVALID("\xFC\x80\x80\x80\x80");
103*2b949d04SAndroid Build Coastguard Worker     INVALID("\xDF");
104*2b949d04SAndroid Build Coastguard Worker     INVALID("\xEF\xBF");
105*2b949d04SAndroid Build Coastguard Worker     INVALID("\xF7\xBF\xBF");
106*2b949d04SAndroid Build Coastguard Worker     INVALID("\xFB\xBF\xBF\xBF");
107*2b949d04SAndroid Build Coastguard Worker     INVALID("\xFD\xBF\xBF\xBF\xBF");
108*2b949d04SAndroid Build Coastguard Worker 
109*2b949d04SAndroid Build Coastguard Worker     INVALID("\xC0\xE0\x80\xF0\x80\x80\xF8\x80\x80\x80\xFC\x80\x80\x80\x80"
110*2b949d04SAndroid Build Coastguard Worker             "\xDF\xEF\xBF\xF7\xBF\xBF\xFB\xBF\xBF\xBF\xFD\xBF\xBF\xBF\xBF");
111*2b949d04SAndroid Build Coastguard Worker 
112*2b949d04SAndroid Build Coastguard Worker     INVALID("\xFE");
113*2b949d04SAndroid Build Coastguard Worker     INVALID("\xFF");
114*2b949d04SAndroid Build Coastguard Worker     INVALID("\xFE\xFE\xFF\xFF");
115*2b949d04SAndroid Build Coastguard Worker 
116*2b949d04SAndroid Build Coastguard Worker     INVALID("\xC0\xAF");
117*2b949d04SAndroid Build Coastguard Worker     INVALID("\xE0\x80\xAF");
118*2b949d04SAndroid Build Coastguard Worker     INVALID("\xF0\x80\x80\xAF");
119*2b949d04SAndroid Build Coastguard Worker     INVALID("\xF8\x80\x80\x80\xAF");
120*2b949d04SAndroid Build Coastguard Worker     INVALID("\xFC\x80\x80\x80\x80\xAF");
121*2b949d04SAndroid Build Coastguard Worker 
122*2b949d04SAndroid Build Coastguard Worker     INVALID("\xC1\xBF");
123*2b949d04SAndroid Build Coastguard Worker     INVALID("\xE0\x9F\xBF");
124*2b949d04SAndroid Build Coastguard Worker     INVALID("\xF0\x8F\xBF\xBF");
125*2b949d04SAndroid Build Coastguard Worker     INVALID("\xF8\x87\xBF\xBF\xBF");
126*2b949d04SAndroid Build Coastguard Worker     INVALID("\xFC\x83\xBF\xBF\xBF\xBF");
127*2b949d04SAndroid Build Coastguard Worker 
128*2b949d04SAndroid Build Coastguard Worker     INVALID("\xC0\x80");
129*2b949d04SAndroid Build Coastguard Worker     INVALID("\xE0\x80\x80");
130*2b949d04SAndroid Build Coastguard Worker     INVALID("\xF0\x80\x80\x80");
131*2b949d04SAndroid Build Coastguard Worker     INVALID("\xF8\x80\x80\x80\x80");
132*2b949d04SAndroid Build Coastguard Worker     INVALID("\xFC\x80\x80\x80\x80\x80");
133*2b949d04SAndroid Build Coastguard Worker 
134*2b949d04SAndroid Build Coastguard Worker     INVALID("\xED\xA0\x80");
135*2b949d04SAndroid Build Coastguard Worker     INVALID("\xED\xAD\xBF");
136*2b949d04SAndroid Build Coastguard Worker     INVALID("\xED\xAE\x80");
137*2b949d04SAndroid Build Coastguard Worker     INVALID("\xED\xAF\xBF");
138*2b949d04SAndroid Build Coastguard Worker     INVALID("\xED\xB0\x80");
139*2b949d04SAndroid Build Coastguard Worker     INVALID("\xED\xBE\x80");
140*2b949d04SAndroid Build Coastguard Worker     INVALID("\xED\xBF\xBF");
141*2b949d04SAndroid Build Coastguard Worker 
142*2b949d04SAndroid Build Coastguard Worker     INVALID("\xED\xA0\x80\xED\xB0\x80");
143*2b949d04SAndroid Build Coastguard Worker     INVALID("\xED\xA0\x80\xED\xBF\xBF");
144*2b949d04SAndroid Build Coastguard Worker     INVALID("\xED\xAD\xBF\xED\xB0\x80");
145*2b949d04SAndroid Build Coastguard Worker     INVALID("\xED\xAD\xBF\xED\xBF\xBF");
146*2b949d04SAndroid Build Coastguard Worker     INVALID("\xED\xAE\x80\xED\xB0\x80");
147*2b949d04SAndroid Build Coastguard Worker     INVALID("\xED\xAE\x80\xED\xBF\xBF");
148*2b949d04SAndroid Build Coastguard Worker     INVALID("\xED\xAF\xBF\xED\xB0\x80");
149*2b949d04SAndroid Build Coastguard Worker     INVALID("\xED\xAF\xBF\xED\xBF\xBF");
150*2b949d04SAndroid Build Coastguard Worker 
151*2b949d04SAndroid Build Coastguard Worker     /* INVALID("\xEF\xBF\xBE"); */
152*2b949d04SAndroid Build Coastguard Worker     /* INVALID("\xEF\xBF\xBF"); */
153*2b949d04SAndroid Build Coastguard Worker }
154*2b949d04SAndroid Build Coastguard Worker 
155*2b949d04SAndroid Build Coastguard Worker static void
check_utf32_to_utf8(uint32_t unichar,int expected_length,const char * expected)156*2b949d04SAndroid Build Coastguard Worker check_utf32_to_utf8(uint32_t unichar, int expected_length, const char *expected) {
157*2b949d04SAndroid Build Coastguard Worker     char buffer[7];
158*2b949d04SAndroid Build Coastguard Worker     int length;
159*2b949d04SAndroid Build Coastguard Worker 
160*2b949d04SAndroid Build Coastguard Worker     length = utf32_to_utf8(unichar, buffer);
161*2b949d04SAndroid Build Coastguard Worker 
162*2b949d04SAndroid Build Coastguard Worker     assert(length == expected_length);
163*2b949d04SAndroid Build Coastguard Worker     assert(streq(buffer, expected));
164*2b949d04SAndroid Build Coastguard Worker }
165*2b949d04SAndroid Build Coastguard Worker 
166*2b949d04SAndroid Build Coastguard Worker static void
test_utf32_to_utf8(void)167*2b949d04SAndroid Build Coastguard Worker test_utf32_to_utf8(void)
168*2b949d04SAndroid Build Coastguard Worker {
169*2b949d04SAndroid Build Coastguard Worker     check_utf32_to_utf8(0x0, 2, "");
170*2b949d04SAndroid Build Coastguard Worker     check_utf32_to_utf8(0x40, 2, "\x40");
171*2b949d04SAndroid Build Coastguard Worker     check_utf32_to_utf8(0xA1, 3, "\xc2\xa1");
172*2b949d04SAndroid Build Coastguard Worker     check_utf32_to_utf8(0x2701, 4, "\xe2\x9c\x81");
173*2b949d04SAndroid Build Coastguard Worker     check_utf32_to_utf8(0x1f004, 5, "\xf0\x9f\x80\x84");
174*2b949d04SAndroid Build Coastguard Worker     check_utf32_to_utf8(0x110000, 0, "");
175*2b949d04SAndroid Build Coastguard Worker     check_utf32_to_utf8(0xffffffff, 0, "");
176*2b949d04SAndroid Build Coastguard Worker }
177*2b949d04SAndroid Build Coastguard Worker 
178*2b949d04SAndroid Build Coastguard Worker int
main(void)179*2b949d04SAndroid Build Coastguard Worker main(void)
180*2b949d04SAndroid Build Coastguard Worker {
181*2b949d04SAndroid Build Coastguard Worker     test_is_valid_utf8();
182*2b949d04SAndroid Build Coastguard Worker     test_utf32_to_utf8();
183*2b949d04SAndroid Build Coastguard Worker 
184*2b949d04SAndroid Build Coastguard Worker     return 0;
185*2b949d04SAndroid Build Coastguard Worker }
186