]>
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 Common UNIX Printing System (CUPS).
6 * Copyright 2007-2009 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...
41 static const char * const lang_encodings
[] =
42 { /* Encoding strings */
43 "us-ascii", "iso-8859-1",
44 "iso-8859-2", "iso-8859-3",
45 "iso-8859-4", "iso-8859-5",
46 "iso-8859-6", "iso-8859-7",
47 "iso-8859-8", "iso-8859-9",
48 "iso-8859-10", "utf-8",
49 "iso-8859-13", "iso-8859-14",
50 "iso-8859-15", "windows-874",
51 "windows-1250", "windows-1251",
52 "windows-1252", "windows-1253",
53 "windows-1254", "windows-1255",
54 "windows-1256", "windows-1257",
55 "windows-1258", "koi8-r",
56 "koi8-u", "iso-8859-11",
57 "iso-8859-16", "mac-roman",
75 "windows-932", "windows-936",
76 "windows-949", "windows-950",
77 "windows-1361", "unknown",
100 "unknown", "unknown",
101 "unknown", "unknown",
102 "unknown", "unknown",
103 "unknown", "unknown",
104 "unknown", "unknown",
105 "unknown", "unknown",
106 "unknown", "unknown",
117 static void print_utf8(const char *msg
, const cups_utf8_t
*src
);
121 * 'main()' - Main entry for internationalization test module.
124 int /* O - Exit code */
125 main(int argc
, /* I - Argument Count */
126 char *argv
[]) /* I - Arguments */
128 FILE *fp
; /* File pointer */
129 int count
; /* File line counter */
130 int status
, /* Status of current test */
131 errors
; /* Error count */
132 char line
[1024]; /* File line source string */
133 int len
; /* Length (count) of string */
134 char legsrc
[1024], /* Legacy source string */
135 legdest
[1024], /* Legacy destination string */
136 *legptr
; /* Pointer into legacy string */
137 cups_utf8_t utf8latin
[] = /* UTF-8 Latin-1 source */
138 { 0x41, 0x20, 0x21, 0x3D, 0x20, 0xC3, 0x84, 0x2E, 0x00 };
139 /* "A != <A WITH DIAERESIS>." - use ISO 8859-1 */
140 cups_utf8_t utf8repla
[] = /* UTF-8 Latin-1 replacement */
141 { 0x41, 0x20, 0xE2, 0x89, 0xA2, 0x20, 0xC3, 0x84, 0x2E, 0x00 };
142 /* "A <NOT IDENTICAL TO> <A WITH DIAERESIS>." */
143 cups_utf8_t utf8greek
[] = /* UTF-8 Greek source string */
144 { 0x41, 0x20, 0x21, 0x3D, 0x20, 0xCE, 0x91, 0x2E, 0x00 };
145 /* "A != <ALPHA>." - use ISO 8859-7 */
146 cups_utf8_t utf8japan
[] = /* UTF-8 Japanese source */
147 { 0x41, 0x20, 0x21, 0x3D, 0x20, 0xEE, 0x9C, 0x80, 0x2E, 0x00 };
148 /* "A != <PRIVATE U+E700>." - use Windows 932 or EUC-JP */
149 cups_utf8_t utf8taiwan
[] = /* UTF-8 Chinese source */
150 { 0x41, 0x20, 0x21, 0x3D, 0x20, 0xE4, 0xB9, 0x82, 0x2E, 0x00 };
151 /* "A != <CJK U+4E42>." - use Windows 950 (Big5) or EUC-TW */
152 cups_utf8_t utf8dest
[1024]; /* UTF-8 destination string */
153 cups_utf32_t utf32dest
[1024]; /* UTF-32 destination string */
158 int i
; /* Looping var */
159 cups_encoding_t encoding
; /* Source encoding */
164 puts("Usage: ./testi18n [filename charset]");
168 if ((fp
= fopen(argv
[1], "rb")) == NULL
)
174 for (i
= 0, encoding
= CUPS_AUTO_ENCODING
;
175 i
< (int)(sizeof(lang_encodings
) / sizeof(lang_encodings
[0]));
177 if (!strcasecmp(lang_encodings
[i
], argv
[2]))
179 encoding
= (cups_encoding_t
)i
;
183 if (encoding
== CUPS_AUTO_ENCODING
)
185 fprintf(stderr
, "%s: Unknown character set!\n", argv
[2]);
189 while (fgets(line
, sizeof(line
), fp
))
191 if (cupsCharsetToUTF8(utf8dest
, line
, sizeof(utf8dest
), encoding
) < 0)
193 fprintf(stderr
, "%s: Unable to convert line: %s", argv
[1], line
);
197 fputs((char *)utf8dest
, stdout
);
205 * Make sure we have a symbolic link from the data directory to a
206 * "charmaps" directory, and then point the library at it...
209 if (access("charmaps", 0))
210 symlink("../data", "charmaps");
212 putenv("CUPS_DATADIR=.");
215 * Start with some conversion tests from a UTF-8 test file.
220 if ((fp
= fopen("utf8demo.txt", "rb")) == NULL
)
222 perror("utf8demo.txt");
230 fputs("cupsUTF8ToUTF32 of utfdemo.txt: ", stdout
);
232 for (count
= 0, status
= 0; fgets(line
, sizeof(line
), fp
);)
236 if (cupsUTF8ToUTF32(utf32dest
, (cups_utf8_t
*)line
, 1024) < 0)
238 printf("FAIL (UTF-8 to UTF-32 on line %d)\n", count
);
249 * cupsUTF8ToCharset(CUPS_EUC_JP)
252 fputs("cupsUTF8ToCharset(CUPS_EUC_JP) of utfdemo.txt: ", stdout
);
256 for (count
= 0, status
= 0; fgets(line
, sizeof(line
), fp
);)
260 len
= cupsUTF8ToCharset(legdest
, (cups_utf8_t
*)line
, 1024, CUPS_EUC_JP
);
263 printf("FAIL (UTF-8 to EUC-JP on line %d)\n", count
);
276 * Test charmap load for ISO-8859-1...
279 fputs("_cupsCharmapGet(CUPS_ISO8859_1): ", stdout
);
281 if (!_cupsCharmapGet(CUPS_ISO8859_1
))
290 * Test charmap load for Windows-932 (Shift-JIS)...
293 fputs("_cupsCharmapGet(CUPS_WINDOWS_932): ", stdout
);
295 if (!_cupsCharmapGet(CUPS_WINDOWS_932
))
304 * Test VBCS charmap load for EUC-JP...
307 fputs("_cupsCharmapGet(CUPS_EUC_JP): ", stdout
);
309 if (!_cupsCharmapGet(CUPS_EUC_JP
))
318 * Test VBCS charmap load for EUC-TW...
321 fputs("_cupsCharmapGet(CUPS_EUC_TW): ", stdout
);
323 if (!_cupsCharmapGet(CUPS_EUC_TW
))
332 * Test UTF-8 to legacy charset (ISO 8859-1)...
335 fputs("cupsUTF8ToCharset(CUPS_ISO8859_1): ", stdout
);
339 len
= cupsUTF8ToCharset(legdest
, utf8latin
, 1024, CUPS_ISO8859_1
);
342 printf("FAIL (len=%d)\n", len
);
352 fputs("cupsCharsetToUTF8(CUPS_ISO8859_1): ", stdout
);
354 strcpy(legsrc
, legdest
);
356 len
= cupsCharsetToUTF8(utf8dest
, legsrc
, 1024, CUPS_ISO8859_1
);
357 if (len
!= strlen((char *)utf8latin
))
359 printf("FAIL (len=%d, expected %d)\n", len
, (int)strlen((char *)utf8latin
));
360 print_utf8(" utf8latin", utf8latin
);
361 print_utf8(" utf8dest", utf8dest
);
364 else if (memcmp(utf8latin
, utf8dest
, len
))
366 puts("FAIL (results do not match)");
367 print_utf8(" utf8latin", utf8latin
);
368 print_utf8(" utf8dest", utf8dest
);
371 else if (cupsUTF8ToCharset(legdest
, utf8repla
, 1024, CUPS_ISO8859_1
) < 0)
373 puts("FAIL (replacement characters do not work!)");
380 * Test UTF-8 to/from legacy charset (ISO 8859-7)...
383 fputs("cupsUTF8ToCharset(CUPS_ISO8859_7): ", stdout
);
385 if (cupsUTF8ToCharset(legdest
, utf8greek
, 1024, CUPS_ISO8859_7
) < 0)
392 for (legptr
= legdest
; *legptr
&& *legptr
!= '?'; legptr
++);
396 puts("FAIL (unknown character)");
403 fputs("cupsCharsetToUTF8(CUPS_ISO8859_7): ", stdout
);
405 strcpy(legsrc
, legdest
);
407 len
= cupsCharsetToUTF8(utf8dest
, legsrc
, 1024, CUPS_ISO8859_7
);
408 if (len
!= strlen((char *)utf8greek
))
410 printf("FAIL (len=%d, expected %d)\n", len
, (int)strlen((char *)utf8greek
));
411 print_utf8(" utf8greek", utf8greek
);
412 print_utf8(" utf8dest", utf8dest
);
415 else if (memcmp(utf8greek
, utf8dest
, len
))
417 puts("FAIL (results do not match)");
418 print_utf8(" utf8greek", utf8greek
);
419 print_utf8(" utf8dest", utf8dest
);
426 * Test UTF-8 to/from legacy charset (Windows 932)...
429 fputs("cupsUTF8ToCharset(CUPS_WINDOWS_932): ", stdout
);
431 if (cupsUTF8ToCharset(legdest
, utf8japan
, 1024, CUPS_WINDOWS_932
) < 0)
438 for (legptr
= legdest
; *legptr
&& *legptr
!= '?'; legptr
++);
442 puts("FAIL (unknown character)");
449 fputs("cupsCharsetToUTF8(CUPS_WINDOWS_932): ", stdout
);
451 strcpy(legsrc
, legdest
);
453 len
= cupsCharsetToUTF8(utf8dest
, legsrc
, 1024, CUPS_WINDOWS_932
);
454 if (len
!= strlen((char *)utf8japan
))
456 printf("FAIL (len=%d, expected %d)\n", len
, (int)strlen((char *)utf8japan
));
457 print_utf8(" utf8japan", utf8japan
);
458 print_utf8(" utf8dest", utf8dest
);
461 else if (memcmp(utf8japan
, utf8dest
, len
))
463 puts("FAIL (results do not match)");
464 print_utf8(" utf8japan", utf8japan
);
465 print_utf8(" utf8dest", utf8dest
);
472 * Test UTF-8 to/from legacy charset (EUC-JP)...
475 fputs("cupsUTF8ToCharset(CUPS_EUC_JP): ", stdout
);
477 if (cupsUTF8ToCharset(legdest
, utf8japan
, 1024, CUPS_EUC_JP
) < 0)
484 for (legptr
= legdest
; *legptr
&& *legptr
!= '?'; legptr
++);
488 puts("FAIL (unknown character)");
495 fputs("cupsCharsetToUTF8(CUPS_EUC_JP): ", stdout
);
497 strcpy(legsrc
, legdest
);
499 len
= cupsCharsetToUTF8(utf8dest
, legsrc
, 1024, CUPS_EUC_JP
);
500 if (len
!= strlen((char *)utf8japan
))
502 printf("FAIL (len=%d, expected %d)\n", len
, (int)strlen((char *)utf8japan
));
503 print_utf8(" utf8japan", utf8japan
);
504 print_utf8(" utf8dest", utf8dest
);
507 else if (memcmp(utf8japan
, utf8dest
, len
))
509 puts("FAIL (results do not match)");
510 print_utf8(" utf8japan", utf8japan
);
511 print_utf8(" utf8dest", utf8dest
);
518 * Test UTF-8 to/from legacy charset (Windows 950)...
521 fputs("cupsUTF8ToCharset(CUPS_WINDOWS_950): ", stdout
);
523 if (cupsUTF8ToCharset(legdest
, utf8taiwan
, 1024, CUPS_WINDOWS_950
) < 0)
530 for (legptr
= legdest
; *legptr
&& *legptr
!= '?'; legptr
++);
534 puts("FAIL (unknown character)");
541 fputs("cupsCharsetToUTF8(CUPS_WINDOWS_950): ", stdout
);
543 strcpy(legsrc
, legdest
);
545 len
= cupsCharsetToUTF8(utf8dest
, legsrc
, 1024, CUPS_WINDOWS_950
);
546 if (len
!= strlen((char *)utf8taiwan
))
548 printf("FAIL (len=%d, expected %d)\n", len
, (int)strlen((char *)utf8taiwan
));
549 print_utf8(" utf8taiwan", utf8taiwan
);
550 print_utf8(" utf8dest", utf8dest
);
553 else if (memcmp(utf8taiwan
, utf8dest
, len
))
555 puts("FAIL (results do not match)");
556 print_utf8(" utf8taiwan", utf8taiwan
);
557 print_utf8(" utf8dest", utf8dest
);
564 * Test UTF-8 to/from legacy charset (EUC-TW)...
567 fputs("cupsUTF8ToCharset(CUPS_EUC_TW): ", stdout
);
569 if (cupsUTF8ToCharset(legdest
, utf8taiwan
, 1024, CUPS_EUC_TW
) < 0)
576 for (legptr
= legdest
; *legptr
&& *legptr
!= '?'; legptr
++);
580 puts("FAIL (unknown character)");
587 fputs("cupsCharsetToUTF8(CUPS_EUC_TW): ", stdout
);
589 strcpy(legsrc
, legdest
);
591 len
= cupsCharsetToUTF8(utf8dest
, legsrc
, 1024, CUPS_EUC_TW
);
592 if (len
!= strlen((char *)utf8taiwan
))
594 printf("FAIL (len=%d, expected %d)\n", len
, (int)strlen((char *)utf8taiwan
));
595 print_utf8(" utf8taiwan", utf8taiwan
);
596 print_utf8(" utf8dest", utf8dest
);
599 else if (memcmp(utf8taiwan
, utf8dest
, len
))
601 puts("FAIL (results do not match)");
602 print_utf8(" utf8taiwan", utf8taiwan
);
603 print_utf8(" utf8dest", utf8dest
);
611 * Test UTF-8 (16-bit) to UTF-32 (w/ BOM)...
614 printf("\ntesti18n: Testing UTF-8 to UTF-32 (w/ BOM)...\n");
615 len
= cupsUTF8ToUTF32(utf32dest
, utf8good
, 1024);
620 print_utf8(" utf8good ", utf8good
);
621 print_utf32(" utf32dest", utf32dest
);
623 memcpy (utf32src
, utf32dest
, (len
+ 1) * sizeof(cups_utf32_t
));
624 len
= cupsUTF32ToUTF8(utf8dest
, utf32src
, 1024);
627 if (len
!= strlen ((char *) utf8good
))
629 if (memcmp(utf8good
, utf8dest
, len
) != 0)
633 * Test invalid UTF-8 (16-bit) to UTF-32 (w/ BOM)...
636 printf("\ntesti18n: Testing UTF-8 bad 16-bit source string...\n");
637 len
= cupsUTF8ToUTF32(utf32dest
, utf8bad
, 1024);
641 print_utf8(" utf8bad ", utf8bad
);
644 * Test _cupsCharmapFlush()...
647 printf("\ntesti18n: Testing _cupsCharmapFlush()...\n");
657 * 'print_utf8()' - Print UTF-8 string with (optional) message.
661 print_utf8(const char *msg
, /* I - Message String */
662 const cups_utf8_t
*src
) /* I - UTF-8 Source String */
664 const char *prefix
; /* Prefix string */
670 for (prefix
= " "; *src
; src
++)
672 printf("%s%02x", prefix
, *src
);
674 if ((src
[0] & 0x80) && (src
[1] & 0x80))
685 * End of "$Id: testi18n.c 7560 2008-05-13 06:34:04Z mike $"