Working on cloning / thread safety for ICU stuff
[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         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));
462     }
463
464     YAZ_CHECK_EQ(icu_chain_token_number(chain), 7);
465
466
467     YAZ_CHECK(icu_chain_assign_cstr(chain, "what is this?", &status));
468
469     while (icu_chain_next_token(chain, &status))
470     {
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));
475     }
476
477
478     YAZ_CHECK_EQ(icu_chain_token_number(chain), 3);
479
480     icu_chain_destroy(chain);
481 }
482
483
484 static void check_bug_1140(void)
485 {
486     UErrorCode status = U_ZERO_ERROR;
487     struct icu_chain * chain = 0;
488     
489     const char * xml_str = "<icu locale=\"en\">"
490
491         /* if the first rule is normalize instead. Then it works */
492 #if 0
493         "<transform rule=\"[:Control:] Any-Remove\"/>"
494 #endif
495         "<tokenize rule=\"l\"/>"
496         "<transform rule=\"[[:WhiteSpace:][:Punctuation:]] Remove\"/>"
497         "<display/>"
498         "<casemap rule=\"l\"/>"
499         "</icu>";
500
501     
502     xmlDoc *doc = xmlParseMemory(xml_str, strlen(xml_str));
503     xmlNode *xml_node = xmlDocGetRootElement(doc);
504     YAZ_CHECK(xml_node);
505
506     chain = icu_chain_xml_config(xml_node, 0, &status);
507
508     xmlFreeDoc(doc);
509     YAZ_CHECK(chain);
510     
511     YAZ_CHECK(icu_chain_assign_cstr(
512                   chain,  "O Romeo, Romeo! wherefore art thou\t Romeo?",
513                   &status));
514
515     while (icu_chain_next_token(chain, &status))
516     {
517         ;
518         /* printf("%d '%s' '%s'\n",
519            icu_chain_token_number(chain),
520            icu_chain_token_norm(chain),
521            icu_chain_token_display(chain)); */
522     }
523     
524
525     YAZ_CHECK_EQ(icu_chain_token_number(chain), 7);
526
527     YAZ_CHECK(icu_chain_assign_cstr(chain, "what is this?", &status));
528
529     while (icu_chain_next_token(chain, &status))
530     {
531         ;
532         /* printf("%d '%s' '%s'\n",
533            icu_chain_token_number(chain),
534            icu_chain_token_norm(chain),
535            icu_chain_token_display(chain)); */
536     }
537
538     /* we expect 'what' 'is' 'this', i.e. 3 tokens */
539     YAZ_CHECK_EQ(icu_chain_token_number(chain), 3);
540
541     icu_chain_destroy(chain);
542 }
543
544
545 static void check_chain_empty_token(void)
546 {
547     UErrorCode status = U_ZERO_ERROR;
548     struct icu_chain * chain = 0;
549
550     const char * xml_str = "<icu locale=\"en\">"
551         "<tokenize rule=\"w\"/>"
552         "<transform rule=\"[[:WhiteSpace:][:Punctuation:]] Remove\"/>"
553         "</icu>";
554     
555     xmlDoc *doc = xmlParseMemory(xml_str, strlen(xml_str));
556     xmlNode *xml_node = xmlDocGetRootElement(doc);
557     YAZ_CHECK(xml_node);
558
559     chain = icu_chain_xml_config(xml_node, 0, &status);
560
561     xmlFreeDoc(doc);
562     YAZ_CHECK(chain);
563     
564     YAZ_CHECK(icu_chain_assign_cstr(
565                   chain,  "a string with 15 tokenss and 8 displays",
566                   &status));
567
568     while (icu_chain_next_token(chain, &status))
569     {
570         ;
571         /* printf("%d '%s' '%s'\n",
572            icu_chain_token_number(chain),
573            icu_chain_token_norm(chain),
574            icu_chain_token_display(chain)); */
575     }
576
577     YAZ_CHECK_EQ(icu_chain_token_number(chain), 15);
578
579     icu_chain_destroy(chain);
580 }
581
582 static void check_chain_empty_chain(void)
583 {
584     UErrorCode status = U_ZERO_ERROR;
585     struct icu_chain * chain = 0;
586
587     const char * xml_str = "<icu locale=\"en\">"
588         "</icu>";
589     
590     const char * src8 = "some 5487 weired !¤%&(/& sTuFf";
591     char * dest8 = 0;
592
593     xmlDoc *doc = xmlParseMemory(xml_str, strlen(xml_str));
594     xmlNode *xml_node = xmlDocGetRootElement(doc);
595     YAZ_CHECK(xml_node);
596
597     chain = icu_chain_xml_config(xml_node, 0, &status);
598
599     xmlFreeDoc(doc);
600     YAZ_CHECK(chain);
601     
602     YAZ_CHECK(icu_chain_assign_cstr(
603                   chain,  src8,
604                   &status));
605
606     while (icu_chain_next_token(chain, &status))
607     {
608         ;
609         /* printf("%d '%s' '%s'\n",
610            icu_chain_token_number(chain),
611            icu_chain_token_norm(chain),
612            icu_chain_token_display(chain)); */
613     }
614
615     YAZ_CHECK_EQ(icu_chain_token_number(chain), 1);
616
617     dest8 = (char *) icu_chain_token_norm(chain);
618     YAZ_CHECK_EQ(strcmp(src8, dest8), 0);
619     
620     icu_chain_destroy(chain);
621 }
622
623 static void check_icu_iter1(void)
624 {
625     UErrorCode status = U_ZERO_ERROR;
626     struct icu_chain * chain = 0;
627     xmlNode *xml_node;
628     struct icu_iter *iter;
629     struct icu_buf_utf8 *token;
630
631     const char * xml_str = "<icu locale=\"en\">"
632         "<tokenize rule=\"w\"/>"
633         "<transform rule=\"[[:WhiteSpace:][:Punctuation:]] Remove\"/>"
634         "</icu>";
635     
636     xmlDoc *doc = xmlParseMemory(xml_str, strlen(xml_str));
637     YAZ_CHECK(doc);
638     if (!doc)
639         return;
640     xml_node = xmlDocGetRootElement(doc);
641     YAZ_CHECK(xml_node);
642     if (!xml_node)
643         return ;
644
645     chain = icu_chain_xml_config(xml_node, 0, &status);
646
647     xmlFreeDoc(doc);
648     YAZ_CHECK(chain);
649     
650     iter = icu_iter_create(chain, "a string with 15 tokens and 8 displays");
651     YAZ_CHECK(iter);
652     if (!iter)
653         return;
654     token = icu_buf_utf8_create(0);
655     while (icu_iter_next(iter, token))
656     {
657         yaz_log(YLOG_LOG, "[%.*s]", (int) token->utf8_len, token->utf8);
658     }
659     icu_buf_utf8_destroy(token);
660
661     icu_iter_destroy(iter);
662     icu_chain_destroy(chain);
663 }
664
665 static int test_iter(struct icu_chain *chain, const char *input,
666                      const char *expected)
667 {
668     struct icu_iter *iter = icu_iter_create(chain, input);
669     WRBUF result;
670     struct icu_buf_utf8 *token;
671
672     if (!iter)
673     {
674         yaz_log(YLOG_WARN, "test_iter: input=%s !iter", input);
675         return 0;
676     }
677
678     token = icu_buf_utf8_create(0);
679     result = wrbuf_alloc();
680     while (icu_iter_next(iter, token))
681     {
682         wrbuf_puts(result, "[");
683         wrbuf_write(result, (const char *) token->utf8, (int) token->utf8_len);
684         wrbuf_puts(result, "]");
685     }
686     icu_buf_utf8_destroy(token);
687
688     icu_iter_destroy(iter);
689
690     if (strcmp(expected, wrbuf_cstr(result)))
691     {
692         yaz_log(YLOG_WARN, "test_iter: input=%s expected=%s got=%s",
693                 input, expected, wrbuf_cstr(result));
694         wrbuf_destroy(result);
695         return 0;
696     }
697     wrbuf_destroy(result);
698     return 1;
699 }
700
701 static void *iter_thread(void *p)
702 {
703     struct icu_chain *chain = (struct icu_chain *) p;
704     int i;
705     
706     for (i = 0; i < 10000; i++)
707     {
708         YAZ_CHECK(test_iter(chain, "Adobe Acrobat Reader, 1991-1999.",
709                             "[adobe][acrobat][reader][1991][][1999][]"));
710     }
711     return 0;
712 }
713
714 static void check_iter_threads(struct icu_chain *chain)
715 {
716 #if YAZ_POSIX_THREADS
717 #define NO_THREADS 1
718
719     pthread_t t[NO_THREADS];
720     int i;
721
722     for (i = 0; i < NO_THREADS; i++)
723         pthread_create(t + i, 0, iter_thread, chain);
724
725     for (i = 0; i < NO_THREADS; i++)
726         pthread_join(t[i], 0);
727 #endif
728 }
729 static void check_icu_iter2(void)
730 {
731     UErrorCode status = U_ZERO_ERROR;
732     struct icu_chain * chain = 0;
733     xmlNode *xml_node;
734
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\"/>"
740         "<display/>"
741         "<casemap rule=\"l\"/>"
742         "</icu>";
743
744     xmlDoc *doc = xmlParseMemory(xml_str, strlen(xml_str));
745     YAZ_CHECK(doc);
746     if (!doc)
747         return;
748     xml_node = xmlDocGetRootElement(doc);
749     YAZ_CHECK(xml_node);
750     if (!xml_node)
751         return ;
752
753     chain = icu_chain_xml_config(xml_node, 0, &status);
754
755     xmlFreeDoc(doc);
756     YAZ_CHECK(chain);
757     
758     YAZ_CHECK(test_iter(chain, "Adobe Acrobat Reader, 1991-1999.",
759                         "[adobe][acrobat][reader][1991][][1999][]"));
760
761     check_iter_threads(chain);
762
763     icu_chain_destroy(chain);
764 }
765
766 #endif /* YAZ_HAVE_ICU */
767
768 int main(int argc, char **argv)
769 {
770     YAZ_CHECK_INIT(argc, argv); 
771     YAZ_CHECK_LOG();
772
773 #if YAZ_HAVE_ICU
774
775     check_icu_casemap();
776     check_icu_sortmap();
777     check_icu_normalizer();
778     check_icu_tokenizer();
779     check_icu_chain();
780     check_chain_empty_token();
781     check_chain_empty_chain();
782     check_icu_iter1();
783     check_icu_iter2();
784     
785     check_bug_1140();
786
787 #else /* YAZ_HAVE_ICU */
788
789     yaz_log(YLOG_LOG, "ICU unit tests omitted");
790     YAZ_CHECK(0 == 0);
791
792 #endif /* YAZ_HAVE_ICU */
793    
794     YAZ_CHECK_TERM;
795 }
796
797 /*
798  * Local variables:
799  * c-basic-offset: 4
800  * c-file-style: "Stroustrup"
801  * indent-tabs-mode: nil
802  * End:
803  * vim: shiftwidth=4 tabstop=8 expandtab
804  */
805