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 traits> 13 // basic_istream<char,traits>& operator>>(basic_istream<char,traits>&& in, unsigned char* s); 14 15 #include <istream> 16 #include <cassert> 17 18 template <class CharT> 19 struct testbuf 20 : public std::basic_streambuf<CharT> 21 { 22 typedef std::basic_string<CharT> string_type; 23 typedef std::basic_streambuf<CharT> base; 24 private: 25 string_type str_; 26 public: 27 28 testbuf() {} 29 testbuf(const string_type& str) 30 : str_(str) 31 { 32 base::setg(const_cast<CharT*>(str_.data()), 33 const_cast<CharT*>(str_.data()), 34 const_cast<CharT*>(str_.data()) + str_.size()); 35 } 36 37 CharT* eback() const {return base::eback();} 38 CharT* gptr() const {return base::gptr();} 39 CharT* egptr() const {return base::egptr();} 40 }; 41 42 int main() 43 { 44 { 45 testbuf<char> sb(" abcdefghijk "); 46 std::istream is(&sb); 47 unsigned char s[20]; 48 is >> s; 49 assert(!is.eof()); 50 assert(!is.fail()); 51 assert(std::string((char*)s) == "abcdefghijk"); 52 } 53 { 54 testbuf<char> sb(" abcdefghijk "); 55 std::istream is(&sb); 56 is.width(4); 57 unsigned char s[20]; 58 is >> s; 59 assert(!is.eof()); 60 assert(!is.fail()); 61 assert(std::string((char*)s) == "abc"); 62 assert(is.width() == 0); 63 } 64 { 65 testbuf<char> sb(" abcdefghijk"); 66 std::istream is(&sb); 67 unsigned char s[20]; 68 is >> s; 69 assert( is.eof()); 70 assert(!is.fail()); 71 assert(std::string((char*)s) == "abcdefghijk"); 72 assert(is.width() == 0); 73 } 74 { 75 testbuf<char> sb(" abcdefghijk"); 76 std::istream is(&sb); 77 unsigned char s[20]; 78 is.width(1); 79 is >> s; 80 assert(!is.eof()); 81 assert( is.fail()); 82 assert(std::string((char*)s) == ""); 83 assert(is.width() == 0); 84 } 85 } 86