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>>(short& 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 short n = 0; 49 is >> n; 50 assert(is.fail()); 51 } 52 { 53 testbuf<char> sb("0"); 54 std::istream is(&sb); 55 short 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 short 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" -1234567890 "); 72 std::wistream is(&sb); 73 short n = 10; 74 is >> n; 75 assert(n == std::numeric_limits<short>::min()); 76 assert(!is.eof()); 77 assert( is.fail()); 78 } 79 } 80