Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
27.istream.unformatted.get.cpp
Go to the documentation of this file.
1
30#include <fstream>
31#include <istream>
32#include <sstream>
33#include <cstddef> // for size_t
34
35#include <rw_driver.h> // for rw_test(), ...
36#include <rw_char.h> // for UserTraits
37#include <rw_streambuf.h> // for MyStreambuf
38
39
41#define Bad std::ios_base::badbit
42#define Eof std::ios_base::eofbit
43#define Fail std::ios_base::failbit
44#define Good std::ios_base::goodbit
45
46
47template <class charT, class Traits>
48void
50 const char* cname,
51 int lineno,
52 const char* fun,
53 std::basic_istream<charT, Traits>& strm,
54 int gcount,
55 int nexpect,
56 int rdstate,
57 int exceptions,
58 int caught)
59{
60 static const char* const exnames[] = {
61 "none", "std::ios_base::failure",
62 "streambuf_exception", "unknown exception"
63 };
64
65 const char* const caught_what = exnames [caught];
66
67 // verify that stream is in the expected state
68 rw_assert (strm.rdstate () == rdstate, 0, lineno,
69 "line %d: basic_istream<%s>::%s; "
70 "rdstate () == %{Is}, got %{Is}",
71 __LINE__, cname, fun, rdstate, strm.rdstate ());
72
73
74 if (sb.throw_set_ && sb.ncalls (sb.threw_) == sb.fail_when_) {
75
76 // verify that the same exception (and not ios_base::failure)
77 // as the one thrown from basic_filebuf has been propagated
78 // and caught when badbit is set in exceptions, and that no
79 // exception has been thrown if exceptions is clear
80 if (exceptions & Bad && caught != 2) {
81
82 rw_assert (false, 0, lineno,
83 "line %d: basic_istream<%s>::%s failed "
84 "to propagate an exception thrown "
85 "by basic_filebuf; caught %s instead",
86 __LINE__, cname, fun,
87 caught_what);
88 }
89 else if (!(exceptions & Bad) && caught) {
90
91 rw_assert (false, 0, lineno,
92 "line %d: basic_istream<%s>::%s propagated "
93 "an exception thrown by basic_filebuf when "
94 "ios_base::badbit is clear in exceptions",
95 __LINE__, cname, fun);
96 }
97
98 // verify that the original gcount of 0 was unaffected
99 // (for non-array forms of unformatted input functions)
100 // or that it increased exactly `fail_when_'
101 const std::streamsize actual = strm.gcount () - gcount;
102
103 rw_assert (!actual || actual == sb.fail_when_ - 1, 0, lineno,
104 "line %d: basic_istream<%s>::%s changed "
105 "gcount () from %d to %d after a failure; "
106 "either no change from 0 expected, or "
107 "an increase to %d from 0 expected",
108 __LINE__, cname, fun,
109 gcount, strm.gcount (), sb.fail_when_ - 1);
110 }
111 else {
112
113 // verify that ios_base::failure has been thrown (and caught)
114 // if badbit is set in exceptions
115 rw_assert (!(exceptions & strm.rdstate ()) || caught == 1,
116 0, lineno,
117 "line %d: basic_istream<%s>::%s set %{Is} "
118 "but failed to throw ios_base::failure "
119 "when the same bit is set in exceptions",
120 __LINE__, cname, fun, rdstate);
121
122 // verify that gcount has the expected value
123
124 const std::streamsize actual = strm.gcount () - gcount;
125
126 rw_assert (nexpect == actual, 0, lineno,
127 "line %d: basic_istream<%s>::%s changed "
128 "gcount () from %d to %d after a successful "
129 "extraction; a change from 0 to %d expected",
130 __LINE__, cname, fun,
131 gcount, strm.gcount (), nexpect);
132 }
133}
134
135
137#define TRY_GET(expr) \
138 try { \
139 expr; \
140 } \
141 catch (const std::ios_base::failure&) { \
142 caught = 1; \
143 } \
144 catch (Exception& e) { \
145 caught = ex_stream == e.id_ ? 2 : 3; \
146 } \
147 catch (...) { \
148 caught = 3; \
149 } (void)0
150
151
152
155// 27.6.1.3 Unformatted input functions [lib.istream.unformatted]
156//
157// -1- Each unformatted input function begins execution by constructing
158// an object of class sentry with the default argument noskipws
159// (second) argument true. If the sentry object returns true, when
160// converted to a value of type bool, the function endeavors to
161// obtain the requested input. If an exception is thrown during
162// input then ios::badbit is turned on284) in *this?s error state.
163// If (exception() & badbit) != 0 then the exception is rethrown.
164// It also counts the number of characters extracted. If no exception
165// has been thrown it ends by storing the count in a member object
166// and returning the value specified. In any event the sentry object
167// is destroyed before leaving the unformatted input function.
168
169template <class charT, class Traits>
170void
171test_get_void (charT, const char *cname,
172 const Traits*, int lineno,
173 const char *str,
174 std::size_t strsize,
175 int ival,
176 int rdstate,
177 int exceptions,
178 int failure,
179 int fail_when)
180{
181 // int_type get();
182 // -3- Effects: Extracts a character c, if one is available.
183 // Otherwise, the function calls setstate(failbit),
184 // which may throw ios_base::failure (27.4.4.3),
185 // -4- Returns: c if available, otherwise traits::eof().
186
188 typedef std::basic_istream<charT, Traits> Istream;
189 typedef typename Istream::int_type int_type;
190
191 const int_type val = int_type (ival);
192
193 // create a test streambuf object initialized with `str'
194 Streambuf sb (str, strsize, failure, fail_when);
195
196 Istream strm (&sb);
197
198 strm.exceptions (std::ios_base::iostate (exceptions));
199
200 const std::streamsize gcount = strm.gcount ();
201
202 int_type got = Traits::eof ();
203
204 int caught = 0;
205
206 TRY_GET (got = strm.get ());
207
208 check_failure (sb, cname, lineno, "get()", strm, int (gcount),
209 !rdstate, rdstate, exceptions, caught);
210
211 // verify that the extracted value is the one expected
212
213 if (!sb.throw_set_ && !caught && val != got) {
214
215 const charT cval = Traits::to_char_type (val);
216 const charT cgot = Traits::to_char_type (got);
217
218 rw_assert (false, __FILE__, lineno,
219 "line %d: basic_istream<%s>::get () == %{#lc}, got %{#lc}",
220 __LINE__, cname, cval, cgot);
221 }
222
223 rw_assert (val == Traits::eof () || !(gcount || 1 != strm.gcount ()),
224 0, lineno,
225 "line %d: basic_istream<%s>::get () changed "
226 "gcount () from %d to %d after a successful "
227 "extraction; 0 to 1 expected",
228 __LINE__, cname,
229 gcount, strm.gcount ());
230}
231
232
234template <class charT>
235void test_get_void (charT, const char *cname)
236{
237#define T charT (), cname, (UserTraits<charT>*)0, __LINE__
238#define TEST test_get_void
239
240 rw_info (0, 0, 0, "27.6.1.3, p3 - std::basic_istream<%s>::get ()",
241 cname);
242
243 typename UserTraits<charT>::int_type &eof =
245
246 eof = ~0;
247
248 TEST (T, "", 0, eof, Eof | Fail, Good, 0, -1);
249 TEST (T, "", 1, '\0', Good, Good, 0, -1);
250 TEST (T, "a", 1, 'a', Good, Good, 0, -1);
251 TEST (T, " b", 2, ' ', Good, Good, 0, -1);
252 TEST (T, "\x80", 1, 0x80, Good, Good, 0, -1);
253 TEST (T, "\xff ", 2, 0xff, Good, Good, 0, -1);
254
255 TEST (T, "c", 1, eof, Eof | Fail, Good, Underflow, 1);
256 TEST (T, "d", 1, eof, Bad, Good, Underflow | Throw, 1);
257
258 eof = 256;
259
260 TEST (T, "", 0, eof, Eof | Fail, Good, 0, 0);
261 TEST (T, "f", 1, 'f', Good, Good, 0, 0);
262 TEST (T, "g", 1, eof, Eof | Fail, Good, Underflow, 1);
263 TEST (T, "h", 1, eof, Bad, Good, Underflow | Throw, 1);
264
265 eof = '\0';
266
267 TEST (T, "\0", 1, eof, Eof | Fail, Good, 0, -1);
268 TEST (T, "\1", 1, eof, Eof | Fail, Good, Underflow, 1);
269 TEST (T, "\2", 1, eof, Bad, Good, Underflow | Throw, 1);
270}
271
272
274template <class charT, class Traits>
275void test_get_char (charT, const char* cname,
276 const Traits*, int lineno,
277 const char *str,
278 std::size_t strsize,
279 char initval,
280 char expectval,
281 int rdstate,
282 int exceptions,
283 int failure,
284 int fail_when)
285{
286 // basic_istream<charT, Traits>& get (char_type&);
287 // -5- Effects: Extracts a character, if one is available,
288 // and assigns it to c.(285) Otherwise, the function calls
289 // setstate(failbit) (which may throw ios_base::failure
290 // (27.4.4.3)).
291 // -6- Returns: *this.
292 // ---
293 // 285) Note that this function is not overloaded on types signed
294 // charand unsigned char.
295
297 typedef std::basic_istream<charT, Traits> Istream;
298 typedef typename Istream::int_type int_type;
299
300 // create a test streambuf object initialized with `str'
301 Streambuf sb (str, strsize, failure, fail_when);
302
303 Istream strm (&sb);
304
305 strm.exceptions (std::ios_base::iostate (exceptions));
306
307 const std::streamsize gcount = strm.gcount ();
308
309 typedef unsigned char UChar;
310
311 charT got = charT (UChar (initval));
312 const charT val = charT (UChar (expectval));
313
314 int caught = 0;
315
316 TRY_GET (strm.get (got));
317
318 check_failure (sb, cname, lineno, "get(char_type&)", strm,
319 int (gcount), !rdstate, rdstate, exceptions, caught);
320
321 // verify that the extracted value is the one expected
322
323 rw_assert (sb.throw_set_ || caught || Traits::eq (val, got), 0, lineno,
324 "line %d: basic_istream<%s>::get (char_type& = %{#lc}),"
325 " expected %{#lc}", __LINE__, cname, got, val);
326
327 rw_assert (rdstate || !(gcount || 1 != strm.gcount ()), 0, lineno,
328 "line %d: basic_istream<%s>::get (char_type&) "
329 "changed gcount () from %d to %d after a successful "
330 "extraction; 0 to 1 expected",
331 __LINE__, cname,
332 gcount, strm.gcount ());
333}
334
335
337template <class charT>
338void test_get_char (charT, const char *cname)
339{
340#undef T
341#undef TEST
342
343#define T charT (), cname, (UserTraits<charT>*)0, __LINE__
344#define TEST test_get_char
345
346 rw_info (0, 0, 0,
347 "27.6.1.3, p5 - std::basic_istream<%s>::get (char_type&)",
348 cname);
349
350 typename UserTraits<charT>::int_type &eof =
352
353 eof = ~0;
354
355 // +- initial stream buffer contents (will be widened)
356 // | +- the length of the buffer contents in chars
357 // | | +- initial value of get(char_type&) argument
358 // | | | +- expected value of argument on return
359 // | | | | +- expected stream state on return
360 // | | | | | +- stream exceptions
361 // | | | | | | +- force failure
362 // | | | | | | | +- when to fail
363 // | | | | | | | |
364 // v v v v v v v v
365 TEST (T, "", 0, '\0', '\0', Eof | Fail, Good, 0, -1);
366 TEST (T, "", 1, '\1', '\0', Good, Good, 0, -1);
367 TEST (T, "1", 1, '\2', '1', Good, Good, 0, -1);
368 TEST (T, "2 ", 2, '\3', '2', Good, Good, 0, -1);
369 TEST (T, " 3", 2, '\4', ' ', Good, Good, 0, -1);
370
371 TEST (T, "4", 1, '\5', '\5', Eof | Fail, Good, Underflow, 1);
372 TEST (T, "5", 1, '\6', '\6', Bad, Good, Underflow | Throw, 1);
373 TEST (T, "6", 1, '\7', '\6', Eof | Fail, Eof, Underflow, 1);
374 TEST (T, "7", 1, 'x', 'x', Eof | Fail, Fail, Underflow, 1);
375 TEST (T, "8", 1, 'y', 'y', Eof | Fail, Eof | Fail, Underflow, 1);
376 TEST (T, "9", 1, 'z', 'z', Eof | Fail, Eof | Fail | Bad, Underflow, 1);
377
378 TEST (T, "A", 1, 'a', 'a', Bad, Bad, Underflow | Throw, 1);
379}
380
381
383#define MAXCHARS (4096U * 4U)
384
385
386template <class charT, class Traits>
387void test_get_char_array (charT, const char* cname,
388 const Traits*,
389 int lineno,
390 const char *str,
391 std::size_t strsize,
392 int nread,
393 int idelim,
394 int nexpect,
395 int rdstate,
396 int exceptions,
397 int failure,
398 int fail_when)
399{
400 // basic_istream<charT, traits>&
401 // get(char_type* s, streamsize n, char_type delim);
402 //
403 // -7- Effects: Extracts characters and stores them into successive
404 // locations of an array whose first element is designated by s.
405 // 286) Characters are extracted and stored until any of the
406 // following occurs:
407 // - (n - 1) characters are stored;
408 // - end-of-file occurs on the input sequence (in which case
409 // the function calls setstate(eofbit));
410 // - (c == delim) for the next available input character c
411 // (in which case cis not extracted).
412 //
413 // -8- If the function stores no characters, it calls setstate(failbit)
414 // (which may throw ios_base::failure (27.4.4.3)). In any case, it
415 // then stores a null character into the next successive location
416 // of the array.
417 //
418 // -9- Returns: *this.
419
420 // basic_istream<charT,traits>&
421 // get(char_type* s, streamsize n);
422 //
423 // -10- Effects: Calls get(s, n, widen('\n')).
424 // -11- Returns: Value returned by the call.
425
427 typedef std::basic_istream<charT, Traits> Istream;
428 typedef typename Istream::int_type int_type;
429
430 const int_type delim = int_type (idelim);
431
432 // create a test streambuf object initialized with `str'
433 Streambuf sb (str, strsize, failure, fail_when);
434
435 Istream strm (&sb);
436
437 strm.exceptions (std::ios_base::iostate (exceptions));
438
439 const std::streamsize gcount = strm.gcount ();
440
441 typedef unsigned char UChar;
442
443 int caught = 0;
444
445 charT got [MAXCHARS + 1];
446 Traits::assign (got, sizeof got / sizeof *got, charT ('\xfe'));
447
448 const char *fun;
449
450 if (delim != Traits::eof ()) {
451 fun = "get(char_type*, streamsize, char_type)";
452 TRY_GET (strm.get (got, std::streamsize (nread), delim));
453 }
454 else {
455 fun = "get(char_type*, streamsize)";
456 TRY_GET (strm.get (got, std::streamsize (nread)));
457 }
458
459 check_failure (sb, cname, lineno, fun, strm, int (gcount), nexpect,
460 rdstate, exceptions, caught);
461
462 // verify that number and values of the extracted characters
463 // matches the expected number and values
464
465 rw_assert (strm.gcount () == nexpect, 0, lineno,
466 "line %d: basic_istream<%s>::get (char_type*, "
467 "streamsize = %d).gcount () == %d, got %d",
468 __LINE__, cname, nread, nexpect, strm.gcount ());
469
470 rw_assert (1 < nread || Traits::eq (got [0], charT ()), 0, lineno,
471 "line %d: basic_istream<%s>::get (char_type *s "
472 "= %{*Ac}, streamsize = %d); s[0] == '\\0', got %{#lc}",
473 __LINE__, cname, int (sizeof *got), got, nread, *got);
474
475 rw_assert (!Traits::compare (sb.buf_, got, std::size_t (nexpect)),
476 0, lineno,
477 "line %d: basic_istream<%s>::get (char_type* "
478 "= %{*Ac}, streamsize = %d) data mismatch",
479 __LINE__, cname, int (sizeof *got), got, nread);
480}
481
482
484template <class charT>
485void test_get_char_array (charT, const char *cname)
486{
487#undef T
488#undef TEST
489
490#define T charT (), cname, (UserTraits<charT>*)0, __LINE__
491#define TEST test_get_char_array
492
493 rw_info (0, 0, 0, "27.6.1.3, p5 - std::basic_istream<%s>"
494 "::get (char_type*, streamsize)", cname);
495
496 typename UserTraits<charT>::int_type &eof =
498
499 eof = ~0;
500
501 // +- initial stream buffer contents (will be widened)
502 // | +- the length of the buffer contents in chars
503 // | | +- number of characters (2nd argument to get())
504 // | | +- delimiter (3rd argument to get() or eof)
505 // | | | | +- expected number of extracted characters
506 // | | | | | +- expected stream state on return
507 // | | | | | | +- stream exceptions
508 // | | | | | | | +- force failure
509 // | | | | | | | | +- when to fail
510 // | | | | | | | | |
511 // v v v v v v v v v
512 TEST (T, "", 0, 0, eof, 0, Fail, Good, 0, -1);
513 TEST (T, "\0", 1, 1, eof, 0, Fail, Good, 0, -1);
514 TEST (T, "1", 1, 2, eof, 1, Good, Good, 0, -1);
515 TEST (T, "2", 1, 3, eof, 1, Eof, Good, 0, -1);
516 TEST (T, "23", 2, 3, eof, 2, Good, Good, 0, -1);
517 TEST (T, "34", 2, 4, eof, 2, Eof, Good, 0, -1);
518 TEST (T, "4\n", 2, 3, eof, 1, Good, Good, 0, -1);
519 TEST (T, "56\n", 3, 3, eof, 2, Good, Good, 0, -1);
520 TEST (T, "67\n", 3, 4, eof, 2, Good, Good, 0, -1);
521
522 // exercise the behavior on underflow() failure indicated
523 // by returning traits_type::eof()
524 TEST (T, "78\n9", 4, 4, eof, 0, Eof | Fail, Good, Underflow, 1);
525 TEST (T, "78901", 5, 5, eof, 0, Eof | Fail, Eof, Underflow, 1);
526 TEST (T, "78902", 5, 5, eof, 0, Eof | Fail, Fail, Underflow, 1);
527 TEST (T, "78803", 5, 5, eof, 0, Eof | Fail, Eof | Fail, Underflow, 1);
528 TEST (T, "89\na", 4, 4, eof, 1, Eof, Good, Underflow, 2);
529 TEST (T, "9a\nb", 4, 4, eof, 2, Eof, Good, Underflow, 3);
530 TEST (T, "ab\nc", 4, 4, eof, 2, Good, Good, Underflow, 4);
531
532 // exercise the behavior on underflow() failure caused
533 // by throwing an exception
534 TEST (T, "bc\nd", 4, 4, eof, 0, Bad, Good, Underflow | Throw, 1);
535 TEST (T, "cd\ne", 4, 4, eof, 1, Bad, Good, Underflow | Throw, 2);
536 TEST (T, "def\n", 4, 4, eof, 2, Bad, Good, Underflow | Throw, 3);
537 TEST (T, "efg\n", 4, 4, eof, 2, Bad, Bad, Underflow | Throw, 3);
538 TEST (T, "fgh\n", 4, 4, eof, 3, Good, Bad, Underflow | Throw, 4);
539 TEST (T, "ghij", 4, 4, eof, 3, Good, Bad, Underflow | Throw, 4);
540 TEST (T, "hijk", 4, 5, eof, 3, Bad, Bad, Underflow | Throw, 4);
541
542 const int N = int (MAXCHARS);
543 char *buf = new char [N];
544
545 for (int i = 0; i != N; ++i) {
546 buf [i] = char (i);
547 }
548
549 // set the maximum number of characters in the pending input
550 // sequence to be greater than the default 1 to allow get()
551 // not to have to call underflow() on every iteration and
552 // to exercise any/all branches in the function
553 MyStreambuf<charT, UserTraits<charT> >::in_pending_ = N / 33;
554
555 TEST (T, "12345", 5, 4, eof, 3, Good, Good, 0, -1);
556 TEST (T, "12346", 5, 5, eof, 4, Good, Good, 0, -1);
557 TEST (T, "12347", 5, 6, eof, 5, Good, Good, 0, -1);
558 TEST (T, "12348", 5, 7, eof, 5, Eof, Good, 0, -1);
559
560 for (int i = 0; i < N; i += 256) {
561 TEST (T, buf, N, N, eof, '\n' + i, Good, Good, 0, -1);
562
563 buf ['\n' + i] = '\0';
564 }
565
566 rw_info (0, 0, 0, "27.6.1.3, p5 - std::basic_istream<%s>"
567 "::get (char_type*, streamsize, char_type)", cname);
568
569 TEST (T, "", 0, 0, '\0', 0, Fail, Good, 0, -1);
570 TEST (T, "", 1, 1, '\0', 0, Fail, Good, 0, -1);
571 TEST (T, "a", 1, 2, '\0', 1, Good, Good, 0, -1);
572 TEST (T, "bc", 2, 2, 'c', 1, Good, Good, 0, -1);
573 TEST (T, "cd", 2, 3, 'd', 1, Good, Good, 0, -1);
574 TEST (T, "de", 2, 3, 'd', 0, Fail, Good, 0, -1);
575}
576
577
579template <class charT, class Traits>
580void test_get_streambuf (charT, const char* cname,
581 const Traits*,
582 int lineno,
583 const char *str,
584 std::size_t strsize,
585 int idelim,
586 int nexpect,
587 int rdstate,
588 int exceptions,
589 int failure,
590 int fail_when)
591{
593 typedef std::basic_istream<charT, Traits> Istream;
594 typedef typename Istream::int_type int_type;
595
596 const int_type delim = int_type (idelim);
597
598 int in_failure, in_fail_when;
599 int out_failure, out_fail_when;
600
601 switch (failure & ~Throw) {
602 case Xsgetn: case Underflow: case Uflow:
603 in_failure = failure;
604 in_fail_when = fail_when;
605 out_failure = 0;
606 out_fail_when = 0;
607 break;
608
609 case Overflow: case Xsputn:
610 in_failure = 0;
611 in_fail_when = 0;
612 out_failure = failure;
613 out_fail_when = fail_when;
614 break;
615
616 default:
617 in_failure = failure;
618 in_fail_when = fail_when;
619 out_failure = failure;
620 out_fail_when = fail_when;
621 }
622
623 // create a test streambuf object initialized with `str'
624 Streambuf inbuf (str, strsize, in_failure, in_fail_when);
625
626 Istream strm (&inbuf);
627
628 strm.exceptions (std::ios_base::iostate (exceptions));
629
630 const std::streamsize gcount = strm.gcount ();
631
632 typedef unsigned char UChar;
633
634 int caught = 0;
635
636 Streambuf outbuf (strsize, out_failure, out_fail_when);
637
638 const char *fun;
639
640 if (delim != Traits::eof ()) {
641 fun = "get(basic_streambuf<char_type, traits_type>&, char_type)";
642 TRY_GET (strm.get (outbuf, delim));
643 }
644 else {
645 fun = "get(basic_streambuf<char_type, traits_type>&)";
646 TRY_GET (strm.get (outbuf));
647 }
648
649 check_failure (inbuf, cname, lineno, fun, strm, int (gcount),
650 nexpect, rdstate, exceptions, caught);
651
652 // verify that number and values of the extracted characters
653 // matches the expected number and values
654
655 rw_assert (strm.gcount () == nexpect, 0, lineno,
656 "line %d: basic_istream<%s>::get (basic_streambuf"
657 "<char_type, traits_type>*).gcount () == %d, got %d",
658 __LINE__, cname, nexpect, strm.gcount ());
659
660 rw_assert (!Traits::compare (inbuf.buf_, outbuf.buf_,
661 std::size_t (nexpect)), 0, lineno,
662 "line %d: basic_istream<%s>::get (basic_streambuf"
663 "<char_type, traits_type>*) data mismatch",
664 __LINE__, cname);
665}
666
667
669template <class charT>
670void test_get_streambuf (charT, const char *cname)
671{
672#undef T
673#undef TEST
674
675#define T charT (), cname, (UserTraits<charT>*)0, __LINE__
676#define TEST test_get_streambuf
677
678 rw_info (0, 0, 0, "27.6.1.3, p12 - std::basic_istream<%s>"
679 "::get (basic_streambuf<char_type, traits_type>*, char_type)",
680 cname);
681
682 typename UserTraits<charT>::int_type &eof =
684
685 eof = ~0;
686
687 // set the maximum number of characters in the pending input
688 // sequence to 1 to force a call to underflow() for each
689 // character
690 MyStreambuf<charT, UserTraits<charT> >::in_pending_ = 1;
691
692 // basic_istream<charT, traits>&
693 // get (basic_streambuf<charT, traits> &sb, char_type delim);
694 //
695 // -12- Effects: Extracts characters and inserts them in the output
696 // sequence controlled by sb. Characters are extracted and
697 // inserted until any of the following occurs:
698 // - end-of-file occurs on the input sequence;
699 // - inserting in the output sequence fails (in which case the
700 // character to be inserted is not extracted);
701 // - (c == delim) for the next available input character c
702 // (in which case cis not extracted);
703 // - an exception occurs (in which case, the exception is caught
704 // but not rethrown).
705 // -13- If the function inserts no characters, it calls
706 // setstate(failbit), which may throw ios_base::failure (27.4.4.3).
707 // -14- Returns: *this.
708
709 TEST (T, "", 0, 'x', 0, Eof | Fail, Good, 0, 0);
710 TEST (T, "\0", 1, 'y', 1, Eof, Good, 0, 0);
711 TEST (T, "ab", 2, 'z', 2, Eof, Good, 0, 0);
712 TEST (T, "abc", 3, '\0', 3, Eof, Good, 0, 0);
713 TEST (T, "b\0c", 3, '\0', 1, Good, Good, 0, 0);
714 TEST (T, "cd\0e", 4, '\0', 2, Good, Good, 0, 0);
715 TEST (T, "def\0", 4, '\0', 3, Good, Good, 0, 0);
716
717 TEST (T, "efgh", 4, 'f', 0, Eof | Fail, Good, Underflow, 1);
718 TEST (T, "fghi", 4, 'g', 0, Eof | Fail, Eof, Underflow, 1);
719 TEST (T, "ghij", 4, 'h', 0, Eof | Fail, Fail, Underflow, 1);
720 TEST (T, "hijk", 4, 'i', 0, Eof | Fail, Eof | Fail, Underflow, 1);
721
722 TEST (T, "ijkl", 4, 'k', 1, Eof, Good, Underflow, 2);
723 TEST (T, "jklm", 4, 'm', 2, Eof, Good, Underflow, 3);
724
725 TEST (T, "klmn", 4, 'n', 2, Bad, Good, Underflow | Throw, 3);
726 TEST (T, "lmno", 4, 'o', 2, Bad, Bad, Underflow | Throw, 3);
727
728 TEST (T, "EFGH", 4, 'F', 0, Fail, Good, Overflow, 1);
729 TEST (T, "FGHI", 4, 'G', 0, Fail, Eof, Overflow, 1);
730 TEST (T, "GHIJ", 4, 'H', 0, Fail, Fail, Overflow, 1);
731 TEST (T, "HIJK", 4, 'I', 0, Fail, Eof | Fail, Overflow, 1);
732
733 TEST (T, "IJKL", 4, 'K', 1, Fail, Good, Overflow, 2);
734 TEST (T, "JKLM", 4, 'M', 2, Fail, Good, Overflow, 3);
735
736 TEST (T, "KLMN", 4, 'N', 2, Fail, Good, Overflow | Throw, 3);
737 TEST (T, "LMNO", 4, 'O', 2, Fail, Bad, Overflow | Throw, 3);
738
739 rw_info (0, 0, 0, "27.6.1.3, p15 - std::basic_istream<%s>"
740 "::get (basic_streambuf<char_type, traits_type>&)", cname);
741
742 TEST (T, "", 0, eof, 0, Eof | Fail, Good, 0, 0);
743 TEST (T, "\n", 1, eof, 0, Fail, Good, 0, 0);
744 TEST (T, "a\n", 2, eof, 1, Good, Good, 0, 0);
745 TEST (T, "ab\n", 3, eof, 2, Good, Good, 0, 0);
746}
747
748
750static int rw_opt_no_get_void;
751static int rw_opt_no_get_char;
752static int rw_opt_no_get_char_array;
753static int rw_opt_no_get_streambuf;
754
755
756static int
757run_test (int, char**)
758{
759#undef TEST
760#define TEST(T, arg) \
761 if (rw_opt_no_get_ ## arg) \
762 rw_note (0, 0, 0, \
763 "basic_istream::get(%s) test disabled", #arg); \
764 else \
765 test_get_ ## arg (T (), #T)
766
767
768 TEST (char, void);
769 TEST (char, char);
770 TEST (char, char_array);
771 TEST (char, streambuf);
772
773#ifndef _RWSTD_NO_WCHAR_T
774
775 TEST (wchar_t, void);
776 TEST (wchar_t, char);
777 TEST (wchar_t, char_array);
778 TEST (wchar_t, streambuf);
779
780#endif // _RWSTD_NO_WCHAR_T
781
782 return 0;
783}
784
785
787int main (int argc, char *argv[])
788{
789 return rw_test (argc, argv, __FILE__,
790 "lib.istream.unformatted",
791 "overloads of get() member function",
792 run_test,
793 "|-no-get-void#"
794 "|-no-get-char#"
795 "|-no-get-char_array#"
796 "|-no-get-streambuf#",
797 &rw_opt_no_get_void,
798 &rw_opt_no_get_char,
799 &rw_opt_no_get_char_array,
800 &rw_opt_no_get_streambuf,
801 0);
802}
int ival
#define N
Definition 17.names.cpp:157
#define i
Definition 17.names.cpp:126
#define fun
Definition 17.names.cpp:471
#define sb
Definition 17.names.cpp:542
#define lineno
Definition 17.names.cpp:491
#define buf
Definition 17.names.cpp:452
#define charT
Definition 17.names.cpp:456
#define strm
Definition 17.names.cpp:549
#define val
Definition 17.names.cpp:570
void run_test(const ForwardIterator &dummy, T *, Predicate *pred)
std::char_traits< S > Traits
#define TRY_GET(expr)
#define T
void check_failure(const MyStreambuf< charT, Traits > &sb, const char *cname, int lineno, const char *fun, std::basic_istream< charT, Traits > &strm, int gcount, int nexpect, int rdstate, int exceptions, int caught)
void test_get_char(charT, const char *cname, const Traits *, int lineno, const char *str, std::size_t strsize, char initval, char expectval, int rdstate, int exceptions, int failure, int fail_when)
#define TEST
void test_get_streambuf(charT, const char *cname, const Traits *, int lineno, const char *str, std::size_t strsize, int idelim, int nexpect, int rdstate, int exceptions, int failure, int fail_when)
void test_get_char_array(charT, const char *cname, const Traits *, int lineno, const char *str, std::size_t strsize, int nread, int idelim, int nexpect, int rdstate, int exceptions, int failure, int fail_when)
void test_get_void(charT, const char *cname, const Traits *, int lineno, const char *str, std::size_t strsize, int ival, int rdstate, int exceptions, int failure, int fail_when)
#define main
int caught
const char * str
Definition exec.cpp:170
_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
@ Uflow
@ Overflow
@ Xsgetn
@ Throw
@ Underflow
@ Xsputn
unsigned char UChar
Definition collate.cpp:122