overflow.pass.cpp revision f5256e16dfc425c1d466f6308d4026d529ce9e0b
1//===----------------------------------------------------------------------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10// <fstream>
11
12// int_type overflow(int_type c = traits::eof());
13
14// This test is not entirely portable
15
16#include <fstream>
17#include <cassert>
18
19template <class CharT>
20struct test_buf
21    : public std::basic_filebuf<CharT>
22{
23    typedef std::basic_filebuf<CharT>  base;
24    typedef typename base::char_type   char_type;
25    typedef typename base::int_type    int_type;
26    typedef typename base::traits_type traits_type;
27
28    char_type* pbase() const {return base::pbase();}
29    char_type* pptr()  const {return base::pptr();}
30    char_type* epptr() const {return base::epptr();}
31    void gbump(int n) {base::gbump(n);}
32
33    virtual int_type overflow(int_type c = traits_type::eof()) {return base::overflow(c);}
34};
35
36int main()
37{
38    {
39        test_buf<char> f;
40        assert(f.open("overflow.dat", std::ios_base::out) != 0);
41        assert(f.is_open());
42        assert(f.pbase() == 0);
43        assert(f.pptr() == 0);
44        assert(f.epptr() == 0);
45        assert(f.overflow('a') == 'a');
46        assert(f.pbase() != 0);
47        assert(f.pptr() == f.pbase());
48        assert(f.epptr() - f.pbase() == 4095);
49    }
50    {
51        test_buf<char> f;
52        assert(f.open("overflow.dat", std::ios_base::in) != 0);
53        assert(f.is_open());
54        assert(f.sgetc() == 'a');
55    }
56    std::remove("overflow.dat");
57    {
58        test_buf<char> f;
59        f.pubsetbuf(0, 0);
60        assert(f.open("overflow.dat", std::ios_base::out) != 0);
61        assert(f.is_open());
62        assert(f.pbase() == 0);
63        assert(f.pptr() == 0);
64        assert(f.epptr() == 0);
65        assert(f.overflow('a') == 'a');
66        assert(f.pbase() == 0);
67        assert(f.pptr() == 0);
68        assert(f.epptr() == 0);
69    }
70    {
71        test_buf<char> f;
72        assert(f.open("overflow.dat", std::ios_base::in) != 0);
73        assert(f.is_open());
74        assert(f.sgetc() == 'a');
75    }
76    std::remove("overflow.dat");
77    {
78        test_buf<wchar_t> f;
79        assert(f.open("overflow.dat", std::ios_base::out) != 0);
80        assert(f.is_open());
81        assert(f.pbase() == 0);
82        assert(f.pptr() == 0);
83        assert(f.epptr() == 0);
84        assert(f.overflow(L'a') == L'a');
85        assert(f.pbase() != 0);
86        assert(f.pptr() == f.pbase());
87        assert(f.epptr() - f.pbase() == 4095);
88    }
89    {
90        test_buf<wchar_t> f;
91        assert(f.open("overflow.dat", std::ios_base::in) != 0);
92        assert(f.is_open());
93        assert(f.sgetc() == L'a');
94    }
95    std::remove("overflow.dat");
96    {
97        test_buf<wchar_t> f;
98        f.pubsetbuf(0, 0);
99        assert(f.open("overflow.dat", std::ios_base::out) != 0);
100        assert(f.is_open());
101        assert(f.pbase() == 0);
102        assert(f.pptr() == 0);
103        assert(f.epptr() == 0);
104        assert(f.overflow(L'a') == L'a');
105        assert(f.pbase() == 0);
106        assert(f.pptr() == 0);
107        assert(f.epptr() == 0);
108    }
109    {
110        test_buf<wchar_t> f;
111        assert(f.open("overflow.dat", std::ios_base::in) != 0);
112        assert(f.is_open());
113        assert(f.sgetc() == L'a');
114    }
115    std::remove("overflow.dat");
116    {
117        test_buf<wchar_t> f;
118        f.pubimbue(std::locale("en_US.UTF-8"));
119        assert(f.open("overflow.dat", std::ios_base::out) != 0);
120        assert(f.sputc(0x4E51) == 0x4E51);
121        assert(f.sputc(0x4E52) == 0x4E52);
122        assert(f.sputc(0x4E53) == 0x4E53);
123    }
124    {
125        test_buf<char> f;
126        assert(f.open("overflow.dat", std::ios_base::in) != 0);
127        assert(f.is_open());
128        assert(f.sbumpc() == 0xE4);
129        assert(f.sbumpc() == 0xB9);
130        assert(f.sbumpc() == 0x91);
131        assert(f.sbumpc() == 0xE4);
132        assert(f.sbumpc() == 0xB9);
133        assert(f.sbumpc() == 0x92);
134        assert(f.sbumpc() == 0xE4);
135        assert(f.sbumpc() == 0xB9);
136        assert(f.sbumpc() == 0x93);
137        assert(f.sbumpc() == -1);
138    }
139    std::remove("overflow.dat");
140}
141