1 /* $Id: test_icu_I18N.c,v 1.6 2007-05-01 13:27:32 marc Exp $
2 Copyright (c) 2006-2007, Index Data.
4 This file is part of Pazpar2.
6 Pazpar2 is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
11 Pazpar2 is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with Pazpar2; see the file LICENSE. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 // DO NOT EDIT THIS FILE IF YOUR EDITOR DOES NOT SUPPORT UTF-8
31 #include <yaz/timing.h>
44 // DO NOT EDIT THIS FILE IF YOUR EDITOR DOES NOT SUPPORT UTF-8
46 int test_icu_casemap(const char * locale, char action,
47 const char * src8, const char * check8)
49 NMEM nmem = nmem_create();
52 const char * dest8 = 0;
54 //size_t src8_len = strlen(src8);
57 //printf("original string: '%s' (%d)\n", src8, (int) src8_len);
60 dest8 = icu_casemap(nmem, buf, buf_cap, &dest8_len,
61 src8, locale, action);
64 //printf("icu_casemap '%s:%c' '%s' (%d)\n",
65 // locale, action, dest8, (int) dest8_len);
68 && (dest8_len == strlen(check8))
69 && !strcmp(check8, dest8))
77 // DO NOT EDIT THIS FILE IF YOUR EDITOR DOES NOT SUPPORT UTF-8
79 void test_icu_I18N_casemap(int argc, char **argv)
85 YAZ_CHECK(test_icu_casemap("en", 'l',
86 "A ReD fOx hunTS sQUirriLs",
87 "a red fox hunts squirrils"));
89 YAZ_CHECK(test_icu_casemap("en", 'u',
90 "A ReD fOx hunTS sQUirriLs",
91 "A RED FOX HUNTS SQUIRRILS"));
93 YAZ_CHECK(test_icu_casemap("en", 'f',
94 "A ReD fOx hunTS sQUirriLs",
95 "a red fox hunts squirrils"));
97 // this one fails and needs more investigation ..
98 YAZ_CHECK(0 == test_icu_casemap("en", 't',
99 "A ReD fOx hunTS sQUirriLs",
100 "A Red Fox Hunts Squirrils"));
106 YAZ_CHECK(test_icu_casemap("da", 'l',
107 "åh ÆbLE, øs fLØde i Åen efter bLåBærGRødeN",
108 "åh æble, øs fløde i åen efter blåbærgrøden"));
110 YAZ_CHECK(test_icu_casemap("da", 'u',
111 "åh ÆbLE, øs fLØde i Åen efter bLåBærGRødeN",
112 "ÅH ÆBLE, ØS FLØDE I ÅEN EFTER BLÅBÆRGRØDEN"));
114 YAZ_CHECK(test_icu_casemap("da", 'f',
115 "åh ÆbLE, øs fLØde i Åen efter bLåBærGRødeN",
116 "åh æble, øs fløde i åen efter blåbærgrøden"));
118 YAZ_CHECK(test_icu_casemap("da", 't',
119 "åh ÆbLE, øs fLØde i Åen efter bLåBærGRødeN",
120 "Åh Æble, Øs Fløde I Åen Efter Blåbærgrøden"));
125 YAZ_CHECK(test_icu_casemap("de", 'l',
126 "zWÖlf ärgerliche Würste rollen ÜBer die StRAße",
127 "zwölf ärgerliche würste rollen über die straße"));
129 YAZ_CHECK(test_icu_casemap("de", 'u',
130 "zWÖlf ärgerliche Würste rollen ÜBer die StRAße",
131 "ZWÖLF ÄRGERLICHE WÜRSTE ROLLEN ÜBER DIE STRASSE"));
133 YAZ_CHECK(test_icu_casemap("de", 'f',
134 "zWÖlf ärgerliche Würste rollen ÜBer die StRAße",
135 "zwölf ärgerliche würste rollen über die strasse"));
137 YAZ_CHECK(test_icu_casemap("de", 't',
138 "zWÖlf ärgerliche Würste rollen ÜBer die StRAße",
139 "Zwölf Ärgerliche Würste Rollen Über Die Straße"));
144 // DO NOT EDIT THIS FILE IF YOUR EDITOR DOES NOT SUPPORT UTF-8
146 void test_icu_I18N_casemap_failures(int argc, char **argv)
149 size_t buf_cap = 128;
151 size_t dest8_len = 0;
152 NMEM nmem = nmem_create();
155 const char * src8 = "A ReD fOx hunTS sQUirriLs";
156 //size_t src8_len = strlen(src8);
158 //printf("original string: '%s' (%d)\n", src8, (int) src8_len);
160 // some calling error needs investigation
161 dest8 = icu_casemap(nmem, buf, buf_cap, &dest8_len,
163 YAZ_CHECK(0 == dest8_len);
164 //printf("icu_casemap 'en:t' '%s' (%d)\n", dest8, (int) dest8_len);
167 // attention: does not fail even if no locale 'xy_zz' defined
168 // it seems to default to english locale
169 dest8 = icu_casemap(nmem, buf, buf_cap, &dest8_len,
170 src8, "zz_abc", 'l');
171 YAZ_CHECK(dest8_len);
172 //printf("icu_casemap 'zz:l' '%s' (%d)\n", dest8, (int) dest8_len);
175 // shall fail - no buf buffer defined
176 dest8 = icu_casemap(nmem, 0, buf_cap, &dest8_len,
178 YAZ_CHECK(0 == dest8_len);
179 //printf("icu_casemap 'en:l' '%s' (%d)\n", dest8, (int) dest8_len);
181 // shall fail - no buf_cap defined
182 dest8 = icu_casemap(nmem, buf, 0, &dest8_len,
184 YAZ_CHECK(0 == dest8_len);
185 //printf("icu_casemap 'en:l' '%s' (%d)\n", dest8, (int) dest8_len);
187 // shall fail - no action 'x' defined
188 dest8 = icu_casemap(nmem, buf, buf_cap, &dest8_len,
190 YAZ_CHECK(0 == dest8_len);
191 //printf("icu_casemap 'en:x' '%s' (%d)\n", dest8, (int) dest8_len);
196 // DO NOT EDIT THIS FILE IF YOUR EDITOR DOES NOT SUPPORT UTF-8
198 int test_icu_sortmap(const char * locale, size_t list_len,
199 const char ** src8_list, const char ** check8_list)
206 NMEM nmem = nmem_create();
207 size_t buf_cap = 128;
209 struct icu_termmap ** dest8_list
210 = nmem_malloc(nmem, sizeof(struct icu_termmap *) * list_len);
211 //size_t dest8_len = 0;
212 //size_t src8_len = strlen(src8);
214 // initializing icu_termmap
215 for (i = 0; i < list_len; i++){
216 dest8_list[i] = icu_termmap_create(nmem);
217 dest8_list[i]->norm_term = nmem_strdup(nmem, src8_list[i]);
218 dest8_list[i]->disp_term = nmem_strdup(nmem, src8_list[i]);
219 //dest8_list[i]->sort_key = nmem_strdup(nmem, src8_list[i]);
220 //dest8_list[i]->sort_len = strlen(src8_list[i]);
221 dest8_list[i]->sort_key
222 = icu_sortmap(nmem, buf, buf_cap, &(dest8_list[i]->sort_len),
223 src8_list[i], locale);
227 qsort(dest8_list, list_len,
228 sizeof(struct icu_termmap *), icu_termmap_cmp);
230 // checking correct sorting
231 for (i = 0; i < list_len; i++){
232 if (0 != strcmp(dest8_list[i]->disp_term, check8_list[i])){
238 for (i = 0; i < list_len; i++){
239 printf("icu_sortmap '%s': '%s' '%s'\n", locale,
240 dest8_list[i]->disp_term, check8_list[i]);
249 // DO NOT EDIT THIS FILE IF YOUR EDITOR DOES NOT SUPPORT UTF-8
251 void test_icu_I18N_sortmap(int argc, char **argv)
256 const char * en_1_src[6] = {"z", "K", "a", "A", "Z", "k"};
257 const char * en_1_cck[6] = {"a", "A", "K", "k", "z", "Z"};
258 YAZ_CHECK(test_icu_sortmap("en", en_1_len, en_1_src, en_1_cck));
259 YAZ_CHECK(0 == test_icu_sortmap("en_AU", en_1_len, en_1_src, en_1_cck));
260 YAZ_CHECK(0 == test_icu_sortmap("en_CA", en_1_len, en_1_src, en_1_cck));
261 YAZ_CHECK(0 == test_icu_sortmap("en_GB", en_1_len, en_1_src, en_1_cck));
262 YAZ_CHECK(0 == test_icu_sortmap("en_US", en_1_len, en_1_src, en_1_cck));
264 // sucessful tests - this one fails and should not!!!
266 const char * da_1_src[6] = {"z", "å", "o", "æ", "a", "ø"};
267 const char * da_1_cck[6] = {"a", "o", "z", "æ", "ø", "å"};
268 YAZ_CHECK(0 == test_icu_sortmap("da", da_1_len, da_1_src, da_1_cck));
269 YAZ_CHECK(0 == test_icu_sortmap("da_DK", da_1_len, da_1_src, da_1_cck));
273 const char * de_1_src[9] = {"u", "ä", "o", "t", "s", "ß", "ü", "ö", "a"};
274 const char * de_1_cck[9] = {"ä", "a", "o", "ö", "s", "ß", "t", "u", "ü"};
275 YAZ_CHECK(test_icu_sortmap("de", de_1_len, de_1_src, de_1_cck));
276 YAZ_CHECK(0 == test_icu_sortmap("de_AT", de_1_len, de_1_src, de_1_cck));
277 YAZ_CHECK(0 == test_icu_sortmap("de_DE", de_1_len, de_1_src, de_1_cck));
284 // DO NOT EDIT THIS FILE IF YOUR EDITOR DOES NOT SUPPORT UTF-8
286 int main(int argc, char **argv)
289 YAZ_CHECK_INIT(argc, argv);
294 test_icu_I18N_casemap_failures(argc, argv);
295 test_icu_I18N_casemap(argc, argv);
296 test_icu_I18N_sortmap(argc, argv);
300 printf("ICU unit tests omitted.\n"
301 "Please install libicu36-dev and icu-doc or similar\n");
310 // DO NOT EDIT THIS FILE IF YOUR EDITOR DOES NOT SUPPORT UTF-8
315 * indent-tabs-mode: nil
317 * vim: shiftwidth=4 tabstop=8 expandtab