3a4c2536fe2724c1cd5952f5f77cb6cbb476e542
[pazpar2-moved-to-github.git] / src / test_icu_I18N.c
1 /* $Id: test_icu_I18N.c,v 1.6 2007-05-01 13:27:32 marc Exp $
2    Copyright (c) 2006-2007, Index Data.
3
4 This file is part of Pazpar2.
5
6 Pazpar2 is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
9 version.
10
11 Pazpar2 is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Pazpar2; see the file LICENSE.  If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA.
20  */
21
22 // DO NOT EDIT THIS FILE IF YOUR EDITOR DOES NOT SUPPORT UTF-8
23  
24
25 #if HAVE_CONFIG_H
26 #include "cconfig.h"
27 #endif
28
29 #define USE_TIMING 0
30 #if USE_TIMING
31 #include <yaz/timing.h>
32 #endif
33
34 #include <yaz/test.h>
35
36
37
38 #ifdef HAVE_ICU
39 #include "icu_I18N.h"
40 #include <string.h>
41 #include <stdlib.h>
42
43
44 // DO NOT EDIT THIS FILE IF YOUR EDITOR DOES NOT SUPPORT UTF-8
45
46 int test_icu_casemap(const char * locale, char action,
47                      const char * src8, const char * check8)
48 {
49     NMEM nmem = nmem_create();    
50     size_t buf_cap = 128;
51     char buf[buf_cap];
52     const char * dest8 = 0;
53     size_t dest8_len = 0;
54     //size_t src8_len = strlen(src8);
55     int sucess = 0;
56
57     //printf("original string:   '%s' (%d)\n", src8, (int) src8_len);
58
59     //these shall succeed
60     dest8 = icu_casemap(nmem, buf, buf_cap, &dest8_len,
61                         src8, locale, action);
62
63
64     //printf("icu_casemap '%s:%c' '%s' (%d)\n", 
65     //       locale, action, dest8, (int) dest8_len);
66
67     if (dest8 
68         && (dest8_len == strlen(check8))
69         && !strcmp(check8, dest8))
70         sucess = dest8_len;
71
72     nmem_destroy(nmem);
73
74     return sucess;
75 }
76
77 // DO NOT EDIT THIS FILE IF YOUR EDITOR DOES NOT SUPPORT UTF-8
78
79 void test_icu_I18N_casemap(int argc, char **argv)
80 {
81
82     // Locale 'en'
83
84     // sucessful tests
85     YAZ_CHECK(test_icu_casemap("en", 'l',
86                                "A ReD fOx hunTS sQUirriLs", 
87                                "a red fox hunts squirrils"));
88     
89     YAZ_CHECK(test_icu_casemap("en", 'u',
90                                "A ReD fOx hunTS sQUirriLs", 
91                                "A RED FOX HUNTS SQUIRRILS"));
92     
93     YAZ_CHECK(test_icu_casemap("en", 'f',
94                                "A ReD fOx hunTS sQUirriLs", 
95                                "a red fox hunts squirrils"));
96     
97     // this one fails and needs more investigation ..
98     YAZ_CHECK(0 == test_icu_casemap("en", 't',
99                                "A ReD fOx hunTS sQUirriLs", 
100                                "A Red Fox Hunts Squirrils"));
101
102
103     // Locale 'da'
104
105     // sucess expected    
106     YAZ_CHECK(test_icu_casemap("da", 'l',
107                                "åh ÆbLE, øs fLØde i Åen efter bLåBærGRødeN", 
108                                "åh æble, øs fløde i åen efter blåbærgrøden"));
109
110     YAZ_CHECK(test_icu_casemap("da", 'u',
111                                "åh ÆbLE, øs fLØde i Åen efter bLåBærGRødeN", 
112                                "ÅH ÆBLE, ØS FLØDE I ÅEN EFTER BLÅBÆRGRØDEN"));
113
114     YAZ_CHECK(test_icu_casemap("da", 'f',
115                                "åh ÆbLE, øs fLØde i Åen efter bLåBærGRødeN", 
116                                "åh æble, øs fløde i åen efter blåbærgrøden"));
117
118     YAZ_CHECK(test_icu_casemap("da", 't',
119                                "åh ÆbLE, øs fLØde i Åen efter bLåBærGRødeN", 
120                                "Åh Æble, Øs Fløde I Åen Efter Blåbærgrøden"));
121
122     // Locale 'de'
123
124     // sucess expected    
125     YAZ_CHECK(test_icu_casemap("de", 'l',
126                           "zWÖlf ärgerliche Würste rollen ÜBer die StRAße",
127                           "zwölf ärgerliche würste rollen über die straße"));
128
129     YAZ_CHECK(test_icu_casemap("de", 'u',
130                            "zWÖlf ärgerliche Würste rollen ÜBer die StRAße",
131                            "ZWÖLF ÄRGERLICHE WÜRSTE ROLLEN ÜBER DIE STRASSE"));
132
133     YAZ_CHECK(test_icu_casemap("de", 'f',
134                            "zWÖlf ärgerliche Würste rollen ÜBer die StRAße",
135                            "zwölf ärgerliche würste rollen über die strasse"));
136
137     YAZ_CHECK(test_icu_casemap("de", 't',
138                            "zWÖlf ärgerliche Würste rollen ÜBer die StRAße",
139                            "Zwölf Ärgerliche Würste Rollen Über Die Straße"));
140
141 }
142
143
144 // DO NOT EDIT THIS FILE IF YOUR EDITOR DOES NOT SUPPORT UTF-8
145
146 void test_icu_I18N_casemap_failures(int argc, char **argv)
147 {
148
149     size_t buf_cap = 128;
150     char buf[buf_cap];
151     size_t dest8_len = 0;
152     NMEM nmem = nmem_create();
153     char * dest8 = 0;
154
155     const char * src8 =  "A ReD fOx hunTS sQUirriLs";
156     //size_t src8_len = strlen(src8);
157     
158     //printf("original string:   '%s' (%d)\n", src8, (int) src8_len);
159
160     // some calling error needs investigation
161     dest8 = icu_casemap(nmem, buf, buf_cap, &dest8_len,
162                         src8, "en", 't');
163     YAZ_CHECK(0 == dest8_len);
164     //printf("icu_casemap 'en:t' '%s' (%d)\n", dest8, (int) dest8_len);
165
166
167     // attention: does not fail even if no locale 'xy_zz' defined
168     // it seems to default to english locale
169     dest8 = icu_casemap(nmem, buf, buf_cap, &dest8_len,
170                         src8, "zz_abc", 'l');
171     YAZ_CHECK(dest8_len);
172     //printf("icu_casemap 'zz:l' '%s' (%d)\n", dest8, (int) dest8_len);
173
174
175     // shall fail - no buf buffer defined
176     dest8 = icu_casemap(nmem, 0, buf_cap, &dest8_len,
177                         src8, "en", 'l');
178     YAZ_CHECK(0 == dest8_len);
179     //printf("icu_casemap 'en:l' '%s' (%d)\n", dest8, (int) dest8_len);
180
181     // shall fail - no buf_cap  defined
182     dest8 = icu_casemap(nmem, buf, 0, &dest8_len,
183                         src8, "en", 'l');
184     YAZ_CHECK(0 == dest8_len);
185     //printf("icu_casemap 'en:l' '%s' (%d)\n", dest8, (int) dest8_len);
186
187     // shall fail - no action 'x' defined
188     dest8 = icu_casemap(nmem, buf, buf_cap, &dest8_len,
189                         src8, "en", 'x');
190     YAZ_CHECK(0 == dest8_len);
191     //printf("icu_casemap 'en:x' '%s' (%d)\n", dest8, (int) dest8_len);
192
193     nmem_destroy(nmem);
194 }
195
196 // DO NOT EDIT THIS FILE IF YOUR EDITOR DOES NOT SUPPORT UTF-8
197
198 int test_icu_sortmap(const char * locale, size_t list_len,
199                      const char ** src8_list, const char ** check8_list)
200 {
201     int sucess = 1;
202
203     size_t i = 0;
204
205
206     NMEM nmem = nmem_create();    
207     size_t buf_cap = 128;
208     char buf[buf_cap];
209     struct icu_termmap ** dest8_list 
210         = nmem_malloc(nmem, sizeof(struct icu_termmap *) * list_len);
211     //size_t dest8_len = 0;
212     //size_t src8_len = strlen(src8);
213
214     // initializing icu_termmap
215     for (i = 0; i < list_len; i++){
216         dest8_list[i] = icu_termmap_create(nmem);
217         dest8_list[i]->norm_term = nmem_strdup(nmem, src8_list[i]); 
218         dest8_list[i]->disp_term = nmem_strdup(nmem, src8_list[i]);
219         //dest8_list[i]->sort_key =  nmem_strdup(nmem, src8_list[i]);
220         //dest8_list[i]->sort_len =  strlen(src8_list[i]);
221         dest8_list[i]->sort_key 
222             = icu_sortmap(nmem, buf, buf_cap, &(dest8_list[i]->sort_len),
223                           src8_list[i], locale);
224     }
225
226     // do the sorting
227     qsort(dest8_list, list_len, 
228           sizeof(struct icu_termmap *), icu_termmap_cmp);
229
230     // checking correct sorting
231     for (i = 0; i < list_len; i++){
232         if (0 != strcmp(dest8_list[i]->disp_term, check8_list[i])){
233             sucess = 0;
234         }
235     }
236
237     if (!sucess)
238         for (i = 0; i < list_len; i++){
239             printf("icu_sortmap '%s': '%s' '%s'\n", locale,
240                    dest8_list[i]->disp_term, check8_list[i]);
241         }
242
243     nmem_destroy(nmem);
244
245     return sucess;
246 }
247
248
249 // DO NOT EDIT THIS FILE IF YOUR EDITOR DOES NOT SUPPORT UTF-8
250
251 void test_icu_I18N_sortmap(int argc, char **argv)
252 {
253
254     // sucessful tests
255     size_t en_1_len = 6;
256     const char * en_1_src[6] = {"z", "K", "a", "A", "Z", "k"};
257     const char * en_1_cck[6] = {"a", "A", "K", "k", "z", "Z"};
258     YAZ_CHECK(test_icu_sortmap("en", en_1_len, en_1_src, en_1_cck));
259     YAZ_CHECK(0 == test_icu_sortmap("en_AU", en_1_len, en_1_src, en_1_cck));
260     YAZ_CHECK(0 == test_icu_sortmap("en_CA", en_1_len, en_1_src, en_1_cck));
261     YAZ_CHECK(0 == test_icu_sortmap("en_GB", en_1_len, en_1_src, en_1_cck));
262     YAZ_CHECK(0 == test_icu_sortmap("en_US", en_1_len, en_1_src, en_1_cck));
263     
264     // sucessful tests - this one fails and should not!!!
265     size_t da_1_len = 6;
266     const char * da_1_src[6] = {"z", "å", "o", "æ", "a", "ø"};
267     const char * da_1_cck[6] = {"a", "o", "z", "æ", "ø", "å"};
268     YAZ_CHECK(0 == test_icu_sortmap("da", da_1_len, da_1_src, da_1_cck));
269     YAZ_CHECK(0 == test_icu_sortmap("da_DK", da_1_len, da_1_src, da_1_cck));
270     
271     // sucessful tests
272     size_t de_1_len = 9;
273     const char * de_1_src[9] = {"u", "ä", "o", "t", "s", "ß", "ü", "ö", "a"};
274     const char * de_1_cck[9] = {"ä", "a", "o", "ö", "s", "ß", "t", "u", "ü"};
275     YAZ_CHECK(test_icu_sortmap("de", de_1_len, de_1_src, de_1_cck));
276     YAZ_CHECK(0 == test_icu_sortmap("de_AT", de_1_len, de_1_src, de_1_cck));
277     YAZ_CHECK(0 == test_icu_sortmap("de_DE", de_1_len, de_1_src, de_1_cck));
278     
279 }
280
281
282 #endif    
283
284 // DO NOT EDIT THIS FILE IF YOUR EDITOR DOES NOT SUPPORT UTF-8
285
286 int main(int argc, char **argv)
287 {
288
289     YAZ_CHECK_INIT(argc, argv); 
290     YAZ_CHECK_LOG();
291
292 #ifdef HAVE_ICU
293
294     test_icu_I18N_casemap_failures(argc, argv);
295     test_icu_I18N_casemap(argc, argv);
296     test_icu_I18N_sortmap(argc, argv);
297  
298 #else
299
300     printf("ICU unit tests omitted.\n"
301            "Please install libicu36-dev and icu-doc or similar\n");
302     YAZ_CHECK(0 == 0);
303
304 #endif    
305    
306     YAZ_CHECK_TERM;
307 }
308
309
310 // DO NOT EDIT THIS FILE IF YOUR EDITOR DOES NOT SUPPORT UTF-8
311
312 /*
313  * Local variables:
314  * c-basic-offset: 4
315  * indent-tabs-mode: nil
316  * End:
317  * vim: shiftwidth=4 tabstop=8 expandtab
318  */