forked from k-takata/Onigmo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_enc_utf8.c
291 lines (240 loc) · 8.96 KB
/
test_enc_utf8.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
#include "config.h"
#ifdef ONIG_ESCAPE_UCHAR_COLLISION
#undef ONIG_ESCAPE_UCHAR_COLLISION
#endif
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "onigmo.h"
/* #define USE_UTF8_31BITS */
#define SLEN(s) strlen(s)
static int nsucc = 0;
static int nfail = 0;
static int nerror = 0;
static FILE* err_file;
static OnigRegion* region;
static void xx(char* pattern, char* str, int from, int to, int mem, int not)
{
int r;
regex_t* reg;
OnigErrorInfo einfo;
OnigSyntaxType syn = *ONIG_SYNTAX_DEFAULT;
r = onig_new(®, (UChar* )pattern, (UChar* )(pattern + SLEN(pattern)),
ONIG_OPTION_NONE, ONIG_ENCODING_UTF8, &syn, &einfo);
if (r) {
char s[ONIG_MAX_ERROR_MESSAGE_LEN];
onig_error_code_to_str((UChar* )s, r, &einfo);
fprintf(err_file, "ERROR: %s\n", s);
nerror++;
return ;
}
r = onig_search(reg, (UChar* )str, (UChar* )(str + SLEN(str)),
(UChar* )str, (UChar* )(str + SLEN(str)),
region, ONIG_OPTION_NONE);
if (r < ONIG_MISMATCH) {
char s[ONIG_MAX_ERROR_MESSAGE_LEN];
onig_error_code_to_str((UChar* )s, r);
fprintf(err_file, "ERROR: %s\n", s);
nerror++;
return ;
}
if (r == ONIG_MISMATCH) {
if (not) {
fprintf(stdout, "OK(N): /%s/ '%s'\n", pattern, str);
nsucc++;
}
else {
fprintf(stdout, "FAIL: /%s/ '%s'\n", pattern, str);
nfail++;
}
}
else {
if (not) {
fprintf(stdout, "FAIL(N): /%s/ '%s'\n", pattern, str);
nfail++;
}
else {
if (region->beg[mem] == from && region->end[mem] == to) {
fprintf(stdout, "OK: /%s/ '%s'\n", pattern, str);
nsucc++;
}
else {
fprintf(stdout, "FAIL: /%s/ '%s' %d-%d : %d-%d\n", pattern, str,
(int)from, (int)to, (int)region->beg[mem], (int)region->end[mem]);
nfail++;
}
}
}
onig_free(reg);
}
static void x2(char* pattern, char* str, int from, int to)
{
xx(pattern, str, from, to, 0, 0);
}
#if 0
static void x3(char* pattern, char* str, int from, int to, int mem)
{
xx(pattern, str, from, to, mem, 0);
}
static void n(char* pattern, char* str)
{
xx(pattern, str, 0, 0, 0, 1);
}
#endif
const OnigEncodingType * target_encoding = ONIG_ENCODING_UTF8;
static void test_mbc_enc_len(const char * str, int expect) {
const OnigEncodingType * enc = target_encoding;
size_t len = strlen(str);
int actual = ONIGENC_PRECISE_MBC_ENC_LEN(enc, (const UChar *)str, (const UChar *)str + len);
if (actual == expect) {
fprintf(stdout, "OK: mbc_enc_len(%s)=%d\n", str, expect);
nsucc++;
} else {
fprintf(stdout, "FAIL: mbc_enc_len(%s)=%d\n", str, expect);
nfail++;
}
}
static void test_code_to_mbclen(OnigCodePoint code, int expect) {
const OnigEncodingType * enc = target_encoding;
int actual = ONIGENC_CODE_TO_MBCLEN(enc, code);
if (actual == expect) {
fprintf(stdout, "OK: code_to_mbclen(U+%04X)=%d\n", code, expect);
nsucc++;
} else {
fprintf(stdout, "FAIL: code_to_mbclen(U+%04X)=%d\n", code, expect);
nfail++;
}
}
static void test_mbc_to_code(const char * str, int expect) {
const OnigEncodingType * enc = target_encoding;
size_t len = strlen(str);
int actual = ONIGENC_MBC_TO_CODE(enc, (const UChar *)str, (const UChar *)str + len);
if (actual == expect) {
fprintf(stdout, "OK: mbc_to_code(%s)=U+%04X\n", str, expect);
nsucc++;
} else {
fprintf(stdout, "FAIL: mbc_to_code(%s)=U+%04X\n", str, expect);
nfail++;
}
}
static void test_code_to_mbc(OnigCodePoint code, const char * expect, int exp_error) {
const OnigEncodingType * enc = target_encoding;
UChar * buf = (UChar *)malloc(ONIGENC_MBC_MAXLEN(enc) + 1);
int len = ONIGENC_CODE_TO_MBC(enc, code, buf);
if (len < 0) {
int err = len;
if (err == exp_error) {
fprintf(stdout, "OK: code_to_mbc(U+%04X)=(error %d)\n", code, len);
nsucc++;
} else {
fprintf(stdout, "FAIL: code_to_mbc(U+%04X)=(error %d)\n", code, len);
nfail++;
}
goto exit;
}
buf[len] = '\0';
if (strcmp((const char *)buf, expect) == 0) {
fprintf(stdout, "OK: code_to_mbc(U+%04X)=%s\n", code, expect);
nsucc++;
} else {
fprintf(stdout, "FAIL: code_to_mbc(U+%04X)=%s\n", code, expect);
nfail++;
}
exit:
free(buf);
}
extern int main(int argc, char* argv[])
{
err_file = stdout;
region = onig_region_new();
test_mbc_enc_len("\xC2\x80", 2); // S0, S1, A
test_code_to_mbclen(0x0080, 2);
test_code_to_mbc(0x0080, "\xC2\x80", 0);
x2("\\x{0080}", "\xC2\x80", 0, 2);
x2("\xC2\x80", "\xC2\x80", 0, 2); // min 2 bytes
test_mbc_enc_len("\xC2\xC0", -1); // S0, S1, F
test_mbc_enc_len("\xDF\xBF", 2); // S0, S1, A
test_code_to_mbclen(0x07FF, 2);
test_code_to_mbc(0x07FF, "\xDF\xBF", 0);
x2("\\x{07FF}", "\xDF\xBF", 0, 2);
x2("\xDF\xBF", "\xDF\xBF", 0, 2); // max 2 bytes
test_mbc_enc_len("\xE0\xA0\x80", 3); // S0, S2, S1, A
test_code_to_mbclen(0x0800, 3);
test_code_to_mbc(0x0800, "\xE0\xA0\x80", 0);
x2("\xE0\xA0\x80", "\xE0\xA0\x80", 0, 3);
x2("\\x{0800}", "\xE0\xA0\x80", 0, 3); // min 3 bytes
test_mbc_enc_len("\xE0\xC0\x80", -1); // S0, S2, F
test_mbc_enc_len("\xEF\xBF\xBF", 3); // S0, S3, S1, A
test_code_to_mbclen(0xFFFF, 3);
test_code_to_mbc(0xFFFF, "\xEF\xBF\xBF", 0);
x2("\xEF\xBF\xBF", "\xEF\xBF\xBF", 0, 3);
x2("\\x{FFFF}", "\xEF\xBF\xBF", 0, 3); // max 3 bytes
test_mbc_enc_len("\xEF\xC0\xBF", -1); // S0, S3, F
test_mbc_enc_len("\xED\x80\x80", 3); // S0, S4, S1, A
test_code_to_mbclen(0xD000, 3);
test_code_to_mbc(0xD000, "\xED\x80\x80", 0);
x2("\xED\x80\x80", "\xED\x80\x80", 0, 3);
x2("\\x{D000}", "\xED\x80\x80", 0, 3);
test_mbc_enc_len("\xED\xA0\xA0", -1); // S0, S4, F
test_mbc_enc_len("\xF0\x90\x80\x80", 4); // S0, S5, S3, S1, A
test_code_to_mbclen(0x00010000, 4);
test_code_to_mbc(0x00010000, "\xF0\x90\x80\x80", 0);
x2("\xF0\x90\x80\x80", "\xF0\x90\x80\x80", 0, 4);
x2("\\x{00010000}", "\xF0\x90\x80\x80", 0, 4); // min 4 bytes
test_mbc_enc_len("\xF0\x80\x80\x80", -1); // S0, S5, F
test_mbc_enc_len("\xF4\x8F\xBF\xBF", 4); // S0, S7, S3, S1, A
test_code_to_mbclen(0x0010FFFF, 4);
test_code_to_mbc(0x0010FFFF, "\xF4\x8F\xBF\xBF", 0);
x2("\xF4\x8F\xBF\xBF", "\xF4\x8F\xBF\xBF", 0, 4);
x2("\\x{0010FFFF}", "\xF4\x8F\xBF\xBF", 0, 4); // max Unicode
#ifndef USE_UTF8_31BITS
test_mbc_enc_len("\xF7\xBF\xBF\xBF", -1); // S0, F
#else
test_mbc_enc_len("\xF7\xBF\xBF\xBF", 4); // S0, S6, S3, S1, A
test_code_to_mbclen(0x001FFFFF, 4);
test_code_to_mbc(0x001FFFFF, "\xF7\xBF\xBF\xBF", 0);
x2("\xF7\xBF\xBF\xBF", "\xF7\xBF\xBF\xBF", 0, 4);
x2("\\x{001FFFFF}", "\xF7\xBF\xBF\xBF", 0, 4); // max 4 bytes (21bits)
test_mbc_enc_len("\xF7\xC0\xBF\xBF", -1); // S0, S6, F
test_mbc_enc_len("\xF8\x88\x80\x80\x80", 5); // S0, S8, S6, S3, S1, A
test_code_to_mbclen(0x00200000, 5);
test_code_to_mbc(0x00200000, "\xF8\x88\x80\x80\x80", 0);
x2("\xF8\x88\x80\x80\x80", "\xF8\x88\x80\x80\x80", 0, 5);
x2("\\x{00200000}", "\xF8\x88\x80\x80\x80", 0, 5); // min 5 bytes
test_mbc_enc_len("\xF8\x80\x80\x80\x80", -1); // S0, S8, F
test_mbc_enc_len("\xFB\xBF\xBF\xBF\xBF", 5); // S0, S9, S6, S3, S1, A
test_code_to_mbclen(0x03FFFFFF, 5);
test_code_to_mbc(0x03FFFFFF, "\xFB\xBF\xBF\xBF\xBF", 0);
x2("\xFB\xBF\xBF\xBF\xBF", "\xFB\xBF\xBF\xBF\xBF", 0, 5);
x2("\\x{03FFFFFF}", "\xFB\xBF\xBF\xBF\xBF", 0, 5); // max 5 bytes
test_mbc_enc_len("\xFB\xC0\xBF\xBF\xBF", -1); // S0, S9, F
test_mbc_enc_len("\xFC\x84\x80\x80\x80\x80", 6); // S0, S10, S9, S6, S3, S1, A
test_code_to_mbclen(0x04000000, 6);
test_code_to_mbc(0x04000000, "\xFC\x84\x80\x80\x80\x80", 0);
x2("\xFC\x84\x80\x80\x80\x80", "\xFC\x84\x80\x80\x80\x80", 0, 6);
x2("\\x{04000000}", "\xFC\x84\x80\x80\x80\x80", 0, 6); // min 6 bytes
test_mbc_enc_len("\xFC\x80\x80\x80\x80\x80", -1); // S0, S10, F
test_mbc_enc_len("\xFD\xBF\xBF\xBF\xBF\xBF", 6); // S0, S11, S9, S6, S3, S1, A
test_code_to_mbclen(0x7FFFFFFF, 6);
test_code_to_mbc(0x7FFFFFFF, "\xFD\xBF\xBF\xBF\xBF\xBF", 0);
x2("\xFD\xBF\xBF\xBF\xBF\xBF", "\xFD\xBF\xBF\xBF\xBF\xBF", 0, 6);
x2("\\x{7FFFFFFF}", "\xFD\xBF\xBF\xBF\xBF\xBF", 0, 6); // max 6 bytes
test_mbc_enc_len("\xFD\xC0\xBF\xBF\xBF\xBF", -1); // S0, S11, F
test_code_to_mbclen(0x80000000, ONIGERR_TOO_BIG_WIDE_CHAR_VALUE);
test_code_to_mbc(0x80000000, "", ONIGERR_TOO_BIG_WIDE_CHAR_VALUE);
test_code_to_mbclen(0xFFFFFFFD, ONIGERR_TOO_BIG_WIDE_CHAR_VALUE);
test_code_to_mbc(0xFFFFFFFD, "", ONIGERR_TOO_BIG_WIDE_CHAR_VALUE);
test_code_to_mbclen(0xFFFFFFFE, 1);
test_code_to_mbc(0xFFFFFFFE, "\xFE", 0);
test_code_to_mbclen(0xFFFFFFFF, 1);
test_code_to_mbc(0xFFFFFFFF, "\xFF", 0);
#endif
test_mbc_to_code("\xFE", 0xFFFFFFFE);
test_mbc_to_code("\xFF", 0xFFFFFFFF);
fprintf(stdout,
"\nRESULT SUCC: %d, FAIL: %d, ERROR: %d (by Onigmo %s)\n",
nsucc, nfail, nerror, onig_version());
onig_region_free(region, 1);
onig_end();
return ((nfail == 0 && nerror == 0) ? 0 : -1);
}