1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2008 Index Data
3 * See the file LICENSE for details.
6 /* DO NOT EDIT THIS FILE IF YOUR EDITOR DOES NOT SUPPORT UTF-8 */
15 #include <yaz/timing.h>
21 #include <yaz/icu_I18N.h>
26 /* DO NOT EDIT THIS FILE IF YOUR EDITOR DOES NOT SUPPORT UTF-8 */
29 #define MAX_KEY_SIZE 256
32 uint8_t sort_key[MAX_KEY_SIZE]; /* standard C string '\0' terminated */
33 char disp_term[MAX_KEY_SIZE]; /* standard C utf-8 string */
38 int icu_termmap_cmp(const void *vp1, const void *vp2)
40 struct icu_termmap *itmp1 = *(struct icu_termmap **) vp1;
41 struct icu_termmap *itmp2 = *(struct icu_termmap **) vp2;
45 cmp = strcmp((const char *)itmp1->sort_key,
46 (const char *)itmp2->sort_key);
53 int test_icu_casemap(const char * locale, char action,
54 const char * src8cstr, const char * chk8cstr)
57 UErrorCode status = U_ZERO_ERROR;
59 struct icu_buf_utf8 * src8 = icu_buf_utf8_create(0);
60 struct icu_buf_utf8 * dest8 = icu_buf_utf8_create(0);
61 struct icu_buf_utf16 * src16 = icu_buf_utf16_create(0);
62 struct icu_buf_utf16 * dest16 = icu_buf_utf16_create(0);
65 int src8cstr_len = strlen(src8cstr);
66 int chk8cstr_len = strlen(chk8cstr);
68 /* converting to UTF16 */
69 icu_utf16_from_utf8_cstr(src16, src8cstr, &status);
71 /* perform case mapping */
72 icu_utf16_casemap(dest16, src16, locale, action, &status);
74 /* converting to UTF8 */
75 icu_utf16_to_utf8(dest8, dest16, &status);
79 /* determine success */
81 && (dest8->utf8_len == strlen(chk8cstr))
82 && !strcmp(chk8cstr, (const char *) dest8->utf8))
90 printf("original string: '%s' (%d)\n", src8cstr, src8cstr_len);
91 printf("icu_casemap '%s:%c' '%s' (%d)\n",
92 locale, action, dest8->utf8, dest8->utf8_len);
93 printf("expected string: '%s' (%d)\n", chk8cstr, chk8cstr_len);
96 /* clean the buffers */
97 icu_buf_utf8_destroy(src8);
98 icu_buf_utf8_destroy(dest8);
99 icu_buf_utf16_destroy(src16);
100 icu_buf_utf16_destroy(dest16);
108 /* DO NOT EDIT THIS FILE IF YOUR EDITOR DOES NOT SUPPORT UTF-8 */
110 void test_icu_I18N_casemap(int argc, char **argv)
115 /* successful tests */
116 YAZ_CHECK(test_icu_casemap("en", 'l',
117 "A ReD fOx hunTS sQUirriLs",
118 "a red fox hunts squirrils"));
120 YAZ_CHECK(test_icu_casemap("en", 'u',
121 "A ReD fOx hunTS sQUirriLs",
122 "A RED FOX HUNTS SQUIRRILS"));
124 YAZ_CHECK(test_icu_casemap("en", 'f',
125 "A ReD fOx hunTS sQUirriLs",
126 "a red fox hunts squirrils"));
128 YAZ_CHECK(test_icu_casemap("en", 't',
129 "A ReD fOx hunTS sQUirriLs",
130 "A Red Fox Hunts Squirrils"));
135 /* success expected */
136 YAZ_CHECK(test_icu_casemap("da", 'l',
137 "åh ÆbLE, øs fLØde i Åen efter bLåBærGRødeN",
138 "åh æble, øs fløde i åen efter blåbærgrøden"));
140 YAZ_CHECK(test_icu_casemap("da", 'u',
141 "åh ÆbLE, øs fLØde i Åen efter bLåBærGRødeN",
142 "ÅH ÆBLE, ØS FLØDE I ÅEN EFTER BLÅBÆRGRØDEN"));
144 YAZ_CHECK(test_icu_casemap("da", 'f',
145 "åh ÆbLE, øs fLØde i Åen efter bLåBærGRødeN",
146 "åh æble, øs fløde i åen efter blåbærgrøden"));
148 YAZ_CHECK(test_icu_casemap("da", 't',
149 "åh ÆbLE, øs fLØde i Åen efter bLåBærGRødeN",
150 "Åh Æble, Øs Fløde I Åen Efter Blåbærgrøden"));
154 /* success expected */
155 YAZ_CHECK(test_icu_casemap("de", 'l',
156 "zWÖlf ärgerliche Würste rollen ÜBer die StRAße",
157 "zwölf ärgerliche würste rollen über die straße"));
159 YAZ_CHECK(test_icu_casemap("de", 'u',
160 "zWÖlf ärgerliche Würste rollen ÜBer die StRAße",
161 "ZWÖLF ÄRGERLICHE WÜRSTE ROLLEN ÜBER DIE STRASSE"));
163 YAZ_CHECK(test_icu_casemap("de", 'f',
164 "zWÖlf ärgerliche Würste rollen ÜBer die StRAße",
165 "zwölf ärgerliche würste rollen über die strasse"));
167 YAZ_CHECK(test_icu_casemap("de", 't',
168 "zWÖlf ärgerliche Würste rollen ÜBer die StRAße",
169 "Zwölf Ärgerliche Würste Rollen Über Die Straße"));
174 /* DO NOT EDIT THIS FILE IF YOUR EDITOR DOES NOT SUPPORT UTF-8 */
176 int test_icu_sortmap(const char * locale, int src_list_len,
177 const char ** src_list, const char ** chk_list)
181 UErrorCode status = U_ZERO_ERROR;
183 struct icu_buf_utf8 * buf8 = icu_buf_utf8_create(0);
184 struct icu_buf_utf16 * buf16 = icu_buf_utf16_create(0);
188 struct icu_termmap * list[src_list_len];
190 UCollator *coll = ucol_open(locale, &status);
191 icu_check_status(status);
193 if(U_FAILURE(status))
196 /* assigning display terms and sort keys using buf 8 and buf16 */
197 for( i = 0; i < src_list_len; i++)
200 list[i] = (struct icu_termmap *) malloc(sizeof(struct icu_termmap));
202 /* copy display term */
203 strcpy(list[i]->disp_term, src_list[i]);
205 /* transforming to UTF16 */
206 icu_utf16_from_utf8_cstr(buf16, list[i]->disp_term, &status);
207 icu_check_status(status);
209 /* computing sortkeys */
210 icu_sortkey8_from_utf16(coll, buf8, buf16, &status);
211 icu_check_status(status);
213 /* assigning sortkeys */
214 memcpy(list[i]->sort_key, buf8->utf8, buf8->utf8_len);
219 qsort(list, src_list_len,
220 sizeof(struct icu_termmap *), icu_termmap_cmp);
222 /* checking correct sorting */
223 for (i = 0; i < src_list_len; i++){
224 if (0 != strcmp(list[i]->disp_term, chk_list[i])){
231 printf("Input str: '%s' : ", locale);
232 for (i = 0; i < src_list_len; i++) {
233 printf(" '%s'", list[i]->disp_term);
236 printf("ICU sort: '%s' : ", locale);
237 for (i = 0; i < src_list_len; i++) {
238 printf(" '%s'", list[i]->disp_term);
241 printf("Expected: '%s' : ", locale);
242 for (i = 0; i < src_list_len; i++) {
243 printf(" '%s'", chk_list[i]);
250 for( i = 0; i < src_list_len; i++)
256 icu_buf_utf8_destroy(buf8);
257 icu_buf_utf16_destroy(buf16);
263 /* DO NOT EDIT THIS FILE IF YOUR EDITOR DOES NOT SUPPORT UTF-8 */
265 void test_icu_I18N_sortmap(int argc, char **argv)
268 /* successful tests */
270 const char * en_1_src[6] = {"z", "K", "a", "A", "Z", "k"};
271 const char * en_1_cck[6] = {"a", "A", "k", "K", "z", "Z"};
272 YAZ_CHECK(test_icu_sortmap("en", en_1_len, en_1_src, en_1_cck));
273 YAZ_CHECK(test_icu_sortmap("en_AU", en_1_len, en_1_src, en_1_cck));
274 YAZ_CHECK(test_icu_sortmap("en_CA", en_1_len, en_1_src, en_1_cck));
275 YAZ_CHECK(test_icu_sortmap("en_GB", en_1_len, en_1_src, en_1_cck));
276 YAZ_CHECK(test_icu_sortmap("en_US", en_1_len, en_1_src, en_1_cck));
278 /* successful tests */
281 const char * da_1_src[6] = {"z", "å", "o", "æ", "a", "ø"};
282 const char * da_1_cck[6] = {"a", "o", "z", "æ", "ø", "å"};
283 YAZ_CHECK(test_icu_sortmap("da", da_1_len, da_1_src, da_1_cck));
284 YAZ_CHECK(test_icu_sortmap("da_DK", da_1_len, da_1_src, da_1_cck));
286 /* successful tests */
289 const char * de_1_src[9] = {"u", "ä", "o", "t", "s", "ß", "ü", "ö", "a"};
290 const char * de_1_cck[9] = {"a","ä", "o", "ö", "s", "ß", "t", "u", "ü"};
291 YAZ_CHECK(test_icu_sortmap("de", de_1_len, de_1_src, de_1_cck));
292 YAZ_CHECK(test_icu_sortmap("de_AT", de_1_len, de_1_src, de_1_cck));
293 YAZ_CHECK(test_icu_sortmap("de_DE", de_1_len, de_1_src, de_1_cck));
299 /* DO NOT EDIT THIS FILE IF YOUR EDITOR DOES NOT SUPPORT UTF-8 */
304 int test_icu_normalizer(const char * rules8cstr,
305 const char * src8cstr,
306 const char * chk8cstr)
310 UErrorCode status = U_ZERO_ERROR;
312 struct icu_buf_utf16 * src16 = icu_buf_utf16_create(0);
313 struct icu_buf_utf16 * dest16 = icu_buf_utf16_create(0);
314 struct icu_buf_utf8 * dest8 = icu_buf_utf8_create(0);
315 struct icu_normalizer * normalizer
316 = icu_normalizer_create(rules8cstr, 'f', &status);
317 icu_check_status(status);
319 icu_utf16_from_utf8_cstr(src16, src8cstr, &status);
320 icu_check_status(status);
322 icu_normalizer_normalize(normalizer, dest16, src16, &status);
323 icu_check_status(status);
325 icu_utf16_to_utf8(dest8, dest16, &status);
326 icu_check_status(status);
329 if(!strcmp((const char *) dest8->utf8,
330 (const char *) chk8cstr))
334 printf("Normalization\n");
335 printf("Rules: '%s'\n", rules8cstr);
336 printf("Input: '%s'\n", src8cstr);
337 printf("Normalized: '%s'\n", dest8->utf8);
338 printf("Expected: '%s'\n", chk8cstr);
342 icu_normalizer_destroy(normalizer);
343 icu_buf_utf16_destroy(src16);
344 icu_buf_utf16_destroy(dest16);
345 icu_buf_utf8_destroy(dest8);
351 /* DO NOT EDIT THIS FILE IF YOUR EDITOR DOES NOT SUPPORT UTF-8 */
353 void test_icu_I18N_normalizer(int argc, char **argv)
356 YAZ_CHECK(test_icu_normalizer("[:Punctuation:] Any-Remove",
360 YAZ_CHECK(test_icu_normalizer("[:Control:] Any-Remove",
364 YAZ_CHECK(test_icu_normalizer("[:Decimal_Number:] Any-Remove",
368 YAZ_CHECK(test_icu_normalizer("Lower; [:^Letter:] Remove",
372 YAZ_CHECK(test_icu_normalizer("[:^Number:] Remove",
373 "Monday 15th of April",
376 YAZ_CHECK(test_icu_normalizer("Lower;"
377 "[[:WhiteSpace:][:Punctuation:]] Remove",
382 YAZ_CHECK(test_icu_normalizer("NFD; [:Nonspacing Mark:] Remove; NFC",
383 "à côté de l'alcôve ovoïde",
384 "a cote de l'alcove ovoide"));
389 /* DO NOT EDIT THIS FILE IF YOUR EDITOR DOES NOT SUPPORT UTF-8 */
391 int test_icu_tokenizer(const char * locale, char action,
392 const char * src8cstr, int count)
396 UErrorCode status = U_ZERO_ERROR;
397 struct icu_buf_utf16 * src16 = icu_buf_utf16_create(0);
398 struct icu_buf_utf16 * tkn16 = icu_buf_utf16_create(0);
399 struct icu_buf_utf8 * tkn8 = icu_buf_utf8_create(0);
400 struct icu_tokenizer * tokenizer = 0;
402 /* transforming to UTF16 */
403 icu_utf16_from_utf8_cstr(src16, src8cstr, &status);
404 icu_check_status(status);
406 /* set up tokenizer */
407 tokenizer = icu_tokenizer_create(locale, action, &status);
408 icu_check_status(status);
409 YAZ_CHECK(tokenizer);
411 /* attach text buffer to tokenizer */
412 icu_tokenizer_attach(tokenizer, src16, &status);
413 icu_check_status(status);
414 YAZ_CHECK(tokenizer->bi);
416 /* perform work on tokens */
417 while(icu_tokenizer_next_token(tokenizer, tkn16, &status)){
418 icu_check_status(status);
420 /* converting to UTF8 */
421 icu_utf16_to_utf8(tkn8, tkn16, &status);
424 if (count != icu_tokenizer_token_count(tokenizer)){
426 printf("\nTokenizer '%s:%c' Error: \n", locale, action);
427 printf("Input: '%s'\n", src8cstr);
428 printf("Tokens: %d", icu_tokenizer_token_count(tokenizer));
429 printf(", expected: %d\n", count);
432 icu_tokenizer_destroy(tokenizer);
433 icu_buf_utf16_destroy(src16);
434 icu_buf_utf16_destroy(tkn16);
435 icu_buf_utf8_destroy(tkn8);
441 /* DO NOT EDIT THIS FILE IF YOUR EDITOR DOES NOT SUPPORT UTF-8 */
443 void test_icu_I18N_tokenizer(int argc, char **argv)
448 = "O Romeo, Romeo! wherefore art thou Romeo?";
450 YAZ_CHECK(test_icu_tokenizer("en", 's', en_str, 2));
451 YAZ_CHECK(test_icu_tokenizer("en", 'l', en_str, 7));
452 YAZ_CHECK(test_icu_tokenizer("en", 'w', en_str, 16));
453 YAZ_CHECK(test_icu_tokenizer("en", 'c', en_str, 41));
459 = "Blåbærtærte. Denne kage stammer fra Finland. "
460 "Den er med blåbær, men alle sommerens forskellige bær kan bruges.";
462 YAZ_CHECK(test_icu_tokenizer("da", 's', da_str, 3));
463 YAZ_CHECK(test_icu_tokenizer("dar", 'l', da_str, 17));
464 YAZ_CHECK(test_icu_tokenizer("da", 'w', da_str, 37));
465 YAZ_CHECK(test_icu_tokenizer("da", 'c', da_str, 110));
471 void test_icu_I18N_chain(int argc, char **argv)
474 = "O Romeo, Romeo! wherefore art thou\t Romeo?";
476 UErrorCode status = U_ZERO_ERROR;
477 struct icu_chain * chain = 0;
480 const char * xml_str = "<icu locale=\"en\">"
481 "<transform rule=\"[:Control:] Any-Remove\"/>"
482 "<tokenize rule=\"l\"/>"
483 "<transform rule=\"[[:WhiteSpace:][:Punctuation:]] Remove\"/>"
485 "<casemap rule=\"l\"/>"
489 xmlDoc *doc = xmlParseMemory(xml_str, strlen(xml_str));
490 xmlNode *xml_node = xmlDocGetRootElement(doc);
493 chain = icu_chain_xml_config(xml_node, 0, &status);
498 YAZ_CHECK(icu_chain_assign_cstr(chain, en_str, &status));
500 while (icu_chain_next_token(chain, &status)){
502 /* printf("%d '%s' '%s'\n",
503 icu_chain_token_number(chain),
504 icu_chain_token_norm(chain),
505 icu_chain_token_display(chain)); */
508 YAZ_CHECK_EQ(icu_chain_token_number(chain), 7);
511 YAZ_CHECK(icu_chain_assign_cstr(chain, "what is this?", &status));
513 while (icu_chain_next_token(chain, &status)){
515 /* printf("%d '%s' '%s'\n",
516 icu_chain_token_number(chain),
517 icu_chain_token_norm(chain),
518 icu_chain_token_display(chain)); */
522 YAZ_CHECK_EQ(icu_chain_token_number(chain), 3);
524 icu_chain_destroy(chain);
528 void test_bug_1140(void)
530 UErrorCode status = U_ZERO_ERROR;
531 struct icu_chain * chain = 0;
533 const char * xml_str = "<icu locale=\"en\">"
535 /* if the first rule is normalize instead. Then it works */
537 "<transform rule=\"[:Control:] Any-Remove\"/>"
539 "<tokenize rule=\"l\"/>"
540 "<transform rule=\"[[:WhiteSpace:][:Punctuation:]] Remove\"/>"
542 "<casemap rule=\"l\"/>"
546 xmlDoc *doc = xmlParseMemory(xml_str, strlen(xml_str));
547 xmlNode *xml_node = xmlDocGetRootElement(doc);
550 chain = icu_chain_xml_config(xml_node, 0, &status);
555 YAZ_CHECK(icu_chain_assign_cstr(
556 chain, "O Romeo, Romeo! wherefore art thou\t Romeo?",
559 while (icu_chain_next_token(chain, &status)){
561 /* printf("%d '%s' '%s'\n",
562 icu_chain_token_number(chain),
563 icu_chain_token_norm(chain),
564 icu_chain_token_display(chain)); */
569 YAZ_CHECK_EQ(icu_chain_token_number(chain), 7);
571 YAZ_CHECK(icu_chain_assign_cstr(chain, "what is this?", &status));
573 while (icu_chain_next_token(chain, &status)){
575 /* printf("%d '%s' '%s'\n",
576 icu_chain_token_number(chain),
577 icu_chain_token_norm(chain),
578 icu_chain_token_display(chain)); */
581 /* we expect 'what' 'is' 'this', i.e. 3 tokens */
582 YAZ_CHECK_EQ(icu_chain_token_number(chain), 3);
584 icu_chain_destroy(chain);
589 void test_chain_empty_token(void)
591 UErrorCode status = U_ZERO_ERROR;
592 struct icu_chain * chain = 0;
594 const char * xml_str = "<icu locale=\"en\">"
595 "<tokenize rule=\"w\"/>"
596 "<transform rule=\"[[:WhiteSpace:][:Punctuation:]] Remove\"/>"
599 xmlDoc *doc = xmlParseMemory(xml_str, strlen(xml_str));
600 xmlNode *xml_node = xmlDocGetRootElement(doc);
603 chain = icu_chain_xml_config(xml_node, 0, &status);
608 YAZ_CHECK(icu_chain_assign_cstr(
609 chain, "a string with 15 tokenss and 8 displays",
612 while (icu_chain_next_token(chain, &status)){
614 /* printf("%d '%s' '%s'\n",
615 icu_chain_token_number(chain),
616 icu_chain_token_norm(chain),
617 icu_chain_token_display(chain)); */
620 YAZ_CHECK_EQ(icu_chain_token_number(chain), 15);
622 icu_chain_destroy(chain);
625 void test_chain_empty_chain(void)
627 UErrorCode status = U_ZERO_ERROR;
628 struct icu_chain * chain = 0;
630 const char * xml_str = "<icu locale=\"en\">"
633 const char * src8 = "some 5487 weired !¤%&(/& sTuFf";
636 xmlDoc *doc = xmlParseMemory(xml_str, strlen(xml_str));
637 xmlNode *xml_node = xmlDocGetRootElement(doc);
640 chain = icu_chain_xml_config(xml_node, 0, &status);
645 YAZ_CHECK(icu_chain_assign_cstr(
649 while (icu_chain_next_token(chain, &status)){
651 /* printf("%d '%s' '%s'\n",
652 icu_chain_token_number(chain),
653 icu_chain_token_norm(chain),
654 icu_chain_token_display(chain)); */
657 YAZ_CHECK_EQ(icu_chain_token_number(chain), 1);
659 dest8 = (char *) icu_chain_token_norm(chain);
660 YAZ_CHECK_EQ(strcmp(src8, dest8), 0);
663 icu_chain_destroy(chain);
666 #endif /* YAZ_HAVE_ICU */
668 /* DO NOT EDIT THIS FILE IF YOUR EDITOR DOES NOT SUPPORT UTF-8 */
670 int main(int argc, char **argv)
673 YAZ_CHECK_INIT(argc, argv);
678 test_icu_I18N_casemap(argc, argv);
679 test_icu_I18N_sortmap(argc, argv);
680 test_icu_I18N_normalizer(argc, argv);
681 test_icu_I18N_tokenizer(argc, argv);
682 test_icu_I18N_chain(argc, argv);
683 test_chain_empty_token();
684 test_chain_empty_chain();
687 #else /* YAZ_HAVE_ICU */
689 printf("ICU unit tests omitted.\n"
690 "Please install libicu36-dev and icu-doc or similar\n");
693 #endif /* YAZ_HAVE_ICU */
699 /* DO NOT EDIT THIS FILE IF YOUR EDITOR DOES NOT SUPPORT UTF-8 */
706 * indent-tabs-mode: nil
708 * vim: shiftwidth=4 tabstop=8 expandtab