xref: /llvm-project/libcxx/test/std/input.output/file.streams/fstreams/filebuf.virtuals/overflow.pass.cpp (revision 38d25aecdf72177d72ed40f3dfbbf1d3c726dc8f)
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 // REQUIRES: locale.en_US.UTF-8
10 
11 // <fstream>
12 
13 // int_type overflow(int_type c = traits::eof());
14 
15 // This test is not entirely portable
16 
17 #include <fstream>
18 #include <cassert>
19 
20 #include "test_macros.h"
21 #include "platform_support.h" // locale name macros
22 
23 template <class CharT>
24 struct test_buf
25     : public std::basic_filebuf<CharT>
26 {
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;
31 
pbasetest_buf32     char_type* pbase() const {return base::pbase();}
pptrtest_buf33     char_type* pptr()  const {return base::pptr();}
epptrtest_buf34     char_type* epptr() const {return base::epptr();}
gbumptest_buf35     void gbump(int n) {base::gbump(n);}
36 
overflowtest_buf37     virtual int_type overflow(int_type c = traits_type::eof()) {return base::overflow(c);}
38 };
39 
main(int,char **)40 int main(int, char**)
41 {
42     {
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);
53     }
54     {
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');
59     }
60     std::remove("overflow.dat");
61     {
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);
73     }
74     {
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');
79     }
80     std::remove("overflow.dat");
81 
82 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
83     {
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);
94     }
95     {
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');
100     }
101     std::remove("overflow.dat");
102     {
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);
114     }
115     {
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');
120     }
121     std::remove("overflow.dat");
122     {
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);
129     }
130     {
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);
144     }
145     std::remove("overflow.dat");
146 #endif // TEST_HAS_NO_WIDE_CHARACTERS
147 
148   return 0;
149 }
150