Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
22.locale.money.put.mt.cpp
Go to the documentation of this file.
1
29#include <ios> // for ios
30#include <iterator> // for ostreambuf_iterator
31#include <locale> // for locale, money_put
32
33#include <cstring> // for strlen()
34
35#include <rw_locale.h>
36#include <rw_thread.h>
37#include <rw_driver.h>
38#include <rw_valcmp.h> // for rw_strncmp ()
39
40
41// maximum number of threads allowed by the command line interface
42#define MAX_THREADS 32
43#define MAX_LOOPS 100000
44
45// default number of threads (will be adjusted to the number
46// of processors/cores later)
48
49// the number of times each thread should iterate (unless specified
50// otherwise on the command line)
51int opt_nloops = 100000;
52
53#if !defined (_RWSTD_OS_HP_UX) || defined (_ILP32)
54
55// number of locales to use
57
58#else // HP-UX in LP64 mode
59
60// work around a small locale cache on HP-UX in LP64 mode
61// (see STDCXX-812)
62int opt_nlocales = 9;
63
64#endif // HP-UX 32/64 bit mode
65
66// should all threads share the same set of locale objects instead
67// of creating their own?
69
70// default timeout used by each threaded section of this test
71int opt_timeout = 60;
72
73
75// array of locale names to use for testing
76static const char*
78
79// number of locale names in the array
80static std::size_t
82
83
85static const char n_money_vals[][20] = {
86 "1", "12", "123", "1234", "12345", "123456", "1234567", "12345678",
87 "-9", "-98", "-987", "-9876", "-98765", "-987654", "-9876543",
88 "1.9", "-12.89", "123.789", "-1234.6789", "-12345.56789"
89};
90
91#ifndef _RWSTD_NO_WCHAR_T
92
93static const wchar_t w_money_vals[][20] = {
94 L"1", L"12", L"123", L"1234", L"12345", L"123456", L"1234567",
95 L"-9", L"-98", L"-987", L"-9876", L"-98765", L"-987654", L"-9876543",
96 L"1.9", L"-12.89", L"123.789", L"-1234.6789", L"-12345.56789"
97};
98
99#endif // _RWSTD_NO_WCHAR_T
100
101//
102struct MyMoneyData
103{
104 enum { BufferSize = 16 };
105
106 enum PutId {
109 put_max
110 };
111
112 // name of the locale the data corresponds to
113 const char* locale_name_;
114
115 // optionally set to the named locale for threads to share
116 std::locale locale_;
117
118 // international or domestic format flag
119 bool intl_;
120
121 // the time struct used to generate strings below
123
124 // type of the data we created string from
126
127 // index into string array [n,w]_money_vals
128 std::size_t money_index_;
129
130 // narrow representations of money_
131 char ncs_ [BufferSize];
132
133#ifndef _RWSTD_NO_WCHAR_T
134
135 // wide representations of money_
136 wchar_t wcs_ [BufferSize];
137
138#endif // _RWSTD_NO_WCHAR_T
139
141
142
143template <class charT, class Traits>
144struct MyIos: std::basic_ios<charT, Traits>
145{
146 MyIos () {
147 this->init (0);
148 }
149};
150
151
152template <class charT, class Traits>
153struct MyStreambuf: std::basic_streambuf<charT, Traits>
154{
155 typedef std::basic_streambuf<charT, Traits> Base;
156
158 : Base () {
159 }
160
161 void pubsetp (charT *pbeg, std::streamsize n) {
162 this->setp (pbeg, pbeg + n);
163 }
164};
165
166
167extern "C" {
168
169bool test_char; // exercise money_put<char>
170bool test_wchar; // exercise money_put<wchar_t>
171
172
173static void*
174thread_func (void*)
175{
176 char ncs [MyMoneyData::BufferSize];
179 nio.rdbuf (&nsb);
180
181#ifndef _RWSTD_NO_WCHAR_T
182 wchar_t wcs [MyMoneyData::BufferSize];
185 wio.rdbuf (&wsb);
186#endif // _RWSTD_NO_WCHAR_T
187
188 for (int i = 0; i != opt_nloops; ++i) {
189
191 break;
192
193 // save the name of the locale
195
196 // construct a named locale, get a reference to the money_put
197 // facet from it and use it to format a random money value
198 const std::locale loc =
200 : std::locale (data.locale_name_);
201
202 if (test_char) {
203 // exercise the narrow char specialization of the facet
204
205 const std::money_put<char> &np =
206 std::use_facet<std::money_put<char> >(loc);
207
208 nio.imbue (loc);
209 nsb.pubsetp (ncs, RW_COUNT_OF (ncs));
210
211 switch (data.type_) {
213 *np.put (std::ostreambuf_iterator<char>(&nsb),
214 data.intl_, nio, ' ', data.money_value_) = '\0';
215 break;
217 *np.put (std::ostreambuf_iterator<char>(&nsb),
218 data.intl_, nio, ' ',
219 n_money_vals [data.money_index_]) = '\0';
220 break;
222 // avoid enumeration value `put_max' not handled in switch
223 // this case should never happen
224 break;
225 }
226
227 RW_ASSERT (!nio.fail ());
228 RW_ASSERT (!rw_strncmp (ncs, data.ncs_));
229
230 }
231
232 // both specializations may be tested at the same time
233
234 if (test_wchar) {
235 // exercise the wide char specialization of the facet
236
237#ifndef _RWSTD_NO_WCHAR_T
238
239 const std::money_put<wchar_t> &wp =
240 std::use_facet<std::money_put<wchar_t> >(loc);
241
242 wio.imbue (loc);
243 wsb.pubsetp (wcs, RW_COUNT_OF (wcs));
244
245 switch (data.type_) {
247 *wp.put (std::ostreambuf_iterator<wchar_t>(&wsb),
248 data.intl_, wio, ' ', data.money_value_) = L'\0';
249 break;
251 *wp.put (std::ostreambuf_iterator<wchar_t>(&wsb),
252 data.intl_, wio, ' ',
253 w_money_vals [data.money_index_]) = L'\0';
254 break;
256 // avoid enumeration value `put_max' not handled in switch
257 // this case should never happen
258 break;
259 }
260
261 RW_ASSERT (!wio.fail ());
262 RW_ASSERT (!rw_strncmp (wcs, data.wcs_));
263
264#endif // _RWSTD_NO_WCHAR_T
265
266 }
267 }
268
269 return 0;
270}
271
272} // extern "C"
273
274
276static int
277run_test (int, char**)
278{
281 nio.rdbuf (&nsb);
282
283#ifndef _RWSTD_NO_WCHAR_T
286 wio.rdbuf (&wsb);
287#endif // _RWSTD_NO_WCHAR_T
288
289 // find all installed locales for which setlocale (LC_ALL) succeeds
290 const char* const locale_list =
291 rw_opt_locales ? rw_opt_locales : rw_locales (_RWSTD_LC_ALL);
292
293 const std::size_t maxinx = RW_COUNT_OF (locales);
294
295 for (const char* name = locale_list;
296 *name;
297 name += std::strlen (name) + 1) {
298
299 const std::size_t inx = nlocales;
300 locales [inx] = name;
301
302 // fill in the money and results for this locale
305
306 try {
307 const std::locale loc (data.locale_name_);
308
309 // initialize with random but valid values
310
311 data.money_value_ = inx;
312 data.type_ = MyMoneyData::PutId (nlocales % MyMoneyData::put_max);
313 data.money_index_ = inx % RW_COUNT_OF (n_money_vals);
314
315 // exercise domestic formats every other iteration
316 // and international formats the rest
317 data.intl_ = 0 == (inx & 1);
318
319 // exercise postive and negative values
320 if (inx & 1)
321 data.money_value_ *= -1.;
322
323 // add some random fractional digits
324 if (inx & 2)
325 data.money_value_ += data.money_value_ / 3.14;
326
327 const std::money_put<char> &np =
328 std::use_facet<std::money_put<char> >(loc);
329
330 nio.imbue (loc);
331 nsb.pubsetp (data.ncs_, RW_COUNT_OF (data.ncs_));
332
333 switch (data.type_) {
335 *np.put (std::ostreambuf_iterator<char>(&nsb),
336 data.intl_, nio, ' ', data.money_value_) = '\0';
337 break;
339 *np.put (std::ostreambuf_iterator<char>(&nsb),
340 data.intl_, nio, ' ',
341 n_money_vals [data.money_index_]) = '\0';
342 break;
344 // avoid enumeration value `put_max' not handled in switch
345 // this case should never happen
346 break;
347 }
348
349 rw_assert (!nio.fail (), __FILE__, __LINE__,
350 "money_put<char>::put(...) "
351 "failed for locale(%#s)",
352 data.locale_name_);
353
354#ifndef _RWSTD_NO_WCHAR_T
355
356 const std::money_put<wchar_t> &wp =
357 std::use_facet<std::money_put<wchar_t> >(loc);
358
359 wio.imbue (loc);
360 wsb.pubsetp (data.wcs_, RW_COUNT_OF (data.wcs_));
361
362 switch (data.type_) {
364 *wp.put (std::ostreambuf_iterator<wchar_t>(&wsb),
365 data.intl_, wio, L' ', data.money_value_) = '\0';
366 break;
368 *wp.put (std::ostreambuf_iterator<wchar_t>(&wsb),
369 data.intl_, wio, L' ',
370 w_money_vals [data.money_index_]) = L'\0';
371 break;
373 // avoid enumeration value `put_max' not handled in switch
374 // this case should never happen
375 break;
376 }
377
378 rw_assert (!nio.fail (), __FILE__, __LINE__,
379 "money_put<wchar_t>::put(...) "
380 "failed for locale(%#s)",
381 data.locale_name_);
382
383#endif // _RWSTD_NO_WCHAR_T
384
386 data.locale_ = loc;
387
388 nlocales += 1;
389
390 }
391 catch (...) {
392 rw_warn (!rw_opt_locales, 0, __LINE__,
393 "failed to create locale(%#s)", name);
394 }
395
396 if (nlocales == maxinx || nlocales == std::size_t (opt_nlocales))
397 break;
398 }
399
400 // avoid divide by zero in thread if there are no locales to test
401 rw_fatal (nlocales != 0, 0, __LINE__,
402 "failed to create one or more usable locales!");
403
404 rw_info (0, 0, 0,
405 "testing std::money_put<charT> with %d thread%{?}s%{;}, "
406 "%d iteration%{?}s%{;} each, in %zu locales { %{ .*A@} }",
409 nlocales, int (nlocales), "%#s", locales);
410
411 rw_info (0, 0, 0, "exercising std::money_put<char>");
412
413 test_char = true;
414 test_wchar = false;
415
416 // create and start a pool of threads and wait for them to finish
417 int result =
418 rw_thread_pool (0, std::size_t (opt_nthreads), 0,
419 thread_func, 0, std::size_t (opt_timeout));
420
421 rw_error (result == 0, 0, __LINE__,
422 "rw_thread_pool(0, %d, 0, %{#f}, 0) failed",
423 opt_nthreads, thread_func);
424
425#ifndef _RWSTD_NO_WCHAR_T
426
427 rw_info (0, 0, 0, "exercising std::money_put<wchar_t>");
428
429 test_char = false;
430 test_wchar = true;
431
432 // start a pool of threads to exercise wstring thread safety
433 result =
434 rw_thread_pool (0, std::size_t (opt_nthreads), 0,
435 thread_func, 0, std::size_t (opt_timeout));
436
437 rw_error (result == 0, 0, __LINE__,
438 "rw_thread_pool(0, %d, 0, %{#f}, 0) failed",
439 opt_nthreads, thread_func);
440
441 // exercise both the char and the wchar_t specializations
442 // at the same time
443
444 rw_info (0, 0, 0,
445 "exercising both std::money_put<char> "
446 "and std::money_put<wchar_t>");
447
448 test_char = true;
449 test_wchar = true;
450
451 // start a pool of threads to exercise wstring thread safety
452 result =
453 rw_thread_pool (0, std::size_t (opt_nthreads), 0,
454 thread_func, 0, std::size_t (opt_timeout));
455
456 rw_error (result == 0, 0, __LINE__,
457 "rw_thread_pool(0, %d, 0, %{#f}, 0) failed",
458 opt_nthreads, thread_func);
459
460#endif // _RWSTD_NO_WCHAR_T
461
462 return result;
463}
464
465
467int main (int argc, char *argv[])
468{
469#ifdef _RWSTD_REENTRANT
470
471 // set nthreads to the greater of the number of processors
472 // and 2 (for uniprocessor systems) by default
474 if (opt_nthreads < 2)
475 opt_nthreads = 2;
476
477#endif // _RWSTD_REENTRANT
478
479 return rw_test (argc, argv, __FILE__,
480 "lib.locale.money.put",
481 "thread safety", run_test,
482 "|-soft-timeout#0 " // must be non-negative
483 "|-nloops#0 " // must be non-negative
484 "|-nthreads#0-* " // must be in [0, MAX_THREADS]
485 "|-nlocales#0 " // arg must be non-negative
486 "|-locales= " // must be provided
487 "|-shared-locale# ",
489 &opt_nloops,
490 int (MAX_THREADS),
495}
#define i
Definition 17.names.cpp:126
#define loc
Definition 17.names.cpp:492
#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
int opt_shared_locale
#define MAX_THREADS
struct MyMoneyData my_money_data[MAX_THREADS]
void run_test(const ForwardIterator &dummy, T *, Predicate *pred)
#define main
const unsigned nlocales
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_assert(int, const char *, int, const char *,...)
Definition driver.cpp:1630
_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
_TEST_EXPORT int rw_strncmp(const char *, const char *, _RWSTD_SIZE_T=_RWSTD_SIZE_MAX, int=CMP_NULTERM)
char ncs_[BufferSize]
wchar_t wcs_[BufferSize]
const char * locale_name_
std::basic_streambuf< charT, Traits > Base
void pubsetp(charT *beg, charT *cur, charT *end)
void pubsetp(charT *pbeg, std::streamsize n)