ICU test refactor. Hide icu_tokenizer struct.
[yaz-moved-to-github.git] / test / tst_icu_I18N.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2010 Index Data
3  * See the file LICENSE for details.
4  */
5
6 /* DO NOT EDIT THIS FILE IF YOUR EDITOR DOES NOT SUPPORT UTF-8 */
7  
8
9 #if HAVE_CONFIG_H
10 #include "config.h"
11 #endif
12
13 #define USE_TIMING 0
14 #if USE_TIMING
15 #include <yaz/timing.h>
16 #endif
17
18 #include <yaz/test.h>
19 #include <yaz/log.h>
20 #include <yaz/wrbuf.h>
21
22 #if YAZ_HAVE_ICU
23 #include <yaz/icu_I18N.h>
24
25 #if YAZ_POSIX_THREADS
26 #include <pthread.h>
27 #endif
28
29 #include <string.h>
30 #include <stdlib.h>
31
32 #define MAX_KEY_SIZE 256
33 struct icu_termmap
34 {
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 */
37 };
38
39
40 static int icu_termmap_cmp(const void *vp1, const void *vp2)
41 {
42     struct icu_termmap *itmp1 = *(struct icu_termmap **) vp1;
43     struct icu_termmap *itmp2 = *(struct icu_termmap **) vp2;
44
45     int cmp = 0;
46     
47     cmp = strcmp((const char *)itmp1->sort_key, 
48                  (const char *)itmp2->sort_key);
49     return cmp;
50 }
51
52
53 static int test_icu_casemap(const char * locale, char action,
54                             const char * src8cstr, const char * chk8cstr)
55 {
56     int success = 0;
57     UErrorCode status = U_ZERO_ERROR;
58
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);
63
64
65     int src8cstr_len = strlen(src8cstr);
66     int chk8cstr_len = strlen(chk8cstr);
67
68     /* converting to UTF16 */
69     icu_utf16_from_utf8_cstr(src16, src8cstr, &status);
70
71     /* perform case mapping */
72     icu_utf16_casemap(dest16, src16, locale, action, &status);
73   
74     /* converting to UTF8 */
75     icu_utf16_to_utf8(dest8, dest16, &status);
76
77     /* determine success */
78     if (dest8->utf8 
79         && (dest8->utf8_len == strlen(chk8cstr))
80         && !strcmp(chk8cstr, (const char *) dest8->utf8))
81         success = 1;
82     else
83         success = 0;
84
85     /* report failures */
86     if (!success)
87     {
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);
95     }
96   
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);
102     
103     return success;
104 }
105
106 static void check_icu_casemap(void)
107 {
108     /* Locale 'en' */
109
110     /* successful tests */
111     YAZ_CHECK(test_icu_casemap("en", 'l',
112                                "A ReD fOx hunTS sQUirriLs", 
113                                "a red fox hunts squirrils"));
114     
115     YAZ_CHECK(test_icu_casemap("en", 'u',
116                                "A ReD fOx hunTS sQUirriLs", 
117                                "A RED FOX HUNTS SQUIRRILS"));
118     
119     YAZ_CHECK(test_icu_casemap("en", 'f',
120                                "A ReD fOx hunTS sQUirriLs", 
121                                "a red fox hunts squirrils"));
122     
123     YAZ_CHECK(test_icu_casemap("en", 't',
124                                "A ReD fOx hunTS sQUirriLs", 
125                                "A Red Fox Hunts Squirrils"));
126     
127     /* Locale 'da' */
128
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"));
133
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"));
137
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"));
141
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"));
145
146     /* Locale 'de' */
147
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"));
152
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"));
156
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"));
160
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"));
164
165 }
166
167 static int test_icu_sortmap(const char * locale, int src_list_len,
168                             const char ** src_list, const char ** chk_list)
169 {
170     int success = 1;
171
172     UErrorCode status = U_ZERO_ERROR;
173
174     struct icu_buf_utf8 * buf8 = icu_buf_utf8_create(0);
175     struct icu_buf_utf16 * buf16 = icu_buf_utf16_create(0);
176
177     int i;
178
179     struct icu_termmap * list[src_list_len];
180
181     UCollator *coll = ucol_open(locale, &status); 
182     icu_check_status(status);
183
184     if (U_FAILURE(status))
185         return 0;
186
187     /* assigning display terms and sort keys using buf 8 and buf16 */
188     for (i = 0; i < src_list_len; i++) 
189     {
190
191         list[i] = (struct icu_termmap *) malloc(sizeof(struct icu_termmap));
192
193         /* copy display term */
194         strcpy(list[i]->disp_term, src_list[i]);    
195
196         /* transforming to UTF16 */
197         icu_utf16_from_utf8_cstr(buf16, list[i]->disp_term, &status);
198         icu_check_status(status);
199
200         /* computing sortkeys */
201         icu_sortkey8_from_utf16(coll, buf8, buf16, &status);
202         icu_check_status(status);
203     
204         /* assigning sortkeys */
205         memcpy(list[i]->sort_key, buf8->utf8, buf8->utf8_len);    
206     } 
207
208     /* do the sorting */
209     qsort(list, src_list_len, sizeof(struct icu_termmap *), icu_termmap_cmp);
210
211     /* checking correct sorting */
212     for (i = 0; i < src_list_len; i++)
213     {
214         if (0 != strcmp(list[i]->disp_term, chk_list[i])){
215             success = 0;
216         }
217     }
218
219     if (!success)
220     {
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); 
225         }
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); 
229         }
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]); 
233         }
234     }
235   
236     for (i = 0; i < src_list_len; i++)
237         free(list[i]);
238    
239     ucol_close(coll);
240
241     icu_buf_utf8_destroy(buf8);
242     icu_buf_utf16_destroy(buf16);
243
244     return success;  
245 }
246
247 static void check_icu_sortmap(void)
248 {
249     /* successful tests */
250     size_t en_1_len = 6;
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));
258     
259     /* successful tests */
260     {
261         size_t da_1_len = 6;
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));
266     }
267     /* successful tests */
268     {
269         size_t de_1_len = 9;
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));
275     }
276 }
277
278 static int test_icu_normalizer(const char * rules8cstr,
279                                const char * src8cstr,
280                                const char * chk8cstr)
281 {
282     int success = 0;
283     
284     UErrorCode status = U_ZERO_ERROR;
285
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);
292     
293     icu_utf16_from_utf8_cstr(src16, src8cstr, &status);
294     icu_check_status(status);
295
296     icu_transform_trans(transform, dest16, src16, &status);
297     icu_check_status(status);
298
299     icu_utf16_to_utf8(dest8, dest16, &status);
300     icu_check_status(status);
301
302
303     if (!strcmp((const char *) dest8->utf8, 
304                (const char *) chk8cstr))
305         success = 1;
306     else
307     {
308         success = 0;
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);
314     }
315
316     icu_transform_destroy(transform);
317     icu_buf_utf16_destroy(src16);
318     icu_buf_utf16_destroy(dest16);
319     icu_buf_utf8_destroy(dest8);
320
321     return success;
322 }
323
324 static void check_icu_normalizer(void)
325 {
326     YAZ_CHECK(test_icu_normalizer("[:Punctuation:] Any-Remove",
327                                   "Don't shoot!",
328                                   "Dont shoot"));
329     
330     YAZ_CHECK(test_icu_normalizer("[:Control:] Any-Remove",
331                                   "Don't\n shoot!",
332                                   "Don't shoot!"));
333
334     YAZ_CHECK(test_icu_normalizer("[:Decimal_Number:] Any-Remove",
335                                   "This is 4 you!",
336                                   "This is  you!"));
337
338     YAZ_CHECK(test_icu_normalizer("Lower; [:^Letter:] Remove",
339                                   "Don't shoot!",
340                                   "dontshoot"));
341     
342     YAZ_CHECK(test_icu_normalizer("[:^Number:] Remove",
343                                   "Monday 15th of April",
344                                   "15"));
345
346     YAZ_CHECK(test_icu_normalizer("Lower;"
347                                   "[[:WhiteSpace:][:Punctuation:]] Remove",
348                                   " word4you? ",
349                                   "word4you"));
350
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"));
354 }
355
356 static int test_icu_tokenizer(const char * locale, char action,
357                               const char * src8cstr, int count)
358 {
359     int success = 1;
360
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;
366
367     /* transforming to UTF16 */
368     icu_utf16_from_utf8_cstr(src16, src8cstr, &status);
369     icu_check_status(status);
370
371     /* set up tokenizer */
372     tokenizer = icu_tokenizer_create(locale, action, &status);
373     icu_check_status(status);
374     YAZ_CHECK(tokenizer);
375
376     /* attach text buffer to tokenizer */
377     icu_tokenizer_attach(tokenizer, src16, &status);    
378     icu_check_status(status);
379
380     /* perform work on tokens */
381     while (icu_tokenizer_next_token(tokenizer, tkn16, &status))
382     {
383         icu_check_status(status);
384
385         /* converting to UTF8 */
386         icu_utf16_to_utf8(tkn8, tkn16, &status);
387     }
388
389     if (count != icu_tokenizer_token_count(tokenizer))
390     {
391         success = 0;
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);
396     }
397
398     icu_tokenizer_destroy(tokenizer);
399     icu_buf_utf16_destroy(src16);
400     icu_buf_utf16_destroy(tkn16);
401     icu_buf_utf8_destroy(tkn8);
402         
403     return success;
404 }
405
406 static void check_icu_tokenizer(void)
407 {
408     const char * en_str 
409         = "O Romeo, Romeo! wherefore art thou Romeo?";
410     
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));
415
416     {
417         const char * da_str 
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.";
420         
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));
425     }
426 }
427
428 static void check_icu_chain(void)
429 {
430     const char * en_str 
431         = "O Romeo, Romeo! wherefore art thou\t Romeo?";
432
433     UErrorCode status = U_ZERO_ERROR;
434     struct icu_chain * chain = 0;
435     
436     const char * xml_str = "<icu locale=\"en\">"
437         "<transform rule=\"[:Control:] Any-Remove\"/>"
438         "<tokenize rule=\"l\"/>"
439         "<transform rule=\"[[:WhiteSpace:][:Punctuation:]] Remove\"/>"
440         "<display/>"
441         "<casemap rule=\"l\"/>"
442         "</icu>";
443
444     
445     xmlDoc *doc = xmlParseMemory(xml_str, strlen(xml_str));
446     xmlNode *xml_node = xmlDocGetRootElement(doc);
447     YAZ_CHECK(xml_node);
448
449     chain = icu_chain_xml_config(xml_node, 0, &status);
450
451     xmlFreeDoc(doc);
452     YAZ_CHECK(chain);
453
454     YAZ_CHECK(icu_chain_assign_cstr(chain, en_str, &status));
455
456     while (icu_chain_next_token(chain, &status))
457     {
458         ;
459         /* printf("%d '%s' '%s'\n",
460            icu_chain_token_number(chain),
461            icu_chain_token_norm(chain),
462            icu_chain_token_display(chain)); */
463     }
464
465     YAZ_CHECK_EQ(icu_chain_token_number(chain), 7);
466
467
468     YAZ_CHECK(icu_chain_assign_cstr(chain, "what is this?", &status));
469
470     while (icu_chain_next_token(chain, &status))
471     {
472         ;
473         /* printf("%d '%s' '%s'\n",
474            icu_chain_token_number(chain),
475            icu_chain_token_norm(chain),
476            icu_chain_token_display(chain)); */
477     }
478
479
480     YAZ_CHECK_EQ(icu_chain_token_number(chain), 3);
481
482     icu_chain_destroy(chain);
483 }
484
485
486 static void check_bug_1140(void)
487 {
488     UErrorCode status = U_ZERO_ERROR;
489     struct icu_chain * chain = 0;
490     
491     const char * xml_str = "<icu locale=\"en\">"
492
493         /* if the first rule is normalize instead. Then it works */
494 #if 0
495         "<transform rule=\"[:Control:] Any-Remove\"/>"
496 #endif
497         "<tokenize rule=\"l\"/>"
498         "<transform rule=\"[[:WhiteSpace:][:Punctuation:]] Remove\"/>"
499         "<display/>"
500         "<casemap rule=\"l\"/>"
501         "</icu>";
502
503     
504     xmlDoc *doc = xmlParseMemory(xml_str, strlen(xml_str));
505     xmlNode *xml_node = xmlDocGetRootElement(doc);
506     YAZ_CHECK(xml_node);
507
508     chain = icu_chain_xml_config(xml_node, 0, &status);
509
510     xmlFreeDoc(doc);
511     YAZ_CHECK(chain);
512     
513     YAZ_CHECK(icu_chain_assign_cstr(
514                   chain,  "O Romeo, Romeo! wherefore art thou\t Romeo?",
515                   &status));
516
517     while (icu_chain_next_token(chain, &status))
518     {
519         ;
520         /* printf("%d '%s' '%s'\n",
521            icu_chain_token_number(chain),
522            icu_chain_token_norm(chain),
523            icu_chain_token_display(chain)); */
524     }
525     
526
527     YAZ_CHECK_EQ(icu_chain_token_number(chain), 7);
528
529     YAZ_CHECK(icu_chain_assign_cstr(chain, "what is this?", &status));
530
531     while (icu_chain_next_token(chain, &status))
532     {
533         ;
534         /* printf("%d '%s' '%s'\n",
535            icu_chain_token_number(chain),
536            icu_chain_token_norm(chain),
537            icu_chain_token_display(chain)); */
538     }
539
540     /* we expect 'what' 'is' 'this', i.e. 3 tokens */
541     YAZ_CHECK_EQ(icu_chain_token_number(chain), 3);
542
543     icu_chain_destroy(chain);
544 }
545
546
547 static void check_chain_empty_token(void)
548 {
549     UErrorCode status = U_ZERO_ERROR;
550     struct icu_chain * chain = 0;
551
552     const char * xml_str = "<icu locale=\"en\">"
553         "<tokenize rule=\"w\"/>"
554         "<transform rule=\"[[:WhiteSpace:][:Punctuation:]] Remove\"/>"
555         "</icu>";
556     
557     xmlDoc *doc = xmlParseMemory(xml_str, strlen(xml_str));
558     xmlNode *xml_node = xmlDocGetRootElement(doc);
559     YAZ_CHECK(xml_node);
560
561     chain = icu_chain_xml_config(xml_node, 0, &status);
562
563     xmlFreeDoc(doc);
564     YAZ_CHECK(chain);
565     
566     YAZ_CHECK(icu_chain_assign_cstr(
567                   chain,  "a string with 15 tokenss and 8 displays",
568                   &status));
569
570     while (icu_chain_next_token(chain, &status))
571     {
572         ;
573         /* printf("%d '%s' '%s'\n",
574            icu_chain_token_number(chain),
575            icu_chain_token_norm(chain),
576            icu_chain_token_display(chain)); */
577     }
578
579     YAZ_CHECK_EQ(icu_chain_token_number(chain), 15);
580
581     icu_chain_destroy(chain);
582 }
583
584 static void check_chain_empty_chain(void)
585 {
586     UErrorCode status = U_ZERO_ERROR;
587     struct icu_chain * chain = 0;
588
589     const char * xml_str = "<icu locale=\"en\">"
590         "</icu>";
591     
592     const char * src8 = "some 5487 weired !¤%&(/& sTuFf";
593     char * dest8 = 0;
594
595     xmlDoc *doc = xmlParseMemory(xml_str, strlen(xml_str));
596     xmlNode *xml_node = xmlDocGetRootElement(doc);
597     YAZ_CHECK(xml_node);
598
599     chain = icu_chain_xml_config(xml_node, 0, &status);
600
601     xmlFreeDoc(doc);
602     YAZ_CHECK(chain);
603     
604     YAZ_CHECK(icu_chain_assign_cstr(
605                   chain,  src8,
606                   &status));
607
608     while (icu_chain_next_token(chain, &status))
609     {
610         ;
611         /* printf("%d '%s' '%s'\n",
612            icu_chain_token_number(chain),
613            icu_chain_token_norm(chain),
614            icu_chain_token_display(chain)); */
615     }
616
617     YAZ_CHECK_EQ(icu_chain_token_number(chain), 1);
618
619     dest8 = (char *) icu_chain_token_norm(chain);
620     YAZ_CHECK_EQ(strcmp(src8, dest8), 0);
621     
622     icu_chain_destroy(chain);
623 }
624
625 static void check_icu_iter1(void)
626 {
627     UErrorCode status = U_ZERO_ERROR;
628     struct icu_chain * chain = 0;
629     xmlNode *xml_node;
630     struct icu_iter *iter;
631     struct icu_buf_utf8 *token;
632
633     const char * xml_str = "<icu locale=\"en\">"
634         "<tokenize rule=\"w\"/>"
635         "<transform rule=\"[[:WhiteSpace:][:Punctuation:]] Remove\"/>"
636         "</icu>";
637     
638     xmlDoc *doc = xmlParseMemory(xml_str, strlen(xml_str));
639     YAZ_CHECK(doc);
640     if (!doc)
641         return;
642     xml_node = xmlDocGetRootElement(doc);
643     YAZ_CHECK(xml_node);
644     if (!xml_node)
645         return ;
646
647     chain = icu_chain_xml_config(xml_node, 0, &status);
648
649     xmlFreeDoc(doc);
650     YAZ_CHECK(chain);
651     
652     iter = icu_iter_create(chain, "a string with 15 tokens and 8 displays");
653     YAZ_CHECK(iter);
654     if (!iter)
655         return;
656     token = icu_buf_utf8_create(0);
657     while (icu_iter_next(iter, token))
658     {
659         yaz_log(YLOG_LOG, "[%.*s]", (int) token->utf8_len, token->utf8);
660     }
661     icu_buf_utf8_destroy(token);
662
663     icu_iter_destroy(iter);
664     icu_chain_destroy(chain);
665 }
666
667 static int test_iter(struct icu_chain *chain, const char *input,
668                      const char *expected)
669 {
670     struct icu_iter *iter = icu_iter_create(chain, input);
671     WRBUF result;
672     struct icu_buf_utf8 *token;
673
674     if (!iter)
675     {
676         yaz_log(YLOG_WARN, "test_iter: input=%s !iter", input);
677         return 0;
678     }
679
680     token = icu_buf_utf8_create(0);
681     result = wrbuf_alloc();
682     while (icu_iter_next(iter, token))
683     {
684         wrbuf_puts(result, "[");
685         wrbuf_write(result, (const char *) token->utf8, (int) token->utf8_len);
686         wrbuf_puts(result, "]");
687     }
688     icu_buf_utf8_destroy(token);
689
690     icu_iter_destroy(iter);
691
692     if (strcmp(expected, wrbuf_cstr(result)))
693     {
694         yaz_log(YLOG_WARN, "test_iter: input=%s expected=%s got=%s",
695                 input, expected, wrbuf_cstr(result));
696         wrbuf_destroy(result);
697         return 0;
698     }
699     wrbuf_destroy(result);
700     return 1;
701 }
702
703 static void *iter_thread(void *p)
704 {
705     struct icu_chain *chain = (struct icu_chain *) p;
706     int i;
707     
708     for (i = 0; i < 10000; i++)
709     {
710         YAZ_CHECK(test_iter(chain, "Adobe Acrobat Reader, 1991-1999.",
711                             "[adobe][acrobat][reader][1991][][1999][]"));
712     }
713     return 0;
714 }
715
716 static void check_iter_threads(struct icu_chain *chain)
717 {
718 #if YAZ_POSIX_THREADS
719 #define NO_THREADS 1
720
721     pthread_t t[NO_THREADS];
722     int i;
723
724     for (i = 0; i < NO_THREADS; i++)
725         pthread_create(t + i, 0, iter_thread, chain);
726
727     for (i = 0; i < NO_THREADS; i++)
728         pthread_join(t[i], 0);
729 #endif
730 }
731 static void check_icu_iter2(void)
732 {
733     UErrorCode status = U_ZERO_ERROR;
734     struct icu_chain * chain = 0;
735     xmlNode *xml_node;
736
737     const char * xml_str = "<icu locale=\"en\">"
738         "<transform rule=\"[:Control:] Any-Remove\"/>"
739         "<tokenize rule=\"l\"/>"
740         "<tokenize rule=\"w\"/>"
741         "<transform rule=\"[[:WhiteSpace:][:Punctuation:]] Remove\"/>"
742         "<display/>"
743         "<casemap rule=\"l\"/>"
744         "</icu>";
745
746     xmlDoc *doc = xmlParseMemory(xml_str, strlen(xml_str));
747     YAZ_CHECK(doc);
748     if (!doc)
749         return;
750     xml_node = xmlDocGetRootElement(doc);
751     YAZ_CHECK(xml_node);
752     if (!xml_node)
753         return ;
754
755     chain = icu_chain_xml_config(xml_node, 0, &status);
756
757     xmlFreeDoc(doc);
758     YAZ_CHECK(chain);
759     
760     YAZ_CHECK(test_iter(chain, "Adobe Acrobat Reader, 1991-1999.",
761                         "[adobe][acrobat][reader][1991][][1999][]"));
762
763     check_iter_threads(chain);
764
765     icu_chain_destroy(chain);
766 }
767
768 #endif /* YAZ_HAVE_ICU */
769
770 int main(int argc, char **argv)
771 {
772     YAZ_CHECK_INIT(argc, argv); 
773     YAZ_CHECK_LOG();
774
775 #if YAZ_HAVE_ICU
776
777     check_icu_casemap();
778     check_icu_sortmap();
779     check_icu_normalizer();
780     check_icu_tokenizer();
781     check_icu_chain();
782     check_chain_empty_token();
783     check_chain_empty_chain();
784     check_icu_iter1();
785     check_icu_iter2();
786     
787     check_bug_1140();
788
789 #else /* YAZ_HAVE_ICU */
790
791     yaz_log(YLOG_LOG, "ICU unit tests omitted");
792     YAZ_CHECK(0 == 0);
793
794 #endif /* YAZ_HAVE_ICU */
795    
796     YAZ_CHECK_TERM;
797 }
798
799 /*
800  * Local variables:
801  * c-basic-offset: 4
802  * c-file-style: "Stroustrup"
803  * indent-tabs-mode: nil
804  * End:
805  * vim: shiftwidth=4 tabstop=8 expandtab
806  */
807