[gn build] Port f3c4b58f4b0d
[llvm-project.git] / libcxx / test / std / input.output / file.streams / fstreams / filebuf.virtuals / overflow.pass.cpp
blob90161432a2d34c4a88220ca2e94624217ad64039
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 //===----------------------------------------------------------------------===//
9 // REQUIRES: locale.en_US.UTF-8
11 // <fstream>
13 // int_type overflow(int_type c = traits::eof());
15 // This test is not entirely portable
17 #include <fstream>
18 #include <cassert>
20 #include "test_macros.h"
21 #include "platform_support.h" // locale name macros
23 template <class CharT>
24 struct test_buf
25 : public std::basic_filebuf<CharT>
27 typedef std::basic_filebuf<CharT> base;
28 typedef typename base::char_type char_type;
29 typedef typename base::int_type int_type;
30 typedef typename base::traits_type traits_type;
32 char_type* pbase() const {return base::pbase();}
33 char_type* pptr() const {return base::pptr();}
34 char_type* epptr() const {return base::epptr();}
35 void gbump(int n) {base::gbump(n);}
37 virtual int_type overflow(int_type c = traits_type::eof()) {return base::overflow(c);}
40 int main(int, char**)
43 test_buf<char> f;
44 assert(f.open("overflow.dat", std::ios_base::out) != 0);
45 assert(f.is_open());
46 assert(f.pbase() == 0);
47 assert(f.pptr() == 0);
48 assert(f.epptr() == 0);
49 assert(f.overflow('a') == 'a');
50 assert(f.pbase() != 0);
51 assert(f.pptr() == f.pbase());
52 assert(f.epptr() - f.pbase() == 4095);
55 test_buf<char> f;
56 assert(f.open("overflow.dat", std::ios_base::in) != 0);
57 assert(f.is_open());
58 assert(f.sgetc() == 'a');
60 std::remove("overflow.dat");
62 test_buf<char> f;
63 f.pubsetbuf(0, 0);
64 assert(f.open("overflow.dat", std::ios_base::out) != 0);
65 assert(f.is_open());
66 assert(f.pbase() == 0);
67 assert(f.pptr() == 0);
68 assert(f.epptr() == 0);
69 assert(f.overflow('a') == 'a');
70 assert(f.pbase() == 0);
71 assert(f.pptr() == 0);
72 assert(f.epptr() == 0);
75 test_buf<char> f;
76 assert(f.open("overflow.dat", std::ios_base::in) != 0);
77 assert(f.is_open());
78 assert(f.sgetc() == 'a');
80 std::remove("overflow.dat");
82 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
84 test_buf<wchar_t> f;
85 assert(f.open("overflow.dat", std::ios_base::out) != 0);
86 assert(f.is_open());
87 assert(f.pbase() == 0);
88 assert(f.pptr() == 0);
89 assert(f.epptr() == 0);
90 assert(f.overflow(L'a') == L'a');
91 assert(f.pbase() != 0);
92 assert(f.pptr() == f.pbase());
93 assert(f.epptr() - f.pbase() == 4095);
96 test_buf<wchar_t> f;
97 assert(f.open("overflow.dat", std::ios_base::in) != 0);
98 assert(f.is_open());
99 assert(f.sgetc() == L'a');
101 std::remove("overflow.dat");
103 test_buf<wchar_t> f;
104 f.pubsetbuf(0, 0);
105 assert(f.open("overflow.dat", std::ios_base::out) != 0);
106 assert(f.is_open());
107 assert(f.pbase() == 0);
108 assert(f.pptr() == 0);
109 assert(f.epptr() == 0);
110 assert(f.overflow(L'a') == L'a');
111 assert(f.pbase() == 0);
112 assert(f.pptr() == 0);
113 assert(f.epptr() == 0);
116 test_buf<wchar_t> f;
117 assert(f.open("overflow.dat", std::ios_base::in) != 0);
118 assert(f.is_open());
119 assert(f.sgetc() == L'a');
121 std::remove("overflow.dat");
123 test_buf<wchar_t> f;
124 f.pubimbue(std::locale(LOCALE_en_US_UTF_8));
125 assert(f.open("overflow.dat", std::ios_base::out) != 0);
126 assert(f.sputc(0x4E51) == 0x4E51);
127 assert(f.sputc(0x4E52) == 0x4E52);
128 assert(f.sputc(0x4E53) == 0x4E53);
131 test_buf<char> f;
132 assert(f.open("overflow.dat", std::ios_base::in) != 0);
133 assert(f.is_open());
134 assert(f.sbumpc() == 0xE4);
135 assert(f.sbumpc() == 0xB9);
136 assert(f.sbumpc() == 0x91);
137 assert(f.sbumpc() == 0xE4);
138 assert(f.sbumpc() == 0xB9);
139 assert(f.sbumpc() == 0x92);
140 assert(f.sbumpc() == 0xE4);
141 assert(f.sbumpc() == 0xB9);
142 assert(f.sbumpc() == 0x93);
143 assert(f.sbumpc() == -1);
145 std::remove("overflow.dat");
146 #endif // TEST_HAS_NO_WIDE_CHARACTERS
148 return 0;