1 //===----------------------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 // <locale>
11 
12 // wbuffer_convert<Codecvt, Elem, Tr>
13 
14 // int_type overflow(int_type c = traits::eof());
15 
16 // This test is not entirely portable
17 
18 #include <locale>
19 #include <codecvt>
20 #include <fstream>
21 #include <cassert>
22 
23 struct test_buf
24     : public std::wbuffer_convert<std::codecvt_utf8<wchar_t> >
25 {
26     typedef std::wbuffer_convert<std::codecvt_utf8<wchar_t> > base;
27     typedef base::char_type   char_type;
28     typedef base::int_type    int_type;
29     typedef base::traits_type traits_type;
30 
31     explicit test_buf(std::streambuf* sb) : base(sb) {}
32 
33     char_type* pbase() const {return base::pbase();}
34     char_type* pptr()  const {return base::pptr();}
35     char_type* epptr() const {return base::epptr();}
36     void gbump(int n) {base::gbump(n);}
37 
38     virtual int_type overflow(int_type c = traits_type::eof()) {return base::overflow(c);}
39 };
40 
41 int main()
42 {
43     {
44         std::ofstream bs("overflow.dat");
45         test_buf f(bs.rdbuf());
46         assert(f.pbase() == 0);
47         assert(f.pptr() == 0);
48         assert(f.epptr() == 0);
49         assert(f.overflow(L'a') == L'a');
50         assert(f.pbase() != 0);
51         assert(f.pptr() == f.pbase());
52         assert(f.epptr() - f.pbase() == 4095);
53     }
54     {
55         std::ifstream bs("overflow.dat");
56         test_buf f(bs.rdbuf());
57         assert(f.sgetc() == L'a');
58     }
59     std::remove("overflow.dat");
60     {
61         std::ofstream bs("overflow.dat");
62         test_buf f(bs.rdbuf());
63         f.pubsetbuf(0, 0);
64         assert(f.pbase() == 0);
65         assert(f.pptr() == 0);
66         assert(f.epptr() == 0);
67         assert(f.overflow('a') == 'a');
68         assert(f.pbase() == 0);
69         assert(f.pptr() == 0);
70         assert(f.epptr() == 0);
71     }
72     {
73         std::ifstream bs("overflow.dat");
74         test_buf f(bs.rdbuf());
75         assert(f.sgetc() == L'a');
76     }
77     std::remove("overflow.dat");
78     {
79         std::ofstream bs("overflow.dat");
80         test_buf f(bs.rdbuf());
81         assert(f.sputc(0x4E51) == 0x4E51);
82         assert(f.sputc(0x4E52) == 0x4E52);
83         assert(f.sputc(0x4E53) == 0x4E53);
84     }
85     {
86         std::ifstream f("overflow.dat");
87         assert(f.is_open());
88         assert(f.get() == 0xE4);
89         assert(f.get() == 0xB9);
90         assert(f.get() == 0x91);
91         assert(f.get() == 0xE4);
92         assert(f.get() == 0xB9);
93         assert(f.get() == 0x92);
94         assert(f.get() == 0xE4);
95         assert(f.get() == 0xB9);
96         assert(f.get() == 0x93);
97         assert(f.get() == -1);
98     }
99     std::remove("overflow.dat");
100 }
101