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         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 
147   return 0;
148 }
149