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
|
#include "unicode_config.h"
#include "courier-unicode.h"
#include "normalization_defs.h"
#define exclusion_table
#include "normalization.h"
#include <iostream>
#include <iomanip>
static void dump(const char32_t *s,
size_t n)
{
std::cout << '"';
for (size_t i=0;
n == (size_t)-1 ? s[i] != 0 : i<n; ++i)
{
if (s[i] < 128)
{
std::cout << (char)s[i];
}
else
{
std::cout << "\\u"
<< std::hex
<< std::setw(4)
<< std::setfill('0')
<< (unsigned long)s[i]
<< std::dec
<< std::setw(0);
}
}
std::cout << "\"\n";
}
void testdecompose()
{
static const struct {
const char32_t *before;
int flags;
const char32_t *after;
} tests[]={
{ U"1\u1ea0\u1ea1 \u1ea4 2",
0,
U"1A\u0323a\u0323 A\u0302\u0301 2",
},
{ U"1\u1ea0\u1ea1 \u1ea4 2",
UNICODE_DECOMPOSE_FLAG_QC,
U"1\u1ea0\u1ea1 \u1ea4 2",
},
{ U"\u1ebf2\u00c0",
0,
U"e\u0302\u03012A\u0300",
},
{ U"\u1e0b\uf900\u0323",
0,
U"d\u0307\u8c48\u0323",
},
{ U"\u0374",
0,
U"\u02b9",
},
{ U"\u0374",
UNICODE_DECOMPOSE_FLAG_QC,
U"\u02b9",
},
{ U"\u0374",
UNICODE_DECOMPOSE_FLAG_COMPAT,
U"\u02b9",
},
{ U"\u2460",
0,
U"\u2460"},
{ U"\u2460",
UNICODE_DECOMPOSE_FLAG_COMPAT,
U"1"},
{ U"\u1eb4",
0,
U"\u0041\u0306\u0303" },
};
for (const auto &t:tests)
{
size_t i;
for (i=0; t.before[i]; ++i)
;
char32_t *before=(char32_t *)malloc((i+1) * sizeof(char32_t));
for (i=0; (before[i]=t.before[i]) != 0; ++i)
;
unicode_decomposition_t info;
unicode_decomposition_init(&info, before, (size_t)-1, NULL);
info.decompose_flags=t.flags;
unicode_decompose(&info);
unicode_decomposition_deinit(&info);
if (info.string[info.string_size] != 0)
{
std::cerr << "C decompose failed, not \\0 terminated."
<< std::endl;
exit(1);
}
if (std::u32string{t.after} !=
std::u32string{info.string, info.string+info.string_size})
{
std::cerr << "C decompose failed:\nExpected: ";
dump(t.after, (size_t)-1);
std::cerr << "Received: ";
dump(info.string, info.string_size);
exit(1);
}
free(info.string);
std::u32string us{t.before};
unicode::decompose(us, t.flags);
if (std::u32string{t.after} != us)
{
std::cerr << "C++ decompose failed:\nExpected: ";
dump(t.after, (size_t)-1);
std::cerr << "Received: ";
dump(us.c_str(), us.size());
exit(1);
}
}
}
static const uint8_t rangetab[][2]={
{0x0040 & 0xFF, 0x0041 & 0xFF},
{0x0045 & 0xFF, 0x0046 & 0xFF},
{0x0340 & 0xFF, 0x0341 & 0xFF},
{0x0343 & 0xFF, 0x0344 & 0xFF},
};
static const size_t starting_indextab[]={
0,
3
};
static const char32_t starting_pagetab[]={
0,
2,
};
void testtablookup()
{
static const struct {
char32_t ch;
int found;
} tests[]={
{0, 0},
{0x3F, 0},
{0x40, 1},
{0x41, 1},
{0x42, 0},
{0x43, 0},
{0x44, 0},
{0x45, 1},
{0x46, 1},
{0x47, 0},
{0xFF, 0},
{0x0141, 0},
{0x0201, 0},
{0x0241, 0},
{0x0301, 0},
{0x033F, 0},
{0x0340, 1},
{0x0341, 1},
{0x0342, 0},
{0x0343, 1},
{0x0441, 0},
};
for (const auto &t:tests)
{
if (unicode_tab_lookup(t.ch,
starting_indextab,
starting_pagetab,
sizeof(starting_indextab)/
sizeof(starting_indextab[0]),
rangetab,
sizeof(rangetab)/
sizeof(rangetab[0]),
NULL,
0) != t.found)
{
std::cerr << "unicode_tab_lookup failed for "
<< (unsigned long)t.ch << std::endl;
}
}
}
void testcompose1()
{
for (size_t i=0; i<sizeof(canonical_compositions)/
sizeof(canonical_compositions[0]); ++i)
{
char32_t string[2]={canonical_compositions[i][0],
canonical_compositions[i][1]};
size_t new_size;
if (unicode_compose(string, 2,
UNICODE_COMPOSE_FLAG_ONESHOT, &new_size)
|| new_size != 1
|| string[0] != canonical_compositions[i][2])
{
std::cerr << "testcompose1: failed to compose "
<< std::hex << std::setw(4)
<< std::setfill('0')
<< (unsigned long)canonical_compositions[i][0]
<< " with "
<< std::hex << std::setw(4)
<< std::setfill('0')
<< (unsigned long)canonical_compositions[i][1]
<< std::endl;
exit(1);
}
string[0]=canonical_compositions[i][1];
string[1]=canonical_compositions[i][0];
if (unicode_compose(string, 2, 0, &new_size)
|| new_size != 2
|| string[0] != canonical_compositions[i][1]
|| string[1] != canonical_compositions[i][0])
{
std::cerr << "testcompose1: should not compose "
<< std::hex << std::setw(4)
<< std::setfill('0')
<< (unsigned long)canonical_compositions[i][1]
<< " with "
<< std::hex << std::setw(4)
<< std::setfill('0')
<< (unsigned long)canonical_compositions[i][0]
<< std::endl;
exit(1);
}
}
}
void testcompose2()
{
static const struct {
std::u32string decomposed;
std::u32string composed;
int flags;
} tests[]={
{
U"\u0d47\u0d3e\u0d47\u0d3e",
U"\u0d4b\u0d4b",
0,
},
{
U"0z\u0335\u0327\u0324\u03011",
U"0\u017a\u0335\u0327\u03241",
0,
},
{
U"0z\u0301\u0335\u0327\u03241",
U"0\u017a\u0335\u0327\u03241",
0,
},
{
U"\u0041\u0306\u0303",
U"\u1eb4",
0,
},
{
U"\u0041\u0335\u0306\u0303",
U"\u1eb4\u0335",
0,
},
{
U"\u0041\u0335\u0306\u0303",
U"\u1eb4",
unicode::compose_flag_removeunused,
},
{
U"a\u0041\u0335\u0306\u0303b",
U"a\u1eb4\u0335b",
0,
},
{
U"a\u0041\u0335\u0306\u0303b",
U"a\u1eb4b",
unicode::compose_flag_removeunused,
},
{
U"\u0306\u0303",
U"\u0306\u0303",
0,
},
{
U"\u0306\u0303",
U"",
unicode::compose_flag_removeunused,
},
{
U"\u0306\u0303a",
U"\u0306\u0303a",
0,
},
{
U"\u0306\u0303a",
U"a",
unicode::compose_flag_removeunused,
},
};
for (const auto &t:tests)
{
std::u32string s=t.decomposed;
unicode::compose(s, t.flags);
if (s != t.composed)
{
std::cerr << "testcompose2 failed"
<< std::endl;
}
}
}
#include <map>
#include <tuple>
int main(int argc, char **argv)
{
testdecompose();
testtablookup();
testcompose1();
testcompose2();
return 0;
}
|