Home | History | Annotate | Download | only in istream.unformatted
      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 // XFAIL: with_system_cxx_lib=macosx10.13
     11 // XFAIL: with_system_cxx_lib=macosx10.12
     12 // XFAIL: with_system_cxx_lib=macosx10.11
     13 // XFAIL: with_system_cxx_lib=macosx10.10
     14 // XFAIL: with_system_cxx_lib=macosx10.9
     15 // XFAIL: with_system_cxx_lib=macosx10.8
     16 // XFAIL: with_system_cxx_lib=macosx10.7
     17 
     18 // <istream>
     19 
     20 // basic_istream<charT,traits>& getline(char_type* s, streamsize n, char_type delim);
     21 
     22 #include <istream>
     23 #include <cassert>
     24 
     25 #include "test_macros.h"
     26 
     27 template <class CharT>
     28 struct testbuf
     29     : public std::basic_streambuf<CharT>
     30 {
     31     typedef std::basic_string<CharT> string_type;
     32     typedef std::basic_streambuf<CharT> base;
     33 private:
     34     string_type str_;
     35 public:
     36 
     37     testbuf() {}
     38     testbuf(const string_type& str)
     39         : str_(str)
     40     {
     41         base::setg(const_cast<CharT*>(str_.data()),
     42                    const_cast<CharT*>(str_.data()),
     43                    const_cast<CharT*>(str_.data()) + str_.size());
     44     }
     45 
     46     CharT* eback() const {return base::eback();}
     47     CharT* gptr() const {return base::gptr();}
     48     CharT* egptr() const {return base::egptr();}
     49 };
     50 
     51 int main()
     52 {
     53     {
     54         testbuf<char> sb("  *    * ");
     55         std::istream is(&sb);
     56         char s[5];
     57         is.getline(s, 5, '*');
     58         assert(!is.eof());
     59         assert(!is.fail());
     60         assert(std::string(s) == "  ");
     61         assert(is.gcount() == 3);
     62         is.getline(s, 5, '*');
     63         assert(!is.eof());
     64         assert(!is.fail());
     65         assert(std::string(s) == "    ");
     66         assert(is.gcount() == 5);
     67         is.getline(s, 5, '*');
     68         assert( is.eof());
     69         assert(!is.fail());
     70         assert(std::string(s) == " ");
     71         assert(is.gcount() == 1);
     72         // Check that even in error case the buffer is properly 0-terminated.
     73         is.getline(s, 5, '*');
     74         assert( is.eof());
     75         assert( is.fail());
     76         assert(std::string(s) == "");
     77         assert(is.gcount() == 0);
     78     }
     79 #ifndef TEST_HAS_NO_EXCEPTIONS
     80     {
     81         testbuf<char> sb(" ");
     82         std::istream is(&sb);
     83         char s[5] = "test";
     84         is.exceptions(std::istream::eofbit | std::istream::badbit);
     85         try
     86         {
     87             is.getline(s, 5, '*');
     88             assert(false);
     89         }
     90         catch (std::ios_base::failure&)
     91         {
     92         }
     93         assert( is.eof());
     94         assert( is.fail());
     95         assert(std::string(s) == " ");
     96         assert(is.gcount() == 1);
     97     }
     98 #endif
     99     {
    100         testbuf<wchar_t> sb(L"  *    * ");
    101         std::wistream is(&sb);
    102         wchar_t s[5];
    103         is.getline(s, 5, L'*');
    104         assert(!is.eof());
    105         assert(!is.fail());
    106         assert(std::wstring(s) == L"  ");
    107         assert(is.gcount() == 3);
    108         is.getline(s, 5, L'*');
    109         assert(!is.eof());
    110         assert(!is.fail());
    111         assert(std::wstring(s) == L"    ");
    112         assert(is.gcount() == 5);
    113         is.getline(s, 5, L'*');
    114         assert( is.eof());
    115         assert(!is.fail());
    116         assert(std::wstring(s) == L" ");
    117         assert(is.gcount() == 1);
    118         // Check that even in error case the buffer is properly 0-terminated.
    119         is.getline(s, 5, L'*');
    120         assert( is.eof());
    121         assert( is.fail());
    122         assert(std::wstring(s) == L"");
    123         assert(is.gcount() == 0);
    124     }
    125 #ifndef TEST_HAS_NO_EXCEPTIONS
    126     {
    127         testbuf<wchar_t> sb(L" ");
    128         std::wistream is(&sb);
    129         wchar_t s[5] = L"test";
    130         is.exceptions(std::wistream::eofbit | std::wistream::badbit);
    131         try
    132         {
    133             is.getline(s, 5, L'*');
    134             assert(false);
    135         }
    136         catch (std::ios_base::failure&)
    137         {
    138         }
    139         assert( is.eof());
    140         assert( is.fail());
    141         assert(std::wstring(s) == L" ");
    142         assert(is.gcount() == 1);
    143     }
    144 #endif
    145 }
    146