]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/testlang.c
2 * Localization test program for CUPS.
4 * Copyright 2007-2017 by Apple Inc.
5 * Copyright 1997-2006 by Easy Software Products.
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more information.
11 * Include necessary headers...
14 #include "cups-private.h"
15 #include "ppd-private.h"
17 # include <CoreFoundation/CoreFoundation.h>
18 #endif /* __APPLE__ */
25 static int test_string(cups_lang_t
*language
, const char *msgid
);
29 * 'main()' - Load the specified language and show the strings for yes and no.
32 int /* O - Exit status */
33 main(int argc
, /* I - Number of command-line arguments */
34 char *argv
[]) /* I - Command-line arguments */
36 int i
; /* Looping var */
37 int errors
= 0; /* Number of errors */
38 cups_lang_t
*language
; /* Message catalog */
39 cups_lang_t
*language2
; /* Message catalog */
40 struct lconv
*loc
; /* Locale data */
41 char buffer
[1024]; /* String buffer */
42 double number
; /* Number */
43 static const char * const tests
[] = /* Test strings */
54 language
= cupsLangDefault();
55 language2
= cupsLangDefault();
59 language
= cupsLangGet(argv
[1]);
60 language2
= cupsLangGet(argv
[1]);
62 setenv("LANG", argv
[1], 1);
63 setenv("SOFTWARE", "CUPS/" CUPS_SVERSION
, 1);
68 if (language
!= language2
)
72 puts("**** ERROR: Language cache did not work! ****");
73 puts("First result from cupsLangGet:");
76 printf("Language = \"%s\"\n", language
->language
);
77 printf("Encoding = \"%s\"\n", _cupsEncodingName(language
->encoding
));
79 errors
+= test_string(language
, "No");
80 errors
+= test_string(language
, "Yes");
82 if (language
!= language2
)
84 puts("Second result from cupsLangGet:");
86 printf("Language = \"%s\"\n", language2
->language
);
87 printf("Encoding = \"%s\"\n", _cupsEncodingName(language2
->encoding
));
88 printf("No = \"%s\"\n", _cupsLangString(language2
, "No"));
89 printf("Yes = \"%s\"\n", _cupsLangString(language2
, "Yes"));
94 for (i
= 0; i
< (int)(sizeof(tests
) / sizeof(tests
[0])); i
++)
96 number
= _cupsStrScand(tests
[i
], NULL
, loc
);
98 printf("_cupsStrScand(\"%s\") number=%f\n", tests
[i
], number
);
100 _cupsStrFormatd(buffer
, buffer
+ sizeof(buffer
), number
, loc
);
102 printf("_cupsStrFormatd(%f) buffer=\"%s\"\n", number
, buffer
);
104 if (strcmp(buffer
, tests
[i
]))
107 puts("**** ERROR: Bad formatted number! ****");
113 ppd_file_t
*ppd
; /* PPD file */
114 ppd_option_t
*option
; /* PageSize option */
115 ppd_choice_t
*choice
; /* PageSize/Letter choice */
117 if ((ppd
= ppdOpenFile(argv
[2])) == NULL
)
119 printf("Unable to open PPD file \"%s\".\n", argv
[2]);
126 if ((option
= ppdFindOption(ppd
, "PageSize")) == NULL
)
128 puts("No PageSize option.");
133 printf("PageSize: %s\n", option
->text
);
135 if ((choice
= ppdFindChoice(option
, "Letter")) == NULL
)
137 puts("No Letter PageSize choice.");
142 printf("Letter: %s\n", choice
->text
);
146 printf("media-empty: %s\n", ppdLocalizeIPPReason(ppd
, "media-empty", NULL
, buffer
, sizeof(buffer
)));
155 * Test all possible language IDs for compatibility with _cupsAppleLocale...
158 CFIndex j
, /* Looping var */
159 num_locales
; /* Number of locales */
160 CFArrayRef locales
; /* Locales */
161 CFStringRef locale_id
, /* Current locale ID */
162 language_id
; /* Current language ID */
163 char locale_str
[256], /* Locale ID C string */
164 language_str
[256], /* Language ID C string */
165 *bufptr
; /* Pointer to ".UTF-8" in POSIX locale */
166 size_t buflen
; /* Length of POSIX locale */
167 # if TEST_COUNTRY_CODES
168 CFIndex k
, /* Looping var */
169 num_country_codes
; /* Number of country codes */
170 CFArrayRef country_codes
; /* Country codes */
171 CFStringRef country_code
, /* Current country code */
172 temp_id
; /* Temporary language ID */
173 char country_str
[256]; /* Country code C string */
174 # endif /* TEST_COUNTRY_CODES */
176 locales
= CFLocaleCopyAvailableLocaleIdentifiers();
177 num_locales
= CFArrayGetCount(locales
);
179 # if TEST_COUNTRY_CODES
180 country_codes
= CFLocaleCopyISOCountryCodes();
181 num_country_codes
= CFArrayGetCount(country_codes
);
182 # endif /* TEST_COUNTRY_CODES */
184 printf("%d locales are available:\n", (int)num_locales
);
186 for (j
= 0; j
< num_locales
; j
++)
188 locale_id
= CFArrayGetValueAtIndex(locales
, j
);
189 language_id
= CFLocaleCreateCanonicalLanguageIdentifierFromString(kCFAllocatorDefault
, locale_id
);
191 if (!locale_id
|| !CFStringGetCString(locale_id
, locale_str
, (CFIndex
)sizeof(locale_str
), kCFStringEncodingASCII
))
193 printf("%d: FAIL (unable to get locale ID string)\n", (int)j
+ 1);
198 if (!language_id
|| !CFStringGetCString(language_id
, language_str
, (CFIndex
)sizeof(language_str
), kCFStringEncodingASCII
))
200 printf("%d %s: FAIL (unable to get language ID string)\n", (int)j
+ 1, locale_str
);
205 if (!_cupsAppleLocale(language_id
, buffer
, sizeof(buffer
)))
207 printf("%d %s(%s): FAIL (unable to convert language ID string to POSIX locale)\n", (int)j
+ 1, locale_str
, language_str
);
212 if ((bufptr
= strstr(buffer
, ".UTF-8")) != NULL
)
213 buflen
= (size_t)(bufptr
- buffer
);
215 buflen
= strlen(buffer
);
217 if ((language
= cupsLangGet(buffer
)) == NULL
)
219 printf("%d %s(%s): FAIL (unable to load POSIX locale \"%s\")\n", (int)j
+ 1, locale_str
, language_str
, buffer
);
224 if (strncasecmp(language
->language
, buffer
, buflen
))
226 printf("%d %s(%s): FAIL (unable to load POSIX locale \"%s\", got \"%s\")\n", (int)j
+ 1, locale_str
, language_str
, buffer
, language
->language
);
231 printf("%d %s(%s): PASS (POSIX locale is \"%s\")\n", (int)j
+ 1, locale_str
, language_str
, buffer
);
236 # if TEST_COUNTRY_CODES
237 CFRelease(country_codes
);
238 # endif /* TEST_COUNTRY_CODES */
240 #endif /* __APPLE__ */
243 puts("ALL TESTS PASSED");
250 * 'test_string()' - Test the localization of a string.
253 static int /* O - 1 on failure, 0 on success */
254 test_string(cups_lang_t
*language
, /* I - Language */
255 const char *msgid
) /* I - Message */
257 const char *msgstr
; /* Localized string */
261 * Get the localized string and then see if we got what we expected.
263 * For the POSIX locale, the string pointers should be the same.
264 * For any other locale, the string pointers should be different.
267 msgstr
= _cupsLangString(language
, msgid
);
268 if (strcmp(language
->language
, "C") && msgid
== msgstr
)
270 printf("%-8s = \"%s\" (FAIL - no message catalog loaded)\n", msgid
, msgstr
);
273 else if (!strcmp(language
->language
, "C") && msgid
!= msgstr
)
275 printf("%-8s = \"%s\" (FAIL - POSIX locale is localized)\n", msgid
, msgstr
);
279 printf("%-8s = \"%s\" (PASS)\n", msgid
, msgstr
);