-
Notifications
You must be signed in to change notification settings - Fork 5
/
test.c
305 lines (256 loc) · 7.38 KB
/
test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <stdio.h>
#include "utf8_valid.h"
/*
* UTF-8
*
* U+0000..U+007F 00..7F
* n C0..C1 80..BF
* U+0080..U+07FF C2..DF 80..BF
* n E0 80..9F 80..BF
* U+0800..U+D7FF E0..ED A0..9F 80..BF
* U+D800..U+DFFF s ED A0..BF 80..BF
* U+E000..U+FFFF EE..EF 80..BF 80..BF
* n F0 80..8F 80..BF 80..BF
* U+0800..U+FFFF F0 80..8F A0..BF 80..BF
* U+10000..U+10FFFF F0..F4 90..8F 80..BF 80..BF
*
* U-110000..U-1FFFFF x F4..F7 90..BF 80..BF 80..BF
* xn F8 80..87 80..BF 80..BF 80..BF
* U-200000..U-3FFFFFF x F8..FB 88..BF 80..BF 80..BF 80..BF
* xn FC 80..83 80..BF 80..BF 80..BF 80..BF
* U-4000000..U-7FFFFFFF x FC..FD 84..BF 80..BF 80..BF 80..BF 80..BF
*
* Legend:
* n = Non-shortest form
* s = Surrogates
* x = Codepoints outside Unicode codespace
*/
/*
* Encodes the given ordinal [0, 7FFFFFFF] using the UTF-8 encoding scheme
* to the given sequence length [1, 6]. This routine can be used to
* produce well-formed and ill-formed UTF-8.
*
* To encode a Unicode scalar value to a well-formed representation:
*
* [U+0000, U+007F] should be encoded to a sequence length of 1
* [U+0080, U+07FF] should be encoded to a sequence length of 2
* [U+0800, U+D7FF] should be encoded to a sequence length of 3
* [U+E000, U+FFFF] should be encoded to a sequence length of 3
* [U+10000, U+10FFFF] should be encoded to a sequence length of 4
*
* To encode a Unicode scalar value to non-shortest form representation:
*
* [U+0000, U+007F] can be encoded to a sequence length of [2, 6]
* [U+0080, U+07FF] can be encoded to a sequence length of [3, 6]
* [U+0800, U+FFFF] can be encoded to a sequence length of [4, 6]
*
* To encode an ordinal outside of Unicode codespace:
*
* [110000, 1FFFFF] can be encoded to a sequence length of 4
* [200000, 3FFFFFF] can be encoded to a sequence length of 5
* [4000000, 7FFFFFFF] can be encoded to a sequence length of 6
*/
char *
encode_ord(uint32_t ord, size_t len, char *dst) {
static const uint32_t kMask[6] = { 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
static const uint32_t kMax[6] = { 1 << 7, 1 << 11, 1 << 16,
1 << 21, 1 << 26, 1 << 31 };
size_t i;
assert(len >= 1);
assert(len <= 6);
assert(ord < kMax[len - 1]);
for (i = len - 1; i > 0; i--) {
dst[i] = (ord & 0x3F) | 0x80;
ord >>= 6;
}
dst[0] = ord | kMask[len - 1];
return dst;
}
char *
escape_str(const char *src, size_t len, char *dst) {
static const char * const kHex = "0123456789ABCDEF";
size_t i;
char *d;
for (d = dst, i = 0; i < len; i++) {
const unsigned char c = src[i];
if (c >= ' ' && c <= '~') {
if (c == '\\' || c == '"')
*d++ = '\\';
*d++ = c;
}
else {
*d++ = '\\';
*d++ = 'x';
*d++ = kHex[c >> 4];
*d++ = kHex[c & 0x0F];
}
}
*d = 0;
return dst;
}
static size_t TestCount = 0;
static size_t TestFailed = 0;
void
test_utf8(const char *src, size_t len, size_t exp_spl, bool exp_ret, unsigned line) {
char escaped[255 * 4 + 1];
size_t offset, got_spl;
bool got_ret;
assert(len <= 255);
got_ret = utf8_check(src, len, &offset);
TestCount++;
if (got_ret != exp_ret) {
escape_str(src, len, escaped);
printf("utf8_valid(\"%s\", %d) != %s at line %u\n",
escaped, (unsigned)len, exp_ret ? "true" : "false", line);
TestFailed++;
}
src += offset;
len -= offset;
TestCount++;
got_spl = utf8_maximal_subpart(src, len);
if (got_spl != exp_spl) {
escape_str(src, len, escaped);
printf("utf8_maximal_subpart(\"%s\", %d) != %d (got: %d) at line %u\n",
escaped, (unsigned)len, (unsigned)exp_spl, (unsigned)got_spl, line);
TestFailed++;
}
}
#define TEST_UTF8(src, len, subpart, exp) \
test_utf8(src, len, subpart, exp, __LINE__)
void
test_unicode_scalar_value() {
uint32_t ord;
char src[4];
/* Unicode scalar value [U+0000, U+007F] */
for (ord = 0x0000; ord <= 0x007F; ord++) {
encode_ord(ord, 1, src);
TEST_UTF8(src, 1, 0, true);
}
/*
* Unicode scalar value [U+0080, U+07FF]
* The maximal subpart is the length of the truncated sequence
*/
for (ord = 0x0080; ord <= 0x07FF; ord++) {
encode_ord(ord, 2, src);
TEST_UTF8(src, 2, 0, true);
}
/*
* Unicode scalar value [U+0800, U+D7FF] and [U+E000, U+FFFF]
* The maximal subpart is the length of the truncated sequence
*/
for (ord = 0x0800; ord <= 0xFFFF && (ord & 0xF800) != 0xD800; ord++) {
encode_ord(ord, 3, src);
TEST_UTF8(src, 3, 0, true);
if ((ord % (1 << 6)) == 0)
TEST_UTF8(src, 2, 2, false);
}
/*
* Unicode scalar value [U+10000, U+10FFF]
* The maximal subpart is the length of the truncated sequence
*/
for (ord = 0x10000; ord <= 0x10FFFF; ord++) {
encode_ord(ord, 4, src);
TEST_UTF8(src, 4, 0, true);
if ((ord % (1 << 6)) == 0)
TEST_UTF8(src, 3, 3, false);
if ((ord % (1 << 12)) == 0)
TEST_UTF8(src, 2, 2, false);
}
}
void
test_non_shortest_form() {
uint32_t ord;
char src[4];
/*
* Non-shortest form 2-byte sequence [U+0000, U+007F]
* The maximal subpart is 1-byte
*/
for (ord = 0x0000; ord <= 0x007F; ord++) {
encode_ord(ord, 2, src);
TEST_UTF8(src, 2, 1, false);
}
/*
* Non-shortest form 3-byte sequence [U+0000, U+07FF]
* The maximal subpart is 1-byte
*/
for (ord = 0x0000; ord <= 0x07FF; ord++) {
encode_ord(ord, 3, src);
TEST_UTF8(src, 3, 1, false);
if ((ord % (1 << 6)) == 0)
TEST_UTF8(src, 2, 1, false);
}
/*
* Non-shortest form 4-byte sequence [U+0000, U+FFFF]
* The maximal subpart is 1-byte
*/
for (ord = 0x0000; ord <= 0xFFFF; ord++) {
encode_ord(ord, 4, src);
TEST_UTF8(src, 4, 1, false);
if ((ord % (1 << 6)) == 0)
TEST_UTF8(src, 3, 1, false);
if ((ord % (1 << 12)) == 0)
TEST_UTF8(src, 2, 1, false);
}
}
void
test_non_unicode() {
uint32_t ord;
char src[4];
/*
* Code point outside Unicode codespace
* The maximal subpart is 1-byte
*/
for (ord = 0x110000; ord <= 0x1FFFFF; ord++) {
encode_ord(ord, 4, src);
TEST_UTF8(src, 4, 1, false);
if ((ord % (1 << 6)) == 0)
TEST_UTF8(src, 3, 1, false);
if ((ord % (1 << 12)) == 0)
TEST_UTF8(src, 2, 1, false);
}
}
void
test_surrogates() {
uint32_t ord;
char src[4];
/*
* Surrogates [U+D800, U+DFFF]
* The maximal subpart is 1-byte
*/
for (ord = 0xD800; ord <= 0xDFFF; ord++) {
encode_ord(ord, 3, src);
TEST_UTF8(src, 3, 1, false);
if ((ord % (1 << 6)) == 0)
TEST_UTF8(src, 2, 1, false);
}
}
void
test_continuations() {
uint8_t ord;
char src[4];
/*
* Missplaced continuation [\x80, \xBF]
* The maximal subpart is 1-byte
*/
for (ord = 0x80; ord <= 0xBF; ord++) {
src[0] = ord;
TEST_UTF8(src, 1, 1, false);
}
}
int
main(int argc, char **argv) {
test_unicode_scalar_value();
test_surrogates();
test_non_shortest_form();
test_non_unicode();
test_continuations();
if (TestFailed)
printf("Failed %zu tests of %zu.\n", TestFailed, TestCount);
else
printf("Passed %zu tests.\n", TestCount);
return TestFailed ? 1 : 0;
}