]> git.ipfire.org Git - thirdparty/cups.git/blame - cups/testi18n.c
Load cups into easysw/current.
[thirdparty/cups.git] / cups / testi18n.c
CommitLineData
ef416fc2 1/*
e1d6a774 2 * "$Id: testi18n.c 5294 2006-03-15 21:09:32Z mike $"
ef416fc2 3 *
4 * Internationalization test for Common UNIX Printing System (CUPS).
5 *
e1d6a774 6 * Copyright 1997-2006 by Easy Software Products.
ef416fc2 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 *
e1d6a774 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.
ef416fc2 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
e1d6a774 40#include "i18n.h"
ef416fc2 41#include "string.h"
ef416fc2 42
43
44/*
45 * Local functions...
46 */
47
ef416fc2 48static void print_utf8(const char *msg, const cups_utf8_t *src);
ef416fc2 49static void print_utf32(const char *msg, const cups_utf32_t *src);
ef416fc2 50
51
52/*
53 * 'main()' - Main entry for internationalization test module.
54 */
55
56int /* O - Exit code */
57main(int argc, /* I - Argument Count */
58 char *argv[]) /* I - Arguments */
ef416fc2 59{
60 FILE *fp; /* File pointer */
61 int count; /* File line counter */
e1d6a774 62 int status, /* Status of current test */
63 errors; /* Error count */
ef416fc2 64 char line[1024]; /* File line source string */
65 int len; /* Length (count) of string */
e1d6a774 66 char legsrc[1024], /* Legacy source string */
67 legdest[1024], /* Legacy destination string */
68 *legptr; /* Pointer into legacy string */
ef416fc2 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 };
e1d6a774 77 /* "A != <ALPHA>." - use ISO 8859-7 */
ef416fc2 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 */
ef416fc2 91 cups_utf32_t utf32src[1024]; /* UTF-32 source string */
92 cups_utf32_t utf32dest[1024]; /* UTF-32 destination string */
e1d6a774 93 _cups_vmap_t *vmap; /* VBCS charmap pointer */
ef416fc2 94
95
96 /*
e1d6a774 97 * Start with some conversion tests from a UTF-8 test file.
ef416fc2 98 */
99
e1d6a774 100 errors = 0;
101
102 if ((fp = fopen("utf8demo.txt", "r")) == NULL)
ef416fc2 103 {
e1d6a774 104 perror("utf8demo.txt");
105 return (1);
ef416fc2 106 }
107
108 /*
e1d6a774 109 * cupsUTF8ToUTF32
ef416fc2 110 */
111
e1d6a774 112 fputs("cupsUTF8ToUTF32 of utfdemo.txt: ", stdout);
113
114 for (count = 0, status = 0; fgets(line, sizeof(line), fp);)
ef416fc2 115 {
e1d6a774 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 }
ef416fc2 125 }
126
e1d6a774 127 if (!status)
128 puts("PASS");
ef416fc2 129
e1d6a774 130 /*
131 * cupsUTF8ToCharset(CUPS_EUC_JP)
132 */
ef416fc2 133
e1d6a774 134 fputs("cupsUTF8ToCharset(CUPS_EUC_JP) of utfdemo.txt: ", stdout);
ef416fc2 135
e1d6a774 136 rewind(fp);
137
138 for (count = 0, status = 0; fgets(line, sizeof(line), fp);)
139 {
140 count ++;
ef416fc2 141
142 len = cupsUTF8ToCharset(legdest, (cups_utf8_t *)line, 1024, CUPS_EUC_JP);
143 if (len < 0)
e1d6a774 144 {
145 printf("FAIL (UTF-8 to EUC-JP on line %d)\n", count);
146 errors ++;
147 status = 1;
148 break;
149 }
ef416fc2 150 }
151
e1d6a774 152 if (!status)
153 puts("PASS");
154
ef416fc2 155 fclose(fp);
156
e1d6a774 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");
ef416fc2 170
171 /*
e1d6a774 172 * Test charmap load for Windows-932 (Shift-JIS)...
ef416fc2 173 */
174
e1d6a774 175 fputs("_cupsCharmapGet(CUPS_WINDOWS_932): ", stdout);
ef416fc2 176
e1d6a774 177 if (!_cupsCharmapGet(CUPS_WINDOWS_932))
178 {
179 errors ++;
180 puts("FAIL");
181 }
182 else
183 puts("PASS");
ef416fc2 184
e1d6a774 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)
ef416fc2 192 {
e1d6a774 193 errors ++;
194 puts("FAIL");
ef416fc2 195 }
e1d6a774 196 else
197 puts("PASS");
ef416fc2 198
199 /*
200 * Test VBCS charmap load for EUC-TW...
201 */
202
e1d6a774 203 fputs("_cupsCharmapGet(CUPS_EUC_TW): ", stdout);
ef416fc2 204
e1d6a774 205 if ((vmap = (_cups_vmap_t *)_cupsCharmapGet(CUPS_EUC_TW)) == NULL)
ef416fc2 206 {
e1d6a774 207 errors ++;
208 puts("FAIL");
ef416fc2 209 }
e1d6a774 210 else
211 puts("PASS");
ef416fc2 212
213 /*
214 * Test UTF-8 to legacy charset (ISO 8859-1)...
215 */
216
e1d6a774 217 fputs("cupsUTF8ToCharset(CUPS_ISO8859_1): ", stdout);
ef416fc2 218
219 legdest[0] = 0;
220
221 len = cupsUTF8ToCharset(legdest, utf8latin, 1024, CUPS_ISO8859_1);
222 if (len < 0)
ef416fc2 223 {
e1d6a774 224 printf("FAIL (len=%d)\n", len);
225 errors ++;
ef416fc2 226 }
e1d6a774 227 else
228 puts("PASS");
ef416fc2 229
e1d6a774 230 /*
231 * cupsCharsetToUTF8
232 */
ef416fc2 233
e1d6a774 234 fputs("cupsCharsetToUTF8(CUPS_ISO8859_1): ", stdout);
ef416fc2 235
e1d6a774 236 strcpy(legsrc, legdest);
ef416fc2 237
e1d6a774 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))
ef416fc2 247 {
e1d6a774 248 puts("FAIL (results do not match)");
249 print_utf8(" utf8latin", utf8latin);
250 print_utf8(" utf8dest", utf8dest);
251 errors ++;
ef416fc2 252 }
e1d6a774 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");
ef416fc2 260
261 /*
e1d6a774 262 * Test UTF-8 to/from legacy charset (ISO 8859-7)...
ef416fc2 263 */
e1d6a774 264
265 fputs("cupsUTF8ToCharset(CUPS_ISO8859_7): ", stdout);
266
267 if (cupsUTF8ToCharset(legdest, utf8greek, 1024, CUPS_ISO8859_7) < 0)
ef416fc2 268 {
e1d6a774 269 puts("FAIL");
270 errors ++;
ef416fc2 271 }
e1d6a774 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
ef416fc2 287 strcpy(legsrc, legdest);
e1d6a774 288
ef416fc2 289 len = cupsCharsetToUTF8(utf8dest, legsrc, 1024, CUPS_ISO8859_7);
e1d6a774 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");
ef416fc2 306
307 /*
e1d6a774 308 * Test UTF-8 to/from legacy charset (Windows 932)...
ef416fc2 309 */
e1d6a774 310
311 fputs("cupsUTF8ToCharset(CUPS_WINDOWS_932): ", stdout);
312
313 if (cupsUTF8ToCharset(legdest, utf8japan, 1024, CUPS_WINDOWS_932) < 0)
ef416fc2 314 {
e1d6a774 315 puts("FAIL");
316 errors ++;
ef416fc2 317 }
e1d6a774 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
ef416fc2 333 strcpy(legsrc, legdest);
e1d6a774 334
ef416fc2 335 len = cupsCharsetToUTF8(utf8dest, legsrc, 1024, CUPS_WINDOWS_932);
e1d6a774 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");
ef416fc2 352
353 /*
e1d6a774 354 * Test UTF-8 to/from legacy charset (EUC-JP)...
ef416fc2 355 */
e1d6a774 356
357 fputs("cupsUTF8ToCharset(CUPS_EUC_JP): ", stdout);
358
359 if (cupsUTF8ToCharset(legdest, utf8japan, 1024, CUPS_EUC_JP) < 0)
ef416fc2 360 {
e1d6a774 361 puts("FAIL");
362 errors ++;
ef416fc2 363 }
e1d6a774 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
ef416fc2 379 strcpy(legsrc, legdest);
e1d6a774 380
ef416fc2 381 len = cupsCharsetToUTF8(utf8dest, legsrc, 1024, CUPS_EUC_JP);
e1d6a774 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");
ef416fc2 398
399 /*
e1d6a774 400 * Test UTF-8 to/from legacy charset (Windows 950)...
ef416fc2 401 */
e1d6a774 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
ef416fc2 411 {
e1d6a774 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");
ef416fc2 421 }
e1d6a774 422
423 fputs("cupsCharsetToUTF8(CUPS_WINDOWS_950): ", stdout);
424
ef416fc2 425 strcpy(legsrc, legdest);
e1d6a774 426
ef416fc2 427 len = cupsCharsetToUTF8(utf8dest, legsrc, 1024, CUPS_WINDOWS_950);
e1d6a774 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");
ef416fc2 444
445 /*
e1d6a774 446 * Test UTF-8 to/from legacy charset (EUC-TW)...
ef416fc2 447 */
e1d6a774 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
ef416fc2 457 {
e1d6a774 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");
ef416fc2 467 }
e1d6a774 468
469 fputs("cupsCharsetToUTF8(CUPS_EUC_TW): ", stdout);
470
ef416fc2 471 strcpy(legsrc, legdest);
e1d6a774 472
ef416fc2 473 len = cupsCharsetToUTF8(utf8dest, legsrc, 1024, CUPS_EUC_TW);
e1d6a774 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");
ef416fc2 490
e1d6a774 491#if 0
ef416fc2 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 /*
e1d6a774 526 * Test _cupsCharmapFlush()...
ef416fc2 527 */
528 if (verbose)
e1d6a774 529 printf("\ntesti18n: Testing _cupsCharmapFlush()...\n");
530 _cupsCharmapFlush();
ef416fc2 531 return (0);
e1d6a774 532#endif /* 0 */
533
534 return (errors > 0);
ef416fc2 535}
536
537
538/*
e1d6a774 539 * 'print_utf8()' - Print UTF-8 string with (optional) message.
ef416fc2 540 */
541
e1d6a774 542static void
543print_utf8(const char *msg, /* I - Message String */
544 const cups_utf8_t *src) /* I - UTF-8 Source String */
ef416fc2 545{
e1d6a774 546 if (msg)
547 printf("%s:", msg);
ef416fc2 548
e1d6a774 549 for (; *src; src ++)
550 printf(" %02x", *src);
ef416fc2 551
e1d6a774 552 putchar('\n');
553}
ef416fc2 554
ef416fc2 555
e1d6a774 556/*
557 * 'print_utf32()' - Print UTF-32 string with (optional) message.
558 */
ef416fc2 559
e1d6a774 560static void
561print_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);
ef416fc2 566
e1d6a774 567 for (; *src; src ++)
568 printf(" %04x", (int) *src);
ef416fc2 569
e1d6a774 570 putchar('\n');
ef416fc2 571}
572
573
574/*
e1d6a774 575 * End of "$Id: testi18n.c 5294 2006-03-15 21:09:32Z mike $"
ef416fc2 576 */