]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - test/std/input.output/iostream.format/input.streams/istream.formatted/istream.formatted.arithmetic/unsigned_long.pass.cpp
Vendor import of libc++ trunk r351319 (just before the release_80
[FreeBSD/FreeBSD.git] / test / std / input.output / iostream.format / input.streams / istream.formatted / istream.formatted.arithmetic / unsigned_long.pass.cpp
1 //===----------------------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 // <istream>
11
12 // template <class charT, class traits = char_traits<charT> >
13 //   class basic_istream;
14
15 // operator>>(unsigned long& val);
16
17 #include <istream>
18 #include <cassert>
19
20 template <class CharT>
21 struct testbuf
22     : public std::basic_streambuf<CharT>
23 {
24     typedef std::basic_string<CharT> string_type;
25     typedef std::basic_streambuf<CharT> base;
26 private:
27     string_type str_;
28 public:
29
30     testbuf() {}
31     testbuf(const string_type& str)
32         : str_(str)
33     {
34         base::setg(const_cast<CharT*>(str_.data()),
35                    const_cast<CharT*>(str_.data()),
36                    const_cast<CharT*>(str_.data()) + str_.size());
37     }
38
39     CharT* eback() const {return base::eback();}
40     CharT* gptr() const {return base::gptr();}
41     CharT* egptr() const {return base::egptr();}
42 };
43
44 int main()
45 {
46     {
47         std::istream is((std::streambuf*)0);
48         unsigned long n = 0;
49         is >> n;
50         assert(is.fail());
51     }
52     {
53         testbuf<char> sb("0");
54         std::istream is(&sb);
55         unsigned long n = 10;
56         is >> n;
57         assert(n == 0);
58         assert( is.eof());
59         assert(!is.fail());
60     }
61     {
62         testbuf<char> sb(" 123 ");
63         std::istream is(&sb);
64         unsigned long n = 10;
65         is >> n;
66         assert(n == 123);
67         assert(!is.eof());
68         assert(!is.fail());
69     }
70     {
71         testbuf<wchar_t> sb(L" 123 ");
72         std::wistream is(&sb);
73         unsigned long n = 10;
74         is >> n;
75         assert(n == 123);
76         assert(!is.eof());
77         assert(!is.fail());
78     }
79 }