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
|
/* Copyright (c) 2015-2018 Dovecot authors, see the included COPYING file */
#include "lib.h"
#include "array.h"
#include "str.h"
#include "unichar.h"
#include "test-common.h"
#include "fts-icu.h"
#include <unicode/uclean.h>
static void test_fts_icu_utf8_to_utf16_ascii_resize(void)
{
ARRAY_TYPE(icu_utf16) dest;
test_begin("fts_icu_utf8_to_utf16 ascii resize");
t_array_init(&dest, 2);
test_assert(buffer_get_writable_size(dest.arr.buffer) == 4);
fts_icu_utf8_to_utf16(&dest, "12");
test_assert(array_count(&dest) == 2);
test_assert(buffer_get_writable_size(dest.arr.buffer) == 4);
fts_icu_utf8_to_utf16(&dest, "123");
test_assert(array_count(&dest) == 3);
test_assert(buffer_get_writable_size(dest.arr.buffer) == 7);
fts_icu_utf8_to_utf16(&dest, "12345");
test_assert(array_count(&dest) == 5);
test_end();
}
static void test_fts_icu_utf8_to_utf16_32bit_resize(void)
{
ARRAY_TYPE(icu_utf16) dest;
unsigned int i;
test_begin("fts_icu_utf8_to_utf16 32bit resize");
for (i = 1; i <= 2; i++) {
t_array_init(&dest, i);
test_assert(buffer_get_writable_size(dest.arr.buffer) == i*2);
fts_icu_utf8_to_utf16(&dest, "\xF0\x90\x90\x80"); /* 0x10400 */
test_assert(array_count(&dest) == 2);
}
test_end();
}
static void test_fts_icu_utf16_to_utf8(void)
{
string_t *dest = t_str_new(64);
const UChar src[] = { 0xbd, 'b', 'c' };
unsigned int i;
test_begin("fts_icu_utf16_to_utf8");
for (i = N_ELEMENTS(src); i > 0; i--) {
fts_icu_utf16_to_utf8(dest, src, i);
test_assert(dest->used == i+1);
}
test_end();
}
static void test_fts_icu_utf16_to_utf8_resize(void)
{
string_t *dest;
const UChar src = UNICODE_REPLACEMENT_CHAR;
unsigned int i;
test_begin("fts_icu_utf16_to_utf8 resize");
for (i = 2; i <= 6; i++) {
dest = t_str_new(i);
test_assert(buffer_get_writable_size(dest) == i);
fts_icu_utf16_to_utf8(dest, &src, 1);
test_assert(dest->used == 3);
test_assert(strcmp(str_c(dest), UNICODE_REPLACEMENT_CHAR_UTF8) == 0);
}
test_end();
}
static UTransliterator *get_translit(const char *id)
{
UTransliterator *translit;
ARRAY_TYPE(icu_utf16) id_utf16;
UErrorCode err = U_ZERO_ERROR;
UParseError perr;
t_array_init(&id_utf16, 8);
fts_icu_utf8_to_utf16(&id_utf16, id);
translit = utrans_openU(array_front(&id_utf16),
array_count(&id_utf16),
UTRANS_FORWARD, NULL, 0, &perr, &err);
test_assert(!U_FAILURE(err));
return translit;
}
static void test_fts_icu_translate(void)
{
const char *translit_id = "Any-Lower";
UTransliterator *translit;
ARRAY_TYPE(icu_utf16) dest;
const UChar src[] = { 0xbd, 'B', 'C' };
const char *error;
unsigned int i;
test_begin("fts_icu_translate");
t_array_init(&dest, 32);
translit = get_translit(translit_id);
for (i = N_ELEMENTS(src); i > 0; i--) {
array_clear(&dest);
test_assert(fts_icu_translate(&dest, src, i,
translit, &error) == 0);
test_assert(array_count(&dest) == i);
}
utrans_close(translit);
test_end();
}
static void test_fts_icu_translate_resize(void)
{
const char *translit_id = "Any-Hex";
const char *src_utf8 = "FOO";
ARRAY_TYPE(icu_utf16) src_utf16, dest;
UTransliterator *translit;
const char *error;
unsigned int i;
test_begin("fts_icu_translate_resize resize");
t_array_init(&src_utf16, 8);
translit = get_translit(translit_id);
for (i = 1; i <= 10; i++) {
array_clear(&src_utf16);
fts_icu_utf8_to_utf16(&src_utf16, src_utf8);
t_array_init(&dest, i);
test_assert(buffer_get_writable_size(dest.arr.buffer) == i*2);
test_assert(fts_icu_translate(&dest, array_front(&src_utf16),
array_count(&src_utf16),
translit, &error) == 0);
}
utrans_close(translit);
test_end();
}
static void test_fts_icu_lcase(void)
{
const char *src = "aBcD\xC3\x84\xC3\xA4";
string_t *dest = t_str_new(64);
test_begin("fts_icu_lcase");
fts_icu_lcase(dest, src);
test_assert(strcmp(str_c(dest), "abcd\xC3\xA4\xC3\xA4") == 0);
test_end();
}
static void test_fts_icu_lcase_resize(void)
{
const char *src = "a\xC3\x84";
string_t *dest;
unsigned int i;
test_begin("fts_icu_lcase resize");
for (i = 1; i <= 3; i++) {
dest = t_str_new(i);
test_assert(buffer_get_writable_size(dest) == i);
fts_icu_lcase(dest, src);
test_assert(strcmp(str_c(dest), "a\xC3\xA4") == 0);
test_assert(buffer_get_writable_size(dest) == 3);
}
test_end();
}
static void test_fts_icu_lcase_resize_invalid_utf8(void)
{
string_t *dest;
test_begin("fts_icu_lcase resize invalid utf8");
dest = t_str_new(1);
fts_icu_lcase(dest, ".\x80.");
test_end();
}
int main(void)
{
static void (*const test_functions[])(void) = {
test_fts_icu_utf8_to_utf16_ascii_resize,
test_fts_icu_utf8_to_utf16_32bit_resize,
test_fts_icu_utf16_to_utf8,
test_fts_icu_utf16_to_utf8_resize,
test_fts_icu_translate,
test_fts_icu_translate_resize,
test_fts_icu_lcase,
test_fts_icu_lcase_resize,
test_fts_icu_lcase_resize_invalid_utf8,
NULL
};
int ret = test_run(test_functions);
fts_icu_deinit();
return ret;
}
|