int.pass.cpp (1927B)
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>>(int& val); 16 17 #include <istream> 18 #include <limits> 19 #include <cassert> 20 21 template <class CharT> 22 struct testbuf 23 : public std::basic_streambuf<CharT> 24 { 25 typedef std::basic_string<CharT> string_type; 26 typedef std::basic_streambuf<CharT> base; 27 private: 28 string_type str_; 29 public: 30 31 testbuf() {} 32 testbuf(const string_type& str) 33 : str_(str) 34 { 35 base::setg(const_cast<CharT*>(str_.data()), 36 const_cast<CharT*>(str_.data()), 37 const_cast<CharT*>(str_.data()) + str_.size()); 38 } 39 40 CharT* eback() const {return base::eback();} 41 CharT* gptr() const {return base::gptr();} 42 CharT* egptr() const {return base::egptr();} 43 }; 44 45 int main() 46 { 47 { 48 std::istream is((std::streambuf*)0); 49 int n = 0; 50 is >> n; 51 assert(is.fail()); 52 } 53 { 54 testbuf<char> sb("0"); 55 std::istream is(&sb); 56 int n = 10; 57 is >> n; 58 assert(n == 0); 59 assert( is.eof()); 60 assert(!is.fail()); 61 } 62 { 63 testbuf<char> sb(" 123 "); 64 std::istream is(&sb); 65 int n = 10; 66 is >> n; 67 assert(n == 123); 68 assert(!is.eof()); 69 assert(!is.fail()); 70 } 71 { 72 testbuf<wchar_t> sb(L" -1234567890123456 "); 73 std::wistream is(&sb); 74 int n = 10; 75 is >> n; 76 assert(n == std::numeric_limits<int>::min()); 77 assert(!is.eof()); 78 assert( is.fail()); 79 } 80 }