Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
22.locale.time.get.mt.cpp
Go to the documentation of this file.
1
30#include <ios> // for ios
31#include <iterator> // for ostreambuf_iterator
32#include <locale> // for locale, time_put
33#include <cstring> // for strlen ()
34#include <ctime> // for tm
35
36#include <rw_locale.h>
37#include <rw_thread.h>
38#include <rw_driver.h> // for rw_assert ()
39#include <rw_valcmp.h> // for rw_strncmp ()
40
41#define MAX_THREADS 32
42#define MAX_LOOPS 100000
43
44// default number of threads (will be adjusted to the number
45// of processors/cores later)
47
48// the number of times each thread should iterate
49int opt_nloops = 50000;
50
51// number of locales to use
53
54// should all threads share the same set of locale objects instead
55// of creating their own?
57
58// default timeout used by each threaded section of this test
59int opt_timeout = 60;
60
61
63// array of locale names to use for testing
64static const char*
66
67// number of locale names in the array
68static std::size_t
70
71
73//
75{
76 enum { BufferSize = 64 };
77
78 // name of the locale the data corresponds to
79 const char* locale_name_;
80
81 // optionally set to the named locale for threads to share
82 std::locale locale_;
83
84 // the time struct used to generate strings below
85 std::tm time_;
86
87 // the type specifier
88 char format_;
89
90 // narrow representations of time_ given the
91 // locale_name_ and the format_
93
94#ifndef _RWSTD_NO_WCHAR_T
95
96 // wide representations of time_
97 wchar_t wcs_ [BufferSize];
98
99#endif // _RWSTD_NO_WCHAR_T
100
102
103
104template <class charT, class Traits>
105struct MyIos: std::basic_ios<charT, Traits>
106{
107 MyIos () {
108 this->init (0);
109 }
110};
111
112
113template <class charT, class Traits>
114struct MyStreambuf: std::basic_streambuf<charT, Traits>
115{
116 typedef std::basic_streambuf<charT, Traits> Base;
117
119 : Base () {
120 }
121
122 void pubsetg (const charT *gbeg, std::streamsize n) {
123 this->setg (_RWSTD_CONST_CAST (charT*, gbeg),
124 _RWSTD_CONST_CAST (charT*, gbeg),
125 _RWSTD_CONST_CAST (charT*, gbeg) + n);
126 }
127
128 void pubsetp (charT *pbeg, std::streamsize n) {
129 this->setp (pbeg, pbeg + n);
130 }
131};
132
133void verify_fields (const std::tm& a, const std::tm& b)
134{
135 RW_ASSERT (a.tm_sec == b.tm_sec);
136 RW_ASSERT (a.tm_min == b.tm_min);
137 RW_ASSERT (a.tm_hour == b.tm_hour);
138 RW_ASSERT (a.tm_mday == b.tm_mday);
139 RW_ASSERT (a.tm_mon == b.tm_mon);
140 RW_ASSERT (a.tm_year == b.tm_year);
141 RW_ASSERT (a.tm_wday == b.tm_wday);
142 RW_ASSERT (a.tm_yday == b.tm_yday);
143}
144#define RW_ASSERT_FIELDS(a,b) verify_fields(a,b)
145
146extern "C" {
147
148bool test_char; // exercise time_get<char>
149bool test_wchar; // exercise time_get<wchar_t>
150
151
152static void*
153thread_func (void*)
154{
157 nio.rdbuf (&nsb);
158
159#ifndef _RWSTD_NO_WCHAR_T
162 wio.rdbuf (&wsb);
163#endif // _RWSTD_NO_WCHAR_T
164
165 std::ios_base::iostate state = std::ios_base::goodbit;
166
167 std::tm local;
168 for (int i = 0; i != opt_nloops; ++i) {
169
171 break;
172
173 // save the name of the locale
175
176 // construct a named locale, get a reference to the time_put
177 // facet from it and use it to format a random time value
178 // using a random conversion specifier
179 const std::locale loc =
181 : std::locale (data.locale_name_);
182
183 if (test_char) {
184 // exercise the narrow char specialization of the facet
185
186 const std::time_get<char> &ng =
187 std::use_facet<std::time_get<char> >(loc);
188
189 nio.imbue (loc);
190 nsb.pubsetg (data.ncs_, RW_COUNT_OF (data.ncs_));
191
192 const std::istreambuf_iterator<char> iter (&nsb);
193 const std::istreambuf_iterator<char> end;
194
195 std::memset (&local, 0, sizeof local);
196
197 switch (data.format_) {
198 case 'X':
199 ng.get_time (iter, end, nio, state, &local);
200 break;
201 case 'x':
202 ng.get_date (iter, end, nio, state, &local);
203 break;
204 case 'a':
205 case 'A':
206 ng.get_weekday (iter, end, nio, state, &local);
207 break;
208 case 'b':
209 case 'B':
210 ng.get_monthname (iter, end, nio, state, &local);
211 break;
212 case 'y':
213 case 'Y':
214 ng.get_year (iter, end, nio, state, &local);
215 break;
216 default:
217
218#ifndef _RWSTD_NO_EXT_TIME_GET
219 ng.get (iter, end, nio, state, &local, data.format_);
220#endif // _RWSTD_NO_EXT_TIME_GET
221
222 break;
223 }
224
225 RW_ASSERT (! (state & std::ios_base::failbit));
226 RW_ASSERT_FIELDS (local, data.time_);
227 }
228
229 // both specializations may be tested at the same time
230
231 if (test_wchar) {
232 // exercise the wide char specialization of the facet
233
234#ifndef _RWSTD_NO_WCHAR_T
235
236 const std::time_get<wchar_t> &wg =
237 std::use_facet<std::time_get<wchar_t> >(loc);
238
239 wio.imbue (loc);
240 wsb.pubsetg (data.wcs_, RW_COUNT_OF (data.wcs_));
241
242 const std::istreambuf_iterator<wchar_t> iter (&wsb);
243 const std::istreambuf_iterator<wchar_t> end;
244
245 std::memset (&local, 0, sizeof local);
246
247 switch (data.format_) {
248 case 'X':
249 wg.get_time (iter, end, wio, state, &local);
250 break;
251 case 'x':
252 wg.get_date (iter, end, wio, state, &local);
253 break;
254 case 'a':
255 case 'A':
256 wg.get_weekday (iter, end, wio, state, &local);
257 break;
258 case 'b':
259 case 'B':
260 wg.get_monthname (iter, end, wio, state, &local);
261 break;
262 case 'y':
263 case 'Y':
264 wg.get_year (iter, end, wio, state, &local);
265 break;
266 default:
267
268#ifndef _RWSTD_NO_EXT_TIME_GET
269 wg.get (iter, end, wio, state, &local, data.format_);
270#endif // _RWSTD_NO_EXT_TIME_GET
271
272 break;
273 }
274
275 RW_ASSERT (! (state & std::ios_base::failbit));
276 RW_ASSERT_FIELDS (local, data.time_);
277
278#endif // _RWSTD_NO_WCHAR_T
279
280 }
281 }
282
283 return 0;
284}
285
286} // extern "C"
287
288
290static int
291run_test (int, char**)
292{
295 nio.rdbuf (&nsb);
296
297#ifndef _RWSTD_NO_WCHAR_T
300 wio.rdbuf (&wsb);
301#endif // _RWSTD_NO_WCHAR_T
302
303 // find all installed locales for which setlocale (LC_ALL) succeeds
304 const char* const locale_list =
305 rw_opt_locales ? rw_opt_locales : rw_locales (_RWSTD_LC_ALL);
306
307 const std::size_t maxinx = RW_COUNT_OF (locales);
308
309 int j = 0;
310 for (const char* name = locale_list;
311 *name;
312 name += std::strlen (name) + 1) {
313
314 const std::size_t inx = nlocales;
315 locales [inx] = name;
316
317 // fill in the time and results for this locale
320
321 // initialize tm with random but valid values
322 std::memset (&data.time_, 0, sizeof data.time_);
323 data.time_.tm_sec = ++j % 61;
324 data.time_.tm_min = ++j % 60;
325 data.time_.tm_hour = ++j % 12;
326 data.time_.tm_wday = ++j % 7;
327 data.time_.tm_mon = ++j % 12;
328 data.time_.tm_mday = ++j % 31;
329 data.time_.tm_yday = ++j % 366;
330 data.time_.tm_year = ++j;
331
332#ifndef _RWSTD_NO_EXT_TIME_GET
333 // format specifiers that we can reliably parse
334 // get_weekday, get_monthname, get_date, get_time,
335 // get_year and the extension get.
336 const char formats [] = "aAbBCdhHjmMnRStTwxXyY";
337#else
338 // formats that generate strings compatible with
339 // get_weekday, get_monthname, get_date, get_time
340 // and get_year.
341 const char formats [] = "aAbBxXyY";
342#endif
343
344 // get the "random" data type used to generate representation
345 data.format_ = formats [nlocales % (sizeof formats - 1)];
346
347 try {
348 const std::locale loc (data.locale_name_);
349
350 const std::time_put<char> &np =
351 std::use_facet<std::time_put<char> >(loc);
352
353 const std::time_get<char> &ng =
354 std::use_facet<std::time_get<char> >(loc);
355
356 std::ios::iostate state = std::ios::goodbit;
357
358 nio.imbue (loc);
359
360 // put the time representation on the stream
361 nsb.pubsetp (data.ncs_, RW_COUNT_OF (data.ncs_));
362 *np.put (std::ostreambuf_iterator<char>(&nsb), nio, ' ',
363 &data.time_, data.format_) = '\0';
364
365 rw_fatal (!nio.fail (), __FILE__, __LINE__,
366 "time_put<char>::put(...) failed for locale(%#s)",
367 data.locale_name_);
368
369 // read the data back out. this is what the thread
370 // is expected to do, and the result it should get.
371
372 nsb.pubsetg (data.ncs_, RW_COUNT_OF (data.ncs_));
373
374 const std::istreambuf_iterator<char> iter (&nsb);
375 const std::istreambuf_iterator<char> end;
376
377 std::memset (&data.time_, 0, sizeof data.time_);
378
379 switch (data.format_) {
380 case 'a':
381 case 'A':
382 ng.get_weekday (iter, end, nio, state, &data.time_);
383 break;
384 case 'b':
385 case 'B':
386 case 'h':
387 ng.get_monthname (iter, end, nio, state, &data.time_);
388 break;
389 case 'y':
390 case 'Y':
391 ng.get_year (iter, end, nio, state, &data.time_);
392 break;
393 case 'x':
394 ng.get_date (iter, end, nio, state, &data.time_);
395 break;
396 case 'X':
397 ng.get_time (iter, end, nio, state, &data.time_);
398 break;
399 default:
400
401#ifndef _RWSTD_NO_EXT_TIME_GET
402 ng.get (iter, end, nio, state, &data.time_, data.format_);
403#endif // _RWSTD_NO_EXT_TIME_GET
404
405 break;
406 }
407
408 // we couldn't read the data back, so don't use this locale
409 if (state & std::ios_base::failbit)
410 continue;
411
412#ifndef _RWSTD_NO_WCHAR_T
413
414 const std::time_put<wchar_t> &wp =
415 std::use_facet<std::time_put<wchar_t> >(loc);
416
417 wio.imbue (loc);
418 wsb.pubsetp (data.wcs_, RW_COUNT_OF (data.wcs_));
419
420 *wp.put (std::ostreambuf_iterator<wchar_t>(&wsb),
421 wio, L' ', &data.time_, data.format_) = L'\0';
422
423 rw_fatal (!wio.fail (), __FILE__, __LINE__,
424 "time_put<wchar_t>::put(...) failed for locale(%#s)",
425 data.locale_name_);
426
427#endif // _RWSTD_NO_WCHAR_T
428
430 data.locale_ = loc;
431
432 nlocales += 1;
433
434 }
435 catch (...) {
436 rw_warn (!rw_opt_locales, 0, __LINE__,
437 "failed to create locale(%#s)", name);
438 }
439
440 if (nlocales == maxinx || nlocales == std::size_t (opt_nlocales))
441 break;
442 }
443
444 // avoid divide by zero in thread if there are no locales to test
445 rw_fatal (nlocales != 0, 0, __LINE__,
446 "failed to create one or more usable locales!");
447
448 rw_info (0, 0, 0,
449 "testing std::time_get<charT> with %d thread%{?}s%{;}, "
450 "%d iteration%{?}s%{;} each, in %zu locales { %{ .*A@} }",
453 nlocales, int (nlocales), "%#s", locales);
454
455 rw_info (0, 0, 0, "exercising std::time_get<char>");
456
457 test_char = true;
458 test_wchar = false;
459
460 // create and start a pool of threads and wait for them to finish
461 int result =
462 rw_thread_pool (0, std::size_t (opt_nthreads), 0,
463 thread_func, 0, std::size_t (opt_timeout));
464
465 rw_error (result == 0, 0, __LINE__,
466 "rw_thread_pool(0, %d, 0, %{#f}, 0) failed",
467 opt_nthreads, thread_func);
468
469#ifndef _RWSTD_NO_WCHAR_T
470
471 rw_info (0, 0, 0, "exercising std::time_get<wchar_t>");
472
473 test_char = false;
474 test_wchar = true;
475
476 // start a pool of threads to exercise wstring thread safety
477 result =
478 rw_thread_pool (0, std::size_t (opt_nthreads), 0,
479 thread_func, 0, std::size_t (opt_timeout));
480
481 rw_error (result == 0, 0, __LINE__,
482 "rw_thread_pool(0, %d, 0, %{#f}, 0) failed",
483 opt_nthreads, thread_func);
484
485 // exercise both the char and the wchar_t specializations
486 // at the same time
487
488 rw_info (0, 0, 0,
489 "exercising both std::time_get<char> and std::time_get<wchar_t>");
490
491 test_char = true;
492 test_wchar = true;
493
494 // start a pool of threads to exercise wstring thread safety
495 result =
496 rw_thread_pool (0, std::size_t (opt_nthreads), 0,
497 thread_func, 0, std::size_t (opt_timeout));
498
499 rw_error (result == 0, 0, __LINE__,
500 "rw_thread_pool(0, %d, 0, %{#f}, 0) failed",
501 opt_nthreads, thread_func);
502
503#endif // _RWSTD_NO_WCHAR_T
504
505 return result;
506}
507
508
510int main (int argc, char *argv[])
511{
512#ifdef _RWSTD_REENTRANT
513
514 // set nthreads to the greater of the number of processors
515 // and 2 (for uniprocessor systems) by default
517 if (opt_nthreads < 2)
518 opt_nthreads = 2;
519
520#endif // _RWSTD_REENTRANT
521
522 return rw_test (argc, argv, __FILE__,
523 "lib.locale.time.get",
524 "thread safety", run_test,
525 "|-soft-timeout#0 " // must be non-negative
526 "|-nloops#0 " // must be non-negative
527 "|-nthreads#0-* " // must be in [0, MAX_THREADS]
528 "|-nlocales#0 " // arg must be non-negative
529 "|-locales= " // must be provided
530 "|-shared-locale# ",
532 &opt_nloops,
533 int (MAX_THREADS),
538}
#define i
Definition 17.names.cpp:126
#define loc
Definition 17.names.cpp:492
#define a
Definition 17.names.cpp:109
#define j
Definition 17.names.cpp:127
#define b
Definition 17.names.cpp:110
#define charT
Definition 17.names.cpp:456
#define n
Definition 17.names.cpp:130
#define inx
Definition 17.names.cpp:484
volatile int result
const char *const data[]
const char * locale_list
#define RW_ASSERT_FIELDS(a, b)
int opt_timeout
struct MyTimeData my_time_data[MAX_THREADS]
void verify_fields(const std::tm &a, const std::tm &b)
int opt_shared_locale
int opt_nlocales
#define MAX_THREADS
bool test_wchar
int opt_nthreads
void run_test(const ForwardIterator &dummy, T *, Predicate *pred)
const int end
#define main
const unsigned nlocales
#define _RWSTD_CONST_CAST(T, x)
Definition _defs.h:536
const struct @19 locales[]
const char * name
Definition codecvt1.cpp:72
std::mbstate_t init
Definition codecvt1.cpp:80
_TEST_EXPORT int rw_test(int argc, char *argv[], const char *filename, const char *clause, const char *comment, int(*testfun)(int, char **), const char *optspec,...)
_TEST_EXPORT int rw_info(int, const char *, int, const char *,...)
Definition driver.cpp:1681
_TEST_EXPORT int rw_fatal(int expr, const char *filename, int line, const char *fmtspec,...)
Definition driver.cpp:1596
_TEST_EXPORT int rw_error(int, const char *, int, const char *,...)
Definition driver.cpp:1613
_TEST_EXPORT int rw_warn(int, const char *, int, const char *,...)
Definition driver.cpp:1647
_TEST_EXPORT int rw_opt_setlocales(int, char *[])
Definition locale.cpp:866
_TEST_EXPORT const char *const & rw_opt_locales
Definition locale.cpp:862
_TEST_EXPORT char * rw_locales(int=_UNUSED_CAT, const char *=0, bool=true)
Definition locale.cpp:368
#define RW_ASSERT(expr)
#define RW_COUNT_OF(x)
_TEST_EXPORT int rw_thread_pool(rw_thread_t *, _RWSTD_SIZE_T, rw_thread_attr_t *, rw_thread_proc *, void **, _RWSTD_SIZE_T=0)
_TEST_EXPORT int rw_get_cpus()
Definition thread.cpp:402
_TEST_EXPORT int rw_thread_pool_timeout_expired()
Definition thread.cpp:65
void pubsetg(charT *beg, charT *cur, charT *end)
std::basic_streambuf< charT, Traits > Base
void pubsetp(charT *beg, charT *cur, charT *end)
void pubsetp(charT *pbeg, std::streamsize n)
void pubsetg(const charT *gbeg, std::streamsize n)
wchar_t wcs_[BufferSize]
const char * locale_name_
char ncs_[BufferSize]