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