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-exception
6 //
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 "test_macros.h"
18 
19 template <class CharT>
20 struct testbuf
21     : public std::basic_streambuf<CharT>
22 {
23     typedef std::basic_string<CharT> string_type;
24     typedef std::basic_streambuf<CharT> base;
25 private:
26     string_type str_;
27 public:
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 
43 int main(int, char**)
44 {
45     {
46         testbuf<char> sb("   123");
47         std::istream is(&sb);
48         ws(is);
49         assert(is.good());
50         assert(is.peek() == '1');
51     }
52 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
53     {
54         testbuf<wchar_t> sb(L"   123");
55         std::wistream is(&sb);
56         ws(is);
57         assert(is.good());
58         assert(is.peek() == L'1');
59     }
60 #endif
61     {
62         testbuf<char> sb("  ");
63         std::istream is(&sb);
64         ws(is);
65         assert(!is.fail());
66         assert(is.eof());
67         ws(is);
68         assert(is.eof());
69         assert(is.fail());
70     }
71 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
72     {
73         testbuf<wchar_t> sb(L"  ");
74         std::wistream is(&sb);
75         ws(is);
76         assert(!is.fail());
77         assert(is.eof());
78         ws(is);
79         assert(is.eof());
80         assert(is.fail());
81     }
82 #endif
83 #ifndef TEST_HAS_NO_EXCEPTIONS
84     {
85         testbuf<char> sb("  ");
86         std::basic_istream<char> is(&sb);
87         is.exceptions(std::ios_base::eofbit);
88 
89         bool threw = false;
90         try {
91             std::ws(is);
92         } catch (std::ios_base::failure const&) {
93             threw = true;
94         }
95 
96         assert(!is.bad());
97         assert(!is.fail());
98         assert( is.eof());
99         assert(threw);
100     }
101 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
102     {
103         testbuf<wchar_t> sb(L"  ");
104         std::basic_istream<wchar_t> is(&sb);
105         is.exceptions(std::ios_base::eofbit);
106 
107         bool threw = false;
108         try {
109             std::ws(is);
110         } catch (std::ios_base::failure const&) {
111             threw = true;
112         }
113 
114         assert(!is.bad());
115         assert(!is.fail());
116         assert( is.eof());
117         assert(threw);
118     }
119 #endif // TEST_HAS_NO_WIDE_CHARACTERS
120 #endif // TEST_HAS_NO_EXCEPTIONS
121 
122     return 0;
123 }
124