|
| 1 | +#ifndef _GNU_SOURCE |
| 2 | +#define _GNU_SOURCE |
| 3 | +#endif |
| 4 | + |
| 5 | +#include <assert.h> |
| 6 | +#include <ctype.h> |
| 7 | +#include <wctype.h> |
| 8 | +#include <stdbool.h> |
| 9 | +#include <langinfo.h> |
| 10 | +#include <limits.h> |
| 11 | +#include <stdlib.h> |
| 12 | +#include <locale.h> |
| 13 | +#include <stdio.h> |
| 14 | +#include <string.h> |
| 15 | +#include <wchar.h> |
| 16 | + |
| 17 | +int main() { |
| 18 | + char buf[64] = { 0 }; |
| 19 | + wint_t c = 0xC9; |
| 20 | + char *locale = setlocale(LC_ALL, ""); |
| 21 | + assert(locale && strlen(locale)); |
| 22 | + if (snprintf(buf, MB_LEN_MAX, "%lc", c) < 0) |
| 23 | + return -1; |
| 24 | + |
| 25 | + assert((unsigned char) buf[0] == 0xc3 && (unsigned char) buf[1] == 0x89 |
| 26 | + && buf[2] == '\0' && buf[3] == '\0'); |
| 27 | + |
| 28 | + locale_t fake = newlocale(LC_ALL_MASK, "swamp german", 0); |
| 29 | + assert(fake == 0); |
| 30 | + |
| 31 | + locale_t german = newlocale(LC_ALL_MASK, "de_DE.utf8", 0); |
| 32 | + assert(german != 0); |
| 33 | + |
| 34 | + locale_t posix = newlocale(LC_ALL_MASK, "POSIX", 0); |
| 35 | + assert(posix != 0); |
| 36 | + |
| 37 | + locale = setlocale(LC_ALL, "C"); |
| 38 | + assert(locale && strlen(locale)); |
| 39 | + |
| 40 | + char *decimal_point = nl_langinfo_l(DECIMAL_POINT, german); |
| 41 | + assert(!strcmp(",", decimal_point)); |
| 42 | + char *tousands_sep = nl_langinfo_l(THOUSEP, german); |
| 43 | + assert(!strcmp(".", tousands_sep)); |
| 44 | + char *yesexpr = nl_langinfo_l(YESEXPR, german); |
| 45 | + assert(!strcmp("^[+1jJyY]", yesexpr)); |
| 46 | + char *noexpr = nl_langinfo_l(NOEXPR, german); |
| 47 | + assert(!strcmp("^[-0nN]", noexpr)); |
| 48 | + char *currency_symbol = nl_langinfo_l(CURRENCY_SYMBOL, german); |
| 49 | + assert(!strcmp("€", currency_symbol)); |
| 50 | + char *crncystr = nl_langinfo_l(CRNCYSTR, german); |
| 51 | + assert(!strcmp("+€", crncystr)); |
| 52 | + char *abday_1 = nl_langinfo_l(ABDAY_1, german); |
| 53 | + assert(!strcmp("So", abday_1)); |
| 54 | + char *day_1 = nl_langinfo_l(DAY_1, german); |
| 55 | + assert(!strcmp("Sonntag", day_1)); |
| 56 | + char *day_7 = nl_langinfo_l(DAY_7, german); |
| 57 | + assert(!strcmp("Samstag", day_7)); |
| 58 | + char *pm = nl_langinfo_l(PM_STR, german); |
| 59 | + assert(!strcmp("", pm)); |
| 60 | + |
| 61 | + char lower = tolower_l('A', german); |
| 62 | + assert(lower == 'a'); |
| 63 | + char upper = toupper_l(lower, german); |
| 64 | + assert(upper == 'A'); |
| 65 | + wchar_t wlower = towlower_l(L'Ä', german); |
| 66 | + assert(wlower == L'ä'); |
| 67 | + |
| 68 | + assert(isblank_l(' ', german) == true); |
| 69 | + assert(isblank_l('\t', german) == true); |
| 70 | + assert(isblank_l('\n', german) == false); |
| 71 | + assert(isblank_l('a', german) == false); |
| 72 | + assert(isblank_l('1', german) == false); |
| 73 | + assert(isblank_l('\v', german) == false); |
| 74 | + assert(isblank_l('\r', german) == false); |
| 75 | + |
| 76 | + assert(isblank_l(' ', posix) == true); |
| 77 | + assert(isblank_l('\t', posix) == true); |
| 78 | + assert(isblank_l('\n', posix) == false); |
| 79 | + assert(isblank_l('a', posix) == false); |
| 80 | + assert(isblank_l('1', posix) == false); |
| 81 | + assert(isblank_l('\v', posix) == false); |
| 82 | + assert(isblank_l('\r', posix) == false); |
| 83 | + |
| 84 | + uselocale(german); |
| 85 | + |
| 86 | + assert(iswalpha_l(L'ß', german)); |
| 87 | + assert(iswlower_l(L'ß', german)); |
| 88 | + assert(!iswalpha_l(L'ß', posix)); |
| 89 | + |
| 90 | + wlower = towlower_l(L'Ä', german); |
| 91 | + assert(wlower == L'ä'); |
| 92 | + |
| 93 | + memset(buf, 0, sizeof(buf)); |
| 94 | + snprintf(buf, sizeof(buf), "%2.2f", 12.34); |
| 95 | + assert(!strcmp("12,34", buf)); |
| 96 | + |
| 97 | + assert(strtod_l(" 12,34", NULL, german) == 12.34); |
| 98 | + |
| 99 | + wctype_t wctype = wctype_l("alpha", german); |
| 100 | + assert(wctype); |
| 101 | + assert(iswctype_l(L'ß', wctype, german)); |
| 102 | + assert(!iswctype_l(L'÷', wctype, german)); |
| 103 | + assert(!iswctype_l(L'❔', wctype, german)); |
| 104 | + wctype = wctype_l("graph", german); |
| 105 | + assert(wctype); |
| 106 | + assert(iswctype_l(L'ß', wctype, german)); |
| 107 | + assert(iswctype_l(L'÷', wctype, german)); |
| 108 | + assert(iswctype_l(L'❔', wctype, german)); |
| 109 | + assert(!iswctype_l(L'\x91', wctype, german)); |
| 110 | + assert(!iswctype_l(WEOF, wctype, german)); |
| 111 | + |
| 112 | + uselocale(LC_GLOBAL_LOCALE); |
| 113 | + freelocale(german); |
| 114 | + |
| 115 | + snprintf(buf, sizeof(buf), "%2.2f", 12.34); |
| 116 | + assert(!strcmp("12.34", buf)); |
| 117 | + |
| 118 | + decimal_point = nl_langinfo_l(DECIMAL_POINT, posix); |
| 119 | + assert(!strcmp(".", decimal_point)); |
| 120 | + tousands_sep = nl_langinfo_l(THOUSEP, posix); |
| 121 | + assert(!strcmp("", tousands_sep)); |
| 122 | + yesexpr = nl_langinfo_l(YESEXPR, posix); |
| 123 | + assert(!strcmp("^[yY]", yesexpr)); |
| 124 | + noexpr = nl_langinfo_l(NOEXPR, posix); |
| 125 | + assert(!strcmp("^[nN]", noexpr)); |
| 126 | + currency_symbol = nl_langinfo_l(CURRENCY_SYMBOL, posix); |
| 127 | + assert(!strcmp("", currency_symbol)); |
| 128 | + crncystr = nl_langinfo_l(CRNCYSTR, posix); |
| 129 | + assert(!strcmp("-", crncystr)); |
| 130 | + abday_1 = nl_langinfo_l(ABDAY_1, posix); |
| 131 | + assert(!strcmp("Sun", abday_1)); |
| 132 | + day_1 = nl_langinfo_l(DAY_1, posix); |
| 133 | + assert(!strcmp("Sunday", day_1)); |
| 134 | + day_7 = nl_langinfo_l(DAY_7, posix); |
| 135 | + assert(!strcmp("Saturday", day_7)); |
| 136 | + pm = nl_langinfo_l(PM_STR, posix); |
| 137 | + assert(!strcmp("PM", pm)); |
| 138 | + |
| 139 | + freelocale(posix); |
| 140 | + |
| 141 | + locale = setlocale(LC_ALL, "de_DE"); |
| 142 | + assert(locale && strlen(locale)); |
| 143 | + |
| 144 | + tousands_sep = nl_langinfo(THOUSEP); |
| 145 | + assert(!strcmp(".", tousands_sep)); |
| 146 | + decimal_point = nl_langinfo(DECIMAL_POINT); |
| 147 | + assert(!strcmp(",", decimal_point)); |
| 148 | + pm = nl_langinfo(PM_STR); |
| 149 | + assert(!strcmp("", pm)); |
| 150 | + |
| 151 | + lower = tolower('A'); |
| 152 | + assert(lower == 'a'); |
| 153 | + upper = toupper(lower); |
| 154 | + assert(upper == 'A'); |
| 155 | + |
| 156 | + assert(isalpha('z')); |
| 157 | + assert(!isalpha('z' + 1)); |
| 158 | + |
| 159 | + snprintf(buf, sizeof(buf), "%'g", 12345.67); |
| 160 | + // assert(!strcmp("12.345,7", buf)); |
| 161 | + |
| 162 | + locale = setlocale(LC_NUMERIC, "C"); |
| 163 | + assert(locale && strlen(locale)); |
| 164 | + |
| 165 | + tousands_sep = nl_langinfo(THOUSEP); |
| 166 | + assert(!strcmp("", tousands_sep)); |
| 167 | + decimal_point = nl_langinfo(DECIMAL_POINT); |
| 168 | + assert(!strcmp(".", decimal_point)); |
| 169 | + pm = nl_langinfo(PM_STR); |
| 170 | + assert(!strcmp("", pm)); |
| 171 | + |
| 172 | + locale = setlocale(LC_TIME, "ru_RU.utf8"); |
| 173 | + assert(locale && strlen(locale)); |
| 174 | + |
| 175 | + day_7 = nl_langinfo(DAY_7); |
| 176 | + assert(!strcmp("Суббота", day_7)); |
| 177 | + |
| 178 | + locale = setlocale(LC_MONETARY, "en_US.utf8"); |
| 179 | + assert(locale && strlen(locale)); |
| 180 | + |
| 181 | + currency_symbol = nl_langinfo(CURRENCY_SYMBOL); |
| 182 | + assert(!strcmp("$", currency_symbol)); |
| 183 | + |
| 184 | + locale = setlocale(LC_ALL, "C"); |
| 185 | + assert(locale && strlen(locale)); |
| 186 | + |
| 187 | + pm = nl_langinfo(PM_STR); |
| 188 | + assert(!strcmp("PM", pm)); |
| 189 | + |
| 190 | + return 0; |
| 191 | +} |
0 commit comments