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>& get(char_type* s, streamsize n);
     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("  \n    \n ");
     55         std::istream is(&sb);
     56         char s[5];
     57         is.get(s, 5);
     58         assert(!is.eof());
     59         assert(!is.fail());
     60         assert(std::string(s) == "  ");
     61         assert(is.gcount() == 2);
     62         is.get(s, 5);
     63         assert(!is.eof());
     64         assert( is.fail());
     65         assert(std::string(s) == "");
     66         assert(is.gcount() == 0);
     67         is.clear();
     68         assert(is.get() == '\n');
     69         is.get(s, 5);
     70         assert(!is.eof());
     71         assert(!is.fail());
     72         assert(std::string(s) == "    ");
     73         assert(is.gcount() == 4);
     74         assert(is.get() == '\n');
     75         is.get(s, 5);
     76         assert( is.eof());
     77         assert(!is.fail());
     78         assert(std::string(s) == " ");
     79         assert(is.gcount() == 1);
     80         // Check that even in error case the buffer is properly 0-terminated.
     81         is.get(s, 5);
     82         assert( is.eof());
     83         assert( is.fail());
     84         assert(std::string(s) == "");
     85         assert(is.gcount() == 0);
     86     }
     87 #ifndef TEST_HAS_NO_EXCEPTIONS
     88     {
     89         testbuf<char> sb(" ");
     90         std::istream is(&sb);
     91         char s[5] = "test";
     92         is.exceptions(std::istream::eofbit | std::istream::badbit);
     93         try
     94         {
     95             is.get(s, 5);
     96             assert(false);
     97         }
     98         catch (std::ios_base::failure&)
     99         {
    100         }
    101         assert( is.eof());
    102         assert( is.fail());
    103         assert(std::string(s) == " ");
    104         assert(is.gcount() == 1);
    105     }
    106 #endif
    107     {
    108         testbuf<wchar_t> sb(L"  \n    \n ");
    109         std::wistream is(&sb);
    110         wchar_t s[5];
    111         is.get(s, 5);
    112         assert(!is.eof());
    113         assert(!is.fail());
    114         assert(std::wstring(s) == L"  ");
    115         assert(is.gcount() == 2);
    116         is.get(s, 5);
    117         assert(!is.eof());
    118         assert( is.fail());
    119         assert(std::wstring(s) == L"");
    120         assert(is.gcount() == 0);
    121         is.clear();
    122         assert(is.get() == L'\n');
    123         is.get(s, 5);
    124         assert(!is.eof());
    125         assert(!is.fail());
    126         assert(std::wstring(s) == L"    ");
    127         assert(is.gcount() == 4);
    128         assert(is.get() == L'\n');
    129         is.get(s, 5);
    130         assert( is.eof());
    131         assert(!is.fail());
    132         assert(std::wstring(s) == L" ");
    133         assert(is.gcount() == 1);
    134         // Check that even in error case the buffer is properly 0-terminated.
    135         is.get(s, 5);
    136         assert( is.eof());
    137         assert( is.fail());
    138         assert(std::wstring(s) == L"");
    139         assert(is.gcount() == 0);
    140     }
    141 #ifndef TEST_HAS_NO_EXCEPTIONS
    142     {
    143         testbuf<wchar_t> sb(L" ");
    144         std::wistream is(&sb);
    145         wchar_t s[5] = L"test";
    146         is.exceptions(std::wistream::eofbit | std::wistream::badbit);
    147         try
    148         {
    149             is.get(s, 5);
    150             assert(false);
    151         }
    152         catch (std::ios_base::failure&)
    153         {
    154         }
    155         assert( is.eof());
    156         assert( is.fail());
    157         assert(std::wstring(s) == L" ");
    158         assert(is.gcount() == 1);
    159     }
    160 #endif
    161 }
    162