1 /* $Id: tst_icu_I18N.c,v 1.8 2007-10-25 08:42:21 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>
37 #include <yaz/icu_I18N.h>
42 /* DO NOT EDIT THIS FILE IF YOUR EDITOR DOES NOT SUPPORT UTF-8 */
45 #define MAX_KEY_SIZE 256
48 uint8_t sort_key[MAX_KEY_SIZE]; /* standard C string '\0' terminated */
49 char disp_term[MAX_KEY_SIZE]; /* standard C utf-8 string */
54 int icu_termmap_cmp(const void *vp1, const void *vp2)
56 struct icu_termmap *itmp1 = *(struct icu_termmap **) vp1;
57 struct icu_termmap *itmp2 = *(struct icu_termmap **) vp2;
61 cmp = strcmp((const char *)itmp1->sort_key,
62 (const char *)itmp2->sort_key);
69 int test_icu_casemap(const char * locale, char action,
70 const char * src8cstr, const char * chk8cstr)
73 UErrorCode status = U_ZERO_ERROR;
75 struct icu_buf_utf8 * src8 = icu_buf_utf8_create(0);
76 struct icu_buf_utf8 * dest8 = icu_buf_utf8_create(0);
77 struct icu_buf_utf16 * src16 = icu_buf_utf16_create(0);
78 struct icu_buf_utf16 * dest16 = icu_buf_utf16_create(0);
81 int src8cstr_len = strlen(src8cstr);
82 int chk8cstr_len = strlen(chk8cstr);
84 /* converting to UTF16 */
85 icu_utf16_from_utf8_cstr(src16, src8cstr, &status);
87 /* perform case mapping */
88 icu_utf16_casemap(dest16, src16, locale, action, &status);
90 /* converting to UTF8 */
91 icu_utf16_to_utf8(dest8, dest16, &status);
95 /* determine success */
97 && (dest8->utf8_len == strlen(chk8cstr))
98 && !strcmp(chk8cstr, (const char *) dest8->utf8))
103 /* report failures */
106 printf("original string: '%s' (%d)\n", src8cstr, src8cstr_len);
107 printf("icu_casemap '%s:%c' '%s' (%d)\n",
108 locale, action, dest8->utf8, dest8->utf8_len);
109 printf("expected string: '%s' (%d)\n", chk8cstr, chk8cstr_len);
112 /* clean the buffers */
113 icu_buf_utf8_destroy(src8);
114 icu_buf_utf8_destroy(dest8);
115 icu_buf_utf16_destroy(src16);
116 icu_buf_utf16_destroy(dest16);
124 /* DO NOT EDIT THIS FILE IF YOUR EDITOR DOES NOT SUPPORT UTF-8 */
126 void test_icu_I18N_casemap(int argc, char **argv)
131 /* successful tests */
132 YAZ_CHECK(test_icu_casemap("en", 'l',
133 "A ReD fOx hunTS sQUirriLs",
134 "a red fox hunts squirrils"));
136 YAZ_CHECK(test_icu_casemap("en", 'u',
137 "A ReD fOx hunTS sQUirriLs",
138 "A RED FOX HUNTS SQUIRRILS"));
140 YAZ_CHECK(test_icu_casemap("en", 'f',
141 "A ReD fOx hunTS sQUirriLs",
142 "a red fox hunts squirrils"));
144 YAZ_CHECK(test_icu_casemap("en", 't',
145 "A ReD fOx hunTS sQUirriLs",
146 "A Red Fox Hunts Squirrils"));
151 /* success expected */
152 YAZ_CHECK(test_icu_casemap("da", 'l',
153 "åh ÆbLE, øs fLØde i Åen efter bLåBærGRødeN",
154 "åh æble, øs fløde i åen efter blåbærgrøden"));
156 YAZ_CHECK(test_icu_casemap("da", 'u',
157 "åh ÆbLE, øs fLØde i Åen efter bLåBærGRødeN",
158 "ÅH ÆBLE, ØS FLØDE I ÅEN EFTER BLÅBÆRGRØDEN"));
160 YAZ_CHECK(test_icu_casemap("da", 'f',
161 "åh ÆbLE, øs fLØde i Åen efter bLåBærGRødeN",
162 "åh æble, øs fløde i åen efter blåbærgrøden"));
164 YAZ_CHECK(test_icu_casemap("da", 't',
165 "åh ÆbLE, øs fLØde i Åen efter bLåBærGRødeN",
166 "Åh Æble, Øs Fløde I Åen Efter Blåbærgrøden"));
170 /* success expected */
171 YAZ_CHECK(test_icu_casemap("de", 'l',
172 "zWÖlf ärgerliche Würste rollen ÜBer die StRAße",
173 "zwölf ärgerliche würste rollen über die straße"));
175 YAZ_CHECK(test_icu_casemap("de", 'u',
176 "zWÖlf ärgerliche Würste rollen ÜBer die StRAße",
177 "ZWÖLF ÄRGERLICHE WÜRSTE ROLLEN ÜBER DIE STRASSE"));
179 YAZ_CHECK(test_icu_casemap("de", 'f',
180 "zWÖlf ärgerliche Würste rollen ÜBer die StRAße",
181 "zwölf ärgerliche würste rollen über die strasse"));
183 YAZ_CHECK(test_icu_casemap("de", 't',
184 "zWÖlf ärgerliche Würste rollen ÜBer die StRAße",
185 "Zwölf Ärgerliche Würste Rollen Über Die Straße"));
190 /* DO NOT EDIT THIS FILE IF YOUR EDITOR DOES NOT SUPPORT UTF-8 */
192 int test_icu_sortmap(const char * locale, int src_list_len,
193 const char ** src_list, const char ** chk_list)
197 UErrorCode status = U_ZERO_ERROR;
199 struct icu_buf_utf8 * buf8 = icu_buf_utf8_create(0);
200 struct icu_buf_utf16 * buf16 = icu_buf_utf16_create(0);
204 struct icu_termmap * list[src_list_len];
206 UCollator *coll = ucol_open(locale, &status);
207 icu_check_status(status);
209 if(U_FAILURE(status))
212 /* assigning display terms and sort keys using buf 8 and buf16 */
213 for( i = 0; i < src_list_len; i++)
216 list[i] = (struct icu_termmap *) malloc(sizeof(struct icu_termmap));
218 /* copy display term */
219 strcpy(list[i]->disp_term, src_list[i]);
221 /* transforming to UTF16 */
222 icu_utf16_from_utf8_cstr(buf16, list[i]->disp_term, &status);
223 icu_check_status(status);
225 /* computing sortkeys */
226 icu_sortkey8_from_utf16(coll, buf8, buf16, &status);
227 icu_check_status(status);
229 /* assigning sortkeys */
230 memcpy(list[i]->sort_key, buf8->utf8, buf8->utf8_len);
235 qsort(list, src_list_len,
236 sizeof(struct icu_termmap *), icu_termmap_cmp);
238 /* checking correct sorting */
239 for (i = 0; i < src_list_len; i++){
240 if (0 != strcmp(list[i]->disp_term, chk_list[i])){
247 printf("Input str: '%s' : ", locale);
248 for (i = 0; i < src_list_len; i++) {
249 printf(" '%s'", list[i]->disp_term);
252 printf("ICU sort: '%s' : ", locale);
253 for (i = 0; i < src_list_len; i++) {
254 printf(" '%s'", list[i]->disp_term);
257 printf("Expected: '%s' : ", locale);
258 for (i = 0; i < src_list_len; i++) {
259 printf(" '%s'", chk_list[i]);
266 for( i = 0; i < src_list_len; i++)
272 icu_buf_utf8_destroy(buf8);
273 icu_buf_utf16_destroy(buf16);
279 /* DO NOT EDIT THIS FILE IF YOUR EDITOR DOES NOT SUPPORT UTF-8 */
281 void test_icu_I18N_sortmap(int argc, char **argv)
284 /* successful tests */
286 const char * en_1_src[6] = {"z", "K", "a", "A", "Z", "k"};
287 const char * en_1_cck[6] = {"a", "A", "k", "K", "z", "Z"};
288 YAZ_CHECK(test_icu_sortmap("en", en_1_len, en_1_src, en_1_cck));
289 YAZ_CHECK(test_icu_sortmap("en_AU", en_1_len, en_1_src, en_1_cck));
290 YAZ_CHECK(test_icu_sortmap("en_CA", en_1_len, en_1_src, en_1_cck));
291 YAZ_CHECK(test_icu_sortmap("en_GB", en_1_len, en_1_src, en_1_cck));
292 YAZ_CHECK(test_icu_sortmap("en_US", en_1_len, en_1_src, en_1_cck));
294 /* successful tests */
297 const char * da_1_src[6] = {"z", "å", "o", "æ", "a", "ø"};
298 const char * da_1_cck[6] = {"a", "o", "z", "æ", "ø", "å"};
299 YAZ_CHECK(test_icu_sortmap("da", da_1_len, da_1_src, da_1_cck));
300 YAZ_CHECK(test_icu_sortmap("da_DK", da_1_len, da_1_src, da_1_cck));
302 /* successful tests */
305 const char * de_1_src[9] = {"u", "ä", "o", "t", "s", "ß", "ü", "ö", "a"};
306 const char * de_1_cck[9] = {"a","ä", "o", "ö", "s", "ß", "t", "u", "ü"};
307 YAZ_CHECK(test_icu_sortmap("de", de_1_len, de_1_src, de_1_cck));
308 YAZ_CHECK(test_icu_sortmap("de_AT", de_1_len, de_1_src, de_1_cck));
309 YAZ_CHECK(test_icu_sortmap("de_DE", de_1_len, de_1_src, de_1_cck));
315 /* DO NOT EDIT THIS FILE IF YOUR EDITOR DOES NOT SUPPORT UTF-8 */
320 int test_icu_normalizer(const char * rules8cstr,
321 const char * src8cstr,
322 const char * chk8cstr)
326 UErrorCode status = U_ZERO_ERROR;
328 struct icu_buf_utf16 * src16 = icu_buf_utf16_create(0);
329 struct icu_buf_utf16 * dest16 = icu_buf_utf16_create(0);
330 struct icu_buf_utf8 * dest8 = icu_buf_utf8_create(0);
331 struct icu_normalizer * normalizer
332 = icu_normalizer_create(rules8cstr, 'f', &status);
333 icu_check_status(status);
335 icu_utf16_from_utf8_cstr(src16, src8cstr, &status);
336 icu_check_status(status);
338 icu_normalizer_normalize(normalizer, dest16, src16, &status);
339 icu_check_status(status);
341 icu_utf16_to_utf8(dest8, dest16, &status);
342 icu_check_status(status);
345 if(!strcmp((const char *) dest8->utf8,
346 (const char *) chk8cstr))
350 printf("Normalization\n");
351 printf("Rules: '%s'\n", rules8cstr);
352 printf("Input: '%s'\n", src8cstr);
353 printf("Normalized: '%s'\n", dest8->utf8);
354 printf("Expected: '%s'\n", chk8cstr);
358 icu_normalizer_destroy(normalizer);
359 icu_buf_utf16_destroy(src16);
360 icu_buf_utf16_destroy(dest16);
361 icu_buf_utf8_destroy(dest8);
367 /* DO NOT EDIT THIS FILE IF YOUR EDITOR DOES NOT SUPPORT UTF-8 */
369 void test_icu_I18N_normalizer(int argc, char **argv)
372 YAZ_CHECK(test_icu_normalizer("[:Punctuation:] Any-Remove",
376 YAZ_CHECK(test_icu_normalizer("[:Control:] Any-Remove",
380 YAZ_CHECK(test_icu_normalizer("[:Decimal_Number:] Any-Remove",
384 YAZ_CHECK(test_icu_normalizer("Lower; [:^Letter:] Remove",
388 YAZ_CHECK(test_icu_normalizer("[:^Number:] Remove",
389 "Monday 15th of April",
392 YAZ_CHECK(test_icu_normalizer("Lower;"
393 "[[:WhiteSpace:][:Punctuation:]] Remove",
398 YAZ_CHECK(test_icu_normalizer("NFD; [:Nonspacing Mark:] Remove; NFC",
399 "à côté de l'alcôve ovoïde",
400 "a cote de l'alcove ovoide"));
405 /* DO NOT EDIT THIS FILE IF YOUR EDITOR DOES NOT SUPPORT UTF-8 */
407 int test_icu_tokenizer(const char * locale, char action,
408 const char * src8cstr, int count)
412 UErrorCode status = U_ZERO_ERROR;
413 struct icu_buf_utf16 * src16 = icu_buf_utf16_create(0);
414 struct icu_buf_utf16 * tkn16 = icu_buf_utf16_create(0);
415 struct icu_buf_utf8 * tkn8 = icu_buf_utf8_create(0);
416 struct icu_tokenizer * tokenizer = 0;
418 /* transforming to UTF16 */
419 icu_utf16_from_utf8_cstr(src16, src8cstr, &status);
420 icu_check_status(status);
422 /* set up tokenizer */
423 tokenizer = icu_tokenizer_create(locale, action, &status);
424 icu_check_status(status);
425 YAZ_CHECK(tokenizer);
427 /* attach text buffer to tokenizer */
428 icu_tokenizer_attach(tokenizer, src16, &status);
429 icu_check_status(status);
430 YAZ_CHECK(tokenizer->bi);
432 /* perform work on tokens */
433 while(icu_tokenizer_next_token(tokenizer, tkn16, &status)){
434 icu_check_status(status);
436 /* converting to UTF8 */
437 icu_utf16_to_utf8(tkn8, tkn16, &status);
440 if (count != icu_tokenizer_token_count(tokenizer)){
442 printf("\nTokenizer '%s:%c' Error: \n", locale, action);
443 printf("Input: '%s'\n", src8cstr);
444 printf("Tokens: %d", icu_tokenizer_token_count(tokenizer));
445 printf(", expected: %d\n", count);
448 icu_tokenizer_destroy(tokenizer);
449 icu_buf_utf16_destroy(src16);
450 icu_buf_utf16_destroy(tkn16);
451 icu_buf_utf8_destroy(tkn8);
457 /* DO NOT EDIT THIS FILE IF YOUR EDITOR DOES NOT SUPPORT UTF-8 */
459 void test_icu_I18N_tokenizer(int argc, char **argv)
464 = "O Romeo, Romeo! wherefore art thou Romeo?";
466 YAZ_CHECK(test_icu_tokenizer("en", 's', en_str, 2));
467 YAZ_CHECK(test_icu_tokenizer("en", 'l', en_str, 7));
468 YAZ_CHECK(test_icu_tokenizer("en", 'w', en_str, 16));
469 YAZ_CHECK(test_icu_tokenizer("en", 'c', en_str, 41));
475 = "Blåbærtærte. Denne kage stammer fra Finland. "
476 "Den er med blåbær, men alle sommerens forskellige bær kan bruges.";
478 YAZ_CHECK(test_icu_tokenizer("da", 's', da_str, 3));
479 YAZ_CHECK(test_icu_tokenizer("dar", 'l', da_str, 17));
480 YAZ_CHECK(test_icu_tokenizer("da", 'w', da_str, 37));
481 YAZ_CHECK(test_icu_tokenizer("da", 'c', da_str, 110));
487 void test_icu_I18N_chain(int argc, char **argv)
490 = "O Romeo, Romeo! wherefore art thou\t Romeo?";
492 UErrorCode status = U_ZERO_ERROR;
493 struct icu_chain * chain = 0;
496 const char * xml_str = "<icu>"
497 "<normalize rule=\"[:Control:] Any-Remove\"/>"
498 "<tokenize rule=\"l\"/>"
499 "<normalize rule=\"[[:WhiteSpace:][:Punctuation:]] Remove\"/>"
501 "<casemap rule=\"l\"/>"
505 xmlDoc *doc = xmlParseMemory(xml_str, strlen(xml_str));
506 xmlNode *xml_node = xmlDocGetRootElement(doc);
509 // printf("ICU chain:\ninput: '%s'\n", en_str);
512 chain = icu_chain_xml_config(xml_node, (uint8_t *) "en", 0, &status);
517 YAZ_CHECK(icu_chain_assign_cstr(chain, en_str, &status));
519 while (icu_chain_next_token(chain, &status)){
521 // printf("%d '%s' '%s'\n",
522 // icu_chain_get_token_count(chain),
523 // icu_chain_get_norm(chain),
524 // icu_chain_get_display(chain));
527 YAZ_CHECK_EQ(icu_chain_get_token_count(chain), 7);
530 YAZ_CHECK(icu_chain_assign_cstr(chain, "what is this?", &status));
532 while (icu_chain_next_token(chain, &status)){
534 //printf("%d '%s' '%s'\n",
535 // icu_chain_get_token_count(chain),
536 // icu_chain_get_norm(chain),
537 // icu_chain_get_display(chain));
541 YAZ_CHECK_EQ(icu_chain_get_token_count(chain), 3);
543 icu_chain_destroy(chain);
547 void test_bug_1140(void)
549 UErrorCode status = U_ZERO_ERROR;
550 struct icu_chain * chain = 0;
552 const char * xml_str = "<icu>"
554 /* if the first rule is normalize instead. Then it works */
556 "<normalize rule=\"[:Control:] Any-Remove\"/>"
558 "<tokenize rule=\"l\"/>"
559 "<normalize rule=\"[[:WhiteSpace:][:Punctuation:]] Remove\"/>"
561 "<casemap rule=\"l\"/>"
565 xmlDoc *doc = xmlParseMemory(xml_str, strlen(xml_str));
566 xmlNode *xml_node = xmlDocGetRootElement(doc);
569 chain = icu_chain_xml_config(xml_node, (uint8_t *) "en", 0, &status);
574 YAZ_CHECK(icu_chain_assign_cstr(
575 chain, "O Romeo, Romeo! wherefore art thou\t Romeo?",
578 while (icu_chain_next_token(chain, &status)){
580 //printf("%d '%s' '%s'\n",
581 // icu_chain_get_token_count(chain),
582 // icu_chain_get_norm(chain),
583 // icu_chain_get_display(chain));
588 YAZ_CHECK_EQ(icu_chain_get_token_count(chain), 7);
590 YAZ_CHECK(icu_chain_assign_cstr(chain, "what is this?", &status));
592 while (icu_chain_next_token(chain, &status)){
594 //printf("%d '%s' '%s'\n",
595 // icu_chain_get_token_count(chain),
596 // icu_chain_get_norm(chain),
597 // icu_chain_get_display(chain));
600 /* we expect 'what' 'is' 'this', i.e. 3 tokens */
601 YAZ_CHECK_EQ(icu_chain_get_token_count(chain), 3);
603 icu_chain_destroy(chain);
608 void test_chain_empty_token(void)
610 UErrorCode status = U_ZERO_ERROR;
611 struct icu_chain * chain = 0;
613 const char * xml_str = "<icu>"
614 "<tokenize rule=\"w\"/>"
615 "<normalize rule=\"[[:WhiteSpace:][:Punctuation:]] Remove\"/>"
618 xmlDoc *doc = xmlParseMemory(xml_str, strlen(xml_str));
619 xmlNode *xml_node = xmlDocGetRootElement(doc);
622 chain = icu_chain_xml_config(xml_node, (uint8_t *) "en", 0, &status);
627 YAZ_CHECK(icu_chain_assign_cstr(
628 chain, "a string with 15 wordbreaks and 8 tokens",
631 while (icu_chain_next_token(chain, &status)){
633 //printf("%d '%s' '%s'\n",
634 // icu_chain_get_token_count(chain),
635 // icu_chain_get_norm(chain),
636 // icu_chain_get_display(chain));
639 YAZ_CHECK_EQ(icu_chain_get_token_count(chain), 8);
641 icu_chain_destroy(chain);
646 /* DO NOT EDIT THIS FILE IF YOUR EDITOR DOES NOT SUPPORT UTF-8 */
648 int main(int argc, char **argv)
651 YAZ_CHECK_INIT(argc, argv);
656 test_icu_I18N_casemap(argc, argv);
657 test_icu_I18N_sortmap(argc, argv);
658 test_icu_I18N_normalizer(argc, argv);
659 test_icu_I18N_tokenizer(argc, argv);
660 test_icu_I18N_chain(argc, argv);
661 test_chain_empty_token();
666 printf("ICU unit tests omitted.\n"
667 "Please install libicu36-dev and icu-doc or similar\n");
670 #endif /* HAVE_ICU */
676 /* DO NOT EDIT THIS FILE IF YOUR EDITOR DOES NOT SUPPORT UTF-8 */
683 * indent-tabs-mode: nil
685 * vim: shiftwidth=4 tabstop=8 expandtab