1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2010 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>
20 #include <yaz/wrbuf.h>
23 #include <yaz/icu_I18N.h>
32 #define MAX_KEY_SIZE 256
35 uint8_t sort_key[MAX_KEY_SIZE]; /* standard C string '\0' terminated */
36 char disp_term[MAX_KEY_SIZE]; /* standard C utf-8 string */
40 static int icu_termmap_cmp(const void *vp1, const void *vp2)
42 struct icu_termmap *itmp1 = *(struct icu_termmap **) vp1;
43 struct icu_termmap *itmp2 = *(struct icu_termmap **) vp2;
47 cmp = strcmp((const char *)itmp1->sort_key,
48 (const char *)itmp2->sort_key);
53 static 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);
77 /* determine success */
79 && (dest8->utf8_len == strlen(chk8cstr))
80 && !strcmp(chk8cstr, (const char *) dest8->utf8))
88 yaz_log(YLOG_WARN, "test_icu_casemap failed");
89 yaz_log(YLOG_LOG, "Original string: '%s' (%d)",
90 src8cstr, src8cstr_len);
91 yaz_log(YLOG_LOG, "icu_casemap '%s:%c' '%s' (%d)",
92 locale, action, dest8->utf8, dest8->utf8_len);
93 yaz_log(YLOG_LOG, "expected string: '%s' (%d)",
94 chk8cstr, chk8cstr_len);
97 /* clean the buffers */
98 icu_buf_utf8_destroy(src8);
99 icu_buf_utf8_destroy(dest8);
100 icu_buf_utf16_destroy(src16);
101 icu_buf_utf16_destroy(dest16);
106 static void check_icu_casemap(void)
110 /* successful tests */
111 YAZ_CHECK(test_icu_casemap("en", 'l',
112 "A ReD fOx hunTS sQUirriLs",
113 "a red fox hunts squirrils"));
115 YAZ_CHECK(test_icu_casemap("en", 'u',
116 "A ReD fOx hunTS sQUirriLs",
117 "A RED FOX HUNTS SQUIRRILS"));
119 YAZ_CHECK(test_icu_casemap("en", 'f',
120 "A ReD fOx hunTS sQUirriLs",
121 "a red fox hunts squirrils"));
123 YAZ_CHECK(test_icu_casemap("en", 't',
124 "A ReD fOx hunTS sQUirriLs",
125 "A Red Fox Hunts Squirrils"));
129 /* success expected */
130 YAZ_CHECK(test_icu_casemap("da", 'l',
131 "åh ÆbLE, øs fLØde i Åen efter bLåBærGRødeN",
132 "åh æble, øs fløde i åen efter blåbærgrøden"));
134 YAZ_CHECK(test_icu_casemap("da", 'u',
135 "åh ÆbLE, øs fLØde i Åen efter bLåBærGRødeN",
136 "ÅH ÆBLE, ØS FLØDE I ÅEN EFTER BLÅBÆRGRØDEN"));
138 YAZ_CHECK(test_icu_casemap("da", 'f',
139 "åh ÆbLE, øs fLØde i Åen efter bLåBærGRødeN",
140 "åh æble, øs fløde i åen efter blåbærgrøden"));
142 YAZ_CHECK(test_icu_casemap("da", 't',
143 "åh ÆbLE, øs fLØde i Åen efter bLåBærGRødeN",
144 "Åh Æble, Øs Fløde I Åen Efter Blåbærgrøden"));
148 /* success expected */
149 YAZ_CHECK(test_icu_casemap("de", 'l',
150 "zWÖlf ärgerliche Würste rollen ÜBer die StRAße",
151 "zwölf ärgerliche würste rollen über die straße"));
153 YAZ_CHECK(test_icu_casemap("de", 'u',
154 "zWÖlf ärgerliche Würste rollen ÜBer die StRAße",
155 "ZWÖLF ÄRGERLICHE WÜRSTE ROLLEN ÜBER DIE STRASSE"));
157 YAZ_CHECK(test_icu_casemap("de", 'f',
158 "zWÖlf ärgerliche Würste rollen ÜBer die StRAße",
159 "zwölf ärgerliche würste rollen über die strasse"));
161 YAZ_CHECK(test_icu_casemap("de", 't',
162 "zWÖlf ärgerliche Würste rollen ÜBer die StRAße",
163 "Zwölf Ärgerliche Würste Rollen Über Die Straße"));
167 static int test_icu_sortmap(const char * locale, int src_list_len,
168 const char ** src_list, const char ** chk_list)
172 UErrorCode status = U_ZERO_ERROR;
174 struct icu_buf_utf8 * buf8 = icu_buf_utf8_create(0);
175 struct icu_buf_utf16 * buf16 = icu_buf_utf16_create(0);
179 struct icu_termmap * list[src_list_len];
181 UCollator *coll = ucol_open(locale, &status);
182 icu_check_status(status);
184 if (U_FAILURE(status))
187 /* assigning display terms and sort keys using buf 8 and buf16 */
188 for (i = 0; i < src_list_len; i++)
191 list[i] = (struct icu_termmap *) malloc(sizeof(struct icu_termmap));
193 /* copy display term */
194 strcpy(list[i]->disp_term, src_list[i]);
196 /* transforming to UTF16 */
197 icu_utf16_from_utf8_cstr(buf16, list[i]->disp_term, &status);
198 icu_check_status(status);
200 /* computing sortkeys */
201 icu_sortkey8_from_utf16(coll, buf8, buf16, &status);
202 icu_check_status(status);
204 /* assigning sortkeys */
205 memcpy(list[i]->sort_key, buf8->utf8, buf8->utf8_len);
209 qsort(list, src_list_len, sizeof(struct icu_termmap *), icu_termmap_cmp);
211 /* checking correct sorting */
212 for (i = 0; i < src_list_len; i++)
214 if (0 != strcmp(list[i]->disp_term, chk_list[i])){
221 yaz_log(YLOG_LOG, "ERROR");
222 yaz_log(YLOG_LOG, "Input str:'%s':", locale);
223 for (i = 0; i < src_list_len; i++) {
224 yaz_log(YLOG_LOG, " '%s'", list[i]->disp_term);
226 yaz_log(YLOG_LOG, "ICU sort: '%s':", locale);
227 for (i = 0; i < src_list_len; i++) {
228 yaz_log(YLOG_LOG, " '%s'", list[i]->disp_term);
230 yaz_log(YLOG_LOG, "Expected: '%s':", locale);
231 for (i = 0; i < src_list_len; i++) {
232 yaz_log(YLOG_LOG, " '%s'", chk_list[i]);
236 for (i = 0; i < src_list_len; i++)
241 icu_buf_utf8_destroy(buf8);
242 icu_buf_utf16_destroy(buf16);
247 static void check_icu_sortmap(void)
249 /* successful tests */
251 const char * en_1_src[6] = {"z", "K", "a", "A", "Z", "k"};
252 const char * en_1_cck[6] = {"a", "A", "k", "K", "z", "Z"};
253 YAZ_CHECK(test_icu_sortmap("en", en_1_len, en_1_src, en_1_cck));
254 YAZ_CHECK(test_icu_sortmap("en_AU", en_1_len, en_1_src, en_1_cck));
255 YAZ_CHECK(test_icu_sortmap("en_CA", en_1_len, en_1_src, en_1_cck));
256 YAZ_CHECK(test_icu_sortmap("en_GB", en_1_len, en_1_src, en_1_cck));
257 YAZ_CHECK(test_icu_sortmap("en_US", en_1_len, en_1_src, en_1_cck));
259 /* successful tests */
262 const char * da_1_src[6] = {"z", "å", "o", "æ", "a", "ø"};
263 const char * da_1_cck[6] = {"a", "o", "z", "æ", "ø", "å"};
264 YAZ_CHECK(test_icu_sortmap("da", da_1_len, da_1_src, da_1_cck));
265 YAZ_CHECK(test_icu_sortmap("da_DK", da_1_len, da_1_src, da_1_cck));
267 /* successful tests */
270 const char * de_1_src[9] = {"u", "ä", "o", "t", "s", "ß", "ü", "ö", "a"};
271 const char * de_1_cck[9] = {"a","ä", "o", "ö", "s", "ß", "t", "u", "ü"};
272 YAZ_CHECK(test_icu_sortmap("de", de_1_len, de_1_src, de_1_cck));
273 YAZ_CHECK(test_icu_sortmap("de_AT", de_1_len, de_1_src, de_1_cck));
274 YAZ_CHECK(test_icu_sortmap("de_DE", de_1_len, de_1_src, de_1_cck));
278 static int test_icu_normalizer(const char * rules8cstr,
279 const char * src8cstr,
280 const char * chk8cstr)
284 UErrorCode status = U_ZERO_ERROR;
286 struct icu_buf_utf16 * src16 = icu_buf_utf16_create(0);
287 struct icu_buf_utf16 * dest16 = icu_buf_utf16_create(0);
288 struct icu_buf_utf8 * dest8 = icu_buf_utf8_create(0);
289 struct icu_transform * transform
290 = icu_transform_create(rules8cstr, 'f', 0, &status);
291 icu_check_status(status);
293 icu_utf16_from_utf8_cstr(src16, src8cstr, &status);
294 icu_check_status(status);
296 icu_transform_trans(transform, dest16, src16, &status);
297 icu_check_status(status);
299 icu_utf16_to_utf8(dest8, dest16, &status);
300 icu_check_status(status);
303 if (!strcmp((const char *) dest8->utf8,
304 (const char *) chk8cstr))
309 yaz_log(YLOG_LOG, "Normalization");
310 yaz_log(YLOG_LOG, " Rules: '%s'", rules8cstr);
311 yaz_log(YLOG_LOG, " Input: '%s'", src8cstr);
312 yaz_log(YLOG_LOG, " Normalized: '%s'", dest8->utf8);
313 yaz_log(YLOG_LOG, " Expected: '%s'", chk8cstr);
316 icu_transform_destroy(transform);
317 icu_buf_utf16_destroy(src16);
318 icu_buf_utf16_destroy(dest16);
319 icu_buf_utf8_destroy(dest8);
324 static void check_icu_normalizer(void)
326 YAZ_CHECK(test_icu_normalizer("[:Punctuation:] Any-Remove",
330 YAZ_CHECK(test_icu_normalizer("[:Control:] Any-Remove",
334 YAZ_CHECK(test_icu_normalizer("[:Decimal_Number:] Any-Remove",
338 YAZ_CHECK(test_icu_normalizer("Lower; [:^Letter:] Remove",
342 YAZ_CHECK(test_icu_normalizer("[:^Number:] Remove",
343 "Monday 15th of April",
346 YAZ_CHECK(test_icu_normalizer("Lower;"
347 "[[:WhiteSpace:][:Punctuation:]] Remove",
351 YAZ_CHECK(test_icu_normalizer("NFD; [:Nonspacing Mark:] Remove; NFC",
352 "à côté de l'alcôve ovoïde",
353 "a cote de l'alcove ovoide"));
356 static int test_icu_tokenizer(const char * locale, char action,
357 const char * src8cstr, int count)
361 UErrorCode status = U_ZERO_ERROR;
362 struct icu_buf_utf16 * src16 = icu_buf_utf16_create(0);
363 struct icu_buf_utf16 * tkn16 = icu_buf_utf16_create(0);
364 struct icu_buf_utf8 * tkn8 = icu_buf_utf8_create(0);
365 struct icu_tokenizer * tokenizer = 0;
367 /* transforming to UTF16 */
368 icu_utf16_from_utf8_cstr(src16, src8cstr, &status);
369 icu_check_status(status);
371 /* set up tokenizer */
372 tokenizer = icu_tokenizer_create(locale, action, &status);
373 icu_check_status(status);
374 YAZ_CHECK(tokenizer);
376 /* attach text buffer to tokenizer */
377 icu_tokenizer_attach(tokenizer, src16, &status);
378 icu_check_status(status);
380 /* perform work on tokens */
381 while (icu_tokenizer_next_token(tokenizer, tkn16, &status))
383 icu_check_status(status);
385 /* converting to UTF8 */
386 icu_utf16_to_utf8(tkn8, tkn16, &status);
389 if (count != icu_tokenizer_token_count(tokenizer))
392 yaz_log(YLOG_LOG, "Tokenizer '%s:%c' Error:", locale, action);
393 yaz_log(YLOG_LOG, " Input: '%s'", src8cstr);
394 yaz_log(YLOG_LOG, " Tokens: %d", icu_tokenizer_token_count(tokenizer));
395 yaz_log(YLOG_LOG, " Expected: %d", count);
398 icu_tokenizer_destroy(tokenizer);
399 icu_buf_utf16_destroy(src16);
400 icu_buf_utf16_destroy(tkn16);
401 icu_buf_utf8_destroy(tkn8);
406 static void check_icu_tokenizer(void)
409 = "O Romeo, Romeo! wherefore art thou Romeo?";
411 YAZ_CHECK(test_icu_tokenizer("en", 's', en_str, 2));
412 YAZ_CHECK(test_icu_tokenizer("en", 'l', en_str, 7));
413 YAZ_CHECK(test_icu_tokenizer("en", 'w', en_str, 16));
414 YAZ_CHECK(test_icu_tokenizer("en", 'c', en_str, 41));
418 = "Blåbærtærte. Denne kage stammer fra Finland. "
419 "Den er med blåbær, men alle sommerens forskellige bær kan bruges.";
421 YAZ_CHECK(test_icu_tokenizer("da", 's', da_str, 3));
422 YAZ_CHECK(test_icu_tokenizer("dar", 'l', da_str, 17));
423 YAZ_CHECK(test_icu_tokenizer("da", 'w', da_str, 37));
424 YAZ_CHECK(test_icu_tokenizer("da", 'c', da_str, 110));
428 static void check_icu_chain(void)
431 = "O Romeo, Romeo! wherefore art thou\t Romeo?";
433 UErrorCode status = U_ZERO_ERROR;
434 struct icu_chain * chain = 0;
436 const char * xml_str = "<icu locale=\"en\">"
437 "<transform rule=\"[:Control:] Any-Remove\"/>"
438 "<tokenize rule=\"l\"/>"
439 "<transform rule=\"[[:WhiteSpace:][:Punctuation:]] Remove\"/>"
441 "<casemap rule=\"l\"/>"
445 xmlDoc *doc = xmlParseMemory(xml_str, strlen(xml_str));
446 xmlNode *xml_node = xmlDocGetRootElement(doc);
449 chain = icu_chain_xml_config(xml_node, 0, &status);
454 YAZ_CHECK(icu_chain_assign_cstr(chain, en_str, &status));
456 while (icu_chain_next_token(chain, &status))
458 yaz_log(YLOG_LOG, "%d '%s' '%s'",
459 icu_chain_token_number(chain),
460 icu_chain_token_norm(chain),
461 icu_chain_token_display(chain));
464 YAZ_CHECK_EQ(icu_chain_token_number(chain), 7);
467 YAZ_CHECK(icu_chain_assign_cstr(chain, "what is this?", &status));
469 while (icu_chain_next_token(chain, &status))
471 yaz_log(YLOG_LOG, "%d '%s' '%s'",
472 icu_chain_token_number(chain),
473 icu_chain_token_norm(chain),
474 icu_chain_token_display(chain));
478 YAZ_CHECK_EQ(icu_chain_token_number(chain), 3);
480 icu_chain_destroy(chain);
484 static void check_bug_1140(void)
486 UErrorCode status = U_ZERO_ERROR;
487 struct icu_chain * chain = 0;
489 const char * xml_str = "<icu locale=\"en\">"
491 /* if the first rule is normalize instead. Then it works */
493 "<transform rule=\"[:Control:] Any-Remove\"/>"
495 "<tokenize rule=\"l\"/>"
496 "<transform rule=\"[[:WhiteSpace:][:Punctuation:]] Remove\"/>"
498 "<casemap rule=\"l\"/>"
502 xmlDoc *doc = xmlParseMemory(xml_str, strlen(xml_str));
503 xmlNode *xml_node = xmlDocGetRootElement(doc);
506 chain = icu_chain_xml_config(xml_node, 0, &status);
511 YAZ_CHECK(icu_chain_assign_cstr(
512 chain, "O Romeo, Romeo! wherefore art thou\t Romeo?",
515 while (icu_chain_next_token(chain, &status))
518 /* printf("%d '%s' '%s'\n",
519 icu_chain_token_number(chain),
520 icu_chain_token_norm(chain),
521 icu_chain_token_display(chain)); */
525 YAZ_CHECK_EQ(icu_chain_token_number(chain), 7);
527 YAZ_CHECK(icu_chain_assign_cstr(chain, "what is this?", &status));
529 while (icu_chain_next_token(chain, &status))
532 /* printf("%d '%s' '%s'\n",
533 icu_chain_token_number(chain),
534 icu_chain_token_norm(chain),
535 icu_chain_token_display(chain)); */
538 /* we expect 'what' 'is' 'this', i.e. 3 tokens */
539 YAZ_CHECK_EQ(icu_chain_token_number(chain), 3);
541 icu_chain_destroy(chain);
545 static void check_chain_empty_token(void)
547 UErrorCode status = U_ZERO_ERROR;
548 struct icu_chain * chain = 0;
550 const char * xml_str = "<icu locale=\"en\">"
551 "<tokenize rule=\"w\"/>"
552 "<transform rule=\"[[:WhiteSpace:][:Punctuation:]] Remove\"/>"
555 xmlDoc *doc = xmlParseMemory(xml_str, strlen(xml_str));
556 xmlNode *xml_node = xmlDocGetRootElement(doc);
559 chain = icu_chain_xml_config(xml_node, 0, &status);
564 YAZ_CHECK(icu_chain_assign_cstr(
565 chain, "a string with 15 tokenss and 8 displays",
568 while (icu_chain_next_token(chain, &status))
571 /* printf("%d '%s' '%s'\n",
572 icu_chain_token_number(chain),
573 icu_chain_token_norm(chain),
574 icu_chain_token_display(chain)); */
577 YAZ_CHECK_EQ(icu_chain_token_number(chain), 15);
579 icu_chain_destroy(chain);
582 static void check_chain_empty_chain(void)
584 UErrorCode status = U_ZERO_ERROR;
585 struct icu_chain * chain = 0;
587 const char * xml_str = "<icu locale=\"en\">"
590 const char * src8 = "some 5487 weired !¤%&(/& sTuFf";
593 xmlDoc *doc = xmlParseMemory(xml_str, strlen(xml_str));
594 xmlNode *xml_node = xmlDocGetRootElement(doc);
597 chain = icu_chain_xml_config(xml_node, 0, &status);
602 YAZ_CHECK(icu_chain_assign_cstr(
606 while (icu_chain_next_token(chain, &status))
609 /* printf("%d '%s' '%s'\n",
610 icu_chain_token_number(chain),
611 icu_chain_token_norm(chain),
612 icu_chain_token_display(chain)); */
615 YAZ_CHECK_EQ(icu_chain_token_number(chain), 1);
617 dest8 = (char *) icu_chain_token_norm(chain);
618 YAZ_CHECK_EQ(strcmp(src8, dest8), 0);
620 icu_chain_destroy(chain);
623 static void check_icu_iter1(void)
625 UErrorCode status = U_ZERO_ERROR;
626 struct icu_chain * chain = 0;
628 struct icu_iter *iter;
629 struct icu_buf_utf8 *token;
631 const char * xml_str = "<icu locale=\"en\">"
632 "<tokenize rule=\"w\"/>"
633 "<transform rule=\"[[:WhiteSpace:][:Punctuation:]] Remove\"/>"
636 xmlDoc *doc = xmlParseMemory(xml_str, strlen(xml_str));
640 xml_node = xmlDocGetRootElement(doc);
645 chain = icu_chain_xml_config(xml_node, 0, &status);
650 iter = icu_iter_create(chain, "a string with 15 tokens and 8 displays");
654 token = icu_buf_utf8_create(0);
655 while (icu_iter_next(iter, token))
657 yaz_log(YLOG_LOG, "[%.*s]", (int) token->utf8_len, token->utf8);
659 icu_buf_utf8_destroy(token);
661 icu_iter_destroy(iter);
662 icu_chain_destroy(chain);
665 static int test_iter(struct icu_chain *chain, const char *input,
666 const char *expected)
668 struct icu_iter *iter = icu_iter_create(chain, input);
670 struct icu_buf_utf8 *token;
674 yaz_log(YLOG_WARN, "test_iter: input=%s !iter", input);
678 token = icu_buf_utf8_create(0);
679 result = wrbuf_alloc();
680 while (icu_iter_next(iter, token))
682 wrbuf_puts(result, "[");
683 wrbuf_write(result, (const char *) token->utf8, (int) token->utf8_len);
684 wrbuf_puts(result, "]");
686 icu_buf_utf8_destroy(token);
688 icu_iter_destroy(iter);
690 if (strcmp(expected, wrbuf_cstr(result)))
692 yaz_log(YLOG_WARN, "test_iter: input=%s expected=%s got=%s",
693 input, expected, wrbuf_cstr(result));
694 wrbuf_destroy(result);
697 wrbuf_destroy(result);
701 static void *iter_thread(void *p)
703 struct icu_chain *chain = (struct icu_chain *) p;
706 for (i = 0; i < 10000; i++)
708 YAZ_CHECK(test_iter(chain, "Adobe Acrobat Reader, 1991-1999.",
709 "[adobe][acrobat][reader][1991][][1999][]"));
714 static void check_iter_threads(struct icu_chain *chain)
716 #if YAZ_POSIX_THREADS
719 pthread_t t[NO_THREADS];
722 for (i = 0; i < NO_THREADS; i++)
723 pthread_create(t + i, 0, iter_thread, chain);
725 for (i = 0; i < NO_THREADS; i++)
726 pthread_join(t[i], 0);
729 static void check_icu_iter2(void)
731 UErrorCode status = U_ZERO_ERROR;
732 struct icu_chain * chain = 0;
735 const char * xml_str = "<icu locale=\"en\">"
736 "<transform rule=\"[:Control:] Any-Remove\"/>"
737 "<tokenize rule=\"l\"/>"
738 "<tokenize rule=\"w\"/>"
739 "<transform rule=\"[[:WhiteSpace:][:Punctuation:]] Remove\"/>"
741 "<casemap rule=\"l\"/>"
744 xmlDoc *doc = xmlParseMemory(xml_str, strlen(xml_str));
748 xml_node = xmlDocGetRootElement(doc);
753 chain = icu_chain_xml_config(xml_node, 0, &status);
758 YAZ_CHECK(test_iter(chain, "Adobe Acrobat Reader, 1991-1999.",
759 "[adobe][acrobat][reader][1991][][1999][]"));
761 check_iter_threads(chain);
763 icu_chain_destroy(chain);
766 #endif /* YAZ_HAVE_ICU */
768 int main(int argc, char **argv)
770 YAZ_CHECK_INIT(argc, argv);
777 check_icu_normalizer();
778 check_icu_tokenizer();
780 check_chain_empty_token();
781 check_chain_empty_chain();
787 #else /* YAZ_HAVE_ICU */
789 yaz_log(YLOG_LOG, "ICU unit tests omitted");
792 #endif /* YAZ_HAVE_ICU */
800 * c-file-style: "Stroustrup"
801 * indent-tabs-mode: nil
803 * vim: shiftwidth=4 tabstop=8 expandtab