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