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