]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/27_io/basic_filebuf/underflow/wchar_t/11603.cc
Licensing changes to GPLv3 resp. GPLv3 with GCC Runtime Exception.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 27_io / basic_filebuf / underflow / wchar_t / 11603.cc
1 // Copyright (C) 2003, 2004, 2009 Free Software Foundation, Inc.
2 //
3 // This file is part of the GNU ISO C++ Library. This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 3, or (at your option)
7 // any later version.
8
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING3. If not see
16 // <http://www.gnu.org/licenses/>.
17
18 // 27.8.1.4 Overridden virtual functions
19
20 #include <fstream>
21 #include <locale>
22 #include <testsuite_hooks.h>
23
24 template <typename InternT, typename StateT = std::mbstate_t>
25 class checksumcvt : public std::codecvt<InternT, char, StateT>
26 {
27 typedef std::codecvt<InternT, char, StateT> Base;
28 static const std::size_t width = sizeof(InternT) + 1;
29
30 public:
31 typedef InternT intern_type;
32 typedef char extern_type;
33
34 explicit checksumcvt(std::size_t refs = 0)
35 : Base(refs)
36 { }
37
38 protected:
39 virtual std::codecvt_base::result
40 do_out(StateT&, const intern_type* from,
41 const intern_type* from_end, const intern_type*& from_next,
42 extern_type* to, extern_type* to_end,
43 extern_type*& to_next) const
44 {
45 size_t len = std::min(static_cast<size_t>(from_end - from),
46 static_cast<size_t>(to_end - to) / width);
47
48 while (len--)
49 {
50 const char* p =
51 reinterpret_cast<const char*>(from);
52 unsigned char checksum = 0;
53
54 for (std::size_t i = 0; i < sizeof(intern_type); ++i)
55 {
56 *to++ = p[i];
57 checksum ^= static_cast<unsigned char>(p[i]);
58 }
59
60 *to++ = checksum;
61 ++from;
62 }
63
64 from_next = from;
65 to_next = to;
66 return from_next == from_end ? std::codecvt_base::ok
67 : std::codecvt_base::partial;
68 }
69
70 virtual std::codecvt_base::result
71 do_unshift(StateT&, extern_type* to,
72 extern_type*, extern_type*& to_next) const
73 {
74 to_next = to;
75 return std::codecvt_base::ok;
76 }
77
78 virtual std::codecvt_base::result
79 do_in(StateT&, const extern_type* from,
80 const extern_type* from_end, const extern_type*& from_next,
81 intern_type* to, intern_type* to_end,
82 intern_type*& to_next) const
83 {
84 size_t len = std::min(static_cast<size_t>(to_end - to),
85 static_cast<size_t>(from_end - from) / width);
86
87 while (len)
88 {
89 const char* f = from;
90 intern_type tmp;
91 char* p = reinterpret_cast<char*>(&tmp);
92 unsigned char checksum = 0;
93
94 for (std::size_t i = 0; i < sizeof(intern_type); ++i)
95 {
96 p[i] = *f;
97 checksum ^= static_cast<unsigned char>(*f++);
98 }
99
100 if (*f++ != checksum)
101 break;
102
103 from = f;
104 *to++ = tmp;
105 len--;
106 }
107
108 from_next = from;
109 to_next = to;
110 return len ? std::codecvt_base::error :
111 (from_next == from_end ? std::codecvt_base::ok
112 : std::codecvt_base::partial);
113 }
114
115 virtual int
116 do_encoding() const throw()
117 { return width; }
118
119 virtual int
120 do_length(const StateT&, const extern_type* from,
121 const extern_type* end, size_t max) const
122 {
123 size_t len = std::min(max,
124 static_cast<size_t>(end - from) / width);
125
126 int ret = 0;
127 while (len--)
128 {
129 unsigned char checksum = 0;
130
131 for (std::size_t i = 0; i < sizeof(intern_type); ++i)
132 {
133 checksum ^= static_cast<unsigned char>(*from++);
134 }
135
136 if (*from++ != checksum)
137 break;
138
139 ret++;
140 }
141
142 return ret;
143 }
144
145 virtual int
146 do_max_length() const throw()
147 { return width; }
148
149 virtual bool
150 do_always_noconv() const throw()
151 { return false; }
152 };
153
154 class Buf : public std::wfilebuf
155 {
156 public:
157 std::streamsize pub_showmanyc()
158 { return showmanyc(); }
159 std::wfilebuf::int_type pub_underflow()
160 { return underflow(); }
161 };
162
163 // libstdc++/11603
164 void test01()
165 {
166 using namespace std;
167 bool test __attribute__((unused)) = true;
168
169 filebuf fbout;
170 fbout.open("tmp_11603", ios_base::out);
171 fbout.sputn("aaaab", 5);
172 fbout.close();
173
174 locale loc(locale::classic(), new checksumcvt<wchar_t>);
175
176 Buf fb;
177 fb.pubimbue(loc);
178 fb.open("tmp_11603", ios_base::in);
179 VERIFY( fb.pub_showmanyc() == 1 );
180
181 try
182 {
183 wfilebuf::int_type ret = fb.pub_underflow();
184 VERIFY( ret != wfilebuf::traits_type::eof() );
185 fb.sbumpc();
186 ret = fb.pub_underflow();
187 VERIFY( ret == wfilebuf::traits_type::eof() );
188 }
189 catch (...)
190 { }
191
192 fb.close();
193 }
194
195 int main()
196 {
197 test01();
198 return 0;
199 }