]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/testi18n.c
2 * "$Id: testi18n.c 7560 2008-05-13 06:34:04Z mike $"
4 * Internationalization test for CUPS.
6 * Copyright 2007-2010 by Apple Inc.
7 * Copyright 1997-2006 by Easy Software Products.
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
15 * This file is subject to the Apple OS-Developed Software exception.
19 * main() - Main entry for internationalization test module.
20 * print_utf8() - Print UTF-8 string with (optional) message.
24 * Include necessary headers...
27 #include "string-private.h"
28 #include "language-private.h"
38 static const char * const lang_encodings
[] =
39 { /* Encoding strings */
40 "us-ascii", "iso-8859-1",
41 "iso-8859-2", "iso-8859-3",
42 "iso-8859-4", "iso-8859-5",
43 "iso-8859-6", "iso-8859-7",
44 "iso-8859-8", "iso-8859-9",
45 "iso-8859-10", "utf-8",
46 "iso-8859-13", "iso-8859-14",
47 "iso-8859-15", "windows-874",
48 "windows-1250", "windows-1251",
49 "windows-1252", "windows-1253",
50 "windows-1254", "windows-1255",
51 "windows-1256", "windows-1257",
52 "windows-1258", "koi8-r",
53 "koi8-u", "iso-8859-11",
54 "iso-8859-16", "mac-roman",
72 "windows-932", "windows-936",
73 "windows-949", "windows-950",
74 "windows-1361", "unknown",
100 "unknown", "unknown",
101 "unknown", "unknown",
102 "unknown", "unknown",
103 "unknown", "unknown",
114 static void print_utf8(const char *msg
, const cups_utf8_t
*src
);
118 * 'main()' - Main entry for internationalization test module.
121 int /* O - Exit code */
122 main(int argc
, /* I - Argument Count */
123 char *argv
[]) /* I - Arguments */
125 FILE *fp
; /* File pointer */
126 int count
; /* File line counter */
127 int status
, /* Status of current test */
128 errors
; /* Error count */
129 char line
[1024]; /* File line source string */
130 int len
; /* Length (count) of string */
131 char legsrc
[1024], /* Legacy source string */
132 legdest
[1024], /* Legacy destination string */
133 *legptr
; /* Pointer into legacy string */
134 cups_utf8_t utf8latin
[] = /* UTF-8 Latin-1 source */
135 { 0x41, 0x20, 0x21, 0x3D, 0x20, 0xC3, 0x84, 0x2E, 0x00 };
136 /* "A != <A WITH DIAERESIS>." - use ISO 8859-1 */
137 cups_utf8_t utf8repla
[] = /* UTF-8 Latin-1 replacement */
138 { 0x41, 0x20, 0xE2, 0x89, 0xA2, 0x20, 0xC3, 0x84, 0x2E, 0x00 };
139 /* "A <NOT IDENTICAL TO> <A WITH DIAERESIS>." */
140 cups_utf8_t utf8greek
[] = /* UTF-8 Greek source string */
141 { 0x41, 0x20, 0x21, 0x3D, 0x20, 0xCE, 0x91, 0x2E, 0x00 };
142 /* "A != <ALPHA>." - use ISO 8859-7 */
143 cups_utf8_t utf8japan
[] = /* UTF-8 Japanese source */
144 { 0x41, 0x20, 0x21, 0x3D, 0x20, 0xEE, 0x9C, 0x80, 0x2E, 0x00 };
145 /* "A != <PRIVATE U+E700>." - use Windows 932 or EUC-JP */
146 cups_utf8_t utf8taiwan
[] = /* UTF-8 Chinese source */
147 { 0x41, 0x20, 0x21, 0x3D, 0x20, 0xE4, 0xB9, 0x82, 0x2E, 0x00 };
148 /* "A != <CJK U+4E42>." - use Windows 950 (Big5) or EUC-TW */
149 cups_utf8_t utf8dest
[1024]; /* UTF-8 destination string */
150 cups_utf32_t utf32dest
[1024]; /* UTF-32 destination string */
155 int i
; /* Looping var */
156 cups_encoding_t encoding
; /* Source encoding */
161 puts("Usage: ./testi18n [filename charset]");
165 if ((fp
= fopen(argv
[1], "rb")) == NULL
)
171 for (i
= 0, encoding
= CUPS_AUTO_ENCODING
;
172 i
< (int)(sizeof(lang_encodings
) / sizeof(lang_encodings
[0]));
174 if (!strcasecmp(lang_encodings
[i
], argv
[2]))
176 encoding
= (cups_encoding_t
)i
;
180 if (encoding
== CUPS_AUTO_ENCODING
)
182 fprintf(stderr
, "%s: Unknown character set!\n", argv
[2]);
186 while (fgets(line
, sizeof(line
), fp
))
188 if (cupsCharsetToUTF8(utf8dest
, line
, sizeof(utf8dest
), encoding
) < 0)
190 fprintf(stderr
, "%s: Unable to convert line: %s", argv
[1], line
);
194 fputs((char *)utf8dest
, stdout
);
202 * Make sure we have a symbolic link from the data directory to a
203 * "charmaps" directory, and then point the library at it...
206 if (access("charmaps", 0))
207 symlink("../data", "charmaps");
209 putenv("CUPS_DATADIR=.");
212 * Start with some conversion tests from a UTF-8 test file.
217 if ((fp
= fopen("utf8demo.txt", "rb")) == NULL
)
219 perror("utf8demo.txt");
227 fputs("cupsUTF8ToUTF32 of utfdemo.txt: ", stdout
);
229 for (count
= 0, status
= 0; fgets(line
, sizeof(line
), fp
);)
233 if (cupsUTF8ToUTF32(utf32dest
, (cups_utf8_t
*)line
, 1024) < 0)
235 printf("FAIL (UTF-8 to UTF-32 on line %d)\n", count
);
246 * cupsUTF8ToCharset(CUPS_EUC_JP)
249 fputs("cupsUTF8ToCharset(CUPS_EUC_JP) of utfdemo.txt: ", stdout
);
253 for (count
= 0, status
= 0; fgets(line
, sizeof(line
), fp
);)
257 len
= cupsUTF8ToCharset(legdest
, (cups_utf8_t
*)line
, 1024, CUPS_EUC_JP
);
260 printf("FAIL (UTF-8 to EUC-JP on line %d)\n", count
);
273 * Test charmap load for ISO-8859-1...
276 fputs("_cupsCharmapGet(CUPS_ISO8859_1): ", stdout
);
278 if (!_cupsCharmapGet(CUPS_ISO8859_1
))
287 * Test charmap load for Windows-932 (Shift-JIS)...
290 fputs("_cupsCharmapGet(CUPS_WINDOWS_932): ", stdout
);
292 if (!_cupsCharmapGet(CUPS_WINDOWS_932
))
301 * Test VBCS charmap load for EUC-JP...
304 fputs("_cupsCharmapGet(CUPS_EUC_JP): ", stdout
);
306 if (!_cupsCharmapGet(CUPS_EUC_JP
))
315 * Test VBCS charmap load for EUC-TW...
318 fputs("_cupsCharmapGet(CUPS_EUC_TW): ", stdout
);
320 if (!_cupsCharmapGet(CUPS_EUC_TW
))
329 * Test UTF-8 to legacy charset (ISO 8859-1)...
332 fputs("cupsUTF8ToCharset(CUPS_ISO8859_1): ", stdout
);
336 len
= cupsUTF8ToCharset(legdest
, utf8latin
, 1024, CUPS_ISO8859_1
);
339 printf("FAIL (len=%d)\n", len
);
349 fputs("cupsCharsetToUTF8(CUPS_ISO8859_1): ", stdout
);
351 strcpy(legsrc
, legdest
);
353 len
= cupsCharsetToUTF8(utf8dest
, legsrc
, 1024, CUPS_ISO8859_1
);
354 if (len
!= strlen((char *)utf8latin
))
356 printf("FAIL (len=%d, expected %d)\n", len
, (int)strlen((char *)utf8latin
));
357 print_utf8(" utf8latin", utf8latin
);
358 print_utf8(" utf8dest", utf8dest
);
361 else if (memcmp(utf8latin
, utf8dest
, len
))
363 puts("FAIL (results do not match)");
364 print_utf8(" utf8latin", utf8latin
);
365 print_utf8(" utf8dest", utf8dest
);
368 else if (cupsUTF8ToCharset(legdest
, utf8repla
, 1024, CUPS_ISO8859_1
) < 0)
370 puts("FAIL (replacement characters do not work!)");
377 * Test UTF-8 to/from legacy charset (ISO 8859-7)...
380 fputs("cupsUTF8ToCharset(CUPS_ISO8859_7): ", stdout
);
382 if (cupsUTF8ToCharset(legdest
, utf8greek
, 1024, CUPS_ISO8859_7
) < 0)
389 for (legptr
= legdest
; *legptr
&& *legptr
!= '?'; legptr
++);
393 puts("FAIL (unknown character)");
400 fputs("cupsCharsetToUTF8(CUPS_ISO8859_7): ", stdout
);
402 strcpy(legsrc
, legdest
);
404 len
= cupsCharsetToUTF8(utf8dest
, legsrc
, 1024, CUPS_ISO8859_7
);
405 if (len
!= strlen((char *)utf8greek
))
407 printf("FAIL (len=%d, expected %d)\n", len
, (int)strlen((char *)utf8greek
));
408 print_utf8(" utf8greek", utf8greek
);
409 print_utf8(" utf8dest", utf8dest
);
412 else if (memcmp(utf8greek
, utf8dest
, len
))
414 puts("FAIL (results do not match)");
415 print_utf8(" utf8greek", utf8greek
);
416 print_utf8(" utf8dest", utf8dest
);
423 * Test UTF-8 to/from legacy charset (Windows 932)...
426 fputs("cupsUTF8ToCharset(CUPS_WINDOWS_932): ", stdout
);
428 if (cupsUTF8ToCharset(legdest
, utf8japan
, 1024, CUPS_WINDOWS_932
) < 0)
435 for (legptr
= legdest
; *legptr
&& *legptr
!= '?'; legptr
++);
439 puts("FAIL (unknown character)");
446 fputs("cupsCharsetToUTF8(CUPS_WINDOWS_932): ", stdout
);
448 strcpy(legsrc
, legdest
);
450 len
= cupsCharsetToUTF8(utf8dest
, legsrc
, 1024, CUPS_WINDOWS_932
);
451 if (len
!= strlen((char *)utf8japan
))
453 printf("FAIL (len=%d, expected %d)\n", len
, (int)strlen((char *)utf8japan
));
454 print_utf8(" utf8japan", utf8japan
);
455 print_utf8(" utf8dest", utf8dest
);
458 else if (memcmp(utf8japan
, utf8dest
, len
))
460 puts("FAIL (results do not match)");
461 print_utf8(" utf8japan", utf8japan
);
462 print_utf8(" utf8dest", utf8dest
);
469 * Test UTF-8 to/from legacy charset (EUC-JP)...
472 fputs("cupsUTF8ToCharset(CUPS_EUC_JP): ", stdout
);
474 if (cupsUTF8ToCharset(legdest
, utf8japan
, 1024, CUPS_EUC_JP
) < 0)
481 for (legptr
= legdest
; *legptr
&& *legptr
!= '?'; legptr
++);
485 puts("FAIL (unknown character)");
492 fputs("cupsCharsetToUTF8(CUPS_EUC_JP): ", stdout
);
494 strcpy(legsrc
, legdest
);
496 len
= cupsCharsetToUTF8(utf8dest
, legsrc
, 1024, CUPS_EUC_JP
);
497 if (len
!= strlen((char *)utf8japan
))
499 printf("FAIL (len=%d, expected %d)\n", len
, (int)strlen((char *)utf8japan
));
500 print_utf8(" utf8japan", utf8japan
);
501 print_utf8(" utf8dest", utf8dest
);
504 else if (memcmp(utf8japan
, utf8dest
, len
))
506 puts("FAIL (results do not match)");
507 print_utf8(" utf8japan", utf8japan
);
508 print_utf8(" utf8dest", utf8dest
);
515 * Test UTF-8 to/from legacy charset (Windows 950)...
518 fputs("cupsUTF8ToCharset(CUPS_WINDOWS_950): ", stdout
);
520 if (cupsUTF8ToCharset(legdest
, utf8taiwan
, 1024, CUPS_WINDOWS_950
) < 0)
527 for (legptr
= legdest
; *legptr
&& *legptr
!= '?'; legptr
++);
531 puts("FAIL (unknown character)");
538 fputs("cupsCharsetToUTF8(CUPS_WINDOWS_950): ", stdout
);
540 strcpy(legsrc
, legdest
);
542 len
= cupsCharsetToUTF8(utf8dest
, legsrc
, 1024, CUPS_WINDOWS_950
);
543 if (len
!= strlen((char *)utf8taiwan
))
545 printf("FAIL (len=%d, expected %d)\n", len
, (int)strlen((char *)utf8taiwan
));
546 print_utf8(" utf8taiwan", utf8taiwan
);
547 print_utf8(" utf8dest", utf8dest
);
550 else if (memcmp(utf8taiwan
, utf8dest
, len
))
552 puts("FAIL (results do not match)");
553 print_utf8(" utf8taiwan", utf8taiwan
);
554 print_utf8(" utf8dest", utf8dest
);
561 * Test UTF-8 to/from legacy charset (EUC-TW)...
564 fputs("cupsUTF8ToCharset(CUPS_EUC_TW): ", stdout
);
566 if (cupsUTF8ToCharset(legdest
, utf8taiwan
, 1024, CUPS_EUC_TW
) < 0)
573 for (legptr
= legdest
; *legptr
&& *legptr
!= '?'; legptr
++);
577 puts("FAIL (unknown character)");
584 fputs("cupsCharsetToUTF8(CUPS_EUC_TW): ", stdout
);
586 strcpy(legsrc
, legdest
);
588 len
= cupsCharsetToUTF8(utf8dest
, legsrc
, 1024, CUPS_EUC_TW
);
589 if (len
!= strlen((char *)utf8taiwan
))
591 printf("FAIL (len=%d, expected %d)\n", len
, (int)strlen((char *)utf8taiwan
));
592 print_utf8(" utf8taiwan", utf8taiwan
);
593 print_utf8(" utf8dest", utf8dest
);
596 else if (memcmp(utf8taiwan
, utf8dest
, len
))
598 puts("FAIL (results do not match)");
599 print_utf8(" utf8taiwan", utf8taiwan
);
600 print_utf8(" utf8dest", utf8dest
);
608 * Test UTF-8 (16-bit) to UTF-32 (w/ BOM)...
611 printf("\ntesti18n: Testing UTF-8 to UTF-32 (w/ BOM)...\n");
612 len
= cupsUTF8ToUTF32(utf32dest
, utf8good
, 1024);
617 print_utf8(" utf8good ", utf8good
);
618 print_utf32(" utf32dest", utf32dest
);
620 memcpy (utf32src
, utf32dest
, (len
+ 1) * sizeof(cups_utf32_t
));
621 len
= cupsUTF32ToUTF8(utf8dest
, utf32src
, 1024);
624 if (len
!= strlen ((char *) utf8good
))
626 if (memcmp(utf8good
, utf8dest
, len
) != 0)
630 * Test invalid UTF-8 (16-bit) to UTF-32 (w/ BOM)...
633 printf("\ntesti18n: Testing UTF-8 bad 16-bit source string...\n");
634 len
= cupsUTF8ToUTF32(utf32dest
, utf8bad
, 1024);
638 print_utf8(" utf8bad ", utf8bad
);
641 * Test _cupsCharmapFlush()...
644 printf("\ntesti18n: Testing _cupsCharmapFlush()...\n");
654 * 'print_utf8()' - Print UTF-8 string with (optional) message.
658 print_utf8(const char *msg
, /* I - Message String */
659 const cups_utf8_t
*src
) /* I - UTF-8 Source String */
661 const char *prefix
; /* Prefix string */
667 for (prefix
= " "; *src
; src
++)
669 printf("%s%02x", prefix
, *src
);
671 if ((src
[0] & 0x80) && (src
[1] & 0x80))
682 * End of "$Id: testi18n.c 7560 2008-05-13 06:34:04Z mike $"