153 lines · cpp
1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9// <istream>10 11// int_type get();12 13#include <istream>14#include <cassert>15#include <streambuf>16 17#include "test_macros.h"18 19template <class CharT>20struct testbuf21 : public std::basic_streambuf<CharT>22{23 typedef std::basic_string<CharT> string_type;24 typedef std::basic_streambuf<CharT> base;25private:26 string_type str_;27public:28 29 testbuf() {}30 testbuf(const string_type& str)31 : str_(str)32 {33 base::setg(const_cast<CharT*>(str_.data()),34 const_cast<CharT*>(str_.data()),35 const_cast<CharT*>(str_.data()) + str_.size());36 }37 38 CharT* eback() const {return base::eback();}39 CharT* gptr() const {return base::gptr();}40 CharT* egptr() const {return base::egptr();}41};42 43int main(int, char**)44{45 {46 testbuf<char> sb(" ");47 std::istream is(&sb);48 char c = static_cast<char>(is.get());49 assert(!is.eof());50 assert(!is.fail());51 assert(c == ' ');52 assert(is.gcount() == 1);53 }54 {55 testbuf<char> sb(" abc");56 std::istream is(&sb);57 char c = static_cast<char>(is.get());58 assert(!is.eof());59 assert(!is.fail());60 assert(c == ' ');61 assert(is.gcount() == 1);62 c = static_cast<char>(is.get());63 assert(!is.eof());64 assert(!is.fail());65 assert(c == 'a');66 assert(is.gcount() == 1);67 c = static_cast<char>(is.get());68 assert(!is.eof());69 assert(!is.fail());70 assert(c == 'b');71 assert(is.gcount() == 1);72 c = static_cast<char>(is.get());73 assert(!is.eof());74 assert(!is.fail());75 assert(c == 'c');76 assert(is.gcount() == 1);77 }78#ifndef TEST_HAS_NO_WIDE_CHARACTERS79 {80 testbuf<wchar_t> sb(L" abc");81 std::wistream is(&sb);82 wchar_t c = is.get();83 assert(!is.eof());84 assert(!is.fail());85 assert(c == L' ');86 assert(is.gcount() == 1);87 c = is.get();88 assert(!is.eof());89 assert(!is.fail());90 assert(c == L'a');91 assert(is.gcount() == 1);92 c = is.get();93 assert(!is.eof());94 assert(!is.fail());95 assert(c == L'b');96 assert(is.gcount() == 1);97 c = is.get();98 assert(!is.eof());99 assert(!is.fail());100 assert(c == L'c');101 assert(is.gcount() == 1);102 }103#endif104#ifndef TEST_HAS_NO_EXCEPTIONS105 {106 testbuf<char> sb("rrrrrrrrr");107 std::basic_istream<char> is(&sb);108 is.exceptions(std::ios_base::eofbit);109 110 bool threw = false;111 try {112 while (true) {113 is.get();114 if (is.eof())115 break;116 }117 } catch (std::ios_base::failure const&) {118 threw = true;119 }120 121 assert(!is.bad());122 assert( is.fail());123 assert( is.eof());124 assert(threw);125 }126#ifndef TEST_HAS_NO_WIDE_CHARACTERS127 {128 testbuf<wchar_t> sb(L"rrrrrrrrr");129 std::basic_istream<wchar_t> is(&sb);130 is.exceptions(std::ios_base::eofbit);131 132 bool threw = false;133 try {134 while (true) {135 is.get();136 if (is.eof())137 break;138 }139 } catch (std::ios_base::failure const&) {140 threw = true;141 }142 143 assert(!is.bad());144 assert( is.fail());145 assert( is.eof());146 assert(threw);147 }148#endif149#endif // TEST_HAS_NO_EXCEPTIONS150 151 return 0;152}153