| #include <locale.h> |
| #include <stdio.h> |
| #include <string.h> |
| |
| #include "third_party/glibc_locales/common/localeinfo.h" |
| #include "third_party/glibc_locales/glibc_locales.h" |
| #include "third_party/glibc_locales/library/current_locale.h" |
| |
| static int validate_locale(google_locale_t locale, int category, char *name, |
| char *where) { |
| if (locale->category_data[category] == NULL) { |
| printf("FAIL: current locale data is NULL %s.\n", where); |
| return 1; |
| } else if (strcmp(locale->category_data[category]->name, name)) { |
| printf("FAIL: current locale name is '%s', not '%s', %s.\n", |
| locale->category_data[category]->name, name, where); |
| return 1; |
| } |
| return 0; |
| } |
| |
| static int do_test() { |
| int res = 0; |
| |
| // Make sure we have a locale before anything is called. |
| google_locale_t locale = google_get_current_locale(); |
| res += |
| validate_locale(locale, GOOGLE_LC_CTYPE, "C", "ctype before setlocale"); |
| res += validate_locale(locale, GOOGLE_LC_COLLATE, "C", |
| "collate before setlocale"); |
| |
| // Set the locale to something that both implementations support |
| google_setlocale(LC_ALL, "en_US.UTF-8"); |
| locale = google_get_current_locale(); |
| res += validate_locale(locale, GOOGLE_LC_CTYPE, "en_US.UTF-8", |
| "for ctype after setlocale"); |
| res += validate_locale(locale, GOOGLE_LC_COLLATE, "en_US.UTF-8", |
| "for collate after setlocale"); |
| |
| // Set only the ctype locale. |
| google_setlocale(LC_CTYPE, "C"); |
| locale = google_get_current_locale(); |
| res += validate_locale(locale, GOOGLE_LC_CTYPE, "C", |
| "for ctype after setlocale(LC_CTYPE)"); |
| res += validate_locale(locale, GOOGLE_LC_COLLATE, "en_US.UTF-8", |
| "for collate after setlocale(LC_CTYPE)"); |
| |
| // Reset the locale. |
| google_setlocale(LC_ALL, "en_US.UTF-8"); |
| |
| // Set only the collate locale. |
| google_setlocale(LC_COLLATE, "C"); |
| locale = google_get_current_locale(); |
| res += validate_locale(locale, GOOGLE_LC_CTYPE, "en_US.UTF-8", |
| "for ctype after setlocale(LC_COLLATE)"); |
| res += validate_locale(locale, GOOGLE_LC_COLLATE, "C", |
| "for collate after setlocale(LC_COLLATE)"); |
| |
| // Reset the locale. |
| google_setlocale(LC_ALL, "en_US.UTF-8"); |
| |
| // Set the locale to something that only glibc_locales supports |
| google_setlocale(LC_ALL, "en_GB"); |
| locale = google_get_current_locale(); |
| res += validate_locale(locale, GOOGLE_LC_CTYPE, "en_US.UTF-8", |
| "for ctype after bad setlocale"); |
| res += validate_locale(locale, GOOGLE_LC_COLLATE, "en_US.UTF-8", |
| "for collate after bad setlocale"); |
| |
| // Finally, set the locale to an empty string. |
| if (google_setlocale(LC_ALL, "") == NULL) { |
| printf("FAIL: google_setlocale(LC_ALL, "") returned NULL.\n"); |
| res += 1; |
| } |
| |
| return res; |
| } |
| |
| int main() { return do_test(); } |