Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
27.std.manip.cpp
Go to the documentation of this file.
1
29#include <rw/_defs.h>
30
31#if !defined _MSC_VER || _MSC_VER > 1310
32
33 // disabled for MSVC to work around its innumerable bugs
34
35 // prevent the inclusion of non-essential public headers; only headers
36 // necessary to compile each header #included here are #included
37 // will produce errors if any templates defined in the header are
38 // instantiated without the names they use having been completely defined
39# define _RWSTD_NO_REDUNDANT_DEFINITIONS
40
41 // prevent out-of-line template definitions in .cc files from being
42 // explicitly #included during the processing of library headers (faster
43 // compilation) assumes that the test doesn't instantiate those templates
44 // on types other than those they were explcitly instantiated on (will
45 // have no effect if explicit instantiation is disabled or unsupported)
46# define _RWSTD_NO_TEMPLATE_DEFINITIONS
47#endif // !defined _MSC_VER || _MSC_VER > 1310
48
49
50#include <climits>
51#include <iomanip>
52#include <istream>
53#include <ostream>
54#include <sstream>
55
56#include <rw_driver.h>
57
58
60// user-defined Traits type exercises the ability to instantiate
61// std::ws and any supporting templates on types other than the
62// default std::char_traits<charT>
63
64template <class charT>
65struct CharTraits: std::char_traits<charT> { /* empty */ };
66
67template < typename T >
68/*static*/ const char* type_name ();
69
71/*static*/ const char*
73{
74 return "char";
75}
76
78/*static*/ const char*
79type_name< std::char_traits<char> > ()
80{
81 return "std::char_traits<char>";
82}
83
85/*static*/ const char*
86type_name< CharTraits<char> > ()
87{
88 return "CharTraits<char>";
89}
90
91#ifndef _RWSTD_NO_WCHAR_T
92
94/*static*/ const char*
96{
97 return "wchar_t";
98}
99
101/*static*/ const char*
102type_name< std::char_traits<wchar_t> > ()
103{
104 return "std::char_traits<wchar_t>";
105}
106
108/*static*/ const char*
109type_name< CharTraits<wchar_t> > ()
110{
111 return "CharTraits<wchar_t>";
112}
113
114#endif // _RWSTD_NO_WCHAR_T
115
116
118enum Manip {
124 setw
126
127
128template <class charT, class Traits>
129struct StreamBuf: std::basic_streambuf<charT, Traits> { /* empty */ };
130
131
132template <class charT, class Traits>
133static int
134test (int lineno, Manip m, int iarg, charT carg)
135{
136 static/*?*/ const char* const cname = type_name< charT > ();
137 static/*?*/ const char* const tname = type_name< Traits > ();
138
139 typedef std::ios_base::fmtflags Fmtflags;
140 typedef std::basic_istream<charT, Traits> Istream;
141 typedef std::basic_ostream<charT, Traits> Ostream;
142 typedef std::basic_iostream<charT, Traits> IOstream;
143
145
146 // stream objects used to apply manipulators to
147 Istream in1 (&sb);
148 Ostream out1 (&sb);
149 IOstream inout1 (&sb);
150
151 // stream objects used to call member functions on
152 Istream in2 (&sb);
153 Ostream out2 (&sb);
154 IOstream inout2 (&sb);
155
156 int nfailed = 0;
157
158 switch (m) {
159
160 case resetiosflags: { // exercise 27.6.3, p3
161
162 Istream &in3 = in1 >> std::resetiosflags (Fmtflags (iarg));
163 in2.setf (Fmtflags (0), Fmtflags (iarg));
164
165 if (in2.flags () != in3.flags ()) {
166 ++nfailed;
167 rw_assert (0, __FILE__, lineno,
168 "in >> std::resetiosflags(%{If}) with basic_istream "
169 "<%s, %s >", iarg, cname, tname);
170 }
171
172 Ostream &out3 = out1 << std::resetiosflags (Fmtflags (iarg));
173 out2.setf (Fmtflags (0), Fmtflags (iarg));
174
175 if (out2.flags () != out3.flags ()) {
176 ++nfailed;
177 rw_assert (0, __FILE__, lineno,
178 "out << std::resetiosflags(%{If}) with basic_ostream "
179 "<%s, %s >", iarg, cname, tname);
180 }
181
182 Ostream &inout3 = inout1 << std::resetiosflags (Fmtflags (iarg));
183 inout2.setf (Fmtflags (0), Fmtflags (iarg));
184
185 if (inout2.flags () != inout3.flags ()) {
186 ++nfailed;
187 rw_assert (0, __FILE__, lineno,
188 "io << std::resetiosflags(%{If}) with basic_iostream "
189 "<%s, %s >", iarg, cname, tname);
190 }
191
192 break;
193 }
194
195 case setiosflags: { // exercise 27.6.3, p3
196
197 Istream &in3 = in1 >> std::setiosflags (Fmtflags (iarg));
198 in2.setf (Fmtflags (iarg));
199
200 if (in2.flags () != in3.flags ()) {
201 ++nfailed;
202 rw_assert (0, __FILE__, lineno,
203 "std::setiosflags (%s) with basic_istream "
204 "<%s, %s >", iarg, cname, tname);
205 }
206
207 Ostream &out3 = out1 << std::setiosflags (Fmtflags (iarg));
208 out2.setf (Fmtflags (iarg));
209
210 if (out2.flags () != out3.flags ()) {
211 ++nfailed;
212 rw_assert (0, __FILE__, lineno,
213 "std::setiosflags (%s) with basic_ostream "
214 "<%s, %s >", iarg, cname, tname);
215 }
216
217 Ostream &inout3 = inout1 << std::setiosflags (Fmtflags (iarg));
218 inout2.setf (Fmtflags (iarg));
219
220 if (inout2.flags () != inout3.flags ()) {
221 ++nfailed;
222 rw_assert (0, __FILE__, lineno,
223 "std::setiosflags (%s) with basic_iostream "
224 "<%s, %s >", iarg, cname, tname);
225 }
226
227 break;
228 }
229
230 case setbase: { // exercise 27.6.3, p5
231
232 out1 << std::setbase (iarg);
233
234 if (0 == iarg || 2 == iarg || 8 == iarg || 10 ==iarg || 16 == iarg) {
235 out2.setf ( 8 == iarg ? std::ios_base::oct
236#ifndef _RWSTD_NO_EXT_BIN_IO
237 : 2 == iarg ? std::ios_base::bin
238#endif // _RWSTD_NO_EXT_BIN_IO
239 : 10 == iarg ? std::ios_base::dec
240 : 16 == iarg ? std::ios_base::hex
241 : Fmtflags (0),
242 std::ios_base::basefield);
243
244 if (out1.flags () != out2.flags ()) {
245 ++nfailed;
246 rw_assert (0, __FILE__, lineno,
247 "std::setbase (%d)", iarg);
248 }
249 }
250
251 std::basic_ostringstream<charT, std::char_traits<charT> > ostrm;
252
253 static const struct {
254 charT str[12];
255 } num[] = {
256 { { '1', '2', '3', '4', '\0' } }, // dec
257 { { 'm', 'c', 'c', 'x', 'x', 'x', 'i', 'v', '\0' } }, // roman
258 { { '1', '0', '0', '1', '1', '0', '1', '0', '0', '1', '0', '\0' } },
259 { { '1', '2', '0', '0', '2', '0', '1', '\0' } }, // 3
260 { { '1', '0', '3', '1', '0', '2', '\0' } }, // 4
261 { { '1', '4', '4', '1', '4', '\0' } }, // 5
262 { { '5', '4', '1', '4', '\0' } }, // 6
263 { { '3', '4', '1', '2', '\0' } }, // 7
264 { { '2', '3', '2', '2', '\0' } }, // oct
265 { { '1', '6', '2', '1', '\0' } }, // 9
266 { { '1', '2', '3', '4', '\0' } }, // dec
267 { { 'a', '2', '2', '\0' } }, // 11
268 { { '8', '6', 'a', '\0' } }, // 12
269 { { '7', '3', 'c', '\0' } }, // 13
270 { { '6', '4', '2', '\0' } }, // 14
271 { { '5', '7', '4', '\0' } }, // 15
272 { { '4', 'd', '2', '\0' } }, // hex
273 { { '4', '4', 'a', '\0' } }, // 17
274 { { '3', 'e', 'a', '\0' } }, // 18
275 { { '3', '7', 'i', '\0' } }, // 19
276 { { '3', '1', 'e', '\0' } }, // 20
277 { { '2', 'g', 'g', '\0' } }, // 21
278 { { '2', 'c', '2', '\0' } }, // 22
279 { { '2', '7', 'f', '\0' } }, // 23
280 { { '2', '3', 'a', '\0' } }, // 24
281 { { '1', 'o', '9', '\0' } }, // 25
282 { { '1', 'l', 'c', '\0' } }, // 26
283 { { '1', 'i', 'j', '\0' } }, // 27
284 { { '1', 'g', '2', '\0' } }, // 28
285 { { '1', 'd', 'g', '\0' } }, // 29
286 { { '1', 'b', '4', '\0' } }, // 30
287 { { '1', '8', 'p', '\0' } }, // 31
288 { { '1', '6', 'i', '\0' } }, // 32
289 { { '1', '4', 'd', '\0' } }, // 33
290 { { '1', '2', 'a', '\0' } }, // 34
291 { { '1', '0', '9', '\0' } }, // 35
292 { { 'y', 'a', '\0' } } // 36
293 };
294
295 ostrm << std::setbase (iarg);
296 ostrm << 1234;
297
298 const int inx = iarg >= 0 && iarg <= 36 ? iarg : 0;
299
300 if (ostrm.str () != num [inx].str) {
301 ++nfailed;
302 rw_assert (0, __FILE__, __LINE__,
303 "std::setbase (%d) inserted 1234 as \"%s\"; "
304 "expected \"%s\"", iarg,
305 ostrm.str ().data (), num [inx].str);
306 }
307
308 break;
309 }
310
311 case setfill: { // exercise 27.6.3, p6
312 Istream &in3 = in1 >> std::setfill (carg);
313 in2.fill (carg);
314
315 if (in2.fill () != in3.fill ()) {
316 ++nfailed;
317 rw_assert (0, __FILE__, lineno,
318 "in >> std::setfill('%c') with basic_istream"
319 "<%s, %s >", carg, cname, tname);
320 }
321
322 Ostream &out3 = out1 << std::setfill (carg);
323 out2.fill (carg);
324
325 if (out2.fill () != out3.fill ()) {
326 ++nfailed;
327 rw_assert (0, __FILE__, lineno,
328 "out << std::setfill('%c') with basic_ostream"
329 "<%s, %s >", carg, cname, tname);
330 }
331
332 Ostream &inout3 = inout1 << std::setfill (carg);
333 inout2.fill (carg);
334
335 if (inout2.fill () != inout3.fill ()) {
336 ++nfailed;
337 rw_assert (0, __FILE__, lineno,
338 "io << std::setfill('%c') with basic_iostream"
339 "<%s, %s >", carg, cname, tname);
340 }
341
342 break;
343 }
344
345 case setprecision: { // exercise 27.6.3, p7
346
347 Istream &in3 = in1 >> std::setprecision (iarg);
348 in2.precision (std::streamsize (iarg));
349
350 if (in2.precision () != in3.precision ()) {
351 ++nfailed;
352 rw_assert (0, __FILE__, lineno,
353 "std::setprecision (%d) with basic_istream "
354 "<%s, %s >", iarg, cname, tname);
355 }
356
357 Ostream &out3 = out1 << std::setprecision (iarg);
358 out2.precision (std::streamsize (iarg));
359
360 if (out2.precision () != out3.precision ()) {
361 ++nfailed;
362 rw_assert (0, __FILE__, lineno,
363 "std::setprecision (%d) with basic_ostream "
364 "<%s, %s >", iarg, cname, tname);
365 }
366
367 Ostream &inout3 = inout1 << std::setprecision (iarg);
368 inout2.precision (std::streamsize (iarg));
369
370 if (inout2.precision () != inout3.precision ()) {
371 ++nfailed;
372 rw_assert (0, __FILE__, lineno,
373 "std::setprecision (%d) with basic_iostream "
374 "<%s, %s >", iarg, cname, tname);
375 }
376
377 break;
378 }
379
380 case setw: { // exercise 27.6.3, p8
381
382 Istream &in3 = in1 >> std::setw (iarg);
383 in2.width (std::streamsize (iarg));
384
385 if (in2.width () != in3.width ()) {
386 ++nfailed;
387 rw_assert (0, __FILE__, lineno,
388 "std::setw (%d) with basic_istream<%s, %s >",
389 iarg, cname, tname);
390 }
391
392 Ostream &out3 = out1 << std::setw (iarg);
393 out2.width (std::streamsize (iarg));
394
395 if (out2.width () != out3.width ()) {
396 ++nfailed;
397 rw_assert (0, __FILE__, lineno,
398 "std::setw (%d) with basic_ostream<%s, %s >",
399 iarg, cname, tname);
400 }
401
402 Ostream &inout3 = inout1 << std::setw (iarg);
403 inout2.width (std::streamsize (iarg));
404
405 if (inout2.width () != inout3.width ()) {
406 ++nfailed;
407 rw_assert (0, __FILE__, lineno,
408 "std::setw (%d) with basic_iostream<%s, %s >",
409 iarg, cname, tname);
410 }
411
412 break;
413 }
414
415 }
416
417 return nfailed;
418}
419
420
422// for convenience
423#define Boolalpha std::ios_base::boolalpha
424#define Dec std::ios_base::dec
425#define Fixed std::ios_base::fixed
426#define Hex std::ios_base::hex
427#define Internal std::ios_base::internal
428#define Left std::ios_base::left
429#define Oct std::ios_base::oct
430#define Right std::ios_base::right
431#define Scientific std::ios_base::scientific
432#define Showbase std::ios_base::showbase
433#define Showpoint std::ios_base::showpoint
434#define Showpos std::ios_base::showpos
435#define Skipws std::ios_base::skipws
436#define Unitbuf std::ios_base::unitbuf
437#define Uppercase std::ios_base::uppercase
438#define Bin std::ios_base::bin
439#define Adjustfield std::ios_base::adjustfield
440#define Basefield std::ios_base::basefield
441#define Floatfield std::ios_base::floatfield
442#define Nolock std::ios_base::nolock
443#define Nolockbuf std::ios_base::nolockbuf
444
445
446template <class charT, class Traits>
447static void do_test ()
448{
449#define TEST(M, V, C) \
450 ++ntried; nfailed += test< charT, Traits > (__LINE__, M, V, C)
451
452 int ntried = 0;
453 int nfailed = 0;
454
456 // exercise std::resetiosflags
457
458 const char* const cname = type_name< charT > ();
459 const char* const tname = type_name< Traits > ();
460
461 rw_info (0, __FILE__, __LINE__,
462 "27.6.3, p3 - std::resetiosflags(ios_base::"
463 "fmtflags) with basic_{i,o,io}stream<%s, %s >",
464 cname, tname);
465
481
482#ifndef _RWSTD_NO_EXT_BIN_IO
483
485
486#endif // _RWSTD_NO_EXT_BIN_IO
487
491
492#ifndef _RWSTD_NO_EXT_REENTRANT_IO
493
496
497#endif // _RWSTD_NO_EXT_REENTRANT_IO
498
499
500 TEST (resetiosflags, Oct | Dec, charT ());
501 TEST (resetiosflags, Oct | Hex, charT ());
502 TEST (resetiosflags, Dec | Hex, charT ());
503 TEST (resetiosflags, Oct | Dec | Hex, charT ());
504
509
513
514 if (!nfailed)
515 rw_assert (true, __FILE__, __LINE__,
516 "std::resetiosflags failed %d out "
517 "of %d assertions", nfailed, ntried);
518
520 // exercise std::setiosflags
521
522 rw_info (0, __FILE__, __LINE__,
523 "27.6.3, p4 - std::setiosflags(ios_base::"
524 "fmtflags) with basic_{i,o,io}stream<%s, %s >",
525 cname, tname);
526
527 ntried = nfailed = 0;
528
530 TEST (setiosflags, Dec, charT ());
532 TEST (setiosflags, Hex, charT ());
534 TEST (setiosflags, Left, charT ());
535 TEST (setiosflags, Oct, charT ());
544
545#ifndef _RWSTD_NO_EXT_BIN_IO
546
547 TEST (setiosflags, Bin, charT ());
548
549#endif // _RWSTD_NO_EXT_BIN_IO
550
556
557 if (!nfailed)
558 rw_assert (true, __FILE__, __LINE__,
559 "std::setiosflags failed %d out "
560 "of %d assertions", nfailed, ntried);
561
563 // exercise std::setbase
564
565 rw_info (0, __FILE__, __LINE__,
566 "27.6.3, p5 - std::setbase(int) with "
567 "basic_{i,o,io}stream<%s, %s >", cname, tname);
568
569 ntried = nfailed = 0;
570
571 TEST (setbase, 0, charT ());
572 TEST (setbase, 8, charT ());
573 TEST (setbase, 10, charT ());
574 TEST (setbase, 16, charT ());
575
576#ifndef _RWSTD_NO_EXT_BIN_IO
577
578 TEST (setbase, 2, charT ());
579
580#endif // _RWSTD_NO_EXT_BIN_IO
581
582#ifndef _RWSTD_NO_EXT_SETBASE
583
584 TEST (setbase, 0, charT ()); // autodetect
585 TEST (setbase, 1, charT ()); // roman
586 TEST (setbase, 2, charT ()); // bin
587 TEST (setbase, 3, charT ());
588 TEST (setbase, 4, charT ());
589 TEST (setbase, 5, charT ());
590 TEST (setbase, 6, charT ());
591 TEST (setbase, 7, charT ());
592 TEST (setbase, 8, charT ()); // oct
593 TEST (setbase, 9, charT ());
594 TEST (setbase, 10, charT ()); // dec
595 TEST (setbase, 11, charT ());
596 TEST (setbase, 12, charT ());
597 TEST (setbase, 16, charT ()); // hex
598 TEST (setbase, 17, charT ());
599 TEST (setbase, 36, charT ()); // 0-9, a-z
600 TEST (setbase, -1, charT ());
601 TEST (setbase, -2, charT ());
602
603#endif // _RWSTD_NO_EXT_SETBASE
604
605 if (!nfailed)
606 rw_assert (true, __FILE__, __LINE__,
607 "std::setbase failed %d out "
608 "of %d assertions", nfailed, ntried);
609
611 // exercise std::setfill
612
613 rw_info (0, __FILE__, __LINE__,
614 "27.6.3, p6 - std::setfill(%s) with "
615 "basic_{i,o,io}stream<%s, %s >", cname, cname, tname);
616
617 ntried = nfailed = 0;
618
619 TEST (setfill, 0, charT ('\0'));
620 TEST (setfill, 0, charT ('\a'));
621 TEST (setfill, 0, charT ('\n'));
622 TEST (setfill, 0, charT ('\r'));
623 TEST (setfill, 0, charT ('\t'));
624 TEST (setfill, 0, charT ('\v'));
625 TEST (setfill, 0, charT (' '));
626 TEST (setfill, 0, charT ('*'));
627 TEST (setfill, 0, charT ('#'));
628
629 if (!nfailed)
630 rw_assert (true, __FILE__, __LINE__,
631 "std::setfill failed %d out "
632 "of %d assertions", nfailed, ntried);
633
635 // exercise std::setprecision
636
637 rw_info (0, __FILE__, __LINE__,
638 "27.6.3, p7 - std::setprecision(int) with "
639 "basic_{i,o,io}stream<%s, %s >", cname, tname);
640
641 ntried = nfailed = 0;
642
643 TEST (setprecision, 0, charT ());
644 TEST (setprecision, 1, charT ());
645 TEST (setprecision, 2, charT ());
648 TEST (setprecision, -1, charT ());
649 TEST (setprecision, -2, charT ());
652
653 if (!nfailed)
654 rw_assert (true, __FILE__, __LINE__,
655 "std::setprecision failed %d out "
656 "of %d assertions", nfailed, ntried);
657
659 // exercise std::setw
660
661 rw_info (0, __FILE__, __LINE__,
662 "27.6.3, p8 - std::setw(int) with "
663 "basic_{i,o,io}stream<%s, %s >", cname, tname);
664
665 ntried = nfailed = 0;
666
667 TEST (setw, 0, charT ());
668 TEST (setw, 1, charT ());
669 TEST (setw, 2, charT ());
670 TEST (setw, SHRT_MAX, charT ());
671 TEST (setw, INT_MAX, charT ());
672 TEST (setw, -1, charT ());
673 TEST (setw, -2, charT ());
674 TEST (setw, SHRT_MIN, charT ());
675 TEST (setw, INT_MIN, charT ());
676
677 if (!nfailed)
678 rw_assert (true, __FILE__, __LINE__,
679 "std::setw failed %d out "
680 "of %d assertions", nfailed, ntried);
681}
682
683
685static int
686run_test (int /*argc*/, char* /*argv*/ [])
687{
688 do_test<char, std::char_traits<char> > ();
689 do_test<char, CharTraits<char> > ();
690
691#ifndef _RWSTD_NO_WCHAR_T
692
693 do_test<wchar_t, std::char_traits<wchar_t> > ();
694 do_test<wchar_t, CharTraits<wchar_t> > ();
695
696#endif // _RWSTD_NO_WCHAR_T
697
698 return 0;
699}
700
701
702/*extern*/ int
703main (int argc, char* argv [])
704{
705 return rw_test (argc, argv, __FILE__,
706 "[lib.std.manip]",
707 "27.6.3 Standard manipulators",
708 run_test, "", 0);
709}
710
#define m
Definition 17.names.cpp:129
#define ostrm
Definition 17.names.cpp:512
#define sb
Definition 17.names.cpp:542
#define lineno
Definition 17.names.cpp:491
#define charT
Definition 17.names.cpp:456
#define inx
Definition 17.names.cpp:484
#define num
Definition 17.names.cpp:505
#define dec
#define hex
#define bin
#define basefield
void do_test(int line, const char *src, std::size_t resoff, ForwardIterator dummy_iter, const T *, const char *predname)
void run_test(const ForwardIterator &dummy, T *, Predicate *pred)
#define Showpos
@ setw
@ setbase
@ resetiosflags
@ setfill
@ setiosflags
@ setprecision
#define Oct
#define Left
_RWSTD_SPECIALIZED_FUNCTION const char * type_name< wchar_t >()
#define Uppercase
#define Showbase
#define Internal
#define TEST(M, V, C)
_RWSTD_SPECIALIZED_FUNCTION const char * type_name< char >()
#define Adjustfield
const char * type_name()
#define Unitbuf
#define Showpoint
#define Dec
#define Basefield
#define Right
#define Hex
#define Nolockbuf
#define Skipws
#define Boolalpha
#define Fixed
#define Scientific
#define Floatfield
#define main
#define _RWSTD_SPECIALIZED_FUNCTION
Definition _defs.h:556
const char * str
Definition exec.cpp:170
#define SHRT_MAX
Definition limits.h:64
#define INT_MIN
Definition limits.h:80
#define INT_MAX
Definition limits.h:76
#define SHRT_MIN
Definition limits.h:68
Definition json.h:5363
_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_assert(int, const char *, int, const char *,...)
Definition driver.cpp:1630