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