-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.cpp
400 lines (345 loc) · 9.31 KB
/
test.cpp
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <time.h>
#include <vector>
#include "utfz.h"
/*
Windows MSVC:
cl /nologo /Zi /EHsc test.cpp utfz.cpp && test
opencppcoverage --sources c:\dev\head\maps\third_party\utfz --modules test.exe -- test.exe
Unix GCC:
g++ -O2 -o test -std=c++11 test.cpp utfz.cpp && ./test
Unix Clang:
clang++ -O2 -o test -std=c++11 test.cpp utfz.cpp && ./test
*/
void test_iterators(const char* s)
{
const char* end = s + strlen(s);
printf("%s (%d): ", s, utfz::seq_len(s[0]));
// iterate using 'next', and with unknown length
std::vector<int> cp1;
char const* iter = s;
int cp;
while (utfz::next(iter, cp))
{
cp1.push_back(cp);
printf("%02x ", cp);
}
printf("== ");
// iterate using 'next', and with known length
std::vector<int> cp2;
iter = s;
while (utfz::next(iter, end, cp))
{
cp2.push_back(cp);
printf("%02x ", cp);
}
// test cp iterator on char* with unknown length
std::vector<int> cp3;
printf("== ");
for (auto cp : utfz::cp(s))
{
cp3.push_back(cp);
printf("%02x ", cp);
}
// test cp iterator on std::string (ie known length)
printf("== ");
std::vector<int> cp4;
std::string sz = s;
for (auto cp : utfz::cp(sz))
{
cp4.push_back(cp);
printf("%02x ", cp);
}
assert(cp1.size() == cp2.size());
assert(cp1.size() == cp3.size());
assert(cp1.size() == cp4.size());
for (size_t i = 0; i < cp1.size(); i++)
{
assert(cp1[i] == cp2[i]);
assert(cp1[i] == cp3[i]);
assert(cp1[i] == cp4[i]);
}
printf("\n");
}
void bench(const char* name, int runLength[4])
{
srand(0);
int tokens = 100000;
int encSize = tokens * 4 + 1;
char* enc = (char*) malloc(encSize);
char* encP = enc;
unsigned rl = 3; // index into runLength[]
int remain = 0; // remaining counts inside our current bucket of runLength[]
int maxValues[4] = {utfz::max1, utfz::max2, utfz::max3, utfz::max4};
for (int i = 0; i < tokens; i++)
{
while (remain == 0)
{
rl = (rl + 1) % 4;
remain = runLength[rl];
}
unsigned cp = (unsigned) rand();
cp = (cp % (maxValues[rl] - 1)) + 1; // the -1, +1 dance is to prevent the zero code point
int n = utfz::encode(encP, cp);
assert(n != 0);
encP += n;
}
char* encEnd = encP;
assert(encEnd - enc < (intptr_t) encSize);
*encEnd = 0;
int sum = 0;
double start = clock();
for (int i = 0; i < 2000; i++)
{
for (auto cp : utfz::cp(enc, encEnd - enc))
sum += cp;
}
double end = (clock() - start) / CLOCKS_PER_SEC;
printf("%-10s, %.0f ms\n", name, end * 1000);
free(enc);
}
int encode_any(int cp, char* buf)
{
unsigned ucp = (unsigned) cp;
if (ucp <= 0x7f)
{
buf[0] = ucp;
return 1;
}
else if (ucp <= 0x7ff)
{
buf[0] = 0xc0 | (ucp >> 6);
buf[1] = 0x80 | (ucp & 0x3f);
return 2;
}
else if (ucp <= 0xffff)
{
buf[0] = 0xe0 | (ucp >> 12);
buf[1] = 0x80 | ((ucp >> 6) & 0x3f);
buf[2] = 0x80 | (ucp & 0x3f);
return 3;
}
else if (ucp <= 0x200000)
{
buf[0] = 0xf0 | (ucp >> 18);
buf[1] = 0x80 | ((ucp >> 12) & 0x3f);
buf[2] = 0x80 | ((ucp >> 6) & 0x3f);
buf[3] = 0x80 | (ucp & 0x3f);
return 4;
}
else
{
return 0;
}
}
int main(int argc, char** argv)
{
const char* s1 = "$"; // 1 byte
const char* s2 = "¢"; // 2 bytes
const char* s3 = "€"; // 3 bytes
const char* s4 = "𐍈"; // 4 bytes
const char* sinvalid1 = "\x80";
const char* sinvalid2 =
"\x80"
"a";
const char* sempty = "";
const int allsize = 7;
const char* all[allsize] = {s1, s2, s3, s4, sinvalid1, sinvalid2, sempty};
int len[allsize] = {1, 2, 3, 4, 1, 2, 0};
for (int i = 1; i < 50000; i++)
{
char buf[10];
int len = utfz::encode(buf, i);
int r = utfz::seq_len(buf[0]);
assert(len == r);
}
for (int i = 0; i < allsize; i++)
test_iterators(all[i]);
int testCP[] = {1, 0x7f, 0x80, 0x7ff, 0x800, 0xfffd, 0x10000, 0x10ffff};
for (size_t i = 0; i < sizeof(testCP) / sizeof(testCP[0]); i++)
{
char encoded[5];
std::string encstr;
int enc_len = utfz::encode(encoded, testCP[i]);
encoded[enc_len] = 0;
assert(utfz::encode(encstr, testCP[i]));
assert(strcmp(encoded, encstr.c_str()) == 0);
int dec_len;
int cp = utfz::decode(encoded, encoded + enc_len, dec_len);
assert(dec_len == enc_len);
assert(cp == testCP[i]);
}
{
// decoding of NUL code point
const char* enc = "\0";
// known length
int seq_len = 0;
int cp = utfz::decode(enc, enc + 1, seq_len);
assert(cp == 0 && seq_len == 1);
// unknown length
seq_len = 0;
cp = utfz::decode(enc, seq_len);
assert(cp == 0 && seq_len == 1);
// test iterator without length
{
std::vector<int> iter;
for (auto cp : utfz::cp(enc))
iter.push_back(cp);
assert(iter.size() == 0);
}
// test iterator with length
{
std::vector<int> iter;
for (auto cp : utfz::cp(enc, 1))
iter.push_back(cp);
assert(iter.size() == 1);
}
}
{
// encoding of NUL code point
char encoded[2] = {1, 1};
assert(utfz::encode(encoded, 0) == 1 && encoded[0] == 0);
}
{
// invalid code point
char encoded[4];
assert(utfz::encode(encoded, 0x110000) == 0);
// Modified UTF-8 decoding
encoded[0] = (char) 0xc0;
encoded[1] = (char) 0x80;
assert(utfz::decode(encoded) == utfz::replace);
assert(utfz::decode(encoded, encoded + 2) == utfz::replace);
// detect overlong sequences
encoded[0] = (char) 0xc0;
encoded[1] = (char) 0x81;
assert(utfz::decode(encoded) == utfz::replace);
}
{
// Some arbitrary code to make sure we get 100% code coverage
const char* str = "h";
char buf[10];
// next_modified on valid input
const char* s = str;
int cp;
utfz::next(s, cp);
assert(cp == 'h');
assert(s == str + 1);
// postfix increment
auto cpiter = utfz::cp(str);
for (auto cp = cpiter.begin(); cp != cpiter.end(); cp++)
printf("%d ", *cp);
// invalid encode to std::string
std::string ebuf;
assert(!utfz::encode(ebuf, 0x10ffff + 1));
assert(utfz::restart(str + 1, str) == str);
// restart on null terminator
buf[0] = 0;
assert(utfz::restart(buf) == buf);
// legality of 3-byte codes
int slen = 0;
// overlong
assert(utfz::decode("\xE0\x01\x01") == utfz::replace);
// UTF-16 surrogate pairs
for (int i = 0xd800; i <= 0xdfff; i++)
{
assert(encode_any(i, buf) == 3);
assert(utfz::decode(buf) == utfz::replace);
assert(utfz::decode(buf, buf + 3) == utfz::replace);
}
// two illegal specials (for BOMs)
assert(encode_any(0xfffe, buf) == 3);
assert(utfz::decode(buf) == utfz::replace);
assert(utfz::decode(buf, buf + 3) == utfz::replace);
assert(encode_any(0xffff, buf) == 3);
assert(utfz::decode(buf) == utfz::replace);
assert(utfz::decode(buf, buf + 3) == utfz::replace);
// too large
assert(encode_any(0x10ffff, buf) == 4);
assert(utfz::decode(buf) == 0x10ffff);
assert(utfz::decode(buf, buf + 4) == 0x10ffff);
assert(encode_any(0x10ffff + 1, buf) == 4);
assert(utfz::decode(buf) == utfz::replace);
assert(utfz::decode(buf, buf + 4) == utfz::replace);
// highest two bits in subsequent bytes are not '10'; next plausible decode point is one byte onwards
const char* badHigh[6] = {
// bit 7 is not 1 (3F is the illegal byte here)
"\xC4\x3F",
"\xE4\xB0\x3F",
"\xF4\xB0\xB0\x3F",
// bit 6 is not 0 (F0 is the illegal byte here)
"\xC4\xF0",
"\xE4\xB0\xF0",
"\xF4\xB0\xB0\xF0",
};
for (int i = 0; i < 6; i++)
{
const char* s = badHigh[i];
const char* end = s + strlen(s);
assert(utfz::decode(s, slen) == utfz::replace);
assert(utfz::decode(s, end, slen) == utfz::replace);
assert(utfz::restart(s) > s);
assert(utfz::restart(s, end) > s);
}
// illegal encoding
assert(utfz::encode(buf, 0xfffe) == 0);
// decode truncated code point with known length
assert(encode_any(0xd123, buf) == 3);
assert(utfz::decode(buf, buf + 2) == utfz::replace);
// decode truncated code points with unknown length
assert(encode_any(utfz::max1 + 1, buf) == 2);
buf[1] = 0;
assert(utfz::decode(buf) == utfz::replace);
assert(encode_any(utfz::max2 + 1, buf) == 3);
buf[2] = 0;
assert(utfz::decode(buf) == utfz::replace);
assert(encode_any(utfz::max3 + 1, buf) == 4);
buf[3] = 0;
assert(utfz::decode(buf) == utfz::replace);
// iterate on empty string
const char* empty = "";
auto iter = utfz::cp(empty).begin();
auto end = utfz::cp(empty).end();
iter++;
iter++;
assert(iter == end);
iter = utfz::cp(empty, 0).begin();
end = utfz::cp(empty, 0).end();
iter++;
iter++;
assert(iter == end);
// iterate on truncated string (known length)
assert(encode_any(utfz::max3 + 1, buf) == 4);
iter = utfz::cp(buf, 3).begin();
end = utfz::cp(buf, 3).end();
assert(iter != end);
assert(*iter == utfz::replace);
iter++;
assert(iter == end);
// iterate on truncated string (unknown length)
assert(encode_any(utfz::max3 + 1, buf) == 4);
buf[3] = 0;
iter = utfz::cp(buf).begin();
end = utfz::cp(buf).end();
assert(iter != end);
assert(*iter == utfz::replace);
iter++;
assert(iter == end);
}
#ifndef _DEBUG
{
// speed
printf("\n");
int ascii[4] = {1, 0, 0, 0};
int low[4] = {10, 2, 1, 0};
int high[4] = {1, 1, 1, 1};
bench("ascii", ascii);
bench("low", low);
bench("high", high);
}
#endif
return 0;
}