unsigned_char_pointer.pass.cpp revision 72215c2dbf0bbff4ad9f73879d346be95facdd83
1//===----------------------------------------------------------------------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. 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 18template <class CharT> 19struct testbuf 20 : public std::basic_streambuf<CharT> 21{ 22 typedef std::basic_string<CharT> string_type; 23 typedef std::basic_streambuf<CharT> base; 24private: 25 string_type str_; 26public: 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 42int 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