41 const char *locale;
42 const char *day1;
43 const char *cursym;
44 } ldata[] = {
45 { "C", "Sunday", "" },
46 { "en_US.UTF-8", "Sunday", "$" },
47 { "de_DE.UTF-8", "Sonntag", "€" },
48 { "ru_RU.UTF-8", "воскресенье", "руб." },
49 { "ja_JP.UTF-8", "日曜日", "¥" },
50 };
51
52 #define NUM_LDATA 5
53 #define NUMTHR 20
54 #define NUMITR 200
55
56 static void
57 test_start(const char *testName, const char *format, ...)
58 {
59 va_list args;
60
61 (void) printf("TEST STARTING %s: ", testName);
62
63 va_start(args, format);
64 (void) vprintf(format, args);
65 va_end(args);
66 (void) fflush(stdout);
67 }
68
69 static void
70 test_failed(const char *testName, const char *format, ...)
71 {
72 va_list args;
73
74 (void) printf("TEST FAILED %s: ", testName);
75
76 va_start(args, format);
77 (void) vprintf(format, args);
78 va_end(args);
79
80 (void) exit(-1);
81 }
82
83 static void
84 test_passed(const char *testName)
85 {
86 (void) printf("TEST PASS: %s\n", testName);
87 (void) fflush(stdout);
88 }
89
90 void *
91 testlocale_thr(void *ptr)
92 {
93 locale_t cloc, loc;
94 struct lconv *lc;
95 char *day;
96 char *tname = ptr;
97
98 for (int i = 0; i < NUMITR; i++) {
99 struct ldata *l = &ldata[i % NUM_LDATA];
100 cloc = uselocale(NULL);
101
102 loc = newlocale(LC_ALL_MASK, l->locale, NULL);
103 if (loc == NULL) {
104 test_failed("newlocale %s failed", l->locale);
105 }
106 day = nl_langinfo_l(DAY_1, loc);
107 if (strcmp(day, l->day1) != 0) {
108 test_failed(tname, "newlocale data mismatch (%s != %s)",
109 day, l->day1);
110 }
111 if (debug)
139 }
140 if (debug)
141 (void) printf("CSYM: %s\n", lc->currency_symbol);
142
143 /* we sleep a random bit to mix it up */
144 (void) usleep(rand() % 10);
145
146 if (uselocale(cloc) != loc) {
147 test_failed(tname, "revert old locale mismatch");
148 }
149 freelocale(loc);
150 if (uselocale(LC_GLOBAL_LOCALE) != cloc) {
151 test_failed(tname, "revert GLOBAL_LOCALE mismatch");
152 }
153 }
154 return (NULL);
155 }
156
157
158 void
159 testlocale(void)
160 {
161 char *tname = "newlocale/uselocale";
162 pthread_t tid[NUMTHR];
163
164 test_start(tname, "running %d threads %d iterations\n", NUMTHR, NUMITR);
165
166 for (int i = 0; i < NUMTHR; i++) {
167 (void) pthread_create(&tid[i], NULL, testlocale_thr, tname);
168 }
169
170 for (int i = 0; i < NUMTHR; i++) {
171 (void) pthread_join(tid[i], NULL);
172 }
173 test_passed(tname);
174 }
175
176 int
177 main(int argc, char **argv)
178 {
179 int optc;
180
181 while ((optc = getopt(argc, argv, "d")) != EOF) {
182 switch (optc) {
183 case 'd':
184 debug++;
185 break;
186 default:
187 (void) fprintf(stderr, "Usage: %s [-d]\n", argv[0]);
188 exit(1);
189 }
190 }
191
192 testlocale();
193
194 exit(0);
195 }
|
41 const char *locale;
42 const char *day1;
43 const char *cursym;
44 } ldata[] = {
45 { "C", "Sunday", "" },
46 { "en_US.UTF-8", "Sunday", "$" },
47 { "de_DE.UTF-8", "Sonntag", "€" },
48 { "ru_RU.UTF-8", "воскресенье", "руб." },
49 { "ja_JP.UTF-8", "日曜日", "¥" },
50 };
51
52 #define NUM_LDATA 5
53 #define NUMTHR 20
54 #define NUMITR 200
55
56 static void
57 test_start(const char *testName, const char *format, ...)
58 {
59 va_list args;
60
61 (void) printf("TEST STARTING %s (%s): ", testName, ARCH);
62
63 va_start(args, format);
64 (void) vprintf(format, args);
65 va_end(args);
66 (void) fflush(stdout);
67 }
68
69 static void
70 test_failed(const char *testName, const char *format, ...)
71 {
72 va_list args;
73
74 (void) printf("TEST FAILED %s (%s): ", testName, ARCH);
75
76 va_start(args, format);
77 (void) vprintf(format, args);
78 va_end(args);
79 (void) printf("\n");
80
81 (void) exit(-1);
82 }
83
84 static void
85 test_passed(const char *testName)
86 {
87 (void) printf("TEST PASS: %s (%s)\n", testName, ARCH);
88 (void) fflush(stdout);
89 }
90
91 void *
92 testlocale_thr_one(void *ptr)
93 {
94 locale_t cloc, loc;
95 struct lconv *lc;
96 char *day;
97 char *tname = ptr;
98
99 for (int i = 0; i < NUMITR; i++) {
100 struct ldata *l = &ldata[i % NUM_LDATA];
101 cloc = uselocale(NULL);
102
103 loc = newlocale(LC_ALL_MASK, l->locale, NULL);
104 if (loc == NULL) {
105 test_failed("newlocale %s failed", l->locale);
106 }
107 day = nl_langinfo_l(DAY_1, loc);
108 if (strcmp(day, l->day1) != 0) {
109 test_failed(tname, "newlocale data mismatch (%s != %s)",
110 day, l->day1);
111 }
112 if (debug)
140 }
141 if (debug)
142 (void) printf("CSYM: %s\n", lc->currency_symbol);
143
144 /* we sleep a random bit to mix it up */
145 (void) usleep(rand() % 10);
146
147 if (uselocale(cloc) != loc) {
148 test_failed(tname, "revert old locale mismatch");
149 }
150 freelocale(loc);
151 if (uselocale(LC_GLOBAL_LOCALE) != cloc) {
152 test_failed(tname, "revert GLOBAL_LOCALE mismatch");
153 }
154 }
155 return (NULL);
156 }
157
158
159 void
160 test_newlocale_threaded(void)
161 {
162 char *tname = "newlocale_threaded";
163 pthread_t tid[NUMTHR];
164
165 test_start(tname, "running %d threads %d iterations\n", NUMTHR, NUMITR);
166
167 for (int i = 0; i < NUMTHR; i++) {
168 (void) pthread_create(&tid[i], NULL, testlocale_thr_one, tname);
169 }
170
171 for (int i = 0; i < NUMTHR; i++) {
172 (void) pthread_join(tid[i], NULL);
173 }
174 test_passed(tname);
175 }
176
177 void
178 test_newlocale_negative(void)
179 {
180
181 locale_t loc, bad;
182 char *day;
183 char *tname = "newlocale_negative";
184
185 test_start(tname, "verify proper failure handling\n");
186 loc = newlocale(LC_ALL_MASK, "de_DE.UTF-8", NULL);
187 if (loc == NULL) {
188 test_failed(tname, "cannot set de_DE.UTF-8");
189 }
190 day = nl_langinfo_l(DAY_1, loc);
191 if (strcmp(day, "Sonntag") != 0) {
192 test_failed(tname, "incorrect Sonntag != %s", day);
193 }
194
195 bad = newlocale(LC_ALL_MASK, "cn_US.BIZRRE", loc);
196 if (bad != NULL) {
197 test_failed(tname, "passed setting bogus locale");
198 }
199 day = nl_langinfo_l(DAY_1, loc);
200 if (strcmp(day, "Sonntag") != 0) {
201 test_failed(tname, "incorrect Sonntag != %s", day);
202 }
203 test_passed(tname);
204 }
205
206 void
207 test_newlocale_categories(void)
208 {
209 locale_t loc;
210 char *day, *cur, *yes;
211 char *tname = "newlocale_categories";
212
213 test_start(tname, "verify different category behavior\n");
214
215 loc = NULL;
216 loc = newlocale(LC_TIME_MASK, "de_DE.UTF-8", loc);
217 loc = newlocale(LC_MESSAGES_MASK, "ru_RU.UTF-8", loc);
218 loc = newlocale(LC_MONETARY_MASK, "en_US.UTF-8", loc);
219
220 if (loc == NULL) {
221 test_failed(tname, "failed to set locale");
222 }
223
224 day = nl_langinfo_l(DAY_1, loc);
225 if ((day == NULL) || (strcmp(day, "Sonntag") != 0)) {
226 test_failed(tname, "day1 mismatch %s != %s", day, "Sonntag");
227 }
228 yes = nl_langinfo_l(YESSTR, loc);
229 if ((yes == NULL) || (strcmp(yes, "да") != 0)) {
230 test_failed(tname, "currency mismatch");
231 }
232 cur = nl_langinfo_l(CRNCYSTR, loc);
233 if ((cur == NULL) || (strcmp(cur, "-$") != 0)) {
234 test_failed(tname, "currency mismatch [%s] != [%s]", cur, "-$");
235 }
236
237 test_passed(tname);
238 }
239
240 void
241 test_newlocale_composite(void)
242 {
243 locale_t loc;
244 char *day, *cur, *yes;
245 char *tname = "newlocale_composite";
246
247 test_start(tname, "verify composite locale behavior\n");
248
249 /* order: CTYPE/NUMERIC/TIME/COLLATE/MONETARY/MESSAGES */
250 loc = newlocale(LC_ALL_MASK,
251 "C/C/de_DE.UTF-8/C/en_US.UTF-8/ru_RU.UTF-8", NULL);
252
253 if (loc == NULL) {
254 test_failed(tname, "failed to set composite locale");
255 }
256
257 day = nl_langinfo_l(DAY_1, loc);
258 if ((day == NULL) || (strcmp(day, "Sonntag") != 0)) {
259 test_failed(tname, "day1 mismatch %s != %s", day, "Sonntag");
260 }
261 yes = nl_langinfo_l(YESSTR, loc);
262 if ((yes == NULL) || (strcmp(yes, "да") != 0)) {
263 test_failed(tname, "currency mismatch");
264 }
265 cur = nl_langinfo_l(CRNCYSTR, loc);
266 if ((cur == NULL) || (strcmp(cur, "-$") != 0)) {
267 test_failed(tname, "currency mismatch [%s] != [%s]", cur, "-$");
268 }
269
270 test_passed(tname);
271 }
272
273 int
274 main(int argc, char **argv)
275 {
276 int optc;
277
278 while ((optc = getopt(argc, argv, "d")) != EOF) {
279 switch (optc) {
280 case 'd':
281 debug++;
282 break;
283 default:
284 (void) fprintf(stderr, "Usage: %s [-d]\n", argv[0]);
285 exit(1);
286 }
287 }
288
289 test_newlocale_threaded();
290 test_newlocale_negative();
291 test_newlocale_categories();
292 test_newlocale_composite();
293
294 exit(0);
295 }
|