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 // XFAIL: LIBCXX-WINDOWS-FIXME
12 
13 // <fstream>
14 
15 // int_type overflow(int_type c = traits::eof());
16 
17 // This test is not entirely portable
18 
19 #include <fstream>
20 #include <cassert>
21 
22 #include "test_macros.h"
23 #include "platform_support.h" // locale name macros
24 
25 template <class CharT>
26 struct test_buf
27     : public std::basic_filebuf<CharT>
28 {
29     typedef std::basic_filebuf<CharT>  base;
30     typedef typename base::char_type   char_type;
31     typedef typename base::int_type    int_type;
32     typedef typename base::traits_type traits_type;
33 
34     char_type* pbase() const {return base::pbase();}
35     char_type* pptr()  const {return base::pptr();}
36     char_type* epptr() const {return base::epptr();}
37     void gbump(int n) {base::gbump(n);}
38 
39     virtual int_type overflow(int_type c = traits_type::eof()) {return base::overflow(c);}
40 };
41 
42 int main(int, char**)
43 {
44     {
45         test_buf<char> f;
46         assert(f.open("overflow.dat", std::ios_base::out) != 0);
47         assert(f.is_open());
48         assert(f.pbase() == 0);
49         assert(f.pptr() == 0);
50         assert(f.epptr() == 0);
51         assert(f.overflow('a') == 'a');
52         assert(f.pbase() != 0);
53         assert(f.pptr() == f.pbase());
54         assert(f.epptr() - f.pbase() == 4095);
55     }
56     {
57         test_buf<char> f;
58         assert(f.open("overflow.dat", std::ios_base::in) != 0);
59         assert(f.is_open());
60         assert(f.sgetc() == 'a');
61     }
62     std::remove("overflow.dat");
63     {
64         test_buf<char> f;
65         f.pubsetbuf(0, 0);
66         assert(f.open("overflow.dat", std::ios_base::out) != 0);
67         assert(f.is_open());
68         assert(f.pbase() == 0);
69         assert(f.pptr() == 0);
70         assert(f.epptr() == 0);
71         assert(f.overflow('a') == 'a');
72         assert(f.pbase() == 0);
73         assert(f.pptr() == 0);
74         assert(f.epptr() == 0);
75     }
76     {
77         test_buf<char> f;
78         assert(f.open("overflow.dat", std::ios_base::in) != 0);
79         assert(f.is_open());
80         assert(f.sgetc() == 'a');
81     }
82     std::remove("overflow.dat");
83 
84 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
85     {
86         test_buf<wchar_t> f;
87         assert(f.open("overflow.dat", std::ios_base::out) != 0);
88         assert(f.is_open());
89         assert(f.pbase() == 0);
90         assert(f.pptr() == 0);
91         assert(f.epptr() == 0);
92         assert(f.overflow(L'a') == L'a');
93         assert(f.pbase() != 0);
94         assert(f.pptr() == f.pbase());
95         assert(f.epptr() - f.pbase() == 4095);
96     }
97     {
98         test_buf<wchar_t> f;
99         assert(f.open("overflow.dat", std::ios_base::in) != 0);
100         assert(f.is_open());
101         assert(f.sgetc() == L'a');
102     }
103     std::remove("overflow.dat");
104     {
105         test_buf<wchar_t> f;
106         f.pubsetbuf(0, 0);
107         assert(f.open("overflow.dat", std::ios_base::out) != 0);
108         assert(f.is_open());
109         assert(f.pbase() == 0);
110         assert(f.pptr() == 0);
111         assert(f.epptr() == 0);
112         assert(f.overflow(L'a') == L'a');
113         assert(f.pbase() == 0);
114         assert(f.pptr() == 0);
115         assert(f.epptr() == 0);
116     }
117     {
118         test_buf<wchar_t> f;
119         assert(f.open("overflow.dat", std::ios_base::in) != 0);
120         assert(f.is_open());
121         assert(f.sgetc() == L'a');
122     }
123     std::remove("overflow.dat");
124     {
125         test_buf<wchar_t> f;
126         f.pubimbue(std::locale(LOCALE_en_US_UTF_8));
127         assert(f.open("overflow.dat", std::ios_base::out) != 0);
128         assert(f.sputc(0x4E51) == 0x4E51);
129         assert(f.sputc(0x4E52) == 0x4E52);
130         assert(f.sputc(0x4E53) == 0x4E53);
131     }
132     {
133         test_buf<char> f;
134         assert(f.open("overflow.dat", std::ios_base::in) != 0);
135         assert(f.is_open());
136         assert(f.sbumpc() == 0xE4);
137         assert(f.sbumpc() == 0xB9);
138         assert(f.sbumpc() == 0x91);
139         assert(f.sbumpc() == 0xE4);
140         assert(f.sbumpc() == 0xB9);
141         assert(f.sbumpc() == 0x92);
142         assert(f.sbumpc() == 0xE4);
143         assert(f.sbumpc() == 0xB9);
144         assert(f.sbumpc() == 0x93);
145         assert(f.sbumpc() == -1);
146     }
147     std::remove("overflow.dat");
148 #endif // TEST_HAS_NO_WIDE_CHARACTERS
149 
150   return 0;
151 }
152