]> git.ipfire.org Git - thirdparty/cups.git/blob - cups/testi18n.c
Load cups into easysw/current.
[thirdparty/cups.git] / cups / testi18n.c
1 /*
2 * "$Id: testi18n.c 5837 2006-08-17 14:37:40Z mike $"
3 *
4 * Internationalization test for Common UNIX Printing System (CUPS).
5 *
6 * Copyright 1997-2006 by Easy Software Products.
7 *
8 * These coded instructions, statements, and computer programs are
9 * the property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the
11 * file "LICENSE.txt" which should have been included with this file.
12 * If this file is missing or damaged please contact Easy Software
13 * Products at:
14 *
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
19 *
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
23 *
24 * Contents:
25 *
26 * main() - Main entry for internationalization test module.
27 * print_utf8() - Print UTF-8 string with (optional) message.
28 */
29
30 /*
31 * Include necessary headers...
32 */
33
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <errno.h>
37 #include <time.h>
38 #include <unistd.h>
39
40 #include "i18n.h"
41 #include "string.h"
42
43
44 /*
45 * Local functions...
46 */
47
48 static void print_utf8(const char *msg, const cups_utf8_t *src);
49
50
51 /*
52 * 'main()' - Main entry for internationalization test module.
53 */
54
55 int /* O - Exit code */
56 main(int argc, /* I - Argument Count */
57 char *argv[]) /* I - Arguments */
58 {
59 FILE *fp; /* File pointer */
60 int count; /* File line counter */
61 int status, /* Status of current test */
62 errors; /* Error count */
63 char line[1024]; /* File line source string */
64 int len; /* Length (count) of string */
65 char legsrc[1024], /* Legacy source string */
66 legdest[1024], /* Legacy destination string */
67 *legptr; /* Pointer into legacy string */
68 cups_utf8_t utf8latin[] = /* UTF-8 Latin-1 source */
69 { 0x41, 0x20, 0x21, 0x3D, 0x20, 0xC3, 0x84, 0x2E, 0x00 };
70 /* "A != <A WITH DIAERESIS>." - use ISO 8859-1 */
71 cups_utf8_t utf8repla[] = /* UTF-8 Latin-1 replacement */
72 { 0x41, 0x20, 0xE2, 0x89, 0xA2, 0x20, 0xC3, 0x84, 0x2E, 0x00 };
73 /* "A <NOT IDENTICAL TO> <A WITH DIAERESIS>." */
74 cups_utf8_t utf8greek[] = /* UTF-8 Greek source string */
75 { 0x41, 0x20, 0x21, 0x3D, 0x20, 0xCE, 0x91, 0x2E, 0x00 };
76 /* "A != <ALPHA>." - use ISO 8859-7 */
77 cups_utf8_t utf8japan[] = /* UTF-8 Japanese source */
78 { 0x41, 0x20, 0x21, 0x3D, 0x20, 0xEE, 0x9C, 0x80, 0x2E, 0x00 };
79 /* "A != <PRIVATE U+E700>." - use Windows 932 or EUC-JP */
80 cups_utf8_t utf8taiwan[] = /* UTF-8 Chinese source */
81 { 0x41, 0x20, 0x21, 0x3D, 0x20, 0xE4, 0xB9, 0x82, 0x2E, 0x00 };
82 /* "A != <CJK U+4E42>." - use Windows 950 (Big5) or EUC-TW */
83 cups_utf8_t utf8dest[1024]; /* UTF-8 destination string */
84 cups_utf32_t utf32dest[1024]; /* UTF-32 destination string */
85
86
87 /*
88 * Make sure we have a symbolic link from the data directory to a
89 * "charmaps" directory, and then point the library at it...
90 */
91
92 if (access("charmaps", 0))
93 symlink("../data", "charmaps");
94
95 putenv("CUPS_DATADIR=.");
96
97 /*
98 * Start with some conversion tests from a UTF-8 test file.
99 */
100
101 errors = 0;
102
103 if ((fp = fopen("utf8demo.txt", "r")) == NULL)
104 {
105 perror("utf8demo.txt");
106 return (1);
107 }
108
109 /*
110 * cupsUTF8ToUTF32
111 */
112
113 fputs("cupsUTF8ToUTF32 of utfdemo.txt: ", stdout);
114
115 for (count = 0, status = 0; fgets(line, sizeof(line), fp);)
116 {
117 count ++;
118
119 if (cupsUTF8ToUTF32(utf32dest, (cups_utf8_t *)line, 1024) < 0)
120 {
121 printf("FAIL (UTF-8 to UTF-32 on line %d)\n", count);
122 errors ++;
123 status = 1;
124 break;
125 }
126 }
127
128 if (!status)
129 puts("PASS");
130
131 /*
132 * cupsUTF8ToCharset(CUPS_EUC_JP)
133 */
134
135 fputs("cupsUTF8ToCharset(CUPS_EUC_JP) of utfdemo.txt: ", stdout);
136
137 rewind(fp);
138
139 for (count = 0, status = 0; fgets(line, sizeof(line), fp);)
140 {
141 count ++;
142
143 len = cupsUTF8ToCharset(legdest, (cups_utf8_t *)line, 1024, CUPS_EUC_JP);
144 if (len < 0)
145 {
146 printf("FAIL (UTF-8 to EUC-JP on line %d)\n", count);
147 errors ++;
148 status = 1;
149 break;
150 }
151 }
152
153 if (!status)
154 puts("PASS");
155
156 fclose(fp);
157
158 /*
159 * Test charmap load for ISO-8859-1...
160 */
161
162 fputs("_cupsCharmapGet(CUPS_ISO8859_1): ", stdout);
163
164 if (!_cupsCharmapGet(CUPS_ISO8859_1))
165 {
166 errors ++;
167 puts("FAIL");
168 }
169 else
170 puts("PASS");
171
172 /*
173 * Test charmap load for Windows-932 (Shift-JIS)...
174 */
175
176 fputs("_cupsCharmapGet(CUPS_WINDOWS_932): ", stdout);
177
178 if (!_cupsCharmapGet(CUPS_WINDOWS_932))
179 {
180 errors ++;
181 puts("FAIL");
182 }
183 else
184 puts("PASS");
185
186 /*
187 * Test VBCS charmap load for EUC-JP...
188 */
189
190 fputs("_cupsCharmapGet(CUPS_EUC_JP): ", stdout);
191
192 if (!_cupsCharmapGet(CUPS_EUC_JP))
193 {
194 errors ++;
195 puts("FAIL");
196 }
197 else
198 puts("PASS");
199
200 /*
201 * Test VBCS charmap load for EUC-TW...
202 */
203
204 fputs("_cupsCharmapGet(CUPS_EUC_TW): ", stdout);
205
206 if (!_cupsCharmapGet(CUPS_EUC_TW))
207 {
208 errors ++;
209 puts("FAIL");
210 }
211 else
212 puts("PASS");
213
214 /*
215 * Test UTF-8 to legacy charset (ISO 8859-1)...
216 */
217
218 fputs("cupsUTF8ToCharset(CUPS_ISO8859_1): ", stdout);
219
220 legdest[0] = 0;
221
222 len = cupsUTF8ToCharset(legdest, utf8latin, 1024, CUPS_ISO8859_1);
223 if (len < 0)
224 {
225 printf("FAIL (len=%d)\n", len);
226 errors ++;
227 }
228 else
229 puts("PASS");
230
231 /*
232 * cupsCharsetToUTF8
233 */
234
235 fputs("cupsCharsetToUTF8(CUPS_ISO8859_1): ", stdout);
236
237 strcpy(legsrc, legdest);
238
239 len = cupsCharsetToUTF8(utf8dest, legsrc, 1024, CUPS_ISO8859_1);
240 if (len != strlen((char *)utf8latin))
241 {
242 printf("FAIL (len=%d, expected %d)\n", len, (int)strlen((char *)utf8latin));
243 print_utf8(" utf8latin", utf8latin);
244 print_utf8(" utf8dest", utf8dest);
245 errors ++;
246 }
247 else if (memcmp(utf8latin, utf8dest, len))
248 {
249 puts("FAIL (results do not match)");
250 print_utf8(" utf8latin", utf8latin);
251 print_utf8(" utf8dest", utf8dest);
252 errors ++;
253 }
254 else if (cupsUTF8ToCharset(legdest, utf8repla, 1024, CUPS_ISO8859_1) < 0)
255 {
256 puts("FAIL (replacement characters do not work!)");
257 errors ++;
258 }
259 else
260 puts("PASS");
261
262 /*
263 * Test UTF-8 to/from legacy charset (ISO 8859-7)...
264 */
265
266 fputs("cupsUTF8ToCharset(CUPS_ISO8859_7): ", stdout);
267
268 if (cupsUTF8ToCharset(legdest, utf8greek, 1024, CUPS_ISO8859_7) < 0)
269 {
270 puts("FAIL");
271 errors ++;
272 }
273 else
274 {
275 for (legptr = legdest; *legptr && *legptr != '?'; legptr ++);
276
277 if (*legptr)
278 {
279 puts("FAIL (unknown character)");
280 errors ++;
281 }
282 else
283 puts("PASS");
284 }
285
286 fputs("cupsCharsetToUTF8(CUPS_ISO8859_7): ", stdout);
287
288 strcpy(legsrc, legdest);
289
290 len = cupsCharsetToUTF8(utf8dest, legsrc, 1024, CUPS_ISO8859_7);
291 if (len != strlen((char *)utf8greek))
292 {
293 printf("FAIL (len=%d, expected %d)\n", len, (int)strlen((char *)utf8greek));
294 print_utf8(" utf8greek", utf8greek);
295 print_utf8(" utf8dest", utf8dest);
296 errors ++;
297 }
298 else if (memcmp(utf8greek, utf8dest, len))
299 {
300 puts("FAIL (results do not match)");
301 print_utf8(" utf8greek", utf8greek);
302 print_utf8(" utf8dest", utf8dest);
303 errors ++;
304 }
305 else
306 puts("PASS");
307
308 /*
309 * Test UTF-8 to/from legacy charset (Windows 932)...
310 */
311
312 fputs("cupsUTF8ToCharset(CUPS_WINDOWS_932): ", stdout);
313
314 if (cupsUTF8ToCharset(legdest, utf8japan, 1024, CUPS_WINDOWS_932) < 0)
315 {
316 puts("FAIL");
317 errors ++;
318 }
319 else
320 {
321 for (legptr = legdest; *legptr && *legptr != '?'; legptr ++);
322
323 if (*legptr)
324 {
325 puts("FAIL (unknown character)");
326 errors ++;
327 }
328 else
329 puts("PASS");
330 }
331
332 fputs("cupsCharsetToUTF8(CUPS_WINDOWS_932): ", stdout);
333
334 strcpy(legsrc, legdest);
335
336 len = cupsCharsetToUTF8(utf8dest, legsrc, 1024, CUPS_WINDOWS_932);
337 if (len != strlen((char *)utf8japan))
338 {
339 printf("FAIL (len=%d, expected %d)\n", len, (int)strlen((char *)utf8japan));
340 print_utf8(" utf8japan", utf8japan);
341 print_utf8(" utf8dest", utf8dest);
342 errors ++;
343 }
344 else if (memcmp(utf8japan, utf8dest, len))
345 {
346 puts("FAIL (results do not match)");
347 print_utf8(" utf8japan", utf8japan);
348 print_utf8(" utf8dest", utf8dest);
349 errors ++;
350 }
351 else
352 puts("PASS");
353
354 /*
355 * Test UTF-8 to/from legacy charset (EUC-JP)...
356 */
357
358 fputs("cupsUTF8ToCharset(CUPS_EUC_JP): ", stdout);
359
360 if (cupsUTF8ToCharset(legdest, utf8japan, 1024, CUPS_EUC_JP) < 0)
361 {
362 puts("FAIL");
363 errors ++;
364 }
365 else
366 {
367 for (legptr = legdest; *legptr && *legptr != '?'; legptr ++);
368
369 if (*legptr)
370 {
371 puts("FAIL (unknown character)");
372 errors ++;
373 }
374 else
375 puts("PASS");
376 }
377
378 fputs("cupsCharsetToUTF8(CUPS_EUC_JP): ", stdout);
379
380 strcpy(legsrc, legdest);
381
382 len = cupsCharsetToUTF8(utf8dest, legsrc, 1024, CUPS_EUC_JP);
383 if (len != strlen((char *)utf8japan))
384 {
385 printf("FAIL (len=%d, expected %d)\n", len, (int)strlen((char *)utf8japan));
386 print_utf8(" utf8japan", utf8japan);
387 print_utf8(" utf8dest", utf8dest);
388 errors ++;
389 }
390 else if (memcmp(utf8japan, utf8dest, len))
391 {
392 puts("FAIL (results do not match)");
393 print_utf8(" utf8japan", utf8japan);
394 print_utf8(" utf8dest", utf8dest);
395 errors ++;
396 }
397 else
398 puts("PASS");
399
400 /*
401 * Test UTF-8 to/from legacy charset (Windows 950)...
402 */
403
404 fputs("cupsUTF8ToCharset(CUPS_WINDOWS_950): ", stdout);
405
406 if (cupsUTF8ToCharset(legdest, utf8taiwan, 1024, CUPS_WINDOWS_950) < 0)
407 {
408 puts("FAIL");
409 errors ++;
410 }
411 else
412 {
413 for (legptr = legdest; *legptr && *legptr != '?'; legptr ++);
414
415 if (*legptr)
416 {
417 puts("FAIL (unknown character)");
418 errors ++;
419 }
420 else
421 puts("PASS");
422 }
423
424 fputs("cupsCharsetToUTF8(CUPS_WINDOWS_950): ", stdout);
425
426 strcpy(legsrc, legdest);
427
428 len = cupsCharsetToUTF8(utf8dest, legsrc, 1024, CUPS_WINDOWS_950);
429 if (len != strlen((char *)utf8taiwan))
430 {
431 printf("FAIL (len=%d, expected %d)\n", len, (int)strlen((char *)utf8taiwan));
432 print_utf8(" utf8taiwan", utf8taiwan);
433 print_utf8(" utf8dest", utf8dest);
434 errors ++;
435 }
436 else if (memcmp(utf8taiwan, utf8dest, len))
437 {
438 puts("FAIL (results do not match)");
439 print_utf8(" utf8taiwan", utf8taiwan);
440 print_utf8(" utf8dest", utf8dest);
441 errors ++;
442 }
443 else
444 puts("PASS");
445
446 /*
447 * Test UTF-8 to/from legacy charset (EUC-TW)...
448 */
449
450 fputs("cupsUTF8ToCharset(CUPS_EUC_TW): ", stdout);
451
452 if (cupsUTF8ToCharset(legdest, utf8taiwan, 1024, CUPS_EUC_TW) < 0)
453 {
454 puts("FAIL");
455 errors ++;
456 }
457 else
458 {
459 for (legptr = legdest; *legptr && *legptr != '?'; legptr ++);
460
461 if (*legptr)
462 {
463 puts("FAIL (unknown character)");
464 errors ++;
465 }
466 else
467 puts("PASS");
468 }
469
470 fputs("cupsCharsetToUTF8(CUPS_EUC_TW): ", stdout);
471
472 strcpy(legsrc, legdest);
473
474 len = cupsCharsetToUTF8(utf8dest, legsrc, 1024, CUPS_EUC_TW);
475 if (len != strlen((char *)utf8taiwan))
476 {
477 printf("FAIL (len=%d, expected %d)\n", len, (int)strlen((char *)utf8taiwan));
478 print_utf8(" utf8taiwan", utf8taiwan);
479 print_utf8(" utf8dest", utf8dest);
480 errors ++;
481 }
482 else if (memcmp(utf8taiwan, utf8dest, len))
483 {
484 puts("FAIL (results do not match)");
485 print_utf8(" utf8taiwan", utf8taiwan);
486 print_utf8(" utf8dest", utf8dest);
487 errors ++;
488 }
489 else
490 puts("PASS");
491
492 #if 0
493 /*
494 * Test UTF-8 (16-bit) to UTF-32 (w/ BOM)...
495 */
496 if (verbose)
497 printf("\ntesti18n: Testing UTF-8 to UTF-32 (w/ BOM)...\n");
498 len = cupsUTF8ToUTF32(utf32dest, utf8good, 1024);
499 if (len < 0)
500 return (1);
501 if (verbose)
502 {
503 print_utf8(" utf8good ", utf8good);
504 print_utf32(" utf32dest", utf32dest);
505 }
506 memcpy (utf32src, utf32dest, (len + 1) * sizeof(cups_utf32_t));
507 len = cupsUTF32ToUTF8(utf8dest, utf32src, 1024);
508 if (len < 0)
509 return (1);
510 if (len != strlen ((char *) utf8good))
511 return (1);
512 if (memcmp(utf8good, utf8dest, len) != 0)
513 return (1);
514
515 /*
516 * Test invalid UTF-8 (16-bit) to UTF-32 (w/ BOM)...
517 */
518 if (verbose)
519 printf("\ntesti18n: Testing UTF-8 bad 16-bit source string...\n");
520 len = cupsUTF8ToUTF32(utf32dest, utf8bad, 1024);
521 if (len >= 0)
522 return (1);
523 if (verbose)
524 print_utf8(" utf8bad ", utf8bad);
525
526 /*
527 * Test _cupsCharmapFlush()...
528 */
529 if (verbose)
530 printf("\ntesti18n: Testing _cupsCharmapFlush()...\n");
531 _cupsCharmapFlush();
532 return (0);
533 #endif /* 0 */
534
535 return (errors > 0);
536 }
537
538
539 /*
540 * 'print_utf8()' - Print UTF-8 string with (optional) message.
541 */
542
543 static void
544 print_utf8(const char *msg, /* I - Message String */
545 const cups_utf8_t *src) /* I - UTF-8 Source String */
546 {
547 if (msg)
548 printf("%s:", msg);
549
550 for (; *src; src ++)
551 printf(" %02x", *src);
552
553 putchar('\n');
554 }
555
556
557 /*
558 * End of "$Id: testi18n.c 5837 2006-08-17 14:37:40Z mike $"
559 */