]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/testi18n.c
a88f1e1f4544508f47068f8f96837ff05f4f6ec2
2 * Internationalization test for CUPS.
4 * Copyright 2007-2014 by Apple Inc.
5 * Copyright 1997-2006 by Easy Software Products.
7 * These coded instructions, statements, and computer programs are the
8 * property of Apple Inc. and are protected by Federal copyright
9 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
10 * which should have been included with this file. If this file is
11 * file is missing or damaged, see the license at "http://www.cups.org/".
13 * This file is subject to the Apple OS-Developed Software exception.
17 * Include necessary headers...
20 #include "string-private.h"
21 #include "language-private.h"
31 static const char * const lang_encodings
[] =
32 { /* Encoding strings */
33 "us-ascii", "iso-8859-1",
34 "iso-8859-2", "iso-8859-3",
35 "iso-8859-4", "iso-8859-5",
36 "iso-8859-6", "iso-8859-7",
37 "iso-8859-8", "iso-8859-9",
38 "iso-8859-10", "utf-8",
39 "iso-8859-13", "iso-8859-14",
40 "iso-8859-15", "windows-874",
41 "windows-1250", "windows-1251",
42 "windows-1252", "windows-1253",
43 "windows-1254", "windows-1255",
44 "windows-1256", "windows-1257",
45 "windows-1258", "koi8-r",
46 "koi8-u", "iso-8859-11",
47 "iso-8859-16", "mac-roman",
65 "windows-932", "windows-936",
66 "windows-949", "windows-950",
67 "windows-1361", "unknown",
107 static void print_utf8(const char *msg
, const cups_utf8_t
*src
);
111 * 'main()' - Main entry for internationalization test module.
114 int /* O - Exit code */
115 main(int argc
, /* I - Argument Count */
116 char *argv
[]) /* I - Arguments */
118 FILE *fp
; /* File pointer */
119 int count
; /* File line counter */
120 int status
, /* Status of current test */
121 errors
; /* Error count */
122 char line
[1024]; /* File line source string */
123 int len
; /* Length (count) of string */
124 char legsrc
[1024], /* Legacy source string */
125 legdest
[1024], /* Legacy destination string */
126 *legptr
; /* Pointer into legacy string */
127 cups_utf8_t utf8latin
[] = /* UTF-8 Latin-1 source */
128 { 0x41, 0x20, 0x21, 0x3D, 0x20, 0xC3, 0x84, 0x2E, 0x00 };
129 /* "A != <A WITH DIAERESIS>." - use ISO 8859-1 */
130 cups_utf8_t utf8repla
[] = /* UTF-8 Latin-1 replacement */
131 { 0x41, 0x20, 0xE2, 0x89, 0xA2, 0x20, 0xC3, 0x84, 0x2E, 0x00 };
132 /* "A <NOT IDENTICAL TO> <A WITH DIAERESIS>." */
133 cups_utf8_t utf8greek
[] = /* UTF-8 Greek source string */
134 { 0x41, 0x20, 0x21, 0x3D, 0x20, 0xCE, 0x91, 0x2E, 0x00 };
135 /* "A != <ALPHA>." - use ISO 8859-7 */
136 cups_utf8_t utf8japan
[] = /* UTF-8 Japanese source */
137 { 0x41, 0x20, 0x21, 0x3D, 0x20, 0xEE, 0x9C, 0x80, 0x2E, 0x00 };
138 /* "A != <PRIVATE U+E700>." - use Windows 932 or EUC-JP */
139 cups_utf8_t utf8taiwan
[] = /* UTF-8 Chinese source */
140 { 0x41, 0x20, 0x21, 0x3D, 0x20, 0xE4, 0xB9, 0x82, 0x2E, 0x00 };
141 /* "A != <CJK U+4E42>." - use Windows 950 (Big5) or EUC-TW */
142 cups_utf8_t utf8dest
[1024]; /* UTF-8 destination string */
143 cups_utf32_t utf32dest
[1024]; /* UTF-32 destination string */
148 int i
; /* Looping var */
149 cups_encoding_t encoding
; /* Source encoding */
154 puts("Usage: ./testi18n [filename charset]");
158 if ((fp
= fopen(argv
[1], "rb")) == NULL
)
164 for (i
= 0, encoding
= CUPS_AUTO_ENCODING
;
165 i
< (int)(sizeof(lang_encodings
) / sizeof(lang_encodings
[0]));
167 if (!_cups_strcasecmp(lang_encodings
[i
], argv
[2]))
169 encoding
= (cups_encoding_t
)i
;
173 if (encoding
== CUPS_AUTO_ENCODING
)
175 fprintf(stderr
, "%s: Unknown character set!\n", argv
[2]);
179 while (fgets(line
, sizeof(line
), fp
))
181 if (cupsCharsetToUTF8(utf8dest
, line
, sizeof(utf8dest
), encoding
) < 0)
183 fprintf(stderr
, "%s: Unable to convert line: %s", argv
[1], line
);
187 fputs((char *)utf8dest
, stdout
);
195 * Start with some conversion tests from a UTF-8 test file.
200 if ((fp
= fopen("utf8demo.txt", "rb")) == NULL
)
202 perror("utf8demo.txt");
210 fputs("cupsUTF8ToUTF32 of utfdemo.txt: ", stdout
);
212 for (count
= 0, status
= 0; fgets(line
, sizeof(line
), fp
);)
216 if (cupsUTF8ToUTF32(utf32dest
, (cups_utf8_t
*)line
, 1024) < 0)
218 printf("FAIL (UTF-8 to UTF-32 on line %d)\n", count
);
229 * cupsUTF8ToCharset(CUPS_EUC_JP)
232 fputs("cupsUTF8ToCharset(CUPS_EUC_JP) of utfdemo.txt: ", stdout
);
236 for (count
= 0, status
= 0; fgets(line
, sizeof(line
), fp
);)
240 len
= cupsUTF8ToCharset(legdest
, (cups_utf8_t
*)line
, 1024, CUPS_EUC_JP
);
243 printf("FAIL (UTF-8 to EUC-JP on line %d)\n", count
);
256 * Test UTF-8 to legacy charset (ISO 8859-1)...
259 fputs("cupsUTF8ToCharset(CUPS_ISO8859_1): ", stdout
);
263 len
= cupsUTF8ToCharset(legdest
, utf8latin
, 1024, CUPS_ISO8859_1
);
266 printf("FAIL (len=%d)\n", len
);
276 fputs("cupsCharsetToUTF8(CUPS_ISO8859_1): ", stdout
);
278 strlcpy(legsrc
, legdest
, sizeof(legsrc
));
280 len
= cupsCharsetToUTF8(utf8dest
, legsrc
, 1024, CUPS_ISO8859_1
);
281 if ((size_t)len
!= strlen((char *)utf8latin
))
283 printf("FAIL (len=%d, expected %d)\n", len
, (int)strlen((char *)utf8latin
));
284 print_utf8(" utf8latin", utf8latin
);
285 print_utf8(" utf8dest", utf8dest
);
288 else if (memcmp(utf8latin
, utf8dest
, (size_t)len
))
290 puts("FAIL (results do not match)");
291 print_utf8(" utf8latin", utf8latin
);
292 print_utf8(" utf8dest", utf8dest
);
295 else if (cupsUTF8ToCharset(legdest
, utf8repla
, 1024, CUPS_ISO8859_1
) < 0)
297 puts("FAIL (replacement characters do not work!)");
304 * Test UTF-8 to/from legacy charset (ISO 8859-7)...
307 fputs("cupsUTF8ToCharset(CUPS_ISO8859_7): ", stdout
);
309 if (cupsUTF8ToCharset(legdest
, utf8greek
, 1024, CUPS_ISO8859_7
) < 0)
316 for (legptr
= legdest
; *legptr
&& *legptr
!= '?'; legptr
++);
320 puts("FAIL (unknown character)");
327 fputs("cupsCharsetToUTF8(CUPS_ISO8859_7): ", stdout
);
329 strlcpy(legsrc
, legdest
, sizeof(legsrc
));
331 len
= cupsCharsetToUTF8(utf8dest
, legsrc
, 1024, CUPS_ISO8859_7
);
332 if ((size_t)len
!= strlen((char *)utf8greek
))
334 printf("FAIL (len=%d, expected %d)\n", len
, (int)strlen((char *)utf8greek
));
335 print_utf8(" utf8greek", utf8greek
);
336 print_utf8(" utf8dest", utf8dest
);
339 else if (memcmp(utf8greek
, utf8dest
, (size_t)len
))
341 puts("FAIL (results do not match)");
342 print_utf8(" utf8greek", utf8greek
);
343 print_utf8(" utf8dest", utf8dest
);
350 * Test UTF-8 to/from legacy charset (Windows 932)...
353 fputs("cupsUTF8ToCharset(CUPS_WINDOWS_932): ", stdout
);
355 if (cupsUTF8ToCharset(legdest
, utf8japan
, 1024, CUPS_WINDOWS_932
) < 0)
362 for (legptr
= legdest
; *legptr
&& *legptr
!= '?'; legptr
++);
366 puts("FAIL (unknown character)");
373 fputs("cupsCharsetToUTF8(CUPS_WINDOWS_932): ", stdout
);
375 strlcpy(legsrc
, legdest
, sizeof(legsrc
));
377 len
= cupsCharsetToUTF8(utf8dest
, legsrc
, 1024, CUPS_WINDOWS_932
);
378 if ((size_t)len
!= strlen((char *)utf8japan
))
380 printf("FAIL (len=%d, expected %d)\n", len
, (int)strlen((char *)utf8japan
));
381 print_utf8(" utf8japan", utf8japan
);
382 print_utf8(" utf8dest", utf8dest
);
385 else if (memcmp(utf8japan
, utf8dest
, (size_t)len
))
387 puts("FAIL (results do not match)");
388 print_utf8(" utf8japan", utf8japan
);
389 print_utf8(" utf8dest", utf8dest
);
396 * Test UTF-8 to/from legacy charset (EUC-JP)...
399 fputs("cupsUTF8ToCharset(CUPS_EUC_JP): ", stdout
);
401 if (cupsUTF8ToCharset(legdest
, utf8japan
, 1024, CUPS_EUC_JP
) < 0)
408 for (legptr
= legdest
; *legptr
&& *legptr
!= '?'; legptr
++);
412 puts("FAIL (unknown character)");
420 fputs("cupsCharsetToUTF8(CUPS_EUC_JP): ", stdout
);
422 strlcpy(legsrc
, legdest
, sizeof(legsrc
));
424 len
= cupsCharsetToUTF8(utf8dest
, legsrc
, 1024, CUPS_EUC_JP
);
425 if ((size_t)len
!= strlen((char *)utf8japan
))
427 printf("FAIL (len=%d, expected %d)\n", len
, (int)strlen((char *)utf8japan
));
428 print_utf8(" utf8japan", utf8japan
);
429 print_utf8(" utf8dest", utf8dest
);
432 else if (memcmp(utf8japan
, utf8dest
, (size_t)len
))
434 puts("FAIL (results do not match)");
435 print_utf8(" utf8japan", utf8japan
);
436 print_utf8(" utf8dest", utf8dest
);
441 #endif /* !__linux */
444 * Test UTF-8 to/from legacy charset (Windows 950)...
447 fputs("cupsUTF8ToCharset(CUPS_WINDOWS_950): ", stdout
);
449 if (cupsUTF8ToCharset(legdest
, utf8taiwan
, 1024, CUPS_WINDOWS_950
) < 0)
456 for (legptr
= legdest
; *legptr
&& *legptr
!= '?'; legptr
++);
460 puts("FAIL (unknown character)");
467 fputs("cupsCharsetToUTF8(CUPS_WINDOWS_950): ", stdout
);
469 strlcpy(legsrc
, legdest
, sizeof(legsrc
));
471 len
= cupsCharsetToUTF8(utf8dest
, legsrc
, 1024, CUPS_WINDOWS_950
);
472 if ((size_t)len
!= strlen((char *)utf8taiwan
))
474 printf("FAIL (len=%d, expected %d)\n", len
, (int)strlen((char *)utf8taiwan
));
475 print_utf8(" utf8taiwan", utf8taiwan
);
476 print_utf8(" utf8dest", utf8dest
);
479 else if (memcmp(utf8taiwan
, utf8dest
, (size_t)len
))
481 puts("FAIL (results do not match)");
482 print_utf8(" utf8taiwan", utf8taiwan
);
483 print_utf8(" utf8dest", utf8dest
);
490 * Test UTF-8 to/from legacy charset (EUC-TW)...
493 fputs("cupsUTF8ToCharset(CUPS_EUC_TW): ", stdout
);
495 if (cupsUTF8ToCharset(legdest
, utf8taiwan
, 1024, CUPS_EUC_TW
) < 0)
502 for (legptr
= legdest
; *legptr
&& *legptr
!= '?'; legptr
++);
506 puts("FAIL (unknown character)");
513 fputs("cupsCharsetToUTF8(CUPS_EUC_TW): ", stdout
);
515 strlcpy(legsrc
, legdest
, sizeof(legsrc
));
517 len
= cupsCharsetToUTF8(utf8dest
, legsrc
, 1024, CUPS_EUC_TW
);
518 if ((size_t)len
!= strlen((char *)utf8taiwan
))
520 printf("FAIL (len=%d, expected %d)\n", len
, (int)strlen((char *)utf8taiwan
));
521 print_utf8(" utf8taiwan", utf8taiwan
);
522 print_utf8(" utf8dest", utf8dest
);
525 else if (memcmp(utf8taiwan
, utf8dest
, (size_t)len
))
527 puts("FAIL (results do not match)");
528 print_utf8(" utf8taiwan", utf8taiwan
);
529 print_utf8(" utf8dest", utf8dest
);
537 * Test UTF-8 (16-bit) to UTF-32 (w/ BOM)...
540 printf("\ntesti18n: Testing UTF-8 to UTF-32 (w/ BOM)...\n");
541 len
= cupsUTF8ToUTF32(utf32dest
, utf8good
, 1024);
546 print_utf8(" utf8good ", utf8good
);
547 print_utf32(" utf32dest", utf32dest
);
549 memcpy(utf32src
, utf32dest
, (len
+ 1) * sizeof(cups_utf32_t
));
550 len
= cupsUTF32ToUTF8(utf8dest
, utf32src
, 1024);
553 if (len
!= strlen ((char *) utf8good
))
555 if (memcmp(utf8good
, utf8dest
, len
) != 0)
559 * Test invalid UTF-8 (16-bit) to UTF-32 (w/ BOM)...
562 printf("\ntesti18n: Testing UTF-8 bad 16-bit source string...\n");
563 len
= cupsUTF8ToUTF32(utf32dest
, utf8bad
, 1024);
567 print_utf8(" utf8bad ", utf8bad
);
570 * Test _cupsCharmapFlush()...
573 printf("\ntesti18n: Testing _cupsCharmapFlush()...\n");
583 * 'print_utf8()' - Print UTF-8 string with (optional) message.
587 print_utf8(const char *msg
, /* I - Message String */
588 const cups_utf8_t
*src
) /* I - UTF-8 Source String */
590 const char *prefix
; /* Prefix string */
596 for (prefix
= " "; *src
; src
++)
598 printf("%s%02x", prefix
, *src
);
600 if ((src
[0] & 0x80) && (src
[1] & 0x80))