]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/testi18n.c
67de108a0bf110db1bf611bc3de7cdfa7c571dd9
2 * Internationalization test for CUPS.
4 * Copyright © 2020-2024 by OpenPrinting.
5 * Copyright © 2007-2014 by Apple Inc.
6 * Copyright © 1997-2006 by Easy Software Products.
8 * Licensed under Apache License v2.0. See the file "LICENSE" for more
13 * Include necessary headers...
17 #include "string-private.h"
18 #include "language-private.h"
28 static const char * const lang_encodings
[] =
29 { /* Encoding strings */
30 "us-ascii", "iso-8859-1",
31 "iso-8859-2", "iso-8859-3",
32 "iso-8859-4", "iso-8859-5",
33 "iso-8859-6", "iso-8859-7",
34 "iso-8859-8", "iso-8859-9",
35 "iso-8859-10", "utf-8",
36 "iso-8859-13", "iso-8859-14",
37 "iso-8859-15", "windows-874",
38 "windows-1250", "windows-1251",
39 "windows-1252", "windows-1253",
40 "windows-1254", "windows-1255",
41 "windows-1256", "windows-1257",
42 "windows-1258", "koi8-r",
43 "koi8-u", "iso-8859-11",
44 "iso-8859-16", "mac-roman",
62 "windows-932", "windows-936",
63 "windows-949", "windows-950",
64 "windows-1361", "unknown",
104 static void print_utf8(const char *msg
, const cups_utf8_t
*src
);
108 * 'main()' - Main entry for internationalization test module.
111 int /* O - Exit code */
112 main(int argc
, /* I - Argument Count */
113 char *argv
[]) /* I - Arguments */
115 FILE *fp
; /* File pointer */
116 int count
; /* File line counter */
117 int status
, /* Status of current test */
118 errors
; /* Error count */
119 char line
[1024]; /* File line source string */
120 int len
; /* Length (count) of string */
121 char legsrc
[1024], /* Legacy source string */
122 legdest
[1024], /* Legacy destination string */
123 *legptr
; /* Pointer into legacy string */
124 cups_utf8_t utf8latin
[] = /* UTF-8 Latin-1 source */
125 { 0x41, 0x20, 0x21, 0x3D, 0x20, 0xC3, 0x84, 0x2E, 0x00 };
126 /* "A != <A WITH DIAERESIS>." - use ISO 8859-1 */
127 cups_utf8_t utf8repla
[] = /* UTF-8 Latin-1 replacement */
128 { 0x41, 0x20, 0xE2, 0x89, 0xA2, 0x20, 0xC3, 0x84, 0x2E, 0x00 };
129 /* "A <NOT IDENTICAL TO> <A WITH DIAERESIS>." */
130 cups_utf8_t utf8greek
[] = /* UTF-8 Greek source string */
131 { 0x41, 0x20, 0x21, 0x3D, 0x20, 0xCE, 0x91, 0x2E, 0x00 };
132 /* "A != <ALPHA>." - use ISO 8859-7 */
133 cups_utf8_t utf8japan
[] = /* UTF-8 Japanese source */
134 { 0x41, 0x20, 0x21, 0x3D, 0x20, 0xEE, 0x9C, 0x80, 0x2E, 0x00 };
135 /* "A != <PRIVATE U+E700>." - use Windows 932 or EUC-JP */
136 cups_utf8_t utf8taiwan
[] = /* UTF-8 Chinese source */
137 { 0x41, 0x20, 0x21, 0x3D, 0x20, 0xE4, 0xB9, 0x82, 0x2E, 0x00 };
138 /* "A != <CJK U+4E42>." - use Windows 950 (Big5) or EUC-TW */
139 cups_utf8_t utf8dest
[1024]; /* UTF-8 destination string */
140 cups_utf32_t utf32dest
[1024]; /* UTF-32 destination string */
145 int i
; /* Looping var */
146 cups_encoding_t encoding
; /* Source encoding */
151 puts("Usage: ./testi18n [filename charset]");
155 if ((fp
= fopen(argv
[1], "rb")) == NULL
)
161 for (i
= 0, encoding
= CUPS_AUTO_ENCODING
;
162 i
< (int)(sizeof(lang_encodings
) / sizeof(lang_encodings
[0]));
164 if (!_cups_strcasecmp(lang_encodings
[i
], argv
[2]))
166 encoding
= (cups_encoding_t
)i
;
170 if (encoding
== CUPS_AUTO_ENCODING
)
172 fprintf(stderr
, "%s: Unknown character set!\n", argv
[2]);
177 while (fgets(line
, sizeof(line
), fp
))
179 if (cupsCharsetToUTF8(utf8dest
, line
, sizeof(utf8dest
), encoding
) < 0)
181 fprintf(stderr
, "%s: Unable to convert line: %s", argv
[1], line
);
186 fputs((char *)utf8dest
, stdout
);
194 * Start with some conversion tests from a UTF-8 test file.
199 if ((fp
= fopen("utf8demo.txt", "rb")) == NULL
)
201 perror("utf8demo.txt");
209 fputs("cupsUTF8ToUTF32 of utfdemo.txt: ", stdout
);
211 for (count
= 0, status
= 0; fgets(line
, sizeof(line
), fp
);)
215 if (cupsUTF8ToUTF32(utf32dest
, (cups_utf8_t
*)line
, 1024) < 0)
217 printf("FAIL (UTF-8 to UTF-32 on line %d)\n", count
);
228 * cupsUTF8ToCharset(CUPS_EUC_JP)
231 fputs("cupsUTF8ToCharset(CUPS_EUC_JP) of utfdemo.txt: ", stdout
);
235 for (count
= 0, status
= 0; fgets(line
, sizeof(line
), fp
);)
239 len
= cupsUTF8ToCharset(legdest
, (cups_utf8_t
*)line
, 1024, CUPS_EUC_JP
);
242 printf("FAIL (UTF-8 to EUC-JP on line %d)\n", count
);
255 * Test UTF-8 to legacy charset (ISO 8859-1)...
258 fputs("cupsUTF8ToCharset(CUPS_ISO8859_1): ", stdout
);
262 len
= cupsUTF8ToCharset(legdest
, utf8latin
, 1024, CUPS_ISO8859_1
);
265 printf("FAIL (len=%d)\n", len
);
275 fputs("cupsCharsetToUTF8(CUPS_ISO8859_1): ", stdout
);
277 cupsCopyString(legsrc
, legdest
, sizeof(legsrc
));
279 len
= cupsCharsetToUTF8(utf8dest
, legsrc
, 1024, CUPS_ISO8859_1
);
280 if ((size_t)len
!= strlen((char *)utf8latin
))
282 printf("FAIL (len=%d, expected %d)\n", len
, (int)strlen((char *)utf8latin
));
283 print_utf8(" utf8latin", utf8latin
);
284 print_utf8(" utf8dest", utf8dest
);
287 else if (memcmp(utf8latin
, utf8dest
, (size_t)len
))
289 puts("FAIL (results do not match)");
290 print_utf8(" utf8latin", utf8latin
);
291 print_utf8(" utf8dest", utf8dest
);
294 else if (cupsUTF8ToCharset(legdest
, utf8repla
, 1024, CUPS_ISO8859_1
) < 0)
296 puts("FAIL (replacement characters do not work!)");
303 * Test UTF-8 to/from legacy charset (ISO 8859-7)...
306 fputs("cupsUTF8ToCharset(CUPS_ISO8859_7): ", stdout
);
308 if (cupsUTF8ToCharset(legdest
, utf8greek
, 1024, CUPS_ISO8859_7
) < 0)
315 for (legptr
= legdest
; *legptr
&& *legptr
!= '?'; legptr
++);
319 puts("FAIL (unknown character)");
326 fputs("cupsCharsetToUTF8(CUPS_ISO8859_7): ", stdout
);
328 cupsCopyString(legsrc
, legdest
, sizeof(legsrc
));
330 len
= cupsCharsetToUTF8(utf8dest
, legsrc
, 1024, CUPS_ISO8859_7
);
331 if ((size_t)len
!= strlen((char *)utf8greek
))
333 printf("FAIL (len=%d, expected %d)\n", len
, (int)strlen((char *)utf8greek
));
334 print_utf8(" utf8greek", utf8greek
);
335 print_utf8(" utf8dest", utf8dest
);
338 else if (memcmp(utf8greek
, utf8dest
, (size_t)len
))
340 puts("FAIL (results do not match)");
341 print_utf8(" utf8greek", utf8greek
);
342 print_utf8(" utf8dest", utf8dest
);
349 * Test UTF-8 to/from legacy charset (Windows 932)...
352 fputs("cupsUTF8ToCharset(CUPS_WINDOWS_932): ", stdout
);
354 if (cupsUTF8ToCharset(legdest
, utf8japan
, 1024, CUPS_WINDOWS_932
) < 0)
361 for (legptr
= legdest
; *legptr
&& *legptr
!= '?'; legptr
++);
365 puts("FAIL (unknown character)");
372 fputs("cupsCharsetToUTF8(CUPS_WINDOWS_932): ", stdout
);
374 cupsCopyString(legsrc
, legdest
, sizeof(legsrc
));
376 len
= cupsCharsetToUTF8(utf8dest
, legsrc
, 1024, CUPS_WINDOWS_932
);
377 if ((size_t)len
!= strlen((char *)utf8japan
))
379 printf("FAIL (len=%d, expected %d)\n", len
, (int)strlen((char *)utf8japan
));
380 print_utf8(" utf8japan", utf8japan
);
381 print_utf8(" utf8dest", utf8dest
);
384 else if (memcmp(utf8japan
, utf8dest
, (size_t)len
))
386 puts("FAIL (results do not match)");
387 print_utf8(" utf8japan", utf8japan
);
388 print_utf8(" utf8dest", utf8dest
);
395 * Test UTF-8 to/from legacy charset (EUC-JP)...
398 fputs("cupsUTF8ToCharset(CUPS_EUC_JP): ", stdout
);
400 if (cupsUTF8ToCharset(legdest
, utf8japan
, 1024, CUPS_EUC_JP
) < 0)
407 for (legptr
= legdest
; *legptr
&& *legptr
!= '?'; legptr
++);
411 puts("FAIL (unknown character)");
418 #if 0 /* Failing and not sure why, might be an iconv issue? */
419 fputs("cupsCharsetToUTF8(CUPS_EUC_JP): ", stdout
);
421 cupsCopyString(legsrc
, legdest
, sizeof(legsrc
));
423 len
= cupsCharsetToUTF8(utf8dest
, legsrc
, 1024, CUPS_EUC_JP
);
424 if ((size_t)len
!= strlen((char *)utf8japan
))
426 printf("FAIL (len=%d, expected %d)\n", len
, (int)strlen((char *)utf8japan
));
427 print_utf8(" utf8japan", utf8japan
);
428 print_utf8(" utf8dest", utf8dest
);
431 else if (memcmp(utf8japan
, utf8dest
, (size_t)len
))
433 puts("FAIL (results do not match)");
434 print_utf8(" utf8japan", utf8japan
);
435 print_utf8(" utf8dest", utf8dest
);
443 * Test UTF-8 to/from legacy charset (Windows 950)...
446 fputs("cupsUTF8ToCharset(CUPS_WINDOWS_950): ", stdout
);
448 if (cupsUTF8ToCharset(legdest
, utf8taiwan
, 1024, CUPS_WINDOWS_950
) < 0)
455 for (legptr
= legdest
; *legptr
&& *legptr
!= '?'; legptr
++);
459 puts("FAIL (unknown character)");
466 fputs("cupsCharsetToUTF8(CUPS_WINDOWS_950): ", stdout
);
468 cupsCopyString(legsrc
, legdest
, sizeof(legsrc
));
470 len
= cupsCharsetToUTF8(utf8dest
, legsrc
, 1024, CUPS_WINDOWS_950
);
471 if ((size_t)len
!= strlen((char *)utf8taiwan
))
473 printf("FAIL (len=%d, expected %d)\n", len
, (int)strlen((char *)utf8taiwan
));
474 print_utf8(" utf8taiwan", utf8taiwan
);
475 print_utf8(" utf8dest", utf8dest
);
478 else if (memcmp(utf8taiwan
, utf8dest
, (size_t)len
))
480 puts("FAIL (results do not match)");
481 print_utf8(" utf8taiwan", utf8taiwan
);
482 print_utf8(" utf8dest", utf8dest
);
489 * Test UTF-8 to/from legacy charset (EUC-TW)...
492 fputs("cupsUTF8ToCharset(CUPS_EUC_TW): ", stdout
);
494 if (cupsUTF8ToCharset(legdest
, utf8taiwan
, 1024, CUPS_EUC_TW
) < 0)
501 for (legptr
= legdest
; *legptr
&& *legptr
!= '?'; legptr
++);
505 puts("FAIL (unknown character)");
512 fputs("cupsCharsetToUTF8(CUPS_EUC_TW): ", stdout
);
514 cupsCopyString(legsrc
, legdest
, sizeof(legsrc
));
516 len
= cupsCharsetToUTF8(utf8dest
, legsrc
, 1024, CUPS_EUC_TW
);
517 if ((size_t)len
!= strlen((char *)utf8taiwan
))
519 printf("FAIL (len=%d, expected %d)\n", len
, (int)strlen((char *)utf8taiwan
));
520 print_utf8(" utf8taiwan", utf8taiwan
);
521 print_utf8(" utf8dest", utf8dest
);
524 else if (memcmp(utf8taiwan
, utf8dest
, (size_t)len
))
526 puts("FAIL (results do not match)");
527 print_utf8(" utf8taiwan", utf8taiwan
);
528 print_utf8(" utf8dest", utf8dest
);
536 * Test UTF-8 (16-bit) to UTF-32 (w/ BOM)...
539 printf("\ntesti18n: Testing UTF-8 to UTF-32 (w/ BOM)...\n");
540 len
= cupsUTF8ToUTF32(utf32dest
, utf8good
, 1024);
545 print_utf8(" utf8good ", utf8good
);
546 print_utf32(" utf32dest", utf32dest
);
548 memcpy(utf32src
, utf32dest
, (len
+ 1) * sizeof(cups_utf32_t
));
549 len
= cupsUTF32ToUTF8(utf8dest
, utf32src
, 1024);
552 if (len
!= strlen ((char *) utf8good
))
554 if (memcmp(utf8good
, utf8dest
, len
) != 0)
558 * Test invalid UTF-8 (16-bit) to UTF-32 (w/ BOM)...
561 printf("\ntesti18n: Testing UTF-8 bad 16-bit source string...\n");
562 len
= cupsUTF8ToUTF32(utf32dest
, utf8bad
, 1024);
566 print_utf8(" utf8bad ", utf8bad
);
569 * Test _cupsCharmapFlush()...
572 printf("\ntesti18n: Testing _cupsCharmapFlush()...\n");
582 * 'print_utf8()' - Print UTF-8 string with (optional) message.
586 print_utf8(const char *msg
, /* I - Message String */
587 const cups_utf8_t
*src
) /* I - UTF-8 Source String */
589 const char *prefix
; /* Prefix string */
595 for (prefix
= " "; *src
; src
++)
597 printf("%s%02x", prefix
, *src
);
599 if ((src
[0] & 0x80) && (src
[1] & 0x80))