Use yaz_log rather than printf for ICU test
[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
21 #if YAZ_HAVE_ICU
22 #include <yaz/icu_I18N.h>
23
24 #include <string.h>
25 #include <stdlib.h>
26
27 #define MAX_KEY_SIZE 256
28 struct icu_termmap
29 {
30     uint8_t sort_key[MAX_KEY_SIZE]; /* standard C string '\0' terminated */
31     char disp_term[MAX_KEY_SIZE];  /* standard C utf-8 string */
32 };
33
34
35 int icu_termmap_cmp(const void *vp1, const void *vp2)
36 {
37     struct icu_termmap *itmp1 = *(struct icu_termmap **) vp1;
38     struct icu_termmap *itmp2 = *(struct icu_termmap **) vp2;
39
40     int cmp = 0;
41     
42     cmp = strcmp((const char *)itmp1->sort_key, 
43                  (const char *)itmp2->sort_key);
44     return cmp;
45 }
46
47
48 int test_icu_casemap(const char * locale, char action,
49                      const char * src8cstr, const char * chk8cstr)
50 {
51     int success = 0;
52     UErrorCode status = U_ZERO_ERROR;
53
54     struct icu_buf_utf8 * src8 = icu_buf_utf8_create(0);
55     struct icu_buf_utf8 * dest8 = icu_buf_utf8_create(0);
56     struct icu_buf_utf16 * src16 = icu_buf_utf16_create(0);
57     struct icu_buf_utf16 * dest16 = icu_buf_utf16_create(0);
58
59
60     int src8cstr_len = strlen(src8cstr);
61     int chk8cstr_len = strlen(chk8cstr);
62
63     /* converting to UTF16 */
64     icu_utf16_from_utf8_cstr(src16, src8cstr, &status);
65
66     /* perform case mapping */
67     icu_utf16_casemap(dest16, src16, locale, action, &status);
68   
69     /* converting to UTF8 */
70     icu_utf16_to_utf8(dest8, dest16, &status);
71
72     /* determine success */
73     if (dest8->utf8 
74         && (dest8->utf8_len == strlen(chk8cstr))
75         && !strcmp(chk8cstr, (const char *) dest8->utf8))
76         success = 1;
77     else
78         success = 0;
79
80     /* report failures */
81     if (!success)
82     {
83         yaz_log(YLOG_WARN, "test_icu_casemap failed");
84         yaz_log(YLOG_LOG, "Original string:   '%s' (%d)",
85                 src8cstr, src8cstr_len);
86         yaz_log(YLOG_LOG, "icu_casemap '%s:%c' '%s' (%d)", 
87                 locale, action, dest8->utf8, dest8->utf8_len);
88         yaz_log(YLOG_LOG, "expected string:   '%s' (%d)",
89                 chk8cstr, chk8cstr_len);
90     }
91   
92     /* clean the buffers */
93     icu_buf_utf8_destroy(src8);
94     icu_buf_utf8_destroy(dest8);
95     icu_buf_utf16_destroy(src16);
96     icu_buf_utf16_destroy(dest16);
97     
98     return success;
99 }
100
101 void test_icu_I18N_casemap(int argc, char **argv)
102 {
103     /* Locale 'en' */
104
105     /* successful tests */
106     YAZ_CHECK(test_icu_casemap("en", 'l',
107                                "A ReD fOx hunTS sQUirriLs", 
108                                "a red fox hunts squirrils"));
109     
110     YAZ_CHECK(test_icu_casemap("en", 'u',
111                                "A ReD fOx hunTS sQUirriLs", 
112                                "A RED FOX HUNTS SQUIRRILS"));
113     
114     YAZ_CHECK(test_icu_casemap("en", 'f',
115                                "A ReD fOx hunTS sQUirriLs", 
116                                "a red fox hunts squirrils"));
117     
118     YAZ_CHECK(test_icu_casemap("en", 't',
119                                "A ReD fOx hunTS sQUirriLs", 
120                                "A Red Fox Hunts Squirrils"));
121     
122     /* Locale 'da' */
123
124     /* success expected */
125     YAZ_CHECK(test_icu_casemap("da", 'l',
126                                "åh ÆbLE, øs fLØde i Åen efter bLåBærGRødeN", 
127                                "åh æble, øs fløde i åen efter blåbærgrøden"));
128
129     YAZ_CHECK(test_icu_casemap("da", 'u',
130                                "åh ÆbLE, øs fLØde i Åen efter bLåBærGRødeN", 
131                                "ÅH ÆBLE, ØS FLØDE I ÅEN EFTER BLÅBÆRGRØDEN"));
132
133     YAZ_CHECK(test_icu_casemap("da", 'f',
134                                "åh ÆbLE, øs fLØde i Åen efter bLåBærGRødeN", 
135                                "åh æble, øs fløde i åen efter blåbærgrøden"));
136
137     YAZ_CHECK(test_icu_casemap("da", 't',
138                                "åh ÆbLE, øs fLØde i Åen efter bLåBærGRødeN", 
139                                "Åh Æble, Øs Fløde I Åen Efter Blåbærgrøden"));
140
141     /* Locale 'de' */
142
143     /* success expected */
144     YAZ_CHECK(test_icu_casemap("de", 'l',
145                                "zWÖlf ärgerliche Würste rollen ÜBer die StRAße",
146                                "zwölf ärgerliche würste rollen über die straße"));
147
148     YAZ_CHECK(test_icu_casemap("de", 'u',
149                                "zWÖlf ärgerliche Würste rollen ÜBer die StRAße",
150                                "ZWÖLF ÄRGERLICHE WÜRSTE ROLLEN ÜBER DIE STRASSE"));
151
152     YAZ_CHECK(test_icu_casemap("de", 'f',
153                                "zWÖlf ärgerliche Würste rollen ÜBer die StRAße",
154                                "zwölf ärgerliche würste rollen über die strasse"));
155
156     YAZ_CHECK(test_icu_casemap("de", 't',
157                                "zWÖlf ärgerliche Würste rollen ÜBer die StRAße",
158                                "Zwölf Ärgerliche Würste Rollen Über Die Straße"));
159
160 }
161
162 int test_icu_sortmap(const char * locale, int src_list_len,
163                      const char ** src_list, const char ** chk_list)
164 {
165     int success = 1;
166
167     UErrorCode status = U_ZERO_ERROR;
168
169     struct icu_buf_utf8 * buf8 = icu_buf_utf8_create(0);
170     struct icu_buf_utf16 * buf16 = icu_buf_utf16_create(0);
171
172     int i;
173
174     struct icu_termmap * list[src_list_len];
175
176     UCollator *coll = ucol_open(locale, &status); 
177     icu_check_status(status);
178
179     if(U_FAILURE(status))
180         return 0;
181
182     /* assigning display terms and sort keys using buf 8 and buf16 */
183     for (i = 0; i < src_list_len; i++) 
184     {
185
186         list[i] = (struct icu_termmap *) malloc(sizeof(struct icu_termmap));
187
188         /* copy display term */
189         strcpy(list[i]->disp_term, src_list[i]);    
190
191         /* transforming to UTF16 */
192         icu_utf16_from_utf8_cstr(buf16, list[i]->disp_term, &status);
193         icu_check_status(status);
194
195         /* computing sortkeys */
196         icu_sortkey8_from_utf16(coll, buf8, buf16, &status);
197         icu_check_status(status);
198     
199         /* assigning sortkeys */
200         memcpy(list[i]->sort_key, buf8->utf8, buf8->utf8_len);    
201     } 
202
203     /* do the sorting */
204     qsort(list, src_list_len, sizeof(struct icu_termmap *), icu_termmap_cmp);
205
206     /* checking correct sorting */
207     for (i = 0; i < src_list_len; i++)
208     {
209         if (0 != strcmp(list[i]->disp_term, chk_list[i])){
210             success = 0;
211         }
212     }
213
214     if (!success)
215     {
216         yaz_log(YLOG_LOG, "ERROR"); 
217         yaz_log(YLOG_LOG, "Input str:'%s':", locale); 
218         for (i = 0; i < src_list_len; i++) {
219             yaz_log(YLOG_LOG, "  '%s'", list[i]->disp_term); 
220         }
221         yaz_log(YLOG_LOG, "ICU sort: '%s':", locale); 
222         for (i = 0; i < src_list_len; i++) {
223             yaz_log(YLOG_LOG, " '%s'", list[i]->disp_term); 
224         }
225         yaz_log(YLOG_LOG, "Expected: '%s':", locale); 
226         for (i = 0; i < src_list_len; i++) {
227             yaz_log(YLOG_LOG, " '%s'", chk_list[i]); 
228         }
229     }
230   
231     for (i = 0; i < src_list_len; i++)
232         free(list[i]);
233    
234     ucol_close(coll);
235
236     icu_buf_utf8_destroy(buf8);
237     icu_buf_utf16_destroy(buf16);
238
239     return success;  
240 }
241
242 void test_icu_I18N_sortmap(int argc, char **argv)
243 {
244     /* successful tests */
245     size_t en_1_len = 6;
246     const char * en_1_src[6] = {"z", "K", "a", "A", "Z", "k"};
247     const char * en_1_cck[6] = {"a", "A", "k", "K", "z", "Z"};
248     YAZ_CHECK(test_icu_sortmap("en", en_1_len, en_1_src, en_1_cck));
249     YAZ_CHECK(test_icu_sortmap("en_AU", en_1_len, en_1_src, en_1_cck));
250     YAZ_CHECK(test_icu_sortmap("en_CA", en_1_len, en_1_src, en_1_cck));
251     YAZ_CHECK(test_icu_sortmap("en_GB", en_1_len, en_1_src, en_1_cck));
252     YAZ_CHECK(test_icu_sortmap("en_US", en_1_len, en_1_src, en_1_cck));
253     
254     /* successful tests */
255     {
256         size_t da_1_len = 6;
257         const char * da_1_src[6] = {"z", "å", "o", "æ", "a", "ø"};
258         const char * da_1_cck[6] = {"a", "o", "z", "æ", "ø", "å"};
259         YAZ_CHECK(test_icu_sortmap("da", da_1_len, da_1_src, da_1_cck));
260         YAZ_CHECK(test_icu_sortmap("da_DK", da_1_len, da_1_src, da_1_cck));
261     }
262     /* successful tests */
263     {
264         size_t de_1_len = 9;
265         const char * de_1_src[9] = {"u", "ä", "o", "t", "s", "ß", "ü", "ö", "a"};
266         const char * de_1_cck[9] = {"a","ä", "o", "ö", "s", "ß", "t", "u", "ü"};
267         YAZ_CHECK(test_icu_sortmap("de", de_1_len, de_1_src, de_1_cck));
268         YAZ_CHECK(test_icu_sortmap("de_AT", de_1_len, de_1_src, de_1_cck));
269         YAZ_CHECK(test_icu_sortmap("de_DE", de_1_len, de_1_src, de_1_cck));
270     }
271     
272 }
273
274 int test_icu_normalizer(const char * rules8cstr,
275                         const char * src8cstr,
276                         const char * chk8cstr)
277 {
278     int success = 0;
279     
280     UErrorCode status = U_ZERO_ERROR;
281
282     struct icu_buf_utf16 * src16 = icu_buf_utf16_create(0);
283     struct icu_buf_utf16 * dest16 = icu_buf_utf16_create(0);
284     struct icu_buf_utf8 * dest8 = icu_buf_utf8_create(0);
285     struct icu_transform * transform
286         = icu_transform_create(rules8cstr, 'f', 0, &status);
287     icu_check_status(status);
288     
289     icu_utf16_from_utf8_cstr(src16, src8cstr, &status);
290     icu_check_status(status);
291
292     icu_transform_trans(transform, dest16, src16, &status);
293     icu_check_status(status);
294
295     icu_utf16_to_utf8(dest8, dest16, &status);
296     icu_check_status(status);
297
298
299     if(!strcmp((const char *) dest8->utf8, 
300                (const char *) chk8cstr))
301         success = 1;
302     else
303     {
304         success = 0;
305         yaz_log(YLOG_LOG, "Normalization");
306         yaz_log(YLOG_LOG, " Rules:      '%s'", rules8cstr);
307         yaz_log(YLOG_LOG, " Input:      '%s'", src8cstr);
308         yaz_log(YLOG_LOG, " Normalized: '%s'", dest8->utf8);
309         yaz_log(YLOG_LOG, " Expected:   '%s'", chk8cstr);
310     }
311
312     icu_transform_destroy(transform);
313     icu_buf_utf16_destroy(src16);
314     icu_buf_utf16_destroy(dest16);
315     icu_buf_utf8_destroy(dest8);
316
317     return success;
318 }
319
320 void test_icu_I18N_normalizer(int argc, char **argv)
321 {
322     YAZ_CHECK(test_icu_normalizer("[:Punctuation:] Any-Remove",
323                                   "Don't shoot!",
324                                   "Dont shoot"));
325     
326     YAZ_CHECK(test_icu_normalizer("[:Control:] Any-Remove",
327                                   "Don't\n shoot!",
328                                   "Don't shoot!"));
329
330     YAZ_CHECK(test_icu_normalizer("[:Decimal_Number:] Any-Remove",
331                                   "This is 4 you!",
332                                   "This is  you!"));
333
334     YAZ_CHECK(test_icu_normalizer("Lower; [:^Letter:] Remove",
335                                   "Don't shoot!",
336                                   "dontshoot"));
337     
338     YAZ_CHECK(test_icu_normalizer("[:^Number:] Remove",
339                                   "Monday 15th of April",
340                                   "15"));
341
342     YAZ_CHECK(test_icu_normalizer("Lower;"
343                                   "[[:WhiteSpace:][:Punctuation:]] Remove",
344                                   " word4you? ",
345                                   "word4you"));
346
347     YAZ_CHECK(test_icu_normalizer("NFD; [:Nonspacing Mark:] Remove; NFC",
348                                   "à côté de l'alcôve ovoïde",
349                                   "a cote de l'alcove ovoide"));
350 }
351
352 int test_icu_tokenizer(const char * locale, char action,
353                        const char * src8cstr, int count)
354 {
355     int success = 1;
356
357     UErrorCode status = U_ZERO_ERROR;
358     struct icu_buf_utf16 * src16 = icu_buf_utf16_create(0);
359     struct icu_buf_utf16 * tkn16 = icu_buf_utf16_create(0);
360     struct icu_buf_utf8 * tkn8 = icu_buf_utf8_create(0);
361     struct icu_tokenizer * tokenizer = 0;
362
363     /* transforming to UTF16 */
364     icu_utf16_from_utf8_cstr(src16, src8cstr, &status);
365     icu_check_status(status);
366
367     /* set up tokenizer */
368     tokenizer = icu_tokenizer_create(locale, action, &status);
369     icu_check_status(status);
370     YAZ_CHECK(tokenizer);
371
372     /* attach text buffer to tokenizer */
373     icu_tokenizer_attach(tokenizer, src16, &status);    
374     icu_check_status(status);
375     YAZ_CHECK(tokenizer->bi);
376
377     /* perform work on tokens */
378     while(icu_tokenizer_next_token(tokenizer, tkn16, &status)){
379         icu_check_status(status);
380
381         /* converting to UTF8 */
382         icu_utf16_to_utf8(tkn8, tkn16, &status);
383     }
384
385     if (count != icu_tokenizer_token_count(tokenizer)){
386         success = 0;
387         yaz_log(YLOG_LOG, "Tokenizer '%s:%c' Error:", locale, action);
388         yaz_log(YLOG_LOG, " Input:  '%s'", src8cstr);
389         yaz_log(YLOG_LOG, " Tokens: %d", icu_tokenizer_token_count(tokenizer));
390         yaz_log(YLOG_LOG, " Expected: %d", count);
391     }
392
393     icu_tokenizer_destroy(tokenizer);
394     icu_buf_utf16_destroy(src16);
395     icu_buf_utf16_destroy(tkn16);
396     icu_buf_utf8_destroy(tkn8);
397         
398     return success;
399 }
400
401 void test_icu_I18N_tokenizer(int argc, char **argv)
402 {
403     const char * en_str 
404         = "O Romeo, Romeo! wherefore art thou Romeo?";
405     
406     YAZ_CHECK(test_icu_tokenizer("en", 's', en_str, 2));
407     YAZ_CHECK(test_icu_tokenizer("en", 'l', en_str, 7));
408     YAZ_CHECK(test_icu_tokenizer("en", 'w', en_str, 16));
409     YAZ_CHECK(test_icu_tokenizer("en", 'c', en_str, 41));
410
411     {
412         const char * da_str 
413             = "Blåbærtærte. Denne kage stammer fra Finland. "
414             "Den er med blåbær, men alle sommerens forskellige bær kan bruges.";
415         
416         YAZ_CHECK(test_icu_tokenizer("da", 's', da_str, 3));
417         YAZ_CHECK(test_icu_tokenizer("dar", 'l', da_str, 17));
418         YAZ_CHECK(test_icu_tokenizer("da", 'w', da_str, 37));
419         YAZ_CHECK(test_icu_tokenizer("da", 'c', da_str, 110));
420     }
421 }
422
423 void test_icu_I18N_chain(int argc, char **argv)
424 {
425     const char * en_str 
426         = "O Romeo, Romeo! wherefore art thou\t Romeo?";
427
428     UErrorCode status = U_ZERO_ERROR;
429     struct icu_chain * chain = 0;
430     
431     const char * xml_str = "<icu locale=\"en\">"
432         "<transform rule=\"[:Control:] Any-Remove\"/>"
433         "<tokenize rule=\"l\"/>"
434         "<transform rule=\"[[:WhiteSpace:][:Punctuation:]] Remove\"/>"
435         "<display/>"
436         "<casemap rule=\"l\"/>"
437         "</icu>";
438
439     
440     xmlDoc *doc = xmlParseMemory(xml_str, strlen(xml_str));
441     xmlNode *xml_node = xmlDocGetRootElement(doc);
442     YAZ_CHECK(xml_node);
443
444     chain = icu_chain_xml_config(xml_node, 0, &status);
445
446     xmlFreeDoc(doc);
447     YAZ_CHECK(chain);
448
449     YAZ_CHECK(icu_chain_assign_cstr(chain, en_str, &status));
450
451     while (icu_chain_next_token(chain, &status))
452     {
453         ;
454         /* printf("%d '%s' '%s'\n",
455            icu_chain_token_number(chain),
456            icu_chain_token_norm(chain),
457            icu_chain_token_display(chain)); */
458     }
459
460     YAZ_CHECK_EQ(icu_chain_token_number(chain), 7);
461
462
463     YAZ_CHECK(icu_chain_assign_cstr(chain, "what is this?", &status));
464
465     while (icu_chain_next_token(chain, &status))
466     {
467         ;
468         /* printf("%d '%s' '%s'\n",
469            icu_chain_token_number(chain),
470            icu_chain_token_norm(chain),
471            icu_chain_token_display(chain)); */
472     }
473
474
475     YAZ_CHECK_EQ(icu_chain_token_number(chain), 3);
476
477     icu_chain_destroy(chain);
478 }
479
480
481 void test_bug_1140(void)
482 {
483     UErrorCode status = U_ZERO_ERROR;
484     struct icu_chain * chain = 0;
485     
486     const char * xml_str = "<icu locale=\"en\">"
487
488         /* if the first rule is normalize instead. Then it works */
489 #if 0
490         "<transform rule=\"[:Control:] Any-Remove\"/>"
491 #endif
492         "<tokenize rule=\"l\"/>"
493         "<transform rule=\"[[:WhiteSpace:][:Punctuation:]] Remove\"/>"
494         "<display/>"
495         "<casemap rule=\"l\"/>"
496         "</icu>";
497
498     
499     xmlDoc *doc = xmlParseMemory(xml_str, strlen(xml_str));
500     xmlNode *xml_node = xmlDocGetRootElement(doc);
501     YAZ_CHECK(xml_node);
502
503     chain = icu_chain_xml_config(xml_node, 0, &status);
504
505     xmlFreeDoc(doc);
506     YAZ_CHECK(chain);
507     
508     YAZ_CHECK(icu_chain_assign_cstr(
509                   chain,  "O Romeo, Romeo! wherefore art thou\t Romeo?",
510                   &status));
511
512     while (icu_chain_next_token(chain, &status))
513     {
514         ;
515         /* printf("%d '%s' '%s'\n",
516            icu_chain_token_number(chain),
517            icu_chain_token_norm(chain),
518            icu_chain_token_display(chain)); */
519     }
520     
521
522     YAZ_CHECK_EQ(icu_chain_token_number(chain), 7);
523
524     YAZ_CHECK(icu_chain_assign_cstr(chain, "what is this?", &status));
525
526     while (icu_chain_next_token(chain, &status))
527     {
528         ;
529         /* printf("%d '%s' '%s'\n",
530            icu_chain_token_number(chain),
531            icu_chain_token_norm(chain),
532            icu_chain_token_display(chain)); */
533     }
534
535     /* we expect 'what' 'is' 'this', i.e. 3 tokens */
536     YAZ_CHECK_EQ(icu_chain_token_number(chain), 3);
537
538     icu_chain_destroy(chain);
539 }
540
541
542 void test_chain_empty_token(void)
543 {
544     UErrorCode status = U_ZERO_ERROR;
545     struct icu_chain * chain = 0;
546
547     const char * xml_str = "<icu locale=\"en\">"
548         "<tokenize rule=\"w\"/>"
549         "<transform rule=\"[[:WhiteSpace:][:Punctuation:]] Remove\"/>"
550         "</icu>";
551     
552     xmlDoc *doc = xmlParseMemory(xml_str, strlen(xml_str));
553     xmlNode *xml_node = xmlDocGetRootElement(doc);
554     YAZ_CHECK(xml_node);
555
556     chain = icu_chain_xml_config(xml_node, 0, &status);
557
558     xmlFreeDoc(doc);
559     YAZ_CHECK(chain);
560     
561     YAZ_CHECK(icu_chain_assign_cstr(
562                   chain,  "a string with 15 tokenss and 8 displays",
563                   &status));
564
565     while (icu_chain_next_token(chain, &status))
566     {
567         ;
568         /* printf("%d '%s' '%s'\n",
569            icu_chain_token_number(chain),
570            icu_chain_token_norm(chain),
571            icu_chain_token_display(chain)); */
572     }
573
574     YAZ_CHECK_EQ(icu_chain_token_number(chain), 15);
575
576     icu_chain_destroy(chain);
577 }
578
579 void test_chain_empty_chain(void)
580 {
581     UErrorCode status = U_ZERO_ERROR;
582     struct icu_chain * chain = 0;
583
584     const char * xml_str = "<icu locale=\"en\">"
585         "</icu>";
586     
587     const char * src8 = "some 5487 weired !¤%&(/& sTuFf";
588     char * dest8 = 0;
589
590     xmlDoc *doc = xmlParseMemory(xml_str, strlen(xml_str));
591     xmlNode *xml_node = xmlDocGetRootElement(doc);
592     YAZ_CHECK(xml_node);
593
594     chain = icu_chain_xml_config(xml_node, 0, &status);
595
596     xmlFreeDoc(doc);
597     YAZ_CHECK(chain);
598     
599     YAZ_CHECK(icu_chain_assign_cstr(
600                   chain,  src8,
601                   &status));
602
603     while (icu_chain_next_token(chain, &status))
604     {
605         ;
606         /* printf("%d '%s' '%s'\n",
607            icu_chain_token_number(chain),
608            icu_chain_token_norm(chain),
609            icu_chain_token_display(chain)); */
610     }
611
612     YAZ_CHECK_EQ(icu_chain_token_number(chain), 1);
613
614     dest8 = (char *) icu_chain_token_norm(chain);
615     YAZ_CHECK_EQ(strcmp(src8, dest8), 0);
616     
617     icu_chain_destroy(chain);
618 }
619
620 void test_icu_iter1(void)
621 {
622     UErrorCode status = U_ZERO_ERROR;
623     struct icu_chain * chain = 0;
624     xmlNode *xml_node;
625     struct icu_iter *iter;
626     struct icu_buf_utf8 *token;
627
628     const char * xml_str = "<icu locale=\"en\">"
629         "<tokenize rule=\"w\"/>"
630         "<transform rule=\"[[:WhiteSpace:][:Punctuation:]] Remove\"/>"
631         "</icu>";
632     
633     xmlDoc *doc = xmlParseMemory(xml_str, strlen(xml_str));
634     YAZ_CHECK(doc);
635     if (!doc)
636         return;
637     xml_node = xmlDocGetRootElement(doc);
638     YAZ_CHECK(xml_node);
639     if (!xml_node)
640         return ;
641
642     chain = icu_chain_xml_config(xml_node, 0, &status);
643
644     xmlFreeDoc(doc);
645     YAZ_CHECK(chain);
646     
647     iter = icu_iter_create(chain, "a string with 15 tokens and 8 displays");
648     YAZ_CHECK(iter);
649     if (!iter)
650         return;
651     token = icu_buf_utf8_create(0);
652     while (icu_iter_next(iter, token))
653     {
654         yaz_log(YLOG_LOG, "[%.*s]", (int) token->utf8_len, token->utf8);
655     }
656     icu_buf_utf8_destroy(token);
657
658     icu_iter_destroy(iter);
659     icu_chain_destroy(chain);
660 }
661
662
663 void test_icu_iter2(void)
664 {
665     UErrorCode status = U_ZERO_ERROR;
666     struct icu_chain * chain = 0;
667     xmlNode *xml_node;
668     struct icu_iter *iter;
669     struct icu_buf_utf8 *token;
670
671     const char * xml_str = "<icu locale=\"en\">"
672         "<transform rule=\"[:Control:] Any-Remove\"/>"
673         "<tokenize rule=\"l\"/>"
674         "<tokenize rule=\"w\"/>"
675         "<transform rule=\"[[:WhiteSpace:][:Punctuation:]] Remove\"/>"
676         "<display/>"
677         "<casemap rule=\"l\"/>"
678         "</icu>";
679
680     xmlDoc *doc = xmlParseMemory(xml_str, strlen(xml_str));
681     YAZ_CHECK(doc);
682     if (!doc)
683         return;
684     xml_node = xmlDocGetRootElement(doc);
685     YAZ_CHECK(xml_node);
686     if (!xml_node)
687         return ;
688
689     chain = icu_chain_xml_config(xml_node, 0, &status);
690
691     xmlFreeDoc(doc);
692     YAZ_CHECK(chain);
693     
694     iter = icu_iter_create(chain, "Adobe Acrobat Reader, 1991-1999.");
695     YAZ_CHECK(iter);
696     if (!iter)
697         return;
698     token = icu_buf_utf8_create(0);
699     while (icu_iter_next(iter, token))
700     {
701         yaz_log(YLOG_LOG, "[%.*s]", (int) token->utf8_len, token->utf8);
702     }
703     icu_buf_utf8_destroy(token);
704
705     icu_iter_destroy(iter);
706     icu_chain_destroy(chain);
707 }
708
709 #endif /* YAZ_HAVE_ICU */
710
711 int main(int argc, char **argv)
712 {
713     YAZ_CHECK_INIT(argc, argv); 
714     YAZ_CHECK_LOG();
715
716 #if YAZ_HAVE_ICU
717
718     test_icu_I18N_casemap(argc, argv);
719     test_icu_I18N_sortmap(argc, argv);
720     test_icu_I18N_normalizer(argc, argv); 
721     test_icu_I18N_tokenizer(argc, argv);
722     test_icu_I18N_chain(argc, argv);
723     test_chain_empty_token();
724     test_chain_empty_chain();
725     test_icu_iter1();
726     test_icu_iter2();
727     
728     test_bug_1140();
729
730 #else /* YAZ_HAVE_ICU */
731
732     yaz_log(YLOG_LOG, "ICU unit tests omitted");
733     YAZ_CHECK(0 == 0);
734
735 #endif /* YAZ_HAVE_ICU */
736    
737     YAZ_CHECK_TERM;
738 }
739
740 /*
741  * Local variables:
742  * c-basic-offset: 4
743  * c-file-style: "Stroustrup"
744  * indent-tabs-mode: nil
745  * End:
746  * vim: shiftwidth=4 tabstop=8 expandtab
747  */
748