126 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// template <class charT, class traits>12// basic_istream<charT,traits>&13// ws(basic_istream<charT,traits>& is);14 15#include <istream>16#include <cassert>17#include <streambuf>18 19#include "test_macros.h"20 21template <class CharT>22struct testbuf23 : public std::basic_streambuf<CharT>24{25 typedef std::basic_string<CharT> string_type;26 typedef std::basic_streambuf<CharT> base;27private:28 string_type str_;29public:30 31 testbuf() {}32 testbuf(const string_type& str)33 : str_(str)34 {35 base::setg(const_cast<CharT*>(str_.data()),36 const_cast<CharT*>(str_.data()),37 const_cast<CharT*>(str_.data()) + str_.size());38 }39 40 CharT* eback() const {return base::eback();}41 CharT* gptr() const {return base::gptr();}42 CharT* egptr() const {return base::egptr();}43};44 45int main(int, char**)46{47 {48 testbuf<char> sb(" 123");49 std::istream is(&sb);50 std::ws(is);51 assert(is.good());52 assert(is.peek() == '1');53 }54#ifndef TEST_HAS_NO_WIDE_CHARACTERS55 {56 testbuf<wchar_t> sb(L" 123");57 std::wistream is(&sb);58 std::ws(is);59 assert(is.good());60 assert(is.peek() == L'1');61 }62#endif63 {64 testbuf<char> sb(" ");65 std::istream is(&sb);66 std::ws(is);67 assert(!is.fail());68 assert(is.eof());69 std::ws(is);70 assert(is.eof());71 assert(is.fail());72 }73#ifndef TEST_HAS_NO_WIDE_CHARACTERS74 {75 testbuf<wchar_t> sb(L" ");76 std::wistream is(&sb);77 std::ws(is);78 assert(!is.fail());79 assert(is.eof());80 std::ws(is);81 assert(is.eof());82 assert(is.fail());83 }84#endif85#ifndef TEST_HAS_NO_EXCEPTIONS86 {87 testbuf<char> sb(" ");88 std::basic_istream<char> is(&sb);89 is.exceptions(std::ios_base::eofbit);90 91 bool threw = false;92 try {93 std::ws(is);94 } catch (std::ios_base::failure const&) {95 threw = true;96 }97 98 assert(!is.bad());99 assert(!is.fail());100 assert( is.eof());101 assert(threw);102 }103#ifndef TEST_HAS_NO_WIDE_CHARACTERS104 {105 testbuf<wchar_t> sb(L" ");106 std::basic_istream<wchar_t> is(&sb);107 is.exceptions(std::ios_base::eofbit);108 109 bool threw = false;110 try {111 std::ws(is);112 } catch (std::ios_base::failure const&) {113 threw = true;114 }115 116 assert(!is.bad());117 assert(!is.fail());118 assert( is.eof());119 assert(threw);120 }121#endif // TEST_HAS_NO_WIDE_CHARACTERS122#endif // TEST_HAS_NO_EXCEPTIONS123 124 return 0;125}126