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