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